/*  ##########################################################################
'	##########################################################################

' 	scripts.js

'	Author(s)
' 	David Dekker, DD, david.dekker@akqa.com
'	Nigel Warson-Hill, NWH
'	Caroline Clifford, CC
'   Niall Johnson, NJ

' 	Copyright 2008 AKQA.  All rights reserved.

'	Version 1.02
'	Created: 02/10/2006
'	Last modified: 29/09/2008

'	Versions:
' 	1.00 - Initial version - DD
'	1.01 - Removed unused function "activateProgrammeChange" as no longer needed - NWH
'	1.02 - Added getElementsByClassName for use with the paging.js
		   Added querystring function for use with the paging.js
		   Commented out these functions as paging function is behaving pecululary slow
'   1.03 - Removed the following function as they are no longer used site wide or have been
           replaced with another JavaScript library - NJ :
           function popupwindow
           function detectPlugins
           function showFloater
           function activateProgramme
           function nextInmate
           function detectPlugins
           function buildplayer
           function updateVideo
           function initialiseTabs
           function toggleTabs

'   2.00 - Added the following functions - NJ:
           function renderPopup
           function mixSelection
           function renderScroller

	##########################################################################
	##########################################################################
*/

/***********************************************
	Code runs as soon as this JS file is loaded
	Code adds the class 'jsEnabled' to the HTML tag
	--------------------------------------------
	Add the class 'jsEnabled' to the HTML tag on a page.
	Runs as soon as this JS file is loaded, as the html tag
	should be the first tag available and doesn't require the
	documentent to be DOM ready.
***********************************************/
if (document.documentElement && document.getElementById) {
    var docClass = document.documentElement.className != null ? document.documentElement.className : document.documentElement.getAttribute('class');
    docClass =  docClass !='' ? docClass + ' jsEnabled' : 'jsEnabled';
    document.documentElement.setAttribute('class',docClass);
    document.documentElement.className = docClass; //For IE
}

function hideDiv(pass) {
	var divs = document.getElementsByTagName("div");
	for(i=0;i<divs.length;i++){
		if(divs[i].id.match(pass)){//if they are 'see' divs
			if (document.getElementById) // DOM3 = IE5, NS6
				divs[i].style.display = "none";// show/hide
			else
				if (document.layers) // Netscape 4
					document.layers[divs[i]].display = "none";
				else // IE 4
					document.all.hideshow.divs[i].visibility = "hidden";
		}
	}
}

function showDiv(pass) {
	var divs = document.getElementsByTagName("div");
	for(i=0;i<divs.length;i++){
		if(divs[i].id.match(pass)){
			if (document.getElementById)
				divs[i].style.display = "block";
			else
				if (document.layers) // Netscape 4
					document.layers[divs[i]].display = "block";
				else // IE 4
					document.all.hideshow.divs[i].visibility = "visible";
		}
	}
}

// Channel Selection
var channelIdOld = "channelOne";
function activateChannel(id) {
	if (id == null || id == "") return false;
	if (channelIdOld != null) hideDiv(channelIdOld);
	showDiv("channel"+id);
	channelIdOld = "channel"+id;
	sIFRReplace();
	return false;
}

