// ready.js
// A appeler lorsque le DOM est complet
// SGL - 2010-03-03
// 
//

// Conflit entre Prototype JS et JQuery
// A migrer tout JQuery
var $jq = jQuery.noConflict();

function ready() {
	$jq("ul.sf-menu").superfish({ delay: 400, dropShadows: true, autoArrows: false, onBeforeShow: function() { sfinit(this); }, onHide: function() { sfclose(this); } });

	$jq("ul.sf-menu").each(function() {
		var ul = this;
		$jq(this).children('li').each(function() {
		
			// Calcul de l'offset top
			var i = $jq(ul).children('li').index(this);
			var top = (i*21)-21;
			
			// Calcul de l'offset left
			//$jq('<span style="display:inline;">'+$jq(this).children('a').html()+'</span>').appendTo($jq(this).children('a').html('')); // fonctionne pas dans IE6...
			
			var left_fleche = parseInt($jq(this).children('a').children('span').width())+15;
			if (left_fleche == 0) left_fleche = 85;
			
			var left = left_fleche+parseInt($jq(this).children('ul').prev('.fleche').width());
			$jq(this).children('ul').css('top',top+'px').prev('.fleche').css('top',(top+16)+'px');
			$jq(this).children('ul').css('left',left+'px').prev('.fleche').css('left',left_fleche+'px');
			$jq(this).children('ul').children('li:first').addClass('premier');
			$jq(this).children('ul').children('li:last').addClass('dernier');
			
			$jq(this).children('a').click(function() {
				if ($jq(this).attr('href') == '') {
					return false;
				}
			})
		});
	});


	// Colorbox
	$jq(".videobox").colorbox({width:580, height:420, iframe:true});
	$jq(".imagebox").colorbox({ });
	$jq(".thickbox").colorbox({width:580, height:420, iframe:true});
	$jq(".pagebox").colorbox({width:700, height:600, iframe:true});
	$jq(".shadowbox").colorbox({width:820, height:530, iframe:true});

	// Roll block
	$jq('h3.roll a').each(function() {
		$jq(this).click(function() {
			return roll_click(this);
		});
	});


	if ($jq.browser.msie) {
		$jq('.fleche').addClass('fleche_ie');
		if (parseInt($jq.browser.version) < 7)
			$jq('#wrapper').css('backgroundImage','url(/images/ul/tuiles/shadow_ie6.gif)');
	}


	if (typeof doc_questionnaire_ready == "function")
		doc_questionnaire_ready();
	if (typeof sitq_ready == "function")
		sitq_ready();
	if (typeof startSB == "function")
		startSB();
}

function sfinit(e) {
	e.prev('.fleche').fadeIn("normal");
}

function sfclose(e) {
	e.prev('.fleche').stop(false,true).hide();
}
function sfCloseAll(a) {
	if (!$jq(a).parent("li").hasClass('sfHover')) {
		$jq("li.sfHover").removeClass("sfHover");
		$jq("ul.sf-menu").children('li').children('ul').hide();
		$jq("ul.sf-menu").find('div.fleche').hide();
	}
}

/**
 * Roll Blocks
 *
 */
function roll_click(handle) {

	if ($jq(handle).hasClass('selected')) {
		$jq(handle).removeClass('selected');
		$jq(handle).parent().next().slideUp('normal',function() {

			if ($jq.browser.msie) {
				$jq('#footer').css('top',($jq('#body').height()+120)+'px');
				$jq('#footer').css('bottom','0px');
				$jq('#footer').css('top','auto');
			}
			
		});
	} else {
		$jq('#footer').css('bottom','0');
		$jq(handle).addClass('selected');
		$jq(handle).parent().next().slideDown('normal',function() {
			if ($jq.browser.msie) {
				$jq('#footer').css('top',$jq(document).height()-60+'px');
				$jq('#footer').css('bottom','auto');
			}
		});
	}
	
	return false;
}


// Ancienne fonction d'ouverture de fenetres utilisee dans les textes
function launchwin(winurl,winname,largeur,hauteur) {
   var winfeatures;
   var LeftWindow = (screen.width - largeur) /2;
   var TopWindow = (screen.height - hauteur) /2;
   winfeatures ='width='+largeur+',height='+hauteur+',top='+TopWindow+',left='+LeftWindow+',scrollbars=1';
   window.open(winurl,winname,winfeatures);
}