/* Javscript - Lessieur */

/***** Sommaire *****/
// 10 -  Write Flash
// 20 - autoRoll
// 30 - Field cleaner
// 40 - Tabs
// 45 - Switchs
// 50 - autoRoll
// 60 - Time fielder
// 70 - Layers
// 75 - rescueFrame
// 80 - Popups
// 85 - GridViewCleaner
// 90 - Compteur de caractère pour les TextBox
// 98 - onUnload
// 99 - onLoad
/*******************/

/***** 10 - Write Flash *****/
function writeFlash(url,id,w,h,o){
	document.write('\n<object id="'+id+'" type="application/x-shockwave-flash" data="'+url+'" width="'+w+'" height="'+h+'">\n');
	document.write('\t<param name="movie" value="'+url+'" />\n');
	if(o != ''){
		var flashOptionsTab = new Array();
		flashOptionsTab = o.split('|');
		for(i=0; i<flashOptionsTab.length; i++){
			var tempParam = flashOptionsTab[i].split('#')[0];
			var tempValue = flashOptionsTab[i].split('#')[1];
			document.write('\t<param name="'+tempParam+'" value="'+tempValue+'" />\n');
		}
	}
	document.write('</object>\n');
}

function writeFlashEmbed(url,id,w,h,o){
	document.write('\n<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" id="'+id+'" width="'+w+'" height="'+h+'">\n');
	// Object
	document.write('\t<param name="movie" value="'+url+'" />\n');
	if(o != ''){
		var flashOptionsTab = new Array();
		flashOptionsTab = o.split('|');
		for(i=0; i<flashOptionsTab.length; i++){
			var tempParam = flashOptionsTab[i].split('#')[0];
			var tempValue = flashOptionsTab[i].split('#')[1];
			document.write('\t<param name="'+tempParam+'" value="'+tempValue+'" />\n');
		}
	}
	// Embed
	document.write('\t<embed src="'+url+'" width="'+w+'" height="'+h+'" id="'+id+'" name="'+id+'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"');
	if(o != ''){
		var flashOptionsTab = new Array();
		flashOptionsTab = o.split('|');
		for(i=0; i<flashOptionsTab.length; i++){
			var tempParam = flashOptionsTab[i].split('#')[0];
			var tempValue = flashOptionsTab[i].split('#')[1];
			document.write(' '+tempParam+'="'+tempValue+'"');
		}
	}
	document.write(' />\n');
	document.write('</object>\n');
}

/***** 20 - autoRoll *****/
var onAdd="-on";
function autoRoll(Img,Event){
	if(!Img.onmouseout && Event == true) Img.onmouseout = function(){ autoRoll(Img) };
	imgExt = Img.src.substring(Img.src.lastIndexOf("."));
	imgName = Img.src.substring(0,Img.src.lastIndexOf("."));
	imgOn = imgName+onAdd+imgExt;
	if(imgName.indexOf(onAdd) != -1) imgName = imgName.substring(0,imgName.length-onAdd.length);
	imgOff = imgName+imgExt;
	if(Img.src.indexOf(onAdd) != -1) Img.src = imgOff;
	else Img.src = imgOn;
}

function autoRollEvents(){
	var imgTable = arguments[0].getElementsByTagName('img');
	for(var i=0; i<imgTable.length; i++){
		if(imgTable[i].src.indexOf(onAdd) == -1) imgTable[i].onmouseover = function(){autoRoll(this,true)};
	}
}

/***** 30 - Field cleaner *****/
function fieldCleaner(field,message){
	if(field.value == message){
		field.value = '';
	}
}

/***** 40 - Tabs *****/
function showTab(id,index){
	for(var i=0; i<tabsLength[id]; i++){
		_('tabs'+id+'Tab'+i).style.display = (index == i)? 'block' : 'none';
		if((_('tabs'+id+'Nav','li',i,'img',0).src.indexOf(onAdd) != -1 && index != i) || (_('tabs'+id+'Nav','li',i,'img',0).src.indexOf(onAdd) == -1 && index == i)){
			autoRoll(_('tabs'+id+'Nav','li',i,'img',0),false);
		}
	}
}

