var RS = {bg: {}};
                  
RS.bg.resizeTimer = null;

$(document).ready(function(){
	if (jQuery.inArray($("body").attr("id"),["full"])!=-1 || jQuery.inArray($("body").attr("id"),["full_black"])!=-1){
	  $(window).bind('resize', function() {
	        RS.bg.setBackground();
	  });
	  RS.bg.setBackground();
	}
	
     $('li#archive').hover(function() {
       $(this).addClass('hover');
     }, function() {
       $(this).removeClass('hover');
     });

     $('#archive ul.archiveitems').addClass('hidden');
     $('#archive ul.archivelist li').hover(function() {
       $(this).find('ul.archiveitems').removeClass('hidden');
     }, function() {
       $(this).find('ul.archiveitems').addClass('hidden');
     });

     $('#downloads ul#files li').hover(function() {
       $("#downloads ul#files .image").addClass('hidden');
       $(this).children(".image").removeClass('hidden');
     });  
     
     $('#imagegrid ul#entries li').hover(function() {
       $(this).closest("li").addClass('hover');
     }, function() {
       $(this).closest("li").removeClass('hover');
     });	

     $('a.video_switcher').click(function() {
         
         if (document.getElementById('video_placeholder')) {
             swfobject.removeSWF('video_placeholder');             
         }
         
         if (swfobject.hasFlashPlayerVersion('9')) {
             $('#main_image').css('display', 'none');
             
                var flashvars = {
                    clip_id: this.id.replace(/video_clip_id_/,''),
                    server: 'vimeo.com',
                    show_title: 1,
                    show_byline: 0,
                    show_portrait: 0,
                    color: 'ffffff',
                    fullscreen: 1
                };
                var params = {
                  menu: "false"
                };
                var attributes = {
                };              

             if (!document.getElementById('video_placeholder')) {
                 var video_placeholder = document.createElement('div');
                 video_placeholder.id = 'video_placeholder';
                 document.getElementById('main_image').parentNode.appendChild(video_placeholder);             
             }
             
             swfobject.embedSWF("http://vimeo.com/moogaloop.swf", "video_placeholder", "570", "428", "9.0.0", null, flashvars, params, attributes);
         
             return false;              
         }
        
     });
     
     $('a.image_switcher').click(function() {
         if (document.getElementById('main_image')) {
             var main_image = $('#main_image img')[0];
             
             if (document.getElementById('video_placeholder')) {
                 swfobject.removeSWF('video_placeholder');             
             }             
             $('#main_image').css('display', 'block');
             
             main_image.src = this.href;
             // main_image.width = "";
             // main_image.height = "";
             var thumbs = this.getElementsByTagName('img');
             if (thumbs.length > 0) {
                 $(main_image).attr('title', $(thumbs[0]).attr('title'));
                 $(main_image).attr('alt', $(thumbs[0]).attr('alt'));
             }
             return false;
         }
     });
    
    $("a[href^='http:']").not("[href*='strangecargo.org.uk']").attr('target','_blank');
    
    $("a.share_link").click(function(){
        if (!document.getElementById('share_container')) {
            $('#footer').append('<div id="share_container"></div>');
        }
        $('#share_container').load(this.href+' #share', function() {
            $('#share ul').append('<li class="cancel"><a href="#">Cancel</a></li>');
            $("a[href^='http:']").not("[href*='mydomain.com']").attr('target','_blank');
            $('#share .external a').click(function(){
                $('#share_container').remove();
                return true;
            });
            $('#share .cancel a').click(function(){
                $('#share_container').remove();
                return false;
            });
        })
        return false;
    });
});

RS.bg.setBackground = function () {
  if (null == RS.bg.image) {
    var container = document.createElement('div');
    RS.bg.image = document.createElement('img');
    $(container).css('z-index','-100');
    $(container).css('position','fixed');
    $(container).css('top','0');
    $(container).css('left','0');    
    $(container).css('width','100%');
    $(container).css('height','100%');
    $(RS.bg.image).css('position','absolute');
    $(RS.bg.image).css('top','0');
    $(RS.bg.image).css('left','0');    
    container.appendChild(RS.bg.image);
    document.body.appendChild(container);
  }
  
    var browserwidth = $(window).width();
    var browserheight = $(window).height();
	
  var rep = RS.bg.reps[0];
  
  for (var i=1; i < RS.bg.reps.length-1; i++) {
    if (rep.width < browserwidth || rep.height < browserheight) {
      rep = RS.bg.reps[i];
    }
  }
  RS.bg.image.src = rep.src;

  var ratio = rep.width / rep.height;
  //Resize image to proper ratio
    
  var width, height;
  
  if ((browserheight/browserwidth) < ratio) {
      width = browserwidth;
      height = browserwidth / ratio;
  } else {
      height = browserheight;
      width = browserheight * ratio;
  }
  
  if (width < browserwidth || height < browserheight) {
      if ((browserheight/browserwidth) > ratio) {
          width = browserwidth;
          height = browserwidth / ratio;
      } else {
          height = browserheight;
          width = browserheight * ratio;
      }
  }

  RS.bg.image.width = width;
  RS.bg.image.height = height;
  
  $(RS.bg.image).css('left', (browserwidth - $(RS.bg.image).width())/2);
  $(RS.bg.image).css('top', (browserheight - $(RS.bg.image).height())/2);    
}

