/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // NOTE Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};


/************** DOCUMENT LOAD **************/

$(document).ready(function(){
	
	// preload images
    var cache = [];
    $.preLoadImages = function() {
        var args_len = arguments.length;
        for (var i = args_len; i--;) {
          var cacheImage = document.createElement('img');
          cacheImage.src = arguments[i];
          cache.push(cacheImage);
        }
    }
    jQuery.preLoadImages(
        "/assets/uswcom/images/template/minus.png",
        "/assets/uswcom/images/template/plus.png",
        "/assets/uswcom/images/template/loader.gif",
        "/assets/uswcom/images/template/pbgover.png",
        "/assets/uswcom/images/template/search.gif",
        "/assets/uswcom/images/template/searchover.gif",
        "/assets/uswcom/images/template/addcart.png",
        "/assets/uswcom/images/template/addcartover.png",
        "/assets/uswcom/images/template/update.png",
        "/assets/uswcom/images/template/updateover.png",
        "/assets/uswcom/images/template/checkout.png",
        "/assets/uswcom/images/template/checkoutover.png",
        "/assets/uswcom/images/template/continueover.png",
        "/assets/uswcom/images/template/shopover.png",
        "/assets/uswcom/images/template/loginover.png",
        "/assets/uswcom/images/template/cvvvisa.gif",
        "/assets/uswcom/images/template/cvvamex.gif",
        "/assets/uswcom/images/template/orderover.png",
        "/assets/uswcom/images/template/changeover.gif",
        "/assets/uswcom/images/template/returnover.gif",
        "/assets/uswcom/images/template/useover.png"
    );

	
	// search box
	$("#sbox").focus(function(){
		if ($(this).val() == 'Search our products . . .') $(this).val("");
	});
	$("#sbox").blur(function(){
		if ($(this).val() == '') $(this).val("Search our products . . .");
	});
	
	// search button
	$("#sbut").hover(function(){
		$(this).attr("src","/assets/uswcom/images/template/searchover.gif");
	}, function(){
		$(this).attr("src","/assets/uswcom/images/template/search.gif");
	});
	
	// set top navigation
	$("#nav a").hover(
		function(){
			$(this).css("background-color","#f1f7fc");
			$(this).css("color","#000000");
			var rel = $(this).attr("rel");
			$(rel).css("left",$(this).position().left-1);
			$(rel).show();
		},
		function() {
			$(this).css("background-color","#ffffff");
			$(this).css("color","#00487e");
			var rel = $(this).attr("rel");
			$(rel).hide();
		}
	);
	
	// nav box hover and nav highlight
	$(".navbox").hover(
		function(){
			var rel = $(".nball",this).attr("rel");
			$(rel).trigger("mouseenter");
		},
		function() {
			var rel = $(".nball",this).attr("rel");
			$(rel).trigger("mouseleave");
		}
	);
	
	// hide all
	$("#leftnav ul").hide();
	
	// show type
	if ($.cookie("pclicktype") == 'yes') autoexpand("#pclicktype","#prodstype");
	if ($.cookie("pclickbrand") == 'yes') autoexpand("#pclickbrand","#prodsbrand");
	if ($.cookie("pclickcondition") == 'yes') autoexpand("#pclickcondition","#prodscondition");
	if ($.cookie("pclickpart") == 'yes') autoexpand("#pclickpart","#prodspart");
	
	// check for any cookies
	if (!$.cookie("pclicktype") && !$.cookie("pclickbrand") && !$.cookie("pclickcondition") && !$.cookie("pclickpart")) autoexpand("#pclicktype","#prodstype");
	
	// rollover for add cart button
	$("#addcart").hover(function(){
		$(this).attr("src","/assets/uswcom/images/template/addcartover.png");
	}, function(){
		$(this).attr("src","/assets/uswcom/images/template/addcart.png");
	});
	
	// rollover for cart update
	$("#mcbupdate").hover(function(){
		$(this).attr("src","/assets/uswcom/images/template/updateover.png");
	}, function(){
		$(this).attr("src","/assets/uswcom/images/template/update.png");
	});
	
	// rollover for cart checkout
	$("#mcbcheckout").hover(function(){
		$(this).attr("src","/assets/uswcom/images/template/checkoutover.png");
	}, function(){
		$(this).attr("src","/assets/uswcom/images/template/checkout.png");
	});
	
	// set loader for add cart
	/*
	$("#addcart").click(function(){
	   setLoader($("#addcartbox"), 'Adding item to cart');
	   setTimeout(function(){
	       $("#prodform").submit();
	   }, 1000);
	});
	*/
	
	// rollover for checkout update
	$(".mcbupdate").hover(function(){
		$(this).attr("src","/assets/uswcom/images/template/updateover.png");
	}, function(){
		$(this).attr("src","/assets/uswcom/images/template/update.png");
	});
	
	// rollover for continue checkout
	$(".ccontinue").hover(function(){
		$(this).attr("src","/assets/uswcom/images/template/continueover.png");
	}, function(){
		$(this).attr("src","/assets/uswcom/images/template/continue.png");
	});
	
	// rollover for continue checkout
	$(".cshop").hover(function(){
		$(this).attr("src","/assets/uswcom/images/template/shopover.png");
	}, function(){
		$(this).attr("src","/assets/uswcom/images/template/shop.png");
	});
	
	// rollover for account login
	$(".clogin").hover(function(){
		$(this).attr("src","/assets/uswcom/images/template/loginover.png");
	}, function(){
		$(this).attr("src","/assets/uswcom/images/template/login.png");
	});
	
	// rollover return button
	$(".useship").hover(function(){
		$(this).attr("src","/assets/uswcom/images/template/useover.png");
	}, function(){
		$(this).attr("src","/assets/uswcom/images/template/use.png");
	});
	
	// rollover order button
	$(".orderbut").hover(function(){
		$(this).attr("src","/assets/uswcom/images/template/orderover.png");
	}, function(){
		$(this).attr("src","/assets/uswcom/images/template/order.png");
	});
	
	// rollover confirm change button
	$(".changebut").hover(function(){
		$(this).attr("src","/assets/uswcom/images/template/changeover.gif");
	}, function(){
		$(this).attr("src","/assets/uswcom/images/template/change.gif");
	});
	
	// rollover return button
	$("#thankreturn").hover(function(){
		$(this).attr("src","/assets/uswcom/images/template/returnover.gif");
	}, function(){
		$(this).attr("src","/assets/uswcom/images/template/return.gif");
	});
	
	// change button form post
	$(".changebut").click(function(){
	   $("#confirmchange").submit();
	});
	
	// change shipping form post
	$("#shiptype").change(function(){
	   $("#confirmship").submit();
	});
	
	// cart continue top button
	$("#cartcontbut").click(function(){
	   $("#cartform").submit();
	});
	
	// billing
    $("#billaddress").hide();
    $("#billswitch").click(function(){
    	$("#billaddress").slideToggle();
    });
    $("#creditcard").change(function(){
    	if ($(this).val() == "amex") {
    		$("#cvv").html('<img src="/assets/uswcom/images/template/cvvamex.gif" alt="" style="border:4px solid #ffffff" />');
    	} else {
    		$("#cvv").html('<img src="/assets/uswcom/images/template/cvvvisa.gif" alt="" style="border:4px solid #ffffff" />');
    	}
    });
	
});

