$(function () {

	$('body').addClass('js');
	
	if (!$('body').hasClass('.merchantTest')) {
		
		//
		$('.offer').voucher();
	}
		
	$('.new-window').attr('target', '_blank');

	// Set the search error text according to country
	if ($('body').attr('id') == 'en') {
		var searchErrortext = 'Enter a store name or restaurant';
		var defaultText = 'Type in a store e.g. Tesco, Pizza Hut';
	} else {
		var searchErrortext = 'Shop oder Restaurant hier eingeben';
		var defaultText = 'Shop eingeben z.B. Otto, Karstadt, ...';
	}
	
	
	// If the user doesn't enter a search term, display an alert
	$('#hdr form, #search, #noresultssearch, div.panel_suggest form').submit(function() {
		var e = $(this).find('input:text');
		if ((e.val() == defaultText) || (e.val() == searchErrortext) || (e.val() == '')) {
			e.val(searchErrortext).addClass('noSearchTerm').data('original', searchErrortext);
			return false;
		}
	});

	$('a#vcLogo').css('background-position', 'bottom left');
	$('a#vcLogo').hover(function () {
		$(this).find('strong').stop(true, true).fadeOut('fast');
	}, function () {
		$(this).find('strong').fadeIn('fast');
	});

	$('.initvalue').each(function () {
		var e = $(this);
		e.data('original', e.val()).focus(function () {
			if (e.val() === e.data('original')) {
				e.removeClass('initvalue');
				e.val('');
			}
		}).blur(function () {
			if (e.val() === '') {
				e.addClass('initvalue');
				e.val(e.data('original'));
			}
		});
	});

	if ($('.calendar').length > 0)
	{
		$('.calendar').datepicker({
			'dateFormat': 'dd/mm/yy',
			'firstDay': 1,
			'duration': '',
			'prevText': '&laquo;',
			'nextText': '&raquo;'
		});
	}

      	var querystring = function () {
		var obj = {};
		var query = window.location.search.substring(1);
		if (query != "") {
			var vars = query.split("&");
			for (var i = 0; i < vars.length; i++) {
				var p = vars[i].split("=");
				obj[p[0]] = p[1];
			}
		}
		return obj;
	}();

	try {
		$.cookie('shownthickbox', null);

		if ('ei' in querystring || ('lb' in querystring && querystring['lb'] === '0'))
		{
			$.cookie('shownlightbox', 'true', { path: '/', expires: 500 });
		}

		if ((('test' in querystring && querystring['test'] === '1') || (('bi' in querystring && querystring['bi']) == 'xp6571') || $.cookie('shownlightbox') !== 'true') && typeof thickbox !== 'undefined' && typeof thickbox.sw === "string" && thickbox.sw === "on" && typeof thickbox.paths !== "undefined") // Is lightbox on?
		{
			var show = false;

			if ($.cookie('firstpage') === 'true' || ('lb' in querystring && querystring['lb'] === '1') || (('bi' in querystring && querystring['bi']) == 'xp6571'))
			{
				if (window.location.pathname in thickbox.paths)
				{
					show = thickbox.paths[window.location.pathname];
				}

				if (querystring['lb'] === '1' ) show = (querystring['lbt'] || 'rest');
				if (querystring['bi'] == 'xp6571'  )  show  = true;
			}

			if (show !== false) {
				setTimeout(function () {
					tb_show('thickbox', '/lightbox/' + show, false);
				}, 1000);
			}

			$.cookie('firstpage', 'true', { path: '/', expires: 1 });
		}
	} catch (e) { }


	$('#hdr form, #search, #noresultssearch, div.panel_suggest form').find('input:text').attr('autocomplete', 'off').autocomplete('/xhr/auto/', {
		scrollHeight: 500,
		selectFirst: false
	});

	if ($('#printablevouchers #pcats ul').children().length > 3)
	{
	    var showmore = $('<li class="showmore"><ul class="moreItems clear"></ul><a href="#"><span>more</span></a></li>');
		$('#printablevouchers #pcats ul').append(showmore);

		var dropdownItems = $('#printablevouchers #pcats .dropdownItem');
		$('#printablevouchers #pcats .moreItems').append(dropdownItems);

        $('#printablevouchers #pcats .moreItems a:last').addClass('last');
		$('.showmore > a').click(function() {
			$('#pcats .moreItems').toggle();
			return false;
		});
	}

    if ($('#ppccats ul').children().length > 5)
	{
	    var showmore = $('<li class="showmore"><ul class="moreItems clear"></ul><a href="#"><span>more</span></a></li>');
		$('#ppccats ul').append(showmore);

		var dropdownItems = $('#ppccats .dropdownItem');
		$('#ppccats .moreItems').append(dropdownItems);

        $('#ppccats .moreItems a:last').addClass('last');
		$('.showmore > a').click(function() {
			$('#ppccats .moreItems').toggle();
			return false;
		});
	}

	$('.sidetabs').each(function () {
		var e = this;
		var panel = $(this).next();

		$(e).find('a').click(function () {
			var f = this;
			panel.find('> *').hide();
			panel.find('.panel_' + $(f).attr('rel')).show();
			$(f).parent().parent().find('a').removeClass('selected');
			$(f).addClass('selected');
			return false;
		});
	});

	$('.addbookmark').click(function() {
		var href = window.location.href, title = document.title;
	    if (window.sidebar) {
	        window.sidebar.addPanel(title, href,"");
	    } else if (document.all) {
	        window.external.AddFavorite(href, title);
	    }
		return false;
	});

	// Sitewide RHS carousel
    
	if ($('body').attr('id') == 'en')
	{
	    if (!$('.side').hasClass('noCarousel'))
    	{
    	    var holidayCarouselItems = [
        	    { identifier: 'holiday_main', url: '/holiday-voucher-codes.html', text: 'View All Holiday Vouchers' },
				{ identifier: 'holiday_lowcostholidays', url: '/holiday-voucher-codes.html#voucher-96438', text: 'View All Holiday Vouchers' },
				{ identifier: 'holiday_opodo', url: '/holiday-voucher-codes.html#voucher-94870', text: 'View All Holiday Vouchers' }
        	];

        	var carousel = new Carousel('holidayCarouselItems', holidayCarouselItems, { speed: 4000 });

        	if (carousel)
        	{
        	    // Are we on a merchant page?
        	    if (($('#merchantPage').length > 0) || ($('#merchanthowto').length > 0) || ($('#merchantreview').length > 0) || ($('#merchantvideo').length > 0) || ($('#merchantsales').length > 0)  )
        	    {
        	        // Carousel needs to go after website screenshot
        	        $('.side .website').after(carousel);
    	        }
    	        else
    	        {
    	            // Carousel can just go at the top of the RHS
        	        $('.side').prepend(carousel);
        	    }
        	}
    	}
    }   

});
