/**********************************************
* Browser definition                          *
**********************************************/
var browserOK = false;
if (navigator.userAgent.toLowerCase().indexOf('msie') != -1 && navigator.userAgent.toLowerCase().indexOf('opera') == -1) {
	var isIE = true;
	var menutimer;
	//get IE version
	if (navigator.userAgent.toLowerCase().indexOf("msie 5.5") != -1) {
		var isIE55 = true;
		browserOK = true;
	} else if (navigator.userAgent.toLowerCase().indexOf("msie 6") != -1)  {
		var isIE6 = true;
		browserOK = true;
	} else if (navigator.userAgent.toLowerCase().indexOf("msie 7") != -1)  {
		var isIE7 = true;
		browserOK = true;
	}
	var timerdiv;
	var timerli;
	var elementdiv;
	var elementli;
} else if (navigator.userAgent.toLowerCase().indexOf('opera') != -1) {
	//not compatible for now (tried with opera 8 & 9)
	var isOp = true;
} else if (navigator.userAgent.toLowerCase().indexOf('firefox') != -1 || 
		  (navigator.userAgent.toLowerCase().indexOf('mozilla') != -1 && (navigator.userAgent.toLowerCase().indexOf('rv:1.7') != -1 || navigator.userAgent.toLowerCase().indexOf('rv:1.8') != -1))) {
	browserOK = true;
	var isMoz = true;
}
var gEffectDuration = 180; /*millisecondes*/

/*add some events on menu for IE compatibility only*/
function startList() {
	if (isIE && !isIE7) {
		var divContenu = document.getElementById("menuh").getElementsByTagName('LI');
		for (var i=0; i<divContenu.length; i++) {
			div = divContenu[i];
		  	if (div.nodeName=="LI" && div.className == "CMS_lvl2") {
		  		div.onmouseenter=function() {
		  			this.className = "CMS_lvl2 over";
		    	}
		  		div.onmouseleave=function() {
		  			this.className = "CMS_lvl2";
				}
			}
		}
	}
}

// SIFR REPLACEMENTS
var sifrReplacements = function(){		
	// siFR 3
	var handOfSean = {
		src: '/swf/HandOfSean.swf'
	};
	sIFR.activate(handOfSean);

	sIFR.replace(handOfSean, {
		selector: '#ContentInterieur h1.h1Interieur',
		//tuneHeight: -6,
		wmode: 'transparent',
		selectable: 1,
		tuneHeight: -6,
		css: [ '.sIFR-root {font-size: 20;color: #FF5400; background-color : #FFFFFF}']
	});	
	/*sIFR.replace(handOfSean, {
		selector: '#zoneGauche #titreLink',
		//tuneHeight: -6,
		wmode: 'transparent',
		selectable: 1,
		tuneHeight: -6,
		css: [ '.sIFR-root {font-size: 14;color: #FF4800; background-color : #FFFFFF}']
	});
	sIFR.replace(handOfSean, {
		selector: '#zoneGauche #titreProject',
		//tuneHeight: -6,
		wmode: 'transparent',
		selectable: 1,
		tuneHeight: -6,
		css: [ '.sIFR-root {font-size: 15;color: #FF4800; background-color : #FFFFFF}']
	});
	sIFR.replace(handOfSean, {
		selector: '#zoneGauche #titreContact',
		//tuneHeight: -6,
		wmode: 'transparent',
		selectable: 1,
		tuneHeight: -6,
		css: [ '.sIFR-root {font-size: 14;color: #FF4800; background-color : #FFFFFF}']
	});*/
}
sifrReplacements();

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

