/* Author: 

*/
var $fsImage;
var origWidth = 1280;
var origHeight = 960;
var fsInit = false;
var path = '';
var supportsHistory = false;
var contentHeight = 0;
$(function()
{	
	if (typeof history.pushState === "function") {
	   supportsHistory = true;
	   window.onpopstate = function(event) {  
	   		if(event.state == '')	check_loc();
		}
	   
	}
	
	contentHeight = $('#content').height(); // run only once
	
	$(window).resize(function()
	{
		var w = $(window).width();
		var h = $(window).height();
		var ch = contentHeight;//$('#content').height();
		$('header,#map,#gallery').height(h);
		
		if(ch < h) $('#content').height(h);
		else $('#content').height($(document).height());
		
		$('#main,#container').height(h);
		$('#posts').height(h - 40);
		$('#gallery').width(w);
		if(fsInit) updateFS();
		
		// set loader pos
		var l = (w - 10) / 2;
		var t = (h - 10) / 2;
		
		$('#loading').css({
			'top':t+'px',
			'left':l+'px'
		});
		
		var ph = $('#posts').height();
		if(ph > h)
		{
			//$('#posts').css('overflow-y','scroll');
		}
	});
	
	init_site();
	if($('#map').length) init_map();	
});

var init_site = function()
{
	img_load();
	$(window).resize();
	init_project();
	map = 0;
	//if($('#map').length) init_map();	
	$fsImage = $('#gallery img').first();
	fsInit = true;
	setFS();
	
	$('.post').hover(function()
	{
		$(this).find('.over-arrow').stop(true,true).fadeIn();
	},function()
	{
		$(this).find('.over-arrow').stop(true,true).fadeOut();
	});
	
	$('a').unbind('click');
	$('a').click(function(e)
	{
		var $el = $(this);
		if($(this).attr('target') != '_blank' && !$el.hasClass('plain')) e.preventDefault();
		if(!$el.hasClass('plain')) {
		var href = $(this).attr('href');
		load_page($(this).attr('title'),href);
		/*
		if(href != '/') href += '/ajx';
		if(href != path) {
			$('#map').html('');
			$('#loading').fadeIn();
			if(!$el.parent().hasClass('li-level-2')) $('nav li ul').fadeOut();
			$.post(href,{ 'ajax':'true' },function(data)
			{
				$('#main').fadeOut('',function()
				{
					$(this).html(data);
					set_nav($el);
					init_site();
					$(this).fadeIn(function()
					{
						if($('#map').length) init_map();
					});
				});
			});
			path = href;
			*/
		//}
		}
		
	});	
		
		
	$('.post.news').bind('click',function(e)
	{
		//$(this).find('a').first().click();
	});
}

var set_nav = function($link)
{
	$('nav li').removeClass('selected');
	if($link.parent().hasClass('li-level-1')) $('nav li li.first').addClass('selected');
	setTimeout(function()
	{
		$link.parent().addClass('selected').find('ul').first().fadeIn();
	},500);
	if($('.projects').length) $('#tags').fadeIn();
	else $('#tags').fadeOut();
	//update_browser($link);
	
	$('.post.news').click(function()
	{
		//$(this).find('a').first().click();
	});
}

var init_project = function()
{
	if($('#project-nav').length)
	{
		var numImages = $('#gallery img').length;
		var imageIndex = 1;
		var $image = $('#gallery img').first();
		$('#image-count').html('1/' + numImages);
		
		$('#next').click(function()
		{
			var $next = $image.next();
			if(!$next.length)
			{
				if(next_page != '/'){
					
					$('#loading').fadeIn();
					$.post(next_page,{ 'ajax':'true' },function(data)
					{
						$('#main').fadeOut('',function()
						{
							$(this).html(data);
							init_site();
							$(this).fadeIn();
						});
					});
				} else {
					$next = $('#gallery img').first();
				}
				imageIndex = 1;
			} else imageIndex++;
			if(numImages > 1){
				//$image.fadeOut();
				$fsImage = $next;
				setFS();
				$image.animate({'left':'-' + $(window).width() + 'px'},700,'easeInOutExpo');
				$next.css('left',$(window).width()+'px').show().animate({'left':'0px'},700,'easeInOutExpo',function()
				{
					$image = $next;
					$('#image-count').html(imageIndex + '/' + numImages);
				});
				$('#content').slideUp(300);
				
				/*fadeIn('',function()
				{
					$image = $next;
					$('#image-count').html(imageIndex + '/' + numImages);
				});*/
			}
		});
		$('#prev').click(function()
		{
			var $next = $image.prev();
			if(!$next.length)
			{
				if(prev_page != '/'){
					$('#loading').fadeIn();
					$.post(prev_page,{ 'ajax':'true' },function(data)
					{
						$('#main').fadeOut('',function()
						{
							$(this).html(data);
							init_site();
							$(this).fadeIn();
						});
					});
				} else {
					$next = $('#gallery img').last();
				}
				imageIndex = numImages;
			} else imageIndex--;
			if(numImages > 1){
				$fsImage = $next;
				setFS();
				$image.animate({'left':$(window).width() + 'px'},700,'easeInOutExpo');
				$next.css('left','-' + $(window).width()+'px').show().animate({'left':'0px'},700,'easeInOutExpo',function()
				{
					$image = $next;
					$('#image-count').html(imageIndex + '/' + numImages);
				});
				$('#content').slideUp(300);
			}
		});
		
		$('.info').click(function(event)
		{
			event.preventDefault();
			$('#content').slideToggle(300);
		});
		$('#gallery').css('cursor','pointer');
		$('#gallery').click(function()
		{
			var $next = $image.next();
			if(!$next.length)
			{
				if(next_page != '/'){
					$.post(next_page,{ 'ajax':'true' },function(data)
					{
						$('#main').fadeOut('',function()
						{
							$(this).html(data);
							init_site();
							$(this).fadeIn();
						});
					});
				} else {
					$next = $('#gallery img').first();
				}
				imageIndex = 1;
			} else imageIndex++;
			if(numImages > 1){
				//$image.fadeOut();
				$fsImage = $next;
				setFS();
				$image.animate({'left':'-' + $(window).width() + 'px'},700,'easeInOutExpo');
				$next.css('left',$(window).width()+'px').show().animate({'left':'0px'},700,'easeInOutExpo',function()
				{
					$image = $next;
					$('#image-count').html(imageIndex + '/' + numImages);
				});	
				$('#content').slideUp(300);		}
		});
	} else {
		$('#gallery img').click(function(e)
		{
			e.preventDefault();
		});
	}
		$('#gallery img').bind('dragstart', function(event) { event.preventDefault(); });
}

