/*
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-11982870-1']);
_gaq.push(['_setAllowAnchor', true]);

	(function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
	})();
*/


jQuery(document).ready(function($) {

/* 	$('#time').jclock(); */

	var n = 1;
	var domain = 'http://' + document.domain;	


	var window = $('#main .img_holder');


	window.resizable().draggable({
		handle: '.window_top',
		cursor: 'move'
	}).each(function(){
		var w = $(this).find('.img_wrapper img:first').width() + 10;
		var h = $(this).find('.img_wrapper').height() + 45;
		$(this).width(w).height(h);
	});





	//rewrite links in .entry
/*
	$('body.home #main h2.entry-title a').each(function(){
		var href = $(this).attr('href');
		var url = '#' + href.substr(domain.length, href.length);
		url = url.substr(0, url.length-1);
		$(this).attr('href', url);
	});
*/
	

	//////////////////////////////////////////////////////////////////////////////////////////////
	//										BBQ													//
	//////////////////////////////////////////////////////////////////////////////////////////////

/*
			var cache = {
				'': $('.bbq-default')
			};

		  $(window).bind( 'hashchange', function(e) {
		    
		    var url = $.param.fragment();
		    $( 'a.bbq-current' ).removeClass( 'bbq-current' );
		    $( '.bbq-content' ).children( ':visible' ).hide();
		    url && $( 'a[href="#' + url + '"]' ).addClass( 'bbq-current' );
		    
		  	// title and tracking
//		    var page = url.substr(1).replace(/-/gi, ' ');
//		    page = page.charAt(0).toUpperCase() + page.slice(1);
//		    if(page == '') page = 'Home';
//		    trackClick(page);
//		    document.title = 'The Thousands > ' + page;	
		    
		    if ( cache[ url ] ) {
		      	cache[ url ].fadeIn(500);
		      	// don't show full entry on pageload haschange event
//		      	if( cache[url].html().length > 50 ) $('#full-entry, .close').fadeIn(500);
		    } else {
		      $( '.bbq-loading' ).fadeIn(500);
		      $('#full-entry').hide();
		      $('.close').hide();
		      
		      cache[ url ] = $( '<div class="bbq-item"/>' )
		        .appendTo( '.bbq-content' )		        
		        .load( url + ' #main .entry', function(){
		          $('#full-entry').show('blind', 1000, function(){
			          $('.close').fadeIn(1000);			  
			          scrollBar();
			          $( '.bbq-loading' ).fadeOut(500);
		          });
		        });
		    }
		  });
		  
		  $(window).trigger( 'hashchange' );
*/


	
	//////////////////////////////////////////////////////////////////////////////////////////////////////		
	
	function scrollBar() 
	{
		if( $('.bbq-item:visible .scroll_wrapper').length !== 0 ) 
		{
			// already set
		}
		else {
			// fix footer in place
			var title = $('#full-entry .bbq-item:visible .entry-title');
			var tpos = $(title).position();				
		
			// measure 
			var t = $('#full-entry .bbq-item:visible .entry-content');
			var t2 = $('#full-entry');
			var w = $(t2).width();
			var h = $(t).height();
			var cH = $('#full-entry .bbq-item:visible .entry-inside').outerHeight();
			var scale = h/cH;
			var sbH = (h*scale);
			
			// if contents are longer than container
			if( cH > h )
			{
				$('#full-entry .entry-content p').css({ 'padding-right': 10 });

				var cH = $('#full-entry .bbq-item:visible .entry-inside').outerHeight();
				var scale = h/cH;
				var sbH = (h*scale);			

				h = h - 10;

				// create scrollbar
				$('#full-entry .bbq-item:visible .entry-content').after('<div class="scroll_wrapper"><div class="scroll_bar">&nbsp;</div></div>');
				$('.bbq-item:visible .scroll_wrapper').css({ 'left': w, 'top': -(h+10), 'height': h }).hide();
				$('.bbq-item:visible .scroll_bar').css({ 'height': sbH }).hide();
				$('.bbq-item:visible .scroll_wrapper, .bbq-item:visible .scroll_bar').fadeIn(500)
				
				h = h - 2;
				
				// make scrollbar work
				$('.scroll_bar').draggable({
					containment: 'parent',
					cursor: 'move',
					axis: 'y',
					drag: function(event, ui) {
						var move = ((ui.position.top / (h - sbH)) * (cH - h)) * -1;
						$('#full-entry .bbq-item:visible .entry-inside').css({ 'top': move });
					}
				});
			}
		}
	}
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	
	function trackClick(page)
	{
		_gaq.push(['_trackEvent', page]);
		$(this).log('GOOGLE ANALYTICS > '+page)
	}
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////	

});

// CONSOLE LOG FUNCTION
jQuery.fn.log = function (msg) {
	if(typeof(console) != 'undefined') console.log("%s: %o", msg, this);
	return this;
};