var hideDropDownTimer = 0;
var uploadError = 0;


function hideDropDown( dropDownName )
{
	$('ul#' + dropDownName).slideUp(300, function(){ $('ul#mainMenu li.submenu').removeClass('submenu'); });
}




var Portfolio =
{
	slideCount:0,
	slideIndex:0,
	slideTimer:0,


	init : function()
	{
		$('ul#workSlider li').each(function()
		{
			if( !$(this).hasClass('current'))
				$(this).hide();
		});

		this.slideCount = $('ul#workSlider li').length;
		this.slideTimer = setInterval("Portfolio.nextSlide()", 3000);
	},
	
	
	nextSlide : function()
	{
		var nextElement = $( "ul#workSliderBar li.current + li" );
		
		if( nextElement.text() == "" )
			nextElement = $( "ul#workSliderBar li:first-child" );
		
		$('ul#workSliderBar li.current').removeClass('current');
		nextElement.fadeIn(300, function(){ nextElement.addClass('current'); });
		
		
		$('ul#workSlider li.current').fadeOut(300, function()
		{
			var nextPhotoElement = $('ul#workSlider li.current + li');
			
			if( nextPhotoElement.children().length == 0 )
				nextPhotoElement = $('ul#workSlider li:first-child');
				
			$('ul#workSlider li.current').removeClass('current');
			nextPhotoElement.fadeIn(300, function(){ nextPhotoElement.addClass('current'); });		
		});
	}
};



function completedItem(event, ID, fileObj, response, data)
{
	// Store the new file names in a hidden textarea
	$('div.uploadifyQueueItem#file_upload' + ID + ' div.cancel').remove();
	$('form#myFileUploader textarea#uploadedFiles').append(response + ',');
}


function onErrorItem(event,ID,fileObj,errorObj)
{
	if(errorObj['type'] == 'File Size')
	{
		alert('The file you\'ve chosen is too large to upload. Please make sure the file is under 100MB in size.');
		$('div.uploadifyQueueItem#file_upload' + ID).remove();
	}
	else
		uploadError = 1;
}


function completedAll(event,data)
{
	if( uploadError == 0 )
	{
		$('form#myFileUploader p.progressSpinner').val('Submitting your info&#8230;');
	
		// trim the last comma in the textarea
		var upFiles = $('form#myFileUploader textarea#uploadedFiles').val( $('form#myFileUploader textarea#uploadedFiles').val().slice(0, -1));
		
		// Submit the form when all files have been submitted.
		setTimeout("$('form#myFileUploader').submit()", 500);	
	}
	else
	{		
		// Clear the uploaded files list...
		$('form#myFileUploader textarea#uploadedFiles').val('');
		
		// Submit form
		setTimeout("$('form#myFileUploader').submit()", 500);
	}
}




