/********************************************************************************************/
/*                                  E-magineurs : Ville de Montreuil                        */
/* Auteur : Costa C. <ccosta@e-magineurs.com                                                */
/* Date : 07/09/2011                                                                        */
/* Version : 0.2                                                                            */
/* Compagnie : E-Magineurs                                                                  */
/********************************************************************************************/


jQuery(document).ready(function(){
	if(readSession("mobile")==null){
		writeSession("mobile", "yes");
	}	

	if(/mobile/i.test(window.location.search)){
		writeSession("mobile", "no");
	}
	
	if((/mobile/gi.test(navigator.userAgent))){
		if(readSession("mobile")!="no"){
			if (confirm("Voulez-vous visiter la version mobile du site web de la ville de Montreuil ?")) { // Clic sur OK
		  	window.location.href="http://mobi.montreuil.fr";
			}else{
				writeSession("mobile", "no");
			}
		}
	}
	
	initAvis();
	initMenuMonCompte();
});

//----------------------------------
// writeSession()
//----------------------------------

function writeSession(Nom, Valeur){
	var argc = writeSession.arguments.length;
	var argv = writeSession.arguments;

	var Expire			= (argc > 2) ? argv[2] : null;
	var Repertoire	= (argc > 3) ? argv[3] : null;
	var Domaine			= (argc > 4) ? argv[4] : null;
	var Securise		= (argc > 5) ? argv[5] : false;

	document.cookie = Nom+"="+escape(Valeur)+
	((Expire==null) ? "" : ("; expires="+Expire.toGMTString()))+
	((Repertoire==null) ? "" : ("; path="+Repertoire))+
	((Domaine==null) ? "" : ("; domain="+Domaine))+
	((Securise==true) ? "; secure" : "");
}

//----------------------------------
// readSession()
//----------------------------------

function readSession(Nom){
	var Argument	= Nom+"=";
	var ArgLen		= Argument.length;
	var CookLen		= document.cookie.length;
	var Counter		= 0;

	while (Counter<CookLen){
		var Offset = Counter + ArgLen;
		if(document.cookie.substring(Counter, Offset)==Argument){
			return getSessionVal(Offset);
		}
		Counter = document.cookie.indexOf(" ",Counter) + 1;
		if(Counter==0) break;
	}
	return null;
}

//----------------------------------
// getSessionVal()
//----------------------------------

function getSessionVal(Offset){
	var EndStr = document.cookie.indexOf (";", Offset);
	if(EndStr == -1){
		EndStr = document.cookie.length;
	}
	return unescape(document.cookie.substring(Offset, EndStr));
}

//----------------------------------
// getExpireSession()
//----------------------------------

function getExpireSession(butWhen){
	var LaDate = new Date();
	var heureEnCours = LaDate.getHours();
	
	if(butWhen == 'Tomorrow'){
		LaDate.setSeconds(0);
		LaDate.setMinutes(0);
		LaDate.setHours(heureEnCours + (24 - heureEnCours) + 2);
		//alert(LaDate);
	}
	
	return(LaDate);
}


//----------------------------------
// initAvis()
//----------------------------------
function initAvis(){
	jQuery('.tx-comments-pi1 .tx-comments-comments').hide();
	
	if( (jQuery('.tx-comments-required-error').size()<=0) && (jQuery('.tx-comments-form-top-message').size()<=0)){
		//alert((jQuery('.tx-comments-required-error').size())
			jQuery('.tx-comments-pi1 .tx-comments-comment-form').hide();
	}	

	
	jQuery('.commentPage').css("float","left");
	jQuery('.commentPage').css("width","700px");
	
	jQuery('.tx-comments-pi1').css("float","left");
	jQuery('.tx-comments-pi1').css("width","700px");
	
	//jQuery('.commentReaction').css("float","left");

	jQuery('.commentPage').click(function(){
		//jQuery('.tx-comments-pi1 .tx-comments-comment-form').slideToggle("slow");
		
		jQuery('.tx-comments-pi1 .tx-comments-comments').slideToggle("slow", function(){
			jQuery('.tx-comments-pi1 .tx-comments-comment-form').slideToggle("slow");
		});
		
		
	}).mouseover(function(){
		jQuery(this).css("text-decoration","underline");
		jQuery(this).css("cursor","pointer");
	}).mouseout(function(){
		jQuery(this).css("text-decoration","none");
	});
	
	
	jQuery('#gotoCommentBtn').click(function(){
		jQuery('.tx-comments-pi1 .tx-comments-comments').slideToggle("slow", function(){
			jQuery('.tx-comments-pi1 .tx-comments-comment-form').slideToggle("slow");
		});
	});	
	

	
	/*jQuery('.commentReaction').click(function(){
		jQuery('.tx-comments-pi1 .tx-comments-comments').slideToggle("slow");
	}).mouseover(function(){
		jQuery(this).css("text-decoration","underline");
		jQuery(this).css("cursor","pointer");
	}).mouseout(function(){
		jQuery(this).css("text-decoration","none");
	});*/
}


function initMenuMonCompte(){
	jQuery('.profilMonCompte img').css("opacity","0.5");
	jQuery('.profilMonCompte .titre_smenu').css("opacity","0.5");	
	
	jQuery('.profilMonCompte').each(function(profilMonCompte){
		jQuery(this).mouseenter(function(){
			jQuery('.profilMonCompte:eq('+profilMonCompte+') img').fadeTo('slow',1);
			jQuery('.profilMonCompte:eq('+profilMonCompte+') .titre_smenu').fadeTo('slow',1);
		}).mouseleave(function(){
			jQuery('.profilMonCompte:eq('+profilMonCompte+') img').fadeTo('slow',0.5);
			jQuery('.profilMonCompte:eq('+profilMonCompte+') .titre_smenu').fadeTo('slow',0.5);
		})
	});
}
