$(document).ready(function() {
    //**********************************************************************
    //global header start 
    //**********************************************************************
    $("#nav li ul li ul li, #anav li ul li ul li").hover(
        function() {
            $(this).children("ul").show();
        }, function() {
            $(this).children("ul").hide();
        }
	); //hover
    $("#nav li ul li, #anav li ul li").hover(
        function() {
            $(this).children("ul").show();
        }, function() {
            $(this).children("ul").hide();
        }
	); //hover

    //head search specs
    $("#search").val("Search our site...").css("font-style", "italic");
    $(".assistance").css("display", "none");
    $("#search").focus(function() {
        $(this).val("").css("font-style", "normal");
        $(".assistance").slideDown("fast");
    });
    $("#search").blur(function() {
        $(".assistance").css("display", "none");
    });
    //End head search specs

    //right sliders (accordian type function)
    $('.aitem h2').addClass('accon');

    $('.drophandle').bind('Open', function() {
        $(this).css('position', 'relative');
        $('.drop', $(this)).css('display', 'block');
    }).bind('Select', function(event, itemText, itemValue) {
    	//alert($(".drop ul li:visible").length);
        $('span', $(this)).html(itemText);
        $('input:first', $(this)).val(itemValue);
    }).bind('Close', function() {
        $(this).css('position', '');
        $('.drop', $(this)).css('display', 'none');
    }).bind('Reset', function() {
        $('input:first', $(this)).val('');
        $('span', $(this)).each(function(){
        	var nextElement = $(this).next();
        	if ($(nextElement).attr("class") == "drop dropselect") {
        		$(this).html($(nextElement).find("ul li:eq(0) div").html());
        	}
        });
    }).click(function(event) {
        if ($('.drop', $(this)).css('display') == "none") {
            $(this).trigger('Open');
        }
        else {
            $(this).trigger('Close');
        }
    }).trigger('Close');

    $('.drophandle').each(function(i) {
        var drophandle = $(this);
        $('li', $(this)).each(function(i) {
            $(this).click(function() {
                $(drophandle).trigger('Select', [$('div', $(this)).html(), $('input', $(this)).val()])
            });
        });
    });

    $(".subnavon").corner("left 3px");
    $(".ccontainr").corner("3px");
    $(".curtaincontent").corner("3px");
    $(".mfitems li").corner("3px");
    $(".saitems li").corner("3px");

    $(".afrightr").corner("3px");
    $(".afright").corner("3px");

    $(".consent").click(function() {
        $.fn.colorbox({ width: "840px", height: "440px", inline: true, href: "#consent", overlayClose: false },
        	function(){ $(document).unbind('keydown.cbox_close'); }
        );
    });
    //**********************************************************************
    //global header end
    //**********************************************************************

    //**********************************************************************
    //global campaign.asp start
    //**********************************************************************
    //Load Contact panels infrastructure
    $('.mfn li span').each(function(i) {
        $(this).click(function() {
            selectHPItem(i);
            $(this).blur();
            $(".mfn li span").blur();
        });
    });
    $('.mfn li span:first').click();
    //END Load Contact panels infrastructure

    $(".able:eq(0) tr:odd").css("background", "#eaefe4");
    $(".able:eq(1) tr:even").css("background", "#eaefe4");
    $(".able:eq(2) tr:odd").css("background", "#eaefe4");

    if ($("#player").length > 0) {
        //load flash
        flowplayer("player", "/flash/flowplayer-3.1.4.swf", {
            clip: {
                // these two configuration variables does the trick 
                autoPlay: false,
                autoBuffering: true // <- do not place a comma here   
            }
        });
    }

    //**********************************************************************
    //global educational-overview.asp start
    //**********************************************************************
    var libIndex = 0;
    $("#libback").click(function() {
        if (libIndex != (-1 * ($('#highlights li img').length) + 3)) {
            libIndex--;
            $("#highlights").animate({ marginLeft: (libIndex * 150) + "px" }, { queue: false, duration: 1000 });
        }
    });
    $("#libnext").click(function() {
        if (libIndex != 0) {
            libIndex++;
            $("#highlights").animate({ marginLeft: (libIndex * 150) + "px" }, { queue: false, duration: 1000 });
        }
    });
    //**********************************************************************
    //global educational-overview.asp end
    //**********************************************************************

    //**********************************************************************
    //global global-contact.asp start
    //**********************************************************************
    $('.quicknav li span').each(function(i) {
        $(this).click(function() {
            selectContactItem(i);
            $(this).blur();
            $(".quicknav li span").blur();
        });
    });
    $('.quicknav li span:first').click();
    //**********************************************************************
    //global global-contact.asp end
    //**********************************************************************

    //**********************************************************************
    //global index.asp start
    //**********************************************************************
    $(".news").each(function(i) {
        $(this).click(function() {
            $.fn.colorbox({ width: "840px", height: "540px", inline: true, href: "#articles", overlayClose: false },
            	function(){ $(document).unbind('keydown.cbox_close'); });
            newsItems(i);
        });
    });
    //END news pop up specs

    //news views
    $('#aright li a').each(
        function(i) {
        $(this).click(function() {
            newsItems(i);
        });
    });
    //**********************************************************************
    //global index.asp end
    //**********************************************************************

    //**********************************************************************
    //global regional-careers-results.asp end
    //**********************************************************************
    $("#jresults li:last").css({
        margin: "0 -9px 0 -9px",
        padding: "0 15px",
        border: "0"
    });
    //**********************************************************************
    //global regional-careers-results.asp end
    //**********************************************************************

    //**********************************************************************
    //global regional-home.asp start
    //**********************************************************************
    if ($('#carousel').length > 0) {
        //Carousel syntax
        $('#carousel').roundabout({
            btnNext: '#next',
            btnPrev: '#prev',
            shape: 'lazySusan',
            minOpacity: 0
        });

        $('#carousel li').each(function(i) {
            $(this).focus(function() {
                $('.content').fadeOut("slow");
                $('.content:eq(' + i + ')').fadeIn("slow");
            });
        });

        $('#carousel li:first').focus();

        $("#next").click(function() {
            $(this).blur();
        });
        $("#prev").click(function() {
            $(this).blur();
        });
    }
    //END Carousel syntax

    //bottom panels infrastructure
    $('#rnn li span').each(function(i) {
        $(this).click(function() {
            selectRegionalItem(i);
            $(this).blur();
            $("#rnn li span").blur();
        });
    });
    $('#rnn li span:first').click();
    //END bottom panels infrastructure
    //**********************************************************************
    //global regional-home.asp end
    //**********************************************************************

    //**********************************************************************
    //Fund-Facts.asp Start
    //**********************************************************************
    $(".mtoday a").each(function(i) {
        $(this).click(function() {
            selectRegion(i);
        });
    });

    $(".mtoday a:eq(0)").click();

    $(".mtp h3").each(function(i) {
        $(this).click(function() {
            selectMarket(i);
        });
    });

    $(".mtpestate").css("display", "none");
    //show first of all accordians
    $((".mtpestate:first"), $(".mtp")).css("display", "block");

    //**********************************************************************
    //Fund-Facts.asp End
    //**********************************************************************
});

