$(document).ready(function(){
	if($('.tabs').size()) {
		$('.tabs').tabs({ fx: { opacity: 'toggle', duration: 'medium' } });
	}
	if($('.zebra-table').size()) {
		$("tr:nth-child(even)").addClass("alt");
	}
	if($('.overlay-form input').size() ) {
		$('.overlay-form input:text').each(function()	{ setFocusValues($(this)); });
	}
	
	if($(".videoplayer-extras-buttons").size()) {				
		$("#videoplayer-close-button").click(function(){
			$(".videoplayer-extras").animate({right: "-386px"}, "fast");
			$(".videoplayer-extras-buttons a").toggle();				
		});
		$("#videoplayer-open-button").click(function(){
			$(".videoplayer-extras").animate({right: "0px"}, "fast");			
			$(".videoplayer-extras-buttons a").toggle();			
		});
		$("#videoplayer-close-button").hide();		
	}
	
	/*
	
	Bug when deployed to the development and live site - breaks TV centre pages. is it used to create tabs anywhere else in the site?
	
	if($(".top-tabs").size()) {
		$(".top-tabs").tabs();
	}
	
	if($(".end-tabs").size()) {
		$(".end-tabs").tabs();
	}
	
	*/
		
	if($(".scrollable").size()) {
		var api = $(".scrollable").scrollable({ size: 1, vertical:false,onSeek: function() { setIndex(this); } }).autoscroll(8000).circular({api: true});		
		setIndex(api);
	}
		
});

function setIndex(target) {
	var curPage 		= target.getPageIndex() +1;
	var totalPages 	= target.getSize() -2; //Necessary to subtract 2 as circular plugin adds cloned items to create circular effect.
	if($(".controller span").length) {
		$(".controller span").html(""+curPage+"/"+totalPages+"" );		
	}
}

function setFocusValues(input) {
	input.focus(function(){ if ($(this).val() == '') {$(this).prev().addClass('focus');	}});	
	input.blur(function()	{ if ($(this).val() == '') { $(this).prev().removeClass('focus').removeClass('hastext');}});	
	if ($(input).val() != '') {$(input).prev().addClass('hastext');};	
	input.keydown(function(){ if ($(this).val() != '') {$(this).prev().addClass('hastext');}});
}

// update current percent rate selection
function updateCurrentPercentSum() {
    var intSum = 0;

    // get all percent input fields in the form
    var arrAllPercentInputs = document.getElementById("portCalcFunds").getElementsByTagName("input");

    // loop through all values, adding them up
    for (var i = 0; i < arrAllPercentInputs.length; i++) {
        // add value to sum if it is a number and not empty 
        if ((isNaN(arrAllPercentInputs[i].value)) || (arrAllPercentInputs[i].value == 0)) {
            arrAllPercentInputs[i].value = "";
        }
        else {
            if (arrAllPercentInputs[i].value != "") {
                intSum += parseFloat(arrAllPercentInputs[i].value);
            }
        }
    }

    // update the table note with the new percent sum
    document.getElementById("percentSum").innerHTML = "(currently " + intSum + " %)";

    // mark red if over or under 100%
    if (intSum != 100) {
        document.getElementById("percentSum").style.color = "#a00";
    }
    else {
        document.getElementById("percentSum").style.color = "";
    }

}

// pop up alert if split does not add up to 100%
function alertWrongSum() {
    alert("Please ensure that your total adds up to 100%.");
}

// make sure that "Custom Portfolio" is selected
function setBackToCustom() {
    document.getElementById("ctl00_cplMainContent_ddInvestorType").selectedIndex = 0;
}

// tooltips on calculators
function toggleLayer(whichLayer) {
    var elem, vis;
    if (document.getElementById) // this is the way the standards work
        elem = document.getElementById(whichLayer);
    else if (document.all) // this is the way old msie versions work
        elem = document.all[whichLayer];
    else if (document.layers) // this is the way nn4 works
        elem = document.layers[whichLayer];
    vis = elem.style;
    // if the style.display value is blank we try to figure it out here
    if (vis.display == '' && elem.offsetWidth != undefined && elem.offsetHeight != undefined)
        vis.display = (elem.offsetWidth != 0 && elem.offsetHeight != 0) ? 'block' : 'none';
    vis.display = (vis.display == '' || vis.display == 'block') ? 'none' : 'block';
}


