/* Elliot Barer, ebarer [at] mac [dot] com, 07/19/2009 */


$(document).ready(function() {

	var pages = new Array('home','','home','about','web','print','photo','contact');
	var $x = '';
	$('.ext').click(function(){ window.open(this.href); return false; });	//Open external links in new window

	function switchTo(page){
		if( pages[0] == page ) { return false; }
		else {
			$('#'+pages[0]+' > h2').removeClass('active').addClass('title').stop()
				.animate({'backgroundColor': '#111', 'color': '#444', 'padding-left': '5px'}, 550);
			$('#'+pages[0]).stop().animate({'height': '45px'}, 550).css({'cursor': 'pointer'}).find('.controls').css('display','none');
			
			pages[0] = page;
			
			$('#'+pages[0]+' > h2').removeClass('title').addClass('active').stop()
				.animate({'backgroundColor': '#191919', 'color': '#fff', 'padding-left': '25px'}, 550);
			$('#'+pages[0]).stop().animate({'height': '300px'}, 550).css({'cursor': 'default'}).find('.controls').css('display','inline-block');
			
			document.location = 'http://' + location.hostname + '#' + pages[0];
			document.title = 'Elliot Barer - ' + titleCaps(pages[0]);
			for(i=1; i < pages.length; i++) { if(pages[i] == page) { $x = i-3; } }
		}
	}
	

/* NAVIGATION */

	$(".content#about, .content#web, .content#print, .content#photo").find("h2")
		.append('<div class="controls"><a class="prev" href="javascript:void(0);" title="Previous">&lt;</a><a class="first" href="javascript:void(0);" title="First">&#9679;</a><a class="next" href="javascript:void(0);" title="Next">&gt;</a></div>')
		.each(function(){$("a.next", "a.prev").css("display","none");});

	$('.content h2').live('click', function(){ switchTo($(this).parent().attr('id')); });
	$('#home1').live('click', function(){ switchTo("contact") });
	
	
/* SUBNAVIGATION */
	
	var accordion = new Array(4);
	for(i=0; i < 4; i++){accordion[i] = new Array();}
	accordion[0]= $('#about .accordion').children();
	accordion[0].splice(0,0, $('#about .accordion'), 3, 3);
	accordion[1]= $('#web .accordion').children();
	accordion[1].splice(0,0, $('#web .accordion'), 3, 3);
	accordion[2]= $('#print .accordion').children();
	accordion[2].splice(0,0, $('#print .accordion'), 3, 3);
	accordion[3]= $('#photo .accordion').children();
	accordion[3].splice(0,0, $('#photo .accordion'), 3, 3);
	
	$('a.first').click(function(){
		$(accordion[$x][0].stop().animate({'left': '0px'}, 550));
		accordion[$x][1] = 3; accordion[$x][2] = 3;
	});
	
	$('a.next').click(function(){
		if( accordion[$x][1] < accordion[$x].length-1 ) {
			var left = parseInt($(accordion[$x][0]).css('left')) - parseInt($(accordion[$x][accordion[$x][1]]).css('width'));
			$(accordion[$x][0].stop().animate({'left': left+'px'}, 300));
			console.log(accordion[$x][0].attr('id'));
			accordion[$x][2] = accordion[$x][1]; accordion[$x][1]++;
		} else { 
			$(accordion[$x][0].stop().animate({'left': '0px'}, 550));
			accordion[$x][1] = 3; accordion[$x][2] = 3;
		}
	});

	$('a.prev').click(function(){
		if( accordion[$x][1] > 3 ) {
			var left = parseInt($(accordion[$x][0]).css('left')) + parseInt($(accordion[$x][accordion[$x][2]]).css('width'));			
			$(accordion[$x][0].stop().animate({'left': left+'px'}, 300));
			accordion[$x][1] = accordion[$x][2]; accordion[$x][2]--;
		} else { 
			$(accordion[$x][0].stop().animate({'left': '0px'}, 550));
			accordion[$x][1] = 3; accordion[$x][2] = 3;
		}
	});


/* HOVER */

	$('#home1').append('<span class="hover"></span>').each(function() {
		var $span = $('.hover', this).css('opacity', 0);
		$('#home1 a').hover( function(){ $span.stop().fadeTo(700, 1); },function(){ $span.stop().fadeTo(700, 0); });
	});


	$('.accordion div').each(function() {
		var $span = $('.hover', this).css('opacity', 0);
		$(this).hover( function(){ $span.stop().fadeTo(550, 1); },function(){ $span.stop().fadeTo(550, 0); });
	});
	
	$('.content h2.title')
		.live('mouseover', function() { $(this).stop().animate({'backgroundColor': '#191919', 'color': '#fff'}, 250); })
		.live('mouseout', function() { $(this).stop().animate({'backgroundColor': '#111', 'color': '#444'}, 250); });
		
	$('a.gallery').fancybox({
		'overlayOpacity':	0.75,
		'padding':			0,
		'zoomSpeedIn':		300,
		'zoomSpeedOut':		300
	});
	
	
/* CONTACT FORM */	
	
	$('input[title], textarea[title]').each(function() {
		if($(this).val() == ''){ $(this).val( $(this).attr('title') ); };
		$(this).focus(function(){ if($(this).val() == $(this).attr('title')) { $(this).val('').addClass('focused'); } });
		$(this).blur(function(){ if($(this).val() == '') { $(this).val( $(this).attr('title') ).removeClass('focused'); } });
	});
	
	$('form').submit(function() {
		var str = $(this).serialize();
		$.ajax({
			type: 'POST',
			url: 'scripts/contact.php',
			data: str,
			success: function(msg) {
    			$('#note').ajaxComplete(function(event, request, settings) {
					if(msg == 'OK') { 
						result = '<div class="notification_ok">Your message has been sent, thank you!</div>';
					} else { result = msg; }
					$(this).html(result);
				});
			}
		}); return false; });
		
	
/* ON LOAD - CHECK HASH EVERY 500MS*/		

	var timer = setInterval(function() {
		var hash = location.hash.toLowerCase().substring(1);
		if(hash != pages[0]) {
			if(hash != '') {
				switchTo(hash);
			} else {
				pages[0] = 'home';
				document.location = 'http://' + location.hostname + '#home';
				document.title = 'Elliot Barer - Home';
			}
		}
	}, 500);

});