jQuery(function ($) {
	$.fn.voucher = function (opt) {
		var options = $.extend({ 
			//reveal: '.reveal',
			//peek: '.reveal .code a',
			//peekClass: 'hint',
			//termsClass: 'toggle',
			// termsHandle: 'View terms &amp; conditions',
			foolproof: true,
			
			// for id: 1997
			//foolproofText: '<p>This YYYYYYYY voucher code has now expired and is unlikely to work.',
			//foolproofTextMer: 'You can view all our active codes and discounts by following the link below:</p><p><a href="/WWWWWWWWWW">YYYYYYYY</a> voucher codes.',
			//foolproofTextEnd: '</p>',
			//foolproofTextDE: '<p>Dieser YYYYYYYY Gutscheincode ist nun ungültig und funktioniert nicht mehr.</p>',
			//foolproofTextDEMer: 'Für alle gültigen Gutscheinecodes und Rabatte folgen Sie bitte diesem Link:</p><p><a href="/WWWWWWWWWW">YYYYYYYY</a> Gutscheincode.',
			//foolproofTextDEEnd: '</p>',
			
			foolproofText: '<p>The YYYYYYYY web site has been been opened in a new window ready for you to shop, it is underneath the current one. To redeem the discount, enter the code <strong>ZZZZZZZZZ</strong> when you reach the checkout.</p><p>If the web site did not open <a href="XXXXXX">please click here</a>.</p>',
			foolproofTextDE: '<p>Die YYYYYYYY Website wurde direkt hinter Ihrem aktuellen Fenster geöffnet. Um den Rabatt einzulösen geben Sie einfach den Code <strong>ZZZZZZZZZ</strong> beim Bezahlvorgang ein.</p><p>Falls sich die Website nicht geöffnet haben sollte <a href="XXXXXX">klicken Sie bitte hier</a>.</p>',
			foolproofClass: 'foolproof'
		}, opt);
		
		$.fn.extend({
		/*
			peek: function () {
				var rev = $(this).find(options.reveal);
				
				$(this).find(options.peek).hover(function () {
					rev.addClass(options.peekClass);
				}, function () {
					rev.removeClass(options.peekClass);
				});
				
				return this;
			},
			reveal: function (e) {
				$(this).attr('target', '_blank');
				
				$(e).find('.revealbutton').hide();
				var text = $.trim($(e).find('.code strong').text());
								
				if ($(this).parents('div .expired').length > 0)
				{
					$(e).find('.reveal').addClass('revealedexpired');  	
				}
				else
				{
					$(e).find('.reveal').addClass('revealed');    
				}
				
				if ($(e).find('.offer .expired').length > 0)
				{
					$(e).find('.revealed').addClass('offerexpired'); 	
				} 
				if ($(e).find('.copied').length === 0)
				{
					var link = $(e).find('.revealbutton').attr('href');
					if (typeof link === "undefined")
					{
						link = $(e).find('.o_side a').attr('href');
					}
					link = link.replace(/tl=[a-z0-9\-]+/, 'tl=reopen');
						if (lang == 'de') {$(e).find('.reveal .revealbutton').after('<div class="copied"><a href="' + link + '" target="_blank">Diesen Code beim<br /> Bezahlen eingeben.</a></div>');
					}else{
						$(e).find('.reveal .revealbutton').after('<div class="copied"><a href="' + link + '" target="_blank">Enter this Code<br />at the Checkout</a></div>');
					}
				}
				$(e).find('.code_b').html('<strong>' + $(e).find('.code strong').text() + '</strong>');
				
				var link = $(this).attr('href');
				var mer_window = window.open(link, '_blank', 'toolbar=1,location=1,directories=1,scrollbars=1,resizable=1,status=1,menubar=1');
				if (typeof mer_window === "object")
				{
					mer_window.blur();
				}
				
				return false;
			},

			terms: function () {
				var terms = $(this).find('.terms');
				if (terms.length > 0)
				{
					terms.find('span').hide();
					// terms.prepend('<a href="#" class="' + options.termsClass + '">' + options.termsHandle + '</a>');
					terms.find('strong').addClass('makelink');
					terms.find('.toggle').click(function () {
						terms.find('*').not('.' + options.termsClass).toggle();
						return false;
					});
				}
			},
			*/
			foolproof: function () {
				if ($(this).find('.foolproof').length === 0)
				{
					var link = $(this).find('a.revealcode').attr('href');
					var slug = $(this).find('a.revealcode').attr('href');
					var themerchant = $(this).find('.merchant').find('span').text();
					if (themerchant == '') {
						themerchant = $('.merchant h1').find('span').text();
					}
					var ismerchantpage = false;
					if($('#merchantPage')) {
    					ismerchantpage = true;
    			    }
					var thecode = $(this).find('.code').find('strong').text();
					if (lang == 'de') {
						$(this).parent().find('.reveal').after('<div class="' + options.foolproofClass + '">' +
						    options.foolproofTextDE.replace('XXXXXX', link).replace('YYYYYYYY', themerchant).replace('ZZZZZZZZZ', thecode) + '</div>');
					}else{
						$(this).find('.reveal').after('<div class="' + options.foolproofClass + '">' +
						    options.foolproofText.replace('XXXXXX', link).replace('YYYYYYYY', 'Monkey').replace('ZZZZZZZZZ', thecode) + '</div>');
					}
					$(this).find('.' + options.foolproofClass).slideDown();
				}
			},
			setFoolproof: function () {
				options.foolproof = true;
			}
		});
		
		return this.each(function () {
			var e = this;
			
			if (!$(this).hasClass('showterms'))
			{
				$(this).terms();
			}
			// else
			// 			{
			// 				$(this).find('h4.terms').prepend('<strong>Terms &amp; Conditions</strong><br />');
			// 			}
			
			if ($(this).is('.code'))
			{
				$(this).peek();
				
				$(this).find('a[href*="/out/"]').click(function () {
					if (options.foolproof) {
						$(e).foolproof();
					}
					return $(this).reveal(e);
				});
			}
		});
	};
});

