var sensTXT = 'normal';
window.addEvent('domready', function() {
	
	$$('a._text_perso').addEvent('click', function(e) {
		loadPopupTexte(this.href);
		return false;
	});

});

function loadPopupTexte(url) {
	var request = new Request.HTML({
		url: url,
		onComplete: function(data, data2, data3, data4) {
		//alert(data4);

			$('anim_fen').style.visibility = 'hidden';
			var div = new Element('div').set('html',data3);

			var data = div.getElement('div[id=ficheproduitsticker]');


			popupProduct = new PopUp(false, data, 'critere_infos', 970, 30);
				
			$$('a.closepopupProduct').addEvent('click', function(e) { setItem(); });
			//$$('div .productColor a').addEvent('click', function(e) { changeColor( });
			
			var flashvars = {'coulhexasticker': productDatas.color[currentColorID].co_hexa.replace('#', ''),
							'backgroundColor': currentBackgroundColor,
							'currentCadre': currentCadre,
							'currentPlante': currentPlante,
							'currentFauteuil': currentFauteuil,
							'currentSol':currentSol,
							'currentMur':currentMur,
							'currentFontSize':fonts_sizes[currentFontSizeID].fs_size,
							'currentFontSizeCM':fonts_sizes[currentFontSizeID].fs_cm,
							'currentFont':fonts[currentFontID].f_titre,
							'isText':true,
							'sens':sensTXT,
							'textPerso':textPerso
				
			};
			
			//var flashvars = {};
			var params =	{'allowscriptaccess':'always'
				};
			var attributes = {};
			swfobject.embedSWF("/templates/vision-deco/swf/header/patatatato.swf?v=0.96545436", "swf_header", "948", "300", "9.0.0", "", flashvars, params, attributes, loadSuccess);

			
			$('font').addEvent('change', function(e) {
				changeFontSWF( this.get('value') );
			});

			$('caract').addEvent('change', function(e) {
				changeSizeSWF( this.get('value') );
			});			

			$('txtstick').addEvent('keyup', function(e) {
				changeTextSWF( this.get('value') );
			});

			$('form_text_perso').addEvent('submit', function(e)
			{
				if($('txtstick').get('value').clean() == '')
				{
					e.stop();
					
					alert('Le contenu textuel ne peut pas être vide');

				}
			});

			$('flipstickers').addEvent('click', function(e) {
				if(sensTXT == 'normal') sensTXT = 'inverse';
				else sensTXT = 'normal';
				$('sens').set('value',sensTXT);
				flash.flipText();
			});
			
			$$('a.tooltip').each(function(element,index) 
			{
				var content = element.get('title').split('::');
				
				element.store('tip:title', content[0]);
				element.store('tip:text', content[1]);
			});
			
			var tipz = new Tips('.tooltip',
			{
				className : 'tool-tip'
			});
		}
	}).get();
}

function changeColorTexte(id, elem) {
	$$('div.productColor a img').dispose();
	$(elem).adopt(new Element('img', {'src':'/templates/vision-deco/images/btcoul.gif', 'width':20, 'height':18, 'border':0} ));
	$('hexa_color').set('html', productDatas.color[id].co_hexa);
	flash.changeTextColor( productDatas.color[id].co_hexa.replace('#', '') );
	$('in_colorID').set('value', id);
	currentColorID = id;
	changePrice();
}

function changeTextSWF(txt) {
	$('number_letter').set('html', txt.length);
	if(txt == '') {
		txt='#null#';
	}
	flash.changeText( txt );
	changePrice();
}

function changeFontSWF(id) {
	flash.changeFont( fonts[id].f_titre );
	currentFontID = id;
}

function changeSizeSWF(id) {
	flash.changeSize( fonts_sizes[id].fs_size, fonts_sizes[id].fs_cm );
	$('currentSize').set('html', fonts_sizes[id].fs_titre);
	currentFontSizeID = id;
	changePrice();
}

