// Main JavaScript holder for various functions used throughout the site
jQuery.noConflict();


// function to enable conversion of obfuscated email adddresses
jQuery.fn.defuscate = function(settings) {
	settings = jQuery.extend({link: true}, settings);
	regex = /\b([A-Z0-9._%-]+)\([^)]+\)((?:[A-Z0-9-]+\.)+[A-Z]{2,6})\b/gi;
	mailto = '<a href="mailto:$1@$2">$1@$2</a>';
	plain = "$1@$2";
	return this.each(function() {
		defuscated = jQuery(this).html().replace(regex, settings.link ? mailto : plain)
		jQuery(this).html(defuscated);
	});
};


// Selects an option by value
// Mathias Bank (http://www.mathias-bank.de), original function
// Sam Collett (http://www.texotela.co.uk), addition of regular expression matching
// @param    String|RegExp value   Which options should be selected can be a string or regular expression
// @param    Boolean clear         Clear existing selected options, default false
// @example  jQuery("#myselect").selectOptions("val1"); // with the value 'val1'
// @example  jQuery("#myselect").selectOptions(/^val/i); // with the value starting with 'val', case insensitive
jQuery.fn.selectOptions = function(value, clear) {
	var v = value;
	var vT = typeof(value);
	var c = clear || false;
	// has to be a string or regular expression (object in IE, function in Firefox)
	if(vT != "string" && vT != "function" && vT != "object") return this;
	this.each(function() {
		if(this.nodeName.toLowerCase() != "select") return this;
		// get options
		var o = this.options;
		// get number of options
		var oL = o.length;
		for(var i = 0; i<oL; i++) {
			if(v.constructor == RegExp) {
				if(o[i].value.match(v)) {
					o[i].selected = true;
				}
				else if(c) {
					o[i].selected = false;
				}
			} else {
				if(o[i].value == v) {
					o[i].selected = true;
				}
				else if(c) {
					o[i].selected = false;
				}
			}
		}
	});
	return this;
};


