﻿/*
 * ----------------------------------------------
 * Panorama rotator v1.0
 * Copyright (c) RulacDesign
 * URL: http://www.rulac.net
 * ----------------------------------------------
 */


var width_layout = 976;
var pano_interval;
function pano_rotate(way){
	if (way == undefined){
		way = "stop";
	}
	if (pano_interval != undefined || way == "stop"){
		window.clearInterval(pano_interval);
	}
	if (panoimg = document.getElementById("panorama360")){
		is360 = true;
	} else if (panoimg = document.getElementById("panorama")){
		is360 = false;
	}
	pano = document.getElementById("panoel");
	if (way != "stop"){
		width = pano.style.backgroundPosition.split(" ")[0];
		width = parseInt(width);
		if (is360 == true){
			if (width < 0){
				width = panoimg.width;
			}
			if (width > panoimg.width){
				width = 1;
			}
		} else if (is360 == false){
			if (width < (width_layout - panoimg.width)){
				way = "left";
			}
			if (width > 0){
				way = "right";
			}
		}
		if (way == "left"){
			width = width +  1;
		} else if (way == "right"){
			width = width -  1;
		}
		pano.style.backgroundPosition = width + "px 0";
		pano_interval = window.setInterval(function(){pano_rotate(way);}, 30);
	}
}
function pano_onoff(){
	panoimg = false;
	if (panoimg = document.getElementById("panorama360")){
		is360 = true;
	} else if (panoimg = document.getElementById("panorama")){
		is360 = false;
	}
	pano360htmlon = "<a href=\"javascript:;\" id=\"pano360on\" title=\"panorama 360\" onclick=\"pano_onoff();\">on</a>";
	panohtmlon = "<a href=\"javascript:;\" id=\"panoon\" title=\"panorama\" onclick=\"pano_onoff();\">on</a>";
	if (panoimg){
		if (!document.getElementById("panoel")){
			panonavig = document.createElement("span");
			panonavig.id = "panonavig";
			panonavig.style.marginTop = panoimg.height - 38 + "px";
			panonavig.style.display = "block";
			if (is360){
				panonavig.innerHTML = pano360htmlon;
			} else {
				panonavig.innerHTML = panohtmlon;
			}
			panoimg.parentNode.insertBefore(panonavig, panoimg);
			panoel = document.createElement("span");
			panoel.id = "panoel";
			panoel.innerHTML = "&nbsp;";
			panoel.style.display = "none";
			panoel.style.height = panoimg.height + "px";
			panoel.style.backgroundImage = "url('" + panoimg.src + "')";
			if (is360){
				panoel.style.backgroundPosition = "0 0";
			} else {
				panoel.style.backgroundPosition = -((panoimg.width - width_layout) / 2) + "px 0";
			}
			panoimg.parentNode.insertBefore(panoel, panoimg);
			
			panoel.onmousedown = listen;
		} else {
			panoel = document.getElementById("panoel");
			panonavig = document.getElementById("panonavig");
			if (panoel.style.display == "" || panoel.style.display == "none"){
				panoel.style.display = "block";
				panoimg.style.display = "none";
				document.getElementById("fullimage").style.width = width_layout + "px";
				panonavig.innerHTML = "<a href=\"javascript:;\" id=\"panoleft\" title=\"left\" onclick=\"pano_rotate('left');\">left</a> <a href=\"javascript:;\" id=\"panostop\" title=\"pause\" onclick=\"pano_rotate('stop');\">stop</a> <a href=\"javascript:;\" id=\"panoright\" title=\"right\" onclick=\"pano_rotate('right');\">right</a> <a href=\"javascript:;\" id=\"panooff\" title=\"close\" onclick=\"pano_onoff();\">off</a>";
				pano_rotate("stop");
			} else {
				panoel.style.display = "none";
				panoimg.style.display = "block";
				document.getElementById("fullimage").style.width = panoimg.width + "px";
				if (is360){
					panonavig.innerHTML = pano360htmlon;
				} else {
					panonavig.innerHTML = panohtmlon;
				}
				pano_rotate("stop");
			}
		}
	}
}
function listen(e){
	document.onselectstart = function(){return false;}
	e = e || window.event;													
	this.mouseX = e.clientX;
	var elem = this;
	document.onmousemove = function(e){
		move(e, elem);
	}								
	document.onmouseup = clear;
}
function clear(){
	document.onselectstart = null;
	document.onmousemove = null;
}
function move(e, elem){
	e = e || window.event;
	panoel = document.getElementById("panoel");
	width = panoel.style.backgroundPosition.split(" ")[0];
	width = parseInt(width);
	if ((elem.mouseX - e.clientX) == 0){
		pano_rotate("stop");
	} else if ((elem.mouseX - e.clientX) > 0){
		pano_rotate("right");
	} else {
		pano_rotate("left");
	}
	newwidth = width - elem.mouseX + e.clientX;
	if (panoimg = document.getElementById("panorama")){
		if (newwidth < 0 && newwidth > (width_layout - panoimg.width)){
			panoel.style.backgroundPosition = newwidth + "px 0";
		} else {
			pano_rotate("stop");
		}
	} else {
		panoel.style.backgroundPosition = newwidth + "px 0";
	}
	elem.mouseX = e.clientX;
}
if(typeof window.addLoadEvent != "function"){
	function addLoadEvent(func){
		var oldonload = window.onload;
		if (typeof window.onload != "function"){
			window.onload = func;
		} else {
			window.onload = function(){
				if (oldonload){
					oldonload();
				}
				func();
			}
		}
	}
}
function pano_autostart(){
	pano_onoff();
	pano_onoff();
	pano_rotate("right");
	
}
addLoadEvent(pano_autostart);