// -----------------------------------------------------------------------------------
//
//	Cargando v1.00.0
//	by Ing. Roberto Felipe Gimenez - http://www.gimenez.com.ar
//	30/01/08
//
//	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
//	
// -----------------------------------------------------------------------------------
/*

	Table of Contents
	-----------------
	Configuration
	Global Variables

	Extending Built-in Objects	
	- Object.extend(Element)
	- Array.prototype.removeDuplicates()
	- Array.prototype.empty()

	Cargando Class Declaration
	- initialize()
	- updateImageList()
	- start()
	- changeImage()
	- resizeOuterContenido()
	- showContenido()
	- updateDetails()
	- updateNav()
	- enableKeyboardNav()
	- disableKeyboardNav()
	- keyboardAction()
	- preloadNeighborImages()
	- end()
	
	Miscellaneous Functions
	- getPageScroll()
	- getPageSize()
	- getKey()
	- listenKey()
	- showSelectBoxes()
	- hideSelectBoxes()
	- showFlash()
	- hideFlash()
	- pause()
	- initCargando()
	
	Function Calls
	- addLoadEvent(initCargando)
	
*/
// -----------------------------------------------------------------------------------

//
//	Configuracion
//
var ImagenCargando = "images/loading.gif";		
var ImagenCargandow = 16;		
var ImagenCargandoh = 16;		
var fileBottomNavCloseImage = "images/closelabel.gif";
var fileBottomNavCloseImagew = 16;		
var fileBottomNavCloseImageh = 16;		
var outerContenidoWidth = 350;		
var Opacidad = 0.8;	// controla transparencia de la sombra
var overlayDuration = 0;
var animate = false;			// 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
var http_request = false;
   
// -----------------------------------------------------------------------------------

//
//	Global Variables
//
var imageArray = new Array;
var activeImage;

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;
	},
	appendChild: function(element,obj) {
		element = $(element);
		element.appendChild(obj);
	}
});

var Cargando = Class.create();

