/* Homepage showcase slider */
$(window).load(function() {
$('#nSlider').nivoSlider({
effect:'fade',
pauseTime:5000,
pauseOnHover:false,
captionOpacity:0,
controlNav:false,
});
});

/* Content slider coda like */
$(document).ready(function () {
var $panels = $('#contentSlider .scrollContainer > div');
var $container = $('#contentSlider .scrollContainer');
var horizontal = true;
if (horizontal) {
$panels.css({
'float' : 'left',
'position' : 'relative'
});
$container.css('width', $panels[0].offsetWidth * $panels.length);
}

var $scroll = $('#contentSlider .scroll').css('overflow', 'hidden');

$scroll;

function selectNav() {
$(this)
.parents('ul:first')
.find('a').each(function(index) {
aClass = $(this).attr("class").split('Selected');
$(this).removeClass();
$(this).addClass(aClass[0]);
});

aClass = $(this).attr("class").split('Selected');
$(this).removeClass();
$(this).addClass(aClass[0]+'Selected');
}
$('#contentNav .navigation').find('a').click(selectNav);

function trigger(data) {
var el = $('#contentNav #navigation').find('a[href$="' + data.id + '"]').get(0);
selectNav.call(el);
}

if (window.location.hash) {
trigger({ id : window.location.hash.substr(1) });
} else {
$('ul.navigation a:first').click();
}

var offset = parseInt((horizontal ? 
$container.css('paddingTop') : 
$container.css('paddingLeft')) 
|| 0) * -1;

var scrollOptions = {
target: $scroll,
items: $panels,
navigation: '.navigation a',
prev: 'img.left', 
next: 'img.right',
axis: 'xy',
onAfter: trigger,
offset: offset,
duration: 500,
easing: 'swing'
};

$('#contentSlider').serialScroll(scrollOptions);
$.localScroll(scrollOptions); scrollOptions.duration = 1;
$.localScroll.hash(scrollOptions);



/* Goodies box sliding effect*/
$(document).ready(function(){
				//To switch directions up/down and left/right just place a "-" in front of the top/left attribute
				//Vertical Sliding
				$('.boxgrid.slidedown').hover(function(){
					$(".cover", this).stop().animate({top:'00px'},{queue:false,duration:300});
				}, function() {
					$(".cover", this).stop().animate({top:'-300px'},{queue:false,duration:300});
				});
				//Horizontal Sliding
				$('.boxgrid.slideright').hover(function(){
					$(".cover", this).stop().animate({left:'325px'},{queue:false,duration:300});
				}, function() {
					$(".cover", this).stop().animate({left:'0px'},{queue:false,duration:300});
				});
				//Diagnal Sliding
				$('.boxgrid.thecombo').hover(function(){
					$(".cover", this).stop().animate({top:'260px', left:'325px'},{queue:false,duration:300});
				}, function() {
					$(".cover", this).stop().animate({top:'0px', left:'0px'},{queue:false,duration:300});
				});
				//Partial Sliding (Only show some of background)
				$('.boxgrid.peek').hover(function(){
					$(".cover", this).stop().animate({top:'90px'},{queue:false,duration:160});
				}, function() {
					$(".cover", this).stop().animate({top:'0px'},{queue:false,duration:160});
				});
				//Full Caption Sliding (Hidden to Visible)
				$('.boxgrid.captionfull').hover(function(){
					$(".cover", this).stop().animate({top:'160px'},{queue:false,duration:160});
				}, function() {
					$(".cover", this).stop().animate({top:'260px'},{queue:false,duration:160});
				});
				//Caption Sliding (Partially Hidden to Visible)
				$('.boxgrid.caption').hover(function(){
					$(".cover", this).stop().animate({top:'160px'},{queue:false,duration:160});
				}, function() {
					$(".cover", this).stop().animate({top:'220px'},{queue:false,duration:160});
				});
			});


/* Contact form */
$('.text').focus(function () {
	if ($(this).val() == $(this).attr("title")) {
		$(this).val("");
	}
}).blur(function () {
	if ($(this).val() == "") {
		$(this).val($(this).attr("title"));
	}
});
});
function makeObject(){
	var x;
	var browser = navigator.appName;
	//If IE
	if(browser == "Microsoft Internet Explorer"){
		x = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		//other browsers
		x = new XMLHttpRequest();
	}
	return x;
}

//Make request
var request = makeObject();
function submitForm( ){
	var name = document.getElementById( 'name' ).value;
	var email = document.getElementById( 'email' ).value;
	var message = document.getElementById( 'message' ).value;
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	var error = false;
	document.getElementById("nameerror").innerHTML="";
	document.getElementById("emailerror").innerHTML="";
	document.getElementById("messageerror").innerHTML="";

	if( name.split(' ').join('') == "" ) {
		document.getElementById("nameerror").innerHTML="Il campo  vuoto.";
		error = true;
	}

	if( !filter.test( email ) ) {
		document.getElementById("emailerror").innerHTML="L'indirizzo email &egrave; corretto? :(";
		error = true;
	}
	
	if( email.split(' ').join('') == "" ) {
		document.getElementById("emailerror").innerHTML="Il campo  vuoto";
		error = true;
	}
	if( message.split(' ').join('') == "" ) {
		document.getElementById("messageerror").innerHTML="Il campo  vuoto";
		error = true;
	}


	if( error == false ) {
		message = message.split('\n').join('__');
		message = encodeURI( message );
		request.open('get', 'mail.php?name='+ name +'&email='+ email +'&message='+ message);
		request.onreadystatechange = parseInfo;
		request.send('');
	}
}

function parseInfo(){
	if( request.readyState == 1 ) {
		document.getElementById('response').innerHTML = '<img src="img/loader.gif" alt="Loading..." />';
	}
	if(request.readyState == 4){
		var answer = request.responseText;
		document.getElementById('response').innerHTML = answer;
	}
}
