/**
 * written by pierluigi@applix.it
 * fixed by andrea@applix.it
 *
 */
//init start



var animationOff=true;
var arrayIconePricingSelected={"android":"icon_select_2.png","ios":"icon_select_1.png","web":"icon_select_3.png"};
var arrayIconePricing={"android":"icon_2.png","ios":"icon_1.png","web":"icon_3.png"};
var arrayBPricing={"android":"pricing_android.png","ios":"pricing_apple.png","web":"pricing_web.png"};
var lastX;
var lastY;
var platform_price;
var usd_active;

$( function() {
			
	var userLang;

	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
		userLang= navigator.userLanguage;
	}
	else userLang = navigator.language;


	if (userLang.substr(0,2) == "it"){
		//alert("ok italian")
		
		//console.log("it")
	
		$(".it").show();
		$(".en").hide();
	}
	else{
		
		//console.log("en")
	
		$(".en").show();
		$(".it").hide();
		
	}
	
	usd_active=false;
	if (userLang.indexOf("us")>0){
		usd_active=true;
	}
	
		
	$("#tutorial_video").click(activateLightBox, true);
	//set the black overlay to 50% and centers it
	/*var halfW=screen.width/2;
	halfW=halfW-(halfW/4);
	console.log(halfW);
	halfW=halfW+"";
	$("#overlay-black").css({"margin-top":halfW,"margin-right":"5"});
	*/
	$("#icons img").click( function() {
		$("#icons img").each( function() {
			$(this).attr("src","img/"+arrayIconePricing[$(this).attr("alt")]);
				
		});
		$(".littlearrow").remove();
		var leftArrow=0;
		if($(this).attr("alt")=="web")
		{
			leftArrow=347;
		}
		else if($(this).attr("alt")=="android")
		{
			leftArrow=257;
		}
//		else if($(this).attr("alt")=="desktop")
//		{
//			leftArrow=403;
//		}
		else if($(this).attr("alt")=="ios")
		{
			leftArrow=165;
		}
		leftArrow=leftArrow+"px";
		$(this).after("<img class='littlearrow' alt='' src='img/littleArrow.png' style='height:36px; position:absolute;top:90px;left:"+leftArrow+";'/>");
		platform_price=$(this).attr("alt");
		//console.log(platform_price);
		$(this).attr("src","img/"+arrayIconePricingSelected[$(this).attr("alt")]);
		if(usd_active){
			var addr="img/"+arrayBPricing[$(this).attr("alt")]+"";
			if(addr.indexOf("_dollar.png")<=0){
				$("#pricing_bars .en img").attr("src", addr.replace(".png","_dollar.png"));
			}

			//$("#pricing_bars .en img").attr("src","img/"+arrayBPricing[$(this).attr("alt")]);
		}
		else{
			$("#pricing_bars img").attr("src","img/"+arrayBPricing[$(this).attr("alt")]);
		}
		$("#price_service_content").children().hide();
		$("#price_setup_content").children().hide();
		$(".setup_"+platform_price).show();
		$(".service_"+platform_price).show();
	});
	
	

	$(".page").each( function(i) {
		$(this).css({"left":i*999+"px"});
	});
	goTo(0);
	$("#toolbar-top .button").click( function() {
		goTo($(this).attr("title"));
	});
	$("#logo").click( function() {
		goTo(0);
	});
	$(".home").click( function(e) {
		activateLightBox();
	});
	$("#home_page").click( function(e) {
		//activateLightBox();
	});
	$('#flags').css({backgroundPosition: "-35px 0px"});
	$("#flags").click(switchLanguage); 
	///-------------------------gestione touch---------------------------------///
	var ua = navigator.userAgent;
	//console.log(ua);
	var isiPad = /iPad/i.test(ua) || /iPhone OS 3_1_2/i.test(ua) || /iPhone OS 3_2_2/i.test(ua);
	if(isiPad) {
		$(document).bind("touchstart", function(e) {

			lastX=null;
			lastY=null;

		});
		$(document).bind("touchmove", function(e) {
			if(	$(e.target).parent().attr("class")!="button") {
				e.preventDefault();
				if(lastX==null) {

					lastX=event.touches[0].screenX;

				} else {
					deltaX=event.touches[0].screenX-lastX;

					//deltaY=event.touches[0].screenY-lastY;

					lastX=event.touches[0].screenX;

					$("#content").animate({left: parseInt($("#content").css("left"))+deltaX},0);

				}
			}
		});
		$(document).bind("touchend", function(e) {
			var num;
			//console.log($(e.target).parent().attr("class"));
			if(	$(e.target).parent().attr("class")!="button") {
				var aTmp=parseInt($("#content").css("left"));
				num=Math.round(aTmp/1000);
				if(num>0) {
					num=0;
				}
				if(num<-5) {
					num=-5;
				}
			} else {
				
				num=$(e.target).parent().attr("title");
			}
			if($(e.target).parent().attr("id")=="contact")
			{
				//console.log($(e.target).parent().attr("id"));
				activateLightBox(1,1);
			}
			else{
			num=Math.abs(num);
			goTo(num);
			}
		});
	}
	
	$("#price_conversion_usd").click(function(e){
		e.preventDefault();
		price_conversion(true);
	});
	$("#price_conversion_eur").click(function(e){
		e.preventDefault();
		price_conversion(false);
	});
	price_conversion(usd_active);
	
	$("#close_overlay").click(function(e){
		if($('player_vimeo').css('display')!='none'){
			//go to the tutorial section
			$('#about').click();
		}
	});
});
//init end

