jQuery(document).ready(function($) {
    // Handle the slideshow
	homeSlideshow();
	
	// Handle the testimonial slider
	testimonialSlider();
	
	// Handle the text of the search bar
    searchBarText();
	
	// Handle the bx links on the home page
	bxLinks();
	
	// Handle the special product page styling tweaks
	productPageTweaks();
	
	// Handle tweaks to the product list pricse
	productListPriceTweaks();
	
	// Add "odd" "even" classes to the product details rows
	productDetailsClasses();
	
	$("a.button").each(function () {
	  if ($(this).html() == 'Add To Cart') {
	    $(this).html("Add to Cart");
	  }
	});
	
	// 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');
	
	// Checkout Overhaul //
	$('.checkout #CreateAccountButton, .checkout #LoginButton, .checkout .billingButton').addClass('button');	
	
	if ($("body.news-list").length > 0) {
		$.getJSON("http://www.wordpressfeeder.com/feed.php?feed=http://watchwarehousedev.mybigcommerce.com/rss.php?action=newblogs&type=rss&content_char_limit=500&max_posts=10&thumbnail=no&read_more=1&callback=?", function(data) {
			for (i = 0; i < data.length; i++)
			{
			   $(".wordpress-feeder").append(
			   "<li><h2><a href='" + data[i].rssItem.link + "'>" + data[i].rssItem.title + "</a></h2><div class='pubdate'>" + data[i].rssItem.pubdate + "</div><div class='image'><img src='" + data[i].rssItem.postThumbnail + "'></div><div class='content'>" + data[i].rssItem.content + "</div><div class='read-more'><a href='" + data[i].rssItem.link + "'>" + data[i].rssItem.readMore + "</a></div></li>"   	
			   ); 		
			}		
		}); 
	}
});

function productDetailsClasses() {

	if ($("#ProductOtherDetails").length == 0) {
		return false;
	}
	
	$("#ProductOtherDetails .DetailRow:even").addClass("odd");
	$("#ProductOtherDetails .DetailRow:odd").addClass("even");

}

function productListPriceTweaks() {

	var price = $(".ProductPriceRating em");
	if (!price.length) {
		return false;
	}
	  
	var strike = {};
	var rating = {};
	  
	// We have to isolate the product price since it's wrapped the same as the retail price.
	price.each(function () {
		strike = $(this).find("strike");
		strike.prepend("retail price: ");
		
		rating = $(this).parent().find("span.Rating");
		
		$(this).parent().append(strike);
		$(this).parent().append(rating);
	});
	
	price.prepend("Your Price: ");
	_productPrice(price);

}

function productPageTweaks() {
  
	if ($("body.product").length == 0) {
		return;
	}
  
	$(".PriceRowSave .Value").prepend($(".YouSaveAmount"));
	$(".YouSave").remove();
  
	// Modify the price
	_productPrice($(".ProductPrice"));
	
	
	var diff = _productPageTweaksClean($(".YouSaveAmount").text());
	
	// Stop here if diff is empty
	if (!diff) {
	  $(".PriceRowSave").remove();
	}
  
	// Compute the savings percentage
	var retail = _productPageTweaksClean($(".RetailPrice strike").text());
	var perc = Math.round((diff / retail) * 100);
	var savings = " (" + perc + "%)"; 
	$(".YouSaveAmount").append(savings);
  
}

function _productPageTweaksClean(num) {

  return parseInt(num.replace(/\$|,/g, ""));

}

function _productPrice(ProductPrice) {

    ProductPrice.each(function () {
		var price = $(this).text().split(".");
		$(this).html(price[0].replace("$", '<span class="SmallDollar">$</span>'));
	});
	
}

function bxLinks() {
	
	if ($("body.home").length > 0) {
		$('#HomeSideTopSellers').bxSlider({
			displaySlideQty: 4,
			moveSlideQty: 4,  
			pager: true,
			infiniteLoop: false
		});
		
		$("#HomeFeaturedProductsSlider ul").bxSlider({
			displaySlideQty: 4,
			moveSlideQty: 4,  
			pager: true,
			infiniteLoop: false
		});
		
		// Add the next and previous buttons to the pager div
		$(".bx-link-container").append($(".bx-prev"));
		$(".bx-link-container").append($(".bx-pager"));
		$(".bx-link-container").append($(".bx-next"));

	}

}

function searchBarText() {
	
	// Text for the search bar
	if ($("#search_query").attr("value") == '') {
	  
		var string = "item number or key words";
		$("#search_query").attr("value", string);
		$("#search_query").click(function () {
			if ($(this).attr("value") == string) {
				$(this).attr("value", "");
			}
		});
	
	}

}

function homeSlideshow() {
	  
	  if ($('.banner_home_page_top p').length == 0) {
		return;
	  }
	  
	  $('.banner_home_page_top p')
	  .cycle({
		  fx: 'fade',  // Effects: http://jquery.malsup.com/cycle/browser.html
		  timeout: 6000
	  });

}

function testimonialSlider() {
	
	// Setting for the scroller
	var tHeight = $("#InfoRow #TestimonialsInner2").height();
	var t = $("#InfoRow #TestimonialsInner2");
	var scrollAmt = $("#InfoRow #TestimonialsInner2 li").outerHeight();
	var scrollTop = 0;
	var scrollPos = scrollTop;	
	
	$(".testimonial-slider-up").click(function () {		
	  if (scrollPos != scrollTop) {
		scrollPos += scrollAmt;
		if (scrollPos > scrollTop) {
			scrollPos = scrollTop;
		}
	  
		t.css("top", scrollPos + "px");		
	  
	  }
	  return false;
	});
	
	$(".testimonial-slider-down").click(function () {
	  
	  scrollPos -= scrollAmt;
	  
	  if ((scrollPos + (tHeight - scrollAmt)) < scrollTop) {
	    scrollPos = ((tHeight - scrollAmt) * -1) + scrollTop;
	  }
	  
	  t.css("top", scrollPos + "px");
	  
	  return false;
	  
	});

}
