$(
	function() {
		$.blockUI.defaults.message = '<img src="/images/ajax-loader.gif" alt="">';
		$.blockUI.defaults.baseZ = 5000;
		$.blockUI.defaults.css.top = ($(window).height() - 32) / 2 + 'px';
		$.blockUI.defaults.css.left = ($(window).width() - 32) / 2 + 'px';
		$.blockUI.defaults.css.width = '32px';
		$('a[name=modal]').click(function(e) {
			// Cancel the link behavior
			e.preventDefault();
			
			// Get the A tag
			var id = $(this).attr('href');
		
			// Get the screen height and width
			var maskHeight = $(document).height();
			var maskWidth = $(window).width();
		
			// Set height and width to mask to fill up the whole screen
			$('#mask').css({'width':maskWidth,'height':maskHeight});
			
			// transition effect
			$('#mask').fadeIn(1000);	
			$('#mask').fadeTo("slow",0.6);	
		
			// Get the window height and width
			var winH = $(window).height();
			var winW = $(window).width();
	              
			// Set the popup window to center
			$(id).css('top',  winH/2-$(id).height()/2);
			$(id).css('left', winW/2-$(id).width()/2);
		
			// transition effect
			$(id).fadeIn(2000); 
		});

		$('.window .close').click(function (e) {
			// Cancel the link behavior
			e.preventDefault();
			
			$('#mask').hide();
			$('.window').hide();
		});		

		// if mask is clicked
		$('#mask').click(function () {
			$(this).hide();
			$('.window').hide();
		});			

		$('#s3').cycle({ 
			fx:    'fade', 
			speed:  2500,
			timeout:  9000 
		});

		// below for header_searchform
		$("#hsf_plinecode").dropdownchecklist({ firstItemChecksAll: true }); 
		// above for header_searchform
		// below for adv_searchform
		$("#hsf_plinecode2").dropdownchecklist({ firstItemChecksAll: true }); 
		// above for adv_searchform

	    $('#mycarousel').jcarousel({
	        auto: 5,
	        wrap: 'last',
			animation: 1500,
	        initCallback: mycarousel_initCallback
	    });
	    $("#example").tabs();
	    $("#comm").tabs({ fx: { opacity: 'toggle' } });
		$('.tooltip').cluetip(
			{ 
				splitTitle: '|', 
				arrows: true, 
				dropShadow: false,
				width:280,
				positionBy: 'bottomTop',
				cluetipClass: 'jtip'
			}
		);
		$('.tooltip_pap').cluetip(
			{
		  		splitTitle: '|', 
		  		arrows: true, 
		  		dropShadow: false,
				width: 350,
				positionBy: 'bottomTop', 
		  		cluetipClass: 'jtip'
		  	}
		);
		$("#cartSummaryDiv").load("/sheet-music/cartSummary.jsp", { r: r() });
		$(".orderForm").ajaxForm(
			{
				beforeSubmit: function () {
					$.blockUI();
				},
				live: true,
				data: { ajax: "Y", r: r() },
				clearForm: true,
				success: function (responseText) {
					$.unblockUI();
					$("#applet1").hide();
					$("#cartSummaryDiv").load("/sheet-music/cartSummary.jsp", { r: r() });
					$("<div id=\"minicart\" />").appendTo("body");
					$("#minicart").html(responseText).dialog(
						{
							height: 600,
							modal: true,
							overlay: { opacity: 0.7, background: "black" },
							position: "center",
							title: "",
							width: 600,
							zindex: 1000,
							close: function () {
								$("#minicart").dialog("destroy").remove();
								$("#applet1").show();
							}
						}
					);
					$("#miniCartSummary").load("/sheet-music/cartSummary.jsp", { r: r() });
					$(".continueShoppingLink").click(
						function () {
							$("#minicart").dialog("destroy").remove();
							$("#applet1").show();
							return false;
						}
					);
				}
			}
		);
		$(".orderLink").live('click',
			function () {
				$.blockUI();
				$("#applet1").hide();
				$("<div id=\"minicart\" />").appendTo("body");
				$("#minicart").load($(this).attr("href") + "&ajax=Y&r=" + r(),
					function () {
						$.unblockUI();
						$("#cartSummaryDiv").load("/sheet-music/cartSummary.jsp", { r: r() });
						$("#miniCartSummary").load("/sheet-music/cartSummary.jsp", { r: r() });
						$(".continueShoppingLink").click(
							function () {
								$("#minicart").dialog("destroy").remove();
								$("#applet1").show();
								return false;
							}
						);
					}
				).dialog(
					{
						height: 600,
						modal: true,
						overlay: { opacity: 0.7, background: "black" },
						position: "center",
						title: "",
						width: 600,
						zindex: 1000,
						close: function () {
							$("#minicart").dialog("destroy").remove();
							$("#applet1").show();
						}
					}
				);
				return false;
			}
		);
		$(".advsearchlink").click(
			function () {
				$('<div id="advsearchArea" />').appendTo('body');
				$('#advsearchArea').load("/sheet-music/advsearch.jsp", {  r: r() }).dialog(
					{
						draggable: false,
						height: 375,
						modal: true,
						overlay: { opacity: 0.7, background: "black" },
						position: ['left','top'],
						title: "Advanced Search",
						close: function () {
							$("#advsearchArea").dialog("destroy").remove();
						},
						width: 700
					}
				);
				return false;
			}
		);
		$(".code_lookup_link").click(
			function () {
				$("<div id=\"codeLookupArea\" />").appendTo("body");
				$("#codeLookupArea").load("/sheet-music/accounts/rewards_code_lookup.jsp", function () {
						$("#code_lookup_form").ajaxForm(
							{
								target: "#code_lookup_results"
							}
						);
					}
				).dialog(
					{
						dialogClass: "flora",
						height: 300,
						modal: true,
						overlay: { opacity: 0.7, background: "black" },
						show: "slide",
						position: "center",
						title: "Crescendo Rewards Code Lookup",
						zIndex: 5000,
						width: 550,
						close: function () {
							$("#codeLookupArea").dialog("destroy").remove();
						}
					}
				);
				return false;
			}
		); 
	}
);
function loadLogout() {
	$("#loginFormArea").dialog("destroy").remove();
	$("#loginDiv").load("/sheet-music/logout.jsp", { r: r() },
		function () {
			$("#logoutLink").click(
				function () {
					$.ajax("/sheet-music/login",
						{
							async: false,
							data: { option: "logout", r: r() },
							type: 'POST'
						}
					);
					return true;
				}
			);
			$(".loginRequired").die('click');
			$(".loginAjaxRequired").die('click');
			$(".loginAjaxRequired").live('click',
				function () {
					ajaxLoggedin($(this).attr("href"));
					return false;
				}
			);
			$(".loginFormRequired").unbind("submit");
		}
	);
}
function loadLogin() {
	$("#loginDiv").load("/sheet-music/login.jsp", { r: r() },
		function () {
			$("#loginLink").click(
				function () {
					login();
					return false;
				}
			);
			$(".loginRequired").live('click',
				function () {
					loginRedirect($(this).attr("href"));
					return false;
				}
			);
			$(".loginFormRequired").live('submit',
				function () {
					loginForm(this);
					return false;
				}
			);
			$(".loginAjaxRequired").die('click');
			$(".loginAjaxRequired").live('click',
				function () {
					loginAjax($(this).attr("href"));
					return false;
				}
			);
		}
	);
}
function loginRedirect(redirect) {
	login(
		function () {
			window.location.href = redirect;
		}
	);
}
function loginForm(form) {
	login(
		function () {
			$(".loginFormRequired").die("submit");
			$(form).submit();
		}
	);
}
function loginAjax(url) {
	login(
		function () {
			ajaxLoggedin(url);
		}
	);
}
function ajaxLoggedin(url) {
	var loginAjaxArea = $("#loginAjaxArea")[0];
	if (!loginAjaxArea) {
		$("<div id=\"loginAjaxArea\" />").appendTo("body");
	}
	$("#loginAjaxArea").load(url, { r: r() });
}
function login(redirect) {
	$("<div id=\"loginFormArea\" />").appendTo("body");
	$("#loginFormArea").load("/sheet-music/login_form.jsp", { r: r() },
		function () {
			$("#loginErrorArea").hide();
			$("#loginHint").hide();
			$("#loginHintArea").hide();
			$("#loginHintLink").click(
				function () {
					$("#loginHint").toggle();
					return false;
				}
			);
			$("#loginForm").ajaxForm(
				{
					url: "/sheet-music/login",
					dataType: "json",
					beforeSubmit: function () {
						$("#loginButton").val("Processing...").attr("disabled", "disabled");
					},
					success: function (data) {
						$("#loginButton").val("Login").removeAttr("disabled");
						if (data.status === OK) {
							loadLogout();
							if (redirect) {
								redirect();
							}
						} else if (data.status === INVALID_EMAIL) {
							$("#loginErrorArea").html("Invalid email address.").show();
							$("#loginHintArea").hide();
						} else if (data.status === INVALID_PASSWORD) {
							$("#loginErrorArea").hide();
							$("#loginHint").html("Hint: " + data.hint);
							$("#loginHintArea").show();
						}
					}
				}
			);
		}
	).dialog(
		{
			height: 200,
			modal: true,
			overlay: { opacity: 0.7, background: "black" },
			position: "center",
			title: "Login",
			width: 400,
			close: function () {
				$("#loginFormArea").dialog("destroy").remove();
			}
		}
	);
	return false;
}
function mycarousel_initCallback(carousel) {
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};
animatedcollapse.addDiv('contemporary', 'fade=1,speed=400,group=new');
animatedcollapse.addDiv('study', 'fade=1,speed=400,group=new');
animatedcollapse.addDiv('popular', 'fade=1,speed=400,group=new');
animatedcollapse.addDiv('broadway', 'fade=1,speed=400,group=new');
animatedcollapse.addDiv('general', 'fade=1,speed=400,group=new');
animatedcollapse.init();