var tabsLength = new Array();
function buildTabsNav(id,array){
	var tabNavCh = '<ul>\n';
	var id = parseInt(id.replace('tabs',''));
	for(var i=0; i<array.length; i++){
		var index = parseInt(array[i].id.replace('tabs'+id+'Tab',''));
		tabNavCh += '\t<li><a href="javascript:showTab('+id+','+index+');">'+_('tabs'+id+'Tab'+index,'a',0).innerHTML+'</a></li>\n';
		_('tabs'+id+'Tab'+index).removeChild(_('tabs'+id+'Tab'+index,'a',0));
	}
	tabNavCh += '</ul>\n';
	tabNavCh += '<div class="clear"></div>\n';
	_('tabs'+id+'Nav').innerHTML = tabNavCh;
	showTab(id,0);
}

/***** 45 - Switchs *****/
function showSwitch(id,index){
	for(var i=0; i<switchsLength[id]; i++){
		_('switchs'+id+'Switch'+i).style.display = (index == i)? 'block' : 'none';
		if(isIE){
			_('switchs'+id+'Nav','img',i).style.filter = (index == i)? 'alpha(opacity=50)' : 'alpha(opacity=100)';
		} else {
			_('switchs'+id+'Nav','img',i).style.opacity = (index == i)? '0.5' : '1';
		}
	}
}

var switchsLength = new Array();
function buildSwitchsNav(id,array){
	var switchNavCh = '<ul>\n';
	var id = parseInt(id.replace('switchs',''));
	for(var i=0; i<array.length; i++){
		var index = parseInt(array[i].id.replace('switchs'+id+'Switch',''));
		switchNavCh += '\t<li onmouseover="showSwitch('+id+','+index+');"><a href="'+_('switchs'+id+'Switch'+index,'a',0).href+'">'+_('switchs'+id+'Switch'+index,'a',0).innerHTML+'<br />'+_('switchs'+id+'Switch'+index,'h3',0).innerHTML+'</a></li>\n';
	}
	switchNavCh += '</ul>\n';
	switchNavCh += '<div class="clear"></div>\n';
	_('switchs'+id+'Nav').innerHTML = switchNavCh;
	showSwitch(id,0);
}

/***** 50 - autoRoll *****/
var onAdd="-on";
function autoRoll(Img,Event){
	if(!Img.onmouseout && Event == true) Img.onmouseout = function(){ autoRoll(Img) };
	imgExt = Img.src.substring(Img.src.lastIndexOf("."));
	imgName = Img.src.substring(0,Img.src.lastIndexOf("."));
	imgOn = imgName+onAdd+imgExt;
	if(imgName.indexOf(onAdd) != -1) imgName = imgName.substring(0,imgName.length-onAdd.length);
	imgOff = imgName+imgExt;
	if(Img.src.indexOf(onAdd) != -1) Img.src = imgOff;
	else Img.src = imgOn;
}

function autoRollEvents(){
	var imgTable = arguments[0].getElementsByTagName('img');
	for(var i=0; i<imgTable.length; i++){
		if(imgTable[i].src.indexOf(onAdd) == -1) imgTable[i].onmouseover = function(){autoRoll(this,true)};
	}
}

function autoRollLink(Link,Event){
    var newClassName=""
    if(Event) newClassName = "hover";
    Link.parentNode.className = newClassName;
}

function autoRollEventsLink(){
	var linkTable = arguments[0].getElementsByTagName('a');
	for(var i=0; i<linkTable.length; i++){
	    if(linkTable[i].parentNode.className!="on"){
		    linkTable[i].onmouseover = function(){autoRollLink(this,true)};
		    linkTable[i].onmouseout = function(){autoRollLink(this,false)};
	    }
	}
}

