/********************************************************************************************
TVNZ Javascript Library functions list:

jsTVNZ.initialize();
		


********************************************************************************************/

var jsTVNZ = window.jsTVNZ || {};
var newsPanel = new Object();
var slideAnimating = false;
var slideClicked = false;
var stopAnimationLoop = false;
var loopCount = 1;
var dc_tile = 0;



var rootURL = "";
//var rootURL = "http://images.tvnz.co.nz/tvnz_site_images/";

// Makes the init run at page load
$(window).load(function() {
	jsTVNZ.initialize();
});

// the Initialize function, which runs on load, all sub-functions are called in here.
jsTVNZ.initialize = function() {
	// checks to see if the slide Panel(s) Exsists, if it does, initialize them.
	if ($(".slidePanel").is(":visible")) {
		jsTVNZ.Common().initSlidePanel();
	}
	

	
	// Below rips out all the PNG images in IE6, and uses the filter in an IE6 only stylesheet
	if ($.browser.msie && $.browser.version <= 6) {
		jsTVNZ.Common().fixUpMyIE6();
	}
	
	
	
}

// TVNZ Common functions, that can be re-called at any point.
jsTVNZ.Common = function() {

	
	
	
	return {
		animateLoop : animateLoop,
	
		
		fixUpMyIE6 : fixUpMyIE6
	}
	
	
	
}


function displayLayer( id, value )
{
    if ( document.getElementById )
    {
        document.getElementById( id ).style.display = value;
    }
    else if ( document.all )
    {
        document.all[ id ].style.display = value;
    }
}

function hide( id )
{
    displayLayer( id, 'none' );
}

function show( id )
{
    displayLayer( id, 'block' );
}


/*
 * This function is used update the slide panel. 
 */

function slidePanel(){
	
	var maxItems = 3;
	var rotationTime = 5; // time in seconds
	var maxChars = 100; //max number of words appear in the summary
	var currentslideIndex = 0;
	var slideElements = $('#slidePanel .slidePanelNav .slide');;
	var slideMiniElements = $('#slidePanel .slidePanelNav .slideMini');
	var slideImages = $('#slidePanel .slidePanelContent a');
	var slideLinks = $('#slidePanel .slidePanelContent .slideLink');
	var arrow = $('.selectedArrow');
	var arrowYPos = 0;
	var slideHeight = $('.slide').height();
	var slideMiniHeight = $('.slideMini').height();
	var timer;
	var pre;
	
	
	performRotation();
	setTimer();
	
	$('.slide, .slidePanelContent').mouseover(function() {
		window.clearInterval(timer);
	});
	
	$('.slide, .slidePanelContent').mouseout(function() {
		setTimer();
	});
	
	$('.slideMini').click(function(){
		
		var clickedId = $(this);
		$.each( slideMiniElements, function(i, l){
			if($(this).attr("id") == $(clickedId).attr("id")){
				currentslideIndex = i;
				performRotation();		
			}
		});
		
		return false;
	});
	
	
	function slidePanelInit(){
		$(slideMiniElements[0]).css("display","block");
		$(slideMiniElements[0]).hide();
		
		//bind the links to the arrow
		$.each( arrow, function(i, l){
			$(this).click(function(){
				var link = $(slideImages[i]).attr("href");
				window.location.replace(link);
			});
		});
		
		

	}
	
	//update the slide panel after a set interval
	function performRotation(){

		$.each( slideElements, function(i, l){
	
			
		});
		
		if(pre == null){
			pre = slideImages[0];
			$(pre).fadeIn();
		}else{
			$(pre).hide();
			var getimg = "url("+$(pre).find('img').attr('src')+")";
			$('.slidePanelContent').css("background",getimg);
			$(slideImages[currentslideIndex]).fadeIn(800);
			pre = slideImages[currentslideIndex];
		}
		currentslideIndex++;
		if(currentslideIndex == maxItems){ currentslideIndex = 0; }
	}
	
	function setTimer(){
		timer = window.setInterval(function() {
			performRotation();
		}, rotationTime * 1000);
	}
	
}





