//DomReady
window.addEvent("load",function(e){
	
	Custom.init();
	heightEqualizer(['menu_category','menu_features']);
	resizeLastRightLi();
});


window.addEvent("domready",function(e){						
		
		addCalendarTips()
			if($("sector_select")!=null){
				$("sector_select").addEvent("click",function(e){
					sectorDisplay();
				});
				$("sector_select").addEvent("mouseout",function(e){
					startCloseSector();
				});
				$("sector_select").addEvent("mouseover",function(e){
					if(sectorOpen){
						sectorOpener();
					}
				});
				$("sector_list").addEvent("mouseover",function(e){		
						sectorOpener();		
				});
				$("sector_list").addEvent("mouseout",function(e){
					startCloseSector();
				});	
			
			}
			if($("status_select")!=null){
				$("status_select").addEvent("click",function(e){
					statutDisplay();
				});
				$("status_select").addEvent("mouseout",function(e){
					startCloseStatus();
				});
				$("status_select").addEvent("mouseover",function(e){
					if(statusOpen){
						statutOpener();
					}
				});
				$("status_list").addEvent("mouseover",function(e){		
					statutOpener();
				});
				$("status_list").addEvent("mouseout",function(e){
					startCloseStatus();
				});
			}
			
			if($("sorting")!=null){
				$("sorting").addEvent("click",function(e){
					
					sortingDisplay();
				});
				$("sorting_select").addEvent("mouseout",function(e){
					startCloseSorting();
				});
				$("sorting_select").addEvent("mouseover",function(e){
					if(sortingOpen){
						sortingOpener();
					}
				});
				$("sorting_list").addEvent("mouseover",function(e){		
					sortingOpener();
				});
				$("sorting_list").addEvent("mouseout",function(e){
					startCloseSorting();
				});
			}
			
			if($("menu_button")!=null){
				// MENU BUTTON
				$("menu_button").addEvent("click", function(e){									
					menuDisplay();			
				});	
				$("menu_button").addEvent("mouseover", function(e){			
					if(menuOpen){
						openMenu();
					}
				});	
				$("menu_button").addEvent("mouseout", function(e){			
					startCloseMenu();
				});		
				// MENU LIST 
				$("menu_list").addEvent("mouseover", function(e){					
					openMenu();
				});	
				$("menu_list").addEvent("mouseout", function(e){		
					startCloseMenu();			
				});	
			}
				initInput()		
	})

// ----------------
// LOGIN BOX SCRIPT
// ----------------

function inputChange(input){
	
	if (input=="f_login_password"){
		var prevElement=$("f_login_password").getPrevious();
		$("f_login_password").destroy()
		var html='<input id="f_login_password" name="f_password"  type="password" />'
		var newElement=new Element("input",{
					name:"f_password"	, id:"f_login_password",type:"password"	   
		})
		newElement.inject(prevElement,"after")
		newElement.focus()
		return;
		//document.getElementById(input).type='password';
	}
	document.getElementById(input).value='';
}

// ----------------
// QUICKPOLL
// ----------------
var arrayIntervale=[]
var totalLongueur=210;		
function answerQuickpoll(answerURL,container_id,do_anim){
	var strURL=""
	$$("#f_quickpoll input").each(function (e){
								
			if(e.get("type")=="radio" && e.checked){
				strURL="/"+e.get("name")+"/"+e.value+".do"
			}									
	})
	if(strURL==""){
		$("qp_make_choice").setStyle("display","block");
		return
	}
	var rqst=new Request({
			method:"get",url:answerURL+strURL,data:"",
			onSuccess:function(txt){
				
				buildQPAnswers(txt,container_id,do_anim)
			}
	}).send()	
}

function buildQPAnswers(stringResult,container_id,do_anim){
		//vide le quickpoll
		//<div id="sticksContainers">
		//<div id="answer_list">
		
		$(container_id).empty()
		//construction des container
		$(container_id).set("html","<div id='"+container_id+"sticksContainers' class='sticksContainers'></div><div id='"+container_id+"answer_list' class='answer_list'></div>")
		
		var arrayAnswers=stringResult.split("|")
		
	//	alert("nbr answer="+arrayAnswers.length)
		for(var i=0;i<arrayAnswers.length;i++){
			var arrayElement=arrayAnswers[i].split(";")	
			createPoolElement((i+1),arrayElement[0],arrayElement[1],container_id,do_anim)	
		}
}


	function createPoolElement(num,pourcent,optionTitle,container_id,do_anim){
						// Create TITLE
						var p= new Element("p",{})
						
						var span=new Element("span",{})
						span.set("html",num)
						span.inject(p)
						p.set("html",p.get("html")+optionTitle)
						
						// CREATE STICK
						var div= new Element("div",{'class':'stick'})
						var numAnswer= new Element("div",{'class':'answer'})
						numAnswer.set("html",num)
						
						var stick= new Element("div",{'class':'color'+num})
						
						numAnswer.inject(div)					
						stick.inject(div)					
						
						var percent= new Element("div",{'class':'percent'})						
						percent.inject(stick,"after")
						if(do_anim){
							percent.set("html","0%")		
						}else{
							percent.set("html",pourcent+"%")		
						}
										
						
						var clear= new Element("div",{'class':'clear'})						
						clear.inject(percent,"after")
						
						///on injecte le div complet
						div.inject($(container_id+"sticksContainers"))
						//on inject la reponse
						p.inject($(container_id+"answer_list"))
						if(!do_anim){
							var lngStick=(pourcent/100)*totalLongueur
							stick.setStyles({"width":lngStick+"px"})
						}else{
							arrayIntervale[num]=setInterval("grow("+num+","+pourcent+")",20)	
						}
						
				}
			
				function grow(num,pourcent){
						var stick=$$(".color"+num)
						
						var percent=$$(".color"+num).getNext(".percent")
						var intPercent=parseInt(percent.get("html"))
						
						if(intPercent<pourcent){		
							intPercent++
							percent.set("html",intPercent+"%")
							var lngStick=(intPercent/100)*totalLongueur
							stick.setStyle("width",lngStick+"px")	
						}else{
							var lngStick=(intPercent/100)*totalLongueur
							stick.setStyle("width",lngStick+"px")	
							clearInterval(arrayIntervale[num])	
						}
				}	
			
