function dropDown() {
    jQuery('.dropdown a#quick-link-link').click(function() {
        jQuery('.dropdown ul').toggle();
    });             
    jQuery('.dropdown ul li a').click(function() {

        jQuery('.dropdown ul').hide();
    });             

    jQuery(document).bind('click', function(e) {
        var jQueryclicked = jQuery(e.target);
        if (! jQueryclicked.parents().hasClass('dropdown'))
            jQuery(".dropdown ul").hide();
    });
}

function quickLinks() {
    jQuery('.quick-link').change(function() {
        window.location = jQuery('.quick-link').val();
    });             
}

function tabsNav() {
    jQuery('#tab-menu-1').tabs();
    jQuery('#tab-menu-2').tabs();
    jQuery('#tab-menu-3').tabs();
}

function mycarousel_initCallback(carousel) {
    jQuery('.jcarousel-control li a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        jQuery(this).siblings('.selected').removeClass('selected');
        jQuery(this).addClass('selected');
        return false;
    });

    jQuery('.jcarousel-control li a:first').click();

    jQuery('#mycarousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('#mycarousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
    
        // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
    carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
    carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
    carousel.stopAuto();
    }, function() {
    carousel.startAuto();
    });
    
    carousel.clip.bind('change',function(){
    jQuery('.jcarousel-control li a').siblings('.selected').removeClass('selected');
    jQuery(this).addClass('selected');
    });

}

function aFeature() {
    jQuery("#mycarousel").jcarousel({
        wrap: 'both',
        start: 1,
        scroll: 1,
        auto: 5,
        initCallback: mycarousel_initCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null,
        itemVisibleInCallback: {
                onAfterAnimation: function(c, o, i, s) {
                    --i;
                    jQuery('.jcarousel-control li a').removeClass('selected');
                    jQuery('.jcarousel-control li a:eq('+i+')').addClass('selected');
                }
            }
    });
}

function mynews_initCallback(carousel) {

    carousel.scroll(1);

    jQuery('#newscarousel-next').bind('click', function() {                                             
        carousel.next();
        jQuery('#pause-reel').click();
        setTimeout("jQuery('#pause-reel').click()", 1000);
        return false;
    });
    
        // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        jQuery('#pause-reel').click();
        setTimeout("jQuery('#pause-reel').click()", 1000);
    }, function() {
        jQuery('#play-reel').click();
        setTimeout("jQuery('#play-reel').click()", 1000);
    });
    
    jQuery('#play-reel').bind('click', function() {
        jQuery('#play-reel').addClass('selected');
        jQuery('#pause-reel').removeClass('selected');
        carousel.startAuto();
        return false;
    });
    
    jQuery('#pause-reel').bind('click', function() {
        jQuery('#pause-reel').addClass('selected');
        jQuery('#play-reel').removeClass('selected');
        carousel.stopAuto();
        return false;
    });

}


function newsReel() {
    jQuery('#newsreel').jcarousel({
        vertical: true,
        wrap: 'both',
        animation: 'slow',
        auto: 2,
        start: 1,
        easing: 'linear',
        scroll: 1,
        initCallback: mynews_initCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
}


function rhsTabs() {
    var tabContainers = jQuery('div.tabs > div');
    tabContainers.hide().filter(':first').show();
    
    jQuery('div.tabs ul.tabNavigation a').click(function () {
        tabContainers.hide();
        tabContainers.filter(this.hash).show();
        jQuery('div.tabs ul.tabNavigation a').removeClass('selected');
        jQuery(this).addClass('selected');
        return false;
    }).filter(':first').click();
}


function newrhsTabs() {
    jQuery("#tabs1").tabs();                    
    jQuery("#tabs2").tabs();                    
    jQuery("#tabs3").tabs();                    
}

// Function to show/hide content
jQuery(document).ready(function() {
    jQuery('.additional').hide();
	jQuery('.switch').show();
    var containers  = jQuery('.additional');
    jQuery.each(jQuery('.switch'), function(i) {
		var switchText = jQuery(this).html();
		// Switch text link MUST include "show", eg. "Click to show details", "Show map" etc.
		// The 'show' will be replaced with 'hide' on click of link, eg. "Click to hide details", "Hide map" etc.
		// If the link doesn't contain "Show" at all, replace the switch text with "Show details"
		if (switchText.search(/show/i) == -1) {
			jQuery(this).html("Show details");
		}
        jQuery(this).click(function() {
			var switchText = jQuery(this).html();
			// Regex to get any text preceding and following "Show"/"Hide"
			var switchRegex = /(.*)(show|hide)(.*)/i ;
			// Get any text before "Show"
			var switchPrefix = switchText.replace(switchRegex,"$1");
			// Get text following "Show"
			var switchSuffix = switchText.replace(switchRegex,"$3");
			// On click of the switcher link, get the 'switch status' ("show" or "hide")
			var switchStatus = jQuery(this).html().replace(switchRegex,"$2");
			// Change link text, replacing "show" with "hide"
			if(switchStatus == "Show"){switchStatus = "Hide";} else {
				if(switchStatus == "show"){switchStatus = "hide";} else {
					if(switchStatus == "Hide"){switchStatus = "Show";} else {
						if(switchStatus == "hide"){switchStatus = "show";}
					}
				}
			}
			jQuery(this).html(switchPrefix + switchStatus + switchSuffix);
			// Show or hide the contents of the 'additional' container
            jQuery(this).toggleClass("open", jQuery(containers[i]).is(":hidden"));
            jQuery(containers[i]).slideToggle('fast'); 
            return false;
        });
    });

    if (window.location.hash) {
        window.location.hash = window.location.hash;
    }
});

// Do NOT use window.onload - it can't be invoked twice
// Use jQuery(document).ready instead
jQuery(document).ready(function(){
    quickLinks();
    dropDown();
    tabsNav();
    jQuery("#a-feature").css("display","block");
    aFeature();
    newsReel();
    // rhsTabs();
    newrhsTabs();
});