/************** FUNCTIONS **************/

function autoexpand(parentid, id) {

	$(parentid).css('background', 'url("/assets/uswcom/images/template/minus.png") left no-repeat');
	$(id).show();

}

function subnav(parentid, id) {
	
	var pid = parentid.substr(1);
	
	if ($(id+":hidden").length) {
		$.cookie(pid, 'yes', { path: '/', expires: 10 });
		$(parentid).css('background', 'url("/assets/uswcom/images/template/minus.png") left no-repeat');
		$(id).slideDown();
	} else {
		$.cookie(pid, 'no', { path: '/', expires: 10 });
		$(parentid).css('background', 'url("/assets/uswcom/images/template/plus.png") left no-repeat');
		$(id).slideUp();
	}

}

function remCart(id) {

    $("#mc"+id).remove();

}

function setLoader(id, msg) {

    $(id).html('<table style="border:0;corder-collpase:collapse;margin:0;padding:0"><tr><td><img src="/assets/uswcom/images/template/loader.gif" alt="" /></td><td style="padding-left:10px;">'+msg+'</td></tr></table>');

}

function setState(sid, val) {

}

function popShip(fn, ln, com, st1, st2, city, st, zip) {

    $("#sfn").val(fn);
    $("#sln").val(ln);
    $("#scom").val(com);
    $("#sst1").val(st1);
    $("#sst2").val(st2);
    $("#scity").val(city);
    $("#shipstate option").each(function(i,selected) {
        if ($(this).val() == st) {
            $('#shipstate option:eq('+i+')').attr('selected', 'selected');
        }
    });
    $("#szip").val(zip);
    
    alert("Your shipping fields have been updated with the selected address.");

}

function cleanSearch() {

    // get string
    var sstr = $("#sbox").val();
    
    // replace slash
    $("#sbox").val(sstr.replace('/',' '));
    
    // return true
    return true;
    
}
