// Javascript
// UI Enhance V0.5
// Author: Robert Lynch

if (window.console && window.console.firebug) {
	alert('Firebug is active and may cause problems loading Local Sing.\n Please disable the addon for this site.');
}

var isNN = (navigator.appName.indexOf("Netscape")!=-1);  //WTF! really? I should just get rid of this.
var hasMap = 0;
var locationChanged = false;


function formFocus(id){
	document.getElementById(id).focus();
}

function focusSearch(searchBox){
	searchBox.style.background = 'none';	
}

function blurSearch(searchBox){
	if(searchBox.value == ""){
		searchBox.style.background = "transparent url(/assets/images/search-box.gif) no-repeat scroll 0 0";
	}
}


// disables the default enter keypress with 
function disableEnter(e){
	
	var key;
	if(window.event){
		  key = window.event.keyCode;     //IE
	}else{
		 key = e.which;     //firefox
	}

	if (key == 13)
	{
		setTimeout("changeLoc(document.getElementById('address').value)",200);
		return false;
	}
	else
	{
		changeButtonColor('#ff358b')
		return true;
	}
}

function changeButtonColor(color){
	locationChanged = true;
	document.getElementById('changeButton').style.color = color;
}
	

function enableEnter(e){
	
	var key;
	if(window.event){
		  key = window.event.keyCode;     //IE
	}else{
		 key = e.which;     //firefox
	}

	if (key == 13)
	{
		document.kSearch.submit();
		return false;
	}else{
		return true;
	}
}




var gdir;
var addressMarker;
var map = null;
var geocoder = null;
var lat = 0;
var long = 0;
var hasMap = 0;	
var MyIcon = null;

function initialize() {
	
	if(document.getElementById("map_canvas"))
	{
		hasMap = 1;
	}

//    map = new google.maps.Map2(document.getElementById("map_canvas"));
//    map.setCenter(new google.maps.LatLng(37.4419, -122.1419), 13);
	if(hasMap){
		map = new google.maps.Map2(document.getElementById("map_canvas"));
	}
	if(typeof loadMap == 'function') {
		loadMap();
	}else{
		if(document.getElementById("lat").value != ''){
			if(hasMap){
				map.setCenter(new google.maps.LatLng(document.getElementById("lat").value, document.getElementById("long").value), 9);
			}
		}else{
			
			if (GBrowserIsCompatible()) {
		        geocoder = new GClientGeocoder();
		    }
			loadDefaultLoc();
		}
	}
	
	
	
}

function loadDefaultLoc(){
	var userLoc = google.loader.ClientLocation;
	//get user location and center the map
	if (userLoc != null) {
		lat = userLoc.latitude;
		long = userLoc.longitude;
		document.getElementById("address").value = userLoc.address.city + ', ' + userLoc.address.region + ', ' + userLoc.address.country;
		if(hasMap){
			map.setCenter(new google.maps.LatLng(lat, long), 9);
		}
		
	} else {
		document.getElementById("address").value = "Unknown";
		if(hasMap){
			map.setCenter(new google.maps.LatLng(lat, long), 1);
			
		}
	}
	
	document.getElementById("lat").value = lat;
	document.getElementById("long").value = long;
	
	setDefaultCookie();
	
}

function setDefaultCookie(){
	var fields = $("#kSearch").serializeArray();
	$.post("/search/setDefaults",fields,function(data,status)
		{
		//	alert(data);
		});
}


function toggleMessageWindow(which){
	if(document.getElementById(which).style.display == 'none'){
		$("#" + which).show("slow");
		$("#" + which + 'Tab').hide(1);
	}else{
		$("#" + which).hide("slow");
		$("#" + which + 'Tab').show(1);
	}
}


function writeMessageWindow(which,html){
	var listId = "statusList";
	
	if(which == "errorMessages"){
		listId = "errorList";
	}
	if(which == "successMessages"){
		listId = "successList";
	}
	if(which == "statusMessages"){
		listId = "statusList";
	}
	
	$("#" + listId).html(html);
	if(document.getElementById(which).style.display != 'block'){
		$("#" + which).show("slow");
		$("#" + which + 'Tab').hide(1);
	}
}


