 $(window).load(function() { 
	$('#viewed-wrap ul li').equalHeights();
	$('.viewed-item-wrap > a table').height($('.viewed-item-wrap > a table').parent('a').outerHeight());
 });
 
 $(function(){

	// Some IE rounded corner fixes 
	$('.no-borderradius .round-top-left').append('<span class="ie-rounded-corner-top-left"></span>');
	$('.no-borderradius .round-bottom-left').append('<span class="ie-rounded-corner-bottom-left"></span>');
	$('.no-borderradius .round-top-right').append('<span class="ie-rounded-corner-top-right"></span>');
	$('.no-borderradius .round-bottom-right').append('<span class="ie-rounded-corner-bottom-right"></span>');
	 
	// Top nav display toggle
	 $('a.admin-arrow').click(function(){
		$('#nav-admin-options,#nav-admin-login').toggle();
	});
	 
	// More IE hover fixes
	$('#nav-secondary #subnav-products > li,#nav-secondary #subnav-industry > li').mouseenter(function() {
		$(this).addClass('hover');
	}).mouseleave(function() {
		$(this).removeClass('hover');
	});

	// Masthead nav functionality
	$('#nav-primary a').click(function() {
		$('#nav-secondary > ul').removeClass('selected');
		$($(this).attr('href')).addClass('selected');
		$('#nav-primary a').removeClass('selected');
		$('#subnav-search').hide();
		
		$($(this).attr('href')).show();
		$('#nav-primary li span').hide();
		$(this).addClass('selected');
		$('span', $(this).parent()).show();
		return false;
	});
		
	// Button
	$(".button, .button-add-to-cart, .button-next, .button-account, .button-account-small").button();
	$(".buttonset").buttonset();
	$(".button-add-to-cart-icon, .button-add-to-cart-small-icon").button({ icons: { primary: 'ui-icon-plusthick' } });
	$(".button-default-sml").button({ icons: { primary: 'ui-icon-arrow-1-e' } });
	
	// Disabled buttons
	$(".paginate-disabled").button({ disabled: true });
	
	// Masthead Quick search
	$('#subnav-search input.textfield').clearField();
	$('#subnav-search input.textfield').autocomplete({
		source: '/shop/search/quickcode',
		minLength: 2,
		select: function( event, ui ) {
			$( "#subnav-search input[name=term]" ).val( ui.item );
		}
	}).data( "autocomplete" )._renderItem = function( ul, item) {
		return $( "<li></li>" )
		.data( "item.autocomplete", item.code + " " + item.label)
		.append($( "<a></a>" ).html( '<strong>' + item.code + '</strong> ' + item.label ) )
		.appendTo( ul );
	};
		
	// Product listing sort field
	$('#category-sort-wrap input').click(function() {
		// reload the page 
		new_target = $(this).parent().parent().attr('action');
		new_target = new_target.replace(/\/sort_by\//, '/' + $(this).val() + '/');
		$(this).parent().parent().attr('action', new_target);
		$(this).parent().parent().submit();
	});
	
	// Jump to page select box
	$('#category-pagination select').change(function() {
		document.location = $(this).val();
	});
	
	// Home page banner rotation
	rotate = function() {
		$current = $('.splash-wrap li.current');
		$next = $current.next('li');
		
		if ($next.length == 0) {
			$next = $('.splash-wrap li.first');
		}
		$current.removeClass('current').fadeOut(500);
		$next.addClass('current').fadeIn(500);
	};
	$('.splash-wrap').each(function() {
		setInterval(rotate, 10 * 1000);
	});
	
	// Product page image swapping
	$('#product-image-meta-wrap a.product-img-tmb').click(function(e) { 
		$('#product-image-meta-wrap a.product-img-lrg img').attr('src', $(this).attr('href')); 
		e.preventDefault(); 
	});
	
	// Admin table sorting
	$("#admin-products-list .primary-ext .fancy, .tablesorter").tablesorter();
	
	$('#product-image-meta-wrap #viewed-wrap li:nth-child(4)').addClass('row-end');
	
	// Assign a class to the li's on the last row of the product list
	(function(){
		var itemsPerRow, numItems, numRows, remainder, i;
		itemsPerRow = 4;
		numItems = $('#viewed-wrap ul li').length;
		remainder = numItems%4;
		numRows = (numItems - remainder) / itemsPerRow;
							
		for (i = 0; i < numItems; i++) {
			if (numItems <= 4) { // If there is only 1 row
				$('#viewed-wrap ul li:eq(' + i + ')').addClass('last-row');
			} else {
				var itemsInRows = numRows * 4;
				if (remainder === 0) { // If the row is full 
					if(i > (numItems - 5)){
						$('#viewed-wrap ul li:eq(' + i + ')').addClass('last-row');	
					}
				}				
				if (i >= (itemsInRows)) { // If the row isn't full
					$('#viewed-wrap ul li:eq(' + i + ')').addClass('last-row');					
				}
			}			
		}
	} )();
	
	$('#add-new-domain-form p:first').addClass('form-input');
	$('#add-new-domain-form p:last').addClass('form-submit');	
	
	$('#submit-products').click(function(){
		$("#debtor-products-form").submit();
		return false;
	});	
	
	$('.button-add-to-cart-other').click(function(){

		var product_id = $(this).attr("id").replace("add-","");
		var qty = $("#qty-"+product_id).val();
		
		if(qty > 0){
			$.post('/shop/ajaxaddtocart', { 'qty': qty, 'product_id': product_id },  function(data) {
				
				//change the quantity back to 0
				$("#qty-"+product_id).val("0");
				
				//pop the message up saying that it's been added
				if(qty == 1){
					qtytext = "1 item";
				} else {
					qtytext = qty+" items";
				}
				
				$("#added-name").text(data);
				$("#added-qty").text(qtytext);
				$('#product-added').show();
				setTimeout(function() { $('#product-added').fadeOut(500); }, 10 * 1000);
				$('#product-added a[href=#]').click(function() {
					$('#product-added').fadeOut(500);
				});
				
				
				//add the quantity of the cart in the cart-count class
				total = parseInt($(".cart-count").html());
				total = parseInt(total) + parseInt(qty);

				if(total == 1){
					$(".cart-count").html("1 item");
				} else {
					$(".cart-count").html(total+" items");
				}
			});
		}
		return false;
	});
	
	//$("#load-saved-cart").easyconfirm({locale: { title: 'Please confirm',text:'This operation will load your previously saved cart and remove your current cart', button: ['No','Yes']}});
	$("#load-saved-cart").click(function(){
		return confirm("This operation will load your saved cart and remove your current cart. Are you sure you want to do this?");
	});
	
	//this is for the your cart has been saved message
	setTimeout(function() { $('#product-added.cart-saved').fadeOut(500); }, 10 * 1000);
		$('#product-added a[href=#]').click(function() {
			$('#product-added').fadeOut(500);
		});
		
	$("#save_cart_button").click(function(){
		$("#save_cart").val(1);
		$("#full-cart-summary").submit();
	});
});







/**
 * Equal Heights Plugin
 * Equalize the heights of elements. Great for columns or any elements
 * that need to be the same size (floats, etc).
 * 
 * Version 1.0
 * Updated 12/10/2008
 *
 * Copyright (c) 2008 Rob Glazebrook (cssnewbie.com) 
 *
 * Usage: $(object).equalHeights([minHeight], [maxHeight]);
 * 
 * Example 1: $(".cols").equalHeights(); Sets all columns to the same height.
 * Example 2: $(".cols").equalHeights(400); Sets all cols to at least 400px tall.
 * Example 3: $(".cols").equalHeights(100,300); Cols are at least 100 but no more
 * than 300 pixels tall. Elements with too much content will gain a scrollbar.
 * 
 */

(function($) {
	$.fn.equalHeights = function(minHeight, maxHeight) {
		tallest = (minHeight) ? minHeight : 0;
		this.each(function() {
			if($(this).height() > tallest) {
				tallest = $(this).height();
			}
		});
		if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
		return this.each(function() {
			$(this).height(tallest).css("overflow","hidden");
		});
	}
})(jQuery);
