/**
*
* @ver: 0.96 Beta
* @file: popup.js. Used to dynamically create a popup.
* @author: killerDesign.com
* @copyright: 2009 - 2010 killerDesign.com
* 
**/

ksd_bgCol = '#000'; // background color (e.g. #FFFFFF or white or none)
ksd_popup_bgCol = '#FFFFFF'; // popup box background color
ksd_borderCol = '#006500'; // popup box border color
ksd_width = '620'; // popup box width
ksd_height = '670'; // popup box height
ksd_displayRepeat = 1; // 1: yes, 0: no (no will display only once until reloading the page)

function ksd_popup() {
	var myWidth = ksd_width;
	var myHeight = ksd_height;
	var scrollTop = 10;
	mywindowWidth = $(window).width();
	myLeftPos = (mywindowWidth - ksd_width) / 2;
	myLeftPos = parseInt(myLeftPos);
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		var popupDisplay = "height:0px; \
						width:0px;\
						position:relative;\
						top:"+scrollTop+"px;\
						left:"+myLeftPos+"px;\
						background: "+ksd_popup_bgCol+";\
						z-index:100;\
						border:4px solid "+ksd_borderCol+";";
	} else {
		var popupDisplay = "height:0px; \
							width:0px;\
							position:fixed;\
							top:"+scrollTop+"px;\
							left:"+myLeftPos+"px;\
							background: "+ksd_popup_bgCol+";\
							z-index:100;\
							border:4px solid "+ksd_borderCol+";";
	}
	
	// create the containing elements
	var shadeDiv = $('<div class="PopShadeDiv"></div>');
	var centerDiv = $('<div class="PopContainerDiv"></div>');
	var containerDiv = $('<div id="PopupBoxDiv"></div>');
	var closeDiv = $('<div></div>');
	var myiFrame = $('<div width="200" height="200"></div>');

	myiFrame.attr({
		style: 'border: medium none;overflow:hidden;width:100%;height:100%; position: absolute;left:0;z-index:100;'
	});
	
	// a fix for a flickering issue
	if ($.browser.mozilla) {
		myiFrame.hide();
		myiFrame.load(function(){
			setTimeout (function(){myiFrame.show();},1000);
		});
	}
	
	// create the DOM structure
	$("body").append(shadeDiv);
	centerDiv.append(containerDiv);
	containerDiv.append(closeDiv);
	
	$("body").append(centerDiv);
	
	// style the center container
	centerDiv.attr({style: 'left:0;position:absolute;top:0;width:100%;'});
	
	// style the close btton
	closeDiv.attr({
		style: 'bottom:0;height:21px;position:absolute;bottom:-10px;right:260px;text-align:right;width:100%;background:transparent none;padding: 5px 0;'
	});
	
	//enable for closebutton
	closeDiv.html('<a style="position:relative;right:8px;top:-10px;cursor:pointer;"><img src="popup/images/close.png" class="PopClose" alt="close" style="z-index:2000000;" /></a>');
	
	$('.PopClose').click(function(){
		ksd_killPopUp();
		$(".video").css('visibility','visible');
	});
	$('#PopContainerDiv').click( function(){
		ksd_killPopUp();	
		$(".video").css('visibility','visible');
	});
	
	myiFrame = $("#ksd_popup").html();
	containerDiv.append(myiFrame);
	containerDiv.attr({
		style: popupDisplay
	});

	// animate the expanding popup
	containerDiv.animate({ 
		width: myWidth+"px",
		height: myHeight+"px"
	}, 100,'swing',function(){});
	
	// set the background fade in
	shadeDiv.attr({
		style: 'background-color:'+ksd_bgCol+';height:'+$(document).height()+'px;left:0;position:fixed;top:0px;width:'+$(window).width()+'px'
	}).css('opacity',0.5).hide().fadeIn('normal');
	
	if (!ksd_displayRepeat) {
		global_showing_popup = true;
	} 
	ksd_popupOpen = true;
	ksd_popupClosed = false;
};

/*
* preload images
*/
jQuery.preloadImages = function() {
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

/*
* Removes the popup
*/
function ksd_killPopUp(){
	$('.PopShadeDiv').css({'display':'none'});
	$('.PopContainerDiv').css({'display':'none'});
	ksd_popupClosed = true;
}

/*
* Get X & Y coordination
*/
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return scrOfY;
}

/*
* Initialise the popup
*/
$(document).ready(function(){
	global_showing_popup = false;
	ksd_popupClosed = false;
	ksd_popupOpen = false;
	
	cur_topPos = getScrollXY();
	topPos = cur_topPos;
	topPos2 = topPos+20;
	// area along the top of the page to make the hit test a bit more acurate
	$(document.body).append($('<div id="PopHitZone" style="position:absolute;width:100%;top:'+topPos+'px;height:20px;z-index:10000;"></div>'));
	$(document.body).append($('<div id="PopHitZone2" style="position:absolute;width:100%;top:'+topPos2+'px;height:10px;z-index:10000;"></div>'));
	
	$(window).scroll(function(){ 
		var topPos = getScrollXY();
		var topPos2 = topPos+20;
		$("#PopHitZone").css({"top":topPos});
		$("#PopHitZone2").css({"top":topPos2});
	});
	
	// preload a 100kb image for the popup
	$.preloadImages("popup/images/bg_center.jpg", "popup/images/close.png");
	
	// set the css
	$('#ksd_container').css({'padding':'10px'});
	
	hitman = false;
	$('#PopHitZone2').mouseover(function(){
		hitman = true;
	});

	$('#PopHitZone').mouseover(function(){
		if (ksd_displayRepeat && hitman == true) {
			// check if the popup box opend now has been closed
			if (ksd_popupOpen) {
				if(ksd_popupClosed && !global_showing_popup && hitman == true) {
					ksd_popup();
					$(".video").css('visibility','hidden');
				}	
			} else {
				ksd_popup();
				$(".video").css('visibility','hidden');
			}
		} else {		
			if (!global_showing_popup && hitman == true) {
				ksd_popup();
				$(".video").css('visibility','hidden');
			}
		}
	});
});
