var addthis_config = {
	ui_click: true
}

var search_elements = [];
var ff, ff_a;
var qt, sg, sgtmr, sgobj, curr_sind, sinds;

$(document).ready(function() {
	if($.browser.msie && $.browser.version.substr(0,1) == 6){
		document.write('To browse Stanton\'s Sheet Music web site, you need to use Internet Explorer version 7 or later.');
	} else {
		$('.slider a').mousedown(function() {
			$(this).toggleClass('in');
			$('.advanced').slideToggle('slow');
			$("#FlexPaperViewer").toggle();
			$(".flash-content").toggle();
			return false;
		}).click(function() {
			return false;
		});

		search_elements = $('.search INPUT[type=text]');

		search_elements.focusin(function() {
			if ($(this).val() == $(this).attr('title')) {
				$(this).val("").addClass('colorblack');
			}
		}).focusout(function() {
			if ($(this).val() == "") {
				$(this).val($(this).attr('title')).removeClass('colorblack');
			}
		});

		$("#search-form").submit(function(){
			search_elements.each(function(){
				if ($(this).val() == $(this).attr('title')) {
					$(this).val("");
				}
			});
		});
	}

	sg = $("#suggestions");
	ff = $("#search-form");
	ff_a = ff.attr("action");
	qt = $("#quick-title");

	$(".sections>ul>li").click(function(){
		$("ul", this).slideToggle();
	});

	$(".sections>ul>li>ul>li").hover(
		function(){ $(this).addClass("active"); },
		function(){ $(this).removeClass("active"); }
	);

	$(".listen").click(function(event){
		event.preventDefault();
		var playWind = window.open(this.href, 'play', 'width=700,height=570,toolbar=no');
		$(this).parent().click();
	});

	$(".clook").click(function(){
		window.open(this.href, 'closerLook1', 'status=yes,scrollbars=yes,resizable=yes,width=900,height=900');
		this.blur();
		return false;
	});

	$(".dd-print").click(function(){
		window.open(this.href, 'dd1', 'status=yes,scrollbars=yes,resizable=yes,width=1024,height=900');
		this.blur();
		return false;
	});

	$(".new-window").each(function(){
		var obj = $(this);
		obj.attr("target", "_blank").removeClass("new-window");
		return false;
	});

	$(".new-window").click(function(e){
		e.preventDefault();
		window.open(this.href);
	});

	$(".new-dwindow").click(function(){
		window.open(this.href, 'dw', 'status=yes,scrollbars=yes,resizable=yes,width=900,height=900');
		return false;
	});

	$(".addtocart").click(function(){
		$($(this).attr("href")).submit();
		this.blur();
		return false;
	});

	$(".call-printer").click(function(){
		print();
		return false;
	});

	if(!$('.product-descr-content').size()){
		$(".to-hide").hide();
	}

	$("#ajax-caller").click(function(){
		$("#ajax-loader").show();
	});

	$('<div id="window-fade"></div><div id="window-content-surr"><div id="window-content"><div class="window-header"><div class="window-header-content"><div class="close"><a href="#" class="close-window">Close</a></div><span></span></div></div><div class="window-body-border"><div class="window-body"></div></div></div></div>').appendTo("body");

	$(".get-window").live("click", function(){
		var title = $(this).attr("title");

		$.get($(this).attr("href"), {}, function(data){
			if(data){
				showWindow(title, data);
			}
		});
		return false;
	});

	$("#window-fade").click(function(){
		return closeWindow();
	});

	$(".call-close").click(function(){
		if(window.opener){
			close();
		}
		return false;
	});

	$(".feedback").click(function(){
		var title = 'Feedback Form';

		$.get($(this).attr("href"), {}, function(data){
			if(data){
				showWindowCallback(title, data, function(){
					$("#feedback-url").html(location.href);

					$("#window-content").delegate("#feedback-form", "submit", function(){
						$.post(this.action, $(this).serialize() + "&" + $.param({url:$("#feedback-url").html()}), function(data){
							$(".window-body").html(data);
							$("#feedback-url").html(location.href);
						});

						return false;
					});
				});
			}
		});

		this.blur();
		return false;
	});

	$(".to-sitemap").click(function(){
		$('#hbm-surr').css({"top": 10, "left":10}).show();
		return false;
	});

	$(".hbm-close").click(function(){
		$('#hbm-surr').hide();
		return false;
	});

	if(sg.size()){
		qt.keydown(function(e){
			if(13 == e.which || 9 == e.which){
				e.preventDefault();
			}
		});

		qt.keyup(function(e){
			if(this.value.length > 2 || $.inArray(e.which, [9,13,38,40]) > -1){
				switch(e.which){
					case 13:
						if(sinds && sinds.filter(":visible").size()){
							this.value = sinds.eq(curr_sind).find("a").text() || "";
							sg.slideUp();
						} else {
							$("#search-form").submit();
						}
						break;

					case 38:
						changeSuggestion(-1);
						break;

					case 9:
					case 40:
						changeSuggestion(+1);
						break;

					default:
						if(sgtmr){
							clearTimeout(sgtmr);
						}

						sgobj = this;
						sgtmr = setTimeout("getSuggestions()", 1000);
				}
			} else {
				sg.slideUp();
			}
		});

		sg.delegate("a", "click", function(e){
			e.preventDefault();

			var obj = $(this);
			qt.val(obj.text());

			sg.slideUp();
		});
	}
});