function goTo(num)//num=index of the view to show
{
	if(animationOff) {
		animationOff=false;
		$("#content").animate({left: -num*999},500, function() {
			animationOff=true;
		});
		$("#pagecount_visual div").each( function(i) {
			if(i==num) {
				$(this).attr("class","current").css({"left":i*10+"px","top":0+"px"});
			} else {
				$(this).attr("class","").css({"left":i*10+"px","top":0+"px"});
			}
		});
		if(num!=0) {
			$("#toolbar-top .button").each( function(i) {
				if(i==num-1) {
					$(this).css({"color":"red"});
				} else {
					$(this).css({"color":"black"});
				}
			});
		} else {
			$("#toolbar-top .button").css({"color":"black"});
		}
	}
}

function switchLanguage(lang) {
	if (($(".it").css("display")=="none")||(lang == "en")) {//English
		$(".it").show();
		$(".en").hide();
		
		$(".more_info img").each(function(i)
		{
			//console.log($(this).attr("src"));
			if($(this).attr("src").indexOf("select")>0)
			{
				$(".more_info img").attr("src","img/info_select.png");
			}
			else{
				$(".more_info img").attr("src","img/info.png");
			}
		});
		
		$("#flags").animate({ backgroundPosition:"0 0"},200);
	} else {//Italian
		$(".it").hide();
		$(".en").show();
		
		$(".more_info img").each(function(i)
		{
			//console.log($(this).attr("src"));
			if($(this).attr("src").indexOf("select")>0)
			{
				$(".more_info img").attr("src","img/info_select_en.png");
			}
			else{
				$(".more_info img").attr("src","img/info_en.png");
			}
			
		});
		
		$("#flags").animate({backgroundPosition:"-35px 0"},200);
	}
}

