/************************************
* Script for the Avantages template *
************************************/

var arrayContent=[]

                  
function buildBoxes(){
	$(".box_containers").each(function(){
		arrayContent.push(Array())
		$(this).children(".box_advantages").each(function(e){				
			arrayContent[(arrayContent.length-1)].push("<div class='box_advantages' id='" + $(this).attr("id") + "'>"+$(this).parent().children("div:eq(0)").html()+"</div>");
			$(this).parent().children("div:eq(0)").remove();
		})	
	})		
	  
	moveBoxes()
}


function moveBoxes(){		
	for(i=0;i<arrayContent.length;i++){
		for(j=0;j<arrayContent[i].length;j++){
			$("#zone"+i+"_column"+(j%3)).append(arrayContent[i][j]);				
		}
	}
}


function init_Avantages() {
	//select all types
	selectAllList_Avantages('advantages',true);
	
	//select all domains
	selectAllList_Avantages('domains',true);
	
	// select all products
	selectAllList_Avantages('products',true);

	
	// disabled products
	selectAllList_Avantages('products',true);
	disabledList_Avantages('products',true);


	// init events
	var advantage = document.getElementById("advantage");
	advantage.onclick = function() {
		
		// disabled products
		selectAllList_Avantages('products',true);
		disabledList_Avantages('products',true);

		// enable domains
		selectAllList_Avantages('domains',true);
		disabledList_Avantages('domains',false);
	
		// enable advantages
		selectAllList_Avantages('advantages',true);
		disabledList_Avantages('advantages',false);
	
		// show order by advantages
		showHide('orderByProduct','hide');
		showHide('orderByType','show');
		
		// filter
		filter_Avantages(); 
	}
	
	var product = document.getElementById("product");
	product.onclick = function() {
		
		// disabled advantages
		selectAllList_Avantages('advantages',true);
		disabledList_Avantages('advantages',true);
	
		// enable domains
		selectAllList_Avantages('domains',true);
		disabledList_Avantages('domains',false);
	
		// enable products
		selectAllList_Avantages('products',true);
		disabledList_Avantages('products',false);
	
		// show order by products
		showHide('orderByType','hide');
		showHide('orderByProduct','show');
		
		// filter
		filter_Avantages(); 
	}

	var i;
	for(i=0 ; i < $("input[name=advantages[]]").length ; i++) {
		$("input[name=advantages[]]")[i].onclick = function() {
			
			// check status for select all
			selectAllCheckStatus_Avantages('advantages');
			
			// filter
			filter_Avantages();
		}
	}
	
	for(i=0 ; i < $("input[name=domains[]]").length ; i++) {
		$("input[name=domains[]]")[i].onclick = function() {
			
			// check status for select all
			selectAllCheckStatus_Avantages('domains');
			
			// filter
			filter_Avantages();
		}
	}
	
	for(i=0 ; i < $("input[name=products[]]").length ; i++) {
		$("input[name=products[]]")[i].onclick = function() {
			
			// check status for select all
			selectAllCheckStatus_Avantages('products');
			
			// filter
			filter_Avantages();
		}
	}
	
	var all_advantages = document.getElementById("all_advantages");
	all_advantages.onclick = function() {
		// update status for all checkbox
		selectAllList('advantages',all_advantages);
	
		// filter
		filter_Avantages();
	}
	
	var all_domains = document.getElementById("all_domains");
	all_domains.onclick = function() {
		// update status for all checkbox
		selectAllList('domains',all_domains);
	
		// filter
		filter_Avantages();
	}
	
	var all_products = document.getElementById("all_products");
	all_products.onclick = function() {
		// update status for all checkbox
		selectAllList('products',all_products);
	
		// filter
		filter_Avantages();
	}
}


function filter_Avantages() {
	var cx, i, div_id;

	// advantages
	for(i=0 ; i < $("input[name=advantages[]]").length ; i++) {
		div_id = $("input[name=advantages[]]")[i].value;
		if(document.getElementById(div_id) != null) {
			if($("input[name=advantages[]]")[i].checked) {
				showHide(div_id,'show');
			} else {
				showHide(div_id,'hide');
			}
		}
	}
	
	// products
	for(i=0 ; i < $("input[name=products[]]").length ; i++) {
		div_id = $("input[name=products[]]")[i].value;
		if(document.getElementById(div_id) != null) {
			if($("input[name=products[]]")[i].checked) {
				showHide(div_id,'show');
			} else {
				showHide(div_id,'hide');
			}
		}
	}
	
	// domains
	for(cx=0 ; cx < $("input[name=advantages[]]").length ; cx++) {
		div_id_advantages = $("input[name=advantages[]]")[cx].value;
		for(i=0 ; i < $("input[name=domains[]]").length ; i++) {
			div_id = div_id_advantages + "/" + $("input[name=domains[]]")[i].value;
			if(document.getElementById(div_id) != null) {			
				if($("input[name=domains[]]")[i].checked) {
					showHide(div_id,'show');
				} else {
					showHide(div_id,'hide');
				}
			}
		}
	}
	for(cx=0 ; cx < $("input[name=products[]]").length ; cx++) {
		div_id_products = $("input[name=products[]]")[cx].value;
		for(i=0 ; i < $("input[name=domains[]]").length ; i++) {
			div_id = div_id_products + "/" + $("input[name=domains[]]")[i].value;
			if(document.getElementById(div_id) != null) {			
				if($("input[name=domains[]]")[i].checked) {
					showHide(div_id,'show');
				} else {
					showHide(div_id,'hide');
				}
			}
		}
	}

	saveFilter_Avantages();
}


