/* 
** Global Utilities Script
** Please comment so we know where it is used and why it belongs here
** Try to keep light and dynamic
*/

// Clears and resets the top right 'Search Keywords' search box
function checkSearchText() {
	var searchField = jQuery(this);

	if (searchField.val() == "Enter Keyword"){ 
		searchField.val("");
	} else if(searchField.val() == ""){ 	
		searchField.val("Enter Keyword");
	}
}

var Tab = Tab || {};

// Dynamically handles tab click events on any page.
// Tab content areas must have a class that matches tab buttons id
Tab = {
    // Bind a click listener to tab container using flyweights
    attachClick : function () {
        var self = this;
        jQuery( "#holidayTabs" ).unbind().bind( "click", function(e) { 
            var targetElement = jQuery( e.originalTarget || e.srcElement );
            
            // If clicked too deep, navigate back out to the li
           if( targetElement.is('canvas') ||targetElement.is('.cufon') || targetElement.is('a') ||  targetElement.is('span') ) {
                targetElement = targetElement.closest('li');
            }
            if( targetElement.is("li") ) self.change( targetElement.attr('id') );
        });    
    },
    
    // Changes tab by hiding the active and showing the new
	// Tab content must have a class that matches the id of the tab button
    change : function ( targetElementId ) {
        jQuery( 'ul#holidayTabs > li.active' ).removeClass( 'active' );
        jQuery('#' + targetElementId).addClass( "active" );
        jQuery( 'div#tab_content > div:visible' ).hide();
        jQuery( 'div#tab_content > div.' + jQuery('#' + targetElementId).get(0).id ).show();

        jQuery( 'ul#holidayTabs > li.active' ).removeClass( 'active' );
        jQuery('#' + targetElementId).addClass( "active" );
        jQuery( 'div#homeDefault > div:visible' ).hide();
        jQuery( 'div#homeDefault > div.' + jQuery('#' + targetElementId).get(0).id ).show();
        Cufon.replace('#holidayTabs a');
    }
}

jQuery(document).ready(function(){
	// Add a click handler to all tab buttons
	Tab.attachClick();

	// Clear or reset the search text in top right search field
	if (jQuery('#topSearch input:text')[0]){
		jQuery('#topSearch input:text')[0].onfocus = checkSearchText;
		jQuery('#topSearch input:text')[0].onblur = checkSearchText;
	}

	// Image galleries initially hidden, show once carousel is initialised
	jQuery('div.mainImg').show();
	
});

