// JavaScript Document

$(function() {
    $('a.spip_out, .item h2 a').click( function() {
        window.open(this.href);
        return false;
    });

		var get_luminosity = function(pos) {
			var color = parseInt(window.bg_colors[pos],16);
			return ((color & 255)+((color >> 8) & 255)+((color >> 16) & 255))/3;			
		}

    var test_color = function($img) {
    	var pos = Math.abs(parseInt($img.css("top"),10)/$img.height()),
    			mean_luminosity;
			if(pos<0.25) {
				mean_luminosity = Math.min(get_luminosity("top_left"),get_luminosity("top_center"),get_luminosity("center_left"),get_luminosity("center_center"),get_luminosity("bottom_left"),get_luminosity("bottom_center"));
			} else {
				mean_luminosity = Math.min(get_luminosity("top_center"),get_luminosity("center_center"),get_luminosity("bottom_center"));
			}
			if(mean_luminosity<64) {
				$("#supersized, #supersized2").append($("<div class='bg_opacity'>"));
			}
			
			
    }

		if(window.screen.availWidth>1024) {
			var $c, $s, $cimg, $simg;
			
			function  set_style() {
					$cimg.attr('style', $simg.attr('style'));
					//test color
					test_color($simg);
			}

			$.supersized({
					//Background image
					slides	:  [ { image : bg_img } ]					
			});
		
			$s = $('#supersized');
			$c = $s.clone(true, true).attr("id","supersized2");

			$c.css({
					'height': 100,
					'overflow': 'hidden',
					'zIndex': 3
			}).show().appendTo('body');
		
			$cimg = $c.find('img');
			$simg = $s.find('img');
		
			window.setTimeout(set_style, 100);
			$(window).bind("load resize",set_style);
		} else {
			$("#supersized").remove();
			$("body").append("<div id='supersized'><img src='"+bg_img+"' /></div>");
		}
    
    var resize = function() {
			if(window.screen.availWidth<=1024) {
	    	var sz = $("#supersized").height(0),
						container = $("#container").height("auto").css({marginBottom:""}),
						dh = $(document).height();
				var dim = Math.max(dh,$(window).height())-100-67-50;//header,footer,margins
				if(container.height()<dim) {
					container.css({marginBottom: 30+dim-container.height()});
				}
				sz.height($(document).height());
				var bg = $("#supersized img").load(resize);
				bg.css({width: "auto !important", height: "100% !important"});
				bg.css({left: -(bg.width()-sz.width())/2, top: "0 !important"});
				if(bg.width()<sz.width()) {
					bg.css({width:"100% !important",height:"auto !important"});
					bg.css({left: 0, top: (-(bg.height()-sz.height())/2) + " !important"});
				}
				test_color(bg);
			}
    }
    
    $(window).resize(resize);
    resize();
    
    if($.browser.msie && $.browser.version=="7.0") {
    	$("#header").clone().attr("id","header2").css({zIndex: 4}).appendTo("body");
    }
    
});

