﻿var languagearray = new Array(["Sijainti", "Lisätietoja", "Info", "Jaa näkymä", "Puhelin", "Faksi", "Sulje", "Jaa näkymä:", "Jaa näkymä Facebookissa", "Upota näkymä kotisivuillesi", "Linkki näkymään", "In english", "Pohjakartat","LOGI esittelyvideo"],["Map", "Footnotes", "Info", "Share", "Phone", "Fax", "Close", "Share this view:", "Share this view in Facebook", "Paste the view on your homepages", "Direct link to the view:", "Suomeksi", "Floormaps","LOGI presentation video"]);

var add_array = new Array(["Facebook","Twitter","Google","Sähköposti","Blogger"],["Facebook","Tweet","Google","E-mail","Blogger"]);

var l_array = new Array(['Varaa huone','Reservation']);

var typearray = new Array(["Ravintolat","Kabinetit","Kokoustilat","Hotellihuoneet","Wellness","Ulkotilat"],["Restaurants","Cabinets","Conference rooms","Hotel rooms","Wellness","Outside"]);
var floorsarray = new Array(["Ulkokuvat","Kerros 1","Kerros 2","Kerros 3","Rantasauna"],["Outside","Floor 1","Floor 2","Floor 3","Lakeside sauna"]);

function showinfo() {
	$(".box3").css("top",$(window).height()+"px");
	$(".box3").css("display","block");
	$(".box3 > .boxcontent > .boxdetails > .boxright").css("width", $(window).width()/2-50+"px");
	$(".box3 > .boxcontent > .boxdetails > .boxleft").css("width", $(window).width()/2-50+"px");
	$(".box3").animate({top: 0},500,function(){});
}

$(window).load(function(){
	
	$(".btn_box1").bind("click",function(event){
		$(".box1").css("top",$(window).height()+"px");
		$(".box1").css("visibility","visible");
		$(".box1").css("display","block");
		orientationchange();
		map.setCenter(new google.maps.LatLng(wgs_lat,wgs_lon));
		$(".box1").animate({top: 0},500,function(){});
	});
	$(".btn_box5").bind("click",function(event){
		$(".box5").css("top",$(window).height()+"px");
		$(".box5").css("visibility","visible");
		$(".box5").css("display","block");
		setcurrentpoint();
		orientationchange();
		$(".box5").animate({top: 0},500,function(){});
	});

	$(".btn_box2").bind("click",function(event){
		$(".box2").css("top",$(window).height()+"px");
		$(".box2").css("display","block");
		$(".box2").animate({top: 0}, 500, function(){});
	});
	$(".btn_box6").bind("click",function(event){
		$(".box6").css("top",$(window).height()+"px");
		$(".box6").css("display","block");
		orientationchange();
		$(".box6").animate({top: 0}, 500, function(){});
	});
	
	$(".btn_box4").bind("click",function(event){
		$(".box4 > .boxcontent > .boxdescription").children("h1").text(languagearray[currentlan][7]+" "+$(".company_name").text());// addthis_default_style addthis_32x32_style
		var urlstr = "http://www.virtuaalikuvat.com/langvik/?t="+krpano().get("plugin[point].tour")+"&p="+krpano().get("plugin[point].id")+"&h="+krpano().get("view.hlookat")+"&v="+krpano().get("view.vlookat")+"&f="+krpano().get("view.fov");
		$("#share_addthis").html("<div class='share_header'><div class='share_maintitle'><h1>"+languagearray[currentlan][3]+"</h1></div></div><div class='share_list'><div id='toolbox' class='addthis_toolbox addthis_default_style addthis_32x32_style' addthis:url='"+urlstr+"'><a class='addthis_button_facebook' title='"+add_array[currentlan][0]+"'></a><a class='addthis_button_twitter' title='"+add_array[currentlan][1]+"'></a><a class='addthis_button_google' title='"+add_array[currentlan][2]+"'></a><a class='addthis_button_email' title='"+add_array[currentlan][3]+"'></a><a class='addthis_button_blogger' title='"+add_array[currentlan][4]+"'></a></div></div>");
		
		addthis.toolbox("#toolbox");
		$("#share_link").html("<div class='share_header'><div class='share_maintitle'><h1>"+languagearray[currentlan][10]+"</h1></div></div><div class='share_list'>"+urlstr+"</div>");
		$("#share_iframe").html("<div class='share_header'><div class='share_maintitle'><h1>"+languagearray[currentlan][9]+"</h1></div></div><div class='share_list'>"+htmlEntities("<iframe title='Långvik' src='"+urlstr+"' width='800' height='600' frameborder='0' allowFullscreen></iframe>")+"</div>");
		
		$(".box4").css("top",$(window).height()+"px");
		$(".box4").css("display","block");
		$(".box4").animate({top: 0},500,function(){});
		});
		
	$(".boxclose").bind("click",function(event){
		$(".box").each(function(){
			if($(this).css("display") != "none") {
				$(this).animate({top: $(window).height()},500,function(){ $(this).css("display","none"); });
			}
		});
	});
	//$(".welcomeboxcontent").fadeIn(2000);
	//$(".welcomeboxcontent").css("top",Math.floor($(window).height()/2-$(".welcomeboxcontent").height()/2)+"px");
	
	$(document).bind("orientationchange",function(event){
		orientationchange();
	});
});