function submitForm(formId){
	document.getElementById(formId).submit();
}

function changeLoc(address,submitForm) {
	if (!geocoder) {
		if (GBrowserIsCompatible()) {
		        geocoder = new GClientGeocoder();
		}else{
			alert('Your browser is not fully compatible with Google Maps or detection has failed.\nPlease upgrade or report this error to us for assistance..');
		}
	}
	if(!locationChanged)
	{
		if(submitForm){
			document.getElementById('kSearch').submit();
			return true;
		}
		alert('Location wasn\'t changed.\nNo check needed.\nLocation:'+locationChanged);
		return true;
	}
	if (geocoder) {
		geocoder.getLatLng(
		  address,
		  function(point)
			{
				if (!point)
				{
				  alert('Address:\n' + address + '\nNot Found');
				  return false;
				} else {
					document.getElementById('changeButton').style.color = 'Black';
					document.getElementById("lat").value = point.y;
					document.getElementById("long").value = point.x;
					//reset this variable so we know we've already run the check, so don't do this again unless it changes again
					locationChanged = false;
					if(hasMap){
						if(submitForm){
							document.getElementById('kSearch').submit();
							return true;
						}
						map.panTo(new GLatLng(point.y, point.x));
						return true;
					}else{
						//alert('Location Changed');
						document.getElementById('kSearch').submit();
						return true;
					}			
				}
			}
		)//end of callback function
	}else{
		alert('The geocoder could not be initialised. Please report this error to us if you continue to get it.');
		return false;
	}
}

var previousArtist = false;
var previousSong = false;

function csvColChange(target,column){
	
	if(target.value == "custom"){
		//alert(document.getElementById('newCustom' + column).style.display);
	//	alert('newCustom' + column);
		document.getElementById('newCustom' + column).style.display = 'inline';
//		alert(document.getElementById('newCustom' + column).style.display);
	}else{
		document.getElementById('newCustom' + column).style.display = 'none';
	}
	
	if(target.value == "artist"){
		if(previousArtist && previousArtist != target){
			previousArtist.selectedIndex = 0;
		}
		previousArtist = target;
		if(previousSong == target){
			previousSong = false;	
		}
	}else if(target.value == "song"){
		if(previousSong && previousSong != target){
			previousSong.selectedIndex = 0;
		}
		previousSong = target;
		if(previousArtist == target){
			previousArtist = false;
		}
	}
	
}

function removePlaylistItem(songId){
	var postData = { id: songId };
	$.post("/search/removePlaylistItem",postData,function(data,status)
		{
			if(data == "song deleted"){
				document.getElementById('trackName' + songId).style.display = 'none';
			}else{
				alert('Error. Result from server was not as expected.\n Please reload this page.');	
			}
		});
	return false;
}

function confirmAction(message){
	return confirm(message);
}


var starsDefault = "";

function stars(rating, nightId){

	starsDefault = document.getElementById("userRating" + nightId).src;

	var newImage = "/assets/images/stars/yellow-" + rating + ".gif";
	$("#userRating" + nightId).attr("src",newImage);
	//alert(newImage + "\n#userRating" + who);
}

function restoreStars(nightId){
	$("#userRating" + nightId).attr("src",starsDefault);
}

function submitStars(rating,nightId){
//	alert(rating + ' ' + nightId);
	var postData = { "stars": rating, "night" : nightId };
	$.post("/karaoke_night/submitRating",postData,function(data,status)
		{
			//alert(data);
			if(data == 'thanks'){
				writeMessageWindow('successMessages','<li>You gave ' + rating + ' stars!  Thanks for voting!</li>');
				rating = rating * 10;
				starsDefault = "/assets/images/stars/yellow-" + rating + ".gif";
				$("#userRating" + nightId).attr("src",starsDefault);
				
			}else if(data == 'login'){
				writeMessageWindow('statusMessages','<li> You must be logged in to a singer account to submit ratings.</li>');
			}else{
				writeMessageWindow('errorMessages','<li>Error: System returned:' + data + '<br></li>');
			}
			
		});
		
	return false;
}



function loadjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}

