/**
 * Included in this file: 
 * - jQuery's Countdown Plugin
 * - www.yensdesign.com - Popup js
 */

	
	/**
	 * jQuery's Countdown Plugin
	 *
	 * display a countdown effect at given seconds, check out the following website for further information:
	 * http://dingyu.me/blog/posts/view/jquery-countdown-plugin
	 *
	 * @author Felix Ding
	 * @version 0.2
	 * @copyright Copyright(c) 2008. Felix Ding
	 * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
	 * @date 2008-03-09
	 * @lastmodified 2008-04-22 16:48    		 
	 * @todo error & exceptions handling
	*/
	jQuery.fn.countdown = function(options) {
		/**
		 * app init
		*/	
		if(!options) options = '()';
		if(jQuery(this).length == 0) return false;
		var obj = this;	
	
		/**
		 * break out and execute callback (if any)
		 */
		if(options.seconds < 0 || options.seconds == 'undefined')
		{
			if(options.callback) eval(options.callback);
			return null;
		}
	
		/**
		 * recursive countdown
		 */
		window.setTimeout(
			function() {
				jQuery(obj).html(String(options.seconds));
				--options.seconds;
				jQuery(obj).countdown(options);
			}
			, 1000
		);
	
		/**
	     * return null
	     */
	    return this;
	}
	
	/**
	 * stop the anonoymous setTimeout event
	 *
	 * @date 2008-04-22
	 */
	jQuery.fn.countdown.stop = function() {
		window.clearTimeout(setTimeout("0")-1);
	}

	/**
	 * Get the names and values of all cookies for the page.
	 *
	 * @example $.cookie();
	 * @desc Get all the cookies for the page
	 *
	 * @return an object with the name-value pairs of all available cookies.
	 * @type Object
	 *
	 * @name $.cookie
	 * @cat Plugins/Cookie
	 * @author Klaus Hartl/klaus.hartl@stilbuero.de
	 */

	/**/
	
	jQuery.icookie = function(name, value, options) {
	    if (typeof value != 'undefined'  ||  (name  &&  typeof name != 'string')) { // name and value given, set cookie
	        if (typeof name == 'string') {
	            options = options || {};
	            if (value === null) {
	                value = '';
	                options.expires = -1;
	            }
	            var expires = '';
	            if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
	                var date;
	                if (typeof options.expires == 'number') {
	                    date = new Date();
	                    date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
	                } else {
	                    date = options.expires;
	                }
	                expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
	            }
	            // CAUTION: Needed to parenthesize options.path and options.domain
	            // in the following expressions, otherwise they evaluate to undefined
	            // in the packed version for some reason...
	            var path = options.path ? '; path=' + (options.path) : '';
	            var domain = options.domain ? '; domain=' + (options.domain) : '';
	            var secure = options.secure ? '; secure' : '';
	            document.cookie = name + '=' + encodeURIComponent(value) + expires + path + domain + secure;
	        } else { // `name` is really an object of multiple cookies to be set.
	          for (var n in name) { jQuery.icookie(n, name[n], value||options); }
	        }
	    } else { // get cookie (or all cookies if name is not provided)
	        var returnValue = {};
	        if (document.cookie) {
	            var cookies = document.cookie.split(';');
	            for (var i = 0; i < cookies.length; i++) {
	                var cookie = jQuery.trim(cookies[i]);
	                // Does this cookie string begin with the name we want?
	                if (!name) {
	                    var nameLength = cookie.indexOf('=');
	                    returnValue[ cookie.substr(0, nameLength)] = decodeURIComponent(cookie.substr(nameLength+1));
	                } else if (cookie.substr(0, name.length + 1) == (name + '=')) {
	                    returnValue = decodeURIComponent(cookie.substr(name.length + 1));
	                    break;
	                }
	            }
	        }
	        return returnValue;
	    }
	};
	
	/***************************/
	//@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;
	
	//loading popup with jQuery magic!
	function loadPopup(){
		//loads popup only if it is disabled
		if(popupStatus==0){
			$("#interPopupBackground").css({
				"opacity": "0.7"
			});
			$(".adspace").fadeOut("fast");
			$("#leaderboard").fadeOut("fast");
			$("#interPopupBackground").fadeIn("slow");
			$("#interPopupBox").fadeIn("slow");
			
			popupStatus = 1;
		}
	}
	
	//disabling popup with jQuery magic!
	function disablePopup(){
		//disables popup only if it is enabled
		if(popupStatus==1){
			
			$("#interPopupBackground").fadeOut("slow");
			$("#interPopupBox").fadeOut("slow");
			$("#leaderboard").fadeIn("slow");
			$(".adspace").fadeIn("slow");
			
			
			popupStatus = 0;
		}
	}
	
	//centering popup
	function centerPopup(){
		//request data for centering
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
		var popupHeight = $("#interPopupBox").height();
		var popupWidth = $("#interPopupBox").width();
		//centering
		$("#interPopupBox").css({
			"position": "absolute",
			"top": windowHeight/2-popupHeight/2,
			"left": windowWidth/2-popupWidth/2
		});
		//only need force for IE6
		
		$("#interPopupBackground").css({
			"height": windowHeight
		});
		
	}
	
	
	//CONTROLLING EVENTS IN jQuery
	
	$(document).ready(function(){
		//LOADING POPUP
		//Has seen ad?
		//var cookie_days = 3;
		var countdown_sec = 12;
		
		
		//$.icookie('seencookie', 'true', { expires: 1, path: '/' });
		//console.log($.icookie());
		//console.log("Cookie" + $.icookie("seeninterstitial"));
		
	
		//if (($.icookie("seencookie") == 'true' && $.icookie("seeninterstitial") != 'true')) {

			//$.icookie('seeninterstitial', 'true', { expires: cookie_days, path: '/' });

			//Anticipate
			$("#interPopupCountDown").countdown({seconds: countdown_sec,callback: 'disablePopup();'});
			
			//centering with css
			centerPopup();
			//load popup
			loadPopup();
		//}
				
		//CLOSING POPUP
		//Click the x event!
		$("#interPopupClose").click(function(){
			disablePopup();
		});
		$("#interPopupCloseWords").click(function(){
			disablePopup();
		});
		//Click out event!
		$("#interPopupBackground").click(function(){
			disablePopup();
		});
		//Press Escape event!
		$(document).keypress(function(e){
			if(e.keyCode==27 && popupStatus==1){
				disablePopup();
			}
		});
	
	});
	
