/* ###################### EXTENDED OBJECT FUNCTIONALITY ####################*/
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,t) {
	   	element = $(element);
    	element.style.left = t +"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;
	}
});

// Puma Lightbox functionality

function Lightbox(boxName,w,h,orientation,contentSrc,contentParams,closeTxt,lvlUp) { //checkRows,elemID,elemType
	if ( arguments.length > 0 ) {
		
		this.boxName = boxName;
		this.boxWidth = w;
		this.boxHeight = h;
		this.orientation = orientation;
		this.contentSrc = contentSrc;
		this.contentParams = contentParams;
		this.closeTxt = closeTxt;
		this.baseHeight = 75;
		this.baseWidth = 75;
		//this.marginLeft = 0;
		this.lvlsUp = this.lvlsUpAct(lvlUp);
		this.OGlvlUp = lvlUp;

		
		if (this.marginLeft == 0) { this.marginLeft = 142; }
	//	alert(this.marginLeft);

		if (orientation == "tb" || orientation == "ee") { this.wrapperDimX = 0; this.wrapperDimY = 20; }
		else if (orientation == "lr") { this.wrapperDimX = 20; this.wrapperDimY = 0; }
	}

	this.createElements = function() {
		objBody = document.getElementsByTagName("body").item(0);
		
		// overlay
		if (!document.getElementById('overlay')) {
			objOverlay = document.createElement("div");
			objOverlay.setAttribute('id','overlay');
			objOverlay.style.display = 'none';
			objBody.appendChild(objOverlay);
		}

		// wrapper
		obj_lb_wrapper = document.createElement("div");
		obj_lb_wrapper.style.display = 'none';
		obj_lb_wrapper.className = "lightbox lb_" + this.orientation;	
		obj_lb_wrapper.setAttribute('id',boxName);
		if (this.closeTxt) { this.wrapperDimY += 12; }
		obj_lb_wrapper.style.width = (this.boxWidth > this.baseWidth ? this.boxWidth + this.wrapperDimX : this.baseWidth + this.wrapperDimX) + "px";
		obj_lb_wrapper.style.height = (this.boxHeight > this.baseHeight ? this.boxHeight + this.wrapperDimY : this.baseHeight + this.wrapperDimY) + "px";
		//obj_lb_wrapper.style.marginLeft = "-" + this.marginLeft + "px";
		objBody.appendChild(obj_lb_wrapper);

		//objLightboxBlankTxt = document.createTextNode("&nbsp;");
		if (this.closeTxt) { obj_lb_wrapper.innerHTML = '<a href="#" onclick="pumaLightbox.close(); return false;" class="close">' + this.closeTxt + ' X<\/a>'; }

		// top structure
		obj_lb_bar1 = document.createElement("div");
			obj_lb_bar1.className = "lb_bar1";
			obj_lb_wrapper.appendChild(obj_lb_bar1);
	
		obj_lb_corner_1_bar1 = document.createElement("div");
			obj_lb_corner_1_bar1.className = "lb_corner_1";
			obj_lb_corner_1_bar1.innerHTML = "&nbsp;";
		obj_lb_spacer_bar1 = document.createElement("div");
			obj_lb_spacer_bar1.className = "lb_spacer";
			if (this.orientation == "tb" || this.orientation == "ee") { obj_lb_spacer_bar1.style.width = (this.boxWidth-20) + "px"; }
			else { obj_lb_spacer_bar1.style.height = (this.boxHeight-20) + "px"; }
			obj_lb_spacer_bar1.innerHTML = "&nbsp;";
		obj_lb_corner_2_bar1 = document.createElement("div");
			obj_lb_corner_2_bar1.className = "lb_corner_2";
			obj_lb_corner_2_bar1.innerHTML = "&nbsp;";
		obj_lb_bar1.appendChild(obj_lb_corner_1_bar1);
		obj_lb_bar1.appendChild(obj_lb_spacer_bar1);
		obj_lb_bar1.appendChild(obj_lb_corner_2_bar1);

		// middle content
		obj_lbcontent = document.createElement("div");
			obj_lbcontent.className = "lbcontent " + this.orientation;
			obj_lbcontent.style.height = (this.boxHeight > this.baseHeight ? this.boxHeight : this.baseHeight ) + "px";
			obj_lbcontent.style.width = (this.boxWidth > this.baseWidth ? this.boxWidth : this.baseWidth ) + "px";
			obj_lb_wrapper.appendChild(obj_lbcontent);
	
		// bottom structure
		obj_lb_bar2 = document.createElement("div");
			obj_lb_bar2.className = "lb_bar2";
			obj_lb_wrapper.appendChild(obj_lb_bar2);
	
		obj_lb_corner_1_bar2 = document.createElement("div");
			obj_lb_corner_1_bar2.className = "lb_corner_1";
			obj_lb_corner_1_bar2.innerHTML = "&nbsp;";
		obj_lb_spacer_bar2 = document.createElement("div");
			obj_lb_spacer_bar2.className = "lb_spacer";
			if (this.orientation == "tb" || this.orientation == "ee") { obj_lb_spacer_bar2.style.width = (this.boxWidth-20) + "px"; }
			else { obj_lb_spacer_bar2.style.height = (this.boxHeight-20) + "px"; }
			obj_lb_spacer_bar2.innerHTML = "&nbsp;";
		obj_lb_corner_2_bar2 = document.createElement("div");
			obj_lb_corner_2_bar2.className = "lb_corner_2";
			obj_lb_corner_2_bar2.innerHTML = "&nbsp;";
		obj_lb_bar2.appendChild(obj_lb_corner_1_bar2);
		obj_lb_bar2.appendChild(obj_lb_spacer_bar2);
		obj_lb_bar2.appendChild(obj_lb_corner_2_bar2);

		this.objOverlay = objOverlay;
		this.obj_lb_wrapper = obj_lb_wrapper;
		this.obj_lbcontent = obj_lbcontent;
	}

	this.activateOverlay = function() {
		hideSelectBoxes();
		var arrayPageSize = getPageSize();
		Element.setHeight(this.objOverlay, arrayPageSize[1]);
		new Effect.Appear(this.objOverlay, { duration: 0.1,fps: 28, from: 0.0, to: 0.8, afterFinish: function() { pumaLightbox.lightboxPosition(true); } } );
	}

	this.lightboxPosition = function(contAction) {
		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();

		var lightboxTop = Math.round((arrayPageScroll[1] + (arrayPageSize[3] / 2)));
		//var lightboxLeft = Math.round((arrayPageSize[0] / 2));
		/*	 lightboxLeft =  Math.round(lightboxLeft-(((this.boxWidth > this.baseWidth ? this.boxWidth : this. baseWidth))/2))
			 alert(this.boxWidth);	
			  alert(this.baseWidth);	 */
		//	lightboxLeft = Math.round(lightboxLeft-(((this.boxWidth > this.baseWidth ? this.boxWidth : this. baseWidth)+this.wrapperDimX)/2)) + 230; // add 230 to put in the center of content area
			
		var lightboxLeft = Math.round((arrayPageSize[0]-this.boxWidth)/2);
	
		lightboxTop = Math.round(lightboxTop-(((this.boxHeight > this.baseHeight ? this.boxHeight : this. baseHeight)+this.wrapperDimY)/2));
			if (lightboxTop <= 0) lightboxTop=0;
		Element.setLeft(this.obj_lb_wrapper, lightboxLeft);
		Element.setTop(this.obj_lb_wrapper, lightboxTop);	
		if (contAction) { this.lightboxLoading(); }
	}

	this.lightboxLoading = function() {
		this.obj_lbcontent.innerHTML = "";
		this.obj_lbcontent.style.backgroundImage = "url('" + imgUrl + "shop/ajaxrotation.gif\')";		
		this.obj_lbcontent.style.backgroundPosition = "center center";
		this.obj_lbcontent.style.backgroundRepeat = "no-repeat";
		new Effect.Appear(this.obj_lb_wrapper, { duration: 0.2, fps: 28, from: 0.0, to: 1, afterFinish: function() { pumaLightbox.populate(); } } );
	
	}	
	
	this.populate = function() {
		ajaxUrl = this.contentSrc;
		ajaxParams = this.contentParams;

		// ajax to grab content
		var url = this.lvlsUp + ajaxUrl;

		var pars = 'rand=' + Math.floor(Math.random() * 999) + "&lvlsUp=" + this.OGlvlUp;
		if (ajaxParams!='') pars += '&' + ajaxParams;
		var myAjax = new Ajax.Request( 
			url,
			{
				method: 'get',
				parameters: pars,
				onComplete: function(response) { pumaLightbox.resizePopulateLightbox(response); },
				onFailure: function() { pumaLightbox.errorPopulate("Error receiving content. Please refresh."); }
			}
		);
	}
	
	this.centerLightbox = function() {
		var lObj = pumaLightbox.obj_lbcontent;
	
	/*if(document.getElementById('bagtb'))
		{
		var test = document.getElementById('bagtb');
		var number = test.className.substr(5);
	
		var height = Element.getHeight(pumaLightbox.obj_lbcontent);
			
		if((!(number == '0') || !(number == '1')) && (height > (number * 65) + 250 ) )
		{
		var check = 100 / ( this.boxHeight / 65);
		var scale = 100 - check;
		new Effect.Scale(this.obj_lbcontent, scale , {scaleX:false , scaleFromCenter: true, scaleContent: false, delay: .1, duration: 0.4});

		}
		} else {
	
		
		} */
		
	pumaLightbox.lightboxPosition();
		/*			if(document.getElementById('bagtb'))
		{
			this.boxHeight = lObj.offsetHeight;
			Element.setHeight(pumaLightbox.obj_lbcontent, this.boxHeight - 75);
			alert('Checkpoint');
		} */
		
	}
	 
	this.resizePopulateLightbox = function(response) {
		this.obj_lbcontent.style.backgroundImage = "none";

		var originalDim = new Array((this.boxWidth > this.baseWidth ? this.boxWidth : this. baseWidth), (this.boxHeight > this.baseHeight ? this.boxHeight : this.baseHeight));
		var scaleAr = new Array(false,100);
		
		// DO: find new content size
		var doScale = true;

		/*
		This operates under the assumption that the page called knows the height of the elements it is returning.
		Appended to each page is a string separated by tildes that act as a parameters to the lightbox.
		
		responseAr[0]: the actual html
		responseAr[1]: height of a row
		responseAr[2]: base height of the element (height with no content)
		responseAr[3]: number of rows
		responseAr[4]: mybag specific parameter, contains number of items in the bag
		*/
		var responseAr = response.responseText.split("~");
	
		if (responseAr[1]) {
			var height = (parseInt(responseAr[1])*parseInt(responseAr[3]))+parseInt(responseAr[2]);
			var newDim = new Array(this.boxWidth, height);
		} else { doScale = false; }

		if (responseAr[4]) { itemsInMyBag = parseInt(responseAr[4]); }

		if (this.boxWidth == 0) {
			scaleAr[0] = true;
			scaleAr[1] = (newDim[0]/originalDim[0])*100;
		} else if (this.boxHeight == 0) {
			scaleAr[0] = false;
			scaleAr[1] = (newDim[1]/originalDim[1])*100;
		} else { doScale = false; }

		if (doScale) {

			this.boxWidth = newDim[0];
			this.boxHeight = newDim[1];

			this.lightboxPosition(false); //reposition, but don't repopulate
		
			new Effect.Scale(pumaLightbox.obj_lbcontent, scaleAr[1], {scaleX: scaleAr[0], scaleY: !scaleAr[0], scaleFromCenter: true, scaleContent: false, delay: .1, duration: .5, scaleMode: { originalHeight: originalDim[1], originalWidth: originalDim[0] }, afterFinish: function() {
				pumaLightbox.obj_lbcontent.innerHTML = responseAr[0];
			} } );
		} else {
		//	var test;
		//	test.innerHTML = responseAr[0];
			pumaLightbox.obj_lbcontent.innerHTML = responseAr[0];
			var arrayPageSize = getPageSize();
			Element.setHeight(this.objOverlay, arrayPageSize[1]);
			
		}
	}
	
	this.errorPopulate = function(msg) { this.obj_lbcontent.innerHTML = msg; }
}

Lightbox.prototype.load = function() {
	this.createElements();
	this.activateOverlay();
}

Lightbox.prototype.closeBag = function() {
	if(this.boxName.indexOf("AddToBagAction.do") != -1){
		this.objOverlay.parentNode.removeChild(this.objOverlay);
		this.obj_lb_wrapper.parentNode.removeChild(this.obj_lb_wrapper);
		showSelectBoxes();
	}
}

Lightbox.prototype.close = function() {
	this.objOverlay.parentNode.removeChild(this.objOverlay);
	this.obj_lb_wrapper.parentNode.removeChild(this.obj_lb_wrapper);
	showSelectBoxes();
}

// EVAN
Lightbox.prototype.closeBoxOnly = function() {
	this.obj_lb_wrapper.parentNode.removeChild(this.obj_lb_wrapper);
}

Lightbox.prototype.lvlsUpAct = function(lvl) {
	if (!lvl) lvl = 0;
	newLev = "";
	for (i=0; i<lvl; i++) {
		newLev += "../";
	}
	return newLev;
}

function addToBagEnd(str) {pumaLightbox.closeBag();}

