//inizio pagination
pagPerPage=20;


var paglength;
var pagCurrentPage;
var pagCurrentSorting;
var logDebug;
var ilogDebug;
ilogDebug=1;

pagV 				= new Array();//variablie generale
pagHTML 			= new Array();
pagmasterHTML 		= new Array();
pagPrices 			= new Array();
pagNames 			= new Array();
pagRatings 			= new Array();
pagZones 			= new Array();
pagSorting 			= new Array();
pagPricesSorted 	= new Array();
pagNamesSorted 		= new Array();
pagRatingsSorted 	= new Array();
pagZonesSorted 		= new Array();
pagSortingSorted 	= new Array();
function addLog(t){
	logDebug=logDebug+"\n"+t;
	
	//document.getElementById('logDebugTxt').value=logDebug;
	ilogDebug++;
}
function sortNumber(a, b){
	return a - b;
}
function reverseSort(a, b) { 
   if(a > b) 
      return -1 
   if(a < b) 
      return 1 
   return 0 
} 


function pagInit(){
	paglength=pagHTML.length-1;	
	pagCurrentPage=1;
	pagPages=Math.max(1,parseInt(paglength/pagPerPage+0.9999));
	pagPrintLoadingEnd();
}
function pagPrintSorting(){
	pagV['pagPaginationHTML'] = "<span class=\"loadingSorting\">Ordina per: ";
	if(pagCurrentSorting!= 'price'){
		pagV['pagPaginationHTML']= pagV['pagPaginationHTML']+" <a href=\"javascript:;\" onClick=\"pagSortBy('price')\">Prezzo</a> ";
	}else{
		pagV['pagPaginationHTML']= pagV['pagPaginationHTML']+" &raquo; <font color='#FF0000'><b>Prezzo</b></font> &laquo; ";
	}
	if(pagCurrentSorting!= 'rating'){
		pagV['pagPaginationHTML']= pagV['pagPaginationHTML']+" <a href=\"javascript:;\" onClick=\"pagSortBy('rating')\">Stelle</a> ";
	}else{
		pagV['pagPaginationHTML']= pagV['pagPaginationHTML']+" &raquo; <font color='#FF0000'><b>Stelle</b></font> &laquo; ";
	}
	if(pagCurrentSorting!= 'name'){
		pagV['pagPaginationHTML']= pagV['pagPaginationHTML']+" <a href=\"javascript:;\" onClick=\"pagSortBy('name')\">Nome</a> ";
	}else{
		pagV['pagPaginationHTML']= pagV['pagPaginationHTML']+" &raquo; <font color='#FF0000'><b>Nome</b></font> &laquo; ";
	}
		
	pagV['pagPaginationHTML']= pagV['pagPaginationHTML']+"</span>";
	if(paglength < 5) pagV['pagPaginationHTML']='';
	pagV['pagPaginationHTML']=pagV['pagPaginationHTML']+ "<span class=\"loadingPages\">"+(((pagCurrentPage -1 )*pagPerPage) + 1 )+" -&gt; "+Math.min(paglength,(pagCurrentPage*pagPerPage))+" di "+paglength+" strutture";
	if(pagPerPage<paglength){
		pagV['pagPaginationHTML']= pagV['pagPaginationHTML']+", Pagina:  ";
		for(i=1;i<=pagPages;i++){
			if(i==pagCurrentPage){
				pagV['pagPaginationHTML']= pagV['pagPaginationHTML']+" [ <font color='#FF0000'><b>"+i+"</b></font> ]";
			}else{
				pagV['pagPaginationHTML']= pagV['pagPaginationHTML']+" <a href=\"javascript:;\"  onClick=\"pagPag("+i+")\"> "+i+" </a> ";
			}
		}
	}
	pagV['pagPaginationHTML']= pagV['pagPaginationHTML']+"</span>";
	pagV['pagPaginationHTML']=pagV['pagPaginationHTML']+"&nbsp; &nbsp;";
	if(paglength < 1)pagV['pagPaginationHTML']='Nessuna struttura corrisponde alle richieste';
	document.getElementById('pagSortingTop').innerHTML=pagV['pagPaginationHTML'];
	if(paglength<5)pagV['pagPaginationHTML']='';
	document.getElementById('pagSortingBottom').innerHTML=pagV['pagPaginationHTML'];
	
}
function pagSortBy(s){
	pagPrintLoadingStart();
	pagSetCurrentSorting(s);
	setTimeout("pagSortByDo()",200);
}
function pagSortByDo(){
	if(pagCurrentSorting == 'price'){
		pagTmpArr= pagPricesSorted;
	}else if(pagCurrentSorting == 'name'){
		pagTmpArr= pagNamesSorted;
	}else if(pagCurrentSorting == 'zone'){
		pagTmpArr= pagZonesSorted;
	}else if(pagCurrentSorting == 'rating'){
		pagTmpArr= pagRatingsSorted;
	}else { //if(pagCurrentSorting == 'sorting')
		pagTmpArr= pagSortingSorted;
	}
	addLog("\n\nSORTING BY  "+pagCurrentSorting);
	for (x in pagTmpArr){
		if(pagTmpArr[x]>0){
			divID=parseInt(x)+1;
			addLog('pagHTML['+divID+'] > pagmasterHTML['+pagTmpArr[x]+']');
			pagHTML[divID]=pagmasterHTML[pagTmpArr[x]];
		}
	}	
	pagPag(1);
}
function pagSetCurrentSorting(s){
	pagCurrentSorting=s;
}
function pagSetCurrentPage(pg){
	pagCurrentPage=pg;
}
function pagPag(pg){
	pagPrintLoadingStart();
	pagSetCurrentPage(pg);
	setTimeout("pagPagDo()",1000);
}
function pagPagDo(){
	pagStart=pagCurrentPage*pagPerPage-pagPerPage;
	pagEnd=pagStart+pagPerPage;
	addLog("\n\nPAGE "+pagCurrentPage);
	for(i=1;i<=Math.min(pagPerPage,paglength) ;i++){//&& i<=paglength  - pagStart
		addLog('pagDiv'+i+'> pagHTML['+(i+pagStart)+']');
		document.getElementById('pagDiv'+i+'').innerHTML=(pagHTML[i+pagStart]?pagHTML[i+pagStart]:'');
	}
	pagPrintLoadingEnd();
}

