var Explorer = new Class({
    initialize:function(){
        // first up, resize the page
        this.pageResize();
        
        if(window.ActiveXObject && !window.XMLHttpRequest){
            this.flickerFix();
            this.positionFixedEmulation();
            this.hoverSubmit();
        }
        if(window.attachEvent)
            this.fixGoogleAutoFill();
        
    }
    , flickerFix:function(){
        document.execCommand('BackgroundImageCache',false,true);
    }
    , positionFixedEmulation:function(){
        var c= document.styleSheets[0];
        c.addRule('.fixed','top: expression(eval(document.documentElement.scrollTop));');
        c.addRule('body','background-attachment: fixed;');
    }
    , hoverSubmit:function(){
        if(window.ActiveXObject && !window.XMLHttpRequest){
            var i= $S('input');
            $A(i).each(function(el){
                if(el.getProperty('type') == 'button' || 
                   el.getProperty('className') == 'submit' || 
                   el.getProperty('btnClose') == 'submit'){
                    el.addEvent('mouseover',function(){
                        //this.setStyle('backgroundPosition','bottom left');
                        this.addClass('over');
                        //alert(this.getProperty('className'));
                    }
                    );
                    el.addEvent('mouseout',function(){
                        //this.setStyle('backgroundPosition','top left');
                        this.removeClass('over');
                        //alert(this.getProperty('className'));
                    }
                    );
                }
            }
            ,this);
        }
    }
    , fixGoogleAutoFill:function() {
         var items = $S("input");
         $A(items).each(function(el) {
            el.attachEvent("onpropertychange",function(){
               if (event.srcElement.getStyle('backgroundColor') != "")
                  event.srcElement.setStyle('backgroundColor','');
            });
            el.setStyle('backgroundColor','');
         }
         , this);
    }
    
    , pageResize:function() {
      var rht = $('rht');
      var rhtInner = $('rhtInner');
      var lft = $('lft');
      var lftHeight = 0;
      var rhtInnerHeight = 0;
      var rhtHeight = 0;
      if( typeof( lft.innerWidth ) == 'number' ) {
        lftHeight = lft.innerHeight;
        rhtInnerHeight = rhtInner.innerHeight;
        rhtHeight = rht.innerHeight;
      } else if( typeof( lft.clientHeight ) == 'number' ) {
        lftHeight = lft.clientHeight;
        rhtInnerHeight = rhtInner.clientHeight;
        rhtHeight = rht.clientHeight;
      } else if( typeof( lft.offsetHeight ) == 'number' ) {
        lftHeight = lft.offsetHeight;
        rhtInnerHeight = rhtInner.offsetHeight;
        rhtHeight = rht.offsetHeight;
      }
      if (rhtHeight < lftHeight)
         rhtInner.setStyle('height', (lftHeight-75)+'px');

      //alert( 'LHeight = ' + lftHeight + ', RHeight = ' + rhtHeight + ', RIHeight = ' + rhtInnerHeight );
   }
}
);

