var MP3Player = null;

function setPlayDone() {
	$("a[id^='PLAYER']").removeClass('stop-button').addClass('play-button');
	$(".product_table_select").removeAttr("disabled");	
}	

$(document).ready(function() {
	// workaround for right column player implementation
	var right_col = $("#wrapper_inner").find("#right_col").length;
	// load los MP3 player
	if($.flash.available) {
		$("#myFlash").height(1);
		$("#myFlash").width(1);
	}
	MP3Player = $("#myFlash");
	MP3Player.flash(
		{
			swf: "/images/hs2/swf/MP3player.swf",
			height: 1,
			width: 1,
			allowScriptAccess: "always"
		}
	);
	// populate correct product SKU for MP3
	$("a[id^='PLAYER']").each(function () {
		if(right_col){
			var multispeed = $(this).parent().next().children(".form_col").find("select").val();
		} else {
			var multispeed = $(this).parent().siblings(".form_col").find("select").val();
		}
		if(multispeed){
			$(this).attr("id", "PLAYER_" + multispeed);	
			if(right_col) {
				$(this).parent().next().children(".form_col").find("input[name='code']").val(multispeed);				
			} else {
				$(this).parent().siblings(".form_col").find("input[name='code']").val(multispeed);				
			}
		}
	});
	// handle tempo change 
	$(".product_table_select").change(function () {
		if(right_col){
			$(this).closest("tr").children(".play-button_col").children("a.play-button").attr("id", "PLAYER_" + $(this).val());
			$(this).siblings("input[name='code']").val($(this).val());
		} else {
			$(this).parent().parent().siblings().first().children("a.play-button").attr("id", "PLAYER_" + $(this).val());
			$(this).siblings("input[name='code']").val($(this).val());
		}
	});
	// handle los click
	$("a[id^='PLAYER']").click(function(evt) {
		evt.preventDefault();
		// first off, stop everybody from playing
		MP3Player.flash(function(){
			this.hsStop();
		});
		// enable select if stopping
		$(".product_table_select").removeAttr("disabled");
		if($(this).hasClass("stop-button")) {
			// track the click with Google Analytics
			_gaq.push(['_trackEvent', 'MP3 Player', 'Stop', $(this).attr("id").substring(7)]);
			
			$(this).toggleClass('stop-button');
			$(this).toggleClass('play-button');				
		} else {
			// track the click with Google Analytics
			_gaq.push(['_trackEvent', 'MP3 Player', 'Play', $(this).attr("id").substring(7)]);
			
			// disable select if playing
			if(right_col){
				var multispeed = $(this).parent().next().children(".form_col").find("select");
			} else {
				var multispeed = $(this).parent().siblings(".form_col").find("select");
			}		
			if(multispeed.length){
				multispeed.attr("disabled", "disabled")
			}
			// then make everybody a "play" icon
			$("a[id^='PLAYER']").removeClass('stop-button').addClass('play-button');
			var sku = $(this).attr("id").substring(7);
			$(this).toggleClass('stop-button');
			$(this).toggleClass('play-button');
			MP3Player.flash(function(){
				this.hsPlay(sku);
			});
		}

	});

	/*
		not MP3 oriented but didn't want to create a new JS file
	*/

	$("a.get_widget").click(function(evt) {
		evt.preventDefault();
		$(this).siblings("div").toggle(200);
		$(this).siblings("div").children("textarea").select();
	});
	
	$("#widgets tr td div.widget_code div textarea").click(function() {
		$(this).select();
	});

	$("a.get_widget_close").click(function(evt) {
		evt.preventDefault();
		$(this).parent().toggle(200);
	});
	$("form.add_to_cart").submit(function() {
		_gaq.push(['_trackEvent', 'Cart Actions', 'Add to Cart', $(this).find('[name="sku"]').val()]);
	});
	$("form.remove_from_cart").submit(function() {
		_gaq.push(['_trackEvent', 'Cart Actions', 'Remove from Cart', $(this).find('[name="sku"]').val()]);
	});
	// ADD GA event tracking to buy button clicks 
	$("input.product_controls").attr("onclick", "_gaq.push(['_trackEvent', 'Buy Button Click', 'Product']);");	
	// ADD GA Event Tracking to inline CTA
	$("p.cta-inline a").attr("onclick", "_gaq.push(['_trackEvent', 'Link Click', 'Inline CTA', $(this).attr('href')]);");
	// ADD GA Event Tracking to checkout clicks
	$("a.checkout-link").attr("onclick", "_gaq.push(['_trackEvent', 'Cart Actions', 'Checkout', $(this).attr('class').split(' ')[1]]);");	
	// ADD GA Event Tracking to outbound links
	$('a').filter(function() {
		var theHref = this;
		if (theHref.hostname && theHref.hostname !== location.hostname) {
			if(!$(theHref).hasClass('checkout-link')){
				$(theHref).attr('target','_blank').bind('click keypress', function(event) {
					var code=event.charCode || event.keyCode;
					if (!code || (code && code == 13)) {
						_gaq.push(['_trackEvent', 'Link Click', 'Outbound', this.href]);
					};
				});
			}
		};
	});

});

