// JavaScript Document

$(document).ready(function() {

//	function $div(id, cssText, div) { 
//		div = document.createElement('div');
//		if (id) {
//            div.id = id;
//        }
//		div.style.cssText = cssText || '';
//		return $(div);
//	}

	$('#portfolio-list a.portfoliolink').click(
		function(event){
			event.preventDefault();

// Let's make sure to track which portfolio items are being viewed
			_gaq.push(['_trackPageview', $(this).attr('href')]);

// Set Up AJAX
			$.ajaxSetup ({  
				cache: false  
			});  
			var ajax_load = "<div id='loading'><img src='/images/loading.gif' alt='loading...' /></div>";  

// Load the Page
			var loadUrl = $(this).attr('href');
			$("#gallery-container").fadeIn();
			$("#gallery-inner")
				.show()
				.append(ajax_load)
				.load(loadUrl, function() {
					$(this).easySlider();
				})
		}
	);

	$("a[href^='http:']").not("[href*='jonbalza.com']").attr('target','_blank');  

// Tabs & History
	var $pages     = $('#pages'),
		$nav       = $('#navigation'),
		$about     = $('#about'),
		$portfolio = $('#portfolio'),
		$contact   = $('#contact');
   
    var $tabs = $pages.children();
    $tabs.hide();
    var document_title = document.title;

    var updateMenu = function(state){
        $nav.children('li:has(a[href="#'+state+'"])').addClass('active').siblings('.active').removeClass('active');
    };

    $.History.bind(function(state){
        document.title = document_title + ' | ' + state;
    });

// Home Page   
    $.History.bind('/home',function(state){
// Don't forget the GA Page Views!
		_gaq.push(['_trackPageview', "/home"]);
        updateMenu(state);
		$('#portfolio-list li:visible').fadeOut(500);
        $tabs.fadeOut(1000);
    });

// Portfolio Page
    $.History.bind('/portfolio',function(state){
		_gaq.push(['_trackPageview', "/portfolio"]);
        updateMenu(state);
        $tabs.fadeOut(1000);
		
		(function hidenext(jq){
			jq.eq(0).fadeIn("fast", function(){
				(jq=jq.slice(1)).length && hidenext(jq);
			});
		})($('#portfolio-list li'))

        $portfolio.stop(true,true).fadeIn(1000);
    });

// About Page
    $.History.bind('/about',function(state){
		_gaq.push(['_trackPageview', "/about"]);
        updateMenu(state);
		$('#portfolio-list li:visible').fadeOut(500);
        $tabs.fadeOut(1000);
        $about.stop(true,true).fadeIn(1000);
    });

// Contact Page
    $.History.bind('/contact',function(state){
		_gaq.push(['_trackPageview', "/contact"]);
        updateMenu(state);
		$('#portfolio-list li:visible').fadeOut(500);
        $tabs.fadeOut(1000);
        $contact.stop(true,true).fadeIn(1000);
    });
});

