﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var selectTags;

	(function($){
		$.fn.vCenter = function(options) {
			var pos = {
				sTop : function() {
					return window.pageYOffset || $.boxModel && document.documentElement.scrollTop || document.body.scrollTop;
				},
				wHeight : function() {
					if ( $.browser.opera || ($.browser.safari && parseInt($.browser.version) > 520) ) {
						return window.innerHeight - (($(document).height() > window.innerHeight) ? getScrollbarWidth() : 0);
					} else if ( $.browser.safari ) {
						return window.innerHeight;
					} else {
						return $.boxModel && document.documentElement.clientHeight || document.body.clientHeight;
					}
				}
			};
			return this.each(function(index) {
				if (index == 0) {
					var $this = $(this);
					var elHeight = $this.height();
					$this.css({
						position: 'absolute',
						marginTop: '0',
						top: pos.sTop() + (pos.wHeight() / 2) - (elHeight / 2)
					});
				}
			});
		};	
	})(jQuery);

	(function($){
		$.fn.hCenter = function(options) {
			var pos = {
				sLeft : function() {
					return window.pageXOffset || $.boxModel && document.documentElement.scrollLeft || document.body.scrollLeft;
				},
				wWidth : function() {
					if ( $.browser.opera || ($.browser.safari && parseInt($.browser.version) > 520) ) {
						return window.innerWidth - (($(document).width() > window.innerWidth) ? getScrollbarHeight() : 0);
					} else if ( $.browser.safari ) {
						return window.innerWidth;
					} else {
						return $.boxModel && document.documentElement.clientWidth || document.body.clientWidth;
					}
				}
			};
			return this.each(function(index) {
				if (index == 0) {
					var $this = $(this);
					var elWidth = $this.width();
					$this.css({
						position: 'absolute',
						marginLeft: '0',
						left: pos.sLeft() + (pos.wWidth() / 2) - (elWidth / 2)
					});
				}
			});
		};	
	})(jQuery);




//loading popup with jQuery magic!
function loadPopup(name,src){
	//loads popup only if it is disabled
	if(popupStatus==0){
		//$("#popupIframe").src=src;
		/*if(src.toLowerCase().indexOf(name)>0)
		{
		   document.getElementById("popupContact").style.backgroundColor='#FFFFFF';
	       document.getElementById("popupContact").style.height="384px";
	       document.getElementById("popupContact").style.width="450px";
		}*/
		iframe='';
		popupdiv='';

		jQuery('select').addClass('hideMe');
		
		if(name=='contacts')
		{
			iframe='popupIframe';
			popupdiv='#popupContact';
		}
		if(name=='tell')
		{
			iframe='popuptellIframe';
			popupdiv='#popuptell';
		}
		//alert(name+"  "+src);
		document.getElementById(iframe).src=src;
		centerPopup(popupdiv);
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$(popupdiv).fadeIn("slow");
		popupStatus = 1;
		
	}
}

//disabling popup with jQuery magic!
function disablePopup(name){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$(name).fadeOut("slow");
		jQuery('select').removeClass('hideMe');
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(name){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	$(name).vCenter();
	$(name).hCenter();

	//only need force for IE6
	$("#backgroundPopup").css("height", windowHeight + 'px');
}


function TellAFriend(name,src) {
  	//document.getElementById("popupIframe").src=src;
	/*document.getElementById("popupContact").style.backgroundColor='#CBD8DF';
	document.getElementById("popupContact").style.height="500px";
	document.getElementById("popupContact").style.width="650px";
	*/
	//document.getElementById("popupContactCloseButton").style.display='none';
		$("#popuptell").vCenter();
		$("#popuptell").hCenter();
		centerPopup("#popuptell");
		//load popup
		loadPopup(name,src);
		return false;
}



//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("#contactPop").click(function(){
		//centering with css
		//alert(document.getElementById("contactPop").href);
		//load popup
		src = this.href;
		loadPopup(src);
		return false;
	});
	

	
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup("#popupContact");
	});
	$("#popupContactCloseButton").click(function(){
		disablePopup("#popupContact");
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup("#popupContact");
	});
	
	
	$("#popuptellClose").click(function(){
		disablePopup("#popuptell");
	});
	$("#popupContactCloseButton").click(function(){
		disablePopup("#popuptell");
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup("#popuptell");
	});
	
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup("#popupContact");
			disablePopup("#popuptellContact");
		}
	});

});