function selectAllList_Avantages(element, value) {
	$("input[name=" + element + "[]]").attr("checked",value);
	$("input[name=all_" + element + "]").attr("checked",value);
}

function disabledList_Avantages(element, value) {
	$("input[name=" + element + "[]]").attr("disabled",value);
	$("input[name=all_" + element + "]").attr("disabled",value);
}

function selectAllCheckStatus_Avantages(element) {
	var i=0;
	var flag=true;

	for(i=0 ; i < $("input[name=" + element + "[]]").length ; i++) {
		if(!$("input[name=" + element + "[]]")[i].checked) {
			flag = false;
		}
	}

	if(flag) {
		$("input[name=all_" + element + "]").attr("checked",true);						
	} else {
		$("input[name=all_" + element + "]").attr("checked",false);
	}
}


function setCookie(name, value, days) {
    var expDate = new Date();
    expDate.setTime(expDate.getTime() + (days * 24 * 3600 * 1000));
    document.cookie = name + "=" + escape(value) + ";expires=" + expDate.toGMTString();
}
 
function delCookie(name) { 
 	setCookie(name, "", -1);
}
 
function getCookie(nom) {
 	start = document.cookie.indexOf(nom + "=")
    if (start >= 0) {
        start += nom.length + 1;
        end = document.cookie.indexOf(";", start);
        if (end < 0) { 
        	end = document.cookie.length;
        }
        
        return unescape(document.cookie.substring(start, end));
    }
        
 	return "";
}


function saveFilter_Avantages() {
	var save = "";
	var id, id_avantage, id_products;
	
	// advantages
	for(i=0 ; i < $("input[name=advantages[]]").length ; i++) {
		id = $("input[name=advantages[]]")[i].value;
		if(!$("input[name=advantages[]]")[i].checked) {
			if(save.length > 0)
				save = save + "-";
			save = save + id; 					
		}
	}
	
	// products
	for(i=0 ; i < $("input[name=products[]]").length ; i++) {
		id = $("input[name=products[]]")[i].value;
		if(!$("input[name=products[]]")[i].checked) {
			if(save.length > 0)
				save = save + "-";
			save = save + id; 	
		}
	}

	// domains
	for(i=0 ; i < $("input[name=domains[]]").length ; i++) {
		id = $("input[name=domains[]]")[i].value;
		if(!$("input[name=domains[]]")[i].checked) {
			if(save.length > 0)
				save = save + "-";
			save = save + id; 	
		}
	}
    
    
    // orderby advantage or product
    if(save.length > 0) {
		save = save + "-";
	}
	if(document.getElementById("product").checked) {
		save = save + "product";
	} else {
		save = save + "advantage";
	}
    
    
    setCookie("EuromutCookie", save, 1);
}


function restoreFilter_Avantages() {
	var restore = getCookie("EuromutCookie");
	
	var orderby = "advantage";

	if(restore.length > 0) {
	    var vec = restore.split("-");
	    for(cx=0 ; cx < vec.length ; cx++) {
	    	if((vec[cx] == "product") || (vec[cx] == "advantage")) {
				orderby = vec[cx];
			} else {
				document.getElementById("cb" + vec[cx]).checked = false;
			}
		}
	    
	    orderby = "product"; //delete me for enabled advantage choice, and remove 'display: none' in tempalte 'Avantages.jsp'
	    
		selectAllCheckStatus_Avantages('advantages');
		selectAllCheckStatus_Avantages('domains');
		selectAllCheckStatus_Avantages('products');
		
		if(orderby == "product") {
			var product = document.getElementById("product");
			product.checked = true;
			
			// disabled advantages
			disabledList_Avantages('advantages',true);
		
			// enable products
			disabledList_Avantages('products',false);
		
			// show order by products
			showHide('orderByType','hide');
			showHide('orderByProduct','show');
		}
		
		// filter
		filter_Avantages();
	}
}


function selectProduct() {
	var url = new String(window.location);
	if(url.indexOf("#") != -1) {
		var product = document.getElementById("product");
		if(product.checked == false) {
			product.checked = true;
			
			// disabled advantages
			selectAllList_Avantages('advantages',true);
			disabledList_Avantages('advantages',true);
		
			// enable domains
			selectAllList_Avantages('domains',true);
			disabledList_Avantages('domains',false);
		
			// enable products
			selectAllList_Avantages('products',true);
			disabledList_Avantages('products',false);
		
			// show order by products
			showHide('orderByType','hide');
			showHide('orderByProduct','show');
			
			// filter
			filter_Avantages();
			
			// relaod the page
			window.location = url;
		}
	}	
}
