insertFlashBanner = function(json_slide_file) {

  $('main-content').insert({ top:'<div id="slides"></div>'});  

  var so = new SWFObject("/flash/company_christmas_banner.swf", "slides-flash", "632", "274", "9", "#FFFFFF");
  so.addParam("wmode", "transparent");
  so.addVariable("json", json_slide_file);

  so.write("slides");

};

$(document).observe("dom:loaded", function() {
  if (document.body.id == 'pages' && document.body.className == 'display' ) {
    insertFlashBanner('../slides/index.json');
  }
  
  
   if ($("flashMessage")) {
     if (!$("flashMessage").empty()) {
       setTimeout(function() {Effect.Fade("flashMessage");Effect.BlindUp("flashMessage");}, 5000);
     }
   }
});

Event.observe(window, 'load', function() {
  hotel_table = $('hotels');
  
  if (hotel_table) {
    hotel_hover_html = '<div></div><img src="/img/hotels_icon.gif" alt="" />';
    hotel_table.up().insert({before: '<div id="hotel-hover">'+hotel_hover_html+'</div>'});
    var hotel_hover = $('hotel-hover');
    hotel_table.observe('mouseout',function(){hotel_hover.update(hotel_hover_html);});    

    $$("#hotels tr.hotel").each(function(el,index) {
      if (hotels[index]['Image']) {
        el.observe('mouseover', function() {
          hotel_hover.update('<div></div>' + hotels[index]['Image']);    
        });
      }
      el.observe('click', function() {
        showHotel(el.id.split(':').last());    
      });

    });
    
    
  }
  
  // Load the venue map
  if (typeof venue_lat != 'undefined' && typeof venue_lng != 'undefined' && 
  	!(isNaN(venue_lat) || isNaN(venue_lng))) {

      var script = document.createElement('script');
      script.setAttribute('src', 'http://maps.google.com/maps?file=api&v=2&key=' + GOOGLE_API_KEY + '&async=2&callback=hotelInit');
      script.setAttribute('type', 'text/javascript');
      document.documentElement.firstChild.appendChild(script);
  	  

  }
});

var hotelInit = function(){
	  if(GBrowserIsCompatible()){
      $('accommodation').insert({bottom: '<div id="map" style="padding: 0 4px; height: 300px"></div>'});
      the_map = $('map');
      var gmap = new GMap2(the_map);
      gmap.setCenter( new GLatLng(venue_lat,venue_lng), 12 );
      var venue_marker = new GMarker(new GLatLng(venue_lat,venue_lng), new GIcon(G_DEFAULT_ICON,'http://www.google.com/mapfiles/markerA.png'));
      gmap.addOverlay(venue_marker);
      GEvent.addListener(venue_marker, "click", function() {
         venue_marker.openInfoWindowHtml('The Venue');
      });
      
      if (typeof hotels != 'undefined') {
        hotel_letter = 'A';
        hotels.each(function(hotel) {
          hotel_letter = hotel_letter.succ();
          var marker = new GMarker(new GLatLng(hotel['Hotel']['lat'],hotel['Hotel']['lng']), new GIcon(G_DEFAULT_ICON,'http://www.google.com/mapfiles/marker'+hotel_letter+'.png'));
          GEvent.addListener(marker, "click", function() {
             showHotel(hotel['Hotel']['id']);
          });
          gmap.addOverlay(marker);
        });  
      }
    }
}
/*
// Round the corners (rounded only after window load so they are in the right position)
Element.observe(window, "load", function() {
  var collection = $$(".parties dd.image img, .post img, #news-images img, body#parties.view .main-information img, body#invite .main-information img, #party-images img, .get-creative dd.image img");
  var array_pointer = 0;
  
  // Do it every 0.1 seconds on pages with lots of images to stop IE getting tired
  new PeriodicalExecuter(function(pe){
											  	if (array_pointer >= collection.length ) {
											  	  pe.stop();
											  	} else {
  											  	collection[array_pointer].roundTheCorner(7);
  											  	array_pointer++; 
											  	}
											  }, 0.1);
  $$("#news-archive, #latest-news").invoke('roundTheCorner', 10);
});
*/

showHotel = function(id) {
  hotel_div = $('hotel');
  if (hotel_div && hotel_div.current == id) {
    new Effect.toggle(hotel_div,'blind');
  } else {
    new Ajax.Request('/hotels/view/' + id, { onComplete: function(response) {
      if (hotel_div) {
        hotel_div.current = id;
        new Effect.BlindUp(hotel_div, { afterFinish: function() { hotel_div.update(response.responseText); new Effect.BlindDown(hotel_div, { afterFinish: function() { $$('#hotel img').invoke('roundTheCorner', 7); } });  },  queue: 'end'});
      } else {
        $('map').insert({before: '<div id="hotel" style="display: none">'+response.responseText+'</div>'});
        new Effect.BlindDown('hotel', { afterFinish: function() { $$('#hotel img').invoke('roundTheCorner', 7); } } );
        $('hotel').current = id;
      }

    } });    
  }

  return false;
};

addBookmark = function(id) {
  if (!$('bookmarks')) {
    $$('div.supporting-content').first().insert({top: '<div id="bookmarks"></div>'});
  }
  new Ajax.Request('/parties/add_bookmark/' + id, { onSuccess: replaceBookmarks });
  return false;
};

deleteBookmark = function(id) {
  new Ajax.Request('/parties/delete_bookmark/' + id, { onSuccess: replaceBookmarks } );
  return false;
};

replaceBookmarks = function(response) {
  $('bookmarks').replace(response.responseText);
};

insertAtCursor = function(myField, myValue) {
  //IE support
  if (document.selection) {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
  }
  //MOZILLA/NETSCAPE support
  else if (myField.selectionStart || myField.selectionStart == '0') {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos)
                  + myValue
                  + myField.value.substring(endPos, myField.value.length);
  } else {
    myField.value += myValue;
  }
};
   

function inputDefaultValue(field){
	if(field.value == field.defaultValue){
		field.value = '';
	}else if(field.value == ''){
		field.value = field.defaultValue;
	}
}