var Btns = new Class({
    initialize: function(o){
        this.o= o;
        this.btns= o.getElementsByClassName('btn');
        $A(this.btns).each(function(el){
            el.o = el.effect('opacity', {
                duration: 750, transition: Fx.Transitions.Sine.easeInOut
            }
            );
            el.onmouseover= function(){
                var curOpacity = el.getStyle('opacity') == undefined ? 1:Math.round((el.getStyle('opacity')) * 100) / 100;
                this.fade(el,curOpacity,0.8);
            }
            .bind(this);
            el.onfocus= el.onmouseover;
            el.onmouseout= function(){
                if(el.getStyle){
                    var curOpacity = Math.round((el.getStyle('opacity')) * 100) / 100;
                    this.fade(el,curOpacity,1);
                }
            }
            .bind(this);
            el.onclick= function(){
                delete btns;
                delete login;
            }
            .bind(this);
            el.onblur= el.onmouseout;
        }
        ,this);
    }
    , fade: function(el,curOpacity,opacity){
        el.o.clearTimer();
        el.o.custom(curOpacity,opacity);
    }
}
);
var SubNavs = new Class({
    initialize: function(){
        if($('navbar')){
            this.items= $S('#navbar ul.sub-nav a');
            $A(this.items).each(function(el){
                el.o = el.effect('opacity', {
                    duration: 500, transition: Fx.Transitions.Sine.easeInOut
                }
                );
                el.onmouseover= function(){
                    var curOpacity = el.getStyle('opacity') == undefined ? 1:Math.round((el.getStyle('opacity')) * 100) / 100;
                    this.fade(el,curOpacity,0.6);
                }
                .bind(this);
                el.onfocus= el.onmouseover;
                el.onmouseout= function(){
                    if(el.getStyle){
                        var curOpacity = Math.round((el.getStyle('opacity')) * 100) / 100;
                        this.fade(el,curOpacity,1);
                    }
                }
                .bind(this);
                el.onblur= el.onmouseout;
            }
            ,this);
        }
    }
    , fade: function(el,curOpacity,opacity){
        el.o.clearTimer();
        el.o.custom(curOpacity,opacity);
    }
}
);
var Navbar = new Class({
    initialize: function(){
        if($('navbar')){
            this.items= $S('#navbar li a.lnk');
            $A(this.items).each(function(el){
                if(!el.hasClass('ignore')){
                    var o= new Element('div');
                    o.addClass('over');
                    window.ActiveXObject && !window.XMLHttpRequest ? o.setStyle('filter',"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/navbar-lnk-over.png', sizingMethod='image')"):null;
                    if(el.hasClass('dbl')){
                        o.addClass('overDbl');
                        window.ActiveXObject && !window.XMLHttpRequest ? o.setStyle('filter',"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/navbar-lnk-over-dbl.png', sizingMethod='image')"):null;
                    }
                    else if(el.hasClass('tpl')){
                        o.addClass('overTpl');
                        window.ActiveXObject && !window.XMLHttpRequest ? o.setStyle('filter',"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/navbar-lnk-over-tpl.png', sizingMethod='image')"):null;
                    }
                    o.over= o.effects('opacity', {
                        duration: 750, transition: Fx.Transitions.Sine.easeOut
                    }
                    );
                    o.out= o.effects('opacity', {
                        duration: 50, transition: Fx.Transitions.Sine.easeOut
                    }
                    );
                    el.isOver = false;
                    o.onmouseover= function(){
                        el.delay ? $clear(el.delay):null;
                        el.isOver= true;
                    }
                    o.onclick= function(){
                        document.location= el.href;
                    }
                    ;
                    var oo= new Element('div');
                    var classNameAr= el.className.split(' ');
                    for(var x = 0;
                    x < classNameAr.length; x++){
                        var c= classNameAr[x];
                        if(c.indexOf('icon') >= 0){
                            oo.addClass(c);
                        }
                    }
                    oo.setHTML(el.innerHTML);
                    oo.injectInside(o);
                    o.injectInside(el);
                    el.o= o;
                    if(el.hasClass('active')){
                        this.moveOver(el);
                        el.removeAttribute('href');
                        el.o.setStyle('cursor','default');
                    }
                    else{
                        el.onmouseover= function(){
                            this.moveOver(el);
                        }
                        .bind(this);
                        el.onfocus= el.onmouseover;
                        el.onmouseout= function(){
                            this.moveOut(el);
                        }
                        .bind(this);
                        el.onblur= el.onmouseout;
                    }
                }
            }
            ,this);
        }
    }
    , moveOver:function(el){
        if(el.isOver != true){
            el.delay ? $clear(el.delay):null;
            el.isOver= true;
            el.o.out.clearTimer();
            el.o.setStyle('left',190 + 'px');
            el.o.over.move(el.o, 0,-10);
        }
    }
    , moveOut:function(el){
        el.delay= (function(){
            el.o.over.clearTimer();
            el.o.out.move(el.o, 0,190);
            el.isOver = false;
        }
        ).delay(100);
    }
}
);

var Logo = new Class({
    initialize: function(){
        var $logo = 'logo';
        if($($logo)){
            this.items= $S('#logo a');
            $A(this.items).each(function(el){ 
               var o= new Element('div');
               o.setOpacity(0);
               o.addClass('over');
               o.over= o.effect('opacity', {
                  duration: 400, transition: Fx.Transitions.Sine.easeInOut
               });
               o.onclick= function(){
                  document.location= el.href;
               };
   
               o.setHTML(el.innerHTML);
               o.injectInside(el);
               el.o= o;
               el.onmouseover= function(){
                  this.moveOver(el);
               }.bind(this);
               el.onfocus= el.onmouseover;
               el.onmouseout= function(){
                  this.moveOut(el);
               }.bind(this);
               el.onblur= el.onmouseout;
            }
            ,this);
       }
    }
    , fade: function(el,curOpacity,opacity){
        el.o.over.clearTimer();
        el.o.over.custom(curOpacity,opacity);
    }
    , moveOver:function(el){
        if(el.o.getStyle){
           var curOpacity = el.o.getStyle('opacity') == undefined ? 1:Math.round((el.o.getStyle('opacity')) * 100) / 100;
           this.fade(el,curOpacity, 1);
        }
    }
    , moveOut:function(el){
        if(el.o.getStyle){
            var curOpacity = Math.round((el.o.getStyle('opacity')) * 100) / 100;
            this.fade(el, curOpacity, 0);
        }
    }
}
);


