window.log = function()
{
	log.history = log.history || [];   // store logs to an array for reference
	log.history.push(arguments);
	if(this.console){
		console.log( Array.prototype.slice.call(arguments) );
	}
};

stage_width = 0, stage_height = 0, officiel_hover = '', traque_hover = '', saison_hover = '';

$(document).ready(function(){
	blood.init();
	t_location = window.location.href;
	t_location = t_location.split('/#/');
	if(t_location[1] != undefined) blood.flashWebsite();
})

$(window).resize(function(){
	blood.init();
}).load(function(){
    blood.init();
	blood.showElmnts();
	setTimeout(blood.init, 200);
	setTimeout(blood.init, 2000);
	setTimeout(blood.init, 3000);
	setTimeout(blood.init, 5000);
	$('#main #traque').find('.face').css('opacity', 0)
})

var blood = new function()
{
	this.init = function()
	{
		clearInterval(officiel_hover);
		clearInterval(traque_hover);
		clearInterval(saison_hover);
		blood.screenSize();
		blood.placeBG();
		blood.traque();
		blood.officiel();
		blood.saison();
		$(document).pngFix();
	}
	
	this.screenSize = function()
	{
		stage_width = $(window).width();
		stage_height = $(window).height();
		stage_ratio = stage_width / stage_height;
	}
	
	
	this.placeBG = function()
	{
		bg = $('#main .bg');
		w_bg = bg.find('img').width();
		h_bg = bg.height();
		if(stage_ratio >= 1.6){
			if(stage_height >= 700){
				bg.find('img').attr({
					width: stage_width,
					height: stage_width / 1.6
				}).css({marginLeft: '0'})
			}else{
				n_ml = (stage_width - (stage_height * 1.6)) / 2;
				bg.find('img').attr({
					width: stage_height * 1.6,
					height: stage_height
				}).css({marginLeft: n_ml+'px'})
			}
		}else{
			n_ml = (stage_height * 1.6 - stage_width) / 2;
			bg.css({
				marginLeft: '-'+n_ml+'px',
				width: stage_width,
				height: '100%'
			})
			bg.find('img').attr({
				width: stage_height * 1.6,
				height: stage_height
			})
		}
	}
	
	this.traque = function()
	{
		var traque = $('#main #traque');
		traque.unbind();
		w_traque = (474*($('#main .bg').find('img').width()))/1680;
		traque.find('img:first').attr({
		    width: w_traque,
		    height: w_traque / 1.93
		}).css({
		    border: '0 solid yellow'
		})
		ml_traque = '-'+(w_traque/2);
		mt_traque = traque.find('.ti').height();
		traque.find('div').css({
		    marginTop: '-'+mt_traque+'px',
		    bottom: 0
		});
		traque.css({
			width: w_traque,
			height: (w_traque / 1.93) + mt_traque,
			left: stage_width*.49,
			marginLeft: ml_traque+'px',
			marginBottom: mt_traque*.2
		}).click(function(e){
		    window.location.href = "http://www.orangecinemaseries.fr/evenement/trueblood3/#/la-traque";
			blood.flashWebsite();
		}).hover(function(){
		    $(this).find('.ti').css({opacity: 0})
		    $(this).find('.face').css({opacity: 1})
		}, function() {
			$(this).find('.ti').css({opacity: 1})
			$(this).find('.face').css({opacity: 0})
		});
		
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		    traque.css({
    			paddingBottom: mt_traque
    		})
		}
	}
	
	this.officiel = function()
	{
		var officiel = $('#main #officiel');
		officiel.unbind();
		w_officiel = (302*($('#main .bg').find('img').width()))/1680;
		ml_officiel = '-'+(3.6*(w_officiel/2));
		mt_officiel = 0;
		officiel.css({
			width: w_officiel,
			marginLeft: ml_officiel+'px',
			marginTop: 0
		})
	}
	
	this.saison = function()
	{
		var saisons = $('#main #saison');
		saisons.unbind();
		w_saisons = (467*($('#main .bg').find('img').width()))/1680;
		ml_saisons = ((w_saisons/2)*1.1);
		mt_saisons = 0;
		saisons.css({
			width: w_saisons,
			marginLeft: ml_saisons+'px'
		}).click(function(e){
			blood.flashWebsite();
		})
	}
	
	this.showElmnts = function()
	{
		$('#main .bg').css('opacity', 1)
		setTimeout(function(){
			$('#main #traque').css('opacity', 1)
			$('#main #traque .ti').css({display: 'block', opacity: 1})
		}, 300)
		setTimeout(function(){
			$('#main #officiel, #main #saison').css('opacity', 1)
			$('#main').css('background', '#000');
		}, 600)
	}
	
	this.flashWebsite = function()
	{   
		$(window).unbind();
		var params = {
			quality: "high",
			scale: "noscale",
			wmode: "opaque",
			allowscriptaccess: "always",
			allowfullscreen :"true",
			bgcolor: "#000000"
		}
		var attributes = {
			id: "container",
			name: "container"
		}
		
		var flash_version = swfobject.getFlashPlayerVersion();
		if(flash_version.minor == 0 && flash_version.major == 0){
		    $('#container').remove();
            $('body').css('background', '#000 url(DATA/images/no-flash.jpg) no-repeat center center').append('<a href="http://get.adobe.com/flashplayer" target="_blank" style="display:block; position;absolute; top:0; left:0; width: 100%; height: 100%; cursor: pointer;">&nbsp;</a>');
		}else swfobject.embedSWF("main.swf", "container", "100%", "100%", "10", "DATA/swf/expressInstall.swf", flashvars, params, attributes);
	}
}

function showLike()
{
    //$('#like-overlay').css('display', 'block');
}

function openBox(url, name, confirm)
{
	var w=670;
	var h=370;
	
	if(!window[confirm]){
		window[confirm] = function(hash)
		{
			var flash = document.getElementById("container");
			flash[confirm](hash);
		}
	}
	
	if (navigator.appName=="Netscape"){
		myleft=window.screenX;
		mytop=window.screenY;
	}else if (navigator.appName.indexOf("Microsoft")!=-1){
		myleft=window.screenLeft;
		mytop=window.screenTop;
	}
	
	if( typeof( window.innerWidth ) == 'number' ){
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ){
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ){
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	wleft = myleft + ((myWidth - w) / 2);
	wtop = mytop + (myHeight/2 - h/2) + 60;
	
	var face_win = window.open(url,name,'width=' + w + ', height=' + h + ', ' +'left=' + wleft + ', top=' + wtop + ', location=no, menubar=no, status=no, toolbar=no, scrollbars=no, resizable=no');
	
	var timer = setTimeout(function(){
        var can_pop = true;
        if (face_win == null || typeof(face_win) == "undefined") {
            can_pop = false;
        } else if(navigator.userAgent.toLowerCase().indexOf('chrome') > -1){
            if(face_win.window.innerHeight != 0){
                can_pop = true;
            }else{
                can_pop = false;
            }
        } else {
        	can_pop = true;
        }

        if(!can_pop){
            $('#container').remove();
            $('body').css('background', '#0b0000 url(DATA/images/no-popup.jpg) no-repeat center center').append('<a href="javascript:window.location.reload()" style="display:block; position;absolute; top:0; left:0; width: 100%; height: 100%; cursor: pointer; z-index: 2000;">&nbsp;</a>');
        }else{
            face_win.resizeTo(w, h);
            face_win.moveTo(wleft, wtop);
            face_win.focus();
        }
    }, 160);
}
