
/* For discussion and comments, see: http://remysharp.com/2009/01/07/html5-enabling-script/
--------------------------------------------------------------------------------------------------------------------*/
(function(){if(!/*@cc_on!@*/0)return;var e = "abbr,article,aside,audio,canvas,datalist,details,eventsource,figure,footer,header,hgroup,mark,menu,meter,nav,output,progress,section,time,video".split(','),i=e.length;while(i--){document.createElement(e[i])}})()


/* Cufon
--------------------------------------------------------------------------------------------------------------------*/
Cufon.replace('.intro,#sidebar h1, .latest-header h2, #featured h2, #comments h2, .simple-rating p.user-rate-info',{textShadow: '#ffffff 0 1px'});


/* Sidebar List
--------------------------------------------------------------------------------------------------------------------*/
var List = function(){
	
	var base;
	var maxPage = 1;
	
	var self = {
		
		// Initialize the list
		Init: function(selector){
			base = $(selector);
			self.PagingSetup();
			self.PagingBehavior();
			
		},
		
		// Setup Paging
		PagingSetup: function(){
			var maxHeight = base.height();
			var pagingHeight = 25;
			var tmpHeight = 0;
			var tmpHeightTotal = 0;
			maxPage = 1;
			
			if(base.find('ul').outerHeight() > maxHeight){
				maxHeight = maxHeight - pagingHeight;
				
				base.find('li:first-child')
								.addClass('paged paged-1')
								.data('top',0);
								
				base.find('li').each(function(){
					if(tmpHeight + $(this).outerHeight() < maxHeight){
						tmpHeight += $(this).outerHeight();
						tmpHeightTotal += $(this).outerHeight();
					}else{
						if(maxPage == 1) $(this).before(self.PagingTemplate(1,maxPage));
						else $(this).before(self.PagingTemplate(3,maxPage));
						
						maxPage++;
						
						$(this)
								.addClass('paged paged-'+maxPage)
								.data('top',0 - (tmpHeightTotal + pagingHeight));
						tmpHeightTotal  += $(this).outerHeight() + pagingHeight;
						tmpHeight = $(this).outerHeight();
					}
				});
				base.find('ul').append(self.PagingTemplate(2,maxPage));
			}
		},
		
		// Handle paging behavior: hover, click
		PagingBehavior: function(){
			base.find('.paging a').click(function(){
				var paging = $(this).parent();
				var type = ($(this).hasClass('next')) ? 'next' : 'prev';
				
				if( (paging.hasClass('type-2') && type == 'next') || (paging.hasClass('type-1') && type == 'prev') )
					return false;
				
				var gotoClass = '.paged-' + $(this).attr('goto');
				base.find('ul').animate({
					top: $(gotoClass).data('top')
				}, 300);
				return false;
			});
			base.find('.paging a.next').hover(
				function(){ $(this).parents('.paging-wrapper').addClass('next-hover');},
				function(){ $(this).parents('.paging-wrapper').removeClass('next-hover');}
			);
			base.find('.paging a.prev').hover(
				function(){ $(this).parents('.paging-wrapper').addClass('prev-hover');},
				function(){ $(this).parents('.paging-wrapper').removeClass('prev-hover');}
			);
		},
		
		// Paging html template
		PagingTemplate: function(type, n){
			var template = ['<li class="paging-wrapper">',
											'	<p class="paging type-' + type+ '">',
											'		<a href="#" class="prev" goto="' + (n-1) + '">Sebelumnya</a>',
											'		<a href="#" class="next" goto="' + (n+1) + '">Selanjutnya</a>',
											'	</p>',
											'</li>'].join('\n');
			return template;
		}
	
	};
	return self;

}();


/* Carousel
--------------------------------------------------------------------------------------------------------------------*/
var Carousel = function(){
		
		var base;
		var content;
		var current = 0;
		var width = 615;
		var max;
		
		var self = {
				Init: function(selector){
					base = $(selector);
					content = base.find('.wrapper');
					
					// setup wrapper width
					var length = content.find('article').length;
					content.width(length * width);
					max = length - 1;
					
					// setup paging
					var paging = '';
					for( var i=0; i<length;i++) paging += '<a href="#'+(i+1)+'">Page '+(i+1)+'</a>\n';
					base.find('.carousel-paging').append(paging);
					
					self.SetActive(current);
					self.SetupArrow();
					self.SetupNav();
				},
				SetActive: function(index){
					content.animate({ left: -1 * (width*index) },function(){
						base.find('.carousel-paging a').removeClass('current');
						base.find('.carousel-paging a').eq(index).addClass('current');
					});
					current = index;
				},
				SetupArrow: function(){
					base.find('.carousel-nav .next').click(function(){
						if(current < max ) self.SetActive(++current);
						return false;
					});
					base.find('.carousel-nav .prev').click(function(){
						if(current > 0) self.SetActive(--current);
						return false;
					});
				},
				SetupNav: function(){
					base.find('.carousel-paging a').click(function(){
						var index = base.find('.carousel-paging a').index($(this));
						self.SetActive(index);
						return false;
					});
				}
		};
		return self;
		
}();


/* Initialize Document
--------------------------------------------------------------------------------------------------------------------*/
jQuery(document).ready(function($){
	if($('#sidebar .list').length > 0 )List.Init('#sidebar .list');
	if($('.latest').length > 0 )Carousel.Init('.latest');
	$('.tabs a').click(function(){ return false;});
});
