var bIsOpen = false;
var curSlug = "";
var lastAddress = "";

$(document).ready(function() {
	$('#award-list').jScrollPane();
	
	fixHeader();
				
	$(".proj").hover(function(){
		$(this).css("backgroundColor", "#1c1c1c");
	}, function() {
		$(this).css("backgroundColor", "#000");			
	});
	
	/*$(".proj-link").bind('click', function(e){
		
      	var url = $('a',this).val('a').attr( 'href' );
	  	alert(url);
	  	//window.open(url, '_self');
	  	//window.location.href = url;
    });*/
    
    $("#work-close").bind('click', function(e){
       closeWork();
    });
    
    $("#logo-btn").click(function(e){
    	e.preventDefault();
    	
    	$('html,body').animate({
        scrollTop: 0},
        'slow');
        
        closeWork();
    });
    
    $(".scroll-link").click(function(e){
    	e.preventDefault();
    	
    	var href = $(this).attr('href');
    	$('html,body').animate({
        scrollTop: $(href).offset().top-90},
        'slow');
    });
    
    $.address.init(function(event) {
       changeFeaturedContent();
    }).change(function(event) {
        if(event.value == "/" && lastAddress != "/") {
            lastAddress = "/";
            bIsOpen = false;
        } else {
          lastAddress = event.value;
          changeFeaturedContent(); 		          
        }
    });
    
    
    
    function changeFeaturedContent() {
	    var url = location.href;
	    pattern = /#\/([a-zA-Z0-9\-]+)\//;
	    slug = pattern.exec(url);
	    if(slug != null)
	    {
		    $("#the-work").slideUp('slow', function() {
		       	$("#the-work").html("");
		       	bIsOpen = true; 
		       	curSlug = slug[1];
		        $.ajax({
		            url: 'work/work-' + slug[1] + '.html',
		            timeout:1000,
		            error : raiseAjaxError,
		            success: displayWorkContent
		        });
	    	});
		}
	}
	
	function setHeight(which) {
	    var div = $(which);
	    div.css({height: div.height()+20});
	}
	
	function displayWorkContent(myHTML) {

		closebtn = '<div id="work-close"><a href="#latest-work" id="work-close-a"><img src="images/x.png" border="0" /></div>';
		//$("#the-work").html(myHTML);
		//setHeight("#the-work");
		//$("#the-work").delay(100).slideDown('slow');

		$("#the-work").html(myHTML).slideDown('slow').show(function(){
			$(this).html(myHTML).show();
			if(!$.browser.msie) {
				$(this).append(closebtn);
			}
			
			$("#work").cycle({fx: 'scrollRight'});
			
		});
		
		
      	bIsOpen = true;
      	$('html, body').animate({ scrollTop: $("#the-work").offset().top - 90}, 200, 'easeInOutQuad');
      	
      	$("#close-it").live("click", function(e){
    		closeWork();
	    });
	    
	    $("#work-close-a").live("click", function(e){
    		closeWork();
	    });
	    
	}
	
	function raiseAjaxError()
	{
	    alert('Could not load the page');
	}
	
	function closeWork() {
		$("#the-work").slideUp('slow', function() {
	    	$("#the-work").html("");
		});
				
		bIsOpen = false;
	}
	
	function fixHeader() {
		var dw = $(document).width();
		if(dw <= 960) {
			$("#main-img-wrapper").css("width", $(document).width());
		} else {
			if(dw > 1400) {
				$("#main-img-wrapper").css("width", 1400);
				$("#main-img-wrapper").css("marginLeft", (dw - 1400) * 0.5);
			} else {
				$("#main-img-wrapper").css("width", $(document).width());
			}
		}
	}
	
	function allOff() {
		$("#btnAbout").removeClass("btnOn");
		$("#btnWork").removeClass("btnOn");
		$("#btnWhatWeDo").removeClass("btnOn");
		$("#btnAwards").removeClass("btnOn");
		$("#btnContact").removeClass("btnOn");
	}
	
	$("#btnAbout").bind('click', function(e){
		closeWork();
		allOff();
	//	$(this).addClass("btnOn");
	});
	$("#btnWork").bind('click', function(e){
		closeWork();
		allOff();
	//	$(this).addClass("btnOn");
	});
	$("#btnWhatWeDo").bind('click', function(e){
		closeWork();
		allOff();
	//	$(this).addClass("btnOn");
	});
	$("#btnAwards").bind('click', function(e){
		closeWork();
		allOff();
	//	$(this).addClass("btnOn");
	});
	$("#btnContact").bind('click', function(e){
		closeWork();
		allOff();
	//	$(this).addClass("btnOn");
	});
	
	 $('#main-img').cycle({
		timeout: 10000, fx: 'scrollLeft', pager: '#nav',
		// callback fn that creates a thumbnail to use as pager anchor 
	    pagerAnchorBuilder: function(idx, slide) { 
	    	$('#main-img').delay(300).fadeIn("slow");
	        return '<a href="#">&nbsp;&nbsp;&nbsp;</a>'; 
	    }
	});
	
	$(window).bind('resize', function(e){
		fixHeader();
	});
	
	
	/*$(window).bind('scroll', function(e){
		allOff();
		var st = $(document).scrollTop();
		if(st <= $("#work-slider").scrollTop()) {
			allOff();
		}
		
		if(st >= $("#work-slider").offset().top - 90 && st <= $("#link-whatwedo").offset().top - 90) {
			allOff();
			$("#btnWork").addClass("btnOn");
		}
		
		if(st >= $("#link-whatwedo").offset().top - 90 && st <= $("#about").offset().top - 90) {
			allOff();
			$("#btnWhatWeDo").addClass("btnOn");
		}
		
		if(st >= $("#about").offset().top - 90 && st <= $("#map").offset().top - 90) {
			allOff();
			$("#btnAbout").addClass("btnOn");
		}
		
		if(st >= $("#map").offset().top - 90) {
			allOff();
			$("#btnContact").addClass("btnOn");
		}
	});*/
	
		 
	//});
    
    
  });
