$(document).ready(function() {
	vCenter();
	focusForm();
	showcase();
});

// remove the value of the form element on focus
function focusForm() {
	$(":input").not(".btn_submit").focus(function() {
		if ($(this).val() == "*Name" || $(this).val() == "Company" || $(this).val() == "Email" || $(this).val() == "Message") {
			$(this).attr({value:""});
		}
	});
}

// calculate viewport to center verticaly the wrapper
function vCenter() {
	var height = $(window).height();
	if (height>800) {
		var MT = (height-800)/2;
		$("#wrapper").css('margin-top', MT);
	};
	
}

//showcase presentation footer
function showcase() {

	var first=0;
	var speed=700;
	var pause=5000;

	function removeFirst() {
	    first = $('.footer li:first').html();
		$('.footer li:first').addClass("first");
	    $('.footer li:first')
	    .animate({
	        opacity: 0
	    },
	    speed)
	    .fadeOut('slow',
	    function() {
	        $('.footer li:first').removeClass("first");
			$(this).remove();
	    });
	    addLast(first);
	}
	function addLast(first) {
	    last = '<li style="display:none">' + first + '</li>';
	    first + '';
	    $('.footer ul').append(last)
	    $('.footer li:last')
	    .animate({
	        opacity: 1
	    },
	    speed)
	    .fadeIn('slow')
	}

	interval=setInterval(removeFirst, pause);
}

//slideshow
function slideShow($target, classes, duration, img) {

	// preload the images
	function preload() {
	    // create object
	    imageObj = new Image();

	    // set image list
	    var images = img;
	    // start preloading
	    var i = 0;
	    // counter
	    var number = images.length;
	    // number of iteration
	    for (i = 0; i < number; i++)
	    {
	        imageObj.src = images[i];
	    }
	    // set what happens once the image has loaded objImage.onLoad=imagesLoaded();
	    //imageObj.onLoad = imagesLoaded();
	    // function invoked on image load
	    // function imagesLoaded()
	    // {
	    //     play();
	    // }
	}
	preload();
	
	var actualClass=$("#wrapper").attr("class");
	var current=jQuery.inArray(actualClass, classes);
	var slide=0;
	
	// navigate slideshow with arrows
	$("#next").bind('click', function() {
		stop();
		$("#play").hide();
		$("#stop").show();
		$target.removeClass(classes[current]);
		current = (current+1)%classes.length;
		$target.addClass(classes[current]);
	});
	// navigate slideshow with arrows
	$("#prev").bind('click', function() {
		stop();
		$("#play").hide();
		$("#stop").show();
		$target.removeClass(classes[current]);
		current = (current-1<0)?classes.length-1:current-1;
		$target.addClass(classes[current]);
	});
	
	// The slideshow
	var play=function(){
		slide=setInterval(function() {
			$target.removeClass(classes[current]);
			current = (current+1)%classes.length;
			$target.addClass(classes[current]);
		}, duration);	
	}
	
	//launch the slideshow by default
	play();
	// function to stop slideshow
	var stop=function() {
		clearInterval(slide);
		$("#play").show();
		$("#stop").hide();
	}
	
	//stop the slideshow on click
	$("#stop").bind('click', function() {
		clearInterval(slide);
		$(this).hide();
		$("#play").show();
	});
	
	//relaunch slide
	$("#play").bind('click', function() {
		play();
		$(this).hide();
		$("#stop").show();
		// Act on the event
	});
	
	// change img background on the pager click
	$("#pager li a").bind('click', function() {
		clearInterval(slide);
		var val=$(this).html();
		$("#wrapper").removeClass().addClass("bg"+val);
	});
	
}

