$(function(){

var is_chrome = (navigator.userAgent).toLowerCase().indexOf("chrome") != -1;
if(is_chrome) $('.input_select').css({'background':'#000'});

$("input[name='process_payment']").click(function(){
	$("#creditcardpayment").submit();
	$(this).attr("value","Processing your Credit Card Payment");
	$(this).attr("disabled",true);
});

$("input[type='image']").click(function(){
	//$(this).attr("disabled","disabled");
});

// ------------------ Home

	$("#header .register .input_text").focus(function () {
		if($(this).val() === $(this).attr("title")) {
			$(this).val("");
		}
	}).blur(function () {
		if ($(this).val() === "") {
			$(this).val($(this).attr("title"));
		}
	});

	
// ------------------ About
	
	//Hover
	$("#content.about ul li a").hover(function(){
	
		//turn off all other buttons
		$("#content.about ul li a img").each(function(){
			$(this).attr("src",$(this).attr("src").replace("on","off"));
		});
		
		//glow current button
		var img = $(this).find("img");
		img.attr("src",img.attr("src").replace("off","on"));
	
		//show current content
		$("#content.about .content div").hide();
		$("#content.about .content ." + $(this).attr("rel")).show();
	});

// ------------------ Products

	//Hover
	$("#content.products .listing .item").hover(function(){
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
	});
	//Checkout button
	$( '#product-checkout_btn' ).click ( function () { top.location = '/Cart'; } );
	
	
// ------------------ Product Detail

	//add to cart
	$("#content.product .purchase .inner .form .add").click(function(){
		$("#content.product .purchase .inner .form .input_button .loader").show();
		$.ajax({
			type: "POST",
			url: "/_ajax_cart.php",
			data: ({
				action: "add",
				id: $("input[name='id']").val(),
				sku: $("input[name='sku']").val(),
				quantity: $("select[name='quantity']").val(),
				color_id: $("input[name='color_id']").val()
			}),
			success: function(msg){
				updateCartQuickContents();
				setTimeout(function(){
					setTimeout(function(){
						hideQuickContents();
					}, 2000);
					showQuickContents();
				}, 200);
			}
		});
		if($(".popup").length){
			var url = "#TB_inline?height=300&width=300&inlineId=popupcontainer";
			tb_show("Product",url);
		}
	});
	
	$("#content.product .colorbox").click(function(){
		$("#content.product .colorbox").removeClass("active");
		$(this).addClass("active");
		$("input[name='color_id']").val($(this).attr("id"));
		switchColorXML($("input[name='id']").val(),$(this).attr("rel"),$(this).attr("ref"));
	});
	

// ------------------ Cart Quick Contents
	

	$("#header #cart .buttons").hoverIntent(function(){
		showQuickContents();
	}, function(){
		hideQuickContents();
	});


// ------------------ Cart
	
	//restrict input to numbers
	$('input.quantity').bind('keypress', function(e) { 
		return ( e.which !== 8 && e.which !== 0 && ( e.which < 48 || e.which > 57)) ? false : true ;
	});

// ------------------ News

	$("#content.news .menu ul li a img").hover(function(){
		//turn off all other buttons
		$("#content.news .menu ul li a img").each(function(){
			$(this).attr("src",$(this).attr("src").replace("on","off"));
		});
		
		//glow current button
		var img = $(this);
		img.attr("src",img.attr("src").replace("off","on"));
	
		//show current content
		$("#content.news .content .category").hide();
		$("#content.news .content ." + $(this).parent().attr("rel")).show();
	});
	
// ------------------ Contact

	$("#content.contact .input_button_primary").click(function(){
		
		$(".error").each(function(){
			$(this).empty();
		});

		var name = $("input.form-name").val();
		var email = $("input.form-email").val();
		var phone = $("input.form-phone").val();
		var message = $("textarea.form-message").val();
		var type = "";
		
		$(".type:checked").each(function(){
			if($(this).val() != ""){
				type += $(this).val() + ", ";
			}
		});
		type = type.substr(0, type.length - 2);

		
		var blnError = false;
		
		if(name == ""){
			$("#errName").text(" Please enter your name.");
			blnError = true;
		}
		if(email == ""){
			$("#errEmail").text(" Please enter your email address.");
			blnError = true;
		}
		if(phone == ""){
			$("#errPhone").text(" Please enter your phone number.");
			blnError = true;
		}
		if(message == ""){
			$("#errMessage").text(" Please enter a message.");
			blnError = true;
		}
		if(type == ""){
			$("#errType").text(" Please select a checkbox to describe yourself.");
			blnError = true;
		}
		
		if(blnError === false){
			$(".loader").show();
			$("#submit").attr("disabled","true");
			
			$.ajax({
				type: "POST",
				url: "_ajax_contact.php",
				data: ({
					name: name,
					email: email,
					message: message,
					phone: phone,
					type: type
				}),
				success: function(msg){		
					$("#content.contact .form").hide();
					$("#content.contact .thankyou").show();
				}
			});
		
		}
	});


/*	
	$('.cards').unbind().click(function() {
		if($('form.payment').is(':visible')) {
			$('form.payment').fadeOut();
		}else{
			$('form.payment').fadeIn();
		}
	}); 
*/	 

	
/*	
	$('input[name=process_payment], .submit-checkout').unbind().click(function(){

			var typesub = $(this).attr('rel');
			var custom = "";
			
			$(".customtag").each(function(){
				custom += "&" + $(this).attr("id") + "=" + $(this).val();
			});
			
			$(this).attr("disabled","true");
			$(this).attr("disabled","disabled");


			if($("input[name='shippingtype']:checked").val() == "fedex" && !$('#update_zipcode').val()){
				$('#cart-form').submit();
			} else {
				$.ajax({
					type: "POST",
					url: "_ajax_submit.php",
					
					data: "zip=" + $('#update_zipcode').val() + custom,
					
					success: function(msg){
					//	alert(msg);
					
					
						if(typesub == 'creditcard'){
							$("form#creditcardpayment").submit();							
						}
						if(typesub == 'amazon') {
						  $('form#amazon').submit();
						}
						if(typesub == 'paypal') {
						  $('form#paypal').submit();
						}
					} 
				}); 				
			}
		

		 	if(!$('#update_zipcode').val() && (typesub == 'amazon' || typesub == 'paypal')) { 
		 		$('#cart-form').submit();
		 	} else {	  
				$.ajax({
					type: "POST",
					url: "_ajax_submit.php",
					
					data: "zip=" + $('#update_zipcode').val() + custom,
					
					success: function(msg){
					//	alert(msg);
						if(typesub == 'creditcard'){
							$("form#creditcardpayment").submit();
						}
						if(typesub == 'amazon') {
						  $('form#amazon').submit();
						}
					} 
				}); 
			  }

		}); 
*/
	
		if($('.cartsummary input.input_small_button').length) { 
			var itemId = 0;
			$('.cartsummary input.input_small_button').unbind().click(function(){ 
				 var actionObj = $(this);
				 itemId = $(this).attr('ref');
				// alert(itemId);
				 $.ajax({
						type: "POST",
						url: "_ajax_remove.php",
						data: ({ sku: itemId }),
						success: function(msg){
						  window.location = self.location;
							/*
								if($(".customize." + itemId).length){
									$(".customize." + itemId).fadeOut(300, function(){
										$(".customize." + itemId).remove();
									});
								}							

								actionObj.parents('.item').fadeOut(300, function(){
									$(this).remove();
								});
							*/
						} 
					}); 
			});
		}

});



