// attach events when DOM is ready
$(document).ready(function() {
	
	
	

	
/*	Color Configurator on product detail page
---------------------------------------------------------------	*/
	// swatch click event
	JQ("ul#colorSwatches a").click(function() {
 		
		
		// get the detail image src and extract the lot number
		var $path = JQ("img.large").attr("src");
		$path = $path.split('/');
		$imgSrc = $path[$path.length - 1];
		$lotNumber = $imgSrc.split("_");
		$lotNumber = $lotNumber[0];
		
		
		
		// get the color id of the swatch clicked
		var $colorId = this.id.split('c');
		$colorId = $colorId[1];
		


		var $colorName = JQ(this).children("img").attr("alt");


 	var re = new RegExp('^'+$colorName,"i");
		JQ("#dropdownimage").selectOptions(re);

		if (JQ("p.colorName").text() !== $colorName) {
			JQ(".productImage").fadeOut("normal", function() {
		
				JQ("img.large").attr("src", "assets/images/products/" + $lotNumber + "_" + $colorId + "_dt.jpg");
				JQ(".productImage a, p.viewLarger a").attr("href", "assets/images/products/" + $lotNumber + "_" + $colorId + "_lg.jpg");
				JQ("p.colorName").text($colorName);
				JQ(".productImage").fadeIn();
			});
			// set a cookie to maintain selected color during session
			JQ.cookie('shoeColor', $colorId);
		}
		return false;
	});
	
	// select menu change event
	JQ("#dropdownimage").change(function() {
		// get the detail image src and extract the lot number

		var $path = GetValue(document.add,"image_" + JQ(this).val());
		var $path2 = JQ("img.large").attr("src");

		$path = $path.split('/');
		$path2 = $path2.split('/');
		$imgSrc = $path[$path.length - 1];
		$imgSrc2 = $path2[$path2.length - 1];
		$lotNumber = $imgSrc2.split("_");
		$lotNumber = $lotNumber[0];


		$currsel=JQ(this).val().split(":::")
		$currname=$currsel[0];




// get the color id of the swatch clicked
		var $colorId = $imgSrc.split('.gif');
		$colorId = $colorId[0];
		


		var $colorName = JQ(this).children("option:selected").text();
		JQ(".productImage").fadeOut("normal", function() {
			JQ("img.large").attr("src", "/assets/images/products/" + $lotNumber + "_" + $colorId + "_dt.jpg");
			JQ(".productImage a, p.viewLarger a").attr("href", "/assets/images/products/" + $lotNumber + "_" + $colorId + "_lg.jpg");
			JQ("p.colorName").text($colorName);
			JQ(".productImage").fadeIn();
		});
		// set a cookie to maintain selected color during session
		JQ.cookie('shoeColor', $colorId);
	});
	
	// Larger product image overlay
	JQ(".productImage a, p.viewLarger a").click(function() {
		var $productName = JQ(".productImage img").attr("alt");
		var $colorName = JQ("select#ctl00_ContentPlaceHolder1_productView1_colorSelect option:selected").text();
		
		return false;
	});
	
	// adjust overlay opacity for FF 2.x mac
	//if (detectMacXFF2()) {
	//	$('body').addClass('mac-ff2');
	//}
	
	// add zebra table row class
	//$("table.zebra tbody tr:even").addClass("even");
	
	//$(".justify").vjustify();
	
	// fix the IE6 background flickr on links
	//try { document.execCommand("BackgroundImageCache", false, true); }
	//catch(err) {}
	
});

// page load events
$(window).load(function() {	
	// homepage audio controls
	
});



/*------------------------------------------
	Utility Functions
--------------------------------------------*/
// Homepage Music Controls
var flashMovie;
function initMusic() {
	if (document.getElementById) {
		flashMovie = document.getElementById("homeSWF");
		// see if music has been muted and 
		// if so, maintain during session
		var $music = $.cookie('music');
		if ($music === 'off') {
			$("p.audioControl a").trigger("click");
		}
	}
}
// stop music
function stopMusic() {
	if (flashMovie) {
		flashMovie.stopTheMusic();
		return false;
	}
}
// start music
function playMusic() {
	if (flashMovie) {
		flashMovie.startTheMusic();
		return false;
	}
}

//	Label Over : Apply label over input
jQuery.fn.labelOver = function(overClass) {
	return this.each(function() {
		var label = jQuery(this);
		var f = label.attr('for');
		if (f) {
			var input = jQuery('#' + f);
			this.hide = function() {
				label.css({ textIndent: -10000 })
			}
			this.show = function() {
				if (input.val() == '') label.css({ textIndent: 0 })
			}
			// handlers
			input.focus(this.hide);
			input.blur(this.show);
			label.addClass(overClass).click(function(){ input.focus() });
			if (input.val() != '') this.hide(); 
		}
	});
}

//	Justify Element Heights
jQuery.fn.vjustify=function() {
	var maxHeight=0;
	this.each(function() {
		if (this.offsetHeight>maxHeight) { maxHeight=this.offsetHeight; }
	});
	this.each(function() {
		$(this).height(maxHeight + "px");
		if (this.offsetHeight>maxHeight) {
			$(this).height((maxHeight-(this.offsetHeight-maxHeight))+"px");
		}
    });
};

// Isolate Firefox 2.x for mac because of overlay opacity issue
function detectMacXFF2() {
	var userAgent = navigator.userAgent.toLowerCase();
	if (/firefox[\/\s](\d+\.\d+)/.test(userAgent)) {
		var ffversion = new Number(RegExp.$1);
		if (ffversion < 3 && userAgent.indexOf('mac') != -1) {
			return true;
		}
	}
}

/**
 * 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.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
		}
		// CAUTION: 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;
	}
};