var iframe_image = null;
var idpano_returned;
var basedir;

// Iframe Pano
//------------------------------------------------

function hideshow_othersdates() {
	if ( document.getElementById("others-dates-bt").value == "Voir d'autres dates" ) {
		document.getElementById("others-dates-bt").value = "Cacher les dates";
		document.getElementById("image_other_date").style.display = "";
	} else {
		document.getElementById("others-dates-bt").value = "Voir d'autres dates";
		document.getElementById("image_other_date").style.display = "none";
	}
}

function showLoading(){
	document.getElementById("loading").innerHTML = '<img src="images/loading.gif" style="position: relative; left: 254px; top: 17px;">';
}

// 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;
	//alert(qstr);
    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;

	var zoomTab = '<a href="javascript:void(0);" onClick="showPano();"><table border="0" cellspacing="0" cellpadding="0"><tr><td align="center" valign="top">';
	zoomTab += '<img id="zoom7"  src="' + basedir + '/77/small/img77-' + idpano_returned + '.jpg">';
	zoomTab += '</td></tr></table></a>';
	
	document.getElementById("loading").innerHTML = zoomTab;
	document.getElementById("otheridpano").innerHTML = formatDate(idpano_returned);
}

function showPano() {
    var val = "iframe_image.php?id=" + document.getElementById("idpano").value;
    document.getElementById("iframe_name").src = val;
	document.getElementById("idpano_label").innerHTML = document.getElementById("otheridpano").innerHTML;
}

function showNextPano() {
	sendRequest('getPano.php','next');
	if ( idpano_returned == document.getElementById("idpano").value ) {
		alert('Pas de date suivante.\nUtilisez le bouton "<" pour voir les dates précédentes.');
	} else {
	    document.getElementById("idpano").value = idpano_returned;
	}
}

function showPrevPano() {
	sendRequest('getPano.php','prev');
	if ( idpano_returned == document.getElementById("idpano").value ) {
		alert('Pas de date précédente.\nDernière date.');
	} else {
	    document.getElementById("idpano").value = idpano_returned;
	}
}

function init(dir,id) {
	basedir = dir;
    document.getElementById("idpano").value = id;
}

