var couleur_sticker="000000";
//var couleur_sticker= document.getElementById('input_couleur').value;
var elements_choisie={couleur:"",background:""};
var configuration_sticker_user={};
var last_libelle="";
var flash_sticker={
	swf:"",
	swfu:"",
	divOpen:"",
	divClic:"",
	couleur:new Array(0,0,0),
	bgcolor:new Array(255,255,255),
	deplacement:true,
	config:{},
	init:function()
	{
		this.swf=document.getElementById("flashSticker");
		this.config=configuration_sticker_user;
		this.swf.unloading();
		this.swf.loadsticker(this.config.sticker);
		if (this.config.couleurs.length > 0) {
			this.ongletCouleurs();
		}
		this.ongletSticker();
		this.ongletBackground();
		initVignette();
	},
	ongletCouleurs:function()
	{
		
		cl=this.config.couleurs;
		pere=$("#cSticker");
		Dcontener=$(document.createElement("div"));
		Dcontener.attr("id","colorContener");
		for(i=0;i<cl.length;i++)
		{
			cont=$(document.createElement("div"));
				div=$(document.createElement("div"));
				
				div.attr("rel_lib",cl[i].libelle);
				div.attr("rel_id",cl[i].id);
				div.attr("rel",cl[i].couleur);
				
				if(typeof cl[i] == "object")	cl[i]=cl[i].couleur;
				div.css({backgroundColor:"#"+cl[i],cursor:"pointer"});
				div.mouseover(function()
				{
					data=$(this).attr("rel_lib");
					lib=data;	
					$("#couleur_spec").text(lib);
				});
				div.mouseout(function()
				{
					$("#couleur_spec").text(last_libelle);
				});
				div.click(function()
				{
					
					
					inputColor = $(this).attr("rel_id") +"_"+ $(this).attr("rel") +"_"+ $(this).attr("rel_lib");
					document.getElementById('colorInput').value = inputColor;
					
					couleur=$(this).attr("rel");
					
					if ( couleur.length != 6 )
						couleur = '000000';
					
					couleur_sticker=couleur;
					r=couleur.substr(0,2);
					eval("r=0x"+r);
					v=couleur.substr(2,2);
					eval("v=0x"+v);
					b=couleur.substr(4,2);
					eval("b=0x"+b);
					//alert("Rouge: "+r+"\nVert: "+v+"\nBleu: "+b+"");
					flash_sticker.swf.changesticker(r,v,b);
					//flash_sticker.couleur=new Array(r,v,b);
				});
				//div.text(cl[i]);
			cont.append(div);
			cont.addClass("colors");
			Dcontener.append(cont);
		}
		
		
		couleur=$('<div align="center"><img src="../modules/mod_shop_products/ressources/couleur_spe.jpg" /></div>');
		pere.append(Dcontener);
		pere.append(couleur);
		//couleurs_special
		special=$(document.createElement("div"));
		special.addClass("special")
		cl=this.config.couleurs_special;
		for(i=0;i<cl.length;i++)
		{
				cont=$(document.createElement("div"));
				div=$(document.createElement("div"));
				//div.attr("rel",cl[i].couleur+"|"+cl[i].libelle);
				
				div.attr("rel_lib",cl[i].libelle);
				div.attr("rel_id",cl[i].id);
				div.attr("rel",cl[i].couleur);
				
				div.css({background:"url("+cl[i].image+")",cursor:"pointer"});
				div.mouseover(function()
				{
					data=$(this).attr("rel_lib");
					
					lib=data;	
					$("#couleur_spec").text(lib);
					/*
					data=$(this).attr("rel").split("|");
					lib=data;	
					$("#couleur_spec").text(lib);
					*/
				});
				div.mouseout(function()
				{
					$("#couleur_spec").text(last_libelle);
				});
				div.click(function()
				{
					inputColor = $(this).attr("rel_id") +"_"+ $(this).attr("rel") +"_"+ $(this).attr("rel_lib");
					document.getElementById('colorInput').value = inputColor;
					
					data=$(this).attr("rel").split("|");
					couleur=data[0];
					
					couleur_sticker=couleur;
					r=couleur.substr(0,2);
					eval("r=0x"+r);
					v=couleur.substr(2,2);
					eval("v=0x"+v);
					b=couleur.substr(4,2);
					eval("b=0x"+b);
					//alert("Rouge: "+r+"\nVert: "+v+"\nBleu: "+b+"");
					flash_sticker.swf.changesticker(r,v,b);
					
					//flash_sticker.couleur=new Array(r,v,b);
					/*data=$(this).attr("rel").split("|");
					couleur=data[0];
					lib=data[1];
					couleur_sticker=couleur;
					r=couleur.substr(0,2);
					eval("r=0x"+r);
					v=couleur.substr(2,2);
					eval("v=0x"+v);
					b=couleur.substr(4,2);
					eval("b=0x"+b);
					alert("Rouge: "+r+"\nVert: "+v+"\nBleu: "+b+"");
					flash_sticker.swf.changesticker(r,v,b);
					$("#couleur_spec").text(lib);
					last_libelle=lib;
					//flash_sticker.couleur=new Array(r,v,b);*/
				});
				//div.text(cl[i]);
			cont.append(div);
			cont.addClass("colors");
			
			special.append(cont);
		}
		
		/*
		clear=$(document.createElement("div"));
		clear.css({clear:"left"});
		special.append(clear);
		*/
		special.append($('<br style="clear:both" /><div id="couleur_spec"></div>'));
		pere.append(special);
		
	},
	ongletSticker:function()
	{
		pere=$("#mSticker");
		div=$(document.createElement("div"));
		div.addClass("flashDeplacement");
		div.attr("id","flash_deplacement");
		div.click(function()
		{
			if(flash_sticker.deplacement)
				return;
			$("#flash_deplacement").removeClass("flashDeplacement");
			$("#flash_deplacement").addClass("flashDeplacementActive");
			flash_sticker.deplacement=true;
			$("#flash_rotation").removeClass("flashRotationActive");
			$("#flash_rotation").addClass("flashRotation");
			flash_sticker.swf.rotatestick();
		});
		pere.append(div);
		
		div=$(document.createElement("div"));
		div.addClass("flashRotation");
		div.attr("id","flash_rotation");
		div.click(function()
		{
			if(!flash_sticker.deplacement)
				return;
			$("#flash_rotation").removeClass("flashRotation");
			$("#flash_rotation").addClass("flashRotationActive");
			flash_sticker.deplacement=false;
			
			$("#flash_deplacement").removeClass("flashDeplacementActive");
			$("#flash_deplacement").addClass("flashDeplacement");
			flash_sticker.swf.rotatestick();
		});
		pere.append(div);
		
		$("#flash_deplacement").removeClass("flashDeplacement");
		$("#flash_deplacement").addClass("flashDeplacementActive");
		
		slider=$(document.createElement("div"));
		slider.attr("id","flash_slider");
			indiq=$(document.createElement("div"));
		slider.addClass("flash_slider");
			indiq.addClass("flash_indicator");
		slider.append(indiq);
		pere.append(slider);
		$('.flash_slider').Slider(
			{
				accept : '.flash_indicator',
				fractions : 200,
				onSlide : function( cordx, cordy, x , y)
				{
					cordx=cordx*2 - 100 +1;
					$("#slide").html(cordx+"%");
					flash_sticker.swf.resize(cordx);
				}
			}
		);
		slide=$(document.createElement("div"));
		slide.attr("id","slide");
		slide.html("0%");
		pere.append(slide);
		$(".flash_indicator").css({left:"90px"});
	},
	ongletBackground:function()
	{
		cl=this.config.background;
		pere=$("#apSticker");
		Dcontener=$(document.createElement("div"));
		Dcontener.attr("id","colorContenerBackground");
		for(i=0;i<cl.length;i++)
		{
			cont=$(document.createElement("div"));
				div=$(document.createElement("div"));
				div.attr("rel",cl[i]);
				div.css({backgroundColor:"#"+cl[i],cursor:"pointer"});
				div.click(function()
				{
					couleur=$(this).attr("rel");
					r=couleur.substr(0,2);
					eval("r=0x"+r);
					v=couleur.substr(2,2);
					eval("v=0x"+v);
					b=couleur.substr(4,2);
					eval("b=0x"+b);
					flash_sticker.swf.changefond(r,v,b);
				});
			cont.append(div);
			cont.addClass("colors");
			Dcontener.append(cont);
		}
		pere.append(Dcontener);
		down=$(document.createElement("div"));
		down.addClass("flashDown");
		down.click(function()
		{
			flash_sticker.swf.download_background();
		});
		pere.append(down);
		titre=$(document.createElement("div"));
		titre.addClass("flashCategGrandNoir");
		titre.html("<b>T&eacute;l&eacute;chargement</b> de la photo de votre pi&egrave;ce");
		titre.attr("rel","cSticker");
		titre.addClass("titreTelecharger");
		Dcontener=$(document.createElement("div"));
		Dcontener.attr("id","imageContener");
		descript=$(document.createElement("div"));
		descript.attr("id","imageDescription");
		descript.html("T&eacute;l&eacute;charger votre photo et simuler ce Sticker dans");
		complement=$(document.createElement("div"));
		complement.attr("id","imageDescriptionSuite");
		complement.html("votre pi&egrave;ce.");
		zoneImage=$(document.createElement("div"));
		zoneImage.attr("id","imageLoadedContener");
		if (this.config.couleurs.length == 0) {
			titre.addClass("titreTelechargerSimple");
			Dcontener.addClass("imageContenerSimple");
			zoneImage.addClass("imageLoadedContenerSimple");
			down.addClass("flashDownSimple");
		}
		pere.append(titre);
		Dcontener.append(descript);
		Dcontener.append(complement);
		pere.append(Dcontener);
		pere.append(zoneImage);
	}
};

