window.addEvent('domready', function(){

	if( $('hotelsearchform') )
	{
		$('hotelsearchform').addEvent('submit', function(){
			
			var usersearch = $('search-q').value;
			
			pageTracker._trackEvent('Hakukoneen haku', 'Hae nappi', usersearch );
			
		});
		
	}

});

/* ---------------------------- */
/* XMLHTTPRequest Enable 		*/
/* ---------------------------- */
function createObject() {
	var request_type;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
	request_type = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		request_type = new XMLHttpRequest();
	}
		return request_type;
}

var http = createObject();

/* -------------------------- */
/* SEARCH					 */
/* -------------------------- */
function autosuggest() {
q = document.getElementById('search-q').value;
// Set te random number to add to URL request
nocache = Math.random();
http.open('get', './phpbin/plugins/bookingHotels/search.php?q='+q+'&nocache = '+nocache);
http.onreadystatechange = autosuggestReply;
http.send(null);
}
function autosuggestReply() {
if(http.readyState == 4){
	var response = http.responseText;
	e = document.getElementById('results');
	if(response!=""){
		e.innerHTML=response;
		e.style.display="block";
	} else {
		e.style.display="none";
	}
}
}

function searchfocus()
{
	q = document.getElementById('search-q').value;
	if( q == "Stad..." )
		document.getElementById('search-q').value = "";
}

function setsearchq( searchq )
{
	e = document.getElementById('search-q');
	e.value=searchq;
	
	r = document.getElementById('results');
	r.style.display="none";
	
	
	exact = document.getElementById('exactsearch');
	exact.value="1";
		
	var usersearch = $('search-q').value;
	pageTracker._trackEvent('Hakukoneen haku', 'Auto suggest', usersearch );
		
	document.getElementById('hotelsearchform').submit();
	
}

function searchbuttonclick()
{
	document.getElementById('exactsearch').value="0";
	return true;
}

function orderresults( ordertype )
{
		
	e = document.getElementById('orderby');
	f = document.getElementById('pagenumber');
	if(e)
	{
		e.value = ordertype;
		f.value = '1';
		
		var ordername = 'hotellin nimi';
		if( ordertype == 2 )
			ordername = 'hotellin tähdet';
		else if( ordertype == 3 )
			ordername = 'hotellin arvostelut';
		else if( ordertype == 4 )
			ordername = 'hotellin hinta';
		
		pageTracker._trackEvent('Javascript events', 'Tulosten järjestys', ordername );
		
		document.getElementById('hotelsearchform').submit();
	}
}

function gotopage(newpage)
{
	e = document.getElementById('pagenumber');
	if( e )
	{
		e.value = newpage;
		
		pageTracker._trackEvent('Javascript events', 'Siirry tulosten sivulle', newpage );
		document.getElementById('hotelsearchform').submit();
	}
}

function togglemap( togglemode )
{
	e = document.getElementById('mapvisible');
	if( e )
	{
		e.value = togglemode;
		pageTracker._trackEvent('Javascript events', 'Tulokset kartalla', togglemode );
		document.getElementById('hotelsearchform').submit();
	}
}

function createPublicMarker(lat, lng, htmldata, tdproductid, stars, hotelname)
{
	var point = new GLatLng(lat, lng);
  	var markeroptions;
	var customIcon = new GIcon(G_DEFAULT_ICON);
	if(stars == 0)
		customIcon.image = "./media/images/mapicon_hotel.png"; // "./favicon.png";
	else if(stars == 1)
		customIcon.image = "./media/images/mapicon_hotel_1star.png";
	else if(stars == 2)
		customIcon.image = "./media/images/mapicon_hotel_2star.png";
	else if(stars == 3)
		customIcon.image = "./media/images/mapicon_hotel_3star.png";
	else if(stars == 4)
		customIcon.image = "./media/images/mapicon_hotel_4star.png";
	else if(stars == 5)
		customIcon.image = "./media/images/mapicon_hotel_5star.png";
	
	customIcon.iconSize = new GSize(27, 33); 
	customIcon.shadow = '';
	//customIcon.shadowSize = new GSize(27, 33); 		
	markerOptions = { icon:customIcon, title:hotelname };			  				
	
	var marker = new GMarker(point, markerOptions);
	
	GEvent.addListener( marker
				  , "click"
				  , function() 
  				    {
    					retrieveMarkerData(tdproductid, marker);
					}
  				  );
				  
	return marker;
  				 	
}

function retrieveMarkerData(tdproductid, marker)
{
	var ajaxurl = './phpbin/plugins/bookingHotels/ajax_calls.php?task=gethotel&hotel=' + tdproductid;
			
	var req = new Request.HTML({url:ajaxurl, 
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
			
			pageTracker._trackEvent('Javascript events', 'Kartta marker avattu', tdproductid );
			globalmarkerhtml = responseHTML;
			marker.openInfoWindowHtml(responseHTML, {maxHeight:310,autoScroll:true});
		},
		onFailure: function() {
			globalmarkerhtml = '';
			marker.openInfoWindowHtml(globalmarkerhtml, {maxHeight:310,autoScroll:true});
		}
	});
	req.send();	
		
}

function retrievePanoramioMarkerData(photo_id, marker)
{
	var ajaxurl = './data/ajax/ajax_calls.php?task=getpanoramio&image=' + photo_id;
	
	var req = new Request.HTML({url:ajaxurl, 
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
			globalmarkerhtml = responseHTML;
			marker.openInfoWindowHtml(responseHTML, {maxHeight:340,autoScroll:true});
			
		},
		onFailure: function() {
			globalmarkerhtml = 'The data for this marker is not available at the moment.';
			marker.openInfoWindowHtml(globalmarkerhtml, {maxHeight:340,autoScroll:true});
		}
	});
	req.send();	
		
}

function moreHotelInfo( hotel_id )
{
	var moreinfoid = 'moreinfo' + hotel_id;
	var basicinfoid = 'basicinfo' + hotel_id;
	var moreinfolinkid = 'moreinfolink' + hotel_id;
	
	var morephotosid = 'morephotos' + hotel_id;
	
	document.getElementById(moreinfolinkid).style.display = 'none';  
	document.getElementById(basicinfoid).style.display = 'none'; 
	document.getElementById(moreinfoid).style.display = 'block';
	
	var ajaxurl = './phpbin/plugins/bookingHotels/ajax_calls.php?task=gethotelphotos&hotel=' + hotel_id;
			
	var req = new Request.HTML({url:ajaxurl, 
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
			
			document.getElementById(morephotosid).style.display = 'block';
			document.getElementById(morephotosid).innerHTML = responseHTML;
			
		},
		onFailure: function() {
			document.getElementById(morephotosid).style.display = 'none';
		}
	});
	req.send();	
	
}