/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
*
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

/* Other stuff */

// Custom Tooltips
function tool_over() { $('#tooltip').load('/tooltip/?id='+$(this).attr("href").slice(1)); }
function tool_out() {} // Blank function required below

var tooltip_config = {
	sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)
	interval: 200, // number = milliseconds for onMouseOver polling interval
	over: tool_over, // function = onMouseOver callback (REQUIRED)
	timeout: 0, // number = milliseconds delay before onMouseOut
	out: tool_out // function = onMouseOut callback (REQUIRED)
}

jQuery(document).ready(function($) {
	// First setup the tooltip handler
	$('.cart_add').tooltip({
		showURL: false,
		delay: 1000,
		fade:250,
		bodyHandler: function() {	return $('#tooltip').html(); }
	});
	// Delay fetching of tooltips sleightly to significantly reduce ajax calls
	// This
	$('.cart_add').hoverIntent(tooltip_config);
});


// Cart Functions
jQuery(document).ready(function($){
	$('.cart_add').live('click', function(){
		var tmp = unescape($(this).attr('href').substring(1));
		tmp = tmp.split('&');
		var id = tmp[0];
		var args = {id: id, action:'add'};
		if(tmp.length > 1) {
			var tmp2 = tmp[1].split('=');
			args[tmp2[0]] = tmp2[1];
		}
		$.get('/api/cart/', args, function(data) {
			$('#dialog').html(data).dialog('option', 'title', 'Cart Updated');
			if($('#shopping_cart').length) {
				document.location = '/commerce/cart.asp';
			} else {
				$('#dialog').dialog('open');
			}
		});
		return false;
	});
	$(".quantity").keyup(function() {
		var tmp = $(this).attr('id').split(':');
		var args = {id: tmp[1], action:"update", quantity: $(this).val()};
		if(tmp.length >2) {
			args.priceselect = tmp[2];
		}
    $.get('/api/cart/', args, function(data) {
      $('#cart_module').html(data);
			if($('#shopping_cart').length) {
				document.location = '/commerce/cart.asp';
			}
    });
  });
  $('.remove_one').live('click', function(){
		var tmp = unescape($(this).attr('href').substring(1));
		tmp = tmp.split('&');
		var id = tmp[0];
		var args = {id: id, action:'remove'};
		if(tmp.length > 1) {
			var tmp2 = tmp[1].split('=');
			args[tmp2[0]] = tmp2[1];
		}
    $.get('/api/cart/', args, function(data) {
      $('#cart_module').html(data);
			if($('#shopping_cart').length) {
				document.location = '/commerce/cart.asp';
			}
    });
    return false;
  });
  $('.remove_from_cart').live('click', function(){
		var tmp = unescape($(this).attr('href').substring(1));
		tmp = tmp.split('&');
		var id = tmp[0];
		var args = {id: id, action:'remove'};
		if(tmp.length > 1) {
			var tmp2 = tmp[1].split('=');
			args[tmp2[0]] = tmp2[1];
		}
		args.quantity = 'all';
    $.get('/api/cart/', args, function(data) {
      $('#cart_module').html(data);
			if($('#shopping_cart').length) {
				document.location = '/commerce/cart.asp';
			}
    });
    return false;
  });
	$('#ship_same').click(function(){
		if($(this).is(':checked')) {
			$('#ship_same_cont').slideUp('fast');
		} else {
			$('#ship_same_cont').slideDown('fast');
		}
	});
	$('#show_login').click(function(){ $('#login').slideDown('fast'); });
	$('#dialog').dialog({
		modal: true,
		autoOpen:false,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	});
	$('#ship_option').change(function() {
		if($(this).val() != 'Standard') {
			$('#ship_info').slideDown('slow');
		} else {
			$('#ship_info').slideUp('fast');
		}
	});
});

// Thumbnails
jQuery(document).ready(function($){
	var zndx = 1000;
	$('.thumb').each(function(){
		var w = $(this).find('img').width();
		var h = $(this).find('img').height();
		$(this).css({width:w, height:h});
	});
	$(".thumb a img").hover(
		function() {
			if($(this).data('width') == undefined) {
				$(this).data('width', $(this).attr('width'));
				$(this).data('height', $(this).attr('height'));
			}
			var w = $(this).data('width');
			var h = $(this).data('height');
			$(this).css('zIndex', zndx).animate({ width:w*4, height:h*4, top:-h, right:-w/2 }, 700, 'easeOutBack');
			zndx++;
		},
		function() {
			$(this).stop().animate({ width:$(this).data('width'), height:$(this).data('height'), top:0, right:0 }, 150, 'easeInOutBack');
		}
	);
	$(".thumb_left a img").hover(
		function() {
			if($(this).data('width') == undefined) {
				$(this).data('width', $(this).attr('width'));
				$(this).data('height', $(this).attr('height'));
			}
			var w = $(this).data('width');
			var h = $(this).data('height');
			$(this).css('zIndex', zndx).animate({ width:w*4, height:h*4, top:-h, left:-w/2 }, 700, 'easeOutBack');
			zndx++;
		},
		function() {
			$(this).stop().animate({ width:$(this).data('width'), height:$(this).data('height'), top:0, left:0 }, 150, 'easeInOutBack');
		}
	);
});

function change_payment() {
	var pay_method = $('#payment_method').val();
	var all_methods = $('#cc_info,#check_info,#mo_info,#cod_info,#net30_info');
	switch(pay_method) {
		case 'Check':
			$(all_methods).not('#check_info').slideUp();
			$('#check_info').slideDown();
			break;
		case 'Money Order':
			$(all_methods).not('#mo_info').slideUp();
			$('#mo_info').slideDown();
			break;
		case 'COD':
			$(all_methods).not('#mo_info').slideUp();
			$('#mo_info').slideDown();
			break;
		case 'Net30':
			$(all_methods).not('#net30_info').slideUp();
			$('#net30_info').slideDown();
			break;
		default:
			$(all_methods).not('#cc_info').slideUp();
			$('#cc_info').slideDown();
			break;
	}
}
/* For Shopping Cart/Checkout Page */
jQuery(document).ready(function(){
	$('#payment_method').change(change_payment).keyup(change_payment);
});
