$(function() {
	var sections = [
		{
			'section': 'about_me', 
			'height' : 187
		},
		{
			'section': 'what_i_do',
			'height' : 138
		},
		{
			'section' : 'vcard',
			'height' : 92
		}
	];
	var currentSection = null;
	var currentWork = 0;
	
	$('#works').css({display:'none'});
	$('#work_next').click(function() {
		if(currentWork < $('#works .work').length - 1) {
			currentWork += 1;
			load_current_work();
		}
	});
	$('#work_previous').click(function() {
		if(currentWork > 0) {
			currentWork -= 1;
			load_current_work();
		}
	});
	
	if(!$.browser.msie)
		$('section').css({opacity: 0});
	$('section').css({display: 'none'});
	$('#logo').click(function() {
		open_section(sections[0]);
	});
	$('nav a').each(function(i,element) {
		$(element).click(click_handler)
	});
	close_section_callback('what_i_do');
	
	function click_handler(event) {
		element = $(event.currentTarget.parentNode);
		if(element.hasClass(currentSection))
			return;
		
		for (var i=0; i < sections.length; i++) {
			if(element.hasClass(sections[i]['section'])) {
				open_section(sections[i]);
			}
		};
	}
	
	function open_section(section) {
		var oldSection = currentSection;
		currentSection = section['section'];
		
		$('nav a').each(function(i,anElement) {
			var anElement = $(anElement.parentNode);
			if(anElement.hasClass(currentSection))
				anElement.addClass('selected');
			else
				anElement.removeClass('selected');
		});
		
		if(oldSection) {
			$('#section_controller').animate({
				height : section['height']
			},500,'swing');
			if(!$.browser.msie)
				$('#'+oldSection).animate({
					opacity: 0
				},500,'swing',function() {
					close_section_callback(oldSection);
				});
			else
				close_section_callback(oldSection);
		}
		else {
			$('#section_controller').css({height:section['height']});
		}
		$('#'+currentSection).css({
			display: '',
		});
		if(!$.browser.msie)
			$('#'+currentSection).animate({
				opacity: 1
			},500,'swing');
			
		if(currentSection == 'what_i_do') {
			load_current_work();
			
			if(!$.browser.msie) {
				$('#what_i_do').addClass('notransition');
				$('#what_i_do .curtains.left').animate({
					marginLeft: 0,
					opacity: 1
				},500,'swing');
				$('#what_i_do .curtains.right').animate({
					marginRight: 0,
					opacity: 1
				},500,'swing');
				$('#what_i_do img').animate({
					opacity: 1
				},500,'swing',function() {
					$('#what_i_do').removeClass('notransition');
				});
			}
			else {
				$('#what_i_do .curtains.left').animate({
					marginLeft: 0,
				},500,'swing');
				$('#what_i_do .curtains.right').animate({
					marginRight: 0,
				},500,'swing');
			}
		}
	}
	
	function close_section_callback(section) {
		if(section == currentSection)
			return;
		$('#'+section).css({
			display: 'none'
		})
		if(section == 'what_i_do') {
			if(!$.browser.msie) {
				$('#what_i_do .curtains.left').css({
					opacity: 0
				});
				$('#what_i_do .curtains.right').css({
					opacity: 0
				});
				$('#what_i_do img').css({
					opacity: 0
				});
			}
			
			$('#what_i_do .curtains.left').css({
				marginLeft: -87,
			});
			$('#what_i_do .curtains.right').css({
				marginRight: -87,
			});
		}
	}
	
	function load_current_work() {
		if(currentWork <= 0)
			$("#work_previous").addClass("disabled");
		else
			$("#work_previous").removeClass("disabled");
			
		if(currentWork >= $("#works .work").length - 1)
			$("#work_next").addClass("disabled");
		else
			$("#work_next").removeClass("disabled");
		
		var work = $($("#works .work")[currentWork]);
		$("#work img")[0].src = "src/what_i_do/" + $(work.find("div")[4]).html();
		$("#work img")[0].alt = $(work.find("div")[1]).html();
		$("#work")[0].href = $(".link_work")[0].href = $(work.find("div")[0]).html();
		$($(".link_work span span")[0]).html($(work.find("div")[2]).html());
		$($(".link_work span span")[1]).html($(work.find("div")[3]).html());
	}
	
	var query = window.location.hash.split(":");
	switch(query[0]) {
		case '#whatido':
		    open_section(sections[1]);
			break;
			
		case '#vcard':
			open_section(sections[2]);
			break;
			
		default:
			open_section(sections[0]);
			break;
	}
	
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");

	$.getScript(gaJsHost + "google-analytics.com/ga.js", function() {
        try {
			var pageTracker = _gat._getTracker("UA-1526344-8");
            pageTracker._trackPageview();
        } catch(err) {

        }
	});
})