function uploadProgress(fileObj, bytesLoaded) {
	try {
		var percent = Math.ceil((bytesLoaded / fileObj.size) * 100);
		flash_sticker.swf.download_background("Telechargement "+percent+"%");
		
	} catch (ex) { this.debug(ex); }
}

function loadSticker(e,flashUrl,config)
{
	e=$(e);
	if(e.length==0)
	{
		alert("ID du calque introuvable");
		return;
	}
	e.empty();
	titre=$(document.createElement("div"));
	titre.addClass("flashCategGrandRose");
	titre.html("<b>Aper&ccedil;u</b> de votre stickers");
	titre.attr("rel","cSticker");
	fl=$(document.createElement("div"));
	fl.attr("id","contenerFlash");
	e.append(titre);
	e.append(fl);
	configuration_sticker_user=config;
	Dcontener=$(document.createElement("div"));
	e.append(Dcontener);
	
	Dcontener.addClass("divActionFlash");
		div=$(document.createElement("div"));
		div.addClass("flashCategPetitRose");
		if (config.couleurs.length > 0) {
			div.html("<b>Couleurs</b> du sticker");
			div.attr("rel","cSticker");
			div.attr("id","firstRose");
			Dcontener.append(div);
				act=$(document.createElement("div"));
				act.addClass("flashAction");
				act.addClass("borderRose");
				act.attr("id","cSticker");
				Dcontener.append(act);
				flash_sticker.divClic=div;
		}
		div=div.clone();
		div.html("<b>Position</b> du sticker");
		div.attr("rel","mSticker");
		if (!(config.couleurs.length > 0)) {
			div.attr("id","firstRose");
		} else {
			div.attr("id","");
		}
		Dcontener.append(div);
			act=$(document.createElement("div"));
			act.addClass("flashAction");
			act.addClass("borderRose");
			act.attr("id","mSticker");
			//act.hide();
			Dcontener.append(act);
		div=div.clone();
		div.html("<b>Couleurs</b> de l'arri&egrave;re plan");
		div.attr("rel","apSticker");
		div.attr("id","");
		Dcontener.append(div);
			act=$(document.createElement("div"));
			act.addClass("flashAction");
			act.addClass("borderRose");
			act.attr("id","apSticker");
			//act.hide();
			Dcontener.append(act);

	
	
	var so = new SWFObject(flashUrl, "flashSticker", "350", "350", "8");
	so.addParam("flashvars", "wmode=transparent&ssid="+configuration_sticker_user.ssid+"&dir="+configuration_sticker_user.rootDir);
	so.setAttribute('wmode', 'transparent');
	so.write("contenerFlash");
}