function getElementsByClassName(elm, tag, className){
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

//rollover
function rollover(img,state){
	var s = String(img.src);
	if (s.indexOf("_on.jpg")<=0) {
		if (state==0 && s.lastIndexOf("_hover.")>0){
			img.src = s.substring(0,s.lastIndexOf("_hover."))+"_off.jpg";
		} else if (state==1 && s.lastIndexOf("_off.")>0) {
			img.src = s.substring(0,s.lastIndexOf("_off."))+"_hover.jpg";
		}
	}
}
		
//FAQs page - toggle FAQs//
function contractArticles(id){
    if(!document.getElementById) return;//check method exists
    if(!document.getElementsByTagName) return;//check method exists
    if(!document.getElementById(id)) return;//check method exists
    var items = document.getElementById(id).getElementsByTagName('div');//find list of articles
    for (var i=0;i<items.length;i++){ //for each div...
        if(items[i].className == 'faqText'){ //... with a class of faqText
            //items[i].childNodes[1].childNodes[0].className='';//...reset class on link
            var bodyTxt = items[i].getElementsByTagName('div');//...get all divs in selected article
            for (var j=0;j<bodyTxt.length;j++){
                if (bodyTxt[j].className == 'faqBody'){//...if the div has a class of articleBody
                    bodyTxt[j].style.display='none';//hide it
                }
            }
        }
        if(items[i].className == 'faqButton'){
            items[i].firstChild.className = 'off';
        }
    }
}    

 

/***********************************************
	function renderPopup (offsetY, offsetX, target, source, cssSource)
	Render an information box popup/overlay 
	--------------------------------------------
	Should be called from the onclick event of an element.
	Note: Requires the JQuery library.
***********************************************/
function renderPopup (offsetY, offsetX, target, source, cssSource){	
    $('div.infoBox').css({left:'-100em'});
    var infoBox = $(link).attr('href').split('#')[1];
    var sourceFileName = source.split(' ')[0];
    
	if ((cssSource != '' && cssSource !=null) && $('head link[href=' + cssSource + ']').length == 0){
		$('head').append('<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href="' + cssSource + '" />')
	}
    
    // Only if the content hasn't already been loaded
    if ($(target).children().length == 0 && source != ''){
		// Load and inject content in DOM
		$(target).load(source, function(){
		
			// What to run once the source content has fully loaded into the target            
           
			// Only for the entertainment mix popup
			if (target.indexOf('div#entsMixInfo')!=-1){
				renderScroller();
			}
            
			// Position the injected content relative to the calling link
			var infoBoxX = $(link).position().left;
			var infoBoxY = $(link).position().top;    
			$('div#' + infoBox).css({top:infoBoxY + offsetY, left:infoBoxX - offsetX});            
        
		});		   
    } else {       
        // Position the injected content relative to the calling link
        var infoBoxX = $(link).position().left;
        var infoBoxY = $(link).position().top;    
        $('div#' + infoBox).css({top:infoBoxY + offsetY, left:infoBoxX - offsetX});        
    }
    
    // For all popups
	if ($(target).nextAll('a.close').length == 0){
		$(target).after('<a href=\"#close\" class=\"close\"><strong>Close X</strong></a>');                
		$('div#' + infoBox + ' a.close').click(function(){
		   $ ('div#' + infoBox).css({left:'-100em'});
			return false;
		});
	}
}

/***********************************************
	function mixSelection(element)
	--------------------------------------------
	Note: Requires the JQuery library.
***********************************************/
function mixSelection(element){
    selectedDiv = element.href.split('#')[1];
    mixDivs = $('div#varietyMix, div#newsMix, div#musicMix, div#knowledgeMix, div#kidsMix, div#styleMix');
    $(mixDivs).each(function (i) {
        if ($(this).attr('id') == selectedDiv) {
            $(this).show();
        } else {
            $(this).hide();
        }
    });
}

/***********************************************
	function renderScroller()
	Takes an HTML list and converts it into a scrolling carousel. 
	--------------------------------------------
	The renderScroller function needs to be called once the page has fully loaded
	and the script has access to the DOM.
	Note: Requires the JQuery library.
***********************************************/
function renderScroller(){
    var $scrollers = $('div.scroll');
    
    $scrollers.each(function (i){
    var ulWidth = 0;
        var $list = $(this).find('ul');
        var $listItems = $list.find('li');                
    $listItems.each(function (i){
            ulWidth += parseInt($(this).css('width'));
    });
        $list.css({width:ulWidth});
    });
    
    $('img.scrollLeft').hover(
        function(){
            var distance = $(this).parent().find('div.scroll ul').css('width');
            var duration = parseInt(distance)*6;
            $(this).parent().find('div.scroll').animate(
                {scrollLeft: '-'+distance},
                {duration: duration}
            );
        },
        function() {
            $(this).parent().find('div.scroll').stop();    
        }
    );
    
    $('img.scrollRight').hover(
        function(){
            var distance = $(this).parent().find('div.scroll ul').css('width');
            var duration = parseInt(distance)*5;
            $(this).parent().find('div.scroll').animate(
                {scrollLeft: distance},
                {duration: duration}
            );
        },
        function() {
            $(this).parent().find('div.scroll').stop();    
        }
    );
}