﻿/*Common.js
Functions used accross the site
*/
$(document).ready(function() {
var DEXIA=(!DEXIA)?{}:DEXIA;
DEXIA.expandable=(function(){
	/*Expand/collapse blocks*/
	init=function(){
 
		$("div.collapsed").each(function () {
			$(this).children("h2").children("span:first").prepend('<a class="expandBut">&nbsp;</a>');
			$(this).children("div:first").children("div:first").slideToggle(250);
			$(this).children("h2").css({'background-position' : 'left top'});
			$(this).children("h2").children("span:first").css({'background-position' : 'right top'});
			
			//alert($(this).children("h2").outerHeight());
			
			heightOfTitle = $(this).children("h2").outerHeight();
			if(heightOfTitle>=82){
				$(this).children("h2").addClass("titlebox_5l");
			}else if(heightOfTitle>=66){
				$(this).children("h2").addClass("titlebox_4l");
			}else if(heightOfTitle>=51){
				$(this).children("h2").addClass("titlebox_3l");
			}else if(heightOfTitle>=35){
				$(this).children("h2").addClass("titlebox_2l");
			}else{
				$(this).children("h2").addClass("titlebox_1l");
			}
			
		});
		
		$("div.always_collapsed").each(function () {
			//$(this).children("h2").children("span:first").prepend('<a class="expandBut">&nbsp;</a>');
			$(this).children("div:first").children("div:first").slideToggle(250);
			$(this).children("h2").css({'background-position' : 'left top'});
			$(this).children("h2").children("span:first").css({'background-position' : 'right top'});
			
			//alert($(this).children("h2").outerHeight());
			
			heightOfTitle = $(this).children("h2").outerHeight();
			if(heightOfTitle>=82){
				$(this).children("h2").addClass("titlebox_5l");
			}else if(heightOfTitle>=66){
				$(this).children("h2").addClass("titlebox_4l");
			}else if(heightOfTitle>=51){
				$(this).children("h2").addClass("titlebox_3l");
			}else if(heightOfTitle>=35){
				$(this).children("h2").addClass("titlebox_2l");
			}else{
				$(this).children("h2").addClass("titlebox_1l");
			}
			
		});

		$("div.expanded").each(function () {
			$(this).children("h2").children("span:first").prepend('<a class="expandBut">&nbsp;</a>');
		});
		
		$("a.expandBut").click(function () { 
			var thisBox = $(this).parent().parent().parent();
			if(thisBox.hasClass("expanded")){
				thisBox.children("div:first").children("div:first").slideToggle();
	
				//alert(thisBox.children("h2").outerHeight());
				
				heightOfTitle = thisBox.children("h2").outerHeight();
				if(heightOfTitle>=82){
					thisBox.children("h2").addClass("titlebox_5l");
				}else if(heightOfTitle>=66){
					thisBox.children("h2").addClass("titlebox_4l");
				}else if(heightOfTitle>=51){
					thisBox.children("h2").addClass("titlebox_3l");
				}else if(heightOfTitle>=35){
					thisBox.children("h2").addClass("titlebox_2l");
				}else{
					thisBox.children("h2").addClass("titlebox_1l");
				}
				
				thisBox.children("h2").css({'background-position' : 'left top'});
				thisBox.children("h2").children("span:first").css({'background-position' : 'right top'});
				//thisBox.children("h2").children("a.expandBut").css({'background-position' : 'center top !important'});
				thisBox.removeClass("expanded");
				thisBox.addClass("collapsed");
			}else{
				thisBox.children("div:first").children("div:first").slideToggle();
				thisBox.children("h2").css({'background-position' : 'left -34px'});
				thisBox.children("h2").children("span:first").css({'background-position' : 'right -34px'});
				//thisBox.children("h2").children("a.expandBut").css({'background-position' : 'center bottom !important'});
				thisBox.removeClass("collapsed");
				thisBox.addClass("expanded");
			}
			
		});
	};
	return {
		init:init
	};
})();

(function($) {  
$.fn.toolTips = function(settings) {      
    options =  {  
        offsetX:  10,    // positionnement X par rapport au pointeur 
        offsetY:  -15,    // positionnement Y par rapport au pointeur 
        baseLine: "top", // alignement de par rapport au pointeur 
        click:    true   // autorise ou non le clic sur le lien 
    };  
    var options = $.extend(options, settings);  
      
    return this.each(function(){  
        var $$ = $(this);  
        var aTitle   = $$.attr('title') || '';  
        var aToolTip = $("<div></div>"); 
        var aCaption = ''; 
        var offsetX = 1; 
        var offsetY = 1; 
        $$.mouseover(function(e) {
			$$.attr("title", "");       
            if (aTitle != '') {  
                aCaption = '<div class="tooltip_caption"><span>'+ aTitle +'</span></div>';  
				aToolTip = $('<div class="tooltip_box"><span>'+ aTitle +'</span></div>');  
            }  
            aToolTip.appendTo("body").hide().css({ position:'absolute', top:0, left:0 }); 
            $$.mousemove(function(e) {  
				var moveBack="0px";
				if($$.hasClass("ico_cat2")){
					moveBack="-15px";
				}else if($$.hasClass("ico_cat3")){
					moveBack="-30px";
				}else if($$.hasClass("ico_cat4")){
					moveBack="-45px";
				}
				aToolTip.css({ 
                    left: e.pageX + (offsetX-130) + "px", 
                    top:  e.pageY + (offsetY-5) + "px",
                    backgroundPosition: "left "+moveBack
                });
            });  
            aToolTip.fadeIn();
        })  

        $$.mouseout(function(e) {
			$$.attr("title", aTitle).unbind("mousemove");       
            aToolTip.remove();  
        });
    });  
};  
})(jQuery);

var DEXIA=(!DEXIA)?{}:DEXIA;
DEXIA.usrList=(function(){
	init=function(){

		$("div.usrList").each(function (index) {
			
			$(this).addClass("usrList"+index);
			$(this).ref = "usrList"+index;
			
			$(this).append("<fieldset><div class='scrollable' style='height:150px'><ul class='resultList'></ul></div></fieldset>");
			$(this).append("<a class='but_checkAll' onclick='DEXIA.usrList.checkAll(\"usrList"+index+"\");'>checkall</a>");
			$(this).append("<a class='but_unCheckAll' onclick='DEXIA.usrList.unCheckAll(\"usrList"+index+"\");'>uncheckall</a>");
			
			var resultL = $(this).find("ul.resultList");
			$(this).find("ul.baseList li input:checked").each(function() {resultL.append("<li id='jsgen"+($(this).attr("id"))+"'>"+$(this).parent().find("label").html()+" <a onclick='DEXIA.usrList.uncheckItem(\""+$(this).attr("id")+"\");'>Remove</a></li>");});
			
			$(this).find("ul.baseList li input").click(function () { 
				if($(this).is(":checked")){resultL.append("<li id='jsgen"+$(this).attr("id")+"'>"+$(this).parent().find("label").html()+" <a onclick='DEXIA.usrList.uncheckItem(\""+$(this).attr("id")+"\");'>Remove</a></li>");}
				else{
					resultL.find("#jsgen"+$(this).attr("id")).remove();
				}
			});
			
			//console.log($(this).find(".optSiteInherit:checked"));
			
			if(!$(this).find("span.optSiteInherit input").attr("checked") && !$(this).find("span.optPageInherit input").attr("checked"))
			{			    
			    $(this).find("ul.baselist div.guestsOrOthers input").removeAttr("disabled");
			    
			    if($(this).find("div.guestsOrOthers span.optGuests input").attr("checked"))
			    {
				    $(this).find("ul.baseList li input").attr("disabled","disabled");
				}
			}
			if(!$(this).find("span.optSiteSpecific input").attr("checked") && !$(this).find("span.optPageSpecific input").attr("checked"))
			{				
			    $(this).find("div.guestsOrOthers span input").attr("disabled","disabled");
				$(this).find("ul.baseList li input").attr("disabled","disabled");
				$(this).toggleClass("disabled");
			}
						
		});

	};
	uncheckItem=function(userId){
		if(!$("#"+userId).parent().parent().parent().parent().parent().hasClass("disabled")) {
			$("#"+userId).removeAttr("checked");
			$("#jsgen"+userId).remove();
		}
		else alert("NO!");
	}
	unCheckAll=function(boxID){
		if(!$("."+boxID).hasClass("disabled")) {
			$("."+boxID).find("ul.baseList li input").removeAttr("checked");
			$("."+boxID).find("ul.resultList li").remove();
		}
		else alert("NO!");
	}
	checkAll=function(boxID){
		if(!$("."+boxID).hasClass("disabled")) {
			$("."+boxID).find("ul.baseList li input").removeAttr("checked");
			$("."+boxID).find("ul.resultList li").remove();
			$("."+boxID).find("ul.baseList li input").each(function() {
				//console.log($(this));
				$(this).attr("checked","1");
				$("."+boxID).find("ul.resultList").append("<li id='jsgen"+($(this).attr("id"))+"'>"+$(this).parent().find("label").html()+" <a onclick='DEXIA.usrList.uncheckItem(\""+$(this).attr("id")+"\");'>Remove</a></li>");	
			});
		}
		else alert("NO!");
		
	}

	disableGuestsOrOthers=function(boxID){
		$("."+boxID).find("div.guestsOrOthers span.optGuests input").attr("disabled","disabled");
		$("."+boxID).find("div.guestsOrOthers span.optOthers input").attr("disabled","disabled");
		disableBox(boxID);
	}
	enableGuestsOrOthers=function(boxID){
		$("."+boxID).find("div.guestsOrOthers span.optGuests").removeAttr("disabled");
		$("."+boxID).find("div.guestsOrOthers span.optOthers").removeAttr("disabled");
		
		$("."+boxID).find("div.guestsOrOthers span.optGuests input").removeAttr("disabled");
		$("."+boxID).find("div.guestsOrOthers span.optOthers input").removeAttr("disabled");
		if($("."+boxID).find("div.guestsOrOthers span.optOthers input").attr("checked"))
		{
		    enableBox(boxID);
		}
	}		

	disableBox=function(boxID){
		$("."+boxID).find("ul.baseList li input").attr("disabled","disabled");
		$("."+boxID).toggleClass("disabled");
	}
	enableBox=function(boxID){
		$("."+boxID).find("ul.baseList li input").removeAttr("disabled");
		$("."+boxID).toggleClass("disabled");
	}		
	return {
		init:init,
		uncheckItem:uncheckItem,
		unCheckAll:unCheckAll,
		checkAll:checkAll,
		disableGuestsOrOthers:disableGuestsOrOthers,
		enableGuestsOrOthers:enableGuestsOrOthers,
		disableBox:disableBox,
		enableBox:enableBox
	};
})();

$(function(){
	DEXIA.expandable.init(); 
	$("a.tooltip").toolTips();
	DEXIA.usrList.init();
})

$(function(){
	/*login panel actions*/
	$(".members_box").bind("mouseover",function(){
		var memberBox=$(this).find(".members_button")[0];
		$(memberBox).addClass("members_button-h");
		if(!$("body").find(".logged_in").length){
			$("body").bind("mouseover",function(){
				$(memberBox).removeClass("members_button-h");
				$("body").unbind("mouseover");
				return false;
			});
		}
		return false;
	});
	
	//sitemap expand collapse
	$(".sitemap a.expander").each(function(){
		var that=this;
		$(this).bind("click",function(){
			if ($(that).parent(".expandable").hasClass("collapsed")){
				$(that).parent(".expandable").removeClass("collapsed");
			} else {
				$(that).parent(".expandable").addClass("collapsed");
			}
			return false;
		});
	});
})

$(document).ready(function() {
	$(".content .btn").each(function(){
		if($(this).hasClass("btn_grey")){
			var maxL = 25;
			var spSiz = "84px";
			var btSiz = 155;
		}else{
			var maxL = 30;
			var spSiz = "111px";
			var btSiz = 155;
		}
		$(this).css("width",btSiz+"px");
		$(this).find("span:nth(0) span").css("width",spSiz);
		if($(this).find("span span span").html().length > maxL){
			$(this).find("span span span").text($(this).find("span span span").html().substr(0, maxL) + "...");
		}
	});
});

});