// JavaScript Document
$(document).ready(function() {
						   
	/* Preload slideshow */
	$.preload( '.slideshow img', {//
		onRequest:request,
		onComplete:complete,
		onFinish:finish,
		placeholder:'i/slidershow/holder.jpg',//
		threshold: 1
	});
	
	/* Turn off the slide-show*/
	$('.slide-off').click(function() {
		$('.slideshow-nav .prev a').animate({left:"0"}, 300);	
		$('.slideshow-nav .next a').animate({right:"0"}, 300);	
			$('.slideshow').cycle('pause');
			$('.pager a').removeClass('active');
				$(this).addClass('active');
	});
	/* Turn on the slide-show*/
	$('.slide-on').click(function() {
		$('.slideshow-nav .prev a').animate({left:"-35px"}, 300);	
		$('.slideshow-nav .next a').animate({right:"-35px"}, 300);	
			$('.slideshow').cycle('resume');
			$('.pager a').removeClass('active');
				$(this).addClass('active');
	});
	
	/* Interval to check how much as loaded, based on how far the 
	bar has moved */
	var id = setInterval(function(){

			width = $(".loader-bar").width();
			per = width / 820 * 100;
			per = Math.round(per);
			$(".loaded").text(per+'%');
			}, 10);
	
	
	/* Move the bar */
	function update( data ){
		per = data.done / data.total * 100;
		$(".loader-bar").stop();
		$('.loader-bar').animate({width:per+"%"}, 500);	
	};
	
	
	function complete( data ){
		update( data );
	
	};
	function request( data ){
		update( data );
	};
	
	/* When loading has finished */
	function finish(){
		
		$('.loader-bar').animate({width:"100%",opacity:1}, 500, function() {;
				$('.slideshow img').fadeIn(1000,function() {
					$('.slideshow').cycle({
						fx: 'fade',
						timeout: 8000,
						speed:1000,
						after:function(currSlideElement, nextSlideElement, options, forwardFlag) {slide_after(options.currSlide,options.nextSlide,options.slideCount);},
						before:function(currSlideElement, nextSlideElement, options, forwardFlag) {slide_before(options.nextSlide);},
						next:'.slideshow-nav .next a',
						prev:'.slideshow-nav .prev a'
					});
					$('.pager').animate({bottom:0},1000);
				});	
		});
	};
	

	/* Functions to show the slideshow numbers
	   LAGANGBANG v0.1	
	*/
	function slide_after(c,n,s) {
		$(".totals .curr").text(c+1);
		$(".totals .total").text(s);
	}
	function slide_before(n) {
		$(".totals .curr").text(n+1);
	}
	
});