jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
    return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
};

$.getDocHeight = function(){
    return Math.max(
        $(document).height(),
        $(window).height(),
        /* For opera: */
        document.documentElement.clientHeight
    );
};

$(function() {
    
    function equalHeight(group) {
        var tallest = 0;
        group.each(function() {
            var thisHeight = $(this).height();
            if(thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        group.height(tallest);
    }
    
    $('body').addClass('js');
    $('#headerWrapper').removeClass('headerWrapperPadding');
    
    $('ul.sub-menu').hide();
    
    function fbs_click(url, title) {
        u= url;
        t= title;
        window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
        return false;
    }

    /* $('.close_full_info').live('click', function() {
        $(this).parents('.widget').fadeOut('100');
        $('div.full_info').slideUp('1500', 'easeInOutQuad');
        return false;
    }); */

    var imageNumber = 1;
    $(".portfolioImages li").each(function(){
        $(this).attr("id", "image" + imageNumber);
        $("#imageNav ul").append('<li><a href="#image' + imageNumber + '">' + imageNumber + '</a></li>');
        imageNumber = imageNumber + 1;
    });
    var numberOfPortfolioImages = $(".portfolioImages li").length;
	
    $("#imageNav ul li a").bind("click", function(){
        $("#imageNav ul li").removeClass("active");
        $(this).parent("li").addClass("active");
        var theImage = $(this).attr("href"),
            imageInt = theImage.match(/image([0-9]+)/),
            imageNumber = imageInt[1],
            image = $(theImage),
            offset = image.offset(),
            offsetLeft = offset.left;
		
        if ($("#portfolioItem").hasClass("vertical")) {
            var height = 448;
            if (imageNumber == '1') {
                $(".portfolioImages li").stop(true, false).animate({
                    "top": "0px"
                }, 500);
            } else {
                $(".portfolioImages li").stop(true, false).animate({
                    "top": "-" + (height * (imageNumber - 1) ) + "px"
                }, 500);
            }
        } else if ($("#portfolioItem").hasClass("horizontal")) {
            var width = 835;
            if (imageNumber == '1') {
                $(".portfolioImages li").stop(true, false).animate({
                    "left": "0px"
                }, 500);
            } else {
                $(".portfolioImages li").stop(true, false).animate({
                    "left": "-" + (width * (imageNumber - 1) ) + "px"
                }, 500);
            }
        }
			
        return false;
    });
    $("#imageNav ul li:first a").click();
	
    $(".prev").bind("click", function(){
        $("#imageNav ul li.active").prev().find("a").click();
        return false;
    });
    $(".next").bind("click", function(){
        $("#imageNav ul li.active").next().find("a").click();
        return false;
    });
	
	// Keyboard nav of portfolio items
    $(document).keydown(function(e){
        if (e.keyCode == 37 || e.keyCode == 38) {
            $(".prev").click();
        }
        if (e.keyCode == 39 || e.keyCode == 40) {
            $(".next").click();
        }
    });
	
	
    /*****
		PORTFOLO SIDEBAR ACCORDION
		---
		Uses the class 'toggle' to add a toggle switch and them slide it up & down
	*****/
	var cur_id=0;
	
    function showPortfolioSidebar() {
        $(".toggle").each(function(){
			cur_id++;
            var theDiv = $(this),
            theDivHeight = theDiv.outerHeight(),
            theDivTitleHeight = theDiv.find("h3").outerHeight(),
            theDivTextheight = theDiv.find(".content").outerHeight();
            $(this).find('.content').hide();
			$(this).attr("id","toggle_div_"+cur_id);
        });
    }
    showPortfolioSidebar();


    $(".toggleSwitch").live("click", function(){
        var theparent = $(this).parent("div");
        
       	$(".toggleSwitch").not(this).parent("div").find(".content").slideUp();
        $(".toggleSwitch").not(this).parent("div").find("span").css({'background-position':'0 0'});
        $(".toggleSwitch").not(this).parent("div").removeClass('opened');
        if (theparent.hasClass('opened')) {
            theparent.removeClass('opened');
            theparent.find(".content").slideUp();
            theparent.find("span").css({'background-position':'0 0'});
        }  else {
            theparent.addClass('opened');
            theparent.find(".content").slideDown();
            theparent.find("span").css({'background-position':'0 -19px'});
        }
        if ($(this).attr("href") == "#") {
    		return false;
    	}
    });
    
    $(".justALink").live("click",function(){
    	$('div.full_info').slideUp();
    	return false;
    });
    
    $('.close_full_info').live('click', function() {
            $(this).parents('.widget').fadeOut('100');
            $('div.full_info').slideUp('1500', 'easeInOutQuad');
            return false;
        })
    
    $(".open").find(".toggleSwitch").click();

    $('div.search h3').toggle(
        function () {
           $(this).parent('div').find('.content').slideFadeToggle();
        }, function() {
           $(this).parent('div').find('.content').slideFadeToggle();
        }
    );
    $('div.search').find('.content').slideUp();
    
    
    /*****
    	LAST ITEMS
    *****/
    // Adds a class of 'last' to the last item on the given objects
    $(".overview .content ul li:last a").addClass("last");
    
    
    /*****
        MAIN NAV
    *****/
    $("#menu > li").addClass("main-link");  		
	$('#menu li').hover(function(){
	    $(this).find("ul:first").stop(true, true).slideFadeToggle();
	}, function(){
	    $(this).find("ul:first").stop(true, true).slideFadeToggle(0);
	});
	$("#menu").mouseleave(function(){
		$(this).find("ul").stop(true, true).hide();
	});
	
	
	/*****
		REMOVE BREADCRUMB LINKS
	*****/
	var bread = $("#breadcrumbs"),
		domain = document.domain;
	
	bread.find("a").each(function(){
	
		//console.log(domain);
	
		var link = $(this),
			href = link.attr("href"),
			text = link.html();
		
		if (href == "http://" + domain + "/who-we-are/") link.before('<b>' + text + '</b>').remove();
		if (href == "http://" + domain + "/what-we-do/") link.before('<b>' + text + '</b>').remove();
		if (href == "http://" + domain + "/what-we-do/disciplines/") link.before('<b>' + text + '</b>').remove();
		if (href == "http://" + domain + "/what-we-do/our-work/") link.before('<b>' + text + '</b>').remove();
		if (href == "http://" + domain + "/how-we-do-it/") link.before('<b>' + text + '</b>').remove();
		if (href == "http://" + domain + "/why/") link.before('<b>' + text + '</b>').remove();
		if (href == "http://" + domain + "/whats-cooking/") link.before('<b>' + text + '</b>').remove();
			
	});	
	
	
	/*****
		MAKE LINKS LINK THROUGH TO FIRST ITEM IN ITS SUB NAV
	*****/
	function linifyNavItemsWithSubMenus() {
	
		var itemWithSubNav = $("#menu li ul li");
		itemWithSubNav.each(function(){
		
			var item = $(this),
				firstUL = item.find("ul:first"),
				itemToChange = item.find("a:first");
							
			
			if (firstUL.length > 0) {
				var firstLink = firstUL.find("li:first a").attr("href");				
				if (itemToChange.length > 0) {
					itemToChange.attr("href", firstLink);
				}
			}
			
		});
				
	}
    linifyNavItemsWithSubMenus();
    
    
    /*****
    	FOOTER FIX
    *****/
    function footercheck(){
    
 		$(".stickyFooterWrapper").removeAttr("style").addClass("attrRemoved");
 
		var documentHeight = $.getDocHeight(),
			windowHeight = $(window).height();	
		if (documentHeight > windowHeight) {
			$(".stickyFooterWrapper").css("height", (documentHeight + 45) + "px !important").removeClass("attrRemoved");
			$("#footer").css({
//				"bottom" : "0px",
//				"left" : "0px",
//				"position" : "relative"
			});
                   
		} else {
			$(".stickyFooterWrapper").removeAttr("style").addClass("attrRemoved");
		}
		$(".hiring").html("doc: " + documentHeight + " -- win: " + windowHeight);
	}
	$(window).resize(footercheck);
	footercheck();
	
	      
});