function load_page(title,path)
{
	var apath = path == '/' ? path : path + 'ajx';
	$.post(apath,{ 'ajax':'true' },function(data)
	{
		$('#main').fadeOut('',function()
		{
			$(this).html(data);
			//contentHeight = $('#content').height();
			init_site();
			$(this).fadeIn('',function()
			{
				
				contentHeight = $('#content').height();
			});
		});
	});
	
	// set navigation
	var $navlink = $('nav a[href="'+path+'"]');
	if($navlink.length)
	{
		set_nav($navlink);
	}
	
	if(supportsHistory)
	{
		window.history.pushState('',title,path);
	} else {
		location.hash = '#' + path;
	}	
}

var setFS = function()
{
	origWidth = $fsImage.attr('width');
	origHeight = $fsImage.attr('height');
	updateFS();
}

var updateFS = function()
{
	//if($fsImage == null) return;
	var w = $(window).width();
	var h = $(window).height();
	var widthRatio = (origHeight/origWidth) * 100;
	var heightRatio = (origWidth/origHeight) * 100;
	var followWidth = new Array();
	var followHeight = new Array();
	
	// Calculate dimensions based on width
	
	followWidth.push(w); // width
	followWidth.push(Math.ceil((w / 100) * widthRatio)); // height
	followHeight.push(Math.ceil((h / 100) * heightRatio)); // width
	followHeight.push(h); // height
	
	if(followWidth[1] > h)
	{
		$fsImage.css('width',followWidth[0]+'px');
		$fsImage.css('height',followWidth[1]+'px');
		var offsetTop = Math.round((followWidth[1] - h) / 2);
		$fsImage.css('top','-'+offsetTop+'px');
		$fsImage.css('left','0px');
	}
	else if(followHeight[0] > w)
	{
		$fsImage.css('width',followHeight[0]+'px');
		$fsImage.css('height',followHeight[1]+'px');
		var offsetLeft = Math.round((followHeight[0] - w) / 2);
		$fsImage.css('left','-'+offsetLeft+'px');
		$fsImage.css('top','0px');
	}
}

var update_browser = function($el)
{
	var path = $el.attr('href');
	var title = $el.attr('title');
	if(supportsHistory)
	{
		window.history.pushState('',title,path);
	} else {
		location.hash = '#' + path;
	}	
	//console.log(path);
}

var check_loc = function()
{
	var p = location.pathname;
	var h = location.hash;
	var path = '';
	
	if(p.length > 1){
		if(!supportsHistory) location = '/#' + p;
		else path = p;
	} 
	
	if(h.length > 1)
	{
		path = h.replace('#','');
	}
	
	load_page('',path);
	
}


var init_map = function()
{
	var myOptions = {
		zoom: 14,
		disableDefaultUI: true,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	map = new google.maps.Map(document.getElementById("map"),myOptions);
	geocoder = new google.maps.Geocoder();
	geocoder.geocode( { 'address': addr }, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			map.setCenter(results[0].geometry.location);
			var infowindow = new google.maps.InfoWindow({
				content: popupText
			});
			var marker = new google.maps.Marker({
				map: map, 
				position: results[0].geometry.location
			});
			infowindow.open(map,marker);
		} else {
			alert("Geocode was not successful for the following reason: " + status);
		}
	});
}

var img_load = function()
{
	if($('#gallery img').length)
	{
	
	$('#loading').fadeIn();
	var num = $('#gallery img').length;
	var loaded = 0;
	$('#gallery img').each(function()
	{
		var $im = $(this);
		var i = new Image();
		$(i).load(function()
		{
			loaded++;
			$im.attr('src',this.src);
			if(loaded == num)
			{
				if($('#gallery').hasClass('transparent')) $('#gallery').fadeTo('',0.25);
				else $('#gallery').fadeIn();
				
						$('#loading').fadeOut();
			} 
		});
		i.src = $(this).attr('src');
	});
	}
}














