// default

var image_index;

//
$.postJSON = function (url, data, callback) {
	$.post(url, data, callback, 'json');
};

//
$(function(){
	
	$(window).scroll(function () {
		if($(document).scrollTop() > $('#gallery').height()){
			$('#left').css({'marginTop': $(document).scrollTop() - $('#gallery').height()});
		} else {
			$('#left').css({'marginTop': 0 });
		}
	});
	
	$('#nav>li>a').bind('click', function(){
		var href = $(this).attr('href');
		var offset = $(href).offset();
		
		if(href != '#home'){
			$('html, body').animate({ scrollTop: offset.top - 246 }, 1000);
		} else {
			$('html, body').animate({ scrollTop: 0 }, 1000);
		}
		
		return false;
	});
	
	$('.thumbnail').live('click', function(){
		$.postJSON('/ajax/', {'command': 'images', 'size': 'medium', 'gallery': $('#select').val(), 'index': $(this).data('index') }, function(data){
			if(data.success == true){
				$('#images').cycle('destroy').empty();
				$('#images').html(data.html);
				startCycle(data.index);
			}
		});
		
		return false;
	});
	
	$('#thumbs-btn').live('click', function(){
		
		if($('#viewer').height() != 375){
			$('#images').cycle('destroy').empty();
			$('#content').slideDown(500);
			$('#gallery').animate({'height': '540px'}, 1000);
			$('#viewer').animate({'paddingLeft': '145px', 'height': '375px'}, 1000);
			$('#images').animate({'height': '375px', 'width':'690px'}, 1000);
			$('#controls').animate({'marginLeft': '195px', 'width':'640px'}, 1000)
		}
		
		$.postJSON('/ajax/', {'command': 'images', 'size': 'grid', 'gallery': $('#select').val(), 'index': '0' }, function(data){
			if(data.success == true){
				$('#images').cycle('destroy').empty();
				$('#images').html(data.html);
				startCycle(data.index);
			}
		});
		
		return false;
	});
	
	$('#full-btn').live('click', function(){
		
		if($('#viewer').height() == 375){
		
			var height = $(window).height();
			var image_index = $('.medium:visible').index();
			
			$('#images').cycle('destroy').empty();
			
			$('#content').slideUp(500);
			$('#gallery').animate({'height': height+'px'}, 1000);
			$('#viewer').animate({'paddingLeft': '0px', 'height': (height-165) +'px'}, 1000);
			$('#images').animate({'height': (height-165) +'px', 'width':'835px'}, 1000);
			$('#controls').animate({'marginLeft': '0px', 'width':'835px'}, 1000, function(){
				
				$.postJSON('/ajax/', {'command': 'images', 'size': 'large', 'gallery': $('#select').val(), 'index': image_index }, function(data){
					if(data.success == true){
						$('#images').html(data.html);
						startCycle(data.index);
					}
				});
				
			});
		
		} else {
			$('#thumbs-btn').trigger('click');
		}
		
		return false;
	});
	
	$('#close-btn').live('click', function(){
		if($('#viewer').height() > 375){
			$('#thumbs-btn').trigger('click');
		} else {
			$('#gallery').animate({ 'height' : '35px'})
		}
	});
	
	$('#select').live('change', function(){
		$('#thumbs-btn').trigger('click');
	});
	
	$('#photography-btn').bind('click', function(){
		if($('#gallery').height() == 35){
			$('#gallery').animate({ 'height' : '540px'})
			$('#viewer,#controls').slideDown(1000);
		}
		
		if(!$(this).hasClass('sel')){
			$(this).toggleClass('sel');
			$('#printing-btn').toggleClass('sel');
			getGalleries('photography');
		}
		return false;
	});
	
	$('#printing-btn').bind('click', function(){
		if($('#gallery').height() == 35){
			$('#gallery').animate({ 'height' : '540px'})
		}
		
		if(!$(this).hasClass('sel')){
			$(this).toggleClass('sel');
			$('#photography-btn').toggleClass('sel');
			getGalleries('printing');
		}
		return false;
	});
	
	if((($(window).height() - $('#splash>div').height()) / 2) > 0){ y_pos = (($(window).height() - $('#splash>div').height()) / 2);	} else {y_pos = 0;}
	if((($(window).width() - $('#splash>div').width()) / 2) > 0){ x_pos = (($(window).width() - $('#splash>div').width()) / 2);	} else { x_pos = 0;}
	$('#splash>div').css({'position': 'absolute','top': y_pos,'left': x_pos});
	$('#splash').delay(5000).fadeOut(1000, function(){$('.websiteOverlay').fadeIn(1000, getGalleries('photography'));});
	$('#splash').click(function(){$('#splash').stop().fadeOut(1000, function(){$('.websiteOverlay').fadeIn(1000, getGalleries('photography'));});});
	
	startCycle();
	
});

function startCycle(index){

	if(!index || index == null || index < 0){ index = 0; }

	$('#images').cycle({ 
		fx:				'scrollHorz', 
		speed:			1000,
		timeout:		0,
		pause:			1,
		cleartype:		1,
		cleartypeNoBg:	1,
		next:   '#next-btn', 
    	prev:   '#prev-btn',
		startingSlide: index
	 });
		
}

function getGalleries(type){

	$.postJSON('/ajax/', {'command': 'galleries', 'type': type }, function(data){
		if(data.success == true){
			$('#select').html(data.html);
			$('#select').trigger('change');
		}
	});
	
}
