// Wasabox by Yacine Merzouk
// Copyright Wasabi Marketing Elements 2009
//
// Manages popups overlays containing an iframe

function Wasabox(){
	this.awesome = true;
	this.autoRefresh = false;
};

Wasabox.prototype.start = function(url){
	//Work from document body
	var objBody = document.getElementsByTagName("body").item(0);	
	
	//Get page size
	var pageSize = this.getPageSize();
	
	//Remove leftovers, if any
	if (document.getElementById('wasabox-overlay')) {
		objBody.removeChild(document.getElementById('wasabox-overlay'));
		objBody.removeChild(document.getElementById('wasabox-iframe-wrapper'));
		objBody.removeChild(document.getElementById('wasabox-overlay-message'));
	}	
	
	//Create overlay div tag
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','wasabox-overlay');
	objOverlay.style.height = pageSize[1]+'px';
	objOverlay.onclick = function(){ wasabox.end(); }
	objBody.appendChild(objOverlay);
	
	//Create wrapper div tag
	var objWasaboxWrapper = document.createElement("div");
	objWasaboxWrapper.setAttribute('id','wasabox-iframe-wrapper');
	objWasaboxWrapper.setAttribute('align','center');
	objWasaboxWrapper.onclick = function(){ wasabox.end(); }
	if(window.pageYOffset){  //firefox
		objWasaboxWrapper.style.top = '10px';
	}else{ //IE
		objWasaboxWrapper.style.top = '10px';
	}
	objBody.appendChild(objWasaboxWrapper);
	
	//Create iframe tag
	var objWasaboxIframe = document.createElement("iframe");
	objWasaboxIframe.setAttribute('id','wasabox-iframe');
	objWasaboxIframe.setAttribute('name','wasabox-iframe');
	objWasaboxIframe.setAttribute("border", "0");
	objWasaboxIframe.setAttribute("src", url);
	objWasaboxIframe.setAttribute("frameBorder", "0");
	objWasaboxIframe.setAttribute("scrolling", "no");
	objWasaboxIframe.style.width = '680px';
	objWasaboxIframe.style.height = '157px';
	objWasaboxWrapper.appendChild(objWasaboxIframe);
	

	//Create message container
	objMessage = document.createElement("div");
	objMessage.setAttribute('id','wasabox-overlay-message');
	objBody.appendChild(objMessage);

	var object = document.getElementById('wasabox-overlay').style;
	object.opacity = 0.8;
	object.MozOpacity = 0.8;
	object.KhtmlOpacity = 0.8;
	object.filter = "alpha(opacity=" + (90) + ")";
	
}

Wasabox.prototype.end = function(){
	
	//Work from document body
	var objBody = document.getElementsByTagName("body").item(0);	
	
	//Remove overlay div tag
	objBody.removeChild(document.getElementById('wasabox-overlay'));
	
	//Remove wrapper div tag
	objBody.removeChild(document.getElementById('wasabox-iframe-wrapper'));

	if(this.autoRefresh){
		window.location = window.location;
	}

}

Wasabox.prototype.getPageSize = function(){	
	var xScroll, yScroll, windowWidth, windowHeight;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.scrollWidth;
		yScroll = parent.innerHeight + parent.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.getElementsByTagName("html").item(0).offsetWidth;
		yScroll = document.getElementsByTagName("html").item(0).offsetHeight;
		xScroll = (xScroll < document.body.offsetWidth) ? document.body.offsetWidth : xScroll;
		yScroll = (yScroll < document.body.offsetHeight) ? document.body.offsetHeight : yScroll;
	}
	if (self.innerHeight) {
		windowWidth = parent.innerWidth;
		windowHeight = parent.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.getElementsByTagName("html").item(0).clientWidth;
		windowHeight = document.getElementsByTagName("html").item(0).clientHeight;
		windowWidth = (windowWidth == 0) ? document.body.clientWidth : windowWidth;
		windowHeight = (windowHeight == 0) ? document.body.clientHeight : windowHeight;
	}
	var pageHeight = (yScroll < windowHeight) ? windowHeight : yScroll;
	var pageWidth = (xScroll < windowWidth) ? windowWidth : xScroll;
	return new Array(pageWidth, pageHeight, windowWidth, windowHeight);	
}