function productImgSwap(target_link, swap_img) {
	if (jQuery("a[id='" + target_link + "']").attr("rel") == "original") {
		var large_img = swap_img.replace(/MED\//, "");
	} else if (jQuery("a[id='" + target_link + "']").attr("rel") == "large") {
		var large_img = swap_img.replace(/MED/, "LRG");
	} else {
		var large_img = "images/no_picture_LRG.gif";
	}
	jQuery("#product-image a").attr("href", large_img);
	jQuery("#product-image a img").attr("src", swap_img);
}


function additionalImgSwap(large_img, swap_img) {
	jQuery("#product-image a").attr("href", large_img);
	jQuery("#product-image a img").attr("src", swap_img);
}


function attribute_url_mod() {
	var current_url = document.location.toString();
	var colorname_start = current_url.lastIndexOf("#");
	if (colorname_start != -1) {
		current_url = current_url.substr(0,colorname_start);
	}
	return current_url;
}


jQuery(document).ready(function() {

	// header search
	jQuery("#header-search input.text").attr("value","Search");
	jQuery("#header-search input.text").focus(function() {
		if (jQuery(this).attr("value") == "Search") 
			jQuery(this).attr("value","");
		jQuery("#header-search form").addClass("focus");
	});
	jQuery("#header-search input.text").blur(function() {
		if (jQuery(this).attr("value") == "") 
			jQuery(this).attr("value","Search");
		jQuery("#header-search form").removeClass("focus");
	});

	// converts obfuscated email addresses into clickable mailto links
	jQuery("span.cloakemail").defuscate();

	// fades out green "success" messages
	jQuery(".messageStackSuccess").animate({opacity: 1.0}, 2000).fadeOut('slow');

	// better visual cue for which address book entry is chosen on checkout pages
	jQuery("#checkout-addresses div.entry input").focus(function() {
		jQuery("#checkout-addresses div.entry").removeClass("selected");
		jQuery(this).parent("div.entry").addClass("selected");
	});
	
	// shipping estimator
	if (jQuery("body").attr("id") == "popup-shipping-estimator") {
		jQuery("p.state-input").css({"display":"none"});
		jQuery("p.zip-input").css({"display":"none"});
		jQuery("a.button").css({"display":"none"});
		jQuery("p.foreign-shipping").css({"display":"none"});
		jQuery("select#country").change(function() {
			if ( this.value != '223' ) {
				jQuery("p.state-input").css({"display":"block"});
				jQuery("p.zip-input").css({"display":"block"});
				jQuery("a.button").css({"display":"block"});
				jQuery("p.domestic-shipping").css({"display":"none"});
				jQuery("p.foreign-shipping").css({"display":"block"});
			}
			else {
				jQuery("p.state-input").css({"display":"none"});
				jQuery("p.zip-input").css({"display":"none"});
				jQuery("a.button").css({"display":"none"});
				jQuery("p.domestic-shipping").css({"display":"block"});
				jQuery("p.foreign-shipping").css({"display":"none"});
			}
		});
	}

	// mouse over additional image changes main image
	jQuery("#additional-images ul li a").hover(
		function () {
			var large_img = jQuery(this).attr("href");
			var swap_img = jQuery(this).attr("medimg");
			additionalImgSwap(large_img, swap_img);
			return false;
		},
		function () {
		}
	);

	// mouse over color swatch changes main image
	jQuery("#attrib-images a.attribImg").hover(
		function () {
			var attribImg_id = jQuery(this).attr("id");
			var attrib_value = attribImg_id.replace(/attribvalue-/, "");
			var swap_img = jQuery(this).attr("href");
			productImgSwap(attribImg_id, swap_img);
			return false;
		},
		function () {
			var attribImg_id = jQuery("#attrib-images a.selected").attr("id");
			if (attribImg_id != null) {
				var attrib_value = attribImg_id.replace(/attribvalue-/, "");
				var swap_img = jQuery("#attrib-images a.selected").attr("href");
				productImgSwap(attribImg_id, swap_img);
			}
			return false;
		}
	);

	// clicking on color swatch automatically selects attribute and swaps main image
	jQuery("#attrib-images a.attribImg").click(function () {
		jQuery("#attrib-images a.attribImg").removeClass("selected");
		jQuery(this).addClass("selected");
		var attribImg_id = jQuery(this).attr("id");
		var attrib_value = attribImg_id.replace(/attribvalue-/, "");
		var swap_img = jQuery(this).attr("href");
		productImgSwap(attribImg_id, swap_img);
		jQuery("#attrib-1").selectOptions(attrib_value,true);
		jQuery("#attrib-1").trigger("focus");

		var current_url = attribute_url_mod();
		document.location = current_url + "#" + jQuery(this).attr("title");

		return false;
	});

	// clicking on color in drop down menu swaps main image and selects color swatch
	jQuery("select#attrib-1").change(function () {
		jQuery("#attrib-images a.attribImg").removeClass("selected");
		var attribImg_id = jQuery(this).attr("value");
		var attrib_value = "attribvalue-" + attribImg_id;
		var swap_img = jQuery("a[id='" + attrib_value + "']").attr("href");
		jQuery("a[id='" + attrib_value + "']").addClass("selected");
		productImgSwap(attrib_value, swap_img);

		var current_url = attribute_url_mod();
		document.location = current_url + "#" + jQuery("a[id='" + attrib_value + "']").attr("title");

		return false;
	});

	// if single color radio, color swatch should be automatically selected
	if ( jQuery("input.single-color:checked").length == 1 ) {
		jQuery("#attrib-images a.attribImg").removeClass("selected");
		var attribImg_id = jQuery("input.single-color").attr("value");
		var attrib_value = "attribvalue-" + attribImg_id;
		var swap_img = jQuery("a[id='" + attrib_value + "']").attr("href");
		jQuery("a[id='" + attrib_value + "']").addClass("selected");
		productImgSwap(attrib_value, swap_img);
	}

	// loads color attribute image based on URL
	var current_url = document.location.toString();
	var colorname_start = current_url.lastIndexOf("#") + 1;
	var url_attribute_name = current_url.substr(colorname_start);
	jQuery("#attrib-images").find("a.attribImg[title='" + url_attribute_name + "']").trigger("click");

	// date picker for product registration form
	jQuery('#product-reg-date').datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'yy-mm-dd',
		maxDate: '=+0m +0w +0y'
	});

	// registered products interface
	jQuery('table#registered-products-list div.details').hide();
	jQuery('table#registered-products-list tr.row table').click(function () {
		jQuery(this).next('.details').slideToggle();
	});

	// temporary for dev purposes
	//jQuery("#cc-cc-number").attr("value", "4111111111111111");
	//jQuery("#cc-cc-cvv").attr("value", "123");
	//jQuery("#conditions").attr("checked", "checked");

});
