var util = {
	go : function( url ){
		window.location.href  = url;
	},
	
	sort: function( value, key ){
		if( key == null)
			key = "o";
			

		url = URL.setParameter( key, value);
		url = URL.removeParameter( url, "p");
		
		util.go( url);
		
		return false;
	
	}
};
/******************************************************************
	  	IFRAME SYSTEM
*******************************************************************/
var iframeSize = function( moyenne_width, moyenne_height){
	
	this.moyenne_width 	= moyenne_width;
	this.moyenne_height = moyenne_height;
	
	this.resize = function(widthGived){
		width = screen.width;
		height = screen.height;
		
		if( !this.moyenne_width ){
			defaut_width = "100%";
			defaut_height = "540px";
			
		}else{
	
			urlIframe1 = "";
			urlIframe2 = "";
	
			coeff = this.moyenne_height / this.moyenne_width;
			
			if( widthGived != null ) width_estime = widthGived
			else 					 width_estime = screen.availWidth;
			
			height_estime = coeff * width_estime;//défini la hauteur en fonction du coeff et la hauteur par défaut
	
			if( height_estime < screen.availHeight ){	//si la hauteur estimé est inférieur a la hauteur que l'on peut utiliser on applique la modif
				
				defaut_width = width_estime + 'px';
				defaut_height = height_estime + 'px';
			}else{
			
				coeff = this.moyenne_width / this.moyenne_height;
				height_estime = screen.availHeight * 0.70; //sinon on prend la hauteur comme base et environ 70%
				width_estime = coeff * height_estime;
				defaut_width = width_estime + 'px';
				defaut_height = height_estime + 'px';
			}
		}
	
		return '&width=' + defaut_width + '&height=' + defaut_height;
	}
	
	this.resizeByHeight = function( heightGived){
		if( !this.moyenne_width ){
				defaut_width = "100%";
				defaut_height = "540px";
		}else{
	
				coeff = this.moyenne_width / this.moyenne_height;
				height_estime = heightGived;
	
				width_estime = coeff * height_estime;
	
				defaut_width = width_estime + 'px';
				defaut_height = height_estime + 'px';
		}
	
		return '&width=' + defaut_width + '&height=' + defaut_height;
		}

}

/******************************************************************
			CAPTCHA  SYSTEM
*******************************************************************/
var captcha = {
	 i: 0,
	
	refresh: function(id){
		url = "/ajax/captcha/?i=" + this.i++; 
		$("#"+id).attr("src", url); 
		
	}

};