function loadSuccess() {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        flash = window['swf_header'];
    }
    else {
        flash = document['swf_header'];
    }
}

function partageEmail(a) {
	flash.getAllDatasSWF(a.href);
	//redirectPrintPage.delay(500,null, a);
}

function changePrice() {
	var color_price		= productDatas.color[currentColorID].co_prix;
	var money			= productDatas.color[currentColorID].co_money;
	var price			= fonts_sizes[currentFontSizeID].fs_prix;


/*
	if(price_promo.toFloat() > 0) {
		var new_price	= priceWidthColor(price_promo, color_price, money);
		var old_price	= priceWidthColor(price, color_price, money);
		$('p_old_price').setStyle('visibility', 'visible')
		$('old_price').set('html', old_price);
	}
	else {
		var new_price	= priceWidthColor(price, color_price, money);
		$('p_old_price').setStyle('visibility', 'hidden');
	}
	*/
	
	var new_price = priceWidthColor(price, color_price, money);
	$('new_price').set('html', new_price);
}

function priceWidthColor(price, color_price, money) {
	price = price.toFloat();
	var num_letter = ($('txtstick').get('value').length) ? $('txtstick').get('value').length : 0;
	price = (price * num_letter);
	color_price = color_price.toFloat();
	if(color_price > 0) {
		if(money ==1) {
			price = price + ((price / 100) * color_price);
		}
		else {
			price = price + color_price;
		}
	}
	return price.numberFormat(2, ',','');
}

function setItem() {
	flash.getAllDatasSWF();
	// currentColorID
	/*
	alert(mur);
	alert(fauteuil);
	alert(plante);
	alert(cadre);
	alert(sol);
	alert(background);
	*/


}

function actualWidthTXT(width) {
	var largeur = ((width-6)/3);
	largeur = largeur.numberFormat(1, '.','')
	$('widthText').set('html', largeur);
}

function saveSWF(mur, fauteuil, plante, cadre, sol, background, sens, url) {
	if(currentSizeID < 1) currentSizeID = '0';
	var textPerso = ( $('txtstick').get('value').length > 0) ? $('txtstick').get('value') : '';
	var postDatas = {'mur':mur, 'fauteuil':fauteuil, 'plante':plante, 'cadre':cadre, 'sol':sol, 'background':background, 'colorID':currentColorID, 'sens':sens, 'textPerso':textPerso, 'font':currentFontID, 'fSize':currentFontSizeID, 'fontNAME':fonts[currentFontID].f_titre, 'fSizeNAME':fonts_sizes[currentFontSizeID].fs_size };
	var request = new Request.JSON({
		url: '/user/save-swf.html',
		onComplete: function(data) {
			if(url) location.href = url;				
		}
	}).post({'datasSWF':postDatas});
	$('anim_fen').style.visibility = 'visible';
	popupProduct.close();
	
}

function partageFB() {
	flash.partageFB();
}

function partageFBresT(mur, fauteuil, plante, cadre, sol, background) {
	if(currentSizeID < 1) currentSizeID = '0';
	var productID = '0';
	if(!sens) sens = 'normal';
	var textPerso = ( $('txtstick').get('value').length > 0) ? $('txtstick').get('value') : '';
	var postDatas = {'mur':mur, 'fauteuil':fauteuil, 'plante':plante, 'cadre':cadre, 'sol':sol, 'background':background, 'colorID':currentColorID, 'imageID':currentSizeID, 'textPerso':textPerso, 'font':currentFontID, 'fSize':currentFontSizeID, 'fontNAME':fonts[currentFontID].f_titre, 'fSizeNAME':fonts_sizes[currentFontSizeID].fs_size };
	var request = new Request({
		url: '/user/make-url-fb.html',
		onComplete: function(data) {
			window.open(data,"nom_popup");
			//alert(data);
		}
	}).post({'datasSWF':postDatas});
}