var createTooltip = function(){
	$("<div class='tooltip'></div>").appendTo("body");
}

var curr_testimonial = 0;
var tms;
var tms_count = 0;

var changeTestimonial = function(){
	tms.eq(curr_testimonial).fadeOut(600, function(){
		curr_testimonial++;
		if(curr_testimonial >= tms_count){
			curr_testimonial = 0;
		}

		tms.eq(curr_testimonial).fadeIn(600);
	});
}

var headers = ["Select Type, Enter Your Zip Code, Estimate Delivery", "Choose Either UPS or USPS for Your Method of Delivery", "This is Your Shipping Summary", "In Store Pickup Note – Please Review"];

var showWindow = function(title, data){
	showWindowCallback(title, data, null);
}

var showWindowCallback = function(title, data, func){
	$("#FlexPaperViewer").hide();
	$("#window-fade").css({width:$(document).width() + 'px', height:$(document).height() + 'px', opacity:0.6}).fadeIn('fast', function(){
		var ww = $(window).width();
		var wh = $(window).height();
		//var l = Math.floor(($(window).width()-840)/2);

		if(!title)
			title = "&nbsp;";

		var wh_obj = $(".window-header");
		wh_obj.find("span").html(title);

		$(".window-body").css("maxHeight", (wh-100) + "px").html(data);//.css({height: wh+'px'})
		$("#window-content-surr")/*.css({left: l + 'px'})*/.fadeIn('fast', function(){
			var obj = $(this);
			obj.animate({top: Math.floor((wh - obj.height())/2), left: Math.floor((ww - obj.width())/2)}, 500);
		});

		window_opened = true;

		$(".close-window").click(function(){
			return closeWindow();
		});

		if(typeof func == "function"){
			func();
		}

		if($("#ez-shipping")){
			initEZ();
		}
	});

	$(window).resize(function(){
		$("#window-fade").css({width:$(window).width() + 'px', height:$(document).height() + 'px'});

		var ww = $(window).width();
		var wh = $(window).height();
		//var l = ($(window).width()-840)/2;

		var wh_obj = $(".window-header");

		$(".window-body").css("maxHeight", (wh-100) + "px")//.html(data);.css({height: wh-30+'px'});

		var wh_obj = $("#window-content-surr");
		wh_obj.css({left: Math.floor((ww - wh_obj.width())/2) + 'px', top: Math.floor((wh - wh_obj.height())/2) + "px" });
	});
}

var closeWindow = function(){
	$("#window-content-surr").fadeOut('fast', function(){
		$(".window-header span").html('');
		$(".window-body").html('');

		$("#window-fade").fadeOut('fast', function(){
			$("#FlexPaperViewer").show();
		});
	});

	if(typeof refreshWindowByClose == 'function'){
		refreshWindowByClose();
	}

	return false;
}

