var l,
t,
relink,
oldhash,
base_url = "http://letskiosk.com/",
settings = {
    gap: 15,
    width: 200,
    easing: 'easeInOutQuart',
    duration: 200,
    speed: 100
};

function sorty(l, prevh, prevw, relink) {

	$(l).empty();
    $(l).animate({
        height: prevh,
        width: prevw
    }, settings.speed, function() {
        $(this).hide();
        $(this).append(relink).delay(settings.duration*4).fadeIn(settings.speed);
    });

    $(l).removeClass('open postwide');

}

function pageload(hash, tn) {

	if (tn===false) { 

	    if (hash) {
	        $("#loader").fadeIn(100);
	        $.ajax({
	            url: base_url+"home/page/" + hash,
	            cache: false,
	            success: function(html) {
	                $("#loader").fadeOut(200, function() {
	                    $("#projectthumbs").append(html);
	                    $("#projectthumbs").css('width',settings.width*3);
	                    $(".project").fadeIn(200);
	                });
	            }
	        });
	    } else {
	        $(t).empty();
	    }

	} else {
	    


	    l = t;
	    t = '#c-' + hash;
	    relink = '<a href="' + oldhash + '" rel="history"><img class="thumbimg" src="' + thumb + '" /></a>';
 //		$(".overlay", t).css('visibility','hidden'); 
		
		h = $(t).height();
		w = $(t).width();
	
	    $(t).prepend('<div class="ploader"></div>');
	    $(t + ' .ploader').css({
	        width: w,
	        height: h
	    });
	
	    if (hash) {
	        $("#loader").fadeIn(100);
	        $.ajax({
	            url: base_url+"home/page/" + hash,
	            cache: false,
	            success: function(html) {
	            	
	                sorty(l, prevh, prevw, relink);
	                prevh = $(t).height();
	                prevw = $(t).width();
	                
	                $("#loader").fadeOut(200, function() {
	                    thumb = $(t).find('img').attr('src');
	                    oldhash = $(t).find('a').attr('href');
	                    $(t).addClass('open postwide');
	                    $(t).append(html);
	                    _height = $("#c-" + hash + ' .project').height() + 30;
	                    $(t).css('height', _height);
	                    
	                    $("#projectthumbs").grid(settings, t, false, true);
	         	      	
 				      	$(".thumbimg").each(function(){
							$(this).fadeTo('slow', 0.3);
						});
						$(t).removeClass('border');
			
	                });
	            }
	        });
	    } else {
	        $(t).empty();
	    }

    }
}

$(function() {

  // on window resize, call again
    var resizeTimer = null;
    $(window).bind('resize', function() {
      if (resizeTimer) clearTimeout(resizeTimer);
      resizeTimer = setTimeout(function(){ 
      		$("#projectthumbs").grid(settings,'',false, true);
      }, 100 );		
    });
    
 
    $(".scrolltopbutton").live("click", function() {

		$('html,body').animate({scrollTop: $(this).attr('id')}, settings.duration, settings.easing);

    });
        
    $(".thumbimg").live("mouseenter", function() {
      $(this).fadeTo('fast', 1);
    }).live("mouseleave", function() {
    	$(this).fadeTo('slow', 0.3);
    });

    settings.animateUpdate = false;
    
    $("#projectthumbs").grid(settings);

    settings.animateUpdate = true;

    $("#menu div > a[rel='history']").live('click', function() {
        var hash = this.href;
        hash = hash.replace(/^.*#/, '');
        $("#projectthumbs").fadeOut(200, function(){
    						window.location = base_url + hash;
    				});    
    });

    $("#projectthumbs div > a[rel='history']").live('click', function() {
 
        var hash = this.href;
      	hash = hash.replace(/^.*#/, '');
        $.historyLoad(hash);
        //return false;
    });
    $.historyInit(pageload);
});