/******************************************************************
				EDITOR SYSTEM
*******************************************************************/

 var Editor = function(settings, idWysiwyg, idDescr, idCorrespondant, type){    
    this.settings = settings;
    this.idWysiwyg = idWysiwyg;
    this.idDescr = idDescr;
    this.idCorrespondant = idCorrespondant;
    this.type = type;

	
	$("#" + idWysiwyg).markItUp(settings);
	
	this.edit = function(){
	
		idWysiwyg 	= this.idWysiwyg;
		idDescr 	= this.idDescr;
	
		$.ajax({
			type: "POST",
			url:"/ajax/show-editable-article/",
			data: { id: this.idCorrespondant, type: this.type },
			dataType: "json",
		  	success: function(data){		  	
	  			if( !data.isValid){
	  				$("#wysiwyg_mess").slideUp(function(){$(this).attr("class", "error").html(data.mess).slideDown() });
	  			}else{
	  				 $("#wysiwyg_mess").hide();
					 $(".edit").hide();
					 $(".cancel").show();
					 
					 $("#div_preview").slideUp();
					 $("#" + idDescr).hide();
					 //$("#wysiwyg_mess").slideUp().attr("class", "error").html(data.mess).slideDown();
					 $("#articleVersion").html(data.mess);
					 $("#div_" + idWysiwyg).show();
	  			}
			}
		});
	}
	
	this.cancel = function(){
		 $(".edit").show();
		 $(".cancel").hide();
		
		 $("#div_" + this.idWysiwyg).hide();
		 $("#" + this.idDescr).show();
		
	}
	
	this.preview = function(){
		
		$.ajax({
			type: "POST",
			url:"/ajax/preview-article/",
			data: {provenance: "jeu", html: $("#wysiwyg").val()  },
			dataType: "json",
		  	success: function(data){
					if(data.isValid){
						$("#div_preview").slideUp( function(){
							$("#content_preview").html(data.html);
							$(this).slideDown();
						});
					}else
						$("#div_preview").slideUp();
			}
		});
	}
	
	this.save = function(){
	
		idWysiwyg 	= this.idWysiwyg;
		idDescr 	= this.idDescr;
		type	 	= this.type;
		raison 	= escape($("#wysiwyg_raison").val());
		html 	= escape( $("#wysiwyg").val() );
		
		param = {html: html, id: this.idCorrespondant, raison:  raison, type: type}
		$.ajax({
			type: "POST",
			url:"/ajax/save-article/",
			data: param,
			dataType: "json",
		  	success: function(data){
		  		
		  		if( data.errorType == "raison" ){
		  			$("#div_preview").slideUp(function(){
		  				$("#error_raison").slideUp().attr("class", "hidden error").html(data.mess).slideDown();	
		  			});
		  		}else{
		  			$("#div_" + idWysiwyg).hide();
		 			$("#" + idDescr).show();
		 			
					 $(".edit").show();
					 $(".cancel").hide();
		 			
			  		if(data.isValid == false){
			  			$("#wysiwyg_mess").slideUp( function(){
			  				$(this).html(data.mess).attr("class", "hidden error").slideDown(); 
			  			});
			  		}
			  		else	
			  			$("#wysiwyg_mess").slideUp(function(){
			  				$(this).html(data.mess).attr("class", "hidden success").slideDown();
			  				$("#wysiwyg_raison").val(""); //Réinitialise le champs raison
			  			});
			  			  		
		  		
		  		}
			}
		});
	}
	
		
}

/******************************************************************
				COMMENTAIRE SYSTEM
*******************************************************************/

 var Commentaire = function(type, id_correspondant){
    this.type = type;
    this.id = id_correspondant;
	
	this.checkLogged = function(){
		$.ajax({
			type: "POST",
			url:"/commentaire/check-logged-ajax/",
			dataType: "json",
		  	success: function(data){
			  	if(data.isValid == false){
			  		$("#c_info").html(data.mess).attr("class", "error");
			  	
				}else{
					$("#c_info").html("").removeClass("error");
					$("#bt_showAddBlock").parent().slideUp(); 
					$("#c_ajout").slideDown(); 
					
				}		  		

			}
		});
		
	}
    
	this.add = function( ){
		
		elt = escape($("#c_content").val());
		
		$.ajax({
			type: "POST",
			url:"/commentaire/add-comment-ajax/",
			data: {id: this.id, type: this.type, content: elt},
			dataType: "json",
		  	success: function(data){
			  	if(data.isValid == false){
			  		$("#c_info").html(data.mess).attr("class", "error");
			  	
				}else{
					$("#c_noComment").hide();
					$("#newComment").html(data.html).show();	
					$("#c_info").html(data.mess).attr("class", "success");	
					$("#c_ajout").hide().ready();
					
				}		  		

			}
		});
	}
}


/******************************************************************
						STAR SYSTEM
*******************************************************************/

