//http://www.sohtanaka.com/web-design/inline-modal-window-w-css-and-jquery/

//Kevin Folz, Fueladvertising.com
//Kevinfolz@gmail.com
//July 2010
//Displays a modal window by dynamically adding the content to the bottom of the page body

var CLOSE_PATH = "/img/close_pop.png";

// PageQuery lets us get query values from URL easily
function PageQuery(q) {
	if (q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if (q) {
		for (var i = 0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
		for (var j = 0; j < this.keyValuePairs.length; j++) {
			if (this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	this.getParameters = function() {
		var a = new Array(this.getLength());
		for (var j = 0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; }
}
function queryString(key) {
	var page = new PageQuery((window.location.search).toLowerCase());
	return unescape(page.getValue(key.toLowerCase()));
}


// Page load code
$(document).ready(function(){

	$('body').append('<div id="fade"></div>');
	if ((window.location+" ").indexOf(".com/fr/") != -1){
		$('body').append('<div id="emailPopup" class="popup_block"><iframe src="http://f.chtah.com/s/33/2082456423/052710_HOMEOUTFITTERS_FR_REG.html" width="620" height="600" frameborder="0" scrolling="no" name="D&#233;co D&#233;couverte"></iframe></div>');
	}else{
		$('body').append('<div id="emailPopup" class="popup_block"><iframe src="http://f.chtah.com/s/33/2082456423/052710_HOMEOUTFITTERS_REG.html" width="620" height="600" frameborder="0" scrolling="no" name="Home Outfitters"></iframe></div>');
	}
	
	registerEmphassis();
});


// Registers all elements with poplight class to launch modal windows
function registerEmphassis(){
						   		   
	//When you click on a link with class of poplight and the href ends with #
	$('a.poplight').click(function() {
	
		var popID = $(this).attr('rel'); //Get Popup Name
		var popURL = $(this).attr('href'); //Get Popup href to define size
		
		var popWidth = popURL.substring(popURL.lastIndexOf("#?w=")+4); //Gets the last query string value

		//Fade in the Popup and add close button
		
		if ((window.location+" ").indexOf(".com/fr/") != -1){
			$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) });//.prepend('<a href="#" class="closeForm"><img src="'+CLOSE_PATH+'" class="btn_close" title="Fermer" alt="Fermer" /></a>');
		}else{
			$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) });//.prepend('<a href="#" class="closeForm"><img src="'+CLOSE_PATH+'" class="btn_close" title="Close" alt="Close" /></a>');
		}
		
		
		//Apply Margin to Popup
		$('#' + popID).css({ 
			'left' : (($('body').width()-$('#' + popID).width())/2)+"px",
			'top' : $(window).scrollTop()+(document.documentElement.clientHeight-$('#' + popID).height())/2
			// vertical scroll offset + (viewport height + popup height) /2
		});
		
		// old method 'top' : $(window).scrollTop()+(document.getElementsByTagName('body')[0].clientHeight-$(window).scrollTop()-$('#' + popID).height())/2
		
		//Fade in Background
		$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 
		
		// Find Window Height
		var winW= 0, winH= 0;       
		if (document.body && document.body.clientWidth) {
			winW= document.body.clientWidth; winH= document.body.clientHeight;
		}else if (typeof(window.innerWidth) == 'number') {
			winW= window.innerWidth; winH= window.innerHeight;
		}  
		else if (document.documentElement && document.documentElement.clientWidth) {
			winW= document.documentElement.clientWidth; winH= document.documentElement.clientHeight;
		}   
		
		
		//alert("body height:"+document.body.clientHeight+ " window:"+ window.innerHeight+ " document height:"+document.documentElement.clientHeight);
		
		// Set size of Fade layer
		$('#fade').css({'height' : winH+"px"});
		
		
		return false;
	});
	
	
	//Close Popups and Fade Layer
	$('a.closeForm, #fade').bind('click', function() { //When clicking on the close or fade layer...
	  	$('#fade , .popup_block').fadeOut(function() {
			$('a.closeForm').remove();  
	}); //fade them both out
		
		return false;
	});

	
}
