/*======================================================================
	Hayward Pools: JavaScript Functions
	--------------------------------------------------------------------
	Author: Jay Sylvester
	Company: WebLinc
	Date: 09.01.09
----------------------------------------------------------------------*/
var WEBLINC = {};

function set_cookie(name,value,days)
{
    if( days )
    {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else
    {
    	var expires = "";
    }
    
    document.cookie = name + "=" + value + expires + "; path=/";
}

function get_cookie( name )
{
    var name_eq = name + "=";
    var ca = document.cookie.split(';');
    
    for( var i=0; i < ca.length; i++ )
    {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(name_eq) == 0) return c.substring(name_eq.length,c.length);
    }
    
    return null;
}
	
$(document).ready(function() {
//$(document).bind('ajaxLoaded', function() {
	/*======================================================================
		UTILITY SELECT MENU'S
		--------------------------------------------------------------------
		Graphical select menu handlers
	----------------------------------------------------------------------*/
		var $optionSelectors = $('#utility-nav ul.option-selector > li');
		
		$('> a',$optionSelectors).click(function() { return false; });
				
		$optionSelectors.hover(
			function () {
				$('> ul',this).show();
			}, 
			function () {
				$('> ul',this).hide();
			}
		);

	/*======================================================================
		TEXT INPUT CLEARING/REINSTATING
		--------------------------------------------------------------------
		Handles text input clearing of text on focus, and reinstate on blur
	----------------------------------------------------------------------*/
		WEBLINC.focusInputs =
		{
			init: function(obj)
			{
				var searchInitVal = obj.val();
				
				obj.focus(function() {
					if($(this).val() == searchInitVal)
						$(this).val("").removeClass("hint");
				})
				.blur(function() {
					if($(this).val() == "")
						$(this).val(searchInitVal).addClass("hint");
				});
			}
		}
		
		// init the global inputs on ready
		WEBLINC.focusInputs.init($('#search-form-container .input-text'));
		WEBLINC.focusInputs.init($('#email-signup-container .input-text'));
		WEBLINC.focusInputs.init($('.landing-search .input-text'));

	/*======================================================================
		TEXT RESIZING
		--------------------------------------------------------------------
		Handles text size changes from utility nav options
	----------------------------------------------------------------------*/
		var $textSizeSelectors = $('#utility-nav .text-size');
		
		$('li a',$textSizeSelectors).click(function() {
			var $body = $("body"),
				$this = $(this);
			
			$('li',$textSizeSelectors).removeClass('active');
			
			switch($this.closest('li').attr('class'))
			{
				case 'small':
					$body.css("font-size","62.5%");
					set_cookie('font-size', '62.5%', 7);
					break;
				case 'medium':
					$body.css("font-size","70%");
					set_cookie('font-size', '70%', 7);
					break;
				case 'large':
					$body.css("font-size","80%");
					set_cookie('font-size', '80%', 7);
					break;
			}
			
			$this.closest('li').addClass('active');
			
			return false;
		});


	/*======================================================================
		RESIDENTIAL HOME PAGE
		--------------------------------------------------------------------
		Sets up controls for navigating through image rotator
	----------------------------------------------------------------------*/
		$(function() {
			$('#stories').tabs();
			$('#stories').append('<ul class="controls"><li class="previous"><a href="#">Previous</a></li><li class="next"><a href="#">Next</a></li></ul>');
		});
		$('ul.controls li.next a').click(function() {
			var $tabs = $('#stories').tabs();
			var next = $tabs.tabs('option', 'selected') + 1;
				if ( next > $tabs ) {
					next = 0;
				};
			$tabs.tabs('select', next);
			return false;
		});
		$('ul.controls li.previous a').click(function() {
			var $tabs = $('#stories').tabs();
			var prev = $tabs.tabs('option', 'selected') - 1;
				if ( prev < 0 ) {
					prev = $tabs;
				};
			$tabs.tabs('select', prev);
			return false;
		});

		// Add markup for displaying up/down arrows in left nav
		$('#side-navigation>ul>li>ul>li>ul').siblings('a').append('<i></i>');
		
		var numImages = $("#content-container div.product-images div.carousel img[name^='thumb']").size();
		
	/*======================================================================
		COMMERCIAL HOME PAGE
		--------------------------------------------------------------------
		Handler for the commercial home page features
	----------------------------------------------------------------------*/
		if( numImages == 0 )
		{
			$('.commercial #content-container div.carousel').jcarousel({ scroll: 2 });
		}

	/*======================================================================
		FLOW CONTROL HOME PAGE
		--------------------------------------------------------------------
		Handler for the flow control home page features
	----------------------------------------------------------------------*/
		if( numImages == 0 )
		{
			$('.flow-control #content-container div.carousel').jcarousel({ scroll: 1, horizontal: true });
		}
		
	/*======================================================================
		HSTC HOME PAGE
		--------------------------------------------------------------------
		Handler for the HSTC home page features
	----------------------------------------------------------------------*/
		if( numImages == 0 )
		{
			$('.HSTC #content-container div.carousel').jcarousel({ scroll: 2, horizontal: true });
		}

	/*======================================================================
		default HOME PAGE
		--------------------------------------------------------------------
		Handler for the default home page features
	----------------------------------------------------------------------*/
		if( numImages == 0 )
		{
			$('.default #content-container div.carousel').jcarousel({ scroll: 2 });
		}

	/*======================================================================
		DETAIL PAGE
		--------------------------------------------------------------------
		Handler for the detail page features
	----------------------------------------------------------------------*/
		$('#content-container div.product-details div.tabs').tabs();

		if( numImages <= 1 )
		{
			$('#content-container div.product-images div.alternates').css("display", "none");
		}
		else if( numImages <= 3 )
		{
			$('#content-container div.product-images div.carousel').jcarousel({
				visible: 3,
				buttonNextHTML: null,
				buttonPrevHTML: null
	    	});
		}
		else
		{
			$('#content-container div.product-images div.carousel').jcarousel({
				visible: 3,
				buttonNextHTML: '<a class="control" href="#"><span>Next</span></a>',
				buttonPrevHTML: '<a class="control" href="#"><span>Previous</span></a>'
	    	});
		}
		
		$('#content-container div.product-images div.carousel a.control').click(function() {
			return false;
		});


	/*======================================================================
		DATE PICKER
		--------------------------------------------------------------------
		Handler for all input fields with a class of .date-picker
		binds click event for date picker.
	----------------------------------------------------------------------*/
		var fedHolidays = [
							[1, 1, 2010], [1, 18, 2010], [2, 15, 2010], [5, 31, 2010], 
							[7, 5, 2010], [9, 6, 2010], [10, 11, 2010], [11, 11, 2010], 
							[11, 25, 2010], [12, 24, 2010], [12, 31, 2010], [1, 17, 2011], 
							[2, 21, 2011], [5, 30, 2011], [7, 4, 2011], [9, 5, 2011], 
							[10, 10, 2011], [11, 11, 2011], [11, 24, 2011], [12, 26, 2011], 
							[1, 2, 2012], [1, 16, 2012], [2, 20, 2012], [5, 28, 2012],  
							[7, 4, 2012], [9, 3, 2012], [10, 8, 2012], [11, 12, 2012], 
							[11, 22, 2012], [12, 25, 2012], [1, 1, 2013], [1, 21, 2013], 
							[2, 18, 2013], [5, 27, 2013], [7, 4, 2013], [9, 2, 2013], 
							[10, 14, 2013], [11, 11, 2013], [11, 28, 2013], [12, 25, 2013], 
							[1, 1, 2014], [1, 20, 2014], [2, 17, 2014], [5, 26, 2014], 
							[7, 4, 2014], [9, 1, 2014], [10, 13, 2014], [11, 11, 2014], 
							[11, 27, 2014], [12, 25, 2014], [1, 1, 2015], [1, 19, 2015], 
							[2, 16, 2015], [5, 25, 2015], [7, 3, 2015], [9, 7, 2015], 
							[10, 12, 2015], [11, 11, 2015], [11, 26, 2015], [12, 25, 2015]
						  ];
						  
		function federalHolidays(date)
		{			
			for( i = 0; i < fedHolidays.length; i++ )
			{
				if( date.getMonth() == fedHolidays[i][0] - 1 &&
				    date.getDate()  == fedHolidays[i][1] &&
				    date.getYear()  == fedHolidays[i][2] )
				{
					return [false, ''];
				}
			}
			
			return [true, ''];
		}
		
		function noWeekendsOrHolidays(date)
		{
			var noWeekend = $.datepicker.noWeekends(date);
			if( noWeekend[0] )
			{
				return federalHolidays( date );
			}
			else
			{
				return noWeekend;
			}
		}
						  
		$('#content-container input.ship-date-picker').datepicker(
			{ altFormat: 'mm-dd-yyyy',
			  minDate: '+0D',
			  hideIfNoPrevNext: true,
			  beforeShowDay: noWeekendsOrHolidays });
		
		$('#content-container input.ship-date-picker').click(function(){
			$(this).datepicker('show');
		});
						  
		$('#content-container input.date-picker').datepicker(
			{ altFormat: 'mm-dd-yyyy',
			  hideIfNoPrevNext: true });
		
		$('#content-container input.date-picker').click(function(){
			$(this).datepicker('show');
		});


	/*======================================================================
		DISTRIBUTOR LOCATOR
		--------------------------------------------------------------------
		Handler for country select form switching. - NOT USED ANYMORE
	----------------------------------------------------------------------
		var $distributorLocators = $('#content-container .distributor-locator');
		
		$('#distributor-country').change(function(){
			switch($(this).val()){
				case 'us':
					$distributorLocators.hide();
					$('#united-states').show();
					break;
				case 'ca':
					$distributorLocators.hide();
					$('#canada').show();
					break;
				case 'other':
					$distributorLocators.hide();
					$('#international').show();
					break;
				default:
					$distributorLocators.hide();
					$('#united-states').show();
					break;
			}
		});
	*/

	/*======================================================================
		BUTTONIZE
		--------------------------------------------------------------------
		Converts form buttons to anchors for better styling control.
	----------------------------------------------------------------------
		(function($) {
			$.fn.buttonize = function(options) {
				var settings = $.extend({ replacedClass: 'button-replaced' }, options);
							
				return this.each(function() {
					var $this = $(this),
						text = $this.text() || $this.val(),
						$button = null;
						
					if($this.is('input')) {
						$button = $('<a href="#"></a>')
							.addClass(this.className)
							.attr('id', this.id)
							.click(function() { $this.trigger('click'); return false; })
							.insertBefore(this);
						
						$.each(['class', 'value', 'id'], function() { $this.removeAttr(this); });
						$this.addClass(settings.replacedClass);
					}
					
					($button || $this).html(text + '<span class="corner corner-tl"></span><span class="corner corner-tr"></span><span class="corner corner-bl"></span><span class="corner corner-br"></span>');
				});
			};
		})(jQuery);
		
		$('.button, .button-alt').buttonize();
		
		*/

});