/***** 60 - Time fielder *****/
function decreaseTime(field){
	var curentValue = parseFloat(field.value);
	if((curentValue-10) > 10 && curentValue != 99999){
		field.value = (curentValue-10);
		_('timeCount').innerHTML = field.value+' minutes';
	} else if(curentValue == 20){
	    field.value = 10;
	    _('timeCount').innerHTML = 'Express';
	} else if(curentValue == 10){
	    field.value = 99999;
	    _('timeCount').innerHTML = 'Indiff&eacute;rent';
	}
}
function increaseTime(field){
	var curentValue = parseFloat(field.value);
	if(curentValue == 99999){
	    field.value = 10;
	    _('timeCount').innerHTML = 'Express';
	} else {
        field.value = (curentValue+10);
        _('timeCount').innerHTML = field.value+' minutes';
    }
}

/***** 70 - Layers *****/
function lostIdentifier(){
	if(_('identifierLayer')){
		_('identifierLayer').style.display = 'block';
		rescueFrame(_('identifierLayer'),10);
	}
}
function findedIndentifier(){
	if(_('identifierLayer')){
		_('identifierLayer').style.display = 'none';
		endRescueFrame();
	}
}

/***** 75 - rescueFrame *****/
function rescueFrame(){
	if(isIE && !isIE7){		
		if(!_('rescueFrame')){
			var rescueObj = document.createElement('iframe');
			if(arguments[0].id == 'identifierLayer'){
				var rescueElement = arguments[0].parentNode.appendChild(rescueObj);
			} else {
				var rescueElement = document.body.appendChild(rescueObj);
			}
			rescueElement.id = 'rescueFrame';
			rescueElement.style.position = 'absolute';
			rescueElement.style.visibility = 'hidden';
			rescueElement.style.border = '0';
			rescueElement.style.filter = 'alpha(opacity=0);';
		}
		_('rescueFrame').style.zIndex = (typeof arguments[1] == 'number')? arguments[1] : _Z(arguments[0])+1;
		if(arguments[0].id == 'identifierLayer'){		
			_('rescueFrame').style.top = '0'; 
			_('rescueFrame').style.right = '2px'; 
		} else {
			_('rescueFrame').style.top = _C('y',arguments[0])+'px'; 
			_('rescueFrame').style.left = _C('x',arguments[0])+'px'; 
		}
		_('rescueFrame').style.width = arguments[0].offsetWidth+'px'; 
		_('rescueFrame').style.height = arguments[0].offsetHeight+'px'; 
		_('rescueFrame').style.visibility = 'visible';
	}
}
function endRescueFrame(){
	if(isIE && !isIE7){
		_('rescueFrame').style.visibility = 'hidden';
	}
}

function htmlPop (htmlElement,visible,isReload) {
	_(htmlElement).style.visibility = visible;
	if(isReload){
	    _('sendToFriend','input',0).value = '';
	    if(_('sendToFriend','input',1)){
			_('sendToFriend','input',1).value = '';
	    }
	    if(_('sendToFriend','input',2)){
			_('sendToFriend','input',2).value = '';
	    }
	    _('resultCont').style.display = 'none';
	}
	if(htmlElement == 'sendToFriend'){
		var layerCoord = _C('y',_('sendToFriend'));
		window.scrollTo(0,layerCoord);
	}
}

function gotoHome(){
	if(!BaseUrl){
		document.location = 'Default.aspx';;
	} else {
		document.location = BaseUrl+'/Default.aspx';
	}
}

/***** 80 - Popups *****/
function openReglementGoutteBonus(){
	win=window.open(BaseUrl + "/Popup/Reglement.htm","reglement", "menubar=no, status=no, scrollbars=no, menubar=no, width=605,height=420");
	win.focus();
}
function openMentions(){
	if(!BaseUrl){
		pathTmp = '';
	} else {
		pathTmp = BaseUrl+'/';
	}
	win=window.open(pathTmp+"Popup/Mentions.htm","mentions", "menubar=no, status=no, scrollbars=no, menubar=no, width=605,height=420");
	win.focus();
}
function openConditions(){
	win=window.open(BaseUrl + "/Popup/Conditions.htm","conditions", "menubar=no, status=no, scrollbars=no, menubar=no, width=605,height=420");
	win.focus();
}
function openReglementMobile(){
	win=window.open(BaseUrl + "/Popup/Reglement-mobile.htm","reglementMobile", "menubar=no, status=no, scrollbars=no, menubar=no, width=605,height=420");
	win.focus();
}
function openTips(){
	win=window.open(BaseUrl + "/Popup/Gagner-Points.htm","gagnerPoint", "menubar=no, status=no, scrollbars=no, menubar=no, width=605,height=420");
	win.focus();
}


