// JavaScript Document
$(document).ready(function(){
						   
	$('.email').attr('href', $.rotate13('znvygb:tnvy@znqvfbavagrevbef.pbz')).html($.rotate13('tnvy<fcna fglyr="sbag-fvmr:14ck;">@</fcna>znqvfbavagrevbef.pbz'));
						   
	var section 	= 0; 
						   
	var portfolio	= [
		{	title	: "<h4>&#8220;Rio del Mar&#8221;</h4>Traditional",
			images	: ["rio_del_mar_03.jpg", "rio_del_mar_05.jpg", "rio_del_mar_new.jpg", "rio_del_mar_04.jpg", "rio_del_mar_01.jpg", "rio_del_mar_02.jpg", "rio_del_mar_06.jpg", "rio_del_mar_07.jpg"]
		},
		{	title	: "<h4>&#8220;Modern Classic&#8221;</h4>Green Design",
			images	: ["clark_01.jpg", "clark_06.jpg", "clark_03.jpg", "clark_04.jpg", "clark_05.jpg", "clark_02.jpg", "clark_07.jpg"]
		},
		{	title	: "<h4>&#8220;1920&#8217;s Renovation&#8221;</h4>Traditional",
			images	: ["1920s_04.jpg", "1920s_03.jpg", "1920s_02.jpg", "1920s_01.jpg", "1920s_05.jpg"]
		},
		{	title	: "<h4>&#8220;Belgian-Inspired&#8221;</h4>Contemporary Classic",
			images	: ["lipson_01.jpg", "lipson_02.jpg"]
		},
		{	title	: "<h4>&#8220;Half Moon Bay&#8221;</h4>Contemporary",
			images	: ["half_moon_bay_01.jpg", "half_moon_bay_02.jpg", "half_moon_bay_04.jpg", "half_moon_bay_03.jpg"]
		},
		{	title	: "<h4>&#8220;El Granada&#8221;</h4>Oceanfront Contemporary",
			images	: ["el_granada_01.jpg", "el_granada_07.jpg", "el_granada_02.jpg", "el_granada_05.jpg", "el_granada_09.jpg", "el_granada_08.jpg", "el_granada_10.jpg", "el_granada_04.jpg", "el_granada_03.jpg", "el_granada_06.jpg"]
		}
	];
	
	var press		= [
		{	title	: "<h4>Good Times</h4>September 23, 2010",
			images	: ["good_times_01.jpg", "good_times_02.jpg", "good_times_03.jpg"]
		},
		{	title	: "<h4>My House</h4>September &middot; October 2005",
			images	: ["my_house_01.jpg", "my_house_02.jpg", "my_house_03.jpg", "my_house_04.jpg", "my_house_05.jpg", "my_house_06.jpg", "my_house_07.jpg", "my_house_08.jpg", "my_house_09.jpg"]
		}
	];
	
	$.easing.easeOutSine = function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	}
	
	// START THE PORTFOLIO
	var portfolio_index	= $("#portfolio-index").clone();
	$("#content-portfolio").scrollable({
		circular		: true,
		mousewheel		: true,
		easing			: 'easeOutSine',
		onBeforeSeek	: function(event, i) {
			if (i==0) {
				if (section!=0) {
					addAll();
				}
				$("#footer-scroll-num").fadeOut(200, function(){
					$("#footer-scroll-num").text("");
				});
			}
		},
		onSeek	: function(event, i) {
			if (i!=0) {
				$("#footer-scroll-num").text(i + " of " + (this.getSize() - 1)).fadeIn(200);
			}
		}
	});
	var portfolio_api 	= $("#content-portfolio").data("scrollable");
	// ADD ALL ITEMS AS DEFAULT	
	addAll();
	
	// START THE PRESS SECTION
	$("#press").append("<div class=\"portfolio-frame frame-back portfolio-title\">" + press[0].title + "<br><img src=\"css/images/shell_small.png\" style=\"margin-top:2px;\"></div>");
	$("#content-press").scrollable({
		circular		: true,
		mousewheel		: true,
		onSeek	: function(event, i) {
			$("#footer-scroll-num").text((i+1) + " of " + this.getSize()).fadeIn(200);
		}
	});
	var press_api 		= $("#content-press").data("scrollable");	
	$.each(press, function(index, value) {
		press_api.addItem("<div class=\"portfolio-frame frame-back portfolio-title\">" + press[index].title + "<br><img src=\"css/images/shell_small.png\" style=\"margin-top:2px;\"></div>");
		$.each(press[index].images, function(index, value) {
			press_api.addItem("<div class=\"portfolio-frame frame-back\"><div class=\"portfolio-frame frame-loading\"><div style=\"background:url(press/" + value + ") 50% 50% no-repeat;\" class=\"portfolio-frame\"></div></div></div>");			   
		});
	});
	press_api.getItems().eq(0).remove();
	$("#press").css("width", (560 * (press_api.getSize()+4)) + "px");
	
	// START THE TABS
	$("ul.nav").tabs("#content > div", { 
		effect			: 'fade',
		fadeOutSpeed	: 800,
		fadeInSpeed		: 1600,
		history			: true,
		onBeforeClick	: function(event, tabIndex) {
			if (tabIndex==1) {
				addAll();
				$("#portfolio").css("left", "-560px" );
				$("#footer-scroll-num").fadeOut(200, function(){
					$("#footer-scroll-num").text("");
				});
				$("#footer-shell").fadeOut(800, function() {
					$("#footer-scroll").fadeIn(400);
				});				
			}
			if (tabIndex==2) {
				$("#side-scroll").fadeIn(800);
				$('#content-bio').animate({
					top: '0px'
				}, 400);
			} else {
				$("#side-scroll").fadeOut(400);
			}
			if (tabIndex==3) {
				$("#press").css("left", "-560px");
				$("#footer-scroll-num").text("1 of " + press_api.getSize()).fadeIn(200);
				$("#footer-shell").fadeOut(800, function() {
					$("#footer-scroll").fadeIn(400);
				});				
			} 			
			if (tabIndex!=1 && tabIndex!=3) {
				$("#footer-scroll").fadeOut(800, function() {
					$("#footer-shell").fadeIn(400);
				});
			}
		},
		onClick	: function(event, tabIndex) {
			if (tabIndex==1) {
				portfolio_api.seekTo(0,0);
			} else if (tabIndex==3) {
				press_api.seekTo(0,0);
			}
		}
	});	
	var tabs_api = $("ul.nav").data("tabs");
	
	$('#nav-1').bind('mousedown', function() {
		if (tabs_api.getIndex()==1) {
			portfolio_api.seekTo(0);
		}
	}).mousedown();
	
	$('#nav-3').bind('mousedown', function() {
		if (tabs_api.getIndex()==3) {
			press_api.seekTo(0);
		}
	}).mousedown();
	
	function addAll() {
		removeAll();
		$.each(portfolio, function(index, value) {
			portfolio_api.addItem("<div class=\"portfolio-frame frame-back portfolio-title\">" + portfolio[index].title + "<br><img src=\"css/images/shell_small.png\" style=\"margin-top:2px;\"></div>");
			$.each(portfolio[index].images, function(index, value) {
				portfolio_api.addItem("<div class=\"portfolio-frame frame-back\"><div class=\"portfolio-frame frame-loading\"><div style=\"background:url(portfolio/" + value + ") 50% 50% no-repeat;\" class=\"portfolio-frame\"></div></div></div>");
			});
		});
		section = 0;
		$("#portfolio").css("width", (560 * (portfolio_api.getSize()+4)) + "px");
	}
	
	function removeAll() {
		$.each(portfolio_api.getItems(), function(index, value) {
			portfolio_api.getItems().eq(1).remove();
		});
	}
	
	function addSection(index) {
		removeAll();		
		portfolio_api.addItem("<div class=\"portfolio-frame frame-back portfolio-title\">" + portfolio[(index-1)].title + "<br><img src=\"css/images/shell_small.png\" style=\"margin-top:2px;\"></div>");
		$.each(portfolio[(index-1)].images, function(index, value) {
			portfolio_api.addItem("<div class=\"portfolio-frame frame-back\"><div class=\"portfolio-frame frame-loading\"><div style=\"background:url(portfolio/" + value + ") 50% 50% no-repeat;\" class=\"portfolio-frame\"></div></div></div>");
		});
		section = index;
		portfolio_api.seekTo(1);
	}
	
	$('.portfolio-list li').bind('mouseup', function(e) {
		addSection($(this).attr("portfolio"));
	});
	
	$('#footer-scroll-left').bind('mouseup', function(e) {
		if (tabs_api.getIndex()==1) {
			portfolio_api.move((0-1));
		} else {
			press_api.move((0-1));
		}
		
	});
	$('#footer-scroll-right').bind('mouseup', function(e) {
		if (tabs_api.getIndex()==1) {
			portfolio_api.move(1);
		} else {
			press_api.move(1);
		}
	});
	
	$('#footer-scroll-top').bind('mouseup', function() {
		$('#content-bio').animate({
			top: '0px'
		}, 800);
	});
	$('#footer-scroll-bottom').bind('mouseup', function() {
		$('#content-bio').animate({
			top: '-464px'
		}, 800);
	});
	
});
