var FB = {

	containerURL : '/parts/container.swf',
	
	init : function () {
		var bodyEl = document.getElementsByTagName("body").item(0);
		
		var outerContainer = document.createElement("div");
		outerContainer.setAttribute('id', 'outer-container');
		outerContainer.style.display = 'none';
		outerContainer.style.position = 'absolute';
		outerContainer.style.top = '0';
		outerContainer.style.left = '0';
		outerContainer.style.zIndex = '30000';
		outerContainer.style.width = '100%';
		outerContainer.style.height = '500px';
		outerContainer.style.overflow = 'hidden';
		bodyEl.appendChild(outerContainer);
		
		var bgContainer = document.createElement("div");
		bgContainer.setAttribute('id', 'bg-container');
		bgContainer.style.position = 'absolute';
		bgContainer.style.zIndex = '0';
		bgContainer.style.width = '100%';
		bgContainer.style.height = '100%';
		bgContainer.style.top = '0';
		bgContainer.style.left = '0';
		bgContainer.style.background = '#000000';
		bgContainer.style.opacity = '0.7';
		outerContainer.appendChild(bgContainer);
		
		var container = document.createElement("div");
		container.setAttribute('id', 'container');
		container.style.position = 'absolute';
		container.style.textAlign = 'center';
		container.style.lineHeight = '0';
		container.style.zIndex = '1';
		outerContainer.appendChild(container);
		
		var fbContainer = document.createElement("div");
		fbContainer.setAttribute('id', 'fbcontainer');
		fbContainer.style.position = 'absolute';
		fbContainer.style.margin = '0 auto';
		container.appendChild(fbContainer);
		
		var tags = document.getElementsByTagName("a");
		for (var i=0; i<tags.length; i++) {
			if (tags[i].className.indexOf("flashbox") > -1) {
				addEvent(tags[i], 'click', FB.show);
				tags[i].onclick = function() {return false;};
			}
		}
	},
	
	hide : function() {
		var outerContainerEl = document.getElementById("outer-container");
		outerContainerEl.style.display = 'none';
		
		var fbcontainer = document.getElementById("fbcontainer");
		var swfContainer = document.getElementById("swfcontainer");
		fbcontainer.removeChild(swfContainer);
	},
	
	show : function() {
		var params_array = this.getAttribute('rel').split(' ');
		var swfW = Number(params_array[0]);
		var swfH = Number(params_array[1]);
		var swfURL = this.getAttribute('href').toString();
			
		var pageW = FB.getPageSize()[0];
		var pageH = FB.getPageSize()[1];
		var winW = FB.getPageSize()[2];
		var winH = FB.getPageSize()[3];
		
		var outerContainer = document.getElementById("outer-container");
		var containerHeight = (pageH < swfH + 60) ?  Number(swfH) + 60 : pageH;
		outerContainer.style.height = containerHeight + "px";
		outerContainer.style.display = 'block';
		
		var bgContainer = document.getElementById("bg-container");
		bgContainer.style.height = containerHeight + "px";
		
		var container = document.getElementById("container");
		var containerLeft = Math.floor((winW - 2000) / 2);
		container.style.left = containerLeft + "px";
		
		var containerTop = FB.getScrollTop() + Math.floor((winH - 2000) / 2);
		if ((swfH - winH) / 2 > 0) containerTop = FB.getScrollTop() + Math.floor((swfH - 2000) / 2) + 30;
		container.style.top = containerTop + "px";
		
		var fbContainer = document.getElementById("fbcontainer");
		var swfContainer = document.createElement("div");
		swfContainer.setAttribute('id', 'swfcontainer');
		fbContainer.appendChild(swfContainer);
		
		var params = {
			quality: 'high',
			wmode: 'transparent'
			};
		var flashvars = {
			theswf: swfURL,
			thewidth: swfW,
			theheight: swfH
			};
		
		swfobject.embedSWF(FB.containerURL, 'swfcontainer', '2000', '2000', '9.0.0', '', flashvars, params);
	},
	
	getScrollTop : function () {
		if (document.all) {
			return (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
		} else {
			return window.pageYOffset;
		}
	},
	
	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;
		}	
		
		// 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
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}


		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
	}

};

addEvent(window, 'load', FB.init);
