function Gallery()
{
    if (Gallery._instance == null || Gallery._instance == undefined) {
        Gallery._instance = this;
    }
    this.container          = null;
    this.contentLayout      = null;
    this.contentFrame      = null;
    this.isVisible  = false;
    this.init();
}

// 'private' static var containing singleton instance
Gallery._instance = null;
Gallery.CONTAINER_ID = 'gallery';
Gallery.LAYOUTER_ID  = Gallery.CONTAINER_ID + 'Wrapper';
Gallery.IFRAME_ID    = Gallery.CONTAINER_ID + 'Content';
Gallery.BASE_PATH = '/specials/rockamring2009';
Gallery.SCRIPT_PATH  = Gallery.BASE_PATH + '/__inc/gallery/contentpage.php';
Gallery.EMPTY_URL = 'about:blank';


// Singleton
Gallery.getInstance = function()
{
    if (Gallery._instance == null || Gallery._instance == undefined) {
        Gallery._instance = new Gallery();
    }
    return Gallery._instance;
}


Gallery.prototype.init = function()
{
    if (!document.getElementById) {
        return;
    }
    this.container = document.getElementById(Gallery.CONTAINER_ID);
    if (this.container) {
        this.container.__galleryRef__ = this;
        var pageSize = Gallery.getPageSize();
        this.container.style.height = pageSize[1] + "px";
        this.container.onclick = function()
        {
            Gallery.getInstance().hide();
        }
        this.contentLayout = document.getElementById(Gallery.LAYOUTER_ID);
        this.contentFrame = document.getElementById(Gallery.IFRAME_ID);
        if (!this.isVisible) {
            this.hide()
        }
    }
}


Gallery.prototype.show = function(settings)
{
    if (this.container) {
        if (settings != null && settings.length > 0) {
            var pageSize = Gallery.getPageSize();
            this.contentLayout.style.top = (pageSize[3] / 5) + Gallery.getPageYScroll() + "px"
            this.container.style.display = 'block';
            var path = Gallery.SCRIPT_PATH + '?' + settings;
            if (path != this.contentFrame.src) {
                this.contentFrame.src = path;
            }
            this.isVisible = true;
        }
		var objects = document.getElementsByTagName('object');
		for (var o = 0; o < objects.length; ++o) {
			objects[o].style.visibility = 'hidden';
		}
		var embeds = document.getElementsByTagName('embed');
		for (var e = 0; e < embeds.length; ++e) {
			embeds[e].style.visibility = 'hidden';
		}
    }
}


Gallery.prototype.hide = function()
{
    if (this.container) {
        this.container.style.display = 'none';
        this.isVisible = false;
        this.contentFrame.src = Gallery.EMPTY_URL;
		var objects = document.getElementsByTagName('object');
		for (var o = 0; o < objects.length; ++o) {
			objects[o].style.visibility = 'visible';
		}
		var embeds = document.getElementsByTagName('embed');
		for (var e = 0; e < embeds.length; ++e) {
			embeds[e].style.visibility = 'visible';
		}
    }
}

Gallery.prototype.setHeight = function(size)
{
    if (this.container) {
        this.contentLayout.style.height = (size + 5) + 'px';
        this.contentFrame.style.height = (size + 5) + 'px';
    }
}

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
Gallery.getPageSize = function()
{
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
    var pageHeight = 0;
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
    var pageWidth = 0;
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	return new Array(pageWidth,pageHeight,windowWidth,windowHeight)
}


Gallery.getPageYScroll = function()
{
	var yScroll = 0;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
    return yScroll;
}

Gallery.prototype.hideHTMLObjects = function()
{
	var objects = document.getElementsByTagName('object');
	for (var o = 0; o < objects.length; ++o) {
		objects[0].style.visibility = 'hidden';
	}
}


Gallery.prototype.restoreHTMLObjects = function()
{
	var objects = document.getElementsByTagName('object');
	for (var o = 0; o < objects.length; ++o) {
		objects[0].style.visibility = 'visible';
	}
}

