(function($) { 
	$.fn.animBg = function(command,options) { 
		if(command && typeof command ==='string'){
			clearInterval($(this).data('anim'));
			return this;
		}
		var $this = $(this),
		o = {'interval':100},
		options = (typeof command === 'object' && typeof options === 'undefined')?command:options,
		o = jQuery.extend({}, o, options), anim;
		if(!o.step && !o.steps){return this;}
		anim = setInterval(function() { 
			var bgpos = parseInt( $this.css('background-position') );
			$this.css('background-position',(-(o.step*o.steps)>=(bgpos-o.step)?0:bgpos-o.step)+'px 0');
		}, o.interval);
		$this.data('anim',anim);
		return this; 
	}
})(jQuery);


var $overlay_wrapper;
var $overlay_panel;

function show_overlay() {
    if ( !$overlay_wrapper ) append_overlay();
    $overlay_wrapper.fadeIn(100);
}

function hide_overlay() {
    $overlay_wrapper.fadeOut(100);
}

function append_overlay() {
    $overlay_wrapper = $('<div id="overlay"></div>').appendTo( $('BODY') );
    $overlay_panel = $('<div id="loader"></div>').appendTo( $overlay_wrapper );

    $overlay_panel.html();

    attach_overlay_events();
}

function attach_overlay_events() {
    $('A.hide-overlay', $overlay_wrapper).click( function(ev) {
        ev.preventDefault();
        hide_overlay();
    });
}


function initSlides(){  
  
  $('#slideshow img').each(function(){
    var $this = $(this);
    var src = $this.attr('src');
    $this.wrap('<div style="background: url('+src+') no-repeat 50% 50%;width: 700px;height: 446px;" >');
  });
  
  $('#slideshow').after('<ul id="nav">').cycle({ 
      fx:     'fade', 
      speed:  'fast', 
      timeout: 0, 
      pager:  '#nav',
      next:   '#slideshow',
      pagerAnchorBuilder: function(idx, slide) { 
          return '#nav li:eq(' + idx + ') a';
      } 
  });
  $("#thumb a").removeClass('collapsed');
  $("#thumb a").addClass('expanded');
}

$(document).ready(function(){
  
  $("#thumb a.collapsed").live('click', function(){
      var projID = $(this).attr('rel');
      $.ajax({
        url: 'album.php?id='+projID,
        success: function(data) {
          $('#ajax').animate({ height: 609 }, { duration: 500 });
          $('#ajax').html(data);
          initSlides();
          $('#thumb').css({ backgroundColor: "#ececec" });
        }
      });
    
  	return false;
  	});
  
    $("#thumb a.expanded").live('click', function(ev){
      ev.preventDefault();
      show_overlay();
      $('div#loader').css('background','url(/assets/css/loader.png) 0 0 no-repeat').animBg({'step':36,'steps':12,'interval':100}); 
        var projID = $(this).attr('rel');
        $.ajax({
          url: 'album.php?id='+projID,
          success: function(data) {
            $('#ajax').animate({ height: 609 }, { duration: 1000 });
            $('#ajax').html(data);
            initSlides();
            $('#thumb').css({ backgroundColor: "#ececec" });
            $.scrollTo( 0, 500);
            hide_overlay();
          }
        });

    	return false;
    	}); 
  
  
  $('#thumb img').each(function() {
      $(this).hover(function() {
      $(this).stop().animate({ opacity: 0.75 }, 100);
      },
      function() {
        $(this).stop().animate({ opacity: 1.0 }, 200);
        return false;
      });
  });
  
  $('a[rel*=external]').click(function(){
  window.open(this.href);
  return false;
  });
  
});
