/*
jQuery.fn.fadeTo = function(speed,to,callback) {
    return this.animate({opacity: to}, speed, function() {
        if (to == 1 && jQuery.browser.msie)
            this.style.removeAttribute('filter');
        if (typeof callback == 'function')
            callback();
    });
};
*/


$(document).ready(function() {

$(".vis1").css('opacity',0);
$("div#logo,div#sls,div#langSw").css('opacity',0);
$("div#map").css('opacity',0);
$("div#fadeWrap").css('opacity',0);

$(document).pngFix();
//$.browser.addSelectors('#quebecdot,#quebec,#mexico,#mexicodot,#igloolik,#igloolikdot,#blogModal,#bigSquare');
$(window).resize(sety);

$("#S1, #S2, #S3, #S4, #S5, #S6").hoverIntent(
                                                {
                                                sensitivity: 4, // number = sensitivity threshold (must be 1 or higher)
                                                interval: 50,  // number = milliseconds for onMouseOver polling interval   
                                                over: lightsUp,
                                                timeout: 1000,  // number = milliseconds delay before onMouseOut
                                                out: lightsOut
                                                }
                                                );
$("#S6").click(ajaxPage);

$(window).load(function(){

$("div#fadeWrap").css("visibility","visible");

sety();
$("img#mainrectimg").css('opacity',0);
$("div#fadeWrap").css('opacity',1);
$("img#mainrectimg").fadeTo(3000, 1, slowUp);
var initial = 1;



}); // end of window load


$("#mainText").ajaxStart(function(){
    $(this).css('opacity',0);
    $("#loading").css('visibility','visible');
});
$("#mainText").ajaxStop(function(){
    var trigger = $("a.blogTrigger");
    $(this).css('opacity',1);
    $("#loading").css('visibility','hidden');
    $("#blogModal").jqmAddTrigger(trigger);
});


}); // end of document ready

function blogClick(){
//  $('#blogModal').jqmAddTrigger('a.blogTrigger');
  var load = function(){$('div#blogPost').jScrollPane();};    
  $('#blogModal').empty().html('...Chargement...Loading...');
  $('#blogModal').jqm({ajax: '@href', trigger: 'a.blogTrigger', onLoad: load});
}



function blogReady(){
registerBlogLinks();
$("div#aa").jScrollPane({maintainPosition:false});
}

      function lightsUp() {
        $(this).find(".vis1").fadeTo(800, 1);
      }

      function lightsOut() {
	    $(this).find(".vis1").fadeTo(1500, 0);
      }

      function slowUp() {
        $("div#logo,div#sls,div#langSw").fadeTo(4000, 1);
      }

      function ajaxPage() {
	stupidworkaround();
      }    	
      	

function stupidworkaround() {
    $.ajax({
	type:'POST',
	dataType:'html',
	success:function(i){$('#mainText').html(i);},
	complete:function(){dochange()},
	url:'/pages/show/pagename/introduction'
	});
	}



function showPlayer(cult) {

var flashvars = {};
flashvars.autostart = "true";
flashvars.controlbar = "over";
flashvars.height = "300";
flashvars.width = "400";
flashvars.file = "/videos/fibo_"+cult+".flv";
flashvars.backcolor = "0x000000";
flashvars.frontcolor = "0x888888";
flashvars.lightcolor = "0xCC0000";
flashvars.screencolor = "0x000000";
flashvars.allowSciptAccess = "always";
//flashvars.showfsbutton = "true";
//flashvars.click = "/images/flvplaybutton.jpg";
var params = {};
params.allowfullscreen = "true";
params.allowscriptaccess = "always";
params.wmode="transparent";
var attributes = {};
attributes.id = "video";
swfobject.embedSWF("/videos/mediaplayer.swf", "video", "400", "300", "9.0.0", false, flashvars, params, attributes);
dochange();
}

function sety()

{
var cont = $("#container");
var logo = $("#logo");
var cw = $(cont).width();
var lw = $(logo).width();
var x = '';                   
var y = 420;			//The minimum size is dependent on the height
var xw = $(window).width();
var yw = $(window).height();
var div = $("div#aa");
if (yw > y){
y = yw;
}

y = y*0.94;
x = y*1.6;			//Establish the proportion

var z = xw/(x*1.09);		//make sure we don't overflow the sides


if(z < 1){
    x = (x*z);
    y = (y*z);
    } 
$(cont).height(y);
$(cont).width(x);


var margin = (xw-x)/2;
$(cont).css({'margin-left':margin+'px', 'margin-right':margin+'px'});
div.jScrollPane({maintainPosition:false});
//alert('WWidth: '+xw+' CalcWidth: '+x+' Margins: '+margin+' Cont: '+$(cont).width());
var vmargin = (yw-y)/2;
$(cont).css({'top':vmargin+'px'});

adjpix();
}

function dochange(){
    var what = $("p img");
    if(what){
        what.load(adjpix);
	    }
    $("div#aa").jScrollPane({maintainPosition:false});
//    $("#aa").css('opacity',1);
    $("#loading,#sls").css('visibility','hidden');
}

function adjpix() {
    var stdboxwidth = 962;
    var curboxwidth = $("#container").width();
    var factor = curboxwidth/stdboxwidth;
    var what = $("p img");
    what.each(function(i){
    var picwidth = Img.getNaturalWidth(this);
    var picheight = Img.getNaturalHeight(this);
    var newwidth = picwidth*factor;
    var newheight = picheight*factor;
    $(this).width(newwidth+"px");
    $(this).height(newheight+"px");
});
}




