// include inc.js
//////////////////////////////////////////////////////////
// RESIZE
//////////////////////////////////////////////////////////
function resize_site(){
	var size = getSize();	
	var hauteur_col = size[1] - 75;
	if($jQ("#colonne").length) $jQ("#colonne").height(hauteur_col);
	if($jQ("#colonne_tous").length) $jQ("#colonne_tous").height(hauteur_col);
	if($jQ("#zone_img").length){
		var res = Math.round((hauteur_col - 385 - 40)/2); 
		//document.getElementById("img_centre").height AU LIEU DE 385 mais il faudrait img.onLoad
		res = (res<20)? 20:res;
		$jQ("#zone_img").css("paddingTop", res);
	}
}
//////////////////////////////////////////////////////////
// MODIF DU HASH
//////////////////////////////////////////////////////////
function setLocationHash(texte){	window.location.hash = "#" + texte;}
function setPageTitle(texte){
	//$jQ("title").html(texte);
	//$jQ(document).attr("title", texte);
	document.title = texte;
}

function test_lien_direct(){
	if(window.location.hash != "" && window.location.hash.length>1){
		var lakey = window.location.hash.substr(1,window.location.hash.length);
		$jQ("#div_"+lakey).click();
		return true;
	} else return false;
}
//////////////////////////////////////////////////////////
// ROLLOVER VIGN ET SELECT IMG
//////////////////////////////////////////////////////////
function get_titre(nom, vendu, laclass){
	var espace = (laclass=="rouge")? "":" ";
	var point = (vendu == 1)? "<span class=\""+laclass+"\">&bull;</span>":"";
	return "<strong>"+nom+"</strong>"+espace+point;
}
/*function get_titre2(nom, vendu, laclass){
	var espace = (laclass=="rouge")? "":" ";
	var point = (vendu == 1)? "&lt;span class=\""+laclass+"\"&gt;"+espace+"&bull;&lt;/span&gt;":"";
	return "&lt;strong&gt;"+nom+"&lt;/strong&gt;"+point;
}*/
var last_vign_over="vide";
var last_vign_sel="vide";

var URL_SITE = "http://www.ccdl.fr/";
var TITRE_SITE = "CCDL - Catherine Coudeville-de Langhe";
var REP_IMG = "medias/photos/";
var REP_INTER = "medias/inter/";
var REP_VIGN = "medias/vignettes/";

var alpha_over = 0.4;
var alpha_sel = 0.2;
var alpha_vitesse = 0;//"fast";