function flashstickerinit()
{
		flash_sticker.init();
}

function initVignette()
{
	$.getJSON("../modules/mod_shop_products/ressources/sticker.php", { session: configuration_sticker_user.ssid }, function(json)
	{
		$.each(json.images,function(i,image)
		{
			if($("#"+image).length==0)
			{
				img=$(document.createElement("img"));
				img.attr("id",image);
				img.attr("src","../modules/mod_shop_products/ressources/sticker.php?session="+configuration_sticker_user.ssid+"&img="+image+".jpg");
				img.click(function()
				{
					flash_sticker.swf.loadfond("../modules/mod_shop_products/background/"+configuration_sticker_user.ssid+"/"+$(this).attr("id")+".jpg");
				});
				$("#imageLoadedContener").append(img);
			}
		});
	});
}

function loadVignette()
{
	$.getJSON("../modules/mod_shop_products/ressources/sticker.php", { session: configuration_sticker_user.ssid }, function(json)
	{
		$.each(json.images,function(i,image)
		{
			if($("#"+image).length==0)
			{
				img=$(document.createElement("img"));
				img.attr("id",image);
				img.attr("src","../modules/mod_shop_products/ressources/sticker.php?session="+configuration_sticker_user.ssid+"&img="+image+".jpg");
				img.click(function()
				{
					flash_sticker.swf.loadfond("../modules/mod_shop_products/background/"+configuration_sticker_user.ssid+"/"+$(this).attr("id")+".jpg");
				});
				$("#imageLoadedContener").append(img);
			}
		});
		flash_sticker.swf.loadfond("../modules/mod_shop_products/background/"+configuration_sticker_user.ssid+"/"+json.last+".jpg");
	});
}


function debug_sticker(string)
{
	d=$("#debug");
	d.html(d.html()+"<br />"+string);
}