function pagAddRow(){
	paglength=pagHTML.length-1;
	document.getElementById('pagSortingTop').innerHTML=""+paglength+" alberghi trovati / Caricamento in corso...&nbsp; &nbsp;";
}
function pagPrintLoadingStart(){
	document.getElementById('pagSortingTop').className='startLoading blu';
	document.getElementById('pagSortingBottom').className='startLoading blu';
	document.getElementById('pagSortingTop').innerHTML="Caricamento...&nbsp; &nbsp;";
	document.getElementById('pagSortingBottom').innerHTML="Caricamento...&nbsp; &nbsp;";
}
function pagPrintLoadingEnd(){
	pagPrintSorting();
	document.getElementById('pagSortingTop').className='endLoading blu';
	document.getElementById('pagSortingBottom').className='endLoading blu';
}




imgMiniZoomHistory= new Array();
function imgMiniZoom(h,i){
	iObj=document.getElementById('miniImg_'+h+'_'+i+'');
	document.getElementById('listImg'+h+'').src=iObj.src;
	iObj.className="imgAcrive";
	if(!imgMiniZoomHistory[h])imgMiniZoomHistory[h]=0;
	document.getElementById('miniImg_'+h+'_'+imgMiniZoomHistory[h]+'').className="";
	imgMiniZoomHistory[h]=i;
}



