// JavaScript Document
function RGBtoHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}
function toHex(N) {
 if (N==null) return "00";
 N=parseInt(N); if (N==0 || isNaN(N)) return "00";
 N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
 return "0123456789ABCDEF".charAt((N-N%16)/16)
      + "0123456789ABCDEF".charAt(N%16);
}


$(document).ready(function() {
	
	//zoom image
	var options =
	{
		zoomWidth: 500,
		zoomHeight: 470,
		title :false
	}
	$(".bottlezoom").jqzoom(options);
	
	//player 360
	tempCat = $(".vid360").attr('id');
	url =  String('/video/360/spirits/'+tempCat+'.mov');
	color = $(".most-popular p a").css("color");
	color = color.substring(4,color.length);
	color = color.substring(0,color.length-1);
	color = color.split(',');
	color = RGBtoHex( parseInt(color[0]),parseInt(color[1]),parseInt(color[2]) );
	imageUrl = String('/img/spirits/360start/'+tempCat+'.png');
	$('.vid360').flash(
        { 
		  wmode : "transparent",
		  src: '/flash/360.swf',
          width: 441,
          height: 860 ,
		  allowfullscreen: true,
		  flashvars: { path: url, color: '0x'+color, image: imageUrl }
		  },
        { version: 9 }
    );
	
    initFavorite();


});

function initFavorite(){
	$(".bottle #content div.last p a.favorite").click( addToFavorite );
	$(".bottle #content div.last p a.favoriteremove").click( removeFromFavorite );
}

function removeFromFavorite(){
	
	objectId = $(this).attr("rel");
	indiceDepart = objectId.lastIndexOf('/')+1;
	indiceArrivee = objectId.length;
	objectId = objectId.substring(indiceDepart, indiceArrivee);
	
	$.getJSON(base_url + "collection/ajax/?method=removeFromFavorite&objectId="+escape(objectId)+"&typeFav=spirits_bottle",
		function(data){
			if(data.success){	
				$('a.favorite').remove();
				$(".wr_fav p").removeClass("favoriteremove");
				$(".wr_fav p").addClass("favorite");
				if ( $("body").hasClass("en") )
				$(".wr_fav p").html('<a href="#" class="favorite" rel="favorite/'+objectId+'">Add to my favorites</a>');
				if ( $("body").hasClass("fr") )
				$(".wr_fav p").html('<a href="#" class="favorite" rel="favorite/'+objectId+'">Ajouter à mes favoris</a>');
				initFavorite();
			}
		}
	);
	
	return false;
}

function addToFavorite(){
	
	objectId = $(this).attr("rel");
	indiceDepart = objectId.lastIndexOf('/')+1;
	indiceArrivee = objectId.length;
	objectId = objectId.substring(indiceDepart, indiceArrivee);
			
	$.getJSON(base_url + "collection/ajax/?method=addtoFavorite&objectId="+escape(objectId)+"&typeFav=spirits_bottle",
		function(data){
			if(data.success){	
				$('a.favorite').remove();
				$(".wr_fav p").removeClass("favorite");
				$(".wr_fav p").addClass("favoriteremove");
				if ( $("body").hasClass("en") )
				$(".wr_fav p").html('<a href="#" class="favoriteremove" rel="favorite/'+objectId+'">Remove from my favorites</a>');
				if ( $("body").hasClass("fr") )
				$(".wr_fav p").html('<a href="#" class="favoriteremove" rel="favorite/'+objectId+'">Retirer de mes favoris</a>');
				initFavorite();
			}
		}
	);
	
	return false;
}