var initEZ = function(){
	tms = $(".testimonials");
	tms_count = tms.size();

	tms.filter(":gt(0)").hide();

	setInterval("changeTestimonial()", 8000);

	createTooltip();

	$("label[title]").hover(
		function(e){
			var posx = 0;
			var posy = 0;
	
			if (!e) var e = window.event;
			if (e.pageX || e.pageY){
				posx = e.pageX;
				posy = e.pageY;
			}
			else if (e.clientX || e.clientY){
				posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
				posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
			}

			$(".tooltip").html($(this).attr("tooltip")).css({"top":posy+10+"px", "left":posx+20+"px"}).fadeIn("fast");

			return false;
		},
		function(){ $(".tooltip").fadeOut("fast"); }
	).each(function(){
		var obj = $(this);
		obj.attr("tooltip", obj.attr("title")).removeAttr("title");
	});

	$(".estimates input").click(function(){
		if($(this).attr("id") != 'estimate_13oz'){
			$("#shipping_method_2").attr("disabled", "disabled");
		} else {
			$("#shipping_method_2").removeAttr("disabled");
		}
	});

	$("#shipping_type_2").click(function(){
		if(this.checked){
			$(".hide-type-2").hide();
			$(".show-type-2").show();
			$(".shipping-methods input:radio, #multiple_shipments, #multiple_shipments_count, #dropship").attr("disabled", "disabled").removeAttr("checked");
			$(".shipping-methods .text").val('');
			$(".shipping-needed, .drop-ship").css("opacity", 0.2);
			$("#process-instructions").html(headers[3]);
		}

		this.blur();
	});

	$("#shipping_type_0, #shipping_type_1").click(function(){
		$(".hide-type-2").show();
		$(".show-type-2").hide();
		$(".shipping-methods input:radio, #multiple_shipments, #multiple_shipments_count, #dropship").removeAttr("disabled");
		$(".shipping-needed, .drop-ship").css("opacity", 1);

		this.blur();
	});

	$(".calculate").click(function(){
		var params = { shipcalc: 1, zip: $("#estimate_zip").val(), lb: $(".estimates input:checked").val() };
		if($("#shipping_type_1").get(0).checked){
			params.residential = 1;
		}

		$(".shipping-methods .text").val('');
		$("#process-instructions").html(headers[0]);

		$(".ajax-loader").show();
		
		$.getJSON($("#shipping-form").attr("action"), params, function(json){
			if(json.err){
				alert(json.err);
			} else {
				for(var i in json){
					$("#shipping_method_price_"+i).val(json[i] > 0 ? "$"+json[i] : "");
					$("#process-instructions").html(headers[1]);
				}

				location.href = '#postal-services';
			}

			$(".ajax-loader").hide();
		});

		return false;
	});

	$(".shipping-methods input:radio").click(function(){
		if(this.checked){
			$("#process-instructions").html(headers[2]);
			location.href = '#your-information';
		}
	});
}

var getSuggestions = function(){
	obj = $(sgobj);
	$.get(ff_a, {gc: obj.val()}, function(data){
		sg.html(data);
		
		curr_sind = -1;
		sinds = sg.find("li");

		//changeSuggestion(0);

		if(1 == sinds.size() && !obj.val()){
			obj.val(sinds.eq(0).find("a").text() || "");
		} else {
			if(data){
				sg.slideDown();
			} else {
				sg.slideUp();
			}
		}
	});
}

var changeSuggestion = function(step){
	curr_sind += step;

	if(curr_sind > sinds.size()-1){
		curr_sind = 0;
	} else if(curr_sind < 0){
		curr_sind = sinds.size()-1;
	}

	sinds.removeClass("active").eq(curr_sind).addClass("active");
	qt.val(sinds.eq(curr_sind).find("a").text() || "");
}

//var curvyCornersNoAutoScan = true;
/*
$(document).ready(function() {
	$(".round-box").each(function(sel) {
		curvyCorners({
	    tl: { radius: 6 },
	    tr: { radius: 6 },
	    bl: { radius: 6 },
	    br: { radius: 6 },
	    antiAlias: true
	  }, this);
	});
});
*/
