// JavaScript Document
var last_element = "";
var cal;
var selectedDealer = -1;
var dealers = new Array();
var map = null;
var geocoder = null;

function dealer(id,name,phone,address,city,zip) {
	this.id = id;
	this.name = name;
	this.phone = phone;
	this.address = address;
	this.city = city;
	this.zip = zip;
}

function show_element(id) {
	
	if(id == last_element) return 0;
	
	new Effect.Appear($(id));
	
	if(last_element != "") {
		new Effect.Shrink($(last_element));	
	}
	
	last_element = id;
}

function addNews(f) {
 	var queryString = Form.serialize(f);
	var dates = cal.selectedDates;
	if(dates[0] == undefined) {
		alert("You must select a date.");	
	} else {
		queryString += "&date="+dates[0];
	 	new Ajax.Updater('news', 'site_includes/news.php?action=add&ajax=1&edit=1', { postBody: queryString, asynchronous:true, evalScripts:true});
	}
}

function deleteNews(id) {
	if(confirm("Are you sure you want to delete this news?")) {
	 	new Ajax.Updater('news', 'site_includes/news.php?action=delete&ajax=1&edit=1&id='+id, { asynchronous:true, evalScripts:true});
	}
}

function selectDealer(index) {
	if(selectedDealer != -1) {
		$('dealerRow'+selectedDealer).setStyle({ backgroundColor: '#FFFFFF' });
		//Element.hide($('dealerInfoRow'+selectedDealer));
	}
	$('dealerRow'+index).setStyle({ backgroundColor: '#EDF2F7' });
	//Element.show($('dealerInfoRow'+index));

	selectedDealer = index
		
	if(map != null) {
		
	}
	
	if (GBrowserIsCompatible() && map == null) {
		map = new GMap2($("dalerMap"));
		geocoder = new GClientGeocoder();
	}
	

	var address = dealers[index].address + " " + dealers[index].zip;
	var info = "<strong>"+dealers[index].name +"</strong> - " + dealers[index].phone + "<br>"+dealers[index].address+", "+dealers[index].city+" " +dealers[index].zip;
	info += "<hr size='1' noshade='noshade' style='width:auto;'>Get Directions:<br><form style='width:auto;' action=\"http://maps.google.com/maps\" method=\"get\" target=\"_blank\"><label for=\"saddr\">Your Address:</label><input type=\"text\" name=\"saddr\" id=\"saddr\" value=\"\" /><input type=\"submit\" value=\"Go\" /><input type=\"hidden\" name=\"daddr\" value=\"" + address + "\" /><input type=\"hidden\" name=\"hl\" value=\"en\" /></form>";
	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(info);
            }
		}
	);
	
	  new Effect.ScrollTo('dalerMap');
}

//ap = approve/deny
function approveDeny(table,row,id,ap) {
	
	//lets remove the row first
	table.deleteRow(row.sectionRowIndex+1);
	table.deleteRow(row.sectionRowIndex);
	
	var whichDiv = (ap == 1) ? "approved" : "denied";
	
	new Ajax.Updater($(whichDiv), 'site_includes/approvallist.php?ajax=1&action='+whichDiv+'&id='+id);
}