/* fadeSlide effect start */
var FadeSlide = function(element, options) {

    Spry.Effect.Cluster.call(this, options);

    var duration = 1000;
    var from = '0%';
    var to = '100%';
    var transition = Spry.fifthTransition;
    var toggle = true;
    var finishFunc;

    if (options) {
    	if (options.duration != null) duration = options.duration;
    	if (options.from != null) from = options.from;
    	if (options.to != null) to = options.to;
    	if (options.transition != null) transition = options.transition;
    	if (options.toggle != null) toggle = options.toggle;
        if (options.finishFunc != null) finishFunc = options.finishFunc;
    }

    var slide_effect = new Spry.Effect.Slide(element, {
        duration: 1000,
        from: from,
        to: to,
        toggle: true,
        finish: finishFunc
    });
    var fade_effect = new Spry.Effect.Fade(element, {
        duration: 1000,
        from: from,
        to: to,
        toggle: true
    });

    this.addParallelEffect(slide_effect);
    this.addParallelEffect(fade_effect);
};
FadeSlide.prototype = new Spry.Effect.Cluster();
FadeSlide.prototype.constructor = FadeSlide;

/* fadeSlide effect end */

/* regionFilter start */

/* t-pont kereso flash callback  */
function setRegion(regionID) {
    selectRegion(regionID);
}
/* t-pont kereso flash callback */

/**Cities which belong to the given regio*/
matchCities = null;
/**Option object*/
selectNode = null;
/**The ID of the selected redion*/
selectedRegionID=null;

/**Select a a regio, and fill combobox with its cities*/
function selectRegion(regionID) {

    selectedRegionID = regionID;
    matchCities = cityHash.get(regionID);
    //convert TO DB numbering 
    previousRegionID=$('searchForm:selectedRegion').value;
    $('searchForm:selectedRegion').value=selectedRegionID;
    //deleted previous selection
    if (selectedRegionID!=previousRegionID){
     $('searchForm:selectedCity').value="";
    }
    selectNode = document.getElementById("selectCityOption");
    
    var options = selectNode.getElementsByTagName('option');
    
    options = $A(options);
    setRegionOptions();
    
    displayCombo();
     
}

/**Display the combobox*/
 function displayCombo(){
     $('selectCityOption').setStyle({display: 'block'});
     $('searchForm:selectedCityOptionLabel').setStyle({display: 'block'});
 }
 
/**Set the selected city name to the hidden input text*/
function  getSelectedElement(){
    var selectedIndex = $('selectCityOption').selectedIndex;
    var selectedElement = $('selectCityOption').options[selectedIndex];
    $('searchForm:selectedCity').value=selectedElement.text;
    
}
/**Set the selected city name to the option text*/
function setSelectedElement(){

    var selectedElement=$('searchForm:selectedCity').value;
    var cityOptionCombo = $('selectCityOption');
    for(i = 0;i<cityOptionCombo.options.length;  i++){
      if (cityOptionCombo.options[i].text == selectedElement){
        cityOptionCombo.selectedIndex=i;
	break;
         }
      }
}

/**Fill combox with cities*/
 function setRegionOptions(){
 
 /** removes items */ 
 for(i = $('selectCityOption').options.length-1; i>=0;  i--){
      $('selectCityOption').options[i] = null;
   }
   
    /** builds new items  */
       for (i = 0; i < matchCities.length; i++) {
          var optn = document.createElement("OPTION");
	  var text = html_entity_decode(matchCities[i]);
          optn.value = text;
	  optn.text =  text;
	  //optn.setAttribute();
          $('selectCityOption').options.add(optn);
       }
     }

/**Decodes HTML entites*/     
function html_entity_decode(s) {
    var t = new Element('textarea',{'id':'temp'});
    t.innerHTML = s;
    var v = t.value;
    t = null;
    return v;
}

/* regionFilter end */


/* googleMap start */

 var directions;
 var directionsPanel;
     
function initialize() {
    if ($('map_canvas')==null){
    return;
    }
    
    var map = new GMap2($('map_canvas'));
//    directionsPanel = document.getElementById("directions");
     
//    initalizeDirection(map);
    addControls(map);
    var latitude = $('latitude').value;
    var longitude = $('longitude').value;
    map.setCenter(new GLatLng(latitude, longitude), 15);
    map.setMapType(G_NORMAL_MAP);
    addMarkerPoint(map,latitude,longitude);
}
//Initalize the direction definer
function initalizeDirection(map){
    directions = new GDirections(map, directionsPanel);
    GEvent.addListener(directions, "error", handleErrors);
}

     
// bind a search control to the map, suppress result list
function searchBar(map){
    map.addControl(new google.maps.LocalSearch(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,20)));
} 
     
//Add necessary controls
function addControls(map){
    var mapTypeControl = new GMapTypeControl();
    map.addControl(mapTypeControl);
    var mapLargeControl = new GLargeMapControl();
    map.addControl(mapLargeControl);
}

//Add marker points
function addMarkerPoint(map,latitude,longitude){
    var point = new GLatLng(latitude, longitude);
    var marker = new GMarker(point, {draggable: false});

    map.addOverlay(marker);
}
     
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_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.");
     
}
/* googleMap end */
