/* 
 * Resources by Web Molecule
 * Version 0.3
 * October 2010 
 * 
 * Notes:
 *  Required jQuery >= 1.3.2 (probably would would with earlier versions)
 *  Use jQuery() instead of shortcut $() to preserve funcitonality of prototype library
 * 
 * Contents:
 *  Simple jQuery image cycler (fade transition)
 *  Woodland Site dropdown click
 *  Navigation hover fix
 *  Bottom Banners images and titles as links
 *  Remove rounded bottom if a content page with images on the right that go to the bottom
 *  IE Sub-Navigation fix and Opera Header Banner fix
 *  Enquiry form IE fix for label of checkbox
 * 
 */

jQuery(document).ready(function()
{

	/* 
	 * IE Sub-Navigation fix
	 */
	if (jQuery("#nav2 > div > ul > li").length == 0) {
		// There is a second navigation bar with no links, add fix class
		jQuery("#Container > .mainContent").addClass("ieNavFix");
	}
    /*
     * Enquiry form IE fix for label of checkbox
     */
    jQuery("label[for*='chkFurtherOffers']").addClass("IEfix-chkboxEnquiryForm");
	/* 
	 * Opera Fading Banner fix
	 */
	if (jQuery.browser.opera) {
		// alignemnt of banner needs correction
		jQuery("#headerBanner").addClass("operaFix");
	}
	
	/*
	 * Navigation hover fix
	 */
	jQuery("#headNav > ul > li.grey")
	   .live('mouseover', function(e) {
		   jQuery(this).next().addClass("current");
	    })
	    .live('mouseout', function(e) {
	    	//only remove current class if current class is not present on 'contact us' tab
	    	if ( ! jQuery("#headNav > ul > li.grey").hasClass("current")) {
	    		jQuery(this).next().removeClass("current");
	    	}
	    });
	//add current class if the contact us button is current selection
	if (jQuery("#headNav > ul > li.grey").hasClass("current")) {
		jQuery("#headNav > ul > li.tabEnd").addClass("current");
	}
	
	/*
	 * Bottom Banners images and title must link to same page as button below them
	 */
	jQuery(".cremBottomBanner img, .cremBottomBanner .top").live("click", function(e) {
		//left click only
		if (!e.which || e.which == 1) {
			jQuery(this).parent().contents(".bannerButton").click();
		}
	});

    /*
     * Lef navigation extend to page bottom fix
     */
	jQuery(window).load(function() {
		if (jQuery(".leftColNav").length > 0)
		{
			//set height to that of parent container
			var height = jQuery(".contentBox").height();
			jQuery(".leftColNav").css("height", height);
			
			//only add bottom border to left nav if images dont fit snugly
			if (jQuery(".cremContentImages img").length == 2)
			{
				height = jQuery(".contentBox").height(); 
				if (jQuery(".cremContentImages").height() >= height)
				{
					//right images height is at least equal to content box height, so return BEFORE adding rounded border
					return;
				}
			}
	
			//add rounded corner
			var border = "<div class=\"bottomRounded\"></div>";
			jQuery(".leftColNav").after(border);
		}
	});
    
    /*
     * Remove rounded bottom if a content page with images on the right that go to the bottom
     * Perform on Window Load (after images have loaded)
     */
	jQuery(window).load(function() {
		if (jQuery(".bottomContent .AdBox").length == 0 && jQuery(".cremContentImages img").length == 2) {
			var height = jQuery(".contentBox").height(); 
			if (jQuery(".cremContentImages").height() < height)
			{				
				//display on content pages that are longer than right images
				jQuery(".bottom-bg").addClass("visible");
			}
		} else {
			//display for pages with banners
			jQuery(".bottom-bg").addClass("visible");
		}
	});

	/* 
	 * Simple jQuery image cycler (fade transition)
	 */
    if (jQuery("#headerBanner").length > 0) {
	    var prevImage=-1;
	    var currImage=0;
	    var imageWaiting = false;
	    //hide images and get number of <li> elements
	    var imageCount = jQuery("#headerBanner li").hide().size();
	    //show first one
	    jQuery("#headerBanner li:eq("+currImage+")").fadeIn("slow");
	    if ( ! imageWaiting ) {
	    	imageWaiting = true;
		    setInterval(function()
		        {
		    		imageWaiting = false;
		            prevImage = currImage;
		            currImage = ++currImage%(imageCount); 
		            jQuery("#headerBanner li:eq("+prevImage+")").fadeOut("slow");            
		            jQuery("#headerBanner li:eq("+currImage+")").fadeIn("slow");    
		
		        },5000); //time in milliseconds
	    }
    }
	
	/*
	 * Woodland Site dropdown click / select
	 */
    if (jQuery("#siteChoiceContent").length > 0) {
    	
    	//redirect function
    	function redirectSiteChoice() {
		    //get url to redirect to and check it
		    var url = jQuery("#siteChoiceContent select").val();
		    
		    //nothing selected
		    if (url == "")
		    {
		    	//show dropdown
		    	jQuery("#siteChoiceContent select").focus();
		    	return;
		    }
		    
		    if (url == null || url.length == 0) {
		    	//no url found
		    	alert("Unable to redirect you.");
		    	return;
		    }
		    //redirect
		    top.window.location=url;
    	}

    	var clicknum = 0;
    	//set dropdown selection to perform button click
    	jQuery("#siteChoiceContent select").live("change", function(event){
		    clicknum = 0;
		    redirectSiteChoice();
    	});
    	
    	//some borwsers (IE) don't fire the change event, look for two clicks
    	jQuery("#siteChoiceContent select").live("click", function(){
    		//left click only
    		if (!e.which || e.which == 1) {
	    		clicknum++;
	            if(clicknum == 2){
	        		redirectSiteChoice();
	                clicknum = 0;
	            }
    		}
    	});
    }
});
