var Global = {
    addEvent : function(obj, type, fn) {
        if (obj.addEventListener) {
            obj.addEventListener(type, fn, false);
        }
        else if (obj.attachEvent) {
            obj["e"+type+fn] = fn;
            obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
            obj.attachEvent("on"+type, obj[type+fn]);
        }
    },
    popup : function (html) {
      window.open(html,'window2','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=450,height=600');
    },
    getElementsByClassName : function(className, tag, elm) {
        var testClass = new RegExp("(^|\s)" + className + "(\s|$)");
        var tag = tag || "*";
        var elm = elm || document;
        var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
        var returnElements = [];
        var current;
        var length = elements.length;
        for(var i=0; i<length; i++){
            current = elements[i];
            if(testClass.test(current.className)){
                returnElements.push(current);
            }
        }
        return returnElements;
    }
}

var btn = {
    init : function() {
        if (!document.getElementById || !document.createElement || !document.appendChild) return false;
        as = Global.getElementsByClassName('btn_(.*)');
        for (i=0; i<as.length; i++) {
            var thisType = "fixed";
            if ( as[i].className.indexOf("btn_flex") != -1 ) {
              thisType = "flex";
            }
            if ( as[i].tagName == "BUTTON" ) {
              if ( navigator.userAgent.indexOf("Safari") != -1 ) {
                var i2 = document.createElement('i');
                while (as[i].firstChild) {
                  i2.appendChild(as[i].firstChild);
                }
                as[i].appendChild(i2);
              }
            } else if ( as[i].tagName == "A" ) {
              var i1 = document.createElement('i');
              while (as[i].firstChild) {
                i1.appendChild(as[i].firstChild);
              }
              as[i].appendChild(i1);
              if ( thisType == "flex" ) {
                var s2 = document.createElement('span');
                as[i].appendChild(s2);
              }
            }
            // Firefox Only
            if ( navigator.userAgent.indexOf("Firefox/") != -1 ) {
              as[i].className+=as[i].className?' btn_firefox':'btn_firefox';
              if ( navigator.userAgent.indexOf("Firefox/3") != -1 ) {
                as[i].className+=as[i].className?' btn_firefox3':'btn_firefox3';
              }
              if ( navigator.userAgent.indexOf("Macintosh") != -1 ) {
                as[i].className+=as[i].className?' btn_firefox_mac':'btn_firefox_mac';
              }
            } else if ( navigator.userAgent.indexOf("AppleWebKit/") != -1 ) {
              as[i].className+=as[i].className?' btn_webkit':'btn_webkit';
            }
        }
    }
}

var oImg = {
    preloadFlag : false,
    init : function() {
        oImg.preloadImages();
        if (!document.getElementById || !document.createElement || !document.appendChild) return false;
        as = Global.getElementsByClassName('png_fix');
        for (i=0; i<as.length; i++) {
          oImg.fixPng(as[i]);
        }
    },
    fixPng: function( img ){
      if( oImg.needPNGFix() ){
        img.parentNode.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=image src='"+ img.src +"')";
        img.src = "/Global/images/spacer.gif";
      } else {
        img.style.visibility = "visible";
      }
    },
    newImage: function (arg) {
      if (document.images) {
        rslt = new Image();
        rslt.src = arg;
        return rslt;
      }
    },
    changeImages : function () {
      if (document.images && (oImg.preloadFlag == true)) {
        for (var i=0; i<oImg.changeImages.arguments.length; i+=2) {
          document[oImg.changeImages.arguments[i]].src = oImg.changeImages.arguments[i+1];
        }
      }
    },
    preloadImages : function () {
      if (document.images) {
        oImg.preloadFlag = true;
      }
    },
    needPNGFix : function () {
      if(document.all && navigator.userAgent.indexOf("MSIE") != -1 ){
        return true;
      }
      return false;
    }
}

var resize = {
    fire : function() {
      if (!document.getElementById || !document.createElement || !document.appendChild) return false;
      if( oImg.needPNGFix() ){
        as = Global.getElementsByClassName('png_fix');
        for (i=0; i<as.length; i++) {
          var cfilter = as[i].parentNode.style.filter;
          as[i].parentNode.style.filter = "none";
          as[i].parentNode.style.filter = cfilter;
        }

        as = Global.getElementsByClassName('btn_(.*)');
        for (i=0; i<as.length; i++) {
          as[i].style.visibility = 'hidden';
          as[i].style.visibility = 'visible';
        }
      }
    }
}

