// -----------------------------------------------------------------------------------
//
//	Swfbox v2.03.3
//	by Lokesh Dhakar - http://www.huddletogether.com
//	5/21/06
//
//	For more information on this script, visit:
//	http://huddletogether.com/projects/Swfbox2/
//
//	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
//	
//	Credit also due to those who have helped, inspired, and made their code available to the public.
//	Including: Scott Upton(uptonic.com), Peter-Paul Koch(quirksmode.com), Thomas Fuchs(mir.aculo.us), and others.
//
//
// -----------------------------------------------------------------------------------
/*

	Table of Contents
	-----------------
	Configuration
	Global Variables

	Extending Built-in Objects	
	- Object.extend(Element)
	- Array.prototype.removeDuplicates()
	- Array.prototype.empty()

	Swfbox Class Declaration
	- initialize()
	- updateImageList()
	- start()
	- changeImage()
	- resizeImageContainer()
	- showImage()
	- updateDetails()
	- updateNav()
	- enableKeyboardNav()
	- disableKeyboardNav()
	- keyboardAction()
	- preloadNeighborImages()
	- end()
	
	Miscellaneous Functions
	- getPageScroll()
	- getPageSize()
	- getKey()
	- listenKey()
	- showSelectBoxes()
	- hideSelectBoxes()
	- showFlash()
	- hideFlash()
	- pause()
	- initSwfbox()
	
	Function Calls
	- addLoadEvent(initSwfbox)
	
*/
// -----------------------------------------------------------------------------------

//
//	Configuration
//
var fileLoadingImage = "themes/2008/img/loading.gif";		
var fileBottomNavCloseImage = "themes/2008/img/close.gif";

var overlayOpacity = 0.7;	// controls transparency of shadow overlay 

var animate = true;			// toggles resizing animations
var resizeSpeed = 7;		// controls the speed of the image resizing animations (1=slowest and 10=fastest)

var borderSize = 10;		//if you adjust the padding in the CSS, you will need to update this variable

// -----------------------------------------------------------------------------------

//
//	Global Variables
//

if(animate == true){
	overlayDuration = 0.2;	// shadow fade in/out duration
	if(resizeSpeed > 10){ resizeSpeed = 10;}
	if(resizeSpeed < 1){ resizeSpeed = 1;}
	resizeDuration = (11 - resizeSpeed) * 0.15;
} else { 
	overlayDuration = 0;
	resizeDuration = 0;
}

// -----------------------------------------------------------------------------------

//
//	Additional methods for Element added by SU, Couloir
//	- further additions by Lokesh Dhakar (huddletogether.com)
//

Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setLeft: function(element,l) {
	   	element = $(element);
    	element.style.left = l +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});

// -----------------------------------------------------------------------------------

//
//	Extending built-in Array object
//	- array.removeDuplicates()
//	- array.empty()
//
Array.prototype.removeDuplicates = function () {
    for(i = 0; i < this.length; i++){
        for(j = this.length-1; j>i; j--){        
            if(this[i][0] == this[j][0]){
                this.splice(j,1);
            }
        }
    }
}

// -----------------------------------------------------------------------------------

Array.prototype.empty = function () {
	for(i = 0; i <= this.length; i++){
		this.shift();
	}
}

var Swfbox = Class.create();