function select_img(key, rub, lurl, nom, vendu, desc, matiere){
	// MAJ du titre et url de la page :
	//alert("select_img");
	if(key != last_vign_sel){
	//	$jQ("#vide").html(nom +" / "+ rub +" - "+ TITRE_SITE);
		var titre_tp = $jQ("#titre").html(nom +" / "+ rub +" - "+ TITRE_SITE).html();
		setPageTitle(titre_tp);//(nom +" / "+ rub +" - "+ TITRE_SITE);
		setLocationHash(key);
		
		// MAJ des vigns  :
		if(last_vign_sel!="vide") {
			$jQ("#img_"+last_vign_sel).css("opacity", 1);
			//$jQ("#img_"+last_vign_sel).fadeTo(alpha_vitesse, 1);
		}
		$jQ("#img_"+key).css("opacity", alpha_sel);
		//$jQ("img_"+key).fadeTo(alpha_vitesse, alpha_sel);
		last_vign_sel = key;
		last_vign_over = "vide";
		
		// MAJ de l'img
		//var elem_lien = $jQ(document.createElement("a"));
		var elem_lien = $jQ("a#lien_centre");
		elem_lien.attr("rel", "lightbox");	
		elem_lien.attr("href", REP_IMG+lurl);	
	//	elem_lien.attr("title", get_titre(nom,vendu,"rouge2") + "<br />" + matiere + " - "+ desc);
		//$jQ("a#lien_centre").empty().append(elem_lien);
		
		if(AFFICHE_LOADER) $jQ("img#img_loader").show(0);
		
		//var elem_img = $jQ(document.createElement("img"));
		//elem_img.attr("id", "img_centre");
		var elem_img = $jQ("img#img_centre");
		elem_img.attr("src", REP_INTER+lurl);//+"?u="+Math.random()*99999);
		elem_img.attr("alt", nom);		
		
		//elem_lien.append(elem_img);
		//$jQ("#limg").empty().append(elem_lien);
		/*
		elem_img.bind('load', function (e) {		
			//if($jQ(this).dropShadow) $jQ(this).dropShadow({left:1, top:1, blur:2, opacity:0.5, color: "#000"});
			$jQ(this).attr("src", REP_INTER+lurl);//+"?u="+Math.random()*99999);
			$jQ(this).attr("alt", nom);	
		});*/
		//});
		// MAJ de la legende
		$jQ("#legende").html(get_titre(nom,vendu,"rouge")); 
		$jQ("#legende").append("<br />" + matiere); 
		$jQ("#legende").append(" - " + desc);
		
		// MAJ des outils :
		var lien_direct = URL_SITE + rub + "-" + key;
		$jQ("#lien_direct").attr("href", lien_direct);
		//LIVE_SHARE_PLUGINS 1 :
		$jQ("#lien_fb_share").attr("href","http://www.facebook.com/sharer.php?u=" + lien_direct);
		$jQ("#lien_twitter_share").attr("href","http://twitter.com/share?url="+ lien_direct);
		
		if(outils_affiche) {
			CreateSocialButtons(lien_direct, document.title);
		}
		/*$jQ("#img_centre").removeShadow();
		$jQ("#img_centre").dropShadow({left:1, top:1, blur:2, opacity:0.5, color: "#000"});*/
	}
}
function select_preview(key, lurl, desc){
	if(last_vign_sel != key){
		if(last_vign_over!="vide")  {
			$jQ("#img_"+last_vign_over).css("opacity", 1);
			//$jQ("#img_"+last_vign_over).fadeTo(alpha_vitesse, 1);
		}
		$jQ("#img_"+key).css("opacity", alpha_over);
		//$jQ("#img_"+key).fadeTo(alpha_vitesse, alpha_over);
		last_vign_over = key;
	}	
	//$jQ("#preview").css("display", "block");
	clearTimeout (timer);
	$jQ("#preview").stop();//.show("slow");
	$jQ("#preview").fadeTo("",1);	
	
	//$jQ("#vign_titre_"+key).show(0);
	$jQ("#vign_titre_"+key).css("display", "block");
	$jQ("img#preview_centre").attr("src", REP_VIGN + lurl);	
	$jQ("img#preview_centre").width(get_pourcentage(desc));

}
var timer = null;
function deselect(){
	//$jQ("#preview").css("display", "none");
	//$jQ("#preview").fadeTo("fast", 0);
	timer = setTimeout ( function(){
		$jQ("#preview").stop();//.hide("slow");
		$jQ("#preview").fadeTo("fast", 0);
    }, 300);
	
	if(last_vign_over!="vide") {
		$jQ("#img_"+last_vign_over).css("opacity", 1);
		//$jQ("#img_"+last_vign_over).fadeTo(alpha_vitesse, 1);
		//$jQ("#vign_titre_"+last_vign_over).hide(0);
		$jQ("#vign_titre_"+last_vign_over).css("display", "none");
	}
	if(last_vign_sel!="vide" && $jQ("#vign_titre_"+last_vign_sel).length) {
		//$jQ("#vign_titre_"+last_vign_sel).hide(0);
		$jQ("#vign_titre_"+last_vign_sel).css("display", "none");
	}
}
function prepare_legende(key, nom, vendu, desc, matiere){
	var legende = get_titre(nom,vendu,"rouge2") + "<br />" + matiere + " - "+ desc;
	$jQ("#lien_"+key).attr("title",legende);
	//alert($jQ("#lien_"+key).attr("id"));
}
function affiche(lakey){
	// Lightbox Resizable v2.04 last line modified
	if (typeof myLightbox != "undefined") {
		var objLink = document.getElementById("lien_"+lakey);
		myLightbox.start(objLink);
	}
}
//////////////////////////////////////////////////////////
// AFFFICHE / CACHE OUTILS
//////////////////////////////////////////////////////////
function init_outils(){
	if($jQ("#outils").length) {
		$jQ("#but_outils").show(0);
		$jQ("#outils").hide(0);
		//$jQ("#but_outils").css("display", "block");
		//$jQ("#outils").css("display", "none");
	}
}
var outils_affiche = false;
function affiche_outils(){
	if($jQ("#outils").length) {
		outils_affiche = !outils_affiche;
		$jQ("#outils").stop();		
		if(outils_affiche) $jQ("#outils").fadeTo("",1);
		else $jQ("#outils").fadeTo("fast",0);
		
		if(!outils_affiche) $jQ("#but_fb_like").empty();
		
		var backimg_tp = (outils_affiche)? "url(imgs/close.gif)":"url(imgs/open2.gif)";
		$jQ("#but_outils").css("background-image", backimg_tp);
		var titre_tp = (outils_affiche)? "fermer":"outils";
		$jQ("#but_outils").attr("title", titre_tp);
		
		if(outils_affiche) CreateSocialButtons($jQ("#lien_direct").attr("href"), document.title);
	}
}
//////////////////////////////////////////////////////////
// INIT
//////////////////////////////////////////////////////////
var AFFICHE_LOADER = false;
$jQ(document).ready(function (){
	resize_site();
	/*$jQ(".vign").mouseover(function(e){
		$jQ("#preview").css("top", e.pageY);//this.offsetTop);
	});*/
	$jQ("#img_centre").bind("load", function (e) { 
		if(AFFICHE_LOADER) $jQ("img#img_loader").hide(0);
		if($jQ(this).dropShadow) {
			/*setTimeout( function(){
				$jQ("#img_centre").removeShadow();
				$jQ("#img_centre").dropShadow({left:1, top:1, blur:2, opacity:0.7, color: "#000"});
			}, 1);*/
			//$jQ(this
			$jQ(this).removeShadow();
			$jQ(this).dropShadow({left:1, top:1, blur:2, opacity:0.7, color: "#000"});
		}
	});
	$jQ("#but_outils").click(function(){	affiche_outils();	});
	$jQ(".vign").mouseleave(function(){		deselect();			});
	$jQ("div.vign a.lien_off").click(function(){		return false;		});
	
	/*$jQ("div.vign a").click(function(){
		alert("vign click");
		//return false;
	});  */
	
	
	/*$jQ(".vign").mouseenter(function(e){
		$jQ("#legende").html($jQ(this).attr("id"));			
		//alert($jQ(this).attr("id"));
	});*/
	
});


