// Namespace
var PRPL = {};

jQuery(document).ready( function($)
{
	//hover effect for the dropdown menus
	$('#nav ul li').bind('mouseenter', function() 
	{
		$(this).find('.list_top').show();
	});
	
	//hover effect for the dropdown menus
	$('#nav ul li').bind('mouseleave', function() 
	{
		$(this).find('.list_top').hide();
	});
	
	
    //  create an appForm object for the form on the contact page
    $('#application_form_wrap').each( function()
    {
        var div = $(this);
        var divId = $(this).attr('id');
        PRPL.app = new PRPL.appForm(div);
        
        $(this).find('form').submit(function(){
        })
    });
    
    //	make a formEraserHelper for each input & textarea field
    $('#email input').each( function()
    {
    	var fieldName	=	$(this).attr('name');
    	PRPL[fieldName]	=	new PRPL.formEraserHelper($(this));
    });

	// news and press tabs function
	$( '.tabs a' ).click( function()
    {
        if ( $(this).hasClass("off") ) // IF this is an off button...
        { 
            $(this).parent().parent().find('a').removeClass('on').addClass('off'); // remove all cases of ON, replace with OFF
            $(this).removeClass('off').addClass('on'); // set this instance to be ON
            
            var targetDiv = $(this).attr('href'); // SET target DIV, reference href of link
            
            $('.media_column > div:visible').fadeOut( // FADEOUT visible sections...
                'fast',
                function()
                {
                    $(targetDiv).fadeIn(); // FADEIN target DIV
                }
            );
        }
        return false;
    });
    
    $('.slideshow_wrap').each( function()
    {
        var divId = $(this).attr('id');
        PRPL[divId] = new PRPL.slideshow($(this));
    });
    
	// tabs for commercial, residential plumbing and leeds
	$( '#tabs a' ).click( function()
    {
        if ( $(this).hasClass("off") ) // IF this is an off button...
        { 
            $(this).parent().parent().find('a').removeClass('on').addClass('off'); // remove all cases of ON, replace with OFF
            $(this).removeClass('off').addClass('on'); // set this instance to be ON
            
            var targetDiv = $(this).attr('href'); // SET target DIV, reference href of link
            
            $('#slides > div:visible').fadeOut( // FADEOUT visible sections...
                'fast',
                function()
                {
                    $(targetDiv).fadeIn(); // FADEIN target DIV
                }
            );
        }
        return false;
    });
});


/*
 * Summary: adds multi-step validation for the application form; with help from Wordpress' Contact Form 7 plugin
 *               
 * Parameters: div: jquery object of wrapper div
 */
PRPL.appForm = function(div)
{
    $               = jQuery;                   //  because wordpress uses jQuery.noConflict, we need to get back the $
    var obj         = this;                     //  cached "this" for use in anonymous functions
    this.div        = div;                      //  jQuery object of the outer div
    this.nextBtns   = $('.next_step');          //  all of the next step buttons
    this.prevBtns   = $('.prev_step');          //  all of the previous step buttons
    this.currentTab = 0;                        //  pointer for the current tab
    this.form     = this.div.find('form');

    //  setup tabs
    this.tabs = this.div.find('.tabs').tabs(
    {
        //  when a tab is changed, update the currentTab pointer
        select: function(event, ui) 
        {
            if(ui.index)
            {
                obj.currentTab = ui.index;
            }
            else
            {
                obj.currentTab = 0;
            }
        }
    });

    //  on the click of a next step button, select the next tab
    this.nextBtns.click(function()
    {
        obj.tabs.tabs('select', obj.currentTab +1);
        window.scroll(0, 0);

        return false;
    });
    
    //  on the click of a prev step button, select the previous tab
    this.prevBtns.click(function()
    {
        obj.tabs.tabs('select', obj.currentTab -1);
        window.scroll(0, 0);
        
        return false;
    });

    //  get rid of all has error markers on tabs before they are re-evaluated
    $('.ui-tabs-nav li').removeClass('has_errors');
        
    /*
    *  after form is submitted and error spans are attached, look through each
    *  tab panel for errors, if any exist, add a class to the panel's corresponding tab
    */
    this.submitCallback = function()
    {
        $('.ui-tabs-panel').each(function()
        {
            var errors = $(this).find('.wpcf7-not-valid-tip, .wpcf7-not-valid-tip-no-ajax');
            if(errors.length)
            {
                //  the index of the tab
                var tabIndex = $('.ui-tabs-panel').index($(this));
                $(".ui-tabs-nav li:eq(" + tabIndex + ")").addClass('has_errors');
            }
        });
    };
    
    this.form.mouseover(function(){
       obj.submitCallback(); 
    });
        
    
};


//  slideshow on the homepage
PRPL.slideshow = function(div)
{
    $               = jQuery;                   //  because wordpress uses jQuery.noConflict, we need to get back the $
    var obj         = this;                     //  cached "this" for use in anonymous functions
    this.div        = div;                      //  jQuery object of the outer div
    this.nextBtns   = $('.right_arrow');        //  all of the next step buttons
    this.prevBtns   = $('.left_arrow');         //  all of the previous step buttons
    this.currentTab = 0;                        //  pointer for the current tab

    
    //slideshow rotator
	 this.tabs = this.div.find('#slideshow_tabs').tabs(
     {
         fx: {
             opacity: 'toggle'
         },
         //  when a tab is changed, update the currentTab
         select: function(event, ui) 
         {
             if(ui.index)
             {
                 obj.currentTab = ui.index;
             }
             else
             {
                 obj.currentTab = 0;
             }
         }
     }).tabs('rotate', 9000 );
     
     this.tabCount   =   $('.slideshow_wrap .ui-tabs-panel').length;
     
     //  on the click of a next step button, select the next tab
     this.nextBtns.click(function()
     {
         
         obj.tabs.tabs( 'rotate' , null );
         // if it's not currently on the last slide, go forward one slide,
         // otherwise go back to the first slide
         if(obj.currentTab+1 < obj.tabCount)
         {
             obj.tabs.tabs('select', obj.currentTab+1);
         }
         else
         {
             obj.currentTab = 0;
             obj.tabs.tabs('select', obj.currentTab);
         }

         return false;
     });

     //  on the click of a prev step button, select the previous tab
     this.prevBtns.click(function()
     {
        obj.tabs.tabs( 'rotate' , null );
         // if it's not the first slide, go back one. if it IS the first tab go to the last slide
         if(obj.currentTab != 0)
         {
             obj.tabs.tabs('select', obj.currentTab-1);
         }
         else
         {
             obj.tabs.tabs('select', obj.tabCount-1);
         }
         return false;
     });
};

//form eraser
PRPL.formEraserHelper	=	function(fieldObj)
{
	var obj				=	this;					// 	cached "this"
	this.fieldObj		=	fieldObj;				//	the form field jQuery object
	this.initialValue	=	this.fieldObj.val();	//	initial value of the form field
	
	//	when focused on, if the value hasn't been changed, empty it
	this.fieldObj.focus(function()
	{
		if ( $(this).val() == obj.initialValue )
		{
			$(this).val('');
		}
	});

	//	when blurred, if the field is empty, populate it 
	this.fieldObj.blur(function()
	{
		if ( $(this).val() === '' )
		{
			$(this).val(obj.initialValue);
		}
	});
};	//	end formEraserHelper
