function confirmation($name)
{
	return confirm("Are you sure you want to remove \""+$name+"\"?");
}

function preload(arrayOfImages) {
    $(arrayOfImages).each(function(){
        $('<img/>')[0].src = this;
    });
}

function toggleThumb(o) {
	$("#thumbs a").each(function(){
		$(this).removeClass('selected');
	});
	$(o).addClass('selected');
}

function setCharAt(str,index,chr) {
	if(index > str.length-1) return str;
	return str.substr(0,index) + chr + str.substr(index+1);
}

function rand (n){
  return ( Math.floor ( Math.random ( ) * n + 1 ) + 200 );
}

function add2Cart()
{
	var url = '';
	$("table.sku-tbl input").each(function(){
		if ($(this).attr("type") == "text" && $(this).val() > 0){
			url += "&" + $(this).attr("name") + "=" + $(this).val();
		}
	});
	if (url.length == 0){
		$("table.sku-tbl input").each(function(){
			if ($(this).attr("type") == "text"){
				$(this).select();
			}
		});
		return false;
	}
	else{
		return true;
	}
}

function addPo()
{
	var order_id  = $("#order_id").val();
	var po_number = $('#po_number').val();
	
	if (po_number == ''){
		$('#po_number').select();
		return false;
	}
	$("#indicator-progress").show();
	$.post("/ajax/order.php", {'method' : 'add_po', 'order_id' : order_id, 'po_number': po_number}, function(data){
		var inner_html = data + ' <a href="javascript:changePo()" style="font-size:11px;">(change)</a>';
		$("#po_number_text").html(inner_html);
		cancelAddPo();
		$("#indicator-progress").hide();
		$("#indicator-ok").show().delay(1000).fadeOut(1000);
   });
}
function changePo()
{
	$("#po_number").show();
	$("#add_po").show();
	$("#cancel_add_po").show();
	$("#po_number_text").hide();
}
function cancelAddPo()
{
	if ('' != $("#po_number_text").html()){
		$("#po_number").hide();
		$("#add_po").hide();
		$("#cancel_add_po").hide();
	}	
	$("#po_number_text").show();
}

function authorize()
{
	$("#authorize-form span.error").each(function(){ 
		$(this).hide(); 
	});
	
	$("#loading").show();
	$.post("/ajax/order.php", $("#authorize-form").serialize(), function(response){
		$("#loading").hide();
		switch(response){
			case 'amount_error':
				$("#amount_error").fadeIn();
				break;
			case 'card_not_accepted_error':
				$("#card_not_accepted_error").fadeIn();
				break;	
			case 'code_error':
				$("#code_error").fadeIn();
				break;
			case 'expiration_error':
				$("#expiration_error").fadeIn();
				break;
			case 'gateway_error':
				$("#gateway_error").fadeIn();
				break;
			case 'number_error':
				$("#number_error").fadeIn();
				break;
			case 'transaction_error':
				$("#transaction_error").fadeIn();
				break;			
			case 'address_error':
				$("#address_error").fadeIn();
				break;
			case 'zip_error':
				$("#zip_error").fadeIn();
				break;	
			default:
				window.location.href = response;
				break;
		}
   });
}

function webtopay() {
	
	$("#webtopay-data-form span.error").each(function(){ 
		$(this).hide(); 
	});
	$("#loading").show();
	$.post("/ajax/order.php", $("#webtopay-data-form").serialize(), function(response){
		$("#loading").hide();
		switch(response){
			case 'p_firstname_error':
				$("#p_firstname_error").fadeIn();
				break;
			case 'p_lastname_error':
				$("#p_lastname_error").fadeIn();
				break;	
			case 'p_street_error':
				$("#p_street_error").fadeIn();
				break;
			case 'p_city_error':
				$("#p_city_error").fadeIn();
				break;
			case 'p_zip_error':
				$("#p_zip_error").fadeIn();
				break;
			default:
				$("#webtopay-request-form").html(response).submit();
				break;
		}
   });

}


$(document).ready(function() {
	
	$("#cart-mini").mouseover(function(){
		var pathname = window.location.pathname;
		if (pathname != '/cart'){
			$('#cart-popup').show('fast');
		}		
	});
	$("#cart-pop-close").click(function(){
		if ($('#cart-popup').is(':visible')){
			$('#cart-popup').hide('fast');
		}		
	});
	$("#cart-popup").mouseleave(function(){
		$('#cart-popup').hide('fast');
	});
	
	$("#search-mini").click(function(){
		$('#search-box').css("display", 'block');
		$('#search').focus();
	});
	$("#search-box a.close-btn").click(function(){
		$('#search-box').hide('fast');
	});
	
	$("#s").click(function(){
		$('#search-form').submit();
	});
	
	$("#thumbs a").click(function(event) {
		//event.preventDefault();
		toggleThumb(this);
	});
	
	$("#turn-imgs").click(function(){
		var img_no   = ($("#turn-imgs-status").val() == '1') ? 2 : 1;
		var btn_text = (img_no == 1) ? "VIEW BACK IMAGES" : "VIEW FRONT IMAGES";
		
		$("#turn-imgs-status").val(img_no);
		$("#turn-imgs").val(btn_text);
		
		$("#item-grid-ctr img").each(function(){
			var img_no_pos = $(this).attr("src").length - 13;
			$(this).fadeOut(rand(800), function() {
				$(this).attr("src", setCharAt($(this).attr("src"), img_no_pos, img_no));
				$(this).fadeIn(rand(800));
			});
		});
		
	});
	
	$("#add-2-cart").mousedown(function(){
		$(this).css("background-image", "url(/img/add_to_cart_bg2.gif)");
	});
	$("#add-2-cart").mouseup(function(){
		$(this).css("background-image", "url(/img/add_to_cart_bg.gif)");
	});
	$("#add-2-cart").click(function(){
		return add2Cart();
	});
	
	$("#added-2-cart").delay(5000).fadeOut('slow');
	
	$("#cart-qtys-updated").delay(4000).fadeOut('slow');
	
	$("#contact-form input#name").focus(function(){
		$(this).removeClass("inp-default");
		if (this.value == 'Your name'){  
			this.value = '';
		}
		else{
			this.select();
		}
	});
	
	$("#contact-form input#name").blur(function(){
		if (this.value == ''){  
			this.value = 'Your name';
			$(this).addClass('inp-default');
		}
	});
	
	
	$("#contact-form input#email").focus(function(){
		$(this).removeClass("inp-default");
		if (this.value == 'Your email address'){  
			this.value = '';
		}
		else{
			this.select();
		}
	});
	
	$("#contact-form input#email").blur(function(){
		if (this.value == ''){  
			this.value = 'Your email address';
			$(this).addClass('inp-default');
		}
	});
	
	$("#contact-form textarea#message").focus(function(){
		$(this).removeClass("inp-default");
		if (this.value == 'Your message'){  
			this.value = '';
		}
		else{
			this.select();
		}
	});
	
	$("#contact-form textarea#message").blur(function(){
		if ($.trim(this.value) == ''){  
			this.value = 'Your message';
			$(this).addClass('inp-default');
		}
	});
	
	$('a.out').live('click', function(){
		var dmn = this.href.split(/\/+/g)[1];
		try {
			_gaq.push(['_trackEvent', 'Links', 'Click', dmn]);
		}catch(err){}
		window.open(this.href);
		return false;
	});
	
	$('.track-btn').click(function(){
		var btn_name = $(this).attr('name');
		try {
			_gaq.push(['_trackEvent', 'Buttons', 'Click', btn_name]);
		}catch(err){}
	});
	

});
