(function($, window, undefined) {

    var current_domain = location.hostname,
    prehome_days = 30,
    prehome_cookie_time = prehome_days * 24 * 60 * 60 * 1000,
    domain = '',
    path = '/luxembourg/fr/prehome/',
    $prehomeDiv,
    $prehomeOverlay;


    function readCookie(name) {
		var nameEQ = name + "=",
			ca = document.cookie.split(';'),
			i,
			l,
			c;
		
		for(i = 0, l = ca.length; i < l; ++i) {
			c = ca[i];
			while (c.charAt(0) == ' ') {
				c = c.substring(1,c.length);
			}
			if (c.indexOf(nameEQ) === 0) {
				return c.substring(nameEQ.length,c.length);
			}
		}
		return '';
    }
    

    function setCookie(name, value) {
		var date = new Date(),
			now = date.getTime(),
			cd = current_domain.match(/([^\.]+\.[^\.]+)$/),
			domain = (cd  ? cd[0] : current_domain),
			expires;
		
		date.setTime(date.getTime() + prehome_cookie_time); 
		expires = date.toGMTString();
		
		document.cookie = name + '=' + value + '|' + now + '; expires=' + expires+';path=/;domain=' + domain;
    }
	
    function prehomeLoad(prehome, cookieName) {
		document.cookie='testGPLU=true;path=/';
		
		if (readCookie('testGPLU') === null) {
			return;
		}
		
		var cookie = readCookie(cookieName),
			show = false;
		
		if (cookie === null || cookie === '') {
			setCookie(cookieName, '1');
			show = true;
		}
		else {
			
			var cc = cookie.split('|');
			if (cc[0] == 1) {
				var dd = new Date();
				if (dd.getTime() > ( parseInt(cc[1]) + prehome_cookie_time)) { 
					setCookie(cookieName,'2');
					show = true;
				}
			}
		}
		
		if (show) {
			prehomeDirectLoad.call(this, prehome);
		}
    }
	
    function prehomeDirectLoad(prehome) {
		$.get(domain + path + prehome, null, prehomeLoaded, 'html');
    }
	
    function prehomeClose() {
		$prehomeDiv.remove();	
		$prehomeOverlay.remove();
    }
	
    function prehomeLoaded(data, status, xhr) {
		var $body = $('body');
		
		$prehomeDiv = $('<div class="gpf-prehome" id="phcontainer">'+data+'</div>').css({
				position: "absolute",
				left:0,
				top:0,
				textAlign: "left",
				width:'100%',
				zIndex:1001
				
		});
		
		$prehomeOverlay = $('<div class="gpf-prehome" id="phoverlay"></div>').css({
				zIndex:1000,
				backgroundColor:"#000",
				opacity:.8,
				width:"100%",
				height:"100%",
				position: "absolute",
				display: "none",
				top: 0,
				left: 0,
				right: 0,
				bottom: 0,
			position: "absolute",
			cursor:"pointer"
			});

		$prehomeDiv.find('.close').click(function onClickClose() {
			prehomeClose();
		});
/*
		$prehomeDiv.on('click', ':not(a, img)', function() {
			prehomeClose();
		});
	*/	
		$prehomeDiv.find('img',this).imagesLoaded(function onImagesLoaded() {
			$prehomeOverlay.show();
			$prehomeDiv.fadeIn(300);	
		});
	
		$body.append($prehomeOverlay);
		$body.append($prehomeDiv);
    }
    

	
    $.fn.imagesLoaded = function(callback){
		var elems = this.filter('img'),
	len = elems.length,
			blank = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
		
		elems.bind('load',function onElementsLoad(){
			if (--len <= 0 && this.src !== blank){ callback.call(elems,this); }
		}).each(function(){
			// cached images don't fire load sometimes, so we reset src.
			if (this.complete || this.complete === undefined) {
				var src = this.src;
				// webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
				// data uri bypasses webkit log warning (thx doug jones)
				this.src = blank;
				this.src = src;
			}
		});
		
		return this;
    };

	
	
    window.$prehome = {
		load: prehomeLoad,
		dload: prehomeDirectLoad,
		close: prehomeClose
    };
})(jQuery, window);