// -----------------------------------------------------------------------------------
Swfbox.prototype = {

	initialize: function(){
		
		this.updateImageList();
		
		var objBody = document.getElementsByTagName("body").item(0);
		
		var objOver = document.createElement("div");
		objOver.setAttribute('id','over');
		//objOver.style.display = 'none';
		objOver.onclick = function() { mySwfbox.fim(); }
		objBody.appendChild(objOver);
	
	
		var objSwfbox = document.createElement("div");
		objSwfbox.setAttribute('id','swfbox');
		objSwfbox.style.display = 'none';
		objSwfbox.onclick = function(e) {	// close Lightbox is user clicks shadow overlay
			if (!e) var e = window.event;
			var clickObj = Event.element(e).id;
			if ( clickObj == 'swfbox') {
				mySwfbox.fim();
			}
		};
		objBody.appendChild(objSwfbox);
		
		var objOuterImageContainer = document.createElement("div");
		objOuterImageContainer.setAttribute('id','swfShow');
		objSwfbox.appendChild(objOuterImageContainer);
	
		// When Lightbox starts it will resize itself from 250 by 250 to the current image dimension.
		// If animations are turned off, it will be hidden as to prevent a flicker of a
		// white 250 by 250 box.
		//Element.setWidth('outerImageContainer', 310);
		//Element.setHeight('outerImageContainer', 280);			
	
		//var objImageContainer = document.createElement("div");
		//objImageContainer.setAttribute('id','imageContainer');
		//objOuterImageContainer.appendChild(objImageContainer);
	
		// criando as tags para executar arquivo SWF *******************************************************************************
		var obj = document.createElement("object");
//		obj.setAttribute('classid','clsid:D27CDB6E-AE6D-11cf-96B8-444553540000');
//		obj.setAttribute('codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0');
//		obj.setAttribute('id','objSwf');
//		obj.setAttribute('width','310');
//		obj.setAttribute('height','280');
		
		var obparam1 = document.createElement("param");
		obparam1.setAttribute('name','movie');
		obparam1.setAttribute('id','obswf');
		//obparam1.setAttribute('value','swf/video.swf');
		obj.appendChild(obparam1);
		
		var obparam2 = document.createElement("param");
		obparam2.setAttribute('name','quality');
		obparam2.setAttribute('value','high');
		obj.appendChild(obparam2);
		var obparam3 = document.createElement("param");

		obparam3.setAttribute('name','wmode');
		obparam3.setAttribute('value','transparent');
		obj.appendChild(obparam3);
		
		var obEmbed = document.createElement("EMBED");
		obEmbed.setAttribute('id','swfsrc');
		obEmbed.setAttribute('quality','high');
		obEmbed.setAttribute('pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash');
		obEmbed.setAttribute('type','application/x-shockwave-flash');
		obEmbed.setAttribute('width','310');
		obEmbed.setAttribute('height','280');
		//obembed.setAttribute('src','swf/video.swf');
		//obj.appendChild(obEmbed);
		//document.getElementById('objSwf').innerHTML = "<embed></embed>";

	//***************************************************************************************************************************
		arrayPageSize = getPageSize();
		document.getElementById('swfbox').style.position="absolute";
		document.getElementById('swfbox').style.top=(arrayPageSize[1]/2)-140+"px";
		document.getElementById('swfbox').style.width="350px";
		document.getElementById('swfbox').style.height="0px";
		document.getElementById('swfbox').style.left=(arrayPageSize[0]/2)-155+"px";

		objOuterImageContainer.appendChild(obj);
		
		objOuterImageContainer.onclick = function() { mySwfbox.fim(); }
		
		
	},
	updateImageList: function() {	
		//return false;
		if (!document.getElementsByTagName){ return; }
		var anchors = document.getElementsByTagName('a');
		var areas = document.getElementsByTagName('area');

		// loop through all anchor tags
		for (var i=0; i<anchors.length; i++){
			var anchor = anchors[i];
			
			var relAttribute = String(anchor.getAttribute('rel'));
			
			// use the string.match() method to catch 'lightbox' references in the rel attribute
			if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('swfbox'))){
				anchor.onclick = function () {mySwfbox.start(this); return false;}
			}
		}
		for (var i=0; i< areas.length; i++){
			var area = areas[i];
			
			var relAttribute = String(area.getAttribute('rel'));
			
			// use the string.match() method to catch 'lightbox' references in the rel attribute
			if (area.getAttribute('href') && (relAttribute.toLowerCase().match('swfbox'))){
				area.onclick = function () {mySwfbox.start(this); return false;}
			}
		}
	},
	start: function(imageLink){
	//	
		
		//hideSelectBoxes();
		//hideFlash();
		arrayPageSize = getPageSize();
		
		Element.setWidth('over', arrayPageSize[0]);
		Element.setHeight('over',arrayPageSize[1]);
		document.getElementById('over').style.position = 'absolute';
		document.getElementById('over').style.top = 0;
		document.getElementById('over').style.backgroundColor = "#000000";
		new Effect.Appear('over', { duration: overlayDuration, from: 0.0, to: overlayOpacity });
		
		if (!document.getElementsByTagName){ return; }
		var anchors = document.getElementsByTagName( imageLink.tagName);
		if((imageLink.getAttribute('rel') == 'swfbox')){
			// add single image to imageArray
			//document.getElementById('obswf').value = imageLink.getAttribute('href');
			monta_swf = "";
			monta_swf += "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0\" width=\"310\" height=\"280\" title=\"\">";
			monta_swf += "<param name=\"movie\" value=\""+ imageLink.getAttribute('href') +"\" />";
			monta_swf += "<param name=\"quality\" value=\"high\" />";
			monta_swf += "<param name=\"menu\" value=\"0\" />";
			//monta_swf += "<param name=\"wmode\" value=\"transparent\" />";
			monta_swf += "<embed src=\""+ imageLink.getAttribute('href') +"\" quality=\"high\" pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\" width=\"310\" height=\"280\"></embed>";
			monta_swf += "</object>";	
			//alert(monta_swf);

			document.getElementById('swfbox').innerHTML = monta_swf;
			document.getElementById('swfbox').style.zIndex = 100000;
			//swfsrc
			//alert(document.body.swfsrc.id);
			//return false;
			//document.getElementById('swfsrc').value = imageLink.getAttribute('href');
			imageArray.push(new Array(imageLink.getAttribute('href'), imageLink.getAttribute('title')));			
		} 
		else {
			
			// if image is part of a set..
			// loop through anchors, find other images in set, and add them to imageArray
			for (var i=0; i<anchors.length; i++){
				var anchor = anchors[i];
				if (anchor.getAttribute('href') && (anchor.getAttribute('rel') == imageLink.getAttribute('rel'))){
					imageArray.push(new Array(anchor.getAttribute('href'), anchor.getAttribute('title')));
				}
			}
			imageArray.removeDuplicates();
			while(imageArray[imageNum][0] != imageLink.getAttribute('href')) { imageNum++;}
		}

		var arrayPageScroll = getPageScroll();
		var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
		var lightboxLeft = arrayPageScroll[0];
		//Element.setTop('swfbox', lightboxTop);
		//Element.setLeft('swfbox', lightboxLeft);
		//Element.setTop('swfbox', lightboxTop);
		//Element.setLeft('swfbox', lightboxLeft);
		Element.show('swfbox');
	},
	fim: function() {
		//this.disableKeyboardNav();
		//document.getElementById("swfShow").style.visibility = "hidden";
		document.getElementById('swfbox').innerHTML = "";
		Element.hide('swfbox');
		//Element.hide('swfShow');
		new Effect.Fade('over', { duration: overlayDuration});
		//showSelectBoxes();
		//showFlash();
	}
}
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		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;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			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;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
		
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}
function listenKey () {	document.onkeypress = getKey; }
	
// ---------------------------------------------------

function showSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

// ---------------------------------------------------

function showFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i < flashObjects.length; i++) {
		flashObjects[i].style.visibility = "visible";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for (i = 0; i < flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i < flashObjects.length; i++) {
		flashObjects[i].style.visibility = "hidden";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for (i = 0; i < flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "hidden";
	}

}


function initSwfbox() { mySwfbox = new Swfbox(); }
Event.observe(window, 'load', initSwfbox, false);
