var iframe_image = null;
var idpano_returned;
var basedir;
var ratio;

// Iframe Pano
//------------------------------------------------

function showLoading(){
	document.getElementById("loading").innerHTML = '<img src="images/loading.gif" style="position: relative; left: 198px; top: 34px;">';
}

function set_iframe(iframe) {
	iframe_image = iframe;
}

function hideshow_zooms($label_show_zoom,$label_hide_zoom) {
	if(iframe_image) {
		if ( document.getElementById("show-zooms-bt").value == $label_hide_zoom ) {
			document.getElementById("show-zooms-bt").value = $label_show_zoom;
		} else {
			document.getElementById("show-zooms-bt").value = $label_hide_zoom;
		}
		iframe_image.hideshow_zooms();
	}
}

function hideshow_othersdates($label_show_date,$label_hide_date) {
	if ( document.getElementById("others-dates-bt").value == $label_show_date ) {
		document.getElementById("others-dates-bt").value = $label_hide_date;
		document.getElementById("pano_other_date").style.display = "";
	} else {
		document.getElementById("others-dates-bt").value = $label_show_date;
		document.getElementById("pano_other_date").style.display = "none";
	}
}

var autoscrollTimerID=null;
var autoscrollTime=30;

function init(dir,id,r) {
	basedir = dir;
	ratio = r;
    document.getElementById("idpano").value = id;

	doScroll();
}

function autoScroll() {
	var iframe_name=window.frames['iframe_name'];
	var max=iframe_name.document.body.scrollWidth-iframe_name.document.body.offsetWidth-16;

	autoscrollTimerID=setTimeout("autoScroll()", autoscrollTime);

	if(sens==1) {
		if(iframe_name.document.body.scrollLeft < max)
			iframe_name.document.body.scrollLeft=iframe_name.document.body.scrollLeft + scrollspeed;
		else {
			iframe_name.document.body.scrollLeft=max;
			sens=-1;
		}
	}
	else {
		if(iframe_name.document.body.scrollLeft > 0)
			iframe_name.document.body.scrollLeft=iframe_name.document.body.scrollLeft - scrollspeed;
		else {
			iframe_name.document.body.scrollLeft=0;
			sens=1;
		}
	}
}

function doScroll(){
	var largeur=window.frames['iframe_name'].document.body.offsetWidth;

	var s = 253 - (largeur / 2);

	window.frames['iframe_name'].scrollBy(s,0);
}

function scrollHandler(step) {
	iframe_name.document.body.scrollLeft=iframe_name.document.body.scrollLeft - step;
}


var scrolling;
var sens=1;
var scrollspeed=3;
scrollspeed=1;

function ScrollWay(D){
	stopScroll();
	
	if(D=="+"){sens=1;}
	else {sens=-1;}
	autoScroll();
}	

function stopScroll(){
	clearTimeout(autoscrollTimerID);
}

// Get pano (next/prev)
//------------------------------------------------

function sendRequest(strURL,action) {
	showLoading();

    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, false);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.send(getquerystring(action));
    if (self.xmlHttpReq.readyState == 4) {
        updatepage(self.xmlHttpReq.responseText);
    }
}

function getquerystring(action) {
    qstr = 'current=' + escape(document.getElementById("idpano").value);  // NOTE: no '?' before querystring
    qstr += "&action=" + action;
    return qstr;
}

function xmlError(e) {
	//there was an error, show the user
	alert(e);
} 

function formatDate(idpano) {
	var s = idpano.substr(6,2) + "/" + idpano.substr(4,2) + "/" + idpano.substr(0,4) + " " + idpano.substr(8,2) + ":" + idpano.substr(10,2);
	return s;
}

function updatepage(jsontext){
	//alert(jsontext);
	var myObj = eval( '(' + jsontext + ')' );
	idpano_returned = myObj.ResultSet.nextPano;
	document.getElementById("loading").innerHTML = '<a href="javascript:void(0);" onClick="showPano();"><img src="resize.php?url=' + basedir + idpano_returned + '.jpg&ratio=' + ratio + '"></a>'
	document.getElementById("otheridpano").innerHTML = formatDate(idpano_returned);
}

function showPano() {
	// Arret de l'autoscroll
	stopScroll();
	
    var val = "iframe_pano.php?idpano=" + document.getElementById("idpano").value;
    document.getElementById("iframe_name").src = val;
	document.getElementById("idpano_label").innerHTML = document.getElementById("otheridpano").innerHTML;
}

function showNextPano($label_no_next_pano) {
	sendRequest('getPano.php','next');
	if ( idpano_returned == document.getElementById("idpano").value ) {
		alert($label_no_next_pano);
	} else {
	    document.getElementById("idpano").value = idpano_returned;
	}
}

function showPrevPano($label_no_prev_pano) {
	sendRequest('getPano.php','prev');
	if ( idpano_returned == document.getElementById("idpano").value ) {
		alert($label_no_prev_pano);
	} else {
	    document.getElementById("idpano").value = idpano_returned;
	}
}