$( document ).ready( function()
{
	// Pricing page visual fix - table fade out
	$( 'body#my_pricing div#fadeOutTable' ).height( $( 'body#my_pricing div#costBreakdownScroller' ).height());
	
	
	// Set the focus for the user name on the uploader page.
	$('form#myFileUploader input#userName').focus();
	
	// Manage the characters input for the telephone field
	$('form#myFileUploader input#userPhone').keydown(function(event)
	{
		var keychar = String.fromCharCode(event.which);
	
		// Control keys
		if( event.which == null || event.which == 0 || event.which == 8 || event.which == 9 )
			return true;
		
		// Allowed characters
		else if(("0123456789").indexOf(keychar) > -1 )
			return true;
		
		// Allowed (-)
		else if( event.which == 189 )
			return true;
		
		// Allowed (.)
		else if( event.which == 190 )
			return true;

		return false;
	});
	
	
	
	// Upload files
	$('input.action_startUploadProcess').click(function()
	{
		var error = 0;
		
		// Check the queue quantity
		if( $('div#file_uploadQueue div.uploadifyQueueItem').length == 0 )
			error = 2;
		
		// Check that all required fields have a value.
		$('form#myFileUploader input[required]').each(function()
		{
			if( $(this).val() == '' )
			{
				error = 1;
				$(this).addClass('error');
			}
			else
				$(this).removeClass('error');
			
		});
		
		
		// Validate the email address
		if($('form#myFileUploader input#userEmail').val() != '' )
		{
			// Validate the email address
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			var emailAddress = $('form#myFileUploader input#userEmail').val();
	
			if( reg.test(emailAddress) == false )
			{
				$('form#myFileUploader input#userEmail').addClass('error');
				error = 3;
			}		
		}
		

		// There were no errors
		if( error == 0 )
		{
			$('form#myFileUploader input#userName').attr('readonly', 'readonly');
			$('form#myFileUploader input#userPhone').attr('readonly', 'readonly');
			$('form#myFileUploader input#userEmail').attr('readonly', 'readonly');
			$('form#myFileUploader textarea#userNotes').attr('readonly', 'readonly');
			$('form#myFileUploader input.sendButton').attr('disabled', 'disabled');
		
			// Hide the select button to prevent the user selecting more files in this session
			$('#file_upload').uploadifySettings('hideButton', true);
			$('form#myFileUploader p.progressSpinner').show();
			$('#file_upload').uploadifyUpload();			
		}
		
		// A required field error occurred.
		else if( error == 1 )
		{
			// Set the focus to the first input field with an error
			$('form#myFileUploader input.error:first').focus();

			alert('Please fill out all required fields.');
		}
		
		// A file queue error occurred.
		else if( error == 2 )
			alert('Please add files to the upload queue.');
		
		//
		else if( error == 3 )
			alert('Please double check your email address.');
	
		return false;
	});
	
	
	// Show submenu drop Down
	$( 'ul#mainMenu li.action_onHover a' ).mouseover( function()
	{
		$(this).parent('li.action_onHover').addClass('submenu');
		clearTimeout(hideDropDownTimer);
		
		var dropDownName = $(this).parent('li.action_onHover').attr('id') + '-DropDown';
		$('ul#' + dropDownName).slideDown(300);
	});
	
	
	// Hide submenu drop down
	$( 'ul#mainMenu li.action_onHover a' ).mouseout( function()
	{
		var dropDownName = $(this).parent('li.action_onHover').attr('id') + '-DropDown';
		
		// Give the user a second before we actually hide the drop down, this gives the opportunity to cancel the hide.
		hideDropDownTimer = setTimeout("hideDropDown('" + dropDownName + "')", 200 );
	});
	
	
	// Prevent clicks for onHover buttons
	$( 'ul#mainMenu li.action_onHover a' ).click( function(){ return false; });
	
	
	// 
	$( 'ul.dropDownMenu' ).mouseover( function()
	{
		clearTimeout(hideDropDownTimer);
	});
	
	
	$( 'ul.dropDownMenu' ).mouseout( function()
	{
		var dropDownName = $(this).attr('id');
		
		// Give the user a second before we actually hide the drop down, this gives the opportunity to cancel the hide.
		hideDropDownTimer = setTimeout("hideDropDown('" + dropDownName + "')", 200 );
	});
	
	
	
	// Fix rounded corners for IE browsers
	if( $.browser.msie )
	{
		// Add rounded corner images to the content blocks with a white background.
		if( $.browser.version == '7.0' || $.browser.version == '8.0' )
		{
			$('div.contentBlock.withBackground').append('<div class="ie_topLeftCorner"></div><div class="ie_topRightCorner"></div><div class="ie_bottomLeftCorner"></div><div class="ie_bottomRightCorner"></div>');
		}
	}
	
	
	// 
	$('body#my_services div#productsPane ul li a').click( function()
	{
		if( $(this).parent('li').hasClass('current'))
			return false;
	
		$('div#productsPane ul li.current').removeClass('current');
		$(this).parent('li').addClass('current');
		
		$('ul#productSamples li.current').fadeOut(300, function()
		{
			$(this).removeClass('current');
			
			var counter = 0;
			$('div#productsPane ul li').each( function()
			{
				counter += 1;
				
				if( $(this).hasClass('current'))
				{
					$('ul#productSamples li:nth-child(' + counter + ')').addClass('current');
					$('ul#productSamples li:nth-child(' + counter + ')').fadeIn();
					return false;
				}	
			});
		});
		
		return false;
	});
	
	
	// 
	$('ul#workSliderBar li a').click( function()
	{
		clearTimeout(Portfolio.slideTimer);
		
		$('ul#workSliderBar li.current').removeClass('current');
		$(this).parent('li').addClass('current');	
		
		$('ul#workSlider li.current').fadeOut( 300, function()
		{
			$(this).removeClass('current');
			
			var counter = 0;
			$('ul#workSliderBar li').each( function()
			{
				counter += 1;
				
				if( $(this).hasClass('current'))
				{
					$('ul#workSlider li:nth-child(' + counter + ')').addClass('current');
					$('ul#workSlider li:nth-child(' + counter + ')').fadeIn( 300 );
				}
			});
		});
		
		return false;		
	});
	
	
	// 
	Portfolio.init();
});
