// JavaScript Document

jQuery(document).ready(function() { 

	jQuery('a[rel*=lightbox]').lightBox(); // Select all links that contains lightbox in the attribute rel

	/**
	 * Main navigation
	 */
	jQuery("div#navigation li").each( function() {
											   
		var link = jQuery(this).find('a');
		
		// If this is the home page
		if (location.pathname == '/' && link.attr('href') == '/') {
			jQuery("div#navigation li a").removeClass("on");
			link.addClass("on");
			
		// Check to see if this is a top level nav item
		} else if ((location.pathname.indexOf(link.attr('href')) > -1) && link.attr('href') != '/') {
			jQuery("div#navigation li a").removeClass("on");
			link.addClass("on");
			
		// Is this a sub page (We're using the Breadcrumbs to work this out - is there a better way?)
		} else {
			/*jQuery("div#breadcrumbs a").each( function() {							
				if ((link.attr('href').indexOf(jQuery(this).attr('href')) > -1) && jQuery(this).attr('href') != '/') {
					jQuery("div#navigation li a").removeClass("on");
					link.addClass("on");
				}
			});*/
		}
	});
		
	/**
	 * Header
	 */
	// Sign up for our newsletter
	var search_text = 'Enter email address';
	jQuery('input#sign_up_email_input').val(search_text);
	jQuery('input#sign_up_email_input').blur(function() {
	if(jQuery(this).val() == '')
		jQuery(this).val(search_text);
	}).focus(function() {
	if(jQuery(this).val() == search_text)
		jQuery(this).val('');
	});

	/**
	 * Home page
	 */
	// Load home page elements
	jQuery('#close_box').show();
	jQuery('#intro_text_hr').show();
	jQuery('#player_controls').show();
	
	// Load all photos
	jQuery('#home_page_photos img').show();
	 
	// Start the image rotation 
	jQuery('#home_page_photos').cycle({ 
		fx: 	'fade',
		next:  	'#home_image_forward',
		prev:	'#home_image_back'
	});
	
	//Hide the play button by default 
	jQuery('#home_image_play').hide(); 
	
	// Player controls
	jQuery('#home_image_pause').click(function(event) {
		jQuery('#home_page_photos').cycle('pause');
		jQuery('#home_image_pause').hide();
		jQuery('#home_image_play').show();
	});
	
	jQuery('#home_image_play').click(function(event) {
		jQuery('#home_page_photos').cycle('resume');
		jQuery('#home_image_play').hide();
		jQuery('#home_image_pause').show();
	});
	
	// Intro Text Box
	jQuery('#close_button').click(function(event) {
		if (jQuery("#intro_text_content").is(':hidden')) {
			jQuery('#open_close_button_img').attr('src', '/images/close_button.gif');
			jQuery('#intro_text').animate({width: '307px'}, {queue:false, duration:300});
			jQuery('#intro_text_content').slideDown(300);
			jQuery('#close_text').show(300);
		} else {
			jQuery('#intro_text_content').slideUp(300);
			jQuery('#intro_text').animate({width: '23px'}, {queue:false, duration:300});
			jQuery('#close_text').hide();
			jQuery('#open_close_button_img').attr('src', '/images/open_button.gif');
		}
	});
	
	
	/**
	 * Contact us maps
	 */
    $('#large_sales_map a').lightBox();
	$('#large_service_map a').lightBox();
	
	
	//<img src="' + slide.getElementsByTagName('img')[0].src + '" alt="' + slide.getElementsByTagName('img')[0].alt + '" />
	
	/**
	 * Custom Installs page
	 */
	jQuery("#custom_install_pager").show(); 
	jQuery("#custom_install_photos").show();  
	
	//Hide the play button by default 
	jQuery('#custom_image_play').hide(); 
	
	// Player controls
	jQuery('#custom_image_pause').click(function(event) {
		jQuery('#custom_install_photos').cycle('pause');
		jQuery('#custom_image_pause').hide();
		jQuery('#custom_image_play').show();
	});
	
	jQuery('#custom_image_play').click(function(event) {
		jQuery('#custom_install_photos').cycle('resume');
		jQuery('#custom_image_play').hide();
		jQuery('#custom_image_pause').show();
	});
	 
    jQuery(".custom_install_photo_wrap:first").children('div.grey_photo_footer').animate({top: "80px"}, 300).animate({top: "0px"}, 500);
	
	jQuery('#custom_install_photos').cycle({ 
		fx: 'scrollHorz',
		speed:  '1000', 
		timeout: '4000',
		pager:  '#custom_install_pager_buttons',
		// callback fn that creates a thumbnail to use as pager anchor 
		pagerAnchorBuilder: function(idx, slide) {
	    	return '<li id="' + slide.getElementsByTagName('span')[0].id + '"><a href="#"></a></li>'; 
	    },
		before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
        	if (currSlideElement != nextSlideElement) {
  				jQuery('#' + currSlideElement.id).children('div.grey_photo_footer').animate({top: "80px"}, 300);
			}
		},
		after: function(currSlideElement, nextSlideElement, options, forwardFlag) {
     		if (currSlideElement != nextSlideElement) {
        		jQuery('#' + nextSlideElement.id).children('div.grey_photo_footer').animate({top: "80px"}, 300).animate({top: "0px"}, 500);
     		}
		}
	});
	
	
	/**
	 * Product sub navigation
	 */
	jQuery("div#product_category_nav li").each( function() {
											   
		var link = jQuery(this).find('a').first(); // Only apply to this a link (the first found) and not any children (or sub navigation items!)
			
		if ((location.pathname.indexOf(link.attr('href')) > -1) && link.attr('href') != '/') {
			jQuery("div#product_category_nav li a").removeClass("on");
			link.addClass("on");
		} else {
			jQuery("div#products_content_wrapper h1 a").each( function() {							
				if ((link.attr('href').indexOf(jQuery(this).attr('href')) > -1) && jQuery(this).attr('href') != '/') {
					jQuery("div#product_category_nav li a").removeClass("on");
					link.addClass("on");
				}
			});
		}
	});

});

function goToAnchor(anchor_name) {	
	jQuery('html,body').animate({scrollTop: jQuery("#"+anchor_name).offset().top},0);
}
