/*
 Flag Browser - The Browser class handles the sidebar of the flags page. It
 provides functionality to load urls, set the active flag, as well as show and
 hide dialogs.
 */

var SideDialog=Class.create();SideDialog.prototype={klass:'SideDialog',flag_title:"",flag_description:"",flag_tag_names:"",initialize:function(map){this.map=map;this.div=$('browser');},flags:function(){},load:function(url){this.busy();if(url==="")url=this.getDefaultUrl();new Ajax.Request(url,{method:'get',onFailure:this.errored.bind(this),onComplete:this.loaded.bind(this)});},addFlag:function(){this.prepareBusy();this.load("/flags/geocode");this.map.removeNewestFlag();this.map.enableAddFlagMode();},showDialog:function(url){this.prepareBusy();this.load(url);},hideDialog:function(){showTab('flags');this.load('/flags/geocode');},prepareBusy:function(){this.div.innerHTML="<div id=\"browser_busy\" style=\"display:none;\"><img src=\"/images/spinner-anim.gif\" alt=\"Busy\"/></div>";},busy:function(){busy("browser_busy");},complete:function(){complete("browser_busy");},loaded:function(request){this.div.innerHTML=request.responseText;Behaviour.apply();this.complete();if($("location_received_address")){Field.focus("location_received_address");if(page.state["address"]){$("location_received_address").value=decodeURI(page.state["address"]);delete page.state["address"];}}},errored:function(request){alert("Oops! There was an error while adding the flag. Try again.");}}
var map;function showTab(currentTab){if($('tab_on_discuss')){Element.hide('tab_on_discuss')};if($('discuss')){Element.hide('discuss')};if($('tab_on_flags')){Element.hide('tab_on_flags')};if($('flags')){Element.hide('flags')};if($('tab_on_add_flag')){Element.hide('tab_on_add_flag')};if($('add_flag')){Element.hide('add_flag')};Element.show('tab_on_'+currentTab);Element.show(currentTab);}
var mapViewRules={'#tab_flags':function(element){Event.observe(element,'click',function(e){map.disableAddFlagMode();showTab('flags');});},'#tab_discuss':function(element){Event.observe(element,'click',function(e){map.disableAddFlagMode();showTab('discuss');});},'#tab_add_flag':function(element){Event.observe(element,'click',function(e){showTab('add_flag');map.enableAddFlagMode();if($('location_received_address'))$('location_received_address').focus();if($('location_received_address'))$('location_received_address').value="";});},'#add_submit':function(element){Event.observe(element,'click',function(e){$('flag_add_form').elements['map_id'].value=map.getMap();});},'#add_cancel':function(element){Event.observe(element,'click',function(e){map.removeNewestFlag();map.browser.prepareBusy();map.browser.load('/flags/geocode');Event.stop(e);});},'#geocode_cancel_btn':function(element){Event.observe(element,'click',function(e){map.disableAddFlagMode();showTab('flags');Event.stop(e);});},'#new_topic_link':function(element){Event.observe(element,'click',function(e){$('post_title').value="";$('post_message').value="";Element.toggle('new_topic');Element.toggle('forum_footer');$('post_title').focus();});},'#post_topic_btn':function(element){Event.observe(element,'click',function(e){Element.toggle('new_topic');if($('no_topics')){Element.hide('no_topics');}});},'#post_cancel_btn':function(element){Event.observe(element,'click',function(e){Element.toggle('new_topic');Element.toggle('forum_footer');});},'div.flag':function(element){Event.observe(element,'mouseover',function(e){if(browserName=="Internet Explorer"){Element.addClassName(element,'highlighted_flag');}
if(map.flagIdsToFlags[element.id.split('-')[1]]){map.flagIdsToFlags[element.id.split('-')[1]].mousedOver();}});Event.observe(element,'mouseout',function(e){if(browserName=='Internet Explorer'){Element.removeClassName(element,'highlighted_flag');}
if(map.flagIdsToFlags[element.id.split('-')[1]]){map.flagIdsToFlags[element.id.split('-')[1]].mousedOut();}});Event.observe(element,'click',function(e){if(map.flagIdsToFlags[element.id.split('-')[1]]){map.flagIdsToFlags[element.id.split('-')[1]].load();map.flagIdsToFlags[element.id.split('-')[1]].mousedOut();}else{flag_lat=$(element.id+'-lat').innerHTML;flag_long=$(element.id+'-long').innerHTML;if(flag_lat&&flag_long){map.gmap.setCenter(new GLatLng(flag_lat,flag_long),18);setTimeout("loadFlag('"+element.id.split('-')[1]+"')",4000);}}
window.location="#";});},'#myspace_link':function(element){Event.observe(element,'click',function(e){Element.toggle('allsites');Element.toggle('myspace');});},'#allsites_link':function(element){Event.observe(element,'click',function(e){Element.toggle('allsites');Element.toggle('myspace');});}};Behaviour.register(mapViewRules);Behaviour.addLoadEvent(function(){if(GBrowserIsCompatible()){map=new Map();sideDialog=new SideDialog();map.browser=sideDialog;sideDialog.map=map;map.resizeToFill=false;globalUnload=window.onunload;window.onunload=function(){globalUnload();GUnload();}
var id=location.href.split('/')[4];map.setMap(mapId);}else{Map.prototype.resized();Map.prototype.complete();Element.show("map");$("map_search_tip").innerHTML="You must have a browser capable of displaying Google Maps to use Flagr.";Element.show("map_search_tip");}});