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;
	}
});

function Lightbox(boxName,w,h,orientation,contentSrc,contentParams,closeTxt, offset) { //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.offset = offset;
		this.baseWidth = 75;
		this.baseHeight = 0;
		this.wrapperDimX = 0;
		this.wrapperDimY = 0;
		if (orientation == "tb") { 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
		objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','overlay');
		//objOverlay.setAttribute('onclick','pumaLightbox.close();');
		objOverlay.onclick = function () {pumaLightbox.close();};
		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";

		objBody.appendChild(obj_lb_wrapper);
		if (this.closeTxt) { obj_lb_wrapper.innerHTML = '<div class="lightBoxTop'+this.boxName+'"><a href="#" onclick="pumaLightbox.close(); return false;" class="black_link">' + this.closeTxt + '&nbsp;&nbsp;<img src="\/images\/icon_down.gif" border="0" alt=""><\/a><\/div>'; }

		// middle content
		obj_lbcontent = document.createElement("div");
			if(this.orientation != "img"){	
			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);
	
		
		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, from: 0.0, to: 0.6, 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[2] / 2));
			if(this.orientation == "pdf"){	
				lightboxLeft = Math.round(lightboxLeft-(((this.boxWidth > this.baseWidth ? this.boxWidth : this. baseWidth)+this.wrapperDimX)/2))+125;
				lightboxTop = Math.round(lightboxTop-(((this.boxHeight > this.baseHeight ? this.boxHeight : this. baseHeight)+this.wrapperDimY)/2));
			}else{
				if(this.orientation == "img"){	
					lightboxLeft = Math.round(lightboxLeft-(((this.boxWidth > this.baseWidth ? this.boxWidth : this. baseWidth)+this.wrapperDimX)/2));
					lightboxTop = Math.round(lightboxTop-(((this.boxHeight > this.baseHeight ? this.boxHeight : this. baseHeight)+this.wrapperDimY)/2))-100;
				}else{
					lightboxLeft = Math.round(lightboxLeft-(((this.boxWidth > this.baseWidth ? this.boxWidth : this. baseWidth)+this.wrapperDimX)/2)) + parseInt(this.offset);
					//lightboxTop = Math.round(lightboxTop-(((this.boxHeight > this.baseHeight ? this.boxHeight : this. baseHeight)+this.wrapperDimY)/2));
					lightboxTop = 0;
				}
			}
			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(\"/about/images/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.3, 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 = ajaxUrl;
		var pars = 'rand=' + Math.floor(Math.random() * 999);
			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.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);
		var doScale = true;
		var responseAr = response.responseText.split("~");
		pumaLightbox.obj_lbcontent.innerHTML = responseAr[0];
		if (responseAr[1]) {
			//pumaLightbox.obj_lbcontent.innerHTML = responseAr[0];
			imgPreloader = new Image();
			imgPreloader.onload=function(){
				var newDim = new Array(imgPreloader.width, imgPreloader.height);
				doScale = true;
				resizeDuration = .3;
				this.boxWidth = newDim[0]+20;
				this.boxHeight = newDim[1]+30;
				new Effect.Scale($('lightImgBox'), this.boxHeight*10, {scaleX:false, scaleY:true, duration: resizeDuration, queue: 'front'});
				new Effect.Scale($('lightImgBox'), this.boxWidth*10, {scaleX:true, scaleY:false, delay: resizeDuration, duration: resizeDuration, afterFinish: function() {
						$('lightImg').src = imgPreloader.src;
						new Effect.Appear($('lightImg'), { duration: resizeDuration, queue: 'end'});
				} } );
				imgPreloader.onload=function(){};	 
			}
			imgPreloader.src = responseAr[1];

		} else {
		 }
		if (responseAr[responseAr.length-1]) {
			var res = responseAr[responseAr.length-1];
			if(res.indexOf("ANALYTICS=") != -1){
					pumaLightbox.obj_lbcontent.innerHTML+=res.replace("ANALYTICS=","");
			}
		}

	}
	
	this.errorPopulate = function(msg) { this.obj_lbcontent.innerHTML = msg; }
}

Lightbox.prototype.load = function() {
	this.createElements();
	this.activateOverlay();
}

Lightbox.prototype.close = function() {
	showSelectBoxes();
	this.objOverlay.parentNode.removeChild(this.objOverlay);
	this.obj_lb_wrapper.parentNode.removeChild(this.obj_lb_wrapper);

}
function getPageScroll(){
	var yScroll;

	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;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	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;
}

function showSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) curtop += obj.y;
	return curtop;
}

// browser detection
var _appName = navigator.appName;
var _appVersion = navigator.appVersion;
var _userAgent = navigator.userAgent.toLowerCase();

function getBrowser() {      
      var browser = '';
      
      if ((_appName.indexOf('Microsoft') != -1) && (_userAgent.indexOf('mac') == -1)) {
		  browser = 'IE';
	  } else if ((_appName.indexOf('Microsoft') != -1) && (_userAgent.indexOf('mac') != -1)) {		
	      browser = 'Other';
	  } else if (_userAgent.indexOf('safari') != -1) {
		  browser = 'Safari';
	  } else if (_userAgent.indexOf('firefox') != -1) {
		  browser = 'Firefox';
	  } else {
		  browser = 'Other';
	  }
	  return browser;
}

function closeLightBox(){
	pumaLightbox.close();
}