var btns_y = new Array();
var btns_x = new Array();

var f_down = 0;
var f_curr = 'all';
var p_curr = 1;

btns_x[0] = 0;
btns_x[1] = -128;
btns_x[2] = -256;
btns_x[3] = -384;

$(document).ready(function() {
	$('#hello').fadeIn(800);
	
	if ($(window).height() < 750) $('#pstay').css('position', 'absolute');
	
	$.easing.easeIn = function (x, t, b, c, d) { 
	    return c*(t/=d)*t + b;
	}
	$.easing.easeOut = function (x, t, b, c, d) { 
		return -c*(t/=d)*(t-2) + b;
	}

	$('._btn').each(function(i){
		btns_y[i] = parseInt($(this).css('background-position').split(' ')[1]);
		$('#'+this.id).css('background-position', btns_x[i]+"px "+btns_y[i]+"px");
		if ($('#'+this.id.substr(2)).hasClass('current')) {
			var y = btns_y[i]-25;
			y = y < 0?0:y;
			$('#'+this.id).css('background-position', btns_x[i]+"px "+y+"px");
		}
		} ); 
	$('.btn')
		.mouseover(function(){
			if ($('#'+this.id).hasClass('current')) return;
			var i = $('.btn').index(this);
			var y = btns_y[i]-8;
			$('#'+this.id).css('background-position', btns_x[i]+"px "+y+"px");
			$('#n_'+this.id).stop().animate({backgroundPosition:"("+btns_x[i]+"px "+y+"px)"}, { duration:100, easing:'easeIn'});
		})
		.mouseout(function(){
			if ($('#'+this.id).hasClass('current')) return;
			var i =$('.btn').index(this);
			var y = btns_y[i];
			$('#n_'+this.id).stop().animate({backgroundPosition:"("+btns_x[i]+"px "+y+"px)"}, { duration:200, easing:'easeIn'});
		})

	$('#filter_head').click(function () {
		if (f_down == 0) $('#filter_ul').slideDown('fast', function () { f_down = 1 });
	});
		
	$('#filter_ul li').click(function () {
		if (f_down == 1) {
			$('#f_'+f_curr).removeClass('filter_on');
			f_curr = this.id.substr(2);
			$('#filter_ul').slideUp('fast', function () {
				f_down = 0;
				$('#filter_show').html(f_curr == 'all'?"ALL PROJECTS":f_curr.toUpperCase())
					$('#f_'+f_curr).addClass('filter_on');
				get_portfolio(f_curr);
			});
		}
	});
	
	$('#jump_head').click(function () {
		if (f_down == 0) $('#jump_ul').slideDown('fast', function () { f_down = 1 });
	});

	$('#jump_ul li').click(function () {
		if (f_down == 1) {
			var jam = this.id.substr(2);
			$('html,body').animate({ scrollTop: $("#"+jam).offset().top },'slow');
			$('#jump_ul').slideUp('fast', function () {
				f_down = 0;
			});
		}
	});
	
	$('a[rel=load_folio]').click(function () {
		var p = this.id.substr(2);
		$('#portfolio').fadeOut(400, function () {
			$('#p_loading').addClass('loading');
			load_project(p);
		});
		return false;
	});
	$('a[rel=nav_folio]').click(function () {
		var p = this.href.split("=")[1];
		$('#portfolio').fadeOut(400, function () {
			$('#p_loading').addClass('loading');
			load_project(p);
		});
		return false;
	});

	$("a.lemmesee").attr('rel', 'gallery').fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 200, 'overlayShow': true, 'overlayOpacity': 0.5, 'overlayColor': '#000', 'titleShow' : false });
	$("a.load_vid").attr('rel', 'gallery').fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'titleShow' 	: false,
				'width'			: 640,
				'height'		: 495,
				'href'			: this.href,
				'type'			: 'swf',
				'swf'			: {
				   	'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'}
			});
	$('.port_vid').hover(
		function () {
			if (this.src.substr(this.src.length-5, 2) != '_o') this.src = this.src.replace(".jpg", "_o.jpg");
		},
		function () {
			if (this.src.substr(this.src.length-5, 2) == 'o.') this.src = this.src.replace("_o.jpg", ".jpg");
		});
		
	var hash = window.location.hash.substr(1, window.location.hash.length);
	if (hash && $('#portfolio').length) load_project(hash);
});

function get_portfolio(f) {
	$.get('jax.php', { 'f': f }, function(d) {
	 	$('#thumbs').html(d);
		$('a[rel=load_folio]').click(function () {
			var p = this.id.substr(2);
			$('#portfolio').fadeOut(400, function () {
				$('#p_loading').addClass('loading');
				load_project(p);
			});
			return false;
		});
		$('a[rel=nav_folio]').click(function () {
			var p = this.href.split("=")[1];
			$('#portfolio').fadeOut(400, function () {
				$('#p_loading').addClass('loading');
				load_project(p);
			});
			return false;
		});
		load_project('refresh');
	})
}

function load_project(p) {
	if (p != 'refresh') parent.location.hash = p;
	p_curr = $('.thumbnail').index($('#t_'+p))+1;
	$('.thumbnail').removeClass('thumbcurr');
	$.get('jax.php', { 'f': f_curr, 'p': p, 'c' : p_curr }, function(d) {
		$('#portfolio').html(d);
		$('#portfolio').fadeIn(600);
		$('#p_loading').removeClass('loading');
		$('#digits').html($('#curr').val());
		$('#p_next').attr("href", 'portfolio?p='+$('#r_next').val())
		$('#p_prev').attr("href", 'portfolio?p='+$('#r_prev').val())
		if (p == "refresh") $('.thumbnail:first').addClass('thumbcurr');
		else $('#t_'+p).addClass('thumbcurr');

		document.title = $('#r_title').val()+" : Portfolio : Lumbroso Productions";

		setTimeout(function () {
			$("a.lemmesee").attr('rel', 'gallery').fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 200, 'overlayShow': true, 'overlayOpacity': 0.5, 'overlayColor': '#000', 'titleShow' : false });
			$("a.load_vid").attr('rel', 'gallery').fancybox({
						'padding'		: 0,
						'autoScale'		: false,
						'transitionIn'	: 'none',
						'transitionOut'	: 'none',
						'title'			: this.title,
						'titleShow' 	: false,
						'width'			: 640,
						'height'		: 495,
						'href'			: this.href,
						'type'			: 'swf',
						'swf'			: {
						'wmode'			: 'transparent',
						'allowfullscreen': 'true'}
					});
			$('.port_vid').hover(
				function () {
					if (this.src.substr(this.src.length-5, 2) != '_o') this.src = this.src.replace(".jpg", "_o.jpg");
				},
				function () {
					if (this.src.substr(this.src.length-5, 2) == 'o.') this.src = this.src.replace("_o.jpg", ".jpg");
				});
		}, 400);
	});
}

function vid_href(s) {
	if (s.match(/vimeo/gi))
		return s.replace(new RegExp(".com\/", "i"), '.com/moogaloop.swf?clip_id=')+'&server=vimeo.com&show_title=0&show_byline=0&show_portrait=0&color=33a3ff&fullscreen=1&autoplay=1';
	else if (s.match(/youtube/gi))
		return s.replace(new RegExp("watch\\?v=", "i"), 'v/')+'&fs=1&autoplay=1';
	else return s;
}
