$(function () {
	
  // Center all thumbnails
  $(".thumbnail").each(function() {
  	var pw = $(this).outerWidth(), ph = $(this).outerHeight(),
  		  img = $(this).find('img'),
  		  iw = img.outerWidth(), ih = img.outerHeight(),
  		  fw = (iw - pw) / 2,
  		  fh = (ih - ph) / 2,
  		  css = {};
  	css['margin-left'] = fw-fw-fw;
  	css['margin-top'] = fh-fh-fh;
  	img.css(css);
  });
  
  var root = $(".scrollable").scrollable({
    circular: true,
    speed: 1000
  })
  .autoscroll({
    interval: 4000,
    autoplay: true,
    autopause: true
  })
  .navigator();
  
  var api = root.data("scrollable");
  
  $(".navi").hover(function () {
    api.pause();
  });
  
  // Sub menu
  $("#menu-main-menu > li").live('mouseenter', function() {
  	$(this).find('.sub-menu').show(100);
  });
  $("#menu-main-menu > li").live('mouseleave', function() {
  	$(this).find('.sub-menu').hide(100);
  });
  
  
  // Celebrity scrollables

	$(".thumbnails").scrollable();
	
	// Gallery posts browsing
	
	$(".album .items a").click(function(e) {
  	e.preventDefault();
  
  	// see if same thumb is being clicked
  	if ($(this).hasClass("active")) { return; }
  
  	// calclulate large image's URL based on the thumbnail URL (flickr specific)
  	var url = $(this).attr("href");
  
  	// get handle to element that wraps the image and make it semi-transparent
  	var wrap = $(".main-image").fadeTo("medium", 0.5);
  
  	// the large image from www.flickr.com
  	var img = new Image();
  
  
  	// call this function after it's loaded
  	img.onload = function() {
  
  		// make wrapper fully visible
  		wrap.fadeTo("fast", 1);
  
  		// change the image
  		wrap.find("img").attr("src", url);
  		wrap.find("a").attr("href", url);
  
  	};
  
  	// begin loading the image from www.flickr.com
  	img.src = url;
  
  	// activate item
  	$(".items a").removeClass("active");
  	$(this).addClass("active");
  
  // when page loads simulate a "click" on the first image
  }).filter(":first").click();


});