var Star = function(type){    
	this.type = type;
	
	this.setNote = function( note, wrapper ){
		if( wrapper == "votre") 	$("#stars-wrapper1").stars("select", note );
		if( wrapper == "leur") 		$("#stars-wrapper2").stars("select", note );
		else 						$("#stars-wrapper3").stars("select", note );
	}
	
	this.noteAdmin = function( avg){
		avg 		= this.formatAvg( avg );
		
		$('#stars-wrapper1').stars({ 
		inputType: "select",
		split: 4,
		starWidth: 26,
		disabled: true  
		});  
		this.setNote(avg, "votre");
	}
	
	this.leurNote = function( avg){
		avg 		= this.formatAvg( avg );
		typeCourant = this.type;
		
		$('#stars-wrapper2').stars({ 
			inputType: "select",
			split: 4,
			starWidth: 27,
			disabled: true  
		});  
		this.setNote(avg, "leur");
	}
	
	this.votreNote = function( note ){
		typeCourant = this.type;
	
		$('#stars-wrapper3').stars({ 
			inputType: "select",
			captionEl: $('#stars-cap'),  
			cancelShow: false, 
			callback: function(ui, type, value){ 
				param = {
					value:	value,
					id:		$("#idCorrespondant").val(),
					type:	typeCourant
				 }
				
				$.ajax({
					type: "POST",
					url:"/ajax/rate-it/",
					data: param,
					dataType: "json",
				  	success: function(data){
					
						if( data.isValid ){
							star = new Star(typeCourant)
							avg = star.formatAvg( data.avg );
							star.setNote( avg, "leur" );
							$("#s_nbVote").html(data.nbVote);
							$("#s_leurNote").html(avg );
							
						}
				 	}
				});
			
				$("#s_votreNote").html(value );
			}  
		});  
		this.setNote(note);
	}  
	
	this.formatAvg = function( val){
		if( val == 0 )
			return -1;
		
		res = val / 0.25;
		res = Math.round(res);	//On vient d'arrondir le chiffre au 25e de centieme pres
		res = res * 0.25;
		return res;
	}    	
}


/******************************************************************
		STYLE SYSTEM
*******************************************************************/
var styleCSS = { 

	tableStyle: function( pattern, class_imgHover, pattern2, class_linkHover){
		
		$(pattern).bind("mouseover", function(e){
			elt = $(this).parent().prev().find("a").addClass(class_imgHover);
		});
		$(pattern).bind("mouseout", function(e){
			elt = $(this).parent().prev().find("a").removeClass(class_imgHover);
		});

		if( pattern2 && class_linkHover){
			
			$(pattern2).bind("mouseover", function(e){
				elt = $(this).parent().parent().next().find("a").addClass(class_linkHover);
			});
			$(pattern2).bind("mouseout", function(e){
				elt = $(this).parent().parent().next().find("a").removeClass(class_linkHover);
			});
		}
		
	}
};

/*
var styleCSS = { 

	tableStyle: function( pattern, class_imgHover, pattern2, class_linkHover,scroll){
		
		$(pattern).bind("mouseover", function(e){
			elt = $(this).parent().prev().find("a").addClass(class_imgHover);
		});
		$(pattern).bind("mouseout", function(e){
			elt = $(this).parent().prev().find("a").removeClass(class_imgHover);
		});

		if( pattern2 && class_linkHover && !scroll){
			
			$(pattern2).bind("mouseover", function(e){
				elt = $(this).parent().parent().next().find("a").addClass(class_linkHover);
			});
			$(pattern2).bind("mouseout", function(e){
				elt = $(this).parent().parent().next().find("a").removeClass(class_linkHover);
			});
		}
		if(scroll)
		{
		  if(scroll == 'prev')
      {
        $(pattern2).bind("mouseover", function(e){
  				scrollovers_MouseOver('',$(this).parent().prev().find("em").attr('id'));
  			});
  			$(pattern2).bind("mouseout", function(e){
  				scrollovers_MouseOut('',$(this).parent().prev().find("em").attr('id'));
  			});
  		}
  		else
      {
        $(pattern2).bind("mouseover", function(e){
  				scrollovers_MouseOver('',$(this).parent().parent().next().find("em").attr('id'));
  			});
  			$(pattern2).bind("mouseout", function(e){
  				scrollovers_MouseOut('',$(this).parent().parent().next().find("em").attr('id'));
  			});
  		}
    }
		
	}
};
*/