

/********************************
 * GET ARIST PORTFOLIOS
 *
 ********************************/


function get_portfolios(search_query, order_by) {

	
	document.getElementById('artist-portfolios').innerHTML = '<img src="/images/loading.gif" class="icon" /> Loading artist portfolios...';


	new Ajax.Request('/includes/get_portfolios.ajax.php',
	  {
		method:'post',
		parameters: {search_query: search_query, order_by: order_by},
		onSuccess: function(transport){
		  var response = transport.responseText || "no response text";
		  
		  var text = '';
		  if (search_query) {
		  	text = 'Showing portfolios for: <strong>' + search_query + '</strong><br /><br />';
		  }
		  
		  document.getElementById('artist-portfolios').innerHTML = 
		  	text + response;
		
		},
		onFailure: function(){ alert('Something went wrong...') }
	  });

}






/********************************
 * GET ALL EXHIBITIONS ON NOW
 *
 ********************************/

function exhibitions_on_now() {

	/* CHANGE TITLE IMAGE */
	document.getElementById('img-exhibs').style.display='inline';
	document.getElementById('img-results').style.display='none';
	
	
	document.getElementById('sr-message').innerHTML = '';
	
	document.getElementById('sr-on-now').innerHTML = '<img src="/images/loading.gif" class="icon" /> Loading exhibitions on now...';
	
	 
	 new Ajax.Request('/includes/get_exhibitions.ajax.php',
	  {
		method:'post',
		parameters: {search_query: ''},
		onSuccess: function(transport){
		  var response = transport.responseText || "no response text";
		  document.getElementById('sr-on-now').innerHTML = response;
		},
		onFailure: function(){ alert('Something went wrong...') }
	  });
	
}










/********************************
 * SEARCH FOR GALLERIES / EXHIBITIONS 
 * / ARTISTS
 *
 ********************************/

function search_results(search_query) {
	
	
	/* CHANGE TITLE IMAGE */
	document.getElementById('img-exhibs').style.display='none';
	document.getElementById('img-results').style.display='inline';
	
	
	/* CLEAR EXISTING ITEMS */
	document.getElementById('sr-on-now').innerHTML = '';
	document.getElementById('sr-galleries').innerHTML = '';
	document.getElementById('sr-exhibitions').innerHTML = '';
	document.getElementById('sr-portfolios').innerHTML = '';

	

	
	if (search_query == '') {
		document.getElementById('sr-message').innerHTML = 'To search for galleries, exhibitions and artists, please enter a search query in the box above, and press enter.<br /><br />Or <a onclick="exhibitions_on_now()" class="pointer">click here to see all exhibitions on now</a>.';
		return;
	} else if (trim(search_query).length < 3) {
		document.getElementById('sr-message').innerHTML = 'Please enter a search query at least three characters long.';
		return;
	}

	
	
	document.getElementById('sr-galleries').innerHTML = '<img src="/images/loading.gif" class="icon" /> Searching galleries, exhibitions and artists...<br /><br />';

	


	

	new Ajax.Request('/includes/get_galleries.ajax.php',
	  {
		method:'post',
		parameters: {search_query: search_query},
		onSuccess: function(transport){
		  var response = transport.responseText || "no response text";
		  
		  document.getElementById('sr-galleries').innerHTML = 
		  '<h2>Galleries</h2>' + response;
		  
		},
		onFailure: function(){ alert('Something went wrong...') }
	  });
	 
	 
	 
	 new Ajax.Request('/includes/get_exhibitions.ajax.php',
	  {
		method:'post',
		parameters: {search_query: search_query},
		onSuccess: function(transport){
		  var response = transport.responseText || "no response text";
		  
		  document.getElementById('sr-exhibitions').innerHTML = 
		  '<h2>Exhibitions</h2>' + response;
		  
		},
		onFailure: function(){ alert('Something went wrong...') }
	  });

	
	
	new Ajax.Request('/includes/get_portfolios.ajax.php',
	  {
		method:'post',
		parameters: {search_query: search_query},
		onSuccess: function(transport){
		  var response = transport.responseText || "no response text";
		  
		  document.getElementById('sr-portfolios').innerHTML = 
		  '<h2>Artists</h2>' + response;

		  
		},
		onFailure: function(){ alert('Something went wrong...') }
	  });
	
			
	
	document.getElementById('sr-message').innerHTML = '';
	

}










