/*
	NOTE: jQuery's native '$' selector is disabled because it conflicts with the '$' selector
	used by both Prototype and Scriptaculous on this site. 

	All selectors/functions that begin with '$' should use 'jQuery' instead
*/

var rotateInt = '';

jQuery(document).ready(function(){

	/* - - - - Defines the number of testimonials to display - - - */
	var testimonialsPerPage = 5;


	/*
		PAGER SYSTEM FUNCTIONALITY
	*/
	if (jQuery(".catalog-category-view .testimonial").length) // check that there's testimonials on the page
	{
		var total = jQuery(".testimonial").length;					// grab the total number of testimonials
		var numPages = Math.ceil(total/testimonialsPerPage);		// calculate the number of pages
		var currentPage = document.location.href.match(/\#.*$/);	// get the current page from the URL [returns an array]
		if (currentPage){currentPage = currentPage[0].substring(2);}
		else {currentPage = 1;}
		var pager = "<div class='pager'><div class='pages'><strong>Page:</strong><ol>";
		for (var i=1;i<=numPages;i++)
		{
			var current='';
			pager += "<li><a href='#p"+i+"'>"+i+"</a></li>";
		}
		pager += "</ol></div></div>";
		jQuery(".category-title").after(pager);
		
		jQuery(".pages a").each(function(i) {
			var low = (i*testimonialsPerPage);		// Lower limit of listings to display for this link
			var high = low+testimonialsPerPage;		// Upper limit of listings to display for this link
			jQuery(this).data('low',low);		// Store the lower limit in data index 'low' associated to this link
			jQuery(this).data('high',high);		// Store the upper limit in data index 'high' associated to this link
		});

		jQuery(".pages a").click(function(e){
			var link = jQuery(e.target); // grab the link that's been clicked
			jQuery(".testimonial").hide().slice(link.data('low'),link.data('high')).show();
			link.closest("ol").find("span").remove().end().find("a").not(link).show()
			link.before("<span class='current'>"+link.text()+"</span>").hide();
		});
		jQuery(".pages ol li:eq("+(currentPage-1)+")").children("a").click();
	}	


	/* HOMEPAGE BANNER/VIDEO CONTROLS */
	if (jQuery(".cms-home").length)
	{
		var firstRun = true; // the first time through, this will prevent the 'fast' switching effect
		jQuery(".image-container ul#banner-controls li").click(function(e){
			if (rotateInt)
			{
				clearInterval(rotateInt);
			}
			jQuery(".image-container ul#banner-controls li").removeClass('current'); // remove all .current classes from all <li>
			var prev = jQuery(e.target).prev("li"); // grab the previous <li>
			if (!prev.length) // 
			{
				prev = jQuery(".image-container ul#banner-controls li:last"); // if there isn't one, set the previous <li> to the last one in the <ul>
			}
			prev.addClass('current'); // add the .current class to the previous element
			if (!firstRun)
			{
				setTimeout("rotate()",10); // if it's not the first run, do a 'fast' switching effect
			}
			firstRun = false;
			rotateInt = setInterval("rotate()",8000);
		}).eq(1).click();
	
		jQuery("#hp-video").flash({
			swf: '/ssp/slideshowpro.swf',
			flashvars: {
				paramXMLPath: "/ssp/param.xml",
				initialURL: escape(document.location)
			},
			params: {
				base: ".",
				quality: "best",
				bgcolor: "#121212",
				wmode: "transparent",
				allowfullscreen: "true"
			},
			attributes: {
				style:"yes"
			},
			height:300,
			width:340
		});
	}

	/* PRODUCT DISCLAIMER CONTROLS */
	jQuery(".catalog-product-view .col-main .product-essential")
		.after("<div class='notice-msg stud-msg'>"
						+"Do you need more fasteners for your project?<br /><br />"
						+"<ul class='notice-list'><li>Fasteners should be placed no more than 12\" apart.</li></ul>"
						+"Please make sure you have enough to complete your project, and order extra fastener sets if necessary."
				 +"</div>")
		.before("<a title='Click to learn more about free shipping' href='/customer-service-and-support' class='shipping-large'><img src='/skin/frontend/default/blank/images/shipping-info-large.gif' alt='Free Shipping on orders over $255' /></a>");

	/* -- - - - - - - Budget Blinds Customers  - Disable main 'Store' button */
	if (jQuery("#nav .nav-store-bb").length || jQuery("#nav .nav-store-dealer-products").length) {
		jQuery("#nav .nav-store > a").click(function(e){e.preventDefault();})
	}

	/* IMAGE GALLERY DESCRIPTION STYLING */
	jQuery("body.gallery-view-album .col-main div:first").addClass('gallery-description');


 	var path = getPathName();
	
	if(path == '/')
	{
		//insert blog section after #page-bottom div
		jQuery("#blogSection").insertAfter(jQuery('#page-bottom'));
	}

	//change sub-catgeroy url

	
	jQuery("#nav > li.nav-2 > ul > li:first > a").attr('href','/products');
	jQuery("#nav > li.nav-3 > ul > li:first > a").attr('href','/about-ez-snap');
	jQuery("#nav > li.nav-4 > ul > li:first > a").attr('href','/photo-gallery');
	jQuery("#nav > li.nav-5 > ul > li:first > a").attr('href','/customer-service-and-support');
	
	var dealerSection = '';
	jQuery("#nav > li:first span").each( function(){
		if( jQuery(this).html() == 'Budget Blind Products' ||  jQuery(this).html() == 'Dealer Products'){
			jQuery("#nav > li:first > a").click(function(e){
				e.preventDefault();
			});
			dealerSection = true;
		}
	} );
	
	if( !dealerSection ){
		jQuery("#nav > li.nav-1 > ul > li:first > a").attr('href','/store');
	}
	
	
	//jQuery("#nav > li:first span").css("border", "1px solid red");

		
	//make main nav not clickable, except the contact page.
	//jQuery("#nav > li > a:not([href=http://www.ezsnapdirect.com/contact-us])").attr('href','#');
});

function rotate() {
	var next = jQuery(".image-container ul#banner-controls li:first");
	if (jQuery(".image-container ul#banner-controls li.current").next("li").length)
	{
		next = jQuery(".image-container ul#banner-controls li.current").next("li");
	}
	var filename = next.attr('id');
	jQuery(".image-container ul#banner-controls li").removeClass('current');
	next.addClass('current');
	jQuery(".image-container img").fadeOut(700, function(){
		jQuery(this).attr('src','/skin/frontend/default/blank/images/'+filename+'.jpg').load(function(){
			jQuery(".image-container img").fadeIn(700);
		});
	});


}

function getPathName(){
	return window.location.pathname;
};
