﻿
var mouseX, mouseY = 0;

$(document).ready(function() {
    $("#main_content").animate({ duration: 700, easing: 'easeInSine', opacity: 'toggle' });

    $("#slideshow").cycle({
        fx: 'scrollRight',
        easing: 'easeOutBounce',
        delay: -4000
    });

    loadEffects();

    $(window).resize(function() {
        $("#popin_coverDiv").height($(window).height());
        $("#popin_coverDiv").width($(window).width());
        $("#popin_coverDiv").height($(document).height());
        $("#popin_coverDiv").width($(document).width());
    });



    $("#nav_links li a").click(function() {
        return animateNavigation($(this), true);
    });


    if (curNavSection != '') {
        animateNavigation($("#topNav" + curNavSection), false);

    }

    if (curSubNavSection != '') {
        animateNavigation($("#topSubNav" + curSubNavSection), false);
    }

    checkCurrentPageLoaded();
    loadLightBoxItems();

});



function animateNavigation(item, navigate) {


    if ($(item).parent("li").children("ul:first").is(":visible")) {
        $(item).removeClass("bold").parent("li").children("ul").slideUp();
    }
    else {
        $(item).addClass("bold").parent("li").children("ul").slideDown();
    }

    $(item).parent("li").siblings().find("ul").slideUp();
    $(item).parent("li").siblings().find("a").removeClass("bold");

    if (dynNavigation && navigate) {

        if ($(item).attr("href") != "#" && $(item).attr("target") != "_blank") 
        {
            loadContent($(item).attr("href"));
            return false;
        }
        else {
            return true;
        }
        
    }

}

function checkCurrentPageLoaded() {
    if (window.location.href.indexOf("#") != -1) {
        var cur_url = window.location.href.substr(window.location.href.indexOf("#") + 1);

        var found = false;
        $("#nav_links li a").each(function() {
            if ($(this).attr("href") == cur_url) {
                animateNavigation($(this), true);
                found = true;
            }
        });

        if (!found) {
            loadContent(cur_url);
        }

        var url_arr = cur_url.split('/');

        animateNavigation($("#topNav" + url_arr[1]), false);
        animateNavigation($("#topSubNav" + url_arr[2]), false);

    }
}

function loadLightBoxItems() {
    $(".left_item img").each(function(i) {

    if ($(this).attr("lightbox") != "true" && $(this).attr("rel") != "nolightbox" && $(this).attr("title") != "nolightbox") {

            if ($(this).attr("src").toLowerCase().indexOf("_large.jpg") == -1) {

                var src = $(this).attr("src").replace(".jpg", "_Large.jpg")

                $(this).wrap("<a href=\"" + src + "\" rel=\"lightbox\"></a>");
                $(this).attr("lightbox", "true");
            }
        }
    })

}

function loadPeopleTracker() {

    $(".thepeople li a, .theclients li a").click(function() {

        if (dynNavigation) {
            if ($(this).attr("href") != "#") {
                loadContent($(this).attr("href"));
            }
            return false;
        }
    });

}

function loadContent(in_href)
{

    $.get(in_href + "?layout=AJAX", function(data) {

        pageTracker._trackPageview(in_href);
        $("#main_content .ajax_content").fadeOut(400, function() {
            $("#main_content .ajax_content").html(data);
            $("#main_content .ajax_content").fadeIn(400, function() { loadPeopleTracker(); loadLightBoxItems(); initLightbox(); });
            window.location.href = "/#" + in_href;

            
            $("#dyn_subnav a").click(function() {

                if (dynNavigation) {
                    if ($(this).attr("href") != "#") {
                        loadContent($(this).attr("href"));
                    }
                    return false;
                }
            });
        });
    });

}

function nav_SlideUp(in_list)
{
    $(in_list).slideUp();
    
}

function loadEffects()
{
            $(".ui-fade-rollover").each(function(){
	            if($(this).attr("effect_loaded") == "true") return;
	            $(this).attr("effect_loaded", "true"); 
	            var wrapper = "<div style='display: block; height: auto; width: auto; background-repeat: no-repeat; background-image: url(" + $(this).attr("src").replace("_off", "_over") + ")'></div>";
	            if($(this).parent().get(0).tagName == "A")
	            {
		            $(this).parent().wrap(wrapper);
	            }
	            else
	            {
		            $(this).wrap(wrapper);
	            }
	            
	            $(this).bind("mouseenter", function(e){
	                $(this).stop().animate({opacity: 0}, 300);
	            });
	            $(this).bind("mouseleave", function(e){
	                $(this).stop().animate({opacity: 1}, 300);
	            });

            });

            

}
