$(document).ready(function () {
    applyCufon();
    checkNavHomeBtn();
    
    $("#newsItemsBox .newsItem:first-child .content a").addClass("selected");
    
    $('.newsItem .content a').mouseover(function(){
      $('.newsItem .content a').removeClass("selected"); 
      $(this).addClass("selected");          
    });

    
    // add blank list element to menu items
    $('#aboutUs li:first-child, #lifeAtGateway li:first-child, #findUs li:first-child, #searchForAnswers li:first-child').addClass('firstLi');
    
    /**** Start scrolling tabs code ****/
    
    // select #flowplanes and make it scrollable. use circular and navigator plugins 
    $("#flowpanes").scrollable({size: 1, speed: 600, clickable: false}).circular().autoscroll( { interval: 5000, autopause: true }).navigator({ 
         // select #flowtabs to be used as navigator 
        navi: "#flowtabs", 
         // select A tags inside the navigator to work as items (not direct children) 
        naviItem: 'li', 
         // assign "current" class name for the active A tag inside navigator 
        activeClass: 'current',
        clickable: false, 
        api: true
    }); 
    
    var rotatorAPI = $("#flowpanes").scrollable();
    
    $('#flowtabs li').bind("click", function() { 
        rotatorAPI.stop(); 
    });
    
    /**** End scrolling tabs code ****/

    // search clicks
    $(".search .searchBtn").bind("click", function(event){
        search();
        event.preventDefault();
    });

    $(".search .searchInput").bind("keypress", function(event){
        if (event.keyCode == 13) { search(); }
    });

    $('.navOn3').each(function() {
        var currentClass = $(this).parent().attr("class");
        $(this).parent().attr("class", currentClass + " hasChildren");
    });

    $('.jqModal').each(function() {
        var anchor = $(this);
        if (anchor.attr('href').indexOf("?") == -1)
        {
            anchor.attr('href', anchor.attr('href') + '?nt=1');
        }
        else
        {
            anchor.attr('href', anchor.attr('href') + '&nt=1');
        }
    });
    
    var openWithFade=function(hash){ 
        
var $trigger = $(hash.t);
        var $modalWindow = $(hash.w);
        var $modalContainer = $('iframe', $modalWindow);
        var myUrl = $trigger.attr('href');
        var myTitle = $trigger.attr('title');
        var newWidth = 0;
        var newHeight = 0;
        var newLeft = 0;
        var newTop = 0;
        $modalContainer.html('').attr('src', myUrl);
        $('#jqmTitleText').text(myTitle);
        myUrl = decodeURIComponent(myUrl);
        myUrl = (myUrl.lastIndexOf("#") > -1) ? myUrl.slice(0, myUrl.lastIndexOf("#")) : myUrl;
        var queryString = (myUrl.indexOf("?") > -1) ? myUrl.substr(myUrl.indexOf("?") + 1) : null;
                 
        if (queryString != null && typeof queryString != 'undefined')
        {
            var queryVarsArray = queryString.split("&");
            for (var i = 0; i < queryVarsArray.length; i++)
            {
                if (unescape(queryVarsArray[i].split("=")[0]) == 'width')
                {
                    var newWidth = queryVarsArray[i].split("=")[1];
                }
                if (escape(unescape(queryVarsArray[i].split("=")[0])) == 'height')
                {
                    var newHeight = queryVarsArray[i].split("=")[1];
                }
                if (escape(unescape(queryVarsArray[i].split("=")[0])) == 'jqmRefresh')
                {
                    // if true, launches a "refresh parent window" order after the modal is closed.

                    hash.refreshAfterClose = queryVarsArray[i].split("=")[1]
                } else
                {

                    hash.refreshAfterClose = false;
                }
            }
            // let's run through all possible values: 90%, nothing or a value in pixel
            if (newHeight != 0)
            {
                if (newHeight.indexOf('%') > -1)
                {

                    newHeight = Math.floor(parseInt($(window).height()) * (parseInt(newHeight) / 100));

                }
                var newTop = Math.floor(parseInt($(window).height() - newHeight) / 2);                
            }
            else
            {
                newHeight = $modalWindow.height();
            }
            if (newWidth != 0)
            {
                if (newWidth.indexOf('%') > -1)
                {
                    newWidth = Math.floor(parseInt($(window).width() / 100) * parseInt(newWidth));
                }
                var newLeft = Math.floor(parseInt($(window).width() / 2) - parseInt(newWidth) / 2);

            }
            else
            {
                newWidth = $modalWindow.width();
            }
            
            // do the animation so that the windows stays on center of screen despite resizing
            $modalWindow.css({
                width: newWidth,
                height: 600,
                top: 0,
                left: newLeft,
                marginLeft: 0,
                opacity: 0
            }).jqmShow().animate({
                width: newWidth,
                height: newHeight,
                top: newTop,
                left: newLeft,
                marginLeft: 0,
                opacity: 1
            }, 'slow');
            
            //alert("new top = " + newTop + "new left = " + newLeft);
        }
        else
        {
            // don't do animations
            $modalWindow.jqmShow();
        }
 
        
    };

    var closeModal = function(hash)
    {
        var $modalWindow = $(hash.w);


        
        
        $modalWindow.hide(function()
        {
        
            hash.o.remove();
            
	$modalWindow.html("");
	//refresh parent
            if (hash.refreshAfterClose == 'true')
            {

                window.location.href = document.location.href;
            }
        });
    };
    

    $('#modalWindow').jqm({
        ajax: '@href',
        modal: true,
        overlay: 50,
        ajaxText: '<p style="width: 600px; text-align: center; margin-top: 68px;background: #fff;" ><img src="/images/lightbox/loading.gif" style="margin: 10px;"></p>',
        onShow: openWithFade,
        onHide: closeModal
    }); 
    
    $('#modalWindow').jqmAddClose('.jqmClose');
});

    // SEARCH FUNCTION -------------------------------------------------------------------------------------------------
    function search() {
        searchTerm = $('.search .searchInput').attr('value');
        window.location.href = "/pages/home/search.aspx?s=" + searchTerm;
    }
    
    
function applyCufon() {
    Cufon.replace('h1', { fontFamily: 'Helvetica Neue Thin' });
	Cufon.replace('h2', { fontFamily: 'Helvetica Neue Thin' });
	Cufon.replace('h3', { fontFamily: 'Helvetica Neue Thin' });
	Cufon.replace('h4', { fontFamily: 'Helvetica Neue Thin' });
	Cufon.replace('h5', { fontFamily: 'Helvetica Neue Thin' });
	Cufon.replace('h6', { fontFamily: 'Helvetica Neue Thin' });
	Cufon.replace('h6', { fontFamily: 'Helvetica Neue Thin' });
	Cufon.replace('#flowtabs li .title', { fontFamily: 'Helvetica Neue Thin' });
	Cufon.replace('#topNav #main_navigation .navOn2 li a', { fontFamily: 'Helvetica Neue Thin' });
}

function checkNavHomeBtn(){
   if($("#main_navigation li:not(li:first)").hasClass("on") == true){
    $("#home").removeClass("on");
   }
}