$(document).ready(function() {
	
	rotate = function(){	
		var triggerID = $active.attr("rel") - 1;
		var currentID = $current.attr("rel") - 1;
		
		if(triggerID !== currentID) {
			$(".slider-wrapper .panel").eq(currentID).fadeOut(600);
			$(".slider-wrapper .panel").eq(triggerID).fadeIn(600);
	
			$(".paging a").removeClass('active');
			$active.addClass('active');
		}
		
		return false;		
	};
	
	rotateSwitch = function(time){	
		$play = setInterval(function(){
			$current = $('.paging a.active');
			$active = $('.paging a.active').next();
			if ( $active.length === 0) {
				$active = $('.paging a:first');
			}
			rotate();
		}, time);
	};
	
	rotateSwitch(8000);
	
	
	
	$.fn.start = function(){
		var url = $('body').data('url'), font = $('body').data('font');
		$("a[rel^='prettyPhoto']").prettyPhoto();
		
		$(".paging a:first").addClass("active");
		$(".slider-wrapper .panel").hide();
		$(".slider-wrapper .panel:first").show();
		
		$(".slider-wrapper .panel").hover(function() {
			clearInterval($play);
		}, function() {
			rotateSwitch(8000);
		});	
		
		$(".paging a").click(function() {	
			$current = $('.paging a.active');
			$active = $(this);
			clearInterval($play);
			rotate();
			rotateSwitch(8000); 
			return false;
		});
		
		$('#s3slider').each(function(){
			$(this).find('img').removeAttr('title');		
			$(this).s3Slider({
				timeOut: 10000
			});	
		});
					
		$('.font-size').jfontsizer({
			applyTo: '#contents',
			plus: '1',
			minus: '1',
			expire: 30,		
			domain: url
		});
		
		$('.menu ul li').hover(function(){
			$(this).find('ul:first').stop().slideDown(250);							
		}, function(){
			$(this).find('ul:first').stop().slideUp(250, function(){
				$(this).attr('style', '');
			});			
		});
		
		$('blockquote').each(function() {
			var text = $(this).html();
			text = text.replace('<p>', '');
			text = text.replace('</p>', '');
			$(this).html('<i><em>&#147;</em></i>'+text+'<b><em>&#148;</em></b>');
		});
		
		if(font) {
			$('#contents').css({'font-size': font+'px'});
		}
		return this;
	}
	
	$.fn.pageload = function(href){
		if(!href) return false;
		var $a = $(this), url = $('body').data('url');
		$('#pagecontainer').load(href+' #page', function(){
			window.location.hash = href.replace(url, '');
			$('body').start();
		});	
		return this;
	}
		
});
