// Swaps an image
function SwapImage(object, image) {
    var obj = document.getElementById(object);
    obj.src = image;
}
// Clears a textbox of any default "accessibility" text on focus
function clearText(textBoxID,defaultVal){
	if (document.getElementById(textBoxID).value == defaultVal) document.getElementById(textBoxID).value = '';
}

// Checks for enter key being pressed and fires the click event for the specified button
function clickOnEnter(ev, buttonid) {
    var key = (ev && ev.which) ? ev.which : ev.keyCode;
    var bt = document.getElementById(buttonid);

    if (bt && key == 13) {
        bt.click();
        return false;
    }
    return true;
}



/**
 * @author mairead.buchan, lance.cooper
 */
$(document).ready(function() {

    $('body').addClass('js');

    $("#search .searchText").focus(function() {
        if ($(this).attr("value") == "Search our site") {
            $(this).attr("value", "");
        }
    });
    $("#search .searchText").blur(function() {
        if ($(this).attr("value") == "") {
            $(this).attr("value", "Search our site");
        }
    });

    //homepage banner slideshow
    $('#homepageBanner > .bannerNav').clone().prependTo('#bannerSlides');

    $('#homepageBanner > .bannerNav').remove();

    $(function() {
        var tabs = $("#bannerSlides")
			.tabs({ fx: { opacity: 'toggle'} })
			.tabs("rotate", 10000);
    });

    //homepage tabs
    $(".tabbedPanel .tabbedNav li a").click(function() {
        return false;
    });
    $(".tabbedPanel").accessibleTabs({
        tabhead: 'h2',
        fx: 'show',
        fxspeed: null
    });

    // Auto scrolling
    var carouselInterval = 5000;
    function carouselSlide() {
        if ($('#carouselNav li:last-child').hasClass("on")) {
            $('#carouselNav li:last-child').removeClass('on');
            $('#carouselNav li:first-child').addClass('on');
            $('#carousel > ul').animate({ left: 0 }, 500, onAnimationComplete, false);
        } else {
            $('#carouselNav li.on').next('li').children('a').click();
        }
    }
    // Pause Auto scrolling on carousel hover
    var carouselScroll = setInterval(carouselSlide, carouselInterval);
    $('#carousel').hover(function() {
        clearInterval(carouselScroll);
    }, function() {
        carouselScroll = setInterval(carouselSlide, carouselInterval);
        carouselSlide();
    });

    //Landing page clickable list items
    $("#landingBoxes li").click(function() {
        window.location = $(this).find("a").attr("href"); return false;
    });

    //Classnames
    $('table > tr:last-child td,tbody > tr:last-child td').addClass('tableLastRow');
		
		
	// Insert Print page link
	$('ul.pageTools').prepend("<li class=\"print\"><a href=\"javascript:void(0);\" onclick=\"window.print()\" title=\"Print friendly version of this page\">print</a></li>");
	
	// External links in a new windoe
	$("a[href*='http://']:not([href*='"+location.hostname+"'])").click( function() {
		window.open( $(this).attr('href') );
		return false;
	});

   /* =====  JQuery for Flowplayer ===== */
    var varAutoPlay = $("input[name='mpAutoPlay']").val();
    var varMediaPath = $("input[name='mpMediaPath']").val();
    var varMediaType = $("input[name='mpMediaType']").val();

    if ($("#player").length) {

        $("#player").html("")
        $(".smallAudioDimensions").attr("style", "height:25px")
        $(".largeAudioDimensions").attr("style", "height:25px")

        $(".largeVideoDimensions").attr("style", "height:325px")
        $(".smallVideoDimensions").attr("style", "height:205px")
		
		
	  //TO DEPLOY TO LIVE PLEASE COMMENT OUT THE NON_COMMERCIAL CALL AND UNCOMMENT BELOW	
		/*if (varMediaType == "audio") {
		$f("player", "/flowplayer/flowplayer.commercial-3.1.5.swf", {
			key: '#$c8783308425d8832f8f',
			 clip: {
		        autoPlay: (varAutoPlay == "true") ? true : false,
		        autoBuffering: true,
		        url: varMediaPath
		        },
			plugins: {
				controls: {
					url: '/flowplayer/flowplayer.controls-3.1.5.swf',
					left: 0,
					bottom: 0,
					backgroundGradient: 'none',
					bufferColor: '#00aeef',
					buttonColor: '#02678E',
					timeColor: '#00aeef',
					all: false,
					play: true,
					scrubber: true,
					mute: true,
					volume: true,
					time: true,
					tooltips: {
						buttons: true,
						fullscreen: 'Enter fullscreen mode'
					}
				}
			}
		});
	}else {
		$f("player", "/flowplayer/flowplayer.commercial-3.1.5.swf", {
			key: '#$c8783308425d8832f8f',
			 clip: {
		        autoPlay: (varAutoPlay == "true") ? true : false,
		        autoBuffering: true,
		        url: varMediaPath
		        },
			plugins: {
				controls: {
					url: '/flowplayer/flowplayer.controls-3.1.5.swf',
					left: 0,
					bottom: 0,
					backgroundGradient: 'none',
					bufferColor: '#00aeef',
					buttonColor: '#02678E',
					timeColor: '#00aeef',
					all: false,
					play: true,
					scrubber: true,
					mute: true,
					volume: true,
					time: true,
					fullscreen: true,
					tooltips: {
						buttons: true,
						fullscreen: 'Enter fullscreen mode'
					}
				}
			}
		});
	};
	*/
	
	//non-commercial for use in UAT and DEV - license only applies to live URL
	if (varMediaType == "audio") {
		$f("player", "/flowplayer/flowplayer-3.1.5.swf", {
			clip: {
		        autoPlay: (varAutoPlay == "true") ? true : false,
		        autoBuffering: true,
		        url: varMediaPath
		        },
			plugins: {
				controls: {
					url: '/flowplayer/flowplayer.controls-3.1.5.swf',
					left: 0,
					bottom: 0,
					backgroundGradient: 'none',
					bufferColor: '#00aeef',
					buttonColor: '#02678E',
					timeColor: '#00aeef',
					all: false,
					play: true,
					scrubber: true,
					mute: true,
					volume: true,
					time: true,
					tooltips: {
						buttons: true,
						fullscreen: 'Enter fullscreen mode'
					}
				}
			}
		});
	}else {
		$f("player", "/flowplayer/flowplayer-3.1.5.swf", {
			 clip: {
		        autoPlay: (varAutoPlay == "true") ? true : false,
		        autoBuffering: true,
		        url: varMediaPath
		        },
			plugins: {
				controls: {
					url: '/flowplayer/flowplayer.controls-3.1.5.swf',
					left: 0,
					bottom: 0,
					backgroundGradient: 'none',
					bufferColor: '#00aeef',
					buttonColor: '#02678E',
					timeColor: '#00aeef',
					all: false,
					play: true,
					scrubber: true,
					mute: true,
					volume: true,
					time: true,
					fullscreen: true,
					tooltips: {
						buttons: true,
						fullscreen: 'Enter fullscreen mode'
					}
				}
			}
		});
	};

        $(function() {
            var version = flashembed.getVersion();
            var supported = flashembed.isSupported([9, 115]);
            if (!supported) {
                $(".smallAudioDimensions").attr("style", "height:100px")
                $(".largeAudioDimensions").attr("style", "height:75px")
                $("#player").html("<div class='flowPlayerError'>Cannot play media. You do not have the correct version of the flash player. <a class='adobeLink' href='http://get.adobe.com/flashplayer' title='download the latest flash player'>Download flash player.</a></div>");
                $("#player").css("display", "block");
            }
        })

        $(function() {
            $('.adobeLink').click(function() {
                window.open('http://get.adobe.com/flashplayer');
                return false;
            });
        });
    }

})