try {
    document.execCommand('BackgroundImageCache', false, true);
}
catch(e) {};

/*******************************************************************************
 * Drop Down/ Overlapping Content- © Dynamic Drive (www.dynamicdrive.com) This
 * notice must stay intact for legal use. Visit http://www.dynamicdrive.com/ for
 * full source code
 ******************************************************************************/
function getposOffset(overlay, offsettype) {
	var totaloffset=(offsettype=="left")? overlay.offsetLeft : overlay.offsetTop;
	var parentEl=overlay.offsetParent;
	while (parentEl!=null) {
		totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
		parentEl=parentEl.offsetParent;
	}
	return totaloffset;
}
function overlay(curobj, subobjstr, opt_position) {
	if (document.getElementById) {
		var subobj=document.getElementById(subobjstr);
		subobj.style.display=(subobj.style.display!="block")? "block" : "none";
		var xpos=getposOffset(curobj, "left")+((typeof opt_position!="undefined" && opt_position.indexOf("right")!=-1)? -(subobj.offsetWidth-curobj.offsetWidth) : 0);
		var ypos=getposOffset(curobj, "top")+((typeof opt_position!="undefined" && opt_position.indexOf("bottom")!=-1)? curobj.offsetHeight : 0);
		subobj.style.left=xpos+"px";
		subobj.style.top=ypos+"px";
		return false;
	} else {
		return true;
	}
}
function overlayclose(subobj) {
	document.getElementById(subobj).style.display="none";
}
function notEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus();
		return false;
	}
	return true;
}
function r() {
	return parseInt(Math.random() * 99999999);
}

