/*
* CQU Main Javascript Library
* Squiz Canberra
* August 2011
* Global use scripts and functions.
* This file assumes that jQuery framework has already been loaded.
* Use 'jQuery' instead of '$' to avoid other framework conflicts e.g. prototype framework uses '$'.
* 
* Modification history:
* 
* 2011-08-17: Squiz Canberra
*             - File creation and initial object/function creation.
*
*/

jQuery('html').removeClass('no-js').addClass('js');

var GlobalScripts = {
    
    /**
     * Generic form controls.
     */
    genericFormControls: function()
    {
        
        var self = this;
        
        // Replace any default text in input text boxes if marked with related class.
        jQuery("input[type=text].replace-default").focus(function() {
                if( this.value == this.defaultValue ) {
                    this.value = "";
                }
        }).blur(function() {
            if( !this.value.length ) {
                this.value = this.defaultValue;
            }
        });
        
    }, // genericFormControls.
    
    /**
     * Generic content manipulation.
     */
    contentManipulation: function()
    {
        
        var self = this;
        
        // Apply tooltip plugin functionality to elements marked as tooltips.
        jQuery(".tool-tip").tipTip();
        
        // Global nav is not dynamic generated so we need to apply active class based on breadcrumbs.
        // Assumes glonav items are top level and their data attributes are populated with asset id's.
        var breadId = jQuery("#breadcrumbs li:eq(1)").attr('data-menuid');
        jQuery('#global-nav > ul > li').each( function(i) {
            var navId = jQuery(this).attr('data-menuid');
            if(navId !== undefined && navId === breadId){
                jQuery(this).addClass('active');
            }
        }); // End each.
        
        // Make sure page content left (when with right col) always matches heighest left or right col.
        // This will make sure CSS dashed borders always sits under curved border.
        var $leftNav = jQuery('#left-nav');
        var $pageContents = jQuery('#page-content-left');
        var $pageContentsRight = jQuery('#page-content-right');
        var objArray = [$leftNav, $pageContents, $pageContentsRight];
        if($leftNav.length && $pageContents.length && $pageContentsRight.length){
            var i;
            for(i=0; i < objArray.length; i++){
                if($pageContents.height() < objArray[i].height()){
                    $pageContents.height(objArray[i].height() - (jQuery('#inside-banner').height()*1));
                }
            }
        }
        var $withBorderAndShadow = jQuery('#with-borderandshadow');
        if($leftNav.height() > $withBorderAndShadow.height()){
            $withBorderAndShadow.height($leftNav.height());
        }
        
        // Inside pages (with right col) banner controls, prepend from body content to first in the stacking order.
        var $insideBanner = jQuery('#inside-banner');
        if($insideBanner.length){
            jQuery('#page-content-left').wrap('<div id="content-left-with-banner" />');
            jQuery('#content-left-with-banner').prepend($insideBanner);
        }

        // Apply ellipses to all link lext between home and last three
        // links if the total breadcrumb text char count is above wrapping point.
        var $breadcrumbs = jQuery('#breadcrumbs li');
        var linkText = '';
        $breadcrumbs.each( function(i) {
            var $self = jQuery(this);
            linkText += $self.text();
            if(linkText.length > 195){
                $breadcrumbs.slice(1,-3).find('a').text('...');
            }
        });

        // Submit search query if available after loading find a degree remote content.
        var urlHash = this.getUrlVars();
        var $remoteExpertsPage = jQuery('#pageid-4112');
        var $expertSearchForm = $remoteExpertsPage.find('form#search');
        if($remoteExpertsPage.length && $expertSearchForm.length && !jQuery('#positonsboxes').length && urlHash['keyword']){
             $expertSearchForm.find('input[name=keyword]').val(urlHash['keyword']);
             $expertSearchForm.submit();
        }
        
    }, // contentManipulation.
    
    /**
    * Set up tab content where required.
    */
    tabContent: function() {
        jQuery('#page-content .tab-nav li a').click( function(e) {
            e.preventDefault();
            var $self = jQuery(this);
            var $parentContainer = $self.parents('.tab-nav').parent();
            if($self.parent().hasClass('current')){
                return false;
            } else {
                $parentContainer.find('.current').removeClass('current');
                $self.parent().addClass('current');
                $parentContainer.find('.tab-content').addClass('remove-from-view');
                $parentContainer.find($self.attr('href')).removeClass('remove-from-view');
            }
        });

        var urlHash = location.hash + '';
        if(urlHash.length && jQuery(urlHash).length){
            jQuery('#tabbed-nav li, #tabbed-content .tab-content').removeClass('current');
            jQuery('#tabbed-content .tab-content').addClass('remove-from-view');
            jQuery('#tabbed-nav a[href="'+urlHash+'"]').parent().addClass('current');
            jQuery(urlHash).addClass('current').removeClass('remove-from-view');        
        }

    }, // tabContent.
    
    /**
    * Call rotator plugin into action - see plugins.js
    */
    rotatingBanners: function () {
        
        var $banners = jQuery("#page-content .rotating-banner");
        var $bannerSelectors = $banners.find('.thumbnails li');
        
        $banners.wtRotator({
            width:700,
            height:320,
            button_width:18,
            button_height:18,
            button_margin:5,
            auto_start:true,
            delay:5000,
            transition:"fade",
            transition_speed:2000,
            auto_center:false,
            cpanel_position:"inside",
            cpanel_align:"BL",
            timer_align:"top",
            display_thumbs:true,
            display_dbuttons:true,
            display_playbutton:true,
            display_numbers:true,
            display_timer:true,
            mouseover_pause:false,
            cpanel_mouseover:false,
            text_mouseover:false,
            text_effect:"none",
            text_sync:false,
            tooltip_type:"none",
            shuffle:false			
        }); // End wtRotator.
        
        // Apply our own classes to the thumbnail LI selectors.
        $bannerSelectors.each( function(i) {
            jQuery(this).addClass('selector').addClass('selector'+i);
        });
        
    }, // rotatingBanners.

    /**
     * GLOBAL USE
     * Read a page's GET URL variables and return them as an associative array.
     * Example - var urlHash = getUrlVars(); alert(urlHash['hello']);.
     */
    getUrlVars: function() {
        var vars = [], hash, i;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    } // End getUrlVars.
    
}; // End GlobalScripts.

jQuery(document).ready( function(){
    
    GlobalScripts.genericFormControls();
    GlobalScripts.contentManipulation();
    GlobalScripts.tabContent();
    GlobalScripts.rotatingBanners();

});

