//var imageArray = new Array( "images/1.jpg" , "images/2.jpg" , "images/3.jpg" );
var imageObject = new Object( );
$(document).ready(function() {
	attachImageEvent();
	emailForm();
	
	// find the div.fade elements and hook the hover event
	$('.fadeButton').hover(function() {
		// on hovering over find the element we want to fade *up*
		var fade = $('> .hover', this);
 
		// if the element is currently being animated (to fadeOut)...
		if (fade.is(':animated')) {
			// ...stop the current animation, and fade it to 1 from current position
			fade.stop().fadeTo(500, 1);
		} else {
			fade.fadeIn(500);
		}
	}, function () {
		var fade = $('> .hover', this);
		if (fade.is(':animated')) {
			fade.stop().fadeTo(500, 0);
		} else {
			fade.fadeOut(500);
		}
	});
 
	// get rid of the text
	$('.fadeButton > .hover').empty();
	
	$(".scrollable").scrollable({ circular: false });
});


	function cacheImages( integer ){
			var newObject = new Object();
			var newImage = new Image();

			newImage.src = imageArray[ integer ]; 
			if( newImage.complete && integer < imageArray.length ){
				imageObject[ imageArray[ integer ]  ] = newImage;
				
				setTimeout( function(){ 
					cacheImages( ( integer + 1 ) );	
				} , 100 );
			}
			else if( integer == imageArray.length ){

			}
			else{
				setTimeout( function(){ 
					cacheImages( integer );
				} , 100 );
			}
	}



	function attachImageEvent(){
		$(".img-thumb").click( function(){ 
				//var currentImage =  $( this ).attr("src").replace("thumbs","images");
				var currentImage =  this.id;
				var image = imageObject[ currentImage ];
				$("#div-img-current").fadeOut( 1500, function(){ 
					$("#div-img-current").css({ 
						"background-image" : "url("+ image.src +")",
						"width" : image.width + "px",
						"height" : image.height + "px"
					 });
					$("#div-img-current").fadeIn(1500);
					
				});
		} );

	}

	function extendPage(){
		$("#page").css("height","690px");
	}

       function init(){
		var arr = $(".img-home-gallery").get();
		$.each( arr , function ( index , value ) {
			var randomnumber = Math.floor(Math.random()*6000);
			$( value ).fadeOut(  randomnumber );
		 } );
		imageArray.sort(function() {return 0.5 - Math.random()}); 
		setTimeout( function(){ placeNewSet();  loop(); } , 6000 );
	}

	function loop(){
		var arr = $(".img-home-gallery").get();
		$.each( arr , function ( index , value ) {
			var randomnumber = Math.floor(Math.random()*6000);
			$( value ).fadeIn(  randomnumber );
		 } );
		setTimeout( function(){ init(); } , 6000 );
	}

	function placeNewSet(){
		$("#div-homepage-gallery").html("");
		var imageString = "";
		for( x in imageArray ){
			if( imageArray[x].indexOf( "/set"+ currentSet +"/" ) > 1 ){
				imageString += "<img class=\"img-home-gallery hidden\" src=\""+ imageArray[x]  +"\">";	
			}
		}
		
		$("#div-homepage-gallery").html( imageString );

		if( currentSet >= 3 ){
			currentSet = 1;
		}
		else{
			currentSet += 1;
		}
	}

function emailForm(){

	$("#button-email-submit").click( function(){
		
		console.log("Email clicked");
		
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
		var stringAlert = new String();
		
		if( $("#input-name").val().length > 0 ){
		}
		else{
			stringAlert += "Name isn't valid \n";
		}

		if( emailPattern.test($("#input-email").val() ) ){
		}
		else{
			stringAlert += "Email is not valid \n";
		}
		
		if( $("#input-subject").val().length > 0 ){
		}
		else{
			stringAlert += "Subject is empty \n";
		}

		if( $("#textarea-message").val().length > 0 ){
		}
		else{
			stringAlert += "Message is empty \n";
		}

		if( stringAlert == "" ){
			$.post("email.php", 
			{ name : $("#input-name").val() , email : $("#input-email").val() , subject : $("#input-subject").val() , 
			  message : $("#textarea-message").val()
			 } , function(){ 
			 } );
			 
			 alert("Your email has been sent!");
		}
		else{
			alert("Please check the following and try again. \n\n" + stringAlert  );
		}

		return false;
	} );

	if( document.getElementById("input-subject") ){
			$("#page").css("height","610px");
	}
		return false;
}


