function init(){
	
	$('#contactSlide').one("click",openContactForm);
	$('#contactSendButton').click(submitContactForm);
	
	$("#homecarousel").jcarousel({
        scroll: 1,
        initCallback: initHomeCarousel,
		itemVisibleInCallback: visibleCallbackHomeCarousel,
        animation: "slow",
		wrap: "both",
		auto: 5
    });
	
	$("#youtubeVideo,.youtubeVideo").click(function() {
		$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'			: 680,
				'height'		: 495,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
				   	 'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});
	
		return false;
	});
}

function checkMail(email){
	if(email=="")
		return false;
	
	var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
	if(!email_reg_exp.test(email))
		return false;
		
	return true;
}

function submitContactForm(){
	
	var button = $(this);
	
	button.hide();
	
	var name = $('#contactFormName').val();
	var email = $('#contactFormEmail').val();
	var message = $('#contactFormMessage').val();
	
	if (name == ""){
		alert("errore name");
		button.show();
		return false;
	}
	
	if (!checkMail(email)){
		alert("errore email");
		button.show();
		return false;
	}
	
	if (message==""){
		alert("errore message");
		button.show();
		return false;
	}
	
	$.post(pathsite+"/contacts.php?send",{
		name: name,
		email: email,
		message: message
	}, function(data){
		if (data=="1"){
			// invio con successo
			$(document).trigger("click");
			alert("messaggio inviato correttamente");
			button.show();
		} else {
			// invio fallito
			alert("errore invio");
			button.show();
		}
	});
	
}

function openContactForm(){
	$("#contactSlide").animate({left:0},1000,function(){
		$(document).one("click",closeContactForm);
	});
}

function closeContactForm(obj){
	if ($(obj.target).closest(".contactForm").length > 0) {
		$(document).one("click", closeContactForm);
		return false;
	}
	$("#contactSlide").animate({left:-225},1000,function(){
		$('#contactSlide').one("click",openContactForm);
	});
	
}

function initScrollApps(){
	//scrollpane parts
	var scrollPane = $('.scroll-pane');
	var scrollContent = $('.scroll-content');
	
	//build slider
	var scrollbar = $(".scroll-bar").slider({
		slide:function(e, ui){
			if( scrollContent.width() > scrollPane.width() ){ scrollContent.css('margin-left', Math.round( ui.value / 100 * ( scrollPane.width() - scrollContent.width() )) + 'px'); }
			else { scrollContent.css('margin-left', 0); }
		}
	});
	
	//append icon to handle
	var handleHelper = scrollbar.find('.ui-slider-handle')
	.mousedown(function(){
		scrollbar.width( handleHelper.width() );
	})
	.mouseup(function(){
		scrollbar.width( '100%' );
	})
	.wrap('<div class="ui-handle-helper-parent"></div>').parent();
	
	//change overflow to hidden now that slider handles the scrolling
	scrollPane.css('overflow','hidden');
	
	//size scrollbar and handle proportionally to scroll distance
	function sizeScrollbar(){
		var remainder = scrollContent.width() - scrollPane.width();
		var proportion = remainder / scrollContent.width();
		var handleSize = scrollPane.width() - (proportion * scrollPane.width());
		scrollbar.find('.ui-slider-handle').css({
			width: handleSize,
			'margin-left': -handleSize/2
		});
		handleHelper.width('').width( scrollbar.width() - handleSize);
	}
	
	//reset slider value based on scroll content position
	function resetValue(){
		var remainder = scrollPane.width() - scrollContent.width();
		var leftVal = scrollContent.css('margin-left') == 'auto' ? 0 : parseInt(scrollContent.css('margin-left'));
		var percentage = Math.round(leftVal / remainder * 100);
		scrollbar.slider("value", percentage);
	}
	//if the slider is 100% and window gets larger, reveal content
	function reflowContent(){
			var showing = scrollContent.width() + parseInt( scrollContent.css('margin-left') );
			var gap = scrollPane.width() - showing;
			if(gap > 0){
				scrollContent.css('margin-left', parseInt( scrollContent.css('margin-left') ) + gap);
			}
	}
	
	//change handle position on window resize
	$(window)
	.resize(function(){
			resetValue();
			sizeScrollbar();
			reflowContent();
	});
	//init scrollbar size
	setTimeout(sizeScrollbar,10);//safari wants a timeout

}

/**
 * We use the initCallback callback
 * to assign functionality to the controls
 */
function initHomeCarousel(carousel) {
    jQuery('.jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });

    jQuery('.jcarousel-scroll select').bind('change', function() {
        carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
        return false;
    });

    jQuery('#mycarousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('#mycarousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};

function visibleCallbackHomeCarousel(carousel,liItem,index,state){
	$('.jcarousel-control a').removeClass("active");
	$($('.jcarousel-control a').get(index-1)).addClass('active');
}

function openAboutUsBox(){
	$("#aboutUsBox").slideDown();
	$.scrollTo("#aboutUsBox", {duration:2000,over:-0.1});
}

function openAppsBar(){
	$('#appsBar').slideToggle("slow");
	initScrollApps();
}

function openHelpDeskPanel(){
	
}