$(document).ready(function(){
	startList();
	
	$("input[type='checkbox']").css({width:"auto"});
	$("input[type='checkbox']").css({border:"none"});
	$("input[type='checkbox']").css({background:"none"});
	$("input[type='file']").css({width:"auto"});
	$("input[type='radio']").css({width:"auto"});
	$("input[type='radio']").css({border:"none"});
	$("input[type='radio']").css({background:"none"});
	$("input[name='rpass']").css({width:"auto"});
	$("input[name='lpass']").css({width:"auto"});
	
	if($("#log div[class='content']")){	
		$("#log div[class='content']").css({display: "none"});
	}
	if($("#newsletter")){	
		var cTop = 	55+	$("#log").height() + 2;	
		$("#newsletter").css({top:cTop});
	}	
	/*search form*/
	if($("#searchMillieu input[class='searchText']")){ 
		$("#searchMillieu input[class='btnsearch']").focus(function(){	
			if($(this).attr('value') == $(this).attr('title')){
				$(this).attr('value', '');
			}
		});
		$("#searchMillieu input[class='btnsearch']").blur(function(){	
			if($(this).attr('value') == ''){
				$(this).attr('value', $(this).attr('title'));
			}
		});			
	}
	
	if($("#ContentInterieur")) {
		/*ajax submission of part of form*/
		$("#ContentInterieur input[rel='deleteMember']").click(function(){
			//get HTML content for wysiwyg fields
			currentId = $(this).attr("id");
			$("input[name='memberToDelete_fieldID']").val(currentId);
		
		});
		$("#ContentInterieur input[name='createMember']").ajaxify({
			event:'click',
		  	method:'POST',
			loading_img:'/img/common/loading.gif',
			loading_txt:'Saving project...',
			target:'#feedback',
		   	link:'/js/modules/polyProjects/controler.manageMember.php?ajax=true&ucms_action=addMember',
		   	forms:$("#ContentInterieur form[name='modifyProjectFormStep2']"),
		   	onStart:function(op){
				var cTop =  window.pageYOffset ? window.pageYOffset : document.body.scrollTop;
		   		$("#feedback").css({top:cTop+"px"});
		   		$("#feedback").slideDown(gEffectDuration).fadeIn(gEffectDuration);
		   		
		   	},
		   	onComplete:function(op){
		   		$("#feedback").css({display: "none"});		   		
		   	}
		});
		$("#ContentInterieur input[rel='deleteMember']").click(function(){
			//get HTML content for wysiwyg fields
			currentId = $(this).attr("id");
			$("input[name='memberToDelete_fieldID']").val(currentId);
		
		});
		$("#ContentInterieur input[rel='deleteMember']").ajaxify({
			event:'click',
		  	method:'POST',
			loading_img:'/img/common/loading.gif',
			loading_txt:'Saving project...',
			target:'#feedback',
		   	link:'/js/modules/polyProjects/controler.manageMember.php?ajax=true&ucms_action=removeMember',
		   	forms:$("#ContentInterieur form[name='modifyProjectFormStep2']"),
		   	onStart:function(op){
				 var cTop =  window.pageYOffset ? window.pageYOffset : document.body.scrollTop;
		   		$("#feedback").css({top:cTop+"px"});
		   		$("#feedback").slideDown(gEffectDuration).fadeIn(gEffectDuration);
		   	},
		   	onComplete:function(op){
		   		$("#feedback").css({display: "none"});
		   		
		   	}
		});
		/*interactivity*/
				
		/*interactivity*/
	/*	if($("div[class='contentOnglet']")) {
			if($("div[class='titreOnglet'] h2").length > 1) {
				$("div[class='titreOnglet'] h2").click(function(){
				
					var eBrother = $(this).parent().parent().children("div[class='contentOnglet']");	
					if(eBrother.css("display") == "none"){			
						eBrother.slideDown(gEffectDuration);		
					
					} else {		
						eBrother.slideUp(gEffectDuration);		
					}
				});
			}
		}*/
		if($("#listProjet")) {
			$("div[class='subtitle']").click(function(){
				var eBrother = $(this).parent().parent().children("div[class='historic']");
				var eArrow =  $(this).children("div[class='arrow']").children("img");
				if(eBrother.css("display") == "none"){			
					eBrother.slideDown(gEffectDuration);
					eArrow.attr("src","/img/2009/arrow-down.gif");
					eArrow.attr("alt","Open");			
					
				} else {		
					eBrother.slideUp(gEffectDuration);		
					eArrow.attr("src","/img/2009/arrow-up.gif");
					eArrow.attr("alt","Close");			
				}
			});
		}

	
		if($("a[class='showDescription']")) {
			$("a[class='showDescription']").click(function(){
				var eBrother = $(this).parent().parent().children("div[class='descriptionLabel']");	
				var status = eBrother.css("display");
				$("div[class='descriptionLabel']").each(function(){
					$(this).hide();
				});
				if(status == "none"){			
					eBrother.fadeIn(gEffectDuration);		
					
				} else {		
					eBrother.fadeOut(gEffectDuration);		
				}
				return false;
			});
			$("body").click(function(){
				$("div[class='descriptionLabel']").each(function(){
					$(this).hide();
				});
								
			});
		}
	
		if($("span[@class^=closeFieldSet]")) {
			$("span[@class^=closeFieldSet]").click(function(){
				var eBrother = $(this).parent().children("div[class='needs']");	
				/*alert($(this).children("input[class='noSize']"));
					$(this).children("input[class='noSize']").attr("checked", "checked");*/
				var status = $(this).children("input[class='noSize']").is(":checked");
				if(status == true){			
					eBrother.show();	
				} else {		
					eBrother.fadeOut(gEffectDuration);	
				}
			});
		}
	}
	if($("input[@class*=confirm]")) {
			
		$("input[@class*=confirm]").click(function(){
			return confirm("Do you really want to remove this item ? This action is immediate and you can not undo it. Confirm ?");				
		});
	}
	if($("#log div[class='titre']")) {
         //SpotImage - DBOIME - 26/10/09 - gestion cookie
         // aucun cookie pour l'instant
				var eB = $("#log div.content");
				if($.cookie('planet_action_tt_open') == null){
					//creation
					$.cookie('planet_action_tt_open','false', { expires: 30 });
					
				}else{
					if($.cookie('planet_action_tt_open') == 'true'){
						eB.show();
						$("#log div[class='titre']").children("span[class='btnBasLog']").attr("class", "btnBasLogUp");
					}else{
						eB.hide();
						if($("#newsletter")){	
							var cTop =	 55+	$("#log").height() + 2;	
							$("#newsletter").css({top:cTop});
						}		
						$("#log div[class='titre']").children("span[class='btnBasLogUp']").attr("class", "btnBasLog");
					}
				}
				$("#logout a").click(function(){$.cookie('planet_action_tt_open','false');});
		$("#log div[class='titre']").click(function(){
			var eBrother = $(this).parent().children("div[class='content']");	
			if(eBrother.css("display") == "none"){			
				eBrother.show();	
				
				$(this).children("span[class='btnBasLog']").attr("class", "btnBasLogUp");
$.cookie('planet_action_tt_open','true');
			} else {		
				eBrother.hide();
				if($("#newsletter")){	
					var cTop = 	55+	$("#log").height() + 2;	
					$("#newsletter").css({top:cTop});
				}		
				$(this).children("span[class='btnBasLogUp']").attr("class", "btnBasLog");
$.cookie('planet_action_tt_open','false');
			}
			return false;
		});
	}
	if($("#newsletter  div[class='titre']")) {
		$("#newsletter  div[class='titre']").click(function(){
			var eBrother = $(this).parent().children("div[class='content']");	
			if(eBrother.css("display") == "none"){			
				eBrother.slideDown(gEffectDuration);		
				$(this).children("span[class='btnBasNewsletter']").attr("class", "btnBasNewsletterUp");
			} else {		
				eBrother.slideUp(gEffectDuration);		
				$(this).children("span[class='btnBasNewsletterUp']").attr("class", "btnBasNewsletter");
			}
			return false;
		});
	}
	if($("#pageGaucheContent  div[class='titre']")) {
		$("#pageGaucheContent  div[class='titre']").each(function(i){	
			$(this).css({cursor:'pointer'});
			var eBrother = $(this).parent().children("div[class='description']");
			eBrother.css({display:'none'});
			if(i != 0 ){					
				$(this).children("a[class='flecheOpen']").attr("class", "flecheClose");				
			}
		});
		$("#pageGaucheContent  div[class='titre']").click(function(){
			var eBrother = $(this).parent().children("div[class='description']");	
			if(eBrother.css("display") == "none"){			
				eBrother.slideDown(gEffectDuration);		
				
				$(this).parent().css({background:"#fff4f0"});
				$(this).children("a[class='flecheClose']").attr("class", "flecheOpen");
				
				return false;
			} else {		
				eBrother.slideUp(gEffectDuration);		
				$(this).children("a[class='flecheOpen']").attr("class", "flecheClose");
				$(this).parent().css({background:"#FFFFFF"});
				return false;
			}
			return false;
		});
	}
	if($("#ContentInterieur div[class='titreNews']")) {
		$("#zoneDroite  div[class='titreNews']").each(function(){
			if($(this).parent().children("div[class='description']").html()){
				$(this).css({cursor:'pointer'});
			}
		});
		$("#ContentInterieur div[class='titreNews']").click(function(){
			var eBrother = $(this).parent().children("div[class='description']");	
			if(eBrother.css("display") == "none"){			
				eBrother.slideDown(gEffectDuration);		
				$(this).children("a[class='flecheClose']").attr("class", "flecheOpen");
				return false;
			} else {		
				eBrother.slideUp(gEffectDuration);	
				$(this).children("a[class='flecheOpen']").attr("class", "flecheClose");	
				return false;
			}
			return false;
		});
	}
	if($("#ContentInterieur div[@class*='titre']")) {
		$("#ContentInterieur div[@class*='titre']").each(function(i){
			var eBrother = $(this).parent().children("div[class='description']");
			if(i != 0 ){					
				$(this).children("a[class='flecheOpen']").attr("class", "flecheClose");
				eBrother .css({display:'none'});
			} else {
				if(eBrother.css("display") == "none"){
					$(this).children("a[class='flecheOpen']").attr("class", "flecheClose");
				}
			}
		});
		$("#ContentInterieur div[@class*='titre']").each(function(){	
			$(this).css({cursor:'pointer'});
		});
		$("#ContentInterieur div[@class*='titre']").click(function(){
			var eBrother = $(this).parent().children("div[class='description']");	
			if(eBrother.css("display") == "none"){			
				eBrother.slideDown(gEffectDuration);		
				$(this).children("a[class='flecheClose']").attr("class", "flecheOpen");
				return false;
			} else {		
				eBrother.slideUp(gEffectDuration);	
				$(this).children("a[class='flecheOpen']").attr("class", "flecheClose");	
				return false;
			}
			return false;
		});
	}
	if($("#selectmembers div[class='createMember']")) {
		$("#selectmembers div[class='createMember']").css({display:'none'});
		$("#selectmembers span[class='createMemberLink']").each(function(){	
			$(this).css({cursor:'pointer'});
		});
		$("#selectmembers span[class='createMemberLink']").click(function(){
			var eBrother = $(this).parent().children("div[class='createMember']");	
			if(eBrother.css("display") == "none"){			
				eBrother.slideDown(gEffectDuration);		
				return false;
			} else {		
				eBrother.slideUp(gEffectDuration);		
				return false;
			}
			return false;
		});
	}
	if($("#menuGauche") || $("#listManageProjects"))  {
		if(window.location.search.indexOf("projectID") != -1) {
			var t = window.location.search.substring(1).split('&');
			var value = ""
			for (var i=0; i<t.length; i++) {
				var x = t[ i ].split('=');
				if(x[0] == "projectID") {
					value = x[1];
					break;
				}
			}
			if(value != ""){
				$("#menuGauche  a").each(function(i){
					var cAttr = $(this).attr("href");
					cAttr += "?projectID="+value;
					$(this).attr("href", cAttr);
				});
				if($("#ariane")) {
					$("#ariane  a").each(function(i){
					var cAttr = $(this).attr("href");
					cAttr += "?projectID="+value;
					$(this).attr("href", cAttr);
					});
				}
				$("#listManageProjects  a").each(function(i){
					var cAttr = $(this).attr("href");
					cAttr += "?projectID="+value;
					$(this).attr("href", cAttr);
				});	
			}
			
		}
	}

	if($("#formProject td[class='tableFiles'] div[class='manageLink'] table"))  {
		var destination = $("#formProject td[class='tableFiles'] div[class='manageLink'] th:contains('Destination')");

		var eBrothers = destination.parent().children("td");
		eBrothers.css({display:'none'});
		var parent = destination.parent().parent();
		parent.children("tr").each(function(i){	
			if(i >= 4){
				$(this).children("th").css({display:'none'});
				$(this).children("td").css({display:'none'});
			}
		});

		
	} 
	if($("form[name='projectNews']") || $("form[name='projectDocs']") )  {
		if(window.location.search.indexOf("projectID") != -1) {
			var t = window.location.search.substring(1).split('&');
			var value = ""
			for (var i=0; i<t.length; i++) {
				var x = t[ i ].split('=');
				if(x[0] == "projectID") {
					value = x[1];
					break;
				}
			}
			if(value != ""){
				if($("form[name='projectNews']")){
					$("form[name='projectNews']").each(function(i){
						var cAttr = $(this).attr("action");
						cAttr += "?projectID="+value;
						$(this).attr("action", cAttr);
					});
				}
				if($("form[name='projectDoc']")){
					$("form[name='projectDoc']").each(function(i){
						var cAttr = $(this).attr("action");
						cAttr += "?projectID="+value;
						$(this).attr("action", cAttr);
					});
				}
				if($("form[name='manageLink']")){
					$("form[name='manageLink']").each(function(i){
						var cAttr = $(this).attr("action");
						cAttr += "?projectID="+value;
						$(this).attr("action", cAttr);
					});
				}
				if($("form[name='projectNewsModify']")){
					$("form[name='projectNewsModify']").each(function(i){
						var cAttr = $(this).attr("action");
						cAttr += "?projectID="+value;
						$(this).attr("action", cAttr);
					});
				}
				if($("form[name='projectDocModify']")){
					$("form[name='projectDocModify']").each(function(i){
						var cAttr = $(this).attr("action");
						cAttr += "?projectID="+value;
						$(this).attr("action", cAttr);
					});
				}
				if($("form[name='manageLinkModify']")){
					$("form[name='manageLinkModify']").each(function(i){
						var cAttr = $(this).attr("action");
						cAttr += "?projectID="+value;
						$(this).attr("action", cAttr);
					});
				}
			}
			
		}
	}});


	