/***** 85 - GridViewCleaner *****/

function getParentNode (node) {
	return node.parentNode;
}

function GridViewCleaner () {
	var nodes = __("tr","pager");
	if (nodes) {
		var findTabs = new Array();
		for (var i=0;i<nodes.length;i++) {
			findTabs[i] = getParentNode(nodes[i]);
			while (findTabs[i].className.indexOf("tableResults")==-1&&findTabs[i].tagName.toLowerCase()!="body") {
				findTabs[i] = getParentNode(findTabs[i]);
			}
			if (findTabs[i].className.indexOf("tableResults")!=-1) {
				var clean = nodes[i].getElementsByTagName("tr")[0].innerHTML;
				clean = clean.replace(/TD/g,"td");
				clean = clean.replace(/<\/td>/g,"");
				var items = clean.split("<td>");
				nodes[i].parentNode.removeChild(nodes[i]);
				var str = '<div class="pager"><ul>';
				for (var j=1;j<items.length;j++) {
					var addClassName = '';
					if (j==1) { addClassName = ' class="first"'; }
					if (items[j].indexOf('Page$First')!=-1) { addClassName = ' class="prev"'; }
					if (items[j].indexOf('Page$Last')!=-1) { addClassName = ' class="next"'; }
					str += '<li'+addClassName+'>' + items[j] + '</li>';
				}
				str += '</ul></div>';
				findTabs[i].parentNode.parentNode.innerHTML += str;
			}
		}
	}
}


/***** 90 - Compteur de caractère pour les TextBox *****/

var currentLength;
var verifTimer;
var lastValue;
var maxLength;
var fieldToTest;
var lengthToEnd;
function testlong(){
	currentLength = fieldToTest.value.length;
	lengthToEnd = maxLength - currentLength;
	if(currentLength > maxLength){
		fieldToTest.value = lastValue;
	}
	if(lengthToEnd >= 0){
		_(fieldToTest.parentNode.id+'Count').innerHTML = lengthToEnd+' caract&egrave;re(s) max.';
		lastValue = fieldToTest.value;
	}
	verifTimer = setTimeout('testlong();',10);
}

