// jQuery was conflicting with the prototype library
// had to use jQuery noconflict mode http://docs.jquery.com/Using_jQuery_with_Other_Libraries
jQuery.noConflict();

var formLabelDefault = {
	set:function(obj,text) {
		jQuery(obj).val(text);
		jQuery(obj).focus(function() {
			jQuery(this).addClass("active");
			if (!this.changed) {
				jQuery(this).val("");
			}
		}).blur(function() {
			jQuery(this).removeClass("active");
			if (( this.changed && !jQuery.trim(jQuery(this).val()) ) || !this.changed) {
				this.changed = false;
				jQuery(this).val(text);
			}
		}).keypress(function() {
			this.changed = true;
		});
	}
};

var searchSite = {
	init:function() {
		formLabelDefault.set("#searchSite input[name=\"keywords\"]","search...");
	}
};
searchSite.init();

var featuredProducts = {
	init:function() {
		var btn = jQuery("#featured-products form input[name=\"add-to-cart\"]");
		if (!btn.length) {
			return false;
		}
		
		btn.replaceWith("<a class=\"btn small\" href=\"\">ADD TO CART</a>");
		
		jQuery("#featured-products form .btn").click(function() {
			jQuery(this).parents("form").submit();
			return false;
		});
	}
};
featuredProducts.init();

var currencySwitcher = {
	init:function() {
		var currency =  jQuery("#currency-switch [name='currency']");
		if (!currency.length) {
			return false;
		}
		// hide the submit button
		jQuery("#currency-switch input[type='submit']").hide();
		// replace the select with the HTML list
		currency.replaceWith(this.buildDropdownHTML());
		jQuery("#currency-switch .current").click(this.showCurrencyList);
		jQuery("#currency-switch ul li").click(this.changeCurrency);
	},
	buildDropdownHTML:function() {
		
		var selected = jQuery("#currency-switch :selected");

		var html = "<a class=\"current\" href=\"\">"+selected.text()+"</a>";
		
		html += "<ul>";
		jQuery("#currency-switch [name='currency'] option:not(:selected)").each(function() {
			html += "<li>"+
				"<a href=\"\">"+jQuery(this).html()+"</a>"+
				"<input type=\"hidden\" name=\"currency\" value=\""+jQuery(this).val()+"\" />"+
				"</li>";
		});
		html += "</ul>";
		
		return html;
	},
	showCurrencyList:function() {
		jQuery("#currency-switch ul").toggle();
		return false;
	},
	changeCurrency:function() {
		var currency = jQuery(this).find(":hidden").val();
		
		jQuery("#currency-switch [name='currency']").remove();
		jQuery("#currency-switch").append("<input type=\"hidden\" name=\"currency\" value=\""+currency+"\" />");

		jQuery("#currency-switch").submit();
		
		return false;
	}
}
currencySwitcher.init();