// ----------------
// VIDEO
// ----------------

function addPlayerVideo(video){
	var videoDiv=new Element("div",{
			styles:{"width":"647px","height":"437px ","z-index":"1","position":"absolute","background":"#f5f5f5"},
			id:"home_video"
	})
	var inDiv=new Element("div",{
			styles:{"width":"647px","z-index":"1","position":"absolute"},
			id:"inDiv"
	})
	
	videoDiv.set('html', 'Installer le plugin Flash pour profiter pleinement de notre site. Vous pouvez le t&eacute;l&eacute;charger gratuitement en cliquant <a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" target="_blank">ici</a>.<br><br>Installeer de Flash plugin om ten volle van onze website te genieten. U kan hem <a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" target="_blank">hier</a> gratis downloaden.');
	
	inDiv.inject(videoDiv,"top");
	
	videoDiv.inject($("content"),"top");
	
	
	var obj = new Swiff('/wasyb/static/front/swf/ml/wa_player.swf', {
		id: 'myBeautifulMovie',
		width: 647,
		height: 444,
		container :inDiv,
		params: {
			wmode: 'transparent'
		},
		vars: {
			movie: video
		},
		callBacks: {
		}
	});
	
}

function closeVideo(){
		$("home_video").destroy()
}

// ----------------
// CALENDAR
// ----------------
function getCalendar(dateToShow){
			var rqst=new Request({
				method:"get",url:dateToShow,data:"",
				onSuccess:function(txt){
				
					$("feature_calendar").set("html",txt)
					addCalendarTips()
				},
				onFailure:function(xhr){
					alert(xhr)
				}

			}).send()
	}
	
	function addCalendarTips(){
				$$(".day_overlayer").each(function(e){
				e.getParent().addEvent("mouseover",function(e){					
					this.getChildren(".day_overlayer").setStyles({display:"block"})
				})	
				e.getParent().addEvent("mouseout",function(e){					
					this.getChildren(".day_overlayer").setStyles({display:"none"})
				})		
			})
	}
// ----------------
// HEIGHT EQUALIZER
// ----------------

function heightEqualizer(heightArray){
	
	var maxHeight = 0;
	// 1. SELECT THE BIGGEST HEIGHT	
	for(i=0;i<heightArray.length;i++){	
		if($(heightArray[i])==null){
			return;
		}
	
		if($(heightArray[i]).getSize().y > maxHeight){
			maxHeight = $(heightArray[i]).getSize().y
		}
	}
	// 2. SET THE NEW HEIGHT
	for(i=0;i<heightArray.length;i++){
		$(heightArray[i]).setStyles({"height":maxHeight+"px"});
	}
}

// -------------
// ARTICLES COMMENTS
// -------------
function reverseList(list){
			var arrayElement=[]
			$(list).getChildren().each(function(e){
					//alert(e.get("html"))
					arrayElement.push(e)		
					//alert(arrayElement[arrayElement.length-1].get("html"))
			})
			//$(list).empty()
			arrayElement.reverse()
			for(var i=0;i<arrayElement.length;i++){
				arrayElement[i].inject($(list))	
			}
}
var orderCommentNormal="normal"
function sortComment(order){
	if(orderCommentNormal!=order){
		reverseList("comment_container")
		orderCommentNormal=order
	}
}

// -------------
// SECTOR SELECT
// -------------

var closeTimerSector;

function startCloseSector(){
	closeTimerSector=window.setTimeout( sectorClose,100);
}
function stopCloseSector(){
	window.clearTimeout(closeTimerSector);
}

var sectorOpen = false;

function sectorOpener(){
	$("sector_list").getPrevious().setStyle("background","url(/wasyb/static/front/img/ml/background_dropdown_over.gif) no-repeat");
	$("sector_list").setStyle("display","block");
	sectorOpen=true;
	stopCloseSector();
}
function sectorClose(){
	$("sector_list").getPrevious().setStyle("background","url(/wasyb/static/front/img/ml/background_dropdown_out.gif) no-repeat");
	$("sector_list").setStyle("display","none");
	sectorOpen=false;
	stopCloseSector();
}