//////////////////////////////////////



	var map1 = false;
	var geocoder = new GClientGeocoder();
	var hotelsInMap= new Array();
	var hotelsInMapCounter= 0;
	var baseIcon = new GIcon();
	var mapHotelLimit=10;
	var mapZoomLevel=9;
	var marker=new Array();
	//baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	baseIcon.iconSize = new GSize(26, 15);
	//baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(1, 19);
	baseIcon.infoWindowAnchor = new GPoint(24, 1);
	baseIcon.infoShadowAnchor = new GPoint(20, 18);

	function loadSearchMap() {
      if (GBrowserIsCompatible() && document.getElementById("GmapSmall")) {
        map1 = new GMap2(document.getElementById("GmapSmall"));
		//map1.setCenter(new GLatLng(37.4419, -122.1419), mapZoomLevel);
		setCenterAddress('New York');
		map1.addControl(new GMapTypeControl());
		map1.addControl(new GLargeMapControl());
		map1.addControl(new GOverviewMapControl());
		map1.addControl(new GScaleControl());
		//initScrollCheck();
		//moveMap();
	  }
    }
	function setCenterAddress(address) {
		if(!map1) return;
		geocoder.getLatLng(address,
		function(point) {if (!point) {alert(address + " not found");} else {map1.setCenter(point, mapZoomLevel);}
		});
	}
	function addHotelAddress(address) {
		if(!map1) return;
	  geocoder.getLatLng(
		address,
		function(point) {
		  if (!point) {
			//alert(address + " not found");
		  } else {
			//map1.setCenter(point, mapZoomLevel);
			var marker = new GMarker(point);
			map1.addOverlay(marker);
			//marker.openInfoWindowHtml(address);
		  }
		}
	  );
	}   
	function addHotel(hid) {
		if(!map1) return;
		if(!hid) return;
		if(!hotelsInMap[hid]) return;
		lat=hotelsInMap[hid]['lat'];
		lon=hotelsInMap[hid]['lon']; 
		imgUrl="http://www.alberghi-newyork.com/img/mapIcons/mark.php?preset=1&string="+hid+"";
		var icon = new GIcon(baseIcon);
		icon.image = imgUrl;
		position=new GLatLng(lat,lon);
		marker[hid] = new GMarker(position,icon);
		map1.addOverlay(marker[hid]);
		GEvent.addListener(marker[hid], "click", function() {
   			 parent.location="#htl"+hid+"";
			 //window.scroll(Math.max((winY() - 150),270));
			 map1.setCenter(position, mapZoomLevel);
  		});
		imgListObj=document.getElementById('imgIlist'+hid+'');
		imgListObj.src=imgUrl;
		imgListObj.alt="Clicca per Rimuovere la struttura dalla mappa";
		hotelsInMap[hid]['active']=true;
	}
	function removeHotel(hid) {
		if(!map1) return;
		map1.removeOverlay(marker[hid]);
		imgUrl="http://www.alberghi-newyork.com/img/mapIcons/mark.php?preset=2&string="+hid+"";
		imgListObj=document.getElementById('imgIlist'+hid+'');
		imgListObj.src=imgUrl;
		imgListObj.alt="Clicca per centrare la mappa su questa struttura";
		hotelsInMap[hid]['active']=false;
		
	}
	function addHotels(){
		
		if(!map1) return;
		
		for(i=0,j=0;j<hotelsInMap.length ;i++) { 
			if(i>=mapHotelLimit) break;
			if(!hotelsInMap[i])continue;
			j++;
			setTimeout('addHotel("'+ i +'")',500 + 1*100);
			hotelsInMap[i]['active']=true;
		}		
	}
	
	function addHotelInMap(hid,lat,lon){
		hotelsInMap[hid]=new Array();
		hotelsInMap[hid]['hid']=hid;
		hotelsInMap[hid]['lat']=lat;
		hotelsInMap[hid]['lon']=lon;
		hotelsInMap[hid]['active']=false;
		hotelsInMapCounter++;
	}
	
	function onOffMap(hid){
		if(!document.getElementById("GmapSmall")) return;
		if(!map1) alert('attendere il caricamento della mappa');
		if(hotelsInMap[hid]['active']){
			removeHotel(hid);
		}else{
			//setMapYmouse() ;
			parent.location="#";
			addHotel(hid);
			map1.panTo(position, mapZoomLevel);
		}
	}




function setMapYmouse(){
	document.getElementById("GmapSmall").style.top=Math.max((yMousePos - 160),270);
}
function setMapYscroll(){
	document.getElementById("GmapSmall").style.top=Math.max((winY() - 150),270);
}
function winY() {//non funziona
	$w=(document.all)?document.body.scrollTop:window.pageYOffset;
	alert($w);
	return $w;
}

function initScrollCheck(){
	if (document.layers) { // Netscape
		document.captureEvents(Event.MOUSEMOVE);
		document.onmousemove = captureMousePosition;
	} else if (document.all) { // Internet Explorer
		document.onmousemove = captureMousePosition;
	} else if (document.getElementById) { // Netcsape 6
		document.onmousemove = captureMousePosition;
	}
	
}
xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page

function captureMousePosition(e) {
    if (document.layers) {
		yMousePos = e.pageY;
		//xMousePosMax = window.innerWidth+window.pageXOffset;
    } else if (document.all) {
		yMousePos = window.event.y+document.body.scrollTop;
		//xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
    } else if (document.getElementById) {
		yMousePos = e.pageY;
		//xMousePosMax = window.innerWidth+window.pageXOffset;
    }
}


var theTop=270;
var old =theTop;
var moveDelay=500;
var moveSpeed=100;
function moveMap(){
	if (window.innerHeight)
	{
		  pos = window.pageYOffset
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		pos = document.documentElement.scrollTop
	}
	else if (document.body)
	{
		  pos = document.body.scrollTop
	}
	if (pos < theTop){
		pos = theTop;	
	}
	tmpTimeout=moveDelay;
	if (pos != old){
		middlepos= old + ( (pos - old)/2 );
		tmpTimeout=moveSpeed;
		if(Math.abs(pos - middlepos) < 5){
			middlepos=pos;
			tmpTimeout=moveDelay;
		}
		document.getElementById("GmapSmall").style.top = middlepos+'px';
		old = middlepos;
	}
	temp = setTimeout('moveMap()',tmpTimeout);
}
