var total = 0;

$(document).ready(function(){
	
	String.prototype.trim = function() {
		return $.trim(this);
	}

	$(".news_input").focus(function() {
		if ($(this).val() == $(this)[0].title) {
			$(this).val("");
		}
	});

	$(".news_input").blur(function() {
		if ($(this).val() == "") {
			$(this).val($(this)[0].title);
		}
	});

	$(".news_input").blur();

	
	// script to remove product
	$('.remove_product').bind({
		click: function(){
			//			if($(this).attr("checked") == true) {
			//				$(this).parents('form').submit();
			//			}
			//			$("input[type=checkbox]").attr("checked",$(this).attr("checked"));

			var keys = "";
			$('input[type=checkbox]:checked').each( function() {
				if( keys != '' ) {
					keys += ",";
				}
				keys = keys + $(this).val() ;
			});
			if(keys != "") {
				$.ajax({
					type: "POST",
					url: window.location.toString(),
					data: "ajax=true&quantity=0&keys="+keys+"&wpsc_update_quantity=true",
					success: function(){
						window.location.href = window.location;
					}
				});
			} else {
				alert("Please Select Any Product");
			}
			return false;
		}
	});
		

	// script to change quantity
	$('.change_quantity').bind({
		change: function(){
			$this	=	$(this);
			$qty	=	$this.val();
			if(!isNaN($qty) && $qty >= 0) {
				$(this).parents('form').submit();
			}
		}
	});

	if($('#login_from_shopping_cart').length > 0) {
		$('#login_from_shopping_cart').bind({
			click: function(){
				if(document.getElementById('wpsc_checkout_forms').log123.value != "" && document.getElementById('wpsc_checkout_forms').pwd123.value != "") {
					document.getElementById('loginform').log.value		=	document.getElementById('wpsc_checkout_forms').log123.value;
					document.getElementById('loginform').pwd.value		=	document.getElementById('wpsc_checkout_forms').pwd123.value;
					document.getElementById('loginform').submit();
				}
			}
		});
	}
	if($(".widget-title").length > 0) {
		$('.widget-title').each(function(){
			if($(this).html().trim() == "Search") {
				$(this).addClass("widget_search");
			}
		});
	}

	if($("#searchsubmit").length > 0) {
		$('#searchsubmit').unbind('mouseover');
		$('#searchsubmit').unbind('mouseout');
		$('#searchsubmit').bind({
			mouseover: function(){
				$(this).addClass('search_button_hovered');
			},
			mouseout: function() {
				$(this).removeClass('search_button_hovered');
			}
		});
	}

	if($("#view_basket").length > 0) {
		$('#view_basket').unbind('mouseover');
		$('#view_basket').unbind('mouseout');
		$('#view_basket').bind({
			mouseover: function(){
				$(this).addClass('view_update2_hover');
			},
			mouseout: function() {
				$(this).removeClass('view_update2_hover');
			}
		});
	}

	$(".newbutton5").unbind('mouseover');
	$(".newbutton5").unbind('mouseout');
	$('.newbutton5').bind({
		mouseover: function(){
			$(this).addClass('newbutton5_hover');
		},
		mouseout: function() {
			$(this).removeClass('newbutton5_hover');
		}
	});

	$(".newbutton2").unbind('mouseover');
	$(".newbutton2").unbind('mouseout');
	$('.newbutton2').bind({
		mouseover: function(){
			$(this).addClass('newbutton2_hover');
		},
		mouseout: function() {
			$(this).removeClass('newbutton2_hover');
		}
	});

	$(".smohed_bottom").unbind('mouseover');
	$(".smohed_bottom").unbind('mouseout');
	$('.smohed_bottom').bind({
		mouseover: function(){
			$(this).addClass('smohed_bottom_hover');
		},
		mouseout: function() {
			$(this).removeClass('smohed_bottom_hover');
		}
	});

	$(".free_delive").unbind('mouseover');
	$(".free_delive").unbind('mouseout');
	$('.free_delive').bind({
		mouseover: function(){
			$(this).addClass('free_delive_hover');
			$(this).parents(".textwidget").find(".on_orders").addClass('on_orders_hover');
		},
		mouseout: function() {
			$(this).removeClass('free_delive_hover');
			$(this).parents(".textwidget").find(".on_orders").removeClass('on_orders_hover');
		}
	});

	$(".on_orders").unbind('mouseover');
	$(".on_orders").unbind('mouseout');
	$('.on_orders').bind({
		mouseover: function(){
			$(this).addClass('on_orders_hover');
			$(this).parents(".textwidget").find(".free_delive").addClass('free_delive_hover');
		},
		mouseout: function() {
			$(this).removeClass('on_orders_hover');
			$(this).parents(".textwidget").find(".free_delive").removeClass('free_delive_hover');
		}
	});


	$(".product-image").unbind('mouseover');
	$(".product-image").unbind('mouseout');
	$('.product-image').bind({
		mouseover: function(){
			$(this).parents(".shop_box201,.shop_2box201").find(".shop_bottom_c1").addClass('shop_bottom_c1_hover');
		},
		mouseout: function() {
			$(this).parents(".shop_box201,.shop_2box201").find(".shop_bottom_c1").removeClass('shop_bottom_c1_hover');
		}
	});

	$(".shop_bottom_c1").unbind('mouseover');
	$(".shop_bottom_c1").unbind('mouseout');
	$('.shop_bottom_c1').bind({
		mouseover: function(){
			$(this).addClass('shop_bottom_c1_hover');
		},
		mouseout: function() {
			$(this).removeClass('shop_bottom_c1_hover');
		}
	});

	$('form').preventDoubleSubmit();

});

$.fn.preventDoubleSubmit = function() {
	$(this).submit(function() {
		if (this.beenSubmitted)
			return false;
		else {
			this.beenSubmitted = true;
			$(this).find("input[type='image'],input[type='submit']").attr("disabled", true);
		}
	});
};

function sleep(){
	var startTime = new Date().getTime(); // get the current time
	while (new Date().getTime() < startTime + 500); // hog cpu
}
