// JavaScript Document

 $(document).ready(function(){
	
	// cycle images
	$('.gal-image').cycle({ 
    	fx: 'scrollRight',
		timeout: 8000,
	});
	
	//cycle details
	$('.detail').cycle({ 
    	fx: 'scrollDown',
		timeout: 8000

	});
	
	$(".pause").click(function(){
		
		if($(this).hasClass('paused')){
			$('.detail, .gal-image').cycle('resume');
			$('.detail, .gal-image').cycle('next');
			$(this).removeClass('paused');
		}
		else{
			$('.detail, .gal-image').cycle('pause');
			$(this).addClass('paused');
		
		}
	});
	
	$(".forward").click(function(){

			$('.detail, .gal-image').cycle('next');
	});
	
	$(".back").click(function(){

			$('.detail, .gal-image').cycle('prev');
	});
	
	// ---- DECORATION ---- //
	$('.caption').hide();
	$('.port-item img').hover(function(){
		var caption = $(this).closest('.port-item').find('.caption').slideDown("slow");
		
	});
	$('.port-item').mouseleave(function(){
		$('.caption').slideUp();
	});
	
	$('.caption').click(function(){
		var image = $(this).closest('.port-item').find('a');
		var imageURL = image.attr('href');
		var type = image.attr('rel');
		
		if (type =="shadowbox[print]"){
			image.click();
		}
		else{
			window.location = imageURL;
		}

	});
	
	
	// ---- FORM FUNCTIONS ---- //
	
	// dump defaults on enter
	$(':text, textarea').bind('click focus',function(){
		var title = $(this).attr('title');
		var value = $(this).val();
		
		if(title == value || $(this).hasClass('error')){
			  $(this).val("");
			  $(this).removeClass('error');
		}
	});
	
	//switch password to password type
	//$('#password').keypress(function(){
//		var boxTitle = $(this).attr('title');
//		var boxVal = $(this).val();
//		
//		if(boxVal != boxTitle){
//			$(this).attr('type', 'password')
//		}
//		else{
//			$(this).attr('type', 'text')
//		}
//	});
	// reset values on blur
	$(':text, textarea').bind('blur unfocus',function(){
		
		var title = $(this).attr('title');
		var value = $(this).val();
		
		if(value == "" || value == null){
			$(this).val(title);
		}
		else{
			$(this).regexpress();
		}
	});
	
	// reset values on button click
	$('#clear-btn').click(function(){
		$(':text, textarea,').each(function(){
			var title = $(this).attr('title');
			$(this).val(title);
			$(this).removeClass('pass');
			$(this).removeClass('error');
			
		});
		$("contact").removeClass('error');
		
	});
	
	// validation
	$('#submit-btn').click(function() {
		
		$(':text, textarea,').each(function(){// checks each input to see if it has an error
			
			var title = $(this).attr('title');
			var value = $(this).val();
			
			if (title == value || value == ""){
				alert('Please fill out all sections');
				$("#contact").addClass("error");
				return false;
				
			}
			
			if($(this).hasClass('error')){// if it does it alerts what the error is and returns false
				alert($(this).val());
				$("#contact, #login").addClass("error");
				return false;
			}
			else{// if not it returns true to let the form process. 
				$("#contact, #login").removeClass("error");
			}
		});
		
	});
	
	$("#contact, #login").submit(function(){
		if($(this).hasClass("error")){
			return false;
		}
		else{
			return true;
		}
	});	
	
	$('#admin-section').change(function(){
		var value = $(this + 'option:selected').val();
		var sectionURI = "admin.php?section=" + value;
		window.location = sectionURI;
	});
	

 });// end doc.ready