function selectFundFactSection(orgination, sectionID) {
    $(".navigation > ul > li").removeClass("subnavon");

    $($($($(orgination).parent()).parent()).parent()).addClass("subnavon");
    $($(orgination).parent()).addClass("subnavon");
    
    $(".FundFactSection").hide();
    
    $("#" + sectionID).show();
    triggerAnalytics(sectionID);
}

function triggerAnalytics(sectionID) {
	// fill in code for sending data to analytics software.
    _tag.dcsMultiTrack('DCS.dcsuri',location.href,'WT.ti',sectionID);
}

function selectRegion(i) {
    $(".mtp").css("display", "none");
    $(".mtoday a").removeClass("mton");
    $(".mtoday a:eq(" + i + ")").addClass("mton").blur();
    $(".mtp").css("display", "none");
    $(".mtp:eq(" + i + ")").css("display", "block");
}

function selectMarket(i) {
    if ($(".mtpestate:eq(" + i + ")").css('display') == "none") {
        $(".mtpestate").slideUp("fast");
        $(".mtpestate:eq(" + i + ")").slideDown("fast");
    }
}

function selectNavigation(topNavIndex, leftNavIndex, leftSecondLevelIndex) {
    $('#nav > li:eq(' + topNavIndex + ') a').addClass('navon');
    $('.navigation ul > li:eq(' + leftNavIndex + ')').addClass('subnavon');
    $('.navigation ul > li ul li:eq(' + leftSecondLevelIndex + ')').addClass('childsubnavon');
}

function SelectNavigationByID(NavElementID) {
    $('#' + NavElementID).addClass('navon');
    $('#' + NavElementID).addClass('subnavon');
    $('#' + NavElementID).addClass('childsubnavon');
}

var currentlySelected = 0;
function selectHPItem(index) {
    if (index < $('.mfn li span').length) {
        $('.mfn li span').removeClass('mfnon');
        $('.mfn li span:eq(' + index + ')').addClass('mfnon');
        $('.cpn').css('display', 'none');
        $('.cpn:eq(' + index + ')').css('display', 'block');
        currentlySelected = index;
    } else {
        selectHPItem(0);
    }
}

function selectContactItem(index) {
    if (index < $('.quicknav li span').length) {
        $('.quicknav li').removeClass('qnon');
        $('.quicknav li:eq(' + index + ')').addClass('qnon');
        $('.lrgpanels').css('display', 'none');
        $('.lrgpanels:eq(' + index + ')').slideDown('fast');
    } else {
        selectContactItem(0);
    }
}

function newsItems(index) {
    $('.nitems').css('display', 'none');
    $('.nitems:eq(' + index + ')').css('display', 'block');
}

function selectRegionalItem(index) {
    if (index < $('#rnn li span').length) {
        $('#rnn li span').removeClass('rnnon');
        $('#rnn li span:eq(' + index + ')').addClass('rnnon');
        $('.rnpanel').css('display', 'none');
        $('.rnpanel:eq(' + index + ')').slideDown('fast');
    } else {
        selectRegionalItem(0);
    }
}

function launchVideo(videoid,height,width){
	var vidid = "#videoplayer"+videoid;
	
	$.fn.colorbox({ width: +width, height: +height, html:$(vidid).html()});
}