var RentalHomeAdGlow = {
  Over : function(i, glow_img, glow_link) {
    if( oImg.needPNGFix() ){
      document.getElementById(glow_link).style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=image src='" + i + "')";
      document.getElementById(glow_link).style.cursor = "hand";
    } else {
      document.getElementById(glow_img).style.visibility = "visible";
      document.getElementById(glow_img).src = i;
    }
  },
  Out : function(glow_img, glow_link) {
    if( oImg.needPNGFix() ){
      document.getElementById(glow_link).style.filter = "none";
    } else {
      document.getElementById(glow_img).style.visibility = "hidden";
    }
  }
}

var OddesseSlider = new Class({
  initialize: function(options){
    this.options = $extend({
      slidesToDisplay:1,
      useButtons:0,
      unHideButtons:0,
      fxDuration:1000,
      fxTransition: 'back:in:out',
      stage: 'myStage',
      container: 'myList',
      previous: 'myPrevious',
      previousContent: '<A HREF="javascript:;">\&lt;</A>',
      next: 'myNext',
      nextContent: '<A HREF="javascript:;">\&gt;</A>',
      autoSlide: $extend({
        enabled:0,
        slideDelay:1000
      }),
      remote: $extend({
        enabled:0,
        alwaysShow:0,
        name: 'myRemote',
        pages: $extend({
          enabled:0,
          regStyle: 'myPage',
          selStyle: 'mySelPage',
          injectLoc: 'myNextBtn'
        })
      })
    },options || {});	
    this.totIncrement = 0; this.autoSlide = 0; this.remote = 0;
    this.totalSlides = $(this.options.container).childNodes.length;
    this.firstLi = $(this.options.container).firstChild;
    this.LiWidth = $(this.firstLi).getWidth();
    this.LiPaddingLeft = $(this.firstLi).getStyle('paddingLeft').toInt();
    this.LiPaddingRight = $(this.firstLi).getStyle('paddingRight').toInt();
    this.LiMarginRight = $(this.firstLi).getStyle('marginRight').toInt();
    this.stageWidth = (this.LiWidth * this.options.slidesToDisplay) + ((this.LiPaddingLeft + this.LiPaddingRight) * this.options.slidesToDisplay) + (this.LiMarginRight * (this.options.slidesToDisplay - 1));
    $(this.options.stage).setStyle('width', this.stageWidth);
    $(this.options.container).setStyle('width', ( this.stageWidth / this.options.slidesToDisplay ) * this.totalSlides);
    this.increment = this.LiWidth + this.LiPaddingLeft + this.LiPaddingRight + this.LiMarginRight;
    this.maxRightIncrement = this.increment*((this.totalSlides - this.options.slidesToDisplay) * -1);
    this.myFx = new Fx.Tween(this.options.container, {
      property: 'margin-left',
      duration: this.options.fxDuration,
      transition: this.options.fxTransition,
      link: 'cancel'
    });
    this.slideIt = function(direction,jumpTo){
      if($chk(jumpTo)){
        this.totIncrement = jumpTo;
      } else {       
        if(!$defined(direction) || direction=='next'){
          this.totIncrement = (this.totIncrement>this.maxRightIncrement) ? this.totIncrement = this.totIncrement-this.increment : this.totIncrement = 0;
        }else{
          this.totIncrement = (this.totIncrement<0) ? this.totIncrement = this.totIncrement+this.increment : this.maxRightIncrement;
        }
      }
      if(this.options.remote.pages.enabled!=0){ this.updatePageLinks(); }
      this.myFx.start(this.totIncrement);
      return false;
    };
    if(this.options.remote.enabled!=0 && this.totalSlides>this.options.slidesToDisplay) {
      this.remote = 1;
      this.numOfPages = Math.ceil( this.totalSlides / this.options.slidesToDisplay );
      var table = new Element('table', { 'id': this.options.remote.name } );
      var tbody = new Element('tbody'); var row = new Element('tr');
      if(this.options.useButtons!=0){
        if ( $(this.options.previous) ) {
          $(this.options.previous).setStyle('display', 'inline-block');
          var cell = new Element('td', { 'id': this.options.remote.pages.injectLoc+'_prev' } );
          cell.grab($(this.options.previous));
          cell.injectInside(row);
        } else {
          var cell = new Element('td', { 'id': this.options.remote.pages.injectLoc+'_prev', 'html': '<DIV CLASS="'+this.options.remote.buttonStyle+'" ID="'+this.options.previous+'" STYLE="display:inline-block;">'+this.options.previousContent+'</DIV>' });
          cell.injectInside(row);
        }
      }
      if(this.options.remote.pages.enabled!=0){
        for (x=1;x<=this.numOfPages;x++){
          (function(page) {
            var jumpTo = this.increment * (page-1) * -1;
            var item1 = new Element('td', { 'align': 'center', 'valign': 'middle', 'class': this.options.remote.pages.regStyle, 'id': this.options.remote.pages.injectLoc+'_p_'+page, 'html': "<a href='javascript:;'>"+x+'</a>', 'events': { 'click': function(){ this.slideIt('',jumpTo) }.bind(this) } });
            item1.injectInside(row);
          }.bind(this))(x);
        }
      }
      if(this.options.useButtons!=0){
        if ( $(this.options.next) ) {
          $(this.options.next).setStyle('display', 'inline-block');
          var celln = new Element('td', { 'id': this.options.remote.pages.injectLoc+'_next' } );
          celln.grab($(this.options.next));
          celln.injectInside(row);
        } else {
          var celln = new Element('td', { 'id': this.options.remote.pages.injectLoc+'_next', 'html': '<DIV CLASS="'+this.options.remote.buttonStyle+'" ID="'+this.options.next+'" STYLE="display:inline-block;">'+this.options.nextContent+'</DIV>' });
          celln.injectInside(row);
        }
      }
      row.injectInside(tbody); tbody.injectInside(table); table.injectInside(this.options.remote.pages.injectLoc);
      if(this.options.remote.pages.enabled!=0){
        this.updatePageLinks = function(){
          onPage = ( ( this.totIncrement / this.increment ) * -1 ) + 1;
          for (x=1;x<=this.numOfPages;x++) {
            $(this.options.remote.pages.injectLoc+'_p_'+x).setProperty('class', (x==onPage)?this.options.remote.pages.selStyle:this.options.remote.pages.regStyle);
          }
        }.bind(this);
        this.updatePageLinks();
      }
    }
    if(this.options.autoSlide.enabled!=0 && this.totalSlides>this.options.slidesToDisplay){
      this.autoSlide = 1;
      $clear(this.sliding);
      this.sliding = this.slideIt.periodical( this.options.autoSlide.slideDelay, this );
    }
    $(this.options.stage).addEvents({
      'mouseover' : function(){
        $clear(this.sliding);
        if(this.remote!=0 && this.options.remote.alwaysShow==0){
          $clear(this.hidingRemote);
          $(this.options.remote.name).fade('in');
        }
      }.bind(this),
      'mouseleave' : function(){
        if(this.remote!=0 && this.options.remote.alwaysShow==0){ $clear(this.hidingRemote); this.hidingRemote = this.hideRemote.delay(1000); }
        if(this.autoSlide!=0){ $clear(this.sliding); this.sliding = this.slideIt.periodical(this.options.autoSlide.slideDelay, this); }
      }.bind(this)
    });
    if(this.remote!=0){
      $(this.options.remote.name).addEvents({
        'mouseenter' : function(){
          if(this.autoSlide!=0){ $clear(this.sliding); }
          if(this.options.remote.alwaysShow==0){ $clear(this.hidingRemote); }
        }.bind(this)
      });
      if(this.options.remote.alwaysShow==0){
        this.hideRemote = function(){
          $(this.options.remote.name).fade('out');
        }.bind(this);
        $(this.options.remote.name).fade('hide');
        $(this.options.remote.name).setStyle('visibility','visible');
      }
    }
    if(this.options.useButtons!=0 && this.totalSlides>this.options.slidesToDisplay) {
      $(this.options.previous).addEvents({ 
        'click' : function(event){
          return this.slideIt('previous');
        }.bind(this)
      }); 
      $(this.options.next).addEvents({ 
        'click' : function(event){ 
          return this.slideIt('next');
         }.bind(this)
      });
      if(this.options.unHideButtons!=0 && this.totalSlides>this.options.slidesToDisplay){
        $(this.options.previous).setStyle('visibility','visible');
        $(this.options.next).setStyle('visibility','visible');
      }
    }
  }
});

