jQuery(document).ready(function($) {

    // Slideshow
	if ($('.banner_home_page_top p').length > 0) {
		$('.banner_home_page_top p').cycle({
			timeout: 4000,
			sync: true
		});
	}
	
	// Search bar text replacement
	function textReplacement(input) {
		var originalvalue = input.val();
		input.focus(function() {
			if($.trim(input.val()) == originalvalue){ input.val(""); }
		});
		input.blur(function() {
			if($.trim(input.val()) == ""){ input.val(originalvalue); }
		});
	}
	
	if (!$("#search_query").val()) {
	  $("#search_query").val("search Museware");
	}
	textReplacement($("#search_query"));
	
	//Newsletter text replacement
	textReplacement($("#nl_first_name"));
	textReplacement($("#nl_email"));
	
	// Handling multiple breadcrumbs
	var counter = 0;	
	$(".Breadcrumb ul").each(function () {
	  counter++;
	  if (counter > 1) {
	    $(this).addClass("spacer");
	  }
	});
	
	// If we are on the checkout page, make sure user agrees to terms and conditions
	/*if ($(".ExpressCheckout").length > 0 && $(".muse-confirm-box").length == 0) {
	  
	  var overlay = $('<div class="muse-overlay"></div>');
	  $("body").prepend(overlay);
	  var box = $('<div class="muse-confirm-box"></div>');
	  var boxInner = $('<div class="muse-confirm-box-inner"></div>');
	  var text = $('<div class="muse-confirm-box-text"></div>');
	  text.html("<h2>Thank you for your interest in Museware Pottery!</h2> <p><em>Please indicate that you have read and understand the following statement:</em></p><p>Please be sure you appreciate the natural imperfections of a hand made product before placing your order. Crooked letters, differences in pottery dimension, paint color, image placement and composition are common characteristics of a hand made product. Some or all of these imperfections are present in every piece of Museware Pottery.<br/><br/>Personalized gifts are hand painted to order and cannot be canceled or returned. Please review your request carefully before placing your order as orders cannot be changed once submitted. Museware Pottery is not responsible for your spelling or other errors.</p>");
	  var buttons = $('<div class="muse-confirm-box-buttons"></div>');
	  var cancel = $('<input type="submit" name="op" value="No" />');
	  var agree = $('<input type="submit" name="op" value="Yes" />');
	  
	  agree.click(function () {
	    $(".muse-overlay, .muse-confirm-box").hide();
		return false;
	  });
	  
	  cancel.click(function () {
	    window.location = "/";
		return false;
	  });
	  
	  buttons.append(agree); //buttons.append(cancel);
	  boxInner.append(text); boxInner.append(buttons);
	  box.append(boxInner);
	  $("body").prepend(box);
	  var boxEl = $(".muse-confirm-box");
	  boxEl.css("top", (($(window).height() - boxEl.outerHeight()) / 2) + $(window).scrollTop() + "px");
	  $(".muse-overlay").css("height", ($(window).height() + ($(window).height() / 2)) + "px");
	  
	}*/
	
	// Handle product attribute textbox character limits
	if ($(".ProductMain").length > 0) {
		
		var count = 0;
		$(".ProductMain input[type='text'], .ProductMain textarea").each(function () {
			characterLimits($(this));
		});	
		
	}
	
	// General form cleanup
	$('.ContactButton').addClass('button');
	
	// Add CurvyBox to Create Account //
	$('.createaccount .Textbox, .createaccount select, .createaccount .JSHidden, #EditAccountForm .Textbox, #ShippingAddressForm .Textbox, #ShippingAddressForm select').addClass('CurvyBox');
	
	// Add CurvyBox to Product Page //
	$('#qty_, #text_qty_, .productAttributeFluidWidth, .fileInput label input, .dateselector select, .productOptionViewSelect select, .productOptionViewRectangle ul li label, .productAttributeConfigurableEntryNumbersOnlyText input').addClass('CurvyBox');
	
	// Add CurvyBox to Contact Form //
	$('#ContactForm .Textbox, #ContactForm textarea').addClass('CurvyBox');
		
	// Checkout Overhaul //
	$('.checkout #CreateAccountButton, .checkout #LoginButton, .checkout .billingButton').addClass('button');	
});

/**
 * Enforce character limits on product attribute elements.
 */
function characterLimits (el) {

	var tagname = el[0].tagName;	
	var wrapper = el.parent();
	var label = wrapper.prev();
	var labelTxt = label.html();
	var pos = 0;
	var limit = 0;
	var countdown = false;
	
	// See if the label text contains brackets
	if ((pos = labelTxt.search(/\[+[c0-9]+\]/)) != -1) {
		
		// Extract the number from the text
		var start = pos;
		var end = labelTxt.search("]") + 1;
		var text = labelTxt.substring(start, end);		
		
		// Remove the brackets from the label text
		labelTxt = labelTxt.replace(/\[+[c0-9]+\]/, '').replace("  :", ':').replace(" :", ':');
		label.html(labelTxt);
		
		// Isolate the number
		var limit = text.replace(/\[|\]/, '');
		
		if (limit.search("c") != -1) {
			limit = limit.replace("c", '');
			countdown = true;
		}
		
		limit = parseInt(limit);
		
		// Textfields are easy...
		if (tagname == 'INPUT') {
			el.attr("maxlength", limit);
		}
		// For textareas, enforce the maxlength with a keystroke event
		else {
			el.bind("keyup", function() {
				if ($(this).val().length >= limit) {
				
					// Remove extra characters if there are any
					$(this).val($(this).val().substring(0, limit));
					
					// Flash a red border so the user knows something happened.
					$(this).css("background", "#ffbcbc").animate({backgroundColor : "#fff"}, 500);
				}
			});
		}
		
		// Handle the countdown if there is one
		if (countdown) {
		
			wrapper.append($('<div class="char-countdown">Available characters: <span>' + limit + '</span></div>'));			
			el.bind("keyup", function () {
				var available = limit - $(this).val().length;
				$(".char-countdown span").html(available < 0 ? 0 : available);
			});
		
		}
		
	}

} 
