var popupDiv = '';
var jsAmp = '&';

function popupCreate(source, params, div, width, height)
{
	var div = "#" + div;
	
	var html = $.ajax({
	   url: source,
	   type: "GET",
	   data: params,
	   async: false
	 }).responseText;
	 
	$(div).html(html);
	
	$(function() {
		$(div).dialog({
			bgiframe: true,
			height: height,
			width: width,
			resizable: false,
			modal: true,
			hide: 'drop',
			buttons: { 
				"Fermer": function() { $(this).dialog("close"); } 
			},
			show: 'drop'
			}
		);
	});
	
	$(div).dialog('open');
	
	popupDiv = div;
	
	$('.ui-widget-overlay').click(function() { $(popupDiv).dialog("close"); });
}

function addNotice(n)
{
	$('#notices-inner').append('<p style="color: #cb121e;">' + n + '</p>');

	if($('#notices').css('display') == 'none')
		$('#notices').show();
}

var helpTimer = Array();
var __inlineEdit = Array();

function switchInlineHelp(helpId, onOff)
{
	if(onOff == 'on' && ($('#inlineHelp' + helpId).css('display') == 'none'))
		$('#inlineHelp' + helpId).fadeIn();
	else if(onOff == 'off')
		$('#inlineHelp' + helpId).fadeOut();
}

function inlineMouseIn(helpId)
{
	clearTimeout(helpTimer[helpId]);
	switchInlineHelp(helpId, 'on');
}

function inlineMouseOut(helpId)
{
	if(!__inlineEdit[helpId])
	{
		helpTimer[helpId] = setTimeout(function(){
			switchInlineHelp(helpId, 'off');
		}, "400")
	}
}

function setCloseOnClickOutside(divId, closeFunction)
{
	$(document).unbind('click');

	setTimeout(function() { 
		var dialogElId = divId;

		$(document).click(function (e) {
			var clickedOutside = true;
			$(e.target).parents().andSelf().each(function () { // search parents and self
/* hack pour le bug de calendrier sur shop-orders */
if((this.title == 'Next') || (this.title == 'Prev'))
{
	clickedOutside = false;
	return false;
}
				if (this.id == dialogElId) { // found
					clickedOutside = false;
					return false;
				}
			});
			if (clickedOutside) {
				closeFunction();
				$(document).unbind('click'); 
			}
		});
	 }, 50);
}


function priceFormat(num)
{
	num = num.toString().replace(/$|,/g, '');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	cents = num % 100;
	num = Math.floor(num / 100).toString();
	if(cents < 10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
	num = num.substring(0, num.length - (4 * i + 3)) + ',' +
	num.substring(num.length - (4 * i + 3));
	return (((sign) ? '' : '-') + num + ',' + cents);
}


function showLogin(submitAction)
{
	__loginSubmitAction = submitAction;

	$("#popupLogin").html(__loginPopup);

	
	$("input[name=submit]").click(function() { loginAndSubmit() });
	$("input[name=cancel]").click(function() { $('#popupLogin').fadeOut(); $(document).unbind('click'); } );


	if($("#popupLogin").css("display") == "none")
	{
		$("#popupLogin").fadeIn();
		setCloseOnClickOutside('popupLogin', function() { $("#popupLogin").fadeOut(); });
	}
}


function showNewsletter()
{
	$("#popupNewsletter").html(__loginNewsletter);

	
	$("input[name=submit]").click(function() { submitNewsletter() });
	$("input[name=cancel]").click(function() { $('#popupNewsletter').fadeOut(); $(document).unbind('click'); } );


	if($("#popupNewsletter").css("display") == "none")
	{
		$("#popupNewsletter").fadeIn();
	}
}


function submitNewsletter()
{
	$.post('index.php?ajax=1&subscribe=1&popup=1&', 
		{popup: true,
		 email: $("input[name=email]").val()}, 
		 function(data){ 
			
			
			if(data.substring(0, 2) == 'ok')
			{
				alert(data.substring(2));
				$('#popupNewsletter').fadeOut();
				$(document).unbind('click');
			}
			else
			{
				alert(data);
				$("input[name=email]").focus();
			}
		}
	);

}


function loginAndSubmit()
{
	$.post('login.php', 
		{popup: true,
		 login: $("input[name=login]").val(), 
		 password: $("input[name=password]").val()}, 
		 function(data){ 
			if(data == "pas ok")
			{
				alert(__badLoginOrPassword); 
				$("input[name=login]").val('');
				$("input[name=password]").val('');
				$("input[name=login]").focus();
			}
			else
			{
				eval(__loginSubmitAction);
			}
		}
	);
}

function eraseFilledInput(field)
{
	$(field).removeClass('input-default-text');
	$(field).val('');
}

function showList(div)
{
	$("ul[id*=listeSelect]").slideUp("fast");
	
	var jQueryDiv = "#" + div;
	
	if($(jQueryDiv).css("display") == "none")
	{
		$(jQueryDiv).slideDown("fast");
		setCloseOnClickOutside(div, function() { $(jQueryDiv).slideUp("fast"); });
	}
}

function __IPESave(code, domId, helpSectionId, language, helpId)
{
	$.get('index.php', 'ajax=1&saveHelp=1&code=' + code + '&title_' + language + '=' + $('#ipe-title-' + domId).val() + '&contents_' + language + '=' + $.URLEncode($('#ipe-contents-' + domId).val()) + '&id=' + helpId + '&helpSectionId=' + helpSectionId + '&marginTop=' + $('#ipe-marginTop-' + domId + '').val() + '&marginLeft=' + $('#ipe-marginLeft-' + domId + '').val() + '&width=' + $('#ipe-width-' + domId + '').val() + '&height=' + $('#ipe-height-' + domId).val(),
		function() { 
			$('#inlineHelpDiv' + domId + '').html($('#ipe-contents-' + domId + '').val());
			$('#inlineHelpDiv' + domId + '').show();
			$('#form-edit-' + domId + '').hide();
			$('#ipe-link-' + domId + '').show();
			__inlineEdit['' + domId + ''] = false;
			inlineMouseOut('' + domId + '');
		}
	);
}

function calcIframeHeight(the_iframe)
{/*alert(the_iframe);*/
    the_iframe.style.height = the_iframe.contentWindow.document.body.scrollHeight; //changement de la hauteur
    the_iframe.style.width = the_iframe.contentWindow.document.body.scrollWidth; //changement de la largeur
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


function strEscape(str)
{
	s = str.replace("'", "&apos;");
	return s;
}


function addFavMerchant(merchantId)
{
	$('#addFavMerchantPicto').fadeOut();
	$.post("product.php",
		   { favMerchantId: merchantId },
		   function(data){
				$('#addFavMerchantPicto').fadeIn();
				
				$('#addFavMerchantLink').after('<span>' + $('#addFavMerchantLink').html() + '</span>');
				$('#addFavMerchantLink').remove();
			}
		 );
}