function sectorDisplay(){	
	if(!sectorOpen){
		sectorOpener()
	} else {
		sectorClose()
	}
}


// -------------
// STATUS SELECT
// -------------

var closeTimerStatus;

function startCloseStatus(){
	closeTimerStatus=window.setTimeout( statutClose,100);
}
function stopCloseStatus(){
	window.clearTimeout(closeTimerStatus);
}

var statusOpen = false;

function statutOpener(){
	$("status_list").getPrevious().setStyle("background","url(/wasyb/static/front/img/ml/background_dropdown_over.gif) no-repeat");
	$("status_list").setStyle("display","block");
	statusOpen=true;
	stopCloseStatus();
}
function statutClose(){
	$("status_list").getPrevious().setStyle("background","url(/wasyb/static/front/img/ml/background_dropdown_out.gif) no-repeat");
	$("status_list").setStyle("display","none");
	statusOpen=false;
	stopCloseStatus();
}



function statutDisplay(){	
	if(!statusOpen){
		statutOpener();
	} else {
		statutClose();
	}
}
// --------------
// Resize Last right LI
// --------------
function resizeLastRightLi(){
		if($('column_spacer')!=null){
			var footerPosition=$('content').getSize().y		
			
			var li_count = 0;
			var featuresSize=0;
			//
			
			$('features').getChildren('li').each(function(e) {
				if(e.get("id")!="column_spacer"){
					li_count++;
					featuresSize+=(e.getSize().y+10);
				}
			});
			var lastLISize=(footerPosition-featuresSize)+18;
			if(lastLISize<=0){
				$('column_spacer').setStyle('display', 'none');
			} else {
				$('column_spacer').setStyle('height', lastLISize);
			}
			
			
		}
	}

// --------------
// SORTING SELECT
// --------------

var closeTimerSorting;

function startCloseSorting(){
	closeTimerSorting = window.setTimeout(sortingClose,100);
}
function stopCloseSorting(){
	window.clearTimeout(closeTimerSorting);
}

var sortingOpen = false;

function sortingOpener(){
	$("sorting_list").getPrevious().setStyle("background","url(/wasyb/static/front/img/ml/background_dropdown_over.gif) no-repeat");
	$("sorting_list").setStyle("display","block");
	sortingOpen = true;
	stopCloseSorting();
}
function sortingClose(){
	$("sorting_list").getPrevious().setStyle("background","url(/wasyb/static/front/img/ml/background_dropdown_out.gif) no-repeat");
	$("sorting_list").setStyle("display","none");
	sortingOpen = false;
	stopCloseSorting();
}


function sortingDisplay(){	
	if(!sortingOpen){
		sortingOpener();
	} else {
		sortingClose();
	}
}


// ------------
// MENU DISPLAY
// ------------

var closeTimer;

function startCloseMenu(){
	closeTimer=window.setTimeout(closeMenu,100);
}
function stopCloseMenu(){
	window.clearTimeout(closeTimer);
}
function openMenu(){
	stopCloseMenu();
	$("menu_button").removeClass("menu_out");
	$("menu_button").addClass("menu_over");
	$("menu_list").setStyles({"visibility":"visible"});
}
function closeMenu(){
	menuOpen=false;
	stopCloseMenu();
	$("menu_button").removeClass("menu_over");
	$("menu_button").addClass("menu_out");
	$("menu_list").setStyles({"visibility":"hidden"});
}


var menuOpen = false;
function menuDisplay(){
	if(menuOpen==false){
		openMenu()
		menuOpen=true;
	} else {
		closeMenu()
		menuOpen=false;
	}
}


// -------------
// PRINT PREVIEW
// -------------

function print_preview(page){
	window.open(page,"StartYourBusiness","width=800, height=550, directories=no, location=no, menubar=no, scrollbars=yes, status=no, resizable=no");
}


// -------------------------------
// Prise en charge du focus par IE
// -------------------------------

function initInput(){
	
	$$("#content input").each(function(e){
		//alert(e)	
		//document.getElementsByTagName("input")[i].onfocus=colour;
		//document.getElementsByTagName("input")[i].onblur=none;
		e.onfocus=colour;
		e.onblur=none;
	})	
}
//Couleur du focus
function colour(event){
	this.style.border='#AFAFAF 1px solid';
}
//Pas de couleur
function none(event){
	this.style.border='#ABC3BE 1px solid';
}

function showAnswer(el){
	$('answer_'+el).setStyles({display:"block"});	
	resizeLastRightLi()
}

/*
function init(){
	if(document.getElementsByTagName("input")){
		for(i=0;i<document.getElementsByTagName("input").length;i++){
			document.getElementsByTagName("input")[i].onfocus=colour;
			document.getElementsByTagName("input")[i].onblur=none;
		}
	}
}
*/

function wa_doPopup(mypage, myname, w, h, scroll) {
  LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
  TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
  settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
  window.open(mypage,myname,settings)
}
