window.addEvent('domready', function() {
    page.init();
});

window.addEvent('resize', function() {
    page.resize();
});


var page = {

  init : function(){  
    page.resize();      
    gallery.resize();
    menu.create();  
    page.preload();
  },  
  
  
  resize : function(){
    var wSize = window.getSize();     

    // Push footer to the bottom
    var footer = $('footer');    
    var fPadTop = wSize.y - (119 + footer.getPosition().y);   
    fPadTop = fPadTop > -1 ? fPadTop : 0;    
    footer.setStyle('padding-top', fPadTop);
    
    // Clip the right tracks    
    var trackRight = $('trackRight');      
    trackRight.setStyle('display', 'block');
    var trSize = 618 - (trackRight.getPosition().x + 619 - wSize.x);
    trSize = trSize < 368 ? 368 : trSize;      
    trackRight.setStyle('width', trSize);
    
    // Fix horizontal scrollbar bug in Safari, related to the #trackRight clipping
    if(Browser.Engine.webkit){      
      if(wSize.x > 1000) 
        document.body.setStyle("overflow-x", "hidden");
      else  
        document.body.setStyle("overflow-x", "auto");
    }

  },
  
  preload : function(){
    new Asset.images(
      'img/bg/menu/gallery_on.png',
      'img/bg/menu/about_on.png',
      'img/bg/menu/links_on.png',
      'img/bg/menu/gallery_on_ie6.png',
      'img/bg/menu/about_on_ie6.png',
	  'img/bg/menu/links_on_ie6.png'
    );
  }
  
}

var gallery = {

  // Resizes the artwork container on gallery pages
  resize : function(){
    var artwork = $('artwork');
    var img = $('img');
    if(artwork && img){
    
      var sImg = img.getSize()
      artwork.setStyle('width', sImg.x);
      
      var sWidth = sImg.x + (sImg.x > sImg.y ? 25 : 21);
      var sHeight = sImg.y + (sImg.x > sImg.y ? 21 : 25);
      
      // Create the dropshadow
      // for IE6
      var shadow = null;
      if(Browser.Engine.trident4) 
        shadow = new Element('div', {
          'class': 'shadow',
          'styles': {
            'width': sWidth,
            'height': sHeight
          }
        });
      
      // for everyone else
      else
        shadow = new Element('img', {
          'src': 'img/bg/shadow_artwork.png',
          'alt': 'image shadow',
          'class': 'shadow',
          'styles': {
            'width': sWidth,
            'height': sHeight
          }
        });
      shadow.inject(artwork);
    }
  }
  
}

var menu = {

  levels : 2,
  
  create : function() {    
    
   	var isIE6 = Browser.Engine.trident4;
   	var isIE7 = Browser.Engine.trident && navigator.userAgent.toLowerCase().indexOf('msie 7') != -1;
   	var isIE8 = Browser.Engine.trident && navigator.userAgent.toLowerCase().indexOf('msie 8') != -1; 
   	var doFlickerFix = isIE6 || isIE7 || isIE8;
    
    // Menus
    for(var j = 1; j <= this.levels; j++){    
      var menu = $$('li.menu' + j);    
      for(var i = 0; i < menu.length; i++){
        var item = menu[i];
      
        item.addEvents({
            'mouseover' : function(event) {  
            this.setStyle('position', 'relative');
            this.setStyle('z-index', '50');
            this.getFirst('ul').setStyle('display', 'block');  
            this.addClass('active');   
            
            
          },      
            'mouseout' : function(event) { 
            this.setStyle('position', 'static');
            this.getFirst('ul').setStyle('display', 'none');              
            this.removeClass('active');  
            
            // Must do this to prevent flicker in IE6 
			if(doFlickerFix){
              var pMenu = this.getParent("li.menu1")
              if(pMenu && event) {
                event.stopPropagation();
                pMenu.fireEvent('mouseout');
              }
			}
          }  
        });            
      }
    }    
  }  
}
