var coords = new Array();

$(document).ready(function() {

  var IE6 = navigator.userAgent.indexOf('MSIE 6')!==-1;
  var itemShadow;
  
  $('#menu').append(itemShadow = $('<div class="item-shadow"></div>').hide());

  /**
   * Set menu listeners
   */   
  $('#menu>li').mouseover(function() {
    $(this).addClass('selected');
    itemShadow.css({left: this.offsetLeft + $(this).width() - 1}).show();;
    if(IE6)
      IEPNGFix.update();
  }).mouseout(function(){
    $(this).removeClass('selected');
    itemShadow.hide();
  });
  
  /**
   * Drop shadows
   * position of shadows now hadrcoded in CSS
   * See .shadow for details
   */         
  $('.dropshadow').each(function(e){
    var elem=$(this).children().eq(0);
    var clone=elem.clone();
    elem.addClass('shadow').before(clone.addClass('shadowed'));
  });
  
  /**
   * Wrap divs around submenu
   */     
  $('#menu ul').wrap("<div class='l'><div class='r'></div></div>");

  /**
   * Replace splash screen icons in IE6
   */
  if(IE6)
  {
    $('.splash .button .icon').each(function(e){
      var loc = $(this).css('background-image').replace('url("','').replace('")','').replace("http://"+location.host,''); 
      $(this).css({'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+loc+'", sizingMethod="crop")',
                   'background-image': 'none'});
    });
  }
  /**
   * Resize background shadow in non-IE6 browsers
   */     
  else
  {
    if($('#shade').width()<$('#container').width())
      $('#shade').width($('#container').width());
    if($(window).height()>$('body').height()) 
      $('#shade').height($(window).height());   
  }
  
  /**
   * Bind event to close button on screenshow window
   */     
  $('#view .close').each(function() {
    $(this).click(function(){
      $('#view').hide();
      $('#fog').hide();
    });
  });
  
  /**
   * Tabs
   * Hide all. Prepare first one   
   */
  $('.tabcontent').hide();
  var first;
  var current=(first=$('.tabs a').eq(0));
  first.parents('li').eq(0).addClass('current');
  $('#'+current.attr('id')+'_content').show();
   
  /**
   * Bind event listener to each tab
   */     
  $('.tabs a').each(function(e){
    $(this).click(function(){
      $(current).parents('li').eq(0).removeClass('current');
      $('#'+current.attr('id')+'_content').hide();
      (current=$(this)).parents('li').eq(0).addClass('current');
      $('#'+current.attr('id')+'_content').show();
      return false;           
    });
  });   
  
  /** 
   * Sliding headers
   */
  $('.slide_heading').click(function(){
    ($(this)).addClass('open').toggleClass('closed');
    $('#'+this.id+'_content').toggle();
  });            
  
  /**
   * Gallery
   * Bind events   
   */
  $('.gallery a').each(function(e){
    $(this).click(function(){
      showGallery(this);
      return false;
    });
  });     
  
  /**
   * Gallery
   * Show a single image, create paging, bind events
   */        
  function showGallery(el)
  {
    var bw, bh, vpH = $('html').attr('clientHeight')-120;
    var tmpI = new Image();
    tmpI.src = el.href;
    
    /**
     * Adjust fog and view window
     */         
    $('#fog').width(bw=$('body').width()).height(bh=$('body').height()).show();
    $('#view').css('left', bw/2-$('#view').width()/2);
  
    $('#view .img img').load(function(){  
      var iW = tmpI.width, iH = tmpI.height;
      if(iH>vpH)
        $(this).css({height: vpH, width: iW/iH*vpH});
      $('#view').show(); 
    }).attr('src',el.href);
    
    $('#view .yetanotherclass').html('<strong>'+$(el).children('img').eq(0).attr('alt').replace('|','</strong><br/>'));
    var cnt = 1;
    var current;
    var currentPagingA;
    
    $('#view .paging').empty();
    (current=$(el)).parent('.gallery').eq(0).children('a').each(function(idx){
      var dstImg = this.href;
      var title = '<strong>'+$(this).children('img').eq(0).attr('alt').replace('|','</strong><br/>');
      var alt = $(this).children('img').eq(0).attr('alt');
      newe = $('<a href="#" title="'+alt+'">'+(cnt++)+'</a>');
      if($(this).attr('href')==current.attr('href'))
      {
        newe.addClass('current');
        currentPagingA=newe;
      }
      newe.click(function(){        
        currentPagingA.removeClass('current');
        (currentPagingA=$(this)).addClass('current');
        $('#view .img img').attr('src', dstImg);
        $('#view .yetanotherclass').html(title);
        return false;
      }); 
      $('#view .paging').append(idx!=0?'&nbsp;/&nbsp;':'').append(newe);
    }); 
  }
  
  if(IE6)
    $(window).scroll(function(ev){
      var scroll = getScrollOffset();
      $('#view').animate({top: scroll.top+10}, 10)  
    });
  
  $(window).resize(function(){
    var bw = $('body').width();
    var cw = $('#container').width();
    if(!IE6)
      $('#shade').width(bw>cw?bw:cw);
    placeObjects();
    if($(window).height()>$('body').height()) 
      $('#shade').height($(window).height());   
  });
  
});

function placeObjects(offsets)
{
  var bw = $('body').width();
  var cw = $('#container').width();
  var scroll = getScrollOffset();
  $('#view').css({left: $('body').width()/2-$('#view').width()/2+scroll.left});
  $('#fog').width(bw>cw?bw:cw).height($('body').height());
}

function getScrollOffset() 
{
  return { 
    left: window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
    top: window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop 
  };
}