/* 
 * A collection of things that must happen on the load of a page
 * 
 */


$(document).ready(function()
{
	// If your on the home page and you have viewed nothing don't show things i love
	if( location.pathname == maxshop.urlPrefix + '/' )
	{
		if( $('#things-i-love > li').length <= 0 &&  $('#recently-viewed-items > li').length <= 0 )
		{
			// Hide the favouries tab
			$('#content_footer').remove();
		}
	}


	// Create the favourite tabs
	$(".page_sub_module_left").tabs({ fx: { height: 'toggle', opacity: 'toggle', duration: 'fast'}, show:maxshop.initScrollPane});

	/*
	 * Load all the black accordions
	 */

	$.ui.accordion.animations.recent = function(options) {
		$('#recentAccordion dt:last').css('background-image', 'none');
		this.slide(options, {
			easing: "easeinout",
			duration: 700
		});
	};


	$.ui.accordion.animations.complete = function(options) {
		$('.complete_the_look .blackAccordion dt:last').css('background-image', 'none');
		this.slide(options, {
			easing: "easeinout",
			duration: 700
		});
	};

	$('#recentAccordion').accordion(
	{
		autoHeight: true,
		fillSpace: true,
		header: 'dt',
		animated: "recent",
		active: 1
	});

	$('.complete_the_look .blackAccordion').accordion(
	{
		autoHeight: true,
		header: 'dt',
		animated: "complete",
		active: 1
	});

	$("#search > input:text").click(maxshop.clearSearch);

	$('#search input[value=go]').click(function()
	{
		if( this.form.q.value == 'Enter Search' || this.form.q.value == '' )
		{
			alert('Please enter a search term');
			maxshop.clearSearch();
			return false;
		}

		return true;
	});

	$.each($('.testimonial .review'), function(i, review)
	{

		var reviewWordSplit = $(review).html().toString().split(" ");
		if( reviewWordSplit.length > 20 )
		{
			// Split the string into 2 sections
			var shortString = reviewWordSplit.splice(0, 18);
			$(review).html(shortString.join(" "));

			// Add the hidden part of the text
			var more = $('<span>&nbsp;</span>');
			// Strip out the last quote char.
			var moreText = reviewWordSplit.join(" ")
			moreText = moreText.substr(0, (moreText.length - 5));

			more.append(moreText);
			more.css('display', 'none');
			$(review).append(more);
			$(review).append('&rdquo;');

			// Add a read more link
			var readMore = $('<a href="#">read more</a>');
			readMore.click(function()
			{
				$(this).hide();
				more.css("display", "inline");
				return false;
			});

			$(review).append('&nbsp;');
			$(review).append(readMore);
		}
	});

	$("tr.promo input[type='image']").click( function()
	{
		$("input[name='action']").attr({value:"apply_coupon"});
	});

	maxshop.addThinsILoveHelp();

	$('#sec_link').fancybox({
			'zoomSpeedIn': 0,
			'zoomSpeedOut': 0,
			'frameWidth': 430,
			'frameHeight': 480,
			'overlayShow': true,
			'hideOnContentClick': true
		});

	// Add all the textbox restrictions

	$('input#qty').restriction({type:'integer', length:3});
	$('.item_qty').restriction({type:'integer', length:3});
	$('#search > input:text').restriction({length:200});
	$('#email_address').restriction({type:'email'});
	$('#mobile').restriction({type:'phone'});
	$('#telephone').restriction({type:'phone'});
	$('#add_postcode').restriction({length:7});
	$('#card_secnumber').restriction({type:'integer', length:4});
	$('#cc_number').restriction({type:'creditcard'});
	$('.address').restriction({type:'address'});




	$('.datepicker').datepicker({ dateFormat: 'dd/mm/yy' });


	var findPostCodeLink = '';
	var urlPrefix = maxshop.urlPrefix.toUpperCase();
	if( urlPrefix == '/AU' || urlPrefix == '/NZ')
	{
		var url = '';

		if( urlPrefix  == "/AU")
		{
			url = "http://www.auspost.com.au/postcodes/";
		}
		
		if( urlPrefix  == "/NZ")
		{
			url = "http://www.nzpost.co.nz/Cultures/en-NZ/OnlineTools/PostCodeFinder/";
		}

		findPostCodeLink = $('<a>find a postcode</a>');
		findPostCodeLink.attr(
		{
			title:"find a postcode",
			href:url,
			target:"_blank"
		})
	}

	$('#add_postcode').after(findPostCodeLink);


	if( $('select.country').val() != "AU" )
	{
		$('#add_state').parent().parent().hide();
	}


	$('select.country').change( function()
	{
		var country = $(this);
		if( country.val() != "AU" )
		{
			$('#add_state').parent().parent().hide();
			$('#add_state').val("");
		}
		else
		{
			$('#add_state').parent().parent().show();
		}
		if( country.val() == "AU" || country.val() == "NZ" )
		{
			$('.postcode_required').show();
		}
		else
		{
			$('.postcode_required').hide();
		}
	})

	$("li.country_selector a").attr('href', $("li.country_selector a").attr('href') + "?inline=true");
	$("li.country_selector a").fancybox({
		'zoomSpeedIn': 0,
		'zoomSpeedOut': 0,
		'frameWidth': 700,
		'frameHeight': 500,
		'overlayShow': true,
		'hideOnContentClick': false,
		'hideOnOverlayClick': false,
		'showCloseButton': true,
		'enableEscapeButton': false
	});
});