/********************************
 * GET EXHIBITIONS FOR A GALLERY
 *
 ********************************/

function get_exhibitions(gallery_id) {

	document.getElementById('gallery-exhibitions').innerHTML = '<img src="/images/loading.gif" class="icon" /> Loading exhibitions...';
	
	new Ajax.Request('/includes/gallery_exhibitions.ajax.php',
	{
		method:'post',
		parameters: {gallery_id: gallery_id},
		onSuccess: function(transport){
			var response = transport.responseText || "no response text";
			document.getElementById('gallery-exhibitions').innerHTML = response;
		},
		onFailure: function(){ alert('Something went wrong...') }
	});
}








/********************************
 * CLEAR/RESET TEXT INPUTS
 *
 ********************************/

function clear_input(id, text) {

	my_div = document.getElementById(id);
	
	if (my_div.value == text) {
		my_div.value = '';
		my_div.style.color='#3e3e3e';
	} 
	

}


function reset_input(id, text) {
	
	my_div = document.getElementById(id);
	
	if (my_div.value == '') {
		my_div.value = text;
		my_div.style.color='#757575';
	}

}






 /* borrow from www.quirksmode.org 
 (and changed) 
 */
function check_key(e) {

	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;

	if (code == 13) {
		search_results(document.getElementById('search_query').value);
	}

 }
 
 
 
 
 /**
*
*  Javascript trim, ltrim, rtrim
*  http://www.webtoolkit.info/
*
*
**/

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
 





/* GOOGLE MAPS STUFF */

var map = null;
var geocoder = null;

function initialize() {

  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map_canvas"));
	//map.setCenter(new GLatLng(37.4419, -122.1419), 13);
	
	map.addControl(new GSmallZoomControl());
	//map.addControl(new GSmallMapControl());

	geocoder = new GClientGeocoder();
	
	directions = new GDirections(map);
	GEvent.addListener(directions, "load", onGDirectionsLoad);
	GEvent.addListener(directions, "error", handleErrors);


  }
}

function showAddress(address) {
  if (geocoder) {
    geocoder.setBaseCountryCode('AU');
	geocoder.getLatLng(
	  address,
	  function(point) {
		if (!point) {
		  alert(address + " not found");
		} else {
		  map.setCenter(point, 13);
		  var marker = new GMarker(point);
		  map.addOverlay(marker);
		  //marker.openInfoWindowHtml(address);
		  
		  //remove directions
		  directions.clear();
		  //remove time
		  document.getElementById('time').innerHTML = '';
		}
	  }
	);
  }
}


function showDirections(from, to) {
	
	if ((from == '') || (to == '')) {
		alert('Please enter an address in the box.');
	} else {

		directions.clear();
		directions.load(from + ' to ' + to);
	
		//blah = directions.getStatus();
		//alert(blah.code);
	}
}


function onGDirectionsLoad() {

	trip_time = directions.getDuration();
	document.getElementById('time').innerHTML = 'Trip time: ' + trip_time.html;
}




    function handleErrors(){
	   if (directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + directions.getStatus().code);
	   else if (directions.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + directions.getStatus().code);
	   
	   else if (directions.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + directions.getStatus().code);

	//   else if (directions.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + directions.getStatus().code);
	     
	   else if (directions.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + directions.getStatus().code);

	   else if (directions.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + directions.getStatus().code);
	    
	   else alert("An unknown error occurred.");
	   
	}