function orientationchange() {
	$(".bottombar").css("bottom",0);
	if($(".box1").css("display") == "block" && $(".box1").css("visibility") == "visible") {
		google.maps.event.trigger(map, 'resize');
	}
	if($(".box3").css("display") == "block") {
		$(".box3 > .boxcontent > .boxdetails > .boxright").css("width", Math.floor($(window).width()/2)-51+"px");
		$(".box3 > .boxcontent > .boxdetails > .boxleft").css("width", Math.floor($(window).width()/2)-51+"px");
	}
	
	if($(".box5").css("display") == "block") {
		$(".box5 > .boxcontent > .boxdetails").css("top",$(".mapbuttons").height()+10 +"px");
		$(".floormapcontainer").each(function(){
			if($(this).css("display") == "block") {
				$(".mapbuttons").css("left",Math.floor($(window).width()/2-$(this).width()/2)+30+"px");
				$(".mapbuttons").css("top",Math.floor(($(window).height()-$(".mapbuttons").height())/2-$(this).height()/2)-10+"px");
				$(".mapbuttons").css("max-width",$(window).width()-$(".boxclose").width()-30-Math.round($(window).width()/2-$(this).children(".floormapimage").width()/2));
			}
			$(this).css("left",Math.round($(window).width()/2-$(this).children(".floormapimage").width()/2)+"px");
			$(this).css("top",Math.floor(($(window).height()-$(".mapbuttons").height())/2-$(this).height()/2)+$(".mapbuttons").height()+"px");
		});
	}
	if($(".box6").css("display") == "block") {
		$(".videobox").css("top",Math.round(($(window).height() - $(".box6 > .boxcontent > .boxdescription").height())/2-240)+"px");
		$(".videobox").css("left",Math.round(($(window).width()/2)-768/2)+"px");
	}
	
}

function krpano() 
{
	return document.getElementById("krpanoSWFObject");
}

function setcurrentpoint() { // setting current point here
	if(krpano().get("plugin[point].id") != null) {
		$("#mappoint_"+currentpoint+" > a").children("img").attr("src","layout/mappoint_default.png");
		$(".floormapcontainer").each(function(){
			$(this).css("display","none");
		});
		
		$("#mappoint_"+krpano().get("plugin[point].id")+" > a").children("img").attr("src","layout/mappoint_selected.png");
		$("#mappoint_"+krpano().get("plugin[point].id")).parent(".floormapcontainer").children().css("z-index",1);
		$("#mappoint_"+krpano().get("plugin[point].id")).parent(".floormapcontainer").css("display","block");
		$("#mappoint_"+krpano().get("plugin[point].id")).css("z-index",2);
		$(".mapbutton_selected").removeClass("mapbutton_selected").addClass("mapbutton_notselected");
		var currentfloor = $("#mappoint_"+krpano().get("plugin[point].id")).parent(".floormapcontainer").attr("id");
		if(currentfloor != null) {
			var floorarray = currentfloor.split("_");
			$("#mapbutton_"+floorarray[1]).removeClass("mapbutton_notselected").addClass("mapbutton_selected");
			$(".maplist_visible").removeClass("maplist_visible").addClass("maplist_invisible");
			$("#maplist_"+floorarray[1]).removeClass("maplist_invisible").addClass("maplist_visible");
		}
		currentpoint = krpano().get("plugin[point].id");
	}
}