Wasabox.prototype.automaticResize = function(w,h){
	document.getElementById('wasabox-iframe').style.width = w+'px';
	if(h){
		document.getElementById('wasabox-iframe').style.height = h+'px';
	}else{
		h = this.getPageSize();
		document.getElementById('wasabox-iframe').style.height = (h[3] - 20)+'px';
	}
}

Wasabox.prototype.setWidth = function(width){
	document.getElementById('wasabox-iframe').style.width = width+'px';
}

Wasabox.prototype.setHeight = function(height, scrollContent){
	var pageSize = this.getPageSize(); 
	if(height > pageSize[3] - 20 && scrollContent){
		document.getElementById('wasabox-iframe').style.height = (pageSize[3] - 20)+'px';
	}else{
		document.getElementById('wasabox-iframe').style.height = (height)+'px';
	}
}

Wasabox.prototype.setScrollableElementHeight = function(element){
	if(Prototype.Browser.IE6){
		element.style.height = (document.getElementById('wasabox-iframe').offsetHeight - 157)+"px";
	}else if(Prototype.Browser.IE7){
		element.style.height = (document.getElementById('wasabox-iframe').offsetHeight - 167)+"px";
	}else if(Prototype.Browser.IE8){
		element.style.height = (document.getElementById('wasabox-iframe').offsetHeight - 158)+"px";
	}else if(Prototype.Browser.WebKit){
		element.style.height = (document.getElementById('wasabox-iframe').offsetHeight - 151)+"px";
	}else{
		element.style.height = (document.getElementById('wasabox-iframe').offsetHeight - 157)+"px";
	}
}

Wasabox.prototype.setScrollableElementHeightLeads = function(element){
	if(Prototype.Browser.IE6){
		element.style.height = (document.getElementById('wasabox-iframe').offsetHeight - 118)+"px";
	}else if(Prototype.Browser.IE7){
		element.style.height = (document.getElementById('wasabox-iframe').offsetHeight - 120)+"px";
	}else if(Prototype.Browser.IE8){
		element.style.height = (document.getElementById('wasabox-iframe').offsetHeight - 119)+"px";
	}else if(Prototype.Browser.WebKit){
		element.style.height = (document.getElementById('wasabox-iframe').offsetHeight - 114)+"px";
	}else{
		element.style.height = (document.getElementById('wasabox-iframe').offsetHeight - 119)+"px";
	}
}



function automaticResize(w,h){
	wasabox.automaticResize(w,h);
}

function adjustBottomMargin(){
	if(Prototype.Browser.IE6){
		$('wrap').style.margin = '0';
		$('detail-content-container').style.marginBottom = '18px';	
	}else if(Prototype.Browser.IE7){
		$('wrap').style.margin = '0';
		$('detail-content-container').style.marginBottom = '20px';	
	}else if(Prototype.Browser.IE){
		$('detail-content-container').style.marginBottom = '20px';	
	}
}

function collapseIframe(){
	if(Prototype.Browser.IE6){
		parent.wasabox.setHeight(139, false);
	}else if(Prototype.Browser.IE7){
		parent.wasabox.setHeight(147, false);
	}else if(Prototype.Browser.IE){
		parent.wasabox.setHeight(158, false);
	}else if(Prototype.Browser.WebKit){
		parent.wasabox.setHeight(151, false);
	}else{
		parent.wasabox.setHeight(157, false);
	}
}

function formatPopup(){
	collapseIframe();
	parent.wasabox.setWidth(680);
	window.onload=function(){
		parent.wasabox.setHeight(parent.document.body.offsetHeight - 20, true);
		parent.wasabox.setScrollableElementHeight($('detail-content-container'));
		parent.displayMessage();
		$('detail-content-container').appear({duration:1.0})
		$('image-loader').fade();
		adjustBottomMargin();
	}
}

function formatStandalonePage(){
	window.onload=function(){
		displayMessage();
		//$('detail-content-container').appear({duration:1.0})
		$('image-loader').fade();
		adjustBottomMargin();
	}
}

Wasabox.prototype.resetDetailIframeHeight = function(){
	document.getElementById("wasabox-iframe").style.height = '157px';
}



var wasabox = new Wasabox();