/**
 * JSON (JavaScript Class) for executing generic sites scripts.
 *
 * Scott Hall [squiz.net.au]
 * July 2010
 *
 * Modification history:
 * 
 * 2010-07-22: Scott Hall
 *             - File Creation.
 * 2011-01-12: Scott Hall
 *             - Corrected bug in indicateLeftNavSubs. Was removing classes from menusubs instead of 'self'.
 * 2011-10-11: Andrew Dunbar
 *             - Updated 'contentfixes' jQuery code for related-content styles
 */

var GlobalGenericSites = {
    
    init: function() {
        
        var self = this;
        self.documentHasLoaded();
        
    }, // End init.
    
    /**
     * Script to execute on page load.
     */
    documentHasLoaded: function(){
        
        var self = this;
        
        jQuery(document).ready( function() {
                
            self.indicateLeftNavSubs();
            self.attachTextResizeEvents();
            self.contentManipulation();
            
        }); // End jQuery document ready.
        
    }, // End documentHasLoaded.
    
    /**
     * Detects presence of direct sub uls of top level li's
     * and adds indicator arrow class, will hide/show depending on level of view.
     */
    indicateLeftNavSubs: function(){
        
        var self = this;
        
        var $menuItemSubs = jQuery('#nav-local ul > li > ul');
        
        $menuItemSubs.each( function(index) {
            var $self = jQuery(this);
            var $parentLinks = $self.prev();
            if ($parentLinks.hasClass('current') || $parentLinks.hasClass('hierarchy')) {
                $self.removeClass('remove-from-view');
                // Some legacy stuff from the main ECU site we need to help with current/heirarchy view.
                $parentLinks.addClass("parent-with-subs");
            } else {
                $parentLinks.addClass('hasSubs').addClass('parent-with-subs');
                $self.addClass('remove-from-view');
            } 
        }); // End each.
        
    }, // End indicateLeftNavSubs.

    /**
     * Modified version of main site text resize as that version requires
     * the cookie plugin to be present and we dont need 80% of the preferences code.
     */
     
    attachTextResizeEvents: function(){

        var fontSizes = ["0.85em","0.95em","1.0em","1.1em","1.2em"];
        var fontIndex = 2; // 1.0em
        var fontArrayLength = fontSizes.length;
        
        // Bind click events to resize links.
        jQuery('#site-tools .decrease-font-size').click( function(e){
            if(fontIndex > 0){
                fontIndex--;
                jQuery('html').css('font-size', fontSizes[fontIndex]);
            }
            e.preventDefault();
        }); // End click.
        
        jQuery('#site-tools .increase-font-size').click( function(e){
            if(fontIndex < fontArrayLength){
                fontIndex++;
                jQuery('html').css('font-size', fontSizes[fontIndex]);
            }
            e.preventDefault();
        }); // End click.
    
    }, // End attachTextResizeEvents.
    
    /**
     * Do all content manipulation here, assumes call after document loaded.
     */
    contentManipulation: function() {

 /* Remove by AD following changes to main ECU 'content.css' file for 
        //var $relatedContentWrapped = jQuery('#related-content-wrapped');
        //var $floatedImageArea = jQuery('#floated-image-area');
        //var $floatedImageAreaImage = $floatedImageArea.find('img:first');
        //var $mainContent = jQuery('#content-main');
     
        // Dynamically pad first found h2 for pages with related content.
        if($relatedContentWrapped.length) {
            $mainContent.find('h2:first').addClass("content-title").append('<!-- jQuery added content-title class -->');
        }
        
        // Fix the width of the floated image area container based on image size.
        if($floatedImageAreaImage.length){
            var imgWidth = $floatedImageAreaImage.width();
            $floatedImageArea.width(imgWidth);
        }
        
        // Dynamically pull wrapped related content column up
        // by the height of the main content h2 if no floated image exists.
        if($relatedContentWrapped.length && !$floatedImageAreaImage.length){
            $relatedContentWrapped.css("margin-top", "-22px");
            $relatedContentWrapped.append('<!-- jQuery changed the margin of related-content-wrapped -22px -->');
        }
*/  

		// fix the width of the floated image area container based on image size for '601'
		if (jQuery("#floated-image-area img").length !== 0) {
			var imgWidth = jQuery("#floated-image-area img:first-child").width();
			jQuery("#floated-image-area").width(imgWidth);
		}
		
		// remove 'related-content' '453' div if nothing in it */
		if (jQuery("#related-content").children().size() === 0) {  
		  jQuery("#related-content").remove();
		  // set the width of the content back to normal
		  jQuery("#content-with-related").width("697px");
		}
		
		// remove 'related-content' '601' div if nothing in it */
		if (jQuery("#related-content-wrapped").children().size() === 0) {
		  jQuery("#related-content-wrapped").remove();
		}   
		
		// remove '601' floated image from content if nothing in it */
		if (jQuery("#floated-image-area").children().size() === 0) {
		  jQuery("#floated-image-area").remove();
		}
		
		// move h2 heading outside of content structure to allow for longer headings,
		// effectively means related-content now sits with actual content better.
		if (jQuery("#content-main #content-with-related h2").length > 0) {
			// move h2 to correct location for '453'
			jQuery('#skip-to-content').after(jQuery("#content-main #content-with-related h2"));
		} else if (jQuery("#content-main h2").not("#related-content-wrapped h2").length > 0) {
			// move h2 to correct location for '601'
			jQuery('#skip-to-content').after(jQuery("#content-main h2").not("#related-content-wrapped h2, #full-story-contents h2, #portalLoginContainer h2")); 
		}
		
		// add 'content-title' to h2 if not already attached for both '453' and '601'
		if (!jQuery("#content-main h2:first").hasClass("content-title")) {
			jQuery("#content-main h2:first").addClass("content-title");
			
		}

    } // End contentManipulation.

}; // End GlobalGenericSites.

// Initialise generic sites scripts.
GlobalGenericSites.init();

