//General functions for SI
$(document).ready(function()
{
	
	$('a.newtab').click(function (e) 
	{ 
		e.preventDefault();			
		window.open($(this).attr('href'));
	});
	
	if($("#homePortfolioSingleRight").length>0)
	{
		/* Portfolio Page */
		$('#homePortfolioSingleRight #images_container').cycle({
			fx: 'fade',
			pager: '.image_controls',
			activePagerClass: 'active'
		});
	}
	
	if($("#portfolioInner").length>0)
	{
		
	/* Flipper for 'greatest hits' */
	$('#hit1').quickFlip({easing: "easeInOutQuad"});
	$('#hit2').quickFlip({easing: "easeInOutQuad"});
	$('#hit3').quickFlip({easing: "easeInOutQuad"});
	$('#hit4').quickFlip({easing: "easeInOutQuad"});
	
	$('.moreInfo').bind('click', function(){

		$("#portfolioItemContainer").trigger("slideTo", ["#portfolio-"+$(this).attr('id')]);
		$.scrollTo("#top", 1000, {easing:"easeInOutQuad"});
		
	});
	
	$(".hitContainer").bind('click', function(){
		$(this).quickFlipper();
	})	
	
	if(window.location.hash)
	{
		var split = window.location.hash.split("-");
		var firstelement = "#portfolio-"+split[1];
	}
	else
	{
		var firstelement = false;
	}
	
	$("#portfolioItemContainer").carouFredSel({
		width: 940,
		padding: [0],
		items: {
			visible: 1,
			width: "variable",
			height: "variable",
			start: firstelement
		},
		scroll: {
			easing: "easeInOutQuad",
			duration: 500,
			onBefore: function( oldItems, newItems, newSizes, duration ) {
				$(newItems).find('.portfolioSlideshowContent').cycle({
					pager: '#'+$(newItems).attr('id')+' .image_controls_icons',
					activePagerClass: 'active',
					after: function(subCurrentElement, subNextElement, opts, a)
					{
						$(newItems).find('.image_controls .image_controls_number').html('Image '+(opts.currSlide+1)+' / '+opts.slideCount);
					}
				});

				/* Fade out quote, re-populate with new quote and fade in */
				$('#portfolioQuoteText').fadeOut('slow', function(){
					$('#portfolioQuoteText').html($(newItems).children('.hiddenQuote').html());
					$('#portfolioQuoteText').fadeIn('slow');
				})
			},
			onAfter: function( oldItems, newItems, newSizes ) {
				/* Destory slideshow on current slide, to free up resources */
				$(oldItems).find('.portfolioSlideshowContent').cycle('destroy');
			}
		},
		auto: false,
		prev: {
			button: "#viewPrevious",
			key: "left"
		},
		next: {
			button: "#viewNext",
			key: "right"
		}
	}).find('.portfolioSlideshowContent:first').cycle({
			pager: '.image_controls_icons:first',
			activePagerClass: 'active',
			after: function(subCurrentElement, subNextElement, opts, a)
			{
				$('.image_controls:first .image_controls_number').html('Image '+(opts.currSlide+1)+' / '+opts.slideCount);
			}
		});
		
		/* Fade out quote, re-populate with new quote and fade in */
		$('#portfolioQuoteText').html($('.hiddenQuote:first').html());
		
	}
	
	if($("#envelopeContainer"))
	{
		$("#envelopeContainer #message, #envelopeContainer #name, #envelopeContainer #email-add, #envelopeContainer #tel").bind('click, focus', function()
		{
			if($(this).data('cleared')!=true)
			{
				$(this).data('cleared', true);
				$(this).data('default', $(this).val());
				$(this).val('');
			}
			else
			{
				if($(this).data('default')==$(this).val())
				{
					$(this).val('');
				}
			}
			
		});
		
		$("#envelopeContainer #message, #envelopeContainer #name, #envelopeContainer #email-add, #envelopeContainer #tel").bind('blur', function()
		{

			if($(this).val() == '')
			{
				$(this).val($(this).data('default'));
			}
			
		});
				
		/* Contact Envelope */
		$('#envelopeContainer').quickFlip({easing: "easeInOutQuad"});
		
		$("#envelopeContainer .submit").bind('click', function(){
			
			var failed = false;
			var errorMessage = 'Please enter the following information:\n\n';
			$("input, textarea").removeClass("error");
			
			if($("#envelopeContainer #message").val().length == 0 || $("#envelopeContainer #message").val() == "*message")
			{
				$("#envelopeContainer #message").addClass('error');
				errorMessage = errorMessage + '- Your message\n';
				failed = true;
			}
			
			if($("#envelopeContainer #name").val().length == 0 || $("#envelopeContainer #name").val() == "*name")
			{
				$("#envelopeContainer #name").addClass('error');
				errorMessage = errorMessage + '- Your name\n';
				failed = true;
			}
			
			if($("#envelopeContainer #email-add").val().length == 0 || $("#envelopeContainer #email-add").val() == "*email address")
			{
				$("#envelopeContainer #email-add").addClass('error');
				errorMessage = errorMessage + '- Your email\n';
				failed = true;
			}
			
			if(failed == false)
			{
				
				$.ajax({
					type: 'POST',
					url: 'ajax/contact.php',
					data: {
						message: $("#envelopeContainer #message").val(),
						name: $("#envelopeContainer #name").val(),
						email: $("#envelopeContainer #email-add").val(),
						tel: $("#envelopeContainer #tel").val()
					},
					dataType: 'html'
				});
				
				// Send message, flip the flipper
				$("#envelopeContainer").quickFlipper();
				
				
				
			}
			else
			{
				alert(errorMessage);
			}
			
			
		})
		/* End of contact envelope */
	}	
	
}); //END DOCUMENT READY