function activateLightBox() {
	var ua = navigator.userAgent;
	//console.log(ua);
	var isiPad = /iPad/i.test(ua) || /iPhone OS 3_1_2/i.test(ua) || /iPhone OS 3_2_2/i.test(ua);
	if(isiPad && arguments.length!=2) {
		if(arguments.length==0)
		{
			if($(".it").css("display")=="none")//language==English
			{
				window.location.replace("Intro_EN.html");
			}
			else
			{
				window.location.replace("Intro_IT.html");
			}
		}
		else if(arguments.length==1)
		{
			if($(".it").css("display")=="none")//language==English
			{
				window.location.replace("Tutorial_EN.html");
			}
			else
			{
				window.location.replace("Tutorial_IT.html");
			}
		}
		
	} else {
		//console.log(arguments.length);
		if(arguments.length==0) {
			// $("iframe").attr("src","http://player.vimeo.com/video/26713919?title=0&amp;byline=0&amp;portrait=0");
			if($(".it").css("display")=="none")//language==English
			{
				$("iframe").attr("src","http://player.vimeo.com/video/27540731?autoplay=1");
			}
			else
			{
				$("iframe").attr("src","http://player.vimeo.com/video/27541016?autoplay=1");
			}
			$("#contact-overlay").hide();
		} else if(arguments.length==1) {
			// $("iframe").attr("src","http://player.vimeo.com/video/26712050?title=0&amp;byline=0&amp;portrait=0");
			if($(".it").css("display")=="none")//language==English
			{
			$("iframe").attr("src","http://player.vimeo.com/video/27540216");
			}
			else
			{
			$("iframe").attr("src","http://player.vimeo.com/video/27540447");
			}
			$("#contact-overlay").hide();
		}
		else if(arguments.length==2)
		{
			$("iframe").hide();
			$("#contact-overlay").show();
			//console.log("2 elements");
		}
		$("#overlay").show(300, function() {

			$("#close_overlay").click( function() {
				$("#overlay").hide(300);
				var tmp=$("iframe");
				$("iframe").remove();
				$("#overlay-content").html($("#overlay-content").html()+'<iframe id="player_vimeo" src="http://player.vimeo.com/video/15196495" width="600" height="337" frameborder="0" ></iframe>');
				$("iframe").show();
			});
		});
	}

}

function showPricingPage(sw) {
		//console.log(sw);
		if($(".it").css("display")=="none")//language==English
		{
			$(".more_info img").attr("src","img/info_en.png");
			var tmpB=".more_info[title="+sw+"] img";
			$(tmpB).attr("src","img/info_select_en.png");
		}
		if($(".en").css("display")=="none")//language==Italian
		{
			$(".more_info img").attr("src","img/info.png");
			var tmpB=".more_info[title="+sw+"] img";
			$(tmpB).attr("src","img/info_select.png");
		}
		
		$("#content_price_right>div").css("display","none");
		var tmpA="#"+sw;
		$(tmpA).show();
		
		
		var tmpC=".setup_"+platform_price;
		
}

function price_conversion(usd_conversion){
	if(usd_conversion){
		//change the src attr of images to be in USD
		var addr=$("#pricing_bars .en img").attr("src")+"";
		if(addr.indexOf("_dollar.png")<=0){
			$("#pricing_bars .en img").attr("src", addr.replace(".png","_dollar.png"));
		}
		
		var addr2=$("#desc_pricing img").attr("src")+"";
		if(addr2.indexOf("_dollar.png")<=0){
			$("#desc_pricing img").attr("src", addr2.replace(".png","_dollar.png"));
		}
		usd_active=true;
		$("#price_conversion_usd").addClass('active');
		$("#price_conversion_eur").removeClass('active');
		$("#price_list .en a").attr("href","Viewerplus_price_list_USD.pdf");
	}
	else{
		var addr=$("#pricing_bars .en img").attr("src")+"";
		if(addr.indexOf("_dollar.png")>0){
			$("#pricing_bars .en img").attr("src", addr.replace("_dollar.png",".png"));
		}
		
		var addr2=$("#desc_pricing img").attr("src")+"";
		if(addr2.indexOf("_dollar.png")>0){
			$("#desc_pricing img").attr("src", addr2.replace("_dollar.png",".png"));
		}
		usd_active=false;
		$("#price_conversion_eur").addClass('active');
		$("#price_conversion_usd").removeClass('active');
		$("#price_list .en a").attr("href","Viewerplus_price_list_EN_EURO.pdf");
	}
}