function showmap(floor) {
	$(".floormapcontainer").each(function() { 
		$(this).css("display","none");
	});
	$(".mapbutton_selected").each(function(){
		$(this).removeClass("mapbutton_selected").addClass("mapbutton_notselected");	
	});
	$("#mapbutton_"+floor).removeClass("mapbutton_notselected").addClass("mapbutton_selected");
	$(".mapbuttons").css("left",Math.floor($(window).width()/2-$("#floormapcontainer_"+floor).width()/2)+30+"px");
	$(".mapbuttons").css("top",Math.floor(($(window).height()-$(".mapbuttons").height())/2-$("#floormapcontainer_"+floor).height()/2)-10+"px");
	$(".mapbuttons").css("max-width",$(window).width()-$(".boxclose").width()-30-Math.round($(window).width()/2-$("#floormapcontainer_"+floor).children(".floormapimage").width()/2));
	$("#floormapcontainer_"+floor).css("display","block");
	$("#floormapcontainer_"+floor).css("left",Math.round($(window).width()/2-$("#floormapcontainer_"+floor).children(".floormapimage").width()/2)+"px");
	$("#floormapcontainer_"+floor).css("top",Math.floor(($(window).height()-$(".mapbuttons").height())/2-$("#floormapcontainer_"+floor).height()/2)+$(".mapbuttons").height()+"px");
}


function openpoint(tgt,id) {
	if(krpano().get("plugin[point].id")*1 != id) {
		krpano().call("set(plugin[point].id,"+id+");set(plugin[point].tour,"+tgt+");loadpano(jjviewer.php?p="+id+"&t="+tgt+",null,MERGE,BLEND(1));");
		$('.box1').css('display','none');
		$(".box5").css("display","none");
	}
}

var distances = new Array();
var pointids = new Array();
var map_x = 0;
var map_y = 0;
function openmappoint(x,y,tgt) {
	distances = new Array();
	pointids = new Array();
	map_x = x;
	map_y = y;
	$("#floormapcontainer_"+tgt+" > .floormappoints").each(function(){
		var posx = $(this).css("left");
		var posy = $(this).css("top");
		posx = posx.substring(0,posx.length-2)*1;
		posy = posy.substring(0,posy.length-2)*1;
		distances.push(Math.sqrt((map_x-posx)*(map_x-posx)+(map_y-posy)*(map_y-posy)));
		pointids.push($(this).attr("id"));
	});
	var mindist = distances[0];
	var minid = pointids[0];
	for(var i=0;i<distances.length;i++) {
		if(mindist > distances[i]) {
			mindist = distances[i];
			minid = pointids[i];
		}
	}
	openpoint(2,minid.substring(9,minid.length)*1);
}

/*function openlistpoint(target) {
	if(selected.selectedIndex != 0 && selected.options[selected.selectedIndex].value != 0) {
		if(krpano().get("plugin[point].id")*1 != selected.options[selected.selectedIndex].value) {
			krpano().call("set(plugin[point].id,"+selected.options[selected.selectedIndex].value+");loadpano(jjviewer.php?p="+selected.options[selected.selectedIndex].value+"&t=2,null,MERGE,BLEND(1));");
		}
	}
}*/
function openlistpoint(tgt) {
	if(tgt != 0) {
		if(krpano().get("plugin[point].id")*1 != tgt) {
			krpano().call("set(plugin[point].id,"+tgt+");loadpano(jjviewer.php?p="+tgt+"&t=2,null,MERGE,BLEND(1));");
		}
	}
}

function showlist() {
	$("#roomselect").css("top", $(window).height()+1+"px");
	$("#roomselect").css("right", "30px");
	$("#roomselect").css("display","block");
	$("#roomselect").animate({ top: $(window).height() - $("#roomselect").height() - 10 },500,function(){
		var ua = navigator.userAgent.toLowerCase();
		if(ua.indexOf("ipad") >= 0 || ua.indexOf("iphone") >= 0 || ua.indexOf("ipod") >= 0) {
			setTimeout("setevent()", 200);
		} else {
			krpano().set("events.onmousedown","js(hidelist(););");
		}
		});
}

function setevent() { krpano().set("events.onviewchange","js(hidelist(););"); }

function hidelist() {
	krpano().set("events.onviewchange",null);
	krpano().set("events.onmousedown",null);
	$("#roomselect").animate({top: $(window).height()+5},500,function(){ $("#roomselect").css("display","none"); });
}

