function anim_basket() {
	jQuery('.basket-wrapper').show(250, function(){
		jQuery('.basket-wrapper li, .basket-background-top span, .basket-background-top div, .basket-background-bottom a').animate({opacity:1}, 500);
	});
}
function addToCartSimple() {
	act = jQuery('#product_addtocart_form').attr('action');
	jQuery('#product_addtocart_form').attr('action',act+"?redirto=samepage");
	productAddToCartForm.submit();
}
function cleanLi() {
	jQuery("ul").not('#navigator, .shopping-steps, .selector, .basket-wrapper ul, .comments, .splitThree').each(function(){
		 jQuery(this).children("li:last").css({borderRight:"0",borderBottom:"0",background:"none",paddingRight:"0",marginRight:"0"});
	});
}
jQuery(document).ready(function() {
	maxH = 0;
	jQuery("ul.searchPress li h4").each(function(i,d){
	  if(jQuery(d).height() > maxH) {
		maxH = jQuery(d).height();
	  }
	});
	jQuery("ul.searchPress li h4").each(function(i,d){
		jQuery(d).height(maxH);
	});

	jQuery(".pretty").prettyPhoto({
				theme: 'light_square', /* light_rounded / dark_rounded / light_square / dark_square */
				showTitle: false,
				padding: 0
	});

	jQuery(".newsletter").submit(function(){
		jQuery.get("/external/ajax/email.home/email/"+jQuery(this).children('input[type="text"]').val(),function(data) { alert(data); });
		return false;
	});
	jQuery('#emailPopup').jqm({ajax: '@href', trigger: 'a.sendToAFriend'});
	jQuery('#sendFriendPopup').jqm({ajax: '@href', trigger: 'a.sendFriend'});
	jQuery('.submit').click(function(){
		frm = jQuery(this).parents("form");
		frm.submit();
		return false;
	});

	jQuery('input#keyword, .newsletter input[type=text], textarea').focus(function(){
		var currentVal = jQuery(this).val();										// Read initial field value
		jQuery(this).val('');														// Clear initial value

		jQuery(this).blur(function(){
			var getNewVal = jQuery(this).val();										// Read new input value
			if ( getNewVal == '' || getNewVal == ' ' ) {
				jQuery(this).val(currentVal);										// Switch to initial value
			}
		});
	});

	var mode = 'gri';
	if ( mode == 'grid' ) {
		jQuery('#grid').css({ visibility:'visible' });
	} else {
		jQuery('#grid').css({ visibility:'hidden' });
	}

	jQuery('input[type=text], textarea').focus(function(){
		var currentVal = jQuery(this).val();										// Read initial field value
		jQuery(this).select();														// Clear initial value

		jQuery(this).blur(function(){
			var getNewVal = jQuery(this).val();										// Read new input value
			if ( getNewVal == '' || getNewVal == ' ' ) {
				jQuery(this).val(currentVal);										// Switch to initial value
			}
		});
	});
	
	jQuery('form#newsletterHome input[type=text]').focus(function(){
		var currentVal = jQuery(this).val();										// Read initial field value
		jQuery(this).val('');														// Clear initial value

		jQuery(this).blur(function(){
			var getNewVal = jQuery(this).val();										// Read new input value
			if ( getNewVal == '' || getNewVal == ' ' ) {
				jQuery(this).val(currentVal);										// Switch to initial value
			}
		});
	});

	// Cuffon
	if ( jQuery('h2.page-title').length || jQuery('form#discount-coupon-form h3 || ul.ranges a.color').length ) {
		Cufon.replace('h2.page-title, form#discount-coupon-form h3, ul.ranges a.color');
	}

	// fix png for IE
	//jQuery('img[src$=.png], div, li, a').ifixpng();

	// remove any borders on last LI element
	cleanLi();

	if ( jQuery('a#viewRange').length ){
		Cufon.replace('a#viewRange,h1,h2,ul.splitThree li h3');
	}

	// store selector
	jQuery('#store ul li').hover(
		function(){
			jQuery('#store ul li a').not('.on').css({
				visibility:'visible',
				top:'23px'
			});
		},
		function(){
			jQuery('#store ul li a').not('.on').css({
				visibility:'hidden',
				top:'23px'
			});
		}
	);

	// navigation on multiple levels
	var active = jQuery("ul#nav li.active");
	var timer = 0;
	var time_trg = null;
	if ( jQuery('#nav li ul li ul').length ){
		jQuery('#nav li ul li ul').remove();
	}
	
	var mouseout = function(who){

		active.children("ul").show();
		active.addClass("active").removeClass('inactive');
		
		if ( time_trg != active[0] ){
			jQuery(time_trg).children("ul").fadeOut('3000');
			jQuery(time_trg).removeClass("active");
		}
	};
	jQuery("ul#nav>li").not('#search').hover(function() {
			mouseout();
			if ( !jQuery(this).is(".active") ) {
				active.children("ul").hide();
				active.removeClass('active').addClass("inactive");
			}

			jQuery(this).children("ul").fadeIn('3000');
			jQuery(this).addClass("active");
			clearTimeout(timer);
		},
		function() {
			time_trg = this;
			timer = setTimeout(mouseout,2000);
		}
	);

	// basket
//	jQuery('.basket-wrapper').hide();
	jQuery('.basket-wrapper li, .basket-background-top div, .basket-background-bottom a, .basket-background-top span').css({ opacity:0 });

	jQuery('a#baskettrigger').click(function(){
		anim_basket();
		return false;
	});

	var basketTimeout = false;
	jQuery('.basket-wrapper').hover(
		function(){
			if ( basketTimeout ) {
				clearTimeout(basketTimeout);
			}
		},
		function(){
			var $this = jQuery(this);
			var basketTimeout = window.setTimeout( function() {
				jQuery('.basket-wrapper li, .basket-background-top span, .basket-background-top div, .basket-background-bottom a').animate({
					opacity:0
				}, 500, null, function(){
					jQuery('.basket-wrapper').hide(250);
				});
				return false;
			},6000);
		}
	);

	// no follow link on zoom links
	jQuery('.zoom').click(function(){
		return false;
	})

	// calendar hover
	jQuery('.selector li').not('.hovered').hover(
		function(){
			jQuery(this).addClass('hovered');
		},
		function(){
			jQuery(this).removeClass('hovered');
		}
	);

	// tabs
	if ( jQuery('#tabs').length ) {
		jQuery('#tabs>ul li:last').addClass('last');
		//console.log(jQuery('#tabs ul li:last'));
		jQuery('#tabs').tabs();
	}

	// fix range
	if ( jQuery('ul.ranges, ul.distributors') ) {
		jQuery('ul.ranges li:nth-child(3n), ul.distributors li:nth-child(3n)').addClass('clean');
	}

	// alert via jqModal
	if ( jQuery('#alert').length ) {
		jQuery('#alert').jqm({overlay: 60, modal: true, trigger: false});

		  // trigger an alert whenever links of class alert are pressed.
		  jQuery('a.alert').click(function() {
		    alert(jQuery(this).attr('title'));
		    return false;
		  });
	}

	// handle different address in checkout process step 2
	jQuery('#differentaddress').hide();
	jQuery('input[name="sameaddress"]').click(function(){
		if ( jQuery('input[name="sameaddress"]:checked').val() == "other address" )
			jQuery('#differentaddress').show();
		else
			jQuery('#differentaddress').hide();
	});

	// print page
	jQuery('a.print').click(function(){
		window.print();
		return false;
	});

	// multiple boxes on distributors
	if ( jQuery('div.multiple a').length ){
		jQuery('div.multiple a').hide();

		jQuery('div.multiple').hover(
			function(){
//				jQuery(this).children('em').css('display','block');
				jQuery(this).children('a').fadeIn(300);
			},
			function(){
				jQuery(this).children('a').fadeOut(300);
			}
		);
	}

	// remove basket row ---- yeah, it is useless, but Susan asked for it !?!?
	jQuery('a.removebasketrow').click(function(){

		// remove the row
		jQuery(this).parent('td').parent('tr').fadeOut('200',function(){
			jQuery(this).parent('td').parent('tr').remove();

			// compute grand total
			if ( jQuery('.basket-content .grand-total span').text() == '47.98') {
				jQuery('.basket-content .grand-total span').text('23.99');
			} else {
				jQuery('.basket-content .grand-total span').text('0');
			}

		});

		// update top shopping cart
		jQuery('.'+jQuery(this).attr('rel')).remove();
		jQuery('#numprods').html(parseInt(jQuery('#numprods').html())-1);

		return false;
	});

	// keyword lookup
	jQuery("#keyword").autocomplete(
		"/catalogsearch/ajax/suggest/", // change to a PHP file
		{
			delay:10,
			minChars:2,
			matchSubset:1,
			matchContains:1,
			cacheLength:10,
			onItemSelect:selectItem,
			onFindValue:findValue,
			formatItem:formatItem,
			autoFill:true,
			width:188
		}
	);
/*
	jQuery("#keyword").autocompleteArray(
		[
			"Aberdeen", "Ada"
		],
		{
			delay:10,
			minChars:1,
			matchSubset:1,
			onItemSelect:selectItem,
			onFindValue:findValue,
			autoFill:true,
			maxItemsToShow:10,
			width:188
		}
	);
*/
});

// Define indexOf for IE
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

jQuery.fn.log = function (msg) {
  console.log("%s: %o", msg, this);
  return this;
};


/* Autocomplete functions */
function findValue(li) {
	if( li == null ) return alert("No match!");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;

	//alert("The value you selected was: " + sValue);
}

function selectItem(li) {
	findValue(li);
}

function formatItem(row) {
	return row;
	//return row[0] + " (id: " + row[1] + ")";
}

function lookupAjax(){
	var oSuggest = jQuery("#keyword")[0].autocompleter;

	oSuggest.findValue();

	return false;
}

function lookupLocal(){
	var oSuggest = jQuery("#keyword")[0].autocompleter;

	oSuggest.findValue();

	return false;
}

function alert(msg) {
  jQuery('#alert')
    .jqmShow()
    .find('div.jqmAlertContent')
      .html(msg);
}