var Header = {
  _glowOrig : undefined,
  _glowEff : undefined,
  _glowId : undefined,
  _glowColUp : undefined,
  _glowColDown : undefined,
  _searchFocus : 0,
  TabGlow : function (eleid, colup, coldown) {
    this._glowId      = eleid;
    this._glowColUp   = colup;
    this._glowColDown = coldown;
    if ( $(this._glowId) ) {
      this._glowOrig = $(this._glowId).getStyle('background-color');
      $(this._glowId).addEvent('mouseout', (function () { $(this._glowId).setStyle('background-color', this._glowOrig); this._tabGlowUp(); }).bind(this) );
      $(this._glowId).addEvent('mouseover', (function () { if ( this._glowEff ) this._glowEff.cancel(); $(this._glowId).setStyle('background-color', this._glowColDown); }).bind(this) );
      this._tabGlowUp();
    }
  },
  _tabGlowUp : function () {
    if ( this._glowEff ) { this._glowEff.cancel(); }
    this._glowEff = new Fx.Morph(this._glowId, {duration: 2000, onComplete: (function () { this._tabGlowDown(); }).bind(this) });
    this._glowEff.start( { 'background-color': this._glowColUp } );
  },
  _tabGlowDown : function () {
    if ( this._glowEff ) { this._glowEff.cancel(); }
    this._glowEff = new Fx.Morph(this._glowId, {duration: 2000, onComplete: (function () { this._tabGlowUp(); }).bind(this) });
    this._glowEff.start( { 'background-color': this._glowOrig } );
  },
  launchVODSS : function (loc, inURL) {
    var lDest = urlVODss;
    if ( inURL ) lDest = inURL;
    var dwindow = window.open(lDest,'oddesseSSWMV','directories=0,location=0,menubar=0,resizable=0,toolbar=0,status=yes,scrollbars=0,width=300,height=125');
    if ( pageTracker && lDest.indexOf("_ppm_demo_play") > -1 ) {
      try {
        console.debug( pageTracker );
        pageTracker._trackEvent('Action', 'Play PPM Demo', loc);
      } catch (err) {
      }
    }
  },
  searchFiller : function (obj, val, foc) {
    this._searchFocus = foc;
    if ( foc && obj.value == val ) {
      obj.value = '';
    } else if ( ( !foc ) && obj.value == '' ) {
      obj.value = val;
    }
  },
  asIfBlank : function (e,btn) {
    e = new Event(e);
    switch (btn) {
      case 's':
        var cfield = $('v_top_search');
        if ( cfield && cfield.value == v_top_search_text ) {
          e.stop();
          window.location.href = as_url;
        }
        break;
      case 't':
        if ( $('studioDrop') && $('studioDrop').value == 0 ) {
          e.stop();
          window.location.href = as_url;
        }
        break;
      case 'p':
        if ( $('actorDrop') && $('actorDrop').value == 0 ) {
          e.stop();
          window.location.href = as_url;
        }
        break;
      default:
        //
    }
  },
  catsIfBlank : function (e) {
    e = new Event(e);
    if ( $('categoryDrop') && $('categoryDrop').value == 0 ) {
      e.stop();
      window.location.href = catdef_url;
    }
  },
  setDefSearchVal : function () {
    var cfield = $('v_top_search');
    if ( ! cfield ) return;
    if ( ! cfield.value && ! this._searchFocus ) {
      this.searchFiller(cfield, v_top_search_text, 0);
    }
  }
}


var OddHover = new Class({
	Implements: Options,
	options: {
		boxTimer         : 200, // how many milliseconds before the box hides after mouseleave5
    yOffset          : 0, // up and down offset in px - accepts negative ints
    xOffset          : 0, // left and right offset in px - accepts negative ints
		oddHoverSuffix   : '_oddhover', //suffix that creates a hover box
		oddHoverClose    : 'oddhover_close', //this class will add "close" click event to element
		lockY   	       : '', // 'top', 'bottom'
		lockX    	       : '' // 'left', 'right'
	},
	initialize: function(options) {
		this.setOptions(options);
		this.pos = [];
		this.smartBoxes = $$('[id$=' + this.options.oddHoverSuffix + ']');
		this.closeElem = $(document.body).getElements('.' + this.options.oddHoverClose);
		this.closeElem.addEvent('click', function(e){
			e.preventDefault();
			this.closeBox();
		}.bind(this)).setStyle('cursor', 'pointer');
		this.smartBoxes.setStyle('display', 'none');
		this.showHideBox();
		this.closeBox();
	},
	showHideBox: function(){
		this.smartBoxes.each(function(item){
			this.getCurrentBox(item);
 
			item.addEvent('mouseleave', (function(){
        this.closeBoxTimer();
      }).bind(this));
			item.addEvent('mouseenter', (function(){
        $clear(this.delay)
      }).bind(this));
			$(this.currentBox).addEvent('mouseleave', (function(){
        $clear(this.sdelay);
        this.closeBoxTimer();
      }).bind(this));
			$(this.currentBox).addEvent('mouseenter', (function(){
      
        this.sdelay = (function () {
          this.getCurrentBox(item);
          $clear(this.delay);
          this.smartBoxes.setStyle('display', 'none');
          item.setStyles({ display: 'block', position: 'absolute' }).setStyle('z-index', '1000000');
   
          this.positioning(item, this.currentBox);
        }).bind(this).delay(this.options.boxTimer);
 
			}).bind(this)).setStyle('cursor', 'pointer');
		}.bind(this));
	},
	closeBoxTimer: function(){
		this.delay = this.closeBox.bind(this).delay(this.options.boxTimer);
	},
	closeBox: function() {
		this.smartBoxes.setStyle('display', 'none');
	},
	positioning: function(currentItem, currentLink){
		this.pos.windowSize = $(window).getSize();
		this.pos.windowScroll = $(window).getScroll();
		this.pos.b = currentItem.getSize();
		this.pos.c = $(currentLink).getCoordinates();

    var scrollbarWidth = 17;

    currentItem.setStyle('left', this.pos.c.right + this.options.xOffset);
    
    if ((this.pos.c.right + this.pos.b.x + this.options.xOffset) > (this.pos.windowSize.x - this.pos.windowScroll.x - scrollbarWidth )){
      currentItem.setStyle('left', this.pos.c.left - this.pos.b.x - this.options.xOffset );
     }

    if ((this.pos.c.top + this.pos.b.y + this.options.yOffset) > (this.pos.windowSize.y + this.pos.windowScroll.y)){
      this.pos.tn = this.pos.windowSize.y - this.pos.b.y - this.options.yOffset + this.pos.windowScroll.y;     
    } else {
      this.pos.tn = this.pos.c.top + this.options.yOffset;
      
      if ( this.pos.tn < this.pos.windowScroll.y ) {
        this.pos.tn = this.pos.windowScroll.y + this.options.yOffset;
      }
      
    }
    
    currentItem.setStyle('top', this.pos.tn);
	},
	getCurrentBox: function(currentItem){
		this.currentBox = currentItem.getProperty('id');
		this.currentBox = this.currentBox.replace('' + this.options.oddHoverSuffix + '', '');
	}
});

window.addEvent('load', function() { oImg.init();} );
window.addEvent('load', function() { btn.init();} );

window.addEvent('domready', function () {
  if ( ! Header ) return;
  Header.setDefSearchVal();
  try {
    if ( typeof(glowEnable) != "undefined" ) {
      if( glowEnable ) Header.TabGlow('top_tab_signup', glowColor1, glowColor2);
    }
  } catch (err) {
  }
  
  if ( $('header_search_but') ) $('header_search_but').addEvent('click', Header.asIfBlank.bindWithEvent(undefined, 's')); 
  if ( $('categoryGoBtn') ) $('categoryGoBtn').addEvent('click', Header.catsIfBlank.bindWithEvent(undefined)); 
  if ( $('studioGoBtn') ) $('studioGoBtn').addEvent('click', Header.asIfBlank.bindWithEvent(undefined, 't')); 
  if ( $('actorGoBtn') ) $('actorGoBtn').addEvent('click', Header.asIfBlank.bindWithEvent(undefined, 'p')); 
});