function checkinfo() {
	if(krpano().get("plugin[point].tour") != null && krpano().get("plugin[point].tour") != "" && krpano().get("plugin[point].tour") != currenttour) {
		currenttour = krpano().get("plugin[point].tour");
		$.get("getinfo.php?t="+currenttour, function(data){ setinfo(data); });
	}
}
function setinfo(data) {
	xmldoc = $.parseXML(data);
	$xml = $(xmldoc);
	$info = $xml.find("info");
	if($info.attr("title") != null && $info.attr("title") != "") {
		$(".company_name").text($info.attr("title"));
		if($info.attr("address") != "") {
			$(".company_streetaddress").text($info.attr("address"));
			$(".company_postcode").text($info.attr("postcode"));
		} else {
			$(".company_streetaddress").text("");
			$(".company_postcode").text("");
		}
		$(".company_phone").text($info.attr("phonenumber"));
		var wwwstr = $info.attr("www");
		if(wwwstr.indexOf("http://") == -1 && wwwstr.length > 0) wwwstr = "http://"+wwwstr;
		$(".company_www").html("<a href='"+wwwstr+"' target='_blank'>"+wwwstr+"</a>");
	}
	if($info.attr("infodata") != null && $info.attr("infodata") != "") {
		$(".btn_box2").css("display","block");
		$(".box2 > .boxcontent").children(".boxdescription").html("<h1>"+$info.attr("title")+"</h1>");
		$(".box2 > .boxcontent").children(".boxdetails").html($info.attr("infodata"));
	} else {
		$(".btn_box2").css("display","none");
	}
	
	$layout = $xml.find("layout");
	if($layout.attr("bar") != null && $layout.attr("bar") != "") {
		$(".bottombar").css("background-color","#"+$layout.attr("bar"));
	} else {
		$(".bottombar").css("background-color","#545253");
	}
	if($layout.attr("tabs") != null && $layout.attr("tabs") != "") {
		$(".btn_bottom").css("background-color","#"+$layout.attr("tabs"));
	} else {
		$(".btn_bottom").css("background-color","#545253");
	}
	if($layout.attr("infobuttons") != null && $layout.attr("infobuttons") != "") {
		$(".extra_button > a").css("background-color","#"+$layout.attr("infobuttons"));
	} else {
		$(".extra_button > a").css("background-color","#434242");
	}
	if($layout.attr("logourl") != null && $layout.attr("logourl") != "") {
		$(".logo").html("<img src='"+$layout.attr("logourl")+"' alt=''>");
		$(".company_info").css("left",$layout.attr("logowidth")*1+15+"px");
	} else {
		$(".logo").html("");
		$(".company_info").css("left", "15px");
	}
}

function setlanguage() {
	var tgt = 0;
	if(currentlan == 0) {
		tgt = 1;
	} else {
		tgt = 0;
	}
	
	$(".language_button > a").text(languagearray[tgt][11]);
	$(".reservation > a").text(l_array[0][tgt]);
	$(".boxclose").each(function(){ $(this).children("a").text(languagearray[tgt][6]); });
	$(".mapbuttons").parent(".boxdescription").children("h1").text(languagearray[tgt][12]);
	$(".box1 > .boxcontent > .boxdescription").children("h1").text(languagearray[tgt][0]);
	$(".btn_box1 > a").text(languagearray[tgt][0]);
	$(".btn_box2 > a").text(languagearray[tgt][1]);
	$(".btn_box3 > a").text(languagearray[tgt][2]);
	$(".btn_box4 > a").text(languagearray[tgt][3]);
	$(".btn_box5 > a").text(languagearray[tgt][12]);
	
	$(".btn_box6 > a").text(languagearray[tgt][13]);
	$(".box6 > .boxcontent > .boxdescription").children("h1").text(languagearray[tgt][13]);
	
	document.getElementById("roomselect").options[0].text = "Premises";
	$("#roomselect").children("optgroup").each(function(){
		$(this).attr("label", typearray[tgt][typearray[currentlan].indexOf($(this).attr("label"))]);
	});
	
	$(".mapbutton").each(function(){
		$(this).text(floorsarray[tgt][floorsarray[currentlan].indexOf($(this).text())]);
	});
	
	//currenttour = -1;
	//checkinfo();
	currentlan = tgt;
}
if(!Array.prototype.indexOf) {
	Array.prototype.indexOf = function(obj, start) {
	     for (var i = (start || 0), j = this.length; i < j; i++) {
	         if (this[i] === obj) { return i; }
	     }
	     return -1;
	}
}
function htmlEntities(str) {
    return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}
