//----------------------------------------------------------------------------------------------------
 var G = (function(){
//-----------------	
//Private
	function _zeroNotNull(height_return){
			if (height_return == null) {return 0;}
			else {return height_return;}
		};

//-----------------	
//Public	
    var o = {};	
//	o.Vals = 
	o.btn = {state:"after",lacouche:1};
	o.init = function(fromtpl_vals){
		this.Vals = fromtpl_vals;
		//Values Setup;	
		this.Vals.new_h = 0;	
		this.imageLoad("#img_ppbtn_after", this.Vals.img_name);
		this.computeValues();		
		this.setUpEvents();
		this.human = false;
		this.checkCookies();		
	};
/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
	o.dump = function (arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects
	 for(var item in arr) {
	  var value = arr[item];
	 
	  if(typeof(value) == 'object') { //If it is an array,
	   dumped_text += level_padding + "'" + item + "' ...\n";
	   dumped_text += dump(value,level+1);
	  } else {
	   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
	  }
	 }
	} else { //Stings/Chars/Numbers etc.
	 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
	} 
	/*
	 * */

	o.computeValues = function(){
		this.Vals.vp_h = $(window).height();
		this.Vals.vp_w = $(window).width();
		this.Vals.maxdispo = this.Vals.vp_h-50;
		this.Vals.maxWdispo = this.Vals.vp_w-30;				
		this.Vals.ratio = parseInt(this.Vals.img_w)/parseInt(this.Vals.img_h);
		this.Vals.marginer = (this.Vals.maxdispo - parseInt(this.Vals.img_h))/ 2;	
		if (this.Vals.img_h < (this.Vals.maxdispo)) {
			$("#mainimage").css("width", parseInt(this.Vals.img_w) + "px");
			$("#mainimage").css("height", parseInt(this.Vals.img_h) + "px");
			$("#imgpart").css("margin-top", this.Vals.marginer);
			$("#navlinks").css("margin-top", this.Vals.marginer);
			$("#imgpart").css("margin-bottom", this.Vals.marginer);
			$("#barre").css("margin-top", "0px");
			$("#barre_zoom").fadeOut();
		}
		else {
			//get the ratio
			this.Vals.ratio = parseInt(this.Vals.img_w) / parseInt(this.Vals.img_h);
			this.Vals.new_h = this.Vals.maxdispo - 10;
			this.Vals.new_w = Math.round(this.Vals.new_h * this.Vals.ratio);			
			$("#mainimage").css("width", parseInt(this.Vals.new_w + "px"));
			$("#mainimage").css("height", parseInt(this.Vals.new_h + "px"));
			$("#imgpart").css("margin-bottom", "5px");
			$("#imgpart").css("margin-top", "5px");
			$("#navlinks").css("margin-top", "5px");
			$("#barre").css("margin-top", 0);
			$("#barre_zoom").fadeIn();
		};

		$("#mainimage").removeClass("loading");
		$("#allcontainer").removeClass("hidetitle");

		$("#navlinks").css("height", $("#top").height());		
	};

	o.checkCookies = function (){
		//love cookie
		var loveC = "love-" + o.Vals.img_id;
		if ($.cookies.get(loveC) == "ok"){
				$("#barre_love_link")
					.addClass("loveclicked")
					.unbind('click')
					.click(function(){o.messenger.flash("Your love point is already sent. Reset in 1 hour.");return false;})}
	}
	o.imageLoad= function(container,source)      {
			
			var img = new Image();
			$(img).load(function(){
				$("#mainimage").addClass("loading");
				$(this).hide();
				
				$(container).append(this);
				$(this).fadeIn();	
			})
			.error(function(){})
			.attr('src',"/images/"+source);
	};
	o.switcher = function (cssid){
		this.btn.lacouche++;
		switch(cssid.split("_")[1]) {
			case "before":
				if (this.Vals.img_id < 422) {//backward compat
					this.btn.lasource = "/nopp/" + this.Vals.img_name;
				}
				else {
					this.btn.lasource = "/packs/" + this.Vals.img_id +"/" + this.Vals.img_id +"_0.jpg";
				}
				o.messenger.showIn("Loading original RAW");
				break;
			case "after":
				this.btn.lasource = "/images/"+this.Vals.img_name;
				o.messenger.showIn("Loading final post processed Image");							
				break;
			default:
				if (this.Vals.img_id < 422) {//backward compat
					this.btn.lasource = "/nopp/" + cssid.split("_")[1] + "_" + this.Vals.img_name;
				}
				else {
					this.btn.lasource = "/packs/" + this.Vals.img_id +"/" + this.Vals.img_id +"_"+ cssid.split("_")[1]  +".jpg";
				}
				o.messenger.showIn("Loading step "+ cssid.split("_")[1] + " Image");	
			break;
		}
		if ($('#img_' + cssid).length === 0) {//load de l'autre image
			$("#mainimage").append("<div id='img_" + cssid + "'><\/div>");
			$("#img_" + cssid)
						.hide()
						.css("position", "absolute")
						.css("top", 0)
						.css("left", 0)
						.css("z-index", this.btn.lacouche)
						.fadeIn();
			//load
			var img = new Image();
			$(img).load(function(){
				$(this).hide();
				$("#img_" + cssid).append(this);
				$(this).fadeIn();
			}).error(function(){
			}).attr('src', this.btn.lasource);
			$("#img_" + cssid).fadeIn();
			
		}
		else {
			$("#img_" + cssid)
						.hide()
						.css("position", "absolute")
						.css("top", 0)
						.css("left", 0)
						.css("z-index", this.btn.lacouche)			
						.fadeIn();			
		}
		o.messenger.showOff();
		//
		$(".current_pp").removeClass("current_pp");
		$("#" + cssid).addClass("current_pp");	

	};

	o.formWorks = function (){
		
		$('#form_firstline INPUT')
			.focus(function(){$(this).addClass("inpfocus");})			
			.blur(function(){$(this).removeClass("inpfocus");});
		
		;			
		$('#form_txtarea TEXTAREA')
			.focus(function(){$("#form_txtarea").addClass("txtfocus");})
			.blur(function(){$("#form_txtarea").removeClass("txtfocus");})			
		;		
		$("INPUT.submit").hover(function(){
			$(this).css("background-color", "#E4C131");
		}, function(){
			$(this).css("background-color", "#A3A3A3");
		});
		$("#allform").hover(
			function(){
				if (!o.human){
					$("#form_sub").append("<input id='you_are' type='hidden' name='you_are' value='ahuman' />");
					$("INPUT.submit").click(function(){o.submitCom()});
					o.human = true;
				}
			},
			function(){}
		);
	}
	o.submitCom = function(){
		//loader dessus
		if ( $("#message").val() == "" ){return;}
		$("#allform").addClass("loading").height($("#allform FORM").height());
		$("#allform FORM").fadeOut();

		//post contenu form
		 $.post("/index.php?x=save_comment", { 
			name: $("#name").val(),
			email: $("#mail").val(),
			url:$("#url").val(),
			message:$("#message").val(),
			parent_name:$("#parent_name").val(),
			parent_id:$("#parent_id").val(),
			you_are: $("#you_are").val()
			},
			//callback
		   function(data){
		     //recup html
			 $("#commentbloc UL").append(data);
			 $("#commentbloc UL LI:last").hide();
			 	if ($("(#commentbloc UL LI:last):has(span)"))
				{
					 $("#commentbloc UL LI:last").fadeIn();
					 if($("LI.nocom").length > 0){$("LI.nocom").fadeOut();}					 
					 $("#allform").removeClass("loading");
					 $("#message").val(""),
					 $("#allform FORM").fadeIn();
 					 //popup
 
				}
				else {					 				 
					 $("#allform").removeClass("loading");
					 $("#allform FORM").fadeIn();
 					 //popup
					
				}
		   }
		 );		
	}  


	//	
	o.messenger = {
		showIn : function(text){$("#themessage").html(text).fadeIn(250);},
		showOff : function(){$("#themessage").fadeOut(100);},
		flash   : function(text){$("#themessage").html(text).fadeIn("slow").fadeOut(1500);}
	};
	o.setUpEvents = function (){
		//----------------------------------------------------------------------					
		//IMAGE EVENTS
		//----------------------------------------------------------------------
		$("#navlinks a#prevlink").hoverIntent(
			function(){				
				$(this).attr("title", "");
				o.messenger.showIn("View the previous post");},
			function(){o.messenger.showOff();}
			);
		$("#navlinks a#nextlink").hoverIntent(
			function(){				
				$(this).attr("title", "");
				o.messenger.showIn("View the next post");},
			function(){o.messenger.showOff();}
			);			
		$("#navlinks a").click(
			function(){
				$("#mainimage IMG").fadeOut();
				$("#mainimage").addClass("loading");
			}
		);		
		//----------------------------------------------------------------------					
		//PP EVENTS
		//----------------------------------------------------------------------					
		$("DIV.ppclic").click(function(){o.switcher($(this).attr("id"));});

		//----------------------------------------------------------------------					
		//BARRE EVENTS
		//----------------------------------------------------------------------
		//Love
		$("#barre_love_link").click(function(){
		o.messenger.showIn("Sending your additionnal love point.");
		 $.post("/index.php?x=save_comment&xhr=love", { 
			imgid: o.Vals.img_id
			},
			//callback			
			function(data){
				o.messenger.showIn("Now "+ data +" love points. Thank you.");
				$("#lovevalue").fadeOut("fast").html(data).fadeIn("fast");
				$("#barre_love_link")
					.addClass("loveclicked")
					.unbind('click')
					.click(function(){o.messenger.flash("Your love point is already sent. Reset in 1 hour.");return false;})
				o.messenger.showOff();
			}
			);
			return false;
		})
		.hoverIntent(
			function(){$(this).attr("title", "");o.messenger.showIn("Like this image? Click to add your love point.");},
			function(){o.messenger.showOff();}
		);
		//----------------------------------------------------------------------
		//Zoom
		$("#barre_zoom_link")
			.toggle(
				function(){
					$("#mainimage").css("width", parseInt(o.Vals.img_w)+"px");
					$("#mainimage").css("height", parseInt(o.Vals.img_h)+"px");
					$("#barre_zoom_link").addClass("zoomlinkFit");						
				},
				function(){
					$("#mainimage").css("width",parseInt(o.Vals.new_w+"px"));
					$("#mainimage").css("height",parseInt(o.Vals.new_h+"px"));
					$("#imgpart").css("margin-bottom","5px");
					$("#imgpart").css("margin-top","5px");
					$("#barre").css("margin-top",0);
					$("#barre_zoom_link").removeClass("zoomlinkFit");						
				})				
			.hoverIntent(
				function(){$(this).attr("title", "");o.messenger.showIn("Enlarge image to 100%");},
				function(){o.messenger.showOff();}
			);
		
		//----------------------------------------------------------------------
		//SU
		$("#barre_su_link").hoverIntent(
			function(){$(this).attr("title", "");o.messenger.showIn("Like this image? THUMB IT UP.");},
			function(){o.messenger.showOff();}
		);
		//----------------------------------------------------------------------
		//back HOME
		$("#barre_ref .blocklink").hoverIntent(
			function(){$(this).attr("title", "");o.messenger.showIn("Back Home Index archive");},
			function(){o.messenger.showOff();}
		);
		$("#barre_print_link").hoverIntent(
			function(){$(this).attr("title", "");o.messenger.showIn("Order a print of this picture");},
			function(){o.messenger.showOff();}
		);		
		//----------------------------------------------------------------------
		//Order Print
							
		//----------------------------------------------------------------------					
		//FORM EVENTS
		//----------------------------------------------------------------------
		o.formWorks();
		//----------------------------------------------------------------------					
		//WINDOW EVENTS
		//----------------------------------------------------------------------
		 $(window).bind("resize", function(){  
     		o.computeValues();
 		});
	}
//--------------
  return o;
  })();
//----------------------------------------------------------------------------------------------------
	