$(document).ready(function() {

    if ($(".nia-tv .trigger").length) {
        var trigger = $(".nia-tv .trigger");
        var closeme = $(".tv-menu .tv-menu-close a");

        $(trigger).click(function(e) {
            $(".tv-menu").animate({
                right: "0px"
            }, 500);

            e.preventDefault();
        });

        $(closeme).click(function() {
            $(".tv-menu").animate({
                right: "-429px"
            }, 500);
        });
    }

    if ($(".scrollable").length) {
        var api = $(".scrollable").scrollable({ size: 1, vertical: false, clickable: 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+"" );
	}	
	var adj = -($(target.getItems().eq(target.getIndex() + 1)).position().left);																																			 
	adjustIndexPosition($(target.getItems()).parent(), adj);
}

function adjustIndexPosition(obj, pos) {
    // Fix for IE bug. To get the position we want, subtract 1 from the position we want (only when the position is wrong!!!)
    // Otherwise, setting left to (e.g.) -510 will just set it to -509 when the bug occurs
    // Why? I have no idea. Confirmed this as a bug in IE, not in jQuery. Setting style to pos directly through JavaScript still results in incorrect value   
		if (obj.position().left != pos) {
        obj.css("left", pos - 1);
    }    
}


// 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';
}
