var Succes 			= {};
Succes.version 	= 1.2;


/*
 * functions below are jQuery dependent
 */
 
// TODO: add productcompare text to url

Succes.ProductCompare = function(uri,text) {
  var b 					= this;
  this.uri 				= uri + "/c/";
	this.text				= text;
  this.Products 	= { length: 0 };
  this.CheckBoxes = $("input.checkbox.compare");
  $.each(this.CheckBoxes, function() {
    $(this).removeAttr("disabled");
    $(this).removeAttr("checked");
    $(this).click(function() {
      b.AddRemoveProduct($(this));
    })
  });
  $.each($("a.compareButton.link"), function() {
    $(this).click(function() {
      b.Compare($(this));
    })
  })
};

Succes.ProductCompare.prototype = {
	Compare:function() {
	  var a, b = [];
	  if(this.Products.length > 1) {
	    for(a in this.Products)
				this.Products.hasOwnProperty(a) && a != "length" && b.push(a);
	    document.location.href = this.uri + b.join() + '/' + this.text.UrlSeo;
	  } else {
			alert(this.text.AtLeastTwo);
		}
	}, AddRemoveProduct:function(a) {
  	if($(a).attr("checked") == true) {
    	if(this.Products.length < 3) {
	      this.Products[$(a).val()] = true;
  	    this.Products.length++;
    	  this.Products.length == 3 && this.DisableAll()
	    }
		}else if($(a).attr("checked") == undefined) {
			delete this.Products[$(a).val()];
			this.Products.length--;
			this.Products.length == 2 && this.EnableAll()
		}
		this.SwitchClones(a);
	}, DisableAll:function() {
		$.each($("input.checkbox.compare:enabled"), function() {
			$(this).attr("checked") == undefined && $(this).attr("disabled", true)
		});
	}, EnableAll:function() {
		$.each($("input.checkbox.compare:disabled"), function() {
			$(this).removeAttr("disabled")
		});
	}, SwitchClones:function(a) {
		$.each(this.CheckBoxes, function() {
			if($(this).attr("id") != $(a).attr("id") && $(this).val() == $(a).val())
				$(this).attr("disabled") ? $(this).removeAttr("disabled") : $(this).attr("disabled", true);
		});
	}
};


Succes.ApplyCustomContentsFilter = function(elem, urlPrefix, urlSuffix)
{
	var checkBoxes = $('.contentsCheckbox');
	
	elem.href = urlPrefix + urlSuffix;
	
	$.each(checkBoxes, function(){
		if(this.checked == true)
		{
			elem.href = this.value + urlSuffix;
		}	
	});
}


Succes.updateFormats = function(Id)
{
	$('#DownloadableDescription, #DownloadableFileLinks').empty();
	$.each(D, function(){
		if(this.Id == Id){
			$('#DownloadableDescription').text(this.Desc);
		}
	});
	$.each(DFF, function(){
		if(this.IdD == Id){
			$('#DownloadableFileLinks').append('<a href="javascript:Succes.downloadFile(\'?IdDownloadableFile=' + this.Id + '\')">' + this.FN + '</a>');
			Succes.loadsIFR();
		}
	});
	return false;
}

Succes.downloadFile = function(url)
{
	// Google Analytics
	pageTracker._trackEvent("Shop", "DownloadFile", "Download a Succes downloadable", 0);

	window.location.href = url;
}

Succes.mailFriend = function()
{
	$("#mailfriendlink").slideToggle("fast", function()
	{
		$("#mailfriend").slideToggle("slow");	
	});
}

Succes.notMailFriend = function()
{
	$("#mailfriend").slideToggle("slow", function()
	{
		$("#mailfriendlink").slideToggle("fast", function()
		{
			$.each($('.mailfriendcontrol'), function()
			{
				$(this).removeAttr("disabled");
				$(this).val('');
			});
		});	
	});
}

Succes.sendMailFriend = function(form, url)
{
	var str = $('form').serialize();
	$.each($('.mailfriendcontrol'), function()
	{
		$(this).attr("disabled", "disabled");
	});
	try{
		$.post(url,
			{ 
				productpage	: $('#productpage').val(),
				friendemail	: $('#friendemail').val(),
				email				:	$('#email').val(),
				message			: $('#message').val()
			}, 
			function(data){
	    	$.each($('.frienderror'), function()
	    	{
	    		$(this).hide();
	    	});
	    	$.each($('.mailfriendcontrol'), function()
				{
					$(this).removeAttr("disabled");
				});
	    	if(!data.mailsend)
	    	{
		    	$.each(data.messages, function(i,message)
		      {
			  		$('#'+message.htmlObjId+'error').show();  	
			  		$('#'+message.htmlObjId+'error').text(message.message);
		      });	
	    	}
	    	else if (data.mailerror)
	    	{
	    		alert(data.mailerrormessage);
	    	}
	    	else if (data.mailsend && !data.mailerror)
	    	{
					
					// Google Analytics
					pageTracker._trackEvent("Shop", "MailAFriend", "Send mail-a-friend email", 0);
					
	    		alert(data.mailsendmessage);
					$("#mailfriend").slideToggle("slow", function()
					{
						$("#mailfriendlink").slideToggle("fast");
						$.each($('.mailfriendcontrol'), function()
						{
							$(this).val('');
						});
					});
	    	}
	    }, 'json'
		);
	}	catch(err) {
		alert($("#errormessage").val());	
	}
}


Succes.loadsIFR = function()
{
	if(typeof sIFR == "function"){
		var elems = ['div.productName', 'div.header', '#search div.searchHolder div.searchHeader', 'a.selectorLink', 'a span.selectorProductsLink', 'h4', 'h5', 'div.combiDetails div.productNamePromotion', 'div.cartSteps .step'];
		$.each(elems, function(){
			sIFR.replaceElement(named({
				sSelector: 	this,
				sFlashSrc: 	"/_Lib/Swf/sIFR/humanist 777 bt roman.swf", 
				sColor: 		"#002a5c", 
				sCase: 			"upper",
				sWmode:			"transparent"
			}));		
		});



		elems = ['div.contentBlockArrowLink'];
		$.each(elems, function(){
			sIFR.replaceElement(named({
				sSelector: 	this,
				sFlashSrc: 	"/_Lib/Swf/sIFR/humanist 777 bold bt.swf", 
				sColor: 		"#002a5c", 
				sCase: 			"upper",
				sWmode:			"transparent"
			}));		
		});



		elems = ['div.customHeader', '.contentBlockTitle'];
		$.each(elems, function(){
			sIFR.replaceElement(named({
				sSelector: 	this,
				sFlashSrc: 	"/_Lib/Swf/sIFR/century 725 bold bt.swf", 
				sColor: 		"#002a5c", 
				sWmode:			"transparent"
			}));		
		});



		elems = ['div span.calendarTitle'];
		$.each(elems, function(){
			sIFR.replaceElement(named({
				sSelector: 	this,
				sFlashSrc: 	"/_Lib/Swf/sIFR/century 725 bt.swf", 
				sColor: 		"#002a5c", 
				sWmode:			"transparent"
			}));		
		});
		 


		/*
		sIFR.replaceElement(named({ 
			sSelector: 	"label", 
			sFlashSrc: 	"/_Lib/Swf/sIFR/humanist 777 bold bt.swf", 
			sColor: 		"#002a5c", 
			sCase: 			"upper",
			sWmode:			"transparent"
		}));
		*/
	}
}