$(function(){
	$pop_cart_link = $(".pop_cart_link");
	$pop_cart = $("#pop_cart");
	$paymentForm = $("#paymentForm");
	$shiptocountry = $("#shiptocountry");
	$shiptostate_input = $("#shiptostate_input");
	$shiptostate_select = $("#shiptostate_select");
	$shiptostate_select_canada = $("#shiptostate_select_canada");
	$header_inner = $(".header_inner");
	$facebook_link = $(".facebook_link");
	$sameinfoasbilling = $("#sameinfoasbilling");
	
	if($sameinfoasbilling.length){
		
		var shipping_ele = {
			inps : {
				FIRSTNAME : $('input[name="FIRSTNAME"]').bind("keyup", function(){
					shipping_ele.updateName();
				}),
				LASTNAME : $('input[name="LASTNAME"]').bind("keyup", function(){
					shipping_ele.updateName();
				}),
				ADDRESS1 : $('input[name="ADDRESS1"]').bind("keyup", function(){
					shipping_ele.update('SHIPTOSTREET','ADDRESS1');
				}),
				CITY : $('input[name="CITY"]').bind("keyup", function(){
					shipping_ele.update('SHIPTOCITY','CITY');
				}),
				STATE : $('select[name="STATE"]').bind("change", function(){
					shipping_ele.updateState();
				}),
				ZIP : $('input[name="ZIP"]').bind("keyup", function(){
					shipping_ele.update('SHIPTOZIP','ZIP');
				}),
				SHIPTONAME : $('input[name="SHIPTONAME"]'),
				SHIPTOSTREET : $('input[name="SHIPTOSTREET"]'),
				SHIPTOCITY : $('input[name="SHIPTOCITY"]'),
				SHIPTOSTATE : $('input[name="SHIPTOSTATE"]'),
				SHIPTOSTATE_US : $('select[name="SHIPTOSTATE_US"]'),
				SHIPTOSTATE_CA : $('select[name="SHIPTOSTATE_CA"]'),
				SHIPTOZIP : $('input[name="SHIPTOZIP"]'),
				SHIPTOCOUNTRY : $('select[name="SHIPTOCOUNTRY"]')
			},
			sameasbilling_check : function(){
				return $sameinfoasbilling.is(":checked");
			},
			updateName : function(){
				if( this.sameasbilling_check() ){
					this.inps.SHIPTONAME.val(this.inps.FIRSTNAME.val()+' '+this.inps.LASTNAME.val());
				}
			},
			updateState : function(){
				if( this.sameasbilling_check() ){
					var findoption = 'option[value="'+this.inps.STATE.val()+'"]';
					if(this.inps.SHIPTOSTATE_US.find(findoption).length){
						this.inps.SHIPTOSTATE_US.find(findoption).attr('selected','selected');
						this.inps.SHIPTOSTATE.hide();
						this.inps.SHIPTOSTATE_US.show();
						this.inps.SHIPTOSTATE_CA.hide();
						this.inps.SHIPTOCOUNTRY.val('US');
					} else if(this.inps.SHIPTOSTATE_CA.find(findoption).length){
						this.inps.SHIPTOSTATE_CA.find(findoption).attr('selected','selected');
						this.inps.SHIPTOSTATE.hide();
						this.inps.SHIPTOSTATE_US.hide();
						this.inps.SHIPTOSTATE_CA.show();
						this.inps.SHIPTOCOUNTRY.val('CA');
					}
				}
			},
			update : function(target,from){
				if( this.sameasbilling_check() ){
					this.inps[target].val(this.inps[from].val());
				}
			}
		}
		
		$sameinfoasbilling.bind('change', function(){
			var inps = shipping_ele.inps;
			if( shipping_ele.sameasbilling_check() ){
				shipping_ele.updateName();
				shipping_ele.update('SHIPTOSTREET','ADDRESS1');
				shipping_ele.update('SHIPTOCITY','CITY');
				shipping_ele.updateState();
				shipping_ele.update('SHIPTOZIP','ZIP');
			}
		})
	}
	
	if($facebook_link.length){
		$facebook_link.bind("mouseenter", function(){
			$facebook_link.children("img")[0].src = '/kohana/theme/images/facebook_over.gif';
		}).bind("mouseleave", function(){
			$facebook_link.children("img")[0].src = '/kohana/theme/images/facebook.gif';
		})
	}
	
	function window_resize(){
		if($header_inner.length){
			$header_inner.width( ($("body").width()-1036)/2 + 1036);
		}
	}
	
	$(window).resize(function() {
		window_resize();
	});
	
	window_resize();
	
	if($shiptocountry.length){
		$shiptocountry.bind("change", function(){
			checkState(true);
		});
		function checkState(reset){
			if(reset===true){
				$shiptostate_input.val("");
				$shiptostate_select_canada.val("");
				$shiptostate_select.val("");
			}
			if($shiptocountry.val()=='US'){
				$shiptostate_input.hide();
				$shiptostate_select_canada.hide();
				$shiptostate_select.show();
			} else if($shiptocountry.val()=='CA'){
				$shiptostate_input.hide();
				$shiptostate_select_canada.show();
				$shiptostate_select.hide();
			} else {
				$shiptostate_input.show();
				$shiptostate_select_canada.hide();
				$shiptostate_select.hide();
			}
		}
		checkState(false);
	}
	
	if($pop_cart_link.length && $pop_cart.length){
		$pop_cart_link.remove();
		$pop_cart_link = $("<div class=\"pop_cart_link\"></div>");
		$pop_cart_link.appendTo(".main_top");
		$pop_cart_link.bind("click",function(event){
			event.preventDefault();
			$pop_cart.stop().show().animate({height:188},400);
		});
		$pop_cart.bind("mouseexit mouseleave", function(){
			$(this).stop().animate({height:0},400,function(){
				$(this).hide();
			});
		});
	};
	
	if($paymentForm.length){
		$paymentForm.bind("submit", function(){
			$(".purchase_submit").attr("disabled","disabled").val("Loading...");
		});
	};
	
	var usedby_wrap = $('.usedby'),
        usedby = usedby_wrap.children('ul'),
        ulPadding = 15;
	if(usedby_wrap.length){
		usedby_wrap.scrollLeft(0);
	    //Get menu width
	    var divWidth = usedby_wrap.width();
	
	    //Remove scrollbars
	    usedby_wrap.css({overflow: 'hidden'});
	    var usedby_item_width = 0;
	    usedby.find("li").each(function(i){
	    	usedby_item_width += $(this).width();
	    });
	    usedby.width(usedby_item_width+10000);
	
	    //Find last image container
	    var lastLi = usedby.find('li:last-child');
	
	    //When user move mouse over menu
	    usedby_wrap.mousemove(function(e){
			usedby_wrap.stop();
			clearInterval(scrollmelogos);
			var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
			var left = (e.pageX - usedby_wrap.offset().left) * (ulWidth-divWidth) / divWidth;
			usedby_wrap.scrollLeft(left-100);
	    }).bind("mouseleave", function(){
	    	scrollmelogos_init();
	    });
	    
	    
	    var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
    	var scrollmelogos = null;
    	var scrollmescrollleft = 0;
    	function scrollmelogos_init(){
    		lastLi = $('.usedby > ul').find('li:last-child');
    		ulmax = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding-divWidth;
    		var amt = 1;
    		var dir = 'r';
	    	scrollmelogos = setInterval(function(){
    			lastLi = $('.usedby > ul').find('li:last-child');
    			ulmax = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding-divWidth;
	    		usedby_wrap.stop().scrollLeft(usedby_wrap.scrollLeft()+(dir=='r'?amt:-(amt)))
	    		scrollmescrollleft = usedby_wrap.scrollLeft();
	    		if(scrollmescrollleft>0 && (scrollmescrollleft>ulmax)){
	    			dir = 'l';
	    		}
	    		if(scrollmescrollleft<=0){
	    			dir = 'r';
	    		}
	    	}, 20);
		}
		scrollmelogos_init();
	};

	
});
