var Engine = {
  detect: function() {
    var UA = navigator.userAgent;
    this.isKHTML = /Konqueror|Safari|KHTML/.test(UA);
    this.isGecko = (/Gecko/.test(UA) && !this.isKHTML);
    this.isOpera = /Opera/.test(UA);
    this.isMSIE  = (/MSIE/.test(UA) && !this.isOpera);
    this.isMSIE7 = this.isMSIE && !(/MSIE 6\./.test(UA) && !this.isOpera);
    this.isMSIE6 = this.isMSIE && !this.isMSIE7;
  }
}
Engine.detect();
Position.getPageSize = function() {
  var xScroll, yScroll;

  if (window.scrollMaxX) {  
    xScroll = window.innerWidth  + window.scrollMaxX;
    yScroll = window.innerHeight + window.scrollMaxY;
  } else {
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
  } 
  
  var windowWidth, windowHeight;
  if (self.innerHeight) { // all except Explorer
    windowWidth = self.innerWidth;
    windowHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }
  // for small pages with total height less then height of the viewport
  pageHeight = Math.max(windowHeight, yScroll);

  // for small pages with total width less then width of the viewport
  pageWidth = Math.max(windowWidth, xScroll);

  return { page: { width: pageWidth, height: pageHeight }, window: { width: windowWidth, height: windowHeight } };
}
Position.scrollX = function(){
  return (window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0);
}

Effect.HScrollTo = Class.create();
Object.extend(Object.extend(Effect.HScrollTo.prototype, Effect.Base.prototype), {
  initialize: function(scrollEnd) {
    this.scrollStart = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
    
    var delta = scrollEnd - this.scrollStart;
    var w = Position.getPageSize();
    if(this.scrollStart + delta < 0) delta = -this.scrollStart;
    if(this.scrollStart + delta > (w.page.width-w.window.width)) delta = (w.page.width-w.window.width) - this.scrollStart;
    
    this.delta   = delta;
    
    this.start(arguments[1] || {});
  },
  update: function(position) {
    Position.prepare();
    window.scrollTo(this.scrollStart + (position*this.delta), 0); 
  }
});

Effect.MoveRight = Class.create();
Object.extend(Object.extend(Effect.MoveRight.prototype, Effect.Base.prototype), {
  initialize: function(element, delta) {
    this.element = $(element);
    if(!this.element) throw(Effect._elementDoesNotExistError);
    var options = Object.extend({
      x:    delta
    }, arguments[2] || {});
    this.start(options);
  },
  setup: function() {
    this.originalRight = this.options.initialRight || parseFloat(this.element.getStyle('right') || '0');
  },
  update: function(position) {
    this.element.setStyle({
      right: Math.round(this.options.x  * position + this.originalRight) + 'px'
    });
  }
});


Effect.ScaleMoveRelative = Class.create();
Object.extend(Object.extend(Effect.ScaleMoveRelative.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    if(!this.element) throw(Effect._elementDoesNotExistError);
    var options = Object.extend({
      scaleFrom: [0, 0],
      scaleBy: [1, 1],
      moveBy: [0, 0]
    }, arguments[1] || {});
    this.start(options);
  },
  setup: function() {
    this.scale = [
      (this.options.scaleFrom[0]*this.options.scaleBy[0]) - this.options.scaleFrom[0],
      (this.options.scaleFrom[1]*this.options.scaleBy[1]) - this.options.scaleFrom[1]
    ];
    this.scaleTo = [
      this.options.scaleFrom[0] + this.scale[0],
      this.options.scaleFrom[0] + this.scale[1]
    ];
  },
  update: function(position) {
    style = $H({
      width:  Math.round(this.options.scaleFrom[0] + (this.scale[0] * position)),
      height: Math.round(this.options.scaleFrom[1] + (this.scale[1] * position))
    });
    style.left = 
      Math.round(((this.element.deltax || 0) + (this.options.moveBy[0] * position)) * (style.width/this.options.scaleFrom[0])) + 'px';
    style.top = 
      Math.round(((this.element.deltay || 0) + (this.options.moveBy[1] * position)) * this.options.scaleBy[1]) + 'px',
    //console.log('@'+ position + ': ' + style.inspect());
   
    this.element.setStyle({
      left: style.left,
      top: style.top,
      width: style.width + 'px',
      height: style.height + 'px'
    });
  }
});



Effect.HScroll = Class.create();
 Object.extend(Object.extend(Effect.HScroll.prototype, Effect.Base.prototype), {
   initialize: function(delta) {
     this.scrollStart = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
     
     var w = Position.getPageSize();
     if(this.scrollStart + delta < 0) delta = -this.scrollStart;
     if(this.scrollStart + delta > (w.page.width-w.window.width)) delta = (w.page.width-w.window.width) - this.scrollStart;
     
     this.delta   = delta;
     
     this.start(arguments[1] || {});
   },
   update: function(position) {
     Position.prepare();
     window.scrollTo(this.scrollStart + (position*this.delta), 0); 
   }
 });

function getServerName() {
  var href = document.location.href;
  var rExp = /^https*:\/\//;
  var rExp2 = /\/.*$/;
  var href2 = href.replace(rExp, '')
  var href3 = href2.replace(rExp2, '')
  return href3;
}

function getSecureProtocol(serverName) {
  if(serverName.substring(0,4)=='www.') {
    return "https://";
  }
  else if(serverName.substring(0,6)=='stage.') {
    return "https://";
  }
  else {
    return "http://";
  }
}



function getSite() {
  var href = document.location.href;
  var rExp = /^https*:\/\/[^/]+\/([^/]+)\/.*$/
  var site = href.replace(rExp, '$1');
  return site;
}
function getSecureBaseURL() {
  var serverName = getServerName();
  var protocol = getSecureProtocol(serverName);
  var site = getSite();
  var ur = protocol+serverName+'/'+site;
  return ur;
}

var Animal = Class.create();
Animal.prototype = {
initialize: function(name, sound) {
this.name = name;
this.sound = sound;
},
speak: function() {
alert(this.name + " says: " + this.sound + "!");
}
};

var Dog = Class.create();
Dog.prototype = Object.extend(new Animal(), {
initialize: function(name) {
this.name = name;
this.sound = "woof";
}
});