var Modal = new Class({
    initialize:function($triggers,$panel,$endObj,$confirm){
        if($($panel)){
            $endObj ? this.endObj = $endObj:null;
            this.p= $($panel).clone().setProperty('id',$($panel).getProperty('id') + '-visible').setStyle('visibility','hidden');
            this.t= $triggers;
            this.m= new Element('div');
            this.m.maxOpacity= 0.83;
            this.c = this.p.getElementsByClassName('btnClose');
            this.pFadeIn= this.p.effect('opacity', {
                duration: 350, transition: Fx.Transitions.Sine.easeInOut
            }
            );
            this.pFadeOut= this.p.effect('opacity', {
                duration: 350, transition: Fx.Transitions.Sine.easeInOut, onComplete: function(){
                    this.mFadeOut.custom(this.m.maxOpacity,0);
                }
                .bind(this)
            }
            );
            this.mFadeOut= this.m.effect('opacity', {
                duration: 200, transition: Fx.Transitions.Sine.easeInOut, onComplete: function(){
                    this.cleanUp();
                }
                .bind(this)
            }
            );
            this.mFadeIn= this.m.effect('opacity', {
                duration: 200, transition: Fx.Transitions.Sine.easeInOut, onComplete: function(){
                    this.showPanel();
                }
                .bind(this)
            }
            );
            if(!$triggers || ($triggers && $triggers.length == 0)){
                this.createModal();
            }
            else{
                $A(this.t).each(function(el){
                    el.setProperty('href','javascript:void(0);');
                    el.addEvent('click',function(){
                        this.createModal()
                    }
                    .bind(this));
                }
                ,this);
            }
            window.addEvent('resize',function(){
                this.centerPanel();
            }
            .bind(this));
            $A(this.c).each(function(el){
                el.addEvent('click',function(){
                  var $close = true;
                  if ($confirm && !confirm($confirm)) $close = false;
                  if ($close == true) this.removeModal();
                }
                .bind(this));
            }
            ,this);
            
            this.m.addEvent('click',function() {
               var $close = true;
               if ($confirm && !confirm($confirm)) $close = false;
               if ($close == true) this.removeModal();
            }
            .bind(this));
        }
    }
    , centerPanel:function(){
        this.p.setStyle('left', (Math.round((document.documentElement.clientWidth * .5) - (this.p.offsetWidth * .5)) + 'px'));
    }
    , showPanel:function(){
        $E('body').adopt(this.p);
        new Btns(this.p);
        this.p.setStyle('display','block').setOpacity(0);
        this.pFadeIn.custom(0,1);
        this.centerPanel();
    }
    , createModal:function(){
        var h= document.documentElement.scrollHeight >
        document.documentElement.offsetHeight ? document.documentElement.scrollHeight:document.documentElement.offsetHeight;
        this.m.setProperty('id','modal').setStyle('height',h + 'px').addClass('modal').setOpacity(0);
        $E('body').adopt(this.m);
        this.mFadeIn.custom(0,this.m.maxOpacity);
    }
    , removeModal:function(){
        this.pFadeOut.custom(1,0);
    }
    , cleanUp:function(){
        this.p.remove();
        this.m.remove();
        /*if(this.endObj){
            eval(this.endObj).modalCleanup();
        }*/
    }
}
);
var ToTop= new Class({
    s:new Fx.Scroll({
        duration: 300, transition: Fx.Transitions.Sine.easeInOut
    }),
    
    initialize:function(){
        var page = $('page');
        var b= page.getElementsByClassName('btnToTop');
        $A(b).each(function(el){
            el.setAttribute('href','javascript:void(0);');
            el.addEvent('click',function(){
                this.ToTop()
            }
            .bind(this));
        }
        ,this);
    }
    , ToTop:function(){
        this.s.scrollTo($('page'));
    }
}
);

var DOM = new Class({
   is_js: true
   , is_dom: false
   , initialize:function() {
      if (document.getElementById && document.getElementsByTagName) {
		   var $page = $('page');//document.getElementsByTagName('body')[0];
		   $page.removeClass('no-js');
		   $page.addClass('has-dom');
		   this.is_dom = true;
		}
   }
   , hasRequired: function() {
      return this.is_dom;
	}
});

var btns, login;
var Dom;
window.addEvent('load',function(){
	 Dom = new DOM();
    btns= new Btns($('page'));
    new ToTop();
    new Explorer();
    new Navbar();
    new SubNavs();
    new Logo();
    var $confirm = "Closing this window will cancel sign up!\n\nClick OK to Cancel sign up.\nClick Cancel to continue with sign up";
    $('sign-up-form') ? sign_up_form_visible = new Modal([$('buddha-bar-sign-up')],'sign-up-form', null, $confirm):null;
    $('load-sign-up') ? sign_up_form_visible.createModal():null;
    $('error-panel') ? new Modal([],'error-panel'):null;
    $('success-panel') ? new Modal([],'success-panel'):null;
}
);