/*  @ Title:  GLOBAL Javascript 
@ AGENCY: CONCEP
@ CLIENT: RLAM
@ Author: Ben Copping
@ Date: 14/09/2009 */

$(document).ready(function() {

    //for a snapier animation on all animated areas include this  jQuery.easing.def = "easeOutExpo";

    // our delay function
    jQuery.fn.delay = function(time, func) {
        this.each(function() {
            setTimeout(func, time);
        });

        return this;
    };

    //main navigation animation
    $("ul.mainNav li a").hover(function() {
        $(this).stop().animate(
			{ backgroundPosition: "(0 -22px)" },
			{ "duration": "fast", "easing": "swing" });
    },
    // on mouse out
	function() {
	    if ($(this).parent().hasClass("selected")) { }
	    //animate it
	    else {
	        $(this).stop().animate(
			{ backgroundPosition: "(0 0)" },
			{ "duration": "fast", "easing": "swing" });
	    }

	});

    //login modal light box
    $("a.hLogin").click(function() {

        $("#loginFrame").css({
            'right': 0,
            'top': -88
        });

        var hLoginUrl = $('#login_url').attr('value');

        //call our ajax content for the search	
        $.ajax({
            url: hLoginUrl,
            cache: false,
            success: function(html) {
                $(".overlayContent").contents().remove();

                $("#loginFrame").append(html);
            }
        });
        $(this).delay(350, function() {

            $("#loginFrame").show().animate({
                top: 6
            }, { "duration": 600, "easing": "easeOutExpo" });

        });

        $(this).delay(100, function() { $("#loginInner").fadeIn("fast"); });
    });

    //search modal light box
    $("a.hSearch").click(function() {
        //position our search box	
        $("#searchFrame").css({
            'left': 222,
            'top': -88
        });

        var hSearchUrl = $('#search_url').attr('value');

        //call our ajax content for the search	
        $.ajax({
            url: hSearchUrl,
            cache: false,
            success: function(html) {
                $(".overlayContent").contents().remove();

                $("#searchFrame").append(html);
            }
        });

        $(this).delay(350, function() {

            $("#searchFrame").show().animate({
                top: 6
            }, { "duration": 600, "easing": "easeOutExpo" });

        });
        $(this).delay(100, function() { $("#searchInner").fadeIn("fast"); });
    });

    //loading icon for when fetching ajax
    $(".loginLoading").ajaxStart(function() {
        $(this).show();
    });

    $().ajaxComplete(function(event, request, settings) {
        $(".loginLoading").fadeOut("slow");
    });

    // horizontal animated tabs (charts)
    $(".fundChartHeader ul li a").hover(function() {
        //ease the slider to the current hover nav
        var nav = $(this).position();
        $(".tabsDec").stop().show().animate({
            left: nav.left
        }, { "duration": "fast", "easing": "swing" });

    },
    // on mouse out
	function() {
	    //reset the slider to the currently active position
	    var navReset = $(".fundChartHeader ul li.selected a").position();
	    $(".tabsDec").animate({
	        left: navReset.left
	    }, "fast");

	});

    // load our ajax fund charts and set the nav to selected	
    $(".fundChartHeader ul li a").click(function() {

        $(".fundChartHeader ul li").removeClass("selected");
        $(this).parent().addClass("selected");


        if ($(this).hasClass("fund5Yr")) {
            //call our ajax content
            $.ajax({
                url: "fund-chart-5.html",
                cache: false,
                success: function(html) {
                    $(".fundChartData").contents().remove();

                    $(".fundChartData").append(html);
                }
            });
        }

        if ($(this).hasClass("fund3Yr")) {
            //call our ajax content
            $.ajax({
                url: "fund-chart-3.html",
                cache: false,
                success: function(html) {
                    $(".fundChartData").contents().remove();

                    $(".fundChartData").append(html);
                }
            });
        }

        if ($(this).hasClass("fund1Yr")) {
            //call our ajax content
            $.ajax({
                url: "fund-chart-1.html",
                cache: false,
                success: function(html) {
                    $(".fundChartData").contents().remove();

                    $(".fundChartData").append(html);
                }
            });
        }
    });


    //left navigation animation
    //if the left nav is present
    if ($('.leftNav').length) {
        //hide the left nav decoration
        $('.leftNavDec').hide();

        // for when we have li selected
        if ($('.leftNav ul li').hasClass("selected")) {

            $('.leftNavDec').show();

            lNavPosition = $('.leftNav ul li.selected a').position();

            $('.leftNav .leftNavDec').show().css({ top: lNavPosition.top + 5 });

            function donothing() { }

            function follow() {
                lNavPosition = $(this).position();
                $('.leftNav .leftNavDec').stop().animate({ top: lNavPosition.top + 5 });
            }
            //using the hoverIntent delay function
            var config = {
                sensitivity: 3, // number = sensitivity threshold  
                interval: 125, // number = milliseconds for onMouseOver polling interval    
                over: follow, // mouseover function    
                timeout: 500, // number = milliseconds delay before onMouseOut    
                out: donothing // mouseout function   
            };

            /*$('.col1').mouseleave(function() {

                var navReset = $(".leftNav .selected").position();
            $(".leftNavDec").animate({
            top: navReset.top + 5
            }, "fast");
            });*/

            $('.leftNav ul li a').hoverIntent(config);
        }

        // for a straight hover when the left nav has not been selected
        if (!$('.leftNav ul li').hasClass('selected')) {

            //dont do anything if selected is not present
            function donothing() { }

            function follow() {
                $('.leftNavDec').show();
                lNavPosition = $(this).position();
                $('.leftNav .leftNavDec').stop().animate({ top: lNavPosition.top + 5 });
            }

            //using the hoverIntent delay function
            var config = {
                sensitivity: 3,
                interval: 125,
                over: follow,
                timeout: 500,
                out: donothing
            };

            $('.leftNav ul li a').hoverIntent(config);

            $('.col1').mouseleave(function() {
                $('.leftNavDec').fadeOut('slow');
            });
        }
    } //end if left nav present functions


    //accordions
    $(".accordion ul").hide();
    $(".accordion li.open").children('ul').show();
    $(".accordion li.title span, .accordion li.subTitle span").click(function() {
        $(this).parent().toggleClass("open");
        $(this).next("ul").slideToggle({ "duration": 400, "easing": "easeOutExpo" });
    });


    $("#content input.search").click(function() {

        if ($(this).val() == 'Search') { $(this).val(''); }
    });

});   //end doc ready

(function($) {
    $.fn.extend({
        modalPanel: function() {

            //Create our overlay object
            var overlay = $("<div id='modal-overlay'></div>");

            return this.each(function() {

                //Listen for clicks on objects passed to the plugin
                $(this).click(function(e) {

                    if (typeof document.body.style.maxHeight === "undefined") { //if IE 6
                        $("body", "html").css({ height: "100%", width: "100%" });
                    }

                    //Append the overlay to the document body
                    $("body").append(overlay.click(function() {
                        modalHide();
                    }))

                    //Set the css and fade in our overlay
                    overlay.css("opacity", 0.8);
                    overlay.fadeIn(150);

                    //Prevent the anchor link from loading
                    e.preventDefault();

                    //Activate a listener 
                    $(document).keydown(handleEscape);

                });
            });

            //Our function for hiding the modalbox
            function modalHide() {

                $("div.overlayContent").hide();

                $(document).unbind("keydown", handleEscape)
                var remove = function() {

                    $(this).remove();
                }
                overlay.fadeOut(remove);
            }

            //Our function that listens for escape key.
            function handleEscape(e) {

                if (e.keyCode == 27) {

                    modalHide();
                }
            }

        }
    });
})(jQuery);



$(function() {
    $('a[rel*=modalPanel]').modalPanel();
});

function closeButton() {

    var remove = function() {
        $("#modal-overlay").remove();
    }

    $("#modal-overlay").fadeOut(remove);
    $("div.overlayContent").hide();

}

//clear our specific inputs with specific copy
//so we don't clear inputs once the user has entered their own copy
function clearHygieneForms() {
    $("#loginFrame input.username, #loginFrame input.password, #searchFrame input.search").click(function() {

        if ($(this).val() == 'Username') { $(this).val(''); }
        if ($(this).val() == 'Password') { $(this).val(''); }
        if ($(this).val() == 'Search') { $(this).val(''); }
    });
}

//function hLoginDrop(hLoginUrl) {
//    $("#loginFrame").css({
//        'right': 0,
//        'top': -88
//    });


//    //call our ajax content for the search    
//    $.ajax({
//        url: hLoginUrl,
//        cache: false,
//        success: function(html) {
//            $(".overlayContent").contents().remove();

//            $("#loginFrame").append(html);
//        }
//    });
//    $(this).delay(350, function() {

//        $("#loginFrame").show().animate({
//            top: 6
//        }, { "duration": 600, "easing": "easeOutExpo" });

//    });

//    $(this).delay(100, function() { $("#loginInner").fadeIn("fast"); });
//}

//function hSearchDrop(hSearchUrl) {
//    //search modal light box

//    //position our search box    
//    $("#searchFrame").css({
//        'left': 222,
//        'top': -88
//    });

//    //call our ajax content for the search    
//    $.ajax({
//        url: hSearchUrl,
//        cache: false,
//        success: function(html) {
//            $(".overlayContent").contents().remove();

//            $("#searchFrame").append(html);
//        }
//    });

//    $(this).delay(350, function() {

//        $("#searchFrame").show().animate({
//            top: 6
//        }, { "duration": 600, "easing": "easeOutExpo" });

//    });
//    $(this).delay(100, function() { $("#searchInner").fadeIn("fast"); });
//}
