var HoverBehavior = Class.create();
HoverBehavior.prototype = {
  initialize: function() {
    $A(document.styleSheets).each( function(stylesheet) {
      $A(stylesheet.rules).each( function(rule) {
        if( rule.selectorText.match(/:hover/i) ) {
          stylesheet.addRule( rule.selectorText.replace(/:hover/ig, '.hover'), rule.style.cssText );
        }
      });
    });

    $A(arguments).each( function(arg) {
      $$(arg).each( function(tag) {
        Event.observe(tag, 'mouseover', function() { Element.addClassName(tag, 'hover'); });
        Event.observe(tag, 'mouseout', function() { Element.removeClassName(tag, 'hover'); });
      });
    });
  }
};

function resetFields(whichform) {
  for (var i=0; i<whichform.elements.length; i++) {
    var element = whichform.elements[i];
    if (element.type == "submit") continue;
    if (!element.defaultValue) continue;
    element.style.color = "grey";
    element.onfocus = function() {
      this.style.color = "#000000";
      if (this.value == this.defaultValue) {
        this.value = "";
      }
    }
    
    element.onblur = function() {
      this.style.color = "grey";
      if (this.value == "") {
        this.value = this.defaultValue;
      }
    }
  }
}

function prepareForms() {
  for (var i=0; i<document.forms.length; i++) {
    var thisform = document.forms[i];
    resetFields(thisform);
  }
}

function slideshowNavigation() {
  var slideshow_navigation = $('slideshow_navigation');
  if (!slideshow_navigation) return;
  var links = slideshow_navigation.getElementsByTagName('a');
  for (var i=0; i<links.length; i++) {
    var current_link = links[i];
    Event.observe(current_link, 'mouseover', function() {
      $('animation').style.backgroundImage = "url(" + this.href + ")";
      $('caption_text').innerHTML = this.title;
      var active_links = $$('#slideshow_navigation .active');
      for (var i=0; i<active_links.length; i++) {
        active_links[i].removeClassName('active');
      }
      this.addClassName('active');
    });
    Event.observe(current_link, 'click', function(e) {
      Event.stop(e);
    });
  }
}

var activeLink = function() {
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i<links.length; i++) {
    var linkurl = links[i].getAttribute("href");
    var currenturl = document.location.href;
    if (currenturl.indexOf(linkurl) != -1) {
      Element.addClassName(links[i], 'active');
    }
  }
}

var propertyLinks = function() {
  var description_link = $('description_link');
  var description_list_item = $('description_list_item');
  var details_link = $('details_link');
  var details_list_item = $('details_list_item');
  if (!description_link) return;
  if (!details_link) return;
  
  //hide details
  description_list_item.hide();
  $('details').hide();
  
  //show details on click
  Event.observe(details_link, 'click', function(e) {
    $('description').hide();
    $('details').show();
    details_list_item.hide();
    description_list_item.show();
    Event.stop(e);
  });
  
  //show description on click
  Event.observe(description_link, 'click', function(e) {
    $('details').hide();
    $('description').show();
    description_list_item.hide();
    details_list_item.show();
    Event.stop(e);
  });
}


Event.observe(window, 'load', prepareForms);
Event.observe(window, 'load', slideshowNavigation);
Event.observe(window, 'load', activeLink);
Event.observe(window, 'load', propertyLinks);
Event.observe(window, 'load', function() {
  new HoverBehavior('#navigation', '#navigation li');
});



var validation_labels = [];
validation_labels["comment[author]"] = "Name";
validation_labels["comment[body]"] = "Comment";

Event.observe(window, 'load', function() {
 var form = $('comment_form');
 if (form) new Validation(form);
});




// Firebug
var activateFirebug = function() {
  document.body.setAttribute('debug', 'true');

  //include javascript
  var headID = document.getElementsByTagName("head")[0];         
  var firebugScript = document.createElement('script');
  firebugScript.type = 'text/javascript';
  firebugScript.src = '/theme/javascripts/firebug.js';
  headID.appendChild(firebugScript);
  
}
//Event.observe(window, 'load', activateFirebug);