//********************************
// Document Load Functions
//********************************
$('document').ready(function(){
	//resize left and right col based on window width
	resizeCols();
	$(window).resize( function (){
		resizeCols();
	});
	
	$('.top_button').click(function(){
		var elementClicked = $(this).attr("href");
		$('html,body').animate({ scrollTop: 0}, 750, 'easeOutCubic');
	});
	
	//Get portfolio data via ajax after the page has loaded and start the slideshow.
	$('.next').click(function(){ scrollGalleryLeft(); });
	$('.prev').click(function(){ scrollGalleryRight(); });
	$('.next, .prev').mouseover(function(){ $('#slideshow').cycle('pause'); });
	$('.next, .prev').mouseout(function(){ $('#slideshow').cycle('resume'); });
	$('#slideshow_images').load('/pages/galleries #websites', function(){
		showImages();
		$('#slideshow').fadeIn('slow');
	});
	
	//Validate contact form
	$('#contact_us').validate({
   		rules: {
   			'data[Inquiry][firstname]': "required",
   			'data[Inquiry][lastname]': "required",
   			'data[Inquiry][email]': {
   				required: true,
   				email: true
   			},
   			'data[Inquiry][message]': "required"
   		},
   		messages: {
   			'data[Inquiry][firstname]': "*",
   			'data[Inquiry][lastname]': "*",
   			'data[Inquiry][email]': "*",
   			'data[Inquiry][message]': "*"
   		},
   		submitHandler: function(form) {
   			$.post('/pages/contact_ajax', $(form).serialize(), function(){
   				$('#contact_us').fadeOut('slow', function(){
   					$('#alert').html('Message Sent');
   					$('#alert').fadeIn('slow');
   				});
   			});
   			
   			return false;
   		}
	});
});

//********************************
// Page Behavior Functions
//********************************

function resizeCols(){
	var winWidth = $('body').innerWidth();
	var colWidth = winWidth / 2 - 50.25;
	$('.left_col').width(colWidth);
	$('.right_col').width(colWidth);
	
	//resize product column heights to be the same
	if($('.products_left').height() > $('.products_right').height()){
		$('.products_right').height($('.products_left').height());
	} else {
		$('.products_left').height($('.products_right').height());
	}
}

function goto(section){
	$('html,body').animate({ scrollTop: $('#'+section).offset().top }, 750, 'easeOutCubic');
}

//********************************
// Porfolio Functions
//********************************

var currentSlideShowPhotoOptions;
var currentGalleryIndex = 0;
var galleryArray = new Array('websites', 'platform', 'flash');//
var timer;

function changePortfolio(portfolio){
	alert(portfolio);
}

function showImages(startOnLastSlide){
	$('#slideshow').cycle({
		fx: 'scrollLeft', 
		timeout: 8000,
		easing: 'easeOutCubic',
		nowrap: true,
		height: 400,
		width: 808,
		fit: 1,
		startingSlide: ((startOnLastSlide) ?$('#slideshow > div').size() - 1 : 0),
		before: function (){
			$('.slideshow_details').hide();
		},
		after: function(currSlideElement, nextSlideElement, options, forwardFlag) { 
			currentSlideShowPhotoOptions = options;
			//Update the details and show.
			//console.log(this);
			$('.slideshow_title').html($(this).attr('name'));
			$('.slideshow_description').html($(this).attr('alt'));
			$('.slideshow_details').fadeIn('slow');
		},
		end: function(options) { changeGallery(); }
	});
}

function changeGallery(index, startOnLastSlide){
	clearTimeout(timer);
	$('#slideshow').cycle('destroy');
	$('#slideshow').fadeOut('slow');
	$('.slideshow_details').fadeOut('fast');
	if(index != null){
		currentGalleryIndex = index;
	} else {
		currentGalleryIndex = (currentGalleryIndex == galleryArray.length - 1) ? 0 : currentGalleryIndex + 1;
	}
	
	$('#slideshow_images').load('/pages/galleries #'+galleryArray[currentGalleryIndex], function(){
		if($('#slideshow').children().length == 0){
			changeGallery();
		} else {
			$('#slideshow').fadeIn('slow', function(){
				showImages(startOnLastSlide);
			});
		}
	});
}

function scrollGalleryRight(){
	//console.log(currentSlideShowPhotoOptions);
 	clearTimeout(timer);
 	if(currentSlideShowPhotoOptions.currSlide == 0){
  		currentGalleryIndex = ((currentGalleryIndex == 0) ? galleryArray.length - 1 : (currentGalleryIndex - 1));
  		changeGallery(currentGalleryIndex, true);
  	} else {
	 	$('#slideshow').cycle(currentSlideShowPhotoOptions.currSlide - 1, 'scrollRight');
 	}
 }
 
function scrollGalleryLeft(){ 	
	//console.log(currentSlideShowPhotoOptions);
	clearTimeout(timer);
  	$('.slideshow_details').fadeOut('fast');
  	if(currentSlideShowPhotoOptions.currSlide == currentSlideShowPhotoOptions.slideCount - 1){
  		currentGalleryIndex = ((currentGalleryIndex == galleryArray.length - 1) ? 0 : (currentGalleryIndex + 1));
  		changeGallery(currentGalleryIndex);
  	} else {
	 	$('#slideshow').cycle(currentSlideShowPhotoOptions.currSlide + 1, 'scrollLeft');
 	}
 }
 
//********************************
// Contact Form Functions
//********************************



