function initTypeKit() {
	try{
		Typekit.load();
	} catch(e) { }
}

function initAddress() {
	$.address.init(function(event) {
		$('#carousel-clip a').address(function() {
			var url = $(this).attr('href').split('#!');
			return url[1];
		});
		if(!event.pathNames[0]) {
			var url = $('#carousel-clip ul li:first a').attr('href').split('#!');
			$.address.path(url[1]);
		}
	}).change(function(event) {	
		if(event.pathNames[0]) {
			$('#photo img').remove();
			$('#photo').addClass('loading');
			$("#info").hide();
			$.getJSON(location.pathname + 'image/' + event.pathNames[0] + '/', function(data, textStatus, XMLHttpRequest) { handler(data); });
		}
	});	
}

function initPortfolio() {
	$('#portfolios li').css('cursor','pointer');
	$('#portfolios li').hover(
		function() {
			$(this).addClass('hover');
			$(this).find('img.highlight').fadeTo("slow",1);
		},
		function() {
			$(this).removeClass('hover');
			$(this).find('img.highlight').fadeTo("fast",0);
		}
	);
	$('#portfolios li').click(
		function() {
			window.location = $(this).find('div a').attr('href');	
		}
	);
}

function initCarousel() {
	updateCarousel();
	$('#carousel-clip ul li a').hover(
		function() {
			$(this).find('img.fader').fadeTo("fast",0.5);
		},
		function() {
			$(this).find('img.fader').fadeTo("slow",1);
		}
	);
	$('#carousel-up, #carousel-down').click(carousel);
	$('#controls .prev, #controls .next').click(control);
}

function control() {
	var current = $('#carousel-clip a img.highlight.on').parent().parent().prevAll().length;
	var total = $('#carousel-clip a img.highlight.on').parent().parent().siblings().length;
	if($(this).hasClass('next') && current!=total) {
		var url = $('#carousel-clip li:eq('+(current+1)+') a').attr('href').split('#!');
		$.address.path(url[1]);
	} else if ($(this).hasClass('prev') && current!=0) {
		var url = $('#carousel-clip li:eq('+(current-1)+') a').attr('href').split('#!');
		$.address.path(url[1]);
	}
}

function updateControls(objRef) {
	var current = $(objRef).parent().prevAll().length;
	var total = $(objRef).parent().siblings().length;
	if(current==0) {
			$('#controls .prev').fadeTo('fast',0.25);
			$('#controls .prev').removeClass('click');
			$('#controls .prev').attr('title','');
		} else {
			$('#controls .prev').fadeTo('fast',1);
			$('#controls .prev').addClass('click');
			$('#controls .prev').attr('title',$('#carousel-clip li:eq('+(current-1)+') a img.highlight').attr('title'));
		}
		if (current==total) {
			$('#controls .next').fadeTo('fast',0.25);
			$('#controls .next').removeClass('click');
			$('#controls .next').attr('title','');
		} else {
			$('#controls .next').fadeTo('fast',1);
			$('#controls .next').addClass('click');
			$('#controls .next').attr('title',$('#carousel-clip li:eq('+(current+1)+') a img.highlight').attr('title'));
		}
}

function updateCarousel() {
	if(parseInt($('#carousel-clip ul').css('top'))==0) {
		$('#carousel-up').fadeTo('fast',0.25);
		$('#carousel-up').removeClass('click');
	} else {
		$('#carousel-up').fadeTo('fast',1);
		$('#carousel-up').addClass('click');
	}
	if($('#carousel-clip ul').outerHeight()+parseInt($('#carousel-clip ul').css('top'))<$('#carousel-clip').outerHeight()+11) {
		$('#carousel-down').fadeTo('fast',0.25);
		$('#carousel-down').removeClass('click');
	} else {
		$('#carousel-down').fadeTo('fast',1);
		$('#carousel-down').addClass('click');
	}	
}

function initInfo() {
	$('#controls .info').click(function() {
		if(info==true) {
			info=false;
			$("#info").stop().animate({bottom:0-$('#info').outerHeight()-1+'px'},{queue:false,duration:150});
		} else {
			info=true;
			$("#info").css('bottom',0-$('#info').outerHeight()-1+'px');
			$("#info").stop().animate({bottom:'0'},{queue:false,duration:500,easing:'easeOutBounce'});
		}
	});	
}

function carousel(direction, page) {
	if (direction.originalEvent) { var direction = null; }
	if (!page || page==0) { var page = 1; }
	
	var top_from = (direction!='down') ? parseInt($('#carousel-clip ul').css('top')) : 0;
	var scroll_direction = (direction==null) ? $(this).attr('id') : 'carousel-'+direction; 
	var item_height = ($('#carousel-clip').outerHeight()+10)*page;
	var top_indent = (scroll_direction=='carousel-up') ? top_from + (item_height) : top_from - (item_height);
	
	if ($("#carousel-clip ul:animated").length>0) return false;
			
	if (scroll_direction=='carousel-up'&&top_from==0 || (top_indent+($('#carousel-clip ul').outerHeight()*page))<11) return false;
	
	$('#carousel-clip ul').animate({'top' : top_indent}, 250, function() {
		updateCarousel();
	});
}

function carouselShow(id) {
	var position = $('#thumb_'+id).offset();
	var menu_position = $('#carousel-clip').offset();
	if(position.top>(menu_position.top+$('#carousel-clip').outerHeight())) {
		var page = 1;
		while(position.top>(menu_position.top+($('#carousel-clip').outerHeight()*page))) {
			page++;
		}
		carousel('down', page-1);
	} else if (position.top<(menu_position.top+$('#carousel-clip').outerHeight())) {
		var pages = Math.round(($('#carousel-clip ul').outerHeight()-11)/$('#carousel-clip').outerHeight());
		var page = pages;
		while(position.top<(menu_position.top+($('#carousel-clip').outerHeight()*page))) {
			page--;
		}
		carousel('up', (page*-1)-1);
	}
}

function handler(data) {
	carouselShow(data.id);	
	$.address.title(data.title+" | "+base_title);
	var img = new Image();
	$(img).load(function () {
		$(this).hide();
		if(data.title!='') {
			$('#info h1').text(data.title);
			$('#info h1').show();
		} else {
			$('#info h1').hide();
		}
		if(data.caption!='') {
			$('#info p').text(data.caption);
			$('#info p').show();
		} else {
			$('#info p').hide();
		}
		$('img.highlight.on').fadeTo(10,0);
		$('img.highlight.on').removeClass("on");
		$('#thumb_'+data.id+' img.highlight').addClass("on");
		$('#thumb_'+data.id+' img.highlight').fadeTo(150,1);
		updateControls($('#thumb_'+data.id+' a'));
		$('#photo').removeClass('loading').prepend(this);
		$("#info").css('bottom','-300px');
		$('#info').css('width', data.width-20+'px');
		$('#photo').animate({width: data.width+'px', height: data.height+'px', top: parseInt(($('#frame').outerHeight()-data.height)/2)+'px', left: parseInt(($('#frame').outerWidth()-data.width)/2)+'px'}, 500, 'easeOutCirc', function() { $('#photo img').fadeIn(); $('#info').show(); if (info) { $("#info").hide().css('bottom','0').fadeIn(); } });
		
	}).error(function () {
		// notify the user that the image could not be loaded
	}).attr('src', data.url);
	
};