Cargando.prototype = {
	
	// initialize()
	// Constructor runs on completion of the DOM loading. Calls updateImageList and then
	// the function inserts html at the bottom of the page which is used to display the shadow 
	// overlay and the image container.
	//
	initialize: function() {	
		
		// this.updateImageList();

		// Code inserts html at the bottom of the page that looks similar to this:
		//
		//	<div id="overlay"></div>
		//	<div id="Cargando">
		//		<div id="outerContenido">
		//			<div id="DivClose"><img src="images/close.gif"></div>
		//			<div id="Contenido"></div>
		//		</div>
		//		<div id="loading">
		//			<img id="ImagenCargando" src="images/loading.gif">
		//		</div>
		//	</div>

		var objBody = document.getElementsByTagName("body").item(0);
		
		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','overlay');
		objOverlay.style.display = 'none';
		objOverlay.style.position = 'absolute';
		objOverlay.style.top = 0;
		objOverlay.style.left = 0;
		objOverlay.style.zIndex = 90;
		objOverlay.style.width = '100%';
		objOverlay.style.opacity = Opacidad;
		objOverlay.style.filter = 'alpha(opacity='+parseInt(Opacidad*100)+')';
		objOverlay.style.height = '500px';
		objOverlay.style.backgroundColor = '#ffffff';
		//objOverlay.onclick = function() { miCargando.end(); }
		objBody.appendChild(objOverlay);
		
		var objCargando = document.createElement("div");
		objCargando.setAttribute('id','Cargando');
		//objCargando.style.display = 'none';
		objCargando.style.position = 'absolute';
		objCargando.style.left = '0px';
		objCargando.style.top = '0px';
		objCargando.style.width = '100%';
		objCargando.style.zIndex = '100';
		objCargando.style.textAlign = 'center';
		objCargando.style.lineHeight = '0';
		//objCargando.style.backgroundColor = '#ff0000';
		objBody.appendChild(objCargando);
			
		var objOuterContenido = document.createElement("div");
		objOuterContenido.setAttribute('id','outerContenido');
		objOuterContenido.style.position = 'relative';
		objOuterContenido.style.display = 'none';
		objOuterContenido.style.backgroundColor = '#ffffff';
		objOuterContenido.style.width = outerContenidoWidth+'px';
		//objOuterContenido.style.height = '250px';
		objOuterContenido.style.margin = '0 auto';
		objCargando.appendChild(objOuterContenido);

		// When Cargando 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('outerContenido', 250);
		//Element.setHeight('outerContenido', 250);			
	
		var objDivClose = document.createElement("div");
		objDivClose.setAttribute('id','DivClose');
		objDivClose.style.position = 'relative';
		objDivClose.style.width = '100%';
		objDivClose.style.height = fileBottomNavCloseImageh+'px';
		objDivClose.style.textAlign = 'right';
		objDivClose.style.backgroundColor = '#5d96d9';
		objDivClose.style.borderStyle = 'solid';
		objDivClose.style.borderWidth = '1px';
		objDivClose.style.borderColor = '#5d96d9';
		objOuterContenido.appendChild(objDivClose);
	
		var objBottomNavCloseImage = document.createElement("img");
		objBottomNavCloseImage.setAttribute('id','Close');
		objBottomNavCloseImage.setAttribute('src', fileBottomNavCloseImage);
		objBottomNavCloseImage.setAttribute('width', fileBottomNavCloseImagew );
		objBottomNavCloseImage.setAttribute('height', fileBottomNavCloseImageh );
		objBottomNavCloseImage.setAttribute('align', 'right');
		objBottomNavCloseImage.style.position = 'relative';
		objBottomNavCloseImage.onclick = function() { miCargando.end(); return false; }
		objBottomNavCloseImage.style.cursor = 'pointer';
		objDivClose.appendChild(objBottomNavCloseImage);	

		var objContenido = document.createElement("div");
		objContenido.setAttribute('id','Contenido');
		objContenido.style.position = 'relative';
		objContenido.style.width = '100%';
		objContenido.style.textAlign = 'left';
		objContenido.style.borderStyle = 'solid';
		objContenido.style.borderWidth = '1px';
		objContenido.style.borderColor = '#cccccc';
		objContenido.style.borderTopWidth = '0px';
		
		//objContenido.style.backgroundColor = '#0000ff';
		//objContenido.style.display = 'none';
		//objContenido.appendChild(document.createTextNode("Texto"));
		objOuterContenido.appendChild(objContenido);

		var objLoading = document.createElement("div");
		objLoading.setAttribute('id','loading');
		objLoading.style.position = 'absolute';
		objLoading.style.top = '0';
		objLoading.style.left = '0';
		objLoading.style.width = '100%';
		objLoading.style.textAlign = 'center';
		objLoading.style.verticalAlign = 'middle';
		objLoading.style.display = 'none';
		//objLoading.style.height = '200px';
		//objLoading.style.backgroundColor = '#00ff00';
		objCargando.appendChild(objLoading);
		
		var objLoadingImage = document.createElement("img");
		objLoadingImage.setAttribute('id', 'ImagenCargando');
		objLoadingImage.setAttribute('src', ImagenCargando);
		objLoadingImage.setAttribute('width', ImagenCargandow );
		objLoadingImage.setAttribute('height', ImagenCargandoh );
		objLoading.appendChild(objLoadingImage);

		return;
		alert('Entro 1');
		alert('Entro 2');
		alert('Entro 3');
		alert('Entro 4');
		alert('Entro 5');
		alert('Entro 6');
		alert('Entro 7');
		alert('Entro 8');
	},

	//
	//	start()
	//	Display overlay and Cargando. If image is part of a set, add siblings to imageArray.
	//
	// start: function(imageLink) {	
	start: function(url) {	

		hideSelectBoxes();
		hideFlash();

		// recargo el ancho original
		document.getElementById('outerContenido').style.width = outerContenidoWidth+'px';
		
		// stretch overlay to fill page and fade in
		var arrayPageSize = getPageSize();
		Element.setWidth('overlay', arrayPageSize[0]);
		Element.setHeight('overlay', arrayPageSize[1]);

		//new Effect.Appear('overlay', { duration: overlayDuration, from: 0.0, to: Opacidad });
		Element.show('overlay');
		Element.show('Cargando');

		var b = getPageScroll();
		var c = (arrayPageSize[3]-ImagenCargandoh)/2; 
		if( c < 0 ) c = 0;
		document.getElementById("loading").style.top = c+"px";

		Element.show('loading');
		Element.show('ImagenCargando');
		//alert( $('ImagenCargando').getAttribute('width'));

		// calculate top and left offset for the Cargando 
		var arrayPageScroll = getPageScroll();
		//var CargandoTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
		var CargandoTop = arrayPageScroll[1];
		var CargandoLeft = arrayPageScroll[0];
		Element.setTop('Cargando', CargandoTop);
		Element.setLeft('Cargando', CargandoLeft);
		
		if (!url) {
			return;
		} else {
			while ($('Contenido').childNodes[0]) {
				$('Contenido').removeChild($('Contenido').childNodes[0]);
			}
			var poststr = "";
			arr = url.split("?");
			if(arr[1]) {
					arr1 = arr[1].split("&");
				var i = 0;
				while( arr1[i] ) {
					if( poststr != "" ) poststr += "&";
					arr2 = arr1[i].split("=");
					poststr += arr2[0]+"=";
					if( arr2[1] ) poststr += encodeURI(arr2[1]);
					i++;
				}
			}
			//alert(poststr);
			//alert(arr[0]);
			//this.makePOSTRequest(arr[0], poststr);
			//alert(jQuery.ajax);
			window.jQuery.ajax({
					type: "POST",
					url: arr[0],
					data: poststr,
					success: function(data){
						var funcContenido = eval(data);
						//alert(funcContenido);
						var objCont = funcContenido();
						//alert(objContenido);
						var obj = document.getElementById('Contenido');
						while (obj.childNodes[0]) {
							obj.removeChild(obj.childNodes[0]);
						}			
						//obj.style.backgroundColor = '#ff0000';
						obj.appendChild(objCont);            
						//alert($('Contenido').childNodes[0]);
						//alert(Element.getHeight('Contenido'));
						//miCargando.resizeOuterContenido(Element.getWidth('Contenido'),Element.getHeight('Contenido'));
						miCargando.showContenido();
						var obj = document.getElementById('Cargando');
						var a = getPageSize();
						var b = getPageScroll();
						//alert(a);
						//alert(obj.scrollHeight+ '(1)'+a[3]);
						//obj1 = document.getElementById('outerContenido');
						//obj1.style.width = outerContenidoWidth;
						while( obj.scrollHeight > a[3] ) {
							//alert(obj.scrollHeight+ ','+a[3]);
							obj1.style.width = (parseInt(obj1.style.width) * 1.2)+'px';
							if( parseInt(obj1.style.width)*1.2 > a[2] ) break;
						}
						//alert(obj.scrollHeight+ '(2)'+a[3]);
						//alert(getPageSize());
						var c = (a[3]-obj.scrollHeight)/2+b[1]; 
						if( c < 0 ) c = 0;
						//traeprop(obj);
						obj.style.top = c+"px";
					}
			});

			
			return;

			cc = document.createTextNode("Texto");
			//alert( cc.offsetWidth );
			Element.appendChild('Contenido',cc);
		}
	},

	//
	//	resizeOuterContenido()
	//
	resizeOuterContenido: function( imgWidth, imgHeight) {

/*		// get curren width and height
		this.widthCurrent = Element.getWidth('outerContenido');
		this.heightCurrent = Element.getHeight('outerContenido');

		// get new width and height
		var widthNew = (imgWidth  + (borderSize * 2));
		var heightNew = (imgHeight  + (borderSize * 2));

		// scalars based on change from old to new
		this.xScale = ( widthNew / this.widthCurrent) * 100;
		this.yScale = ( heightNew / this.heightCurrent) * 100;

		// calculate size difference between new and old image, and resize if necessary
		wDiff = this.widthCurrent - widthNew;
		hDiff = this.heightCurrent - heightNew;

		//if(!( hDiff == 0)){ new Effect.Scale('outerContenido', this.yScale, {scaleX: false, duration: resizeDuration, queue: 'front'}); }
		//if(!( wDiff == 0)){ new Effect.Scale('outerContenido', this.xScale, {scaleY: false, delay: resizeDuration, duration: resizeDuration}); }

		// if new and old image are same size and no scaling transition is necessary, 
		// do a quick pause to prevent image flicker.
		if((hDiff == 0) && (wDiff == 0)){
			if (navigator.appVersion.indexOf("MSIE")!=-1){ pause(250); } else { pause(100);} 
		}

		//Element.setHeight('prevLink', imgHeight);
		//Element.setHeight('nextLink', imgHeight);
		//Element.setWidth( 'outerContenido', 350);
		Element.setWidth('DivClose', outerContenidoWidth);
		Element.setWidth('Contenido', outerContenidoWidth);
		Element.setWidth('outerContenido', outerContenidoWidth);
*/
		this.showContenido();
		return;
	},
	
	//
	//	showContenido()
	//	Display image and begin preloading neighbors.
	//
	showContenido: function(){
		Element.hide('loading');
		Element.show('outerContenido');
		//new Effect.Appear('outerContenido', { duration: resizeDuration, queue: 'end' });
		return;
	},
	
	//
	//	disableKeyboardNav()
	//
	disableKeyboardNav: function() {
		document.onkeydown = '';
	},
	
	//
	//	end()
	//
	end: function() {
		this.disableKeyboardNav();
		Element.hide('outerContenido');
		Element.hide('Cargando');
		while ($('Contenido').childNodes[0]) {
			$('Contenido').removeChild($('Contenido').childNodes[0]);
		}			
		//new Effect.Fade('overlay', { duration: overlayDuration});
		Element.hide('overlay');
		showSelectBoxes();
		showFlash();
		outerContenidoWidth = 350;		
		for(var i=0; i<document.images.length; i++){
			var img = document.images[i];
			var imgName = img.src.toUpperCase();
			if (imgName.substring(imgName.length-3, imgName.length) == "GIF"){
				img.src = img.src;
			}
		}
	}
}

// -----------------------------------------------------------------------------------

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
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;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
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;
}

// -----------------------------------------------------------------------------------

//
// getKey(key)
// Gets keycode. If 'x' is pressed then it hides the Cargando.
//
function getKey(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	key = String.fromCharCode(keycode).toLowerCase();
	
	if(key == 'x'){
	}
}

// -----------------------------------------------------------------------------------

//
// listenKey()
//
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";
	}

}


// ---------------------------------------------------

//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Help from Ran Bar-On [ran2103@gmail.com]
//

function pause(ms){
	var date = new Date();
	curDate = null;
	do{var curDate = new Date();}
	while( curDate - date < ms);
}

// ---------------------------------------------------



function initCargando() { 
	miCargando = new Cargando();
	if(lPortada) {
		outerContenidoWidth = 257;		
		miCargando.start('portfoliodet.ajax.php?pag=portada');
	}
}

// Event.observe(element, eventName, handler[, useCapture = false])
// Registers an event handler on a DOM element.
Event.observe(window, 'load', initCargando, false);