/****** 99 - onLoad *****/
window.onload = function(){
	
	// 20 - autoRoll
	if(_('mainNavigation')){
		autoRollEventsLink(_('mainNavigation'));
	}
	if(_('navPromo')){
		autoRollEvents(_('navPromo'));
	}
	if(__('div','tools').length > 0){
		for(var i=0; i<__('div','tools').length; i++){
			autoRollEvents(__('div','tools')[i]);
		}
	}
	
	GridViewCleaner();
	
	// 45 - Switchs
	var switchsTab = __('div','switchs');
	if(switchsTab.length > 0){
		for(var i=0; i<switchsTab.length; i++){
			switchsTab[i].id = 'switchs'+i;
			_('switchs'+i,'div',0).id = 'switchs'+i+'Nav';
		}
		var switchTab = __('div','switch');
		var currentSwitchs = 'switchs0';
		var switchCount = 0;
		var subSwitchs = new Array();
		for(var i=0; i<switchTab.length; i++){
			if(switchTab[i].parentNode.id != currentSwitchs){
				switchsLength[switchsLength.length] = subSwitchs.length;
				buildSwitchsNav(currentSwitchs,subSwitchs);
				subSwitchs = new Array();
				switchCount = 0;
				currentSwitchs = switchTab[i].parentNode.id;
			}
			switchTab[i].id = currentSwitchs+'Switch'+switchCount;
			subSwitchs[subSwitchs.length] = switchTab[i];
			switchCount ++;
		}
		switchsLength[switchsLength.length] = subSwitchs.length;
		buildSwitchsNav(currentSwitchs,subSwitchs);
	}
	
	// 60 - Time fielder
	if(_('timeCount')){
	    var curentValue = __('input','timeField')[0].value;
	    if(curentValue == 10){
	        _('timeCount').innerHTML = 'Express';
	    } else if(curentValue == 99999){
	         _('timeCount').innerHTML = 'Indiff&eacute;rent';
	    } else {
	        _('timeCount').innerHTML = __('input','timeField')[0].value+' minutes';
	    }
		_('decrease').onclick = function(){ decreaseTime(__('input','timeField')[0]); }
		_('increase').onclick = function(){ increaseTime(__('input','timeField')[0]); }
	}
	
	/*if(isLostPassword == true){
	    lostIdentifier();
	}*/
	
	// 90 - Compteur
	if(_('textPerso')){
		_('textPerso','textarea',0).onfocus = function(){
			clearTimeout(verifTimer);
			fieldToTest = _('textPerso','textarea',0);
			maxLength = 250;
			lastValue = '';
			currentLength = '';
			testlong();
		}
		_('textPerso','textarea',0).onblur = function(){ clearTimeout(verifTimer); }
		_('textPersoCount').innerHTML = 250-_('textPerso','textarea',0).value.length+' caract&egrave;re(s) max.';
	}
	
	if(_('textQuestion')){
		_('textQuestion','textarea',0).onfocus = function(){
			
			clearTimeout(verifTimer);
			fieldToTest = _('textQuestion','textarea',0);
			maxLength = 1000;
			lastValue = '';
			currentLength = '';
			testlong();
		}
		_('textQuestion','textarea',0).onblur = function(){ clearTimeout(verifTimer); }
		_('textQuestionCount').innerHTML = 1000-_('textQuestion','textarea',0).value.length+' caract&egrave;re(s) max.';
	}
	
	if(_('textFAQ')){
		_('textFAQ','textarea',0).onfocus = function(){
			fieldCleaner(this,'Votre question');
			clearTimeout(verifTimer);
			fieldToTest = _('textFAQ','textarea',0);
			maxLength = 1000;
			lastValue = '';
			currentLength = '';
			testlong();
		}
		_('textFAQ','textarea',0).onblur = function(){ clearTimeout(verifTimer); }
		_('textFAQCount').innerHTML = 1000-_('textFAQ','textarea',0).value.length+' caract&egrave;re(s) max.';
	}
	
	if(_('textRecipe')){
		_('textRecipe','textarea',0).onfocus = function(){
			clearTimeout(verifTimer);
			fieldToTest = _('textRecipe','textarea',0);
			maxLength = 200;
			lastValue = '';
			currentLength = '';
			testlong();
		}
		_('textRecipe','textarea',0).onblur = function(){ clearTimeout(verifTimer); }
		_('textRecipeCount').innerHTML = 200-_('textRecipe','textarea',0).value.length+' caract&egrave;re(s) max.';
	}
	
}

function showPanel(f){
	if(f.checked == true){
		_S('del',_('communityAdd'),'communitySmall');
		_S('add',_('communityAdd'),'communityFull');
		_('communityAddCont').style.display = 'block';
	} else {
		_S('add',_('communityAdd'),'communitySmall');
		_S('del',_('communityAdd'),'communityFull');
		_('communityAddCont').style.display = 'none';
	}
}

function showFullPanel(){
	_S('del',_('communityAdd'),'communitySmall');
	_S('add',_('communityAdd'),'communityFull');
	_('communityAddCont').style.display = 'block';
}

function hideFullPanel(){
	_S('add',_('communityAdd'),'communitySmall');
	_S('del',_('communityAdd'),'communityFull');
	_('communityAddCont').style.display = 'none';
}

function SetCookie (name, value) {
	var argv=SetCookie.arguments;
	var argc=SetCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=name+"="+escape(value)+
		((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
		((path==null) ? "" : ("; path="+path))+
		((domain==null) ? "" : ("; domain="+domain))+
		((secure==true) ? "; secure" : "");
}

function getCookieVal(offset) {
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1)
			endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
	var arg=name+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen) {
		var j=i+alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	}
	return null;
}

/**** Visite Guidée  ***/		
function gotoSite(){
	window.location = 'Default.aspx';
}

function endVisite(){
	window.location = 'Default.aspx';
}