/*
 * jQuery Timer Plugin
 * http://www.evanbot.com/article/jquery-timer-plugin/23
 *
 * @version      1.0
 * @copyright    2009 Evan Byrne (http://www.evanbot.com)
 */ 

jQuery.timer = function(time,func,callback){
	var a = {timer:setTimeout(func,time),callback:null}
	if(typeof(callback) == 'function'){a.callback = callback;}
	return a;
};

jQuery.clearTimer = function(a){
	clearTimeout(a.timer);
	if(typeof(a.callback) == 'function'){a.callback();};
	return this;
};




var blockwidth = 600; // width of one block (will be computed!) changes here have no effekt!
var blockcount = 1; // how many block will you display (Blockcount will be from 1 - blockcount, and not from 0 - (blockcount-1)!!!)
					// will be computed out of the blocks that have the css class "slider-block".
					// changes here have no effect!
var currentblock = 1; //this is the first block displayed
var slideshowspeed = 3000 //in ms (will be read from html)
var mouseX = 0; 
var mouseY = 0;
var autoslide = true; /* should autostart be enabled */
var pagerWidth = 23;
var slideAnimated = true;

jQuery(document).mousemove(function (e){
	mouseX = e.pageX;
	mouseY = e.pageY;
});


function getPagerHtml(){
	var html = '';
	for(i = 1 ; i <= blockcount; i++){
		html += '<div class="slider-pager slider-pager-inactive" id="slider-pager-block-'+i+'" />';
	}
	html += '<div class="slider-pager slider-pager-active" id="slider-pager-active" />';
	return html;
}


function setCurrentBlock(blocknumber,animated){
	
	if(blocknumber > blockcount){
		blocknumber = 1;
	}else if(blocknumber < 1){
		blocknumber = blockcount;
	}
	
	x = -(blockwidth * (blocknumber-1))+'px';
	xpager = (pagerWidth * (blocknumber-1))+'px';
	
	if(animated){
		jQuery('#slider-internal-wrapper').animate(
			{left: x},
			'slow'
		);
		jQuery('#slider-pager-active').animate(
			{left: xpager},
			'slow'
		);
	}else{
		jQuery('#slider-internal-wrapper').css('left',x);
		jQuery('#slider-pager-active').css('left',xpager);
	}
	currentblock = blocknumber;
}

function startTimer(){
	jQuery.timer(slideshowspeed,function(e){
		
		divposition = jQuery('#slider-wrapper').offset();
		
		x = mouseX - divposition.left;
		y = mouseY - divposition.top;
		
		if( ((0 <= x) && (x <= jQuery('#slider-wrapper').width())) &&
			((0 <= y) && (y <= jQuery('#slider-wrapper').height()))){
				// Curser is in the Block, now we should stop sliding
		}else{
			setCurrentBlock(currentblock+1,slideAnimated);
		}
		startTimer();
	});
	
}

jQuery(document).ready(function(){
	// init goes here
	blockcount = jQuery(".slider-block").length;
	blockwidth = jQuery(".slider-block").css("width");
	//remove "px" at the end
	blockwidth = blockwidth.substr(0, blockwidth.length-2);
	
	configTmp = jQuery("#slideshowspeed").html();
	if((Number(configTmp) != NaN) && (Number(configTmp) != 0)){
		slideshowspeed = Number(configTmp);
	};
	
	configTmp = jQuery("#startblock").html();
	if((Number(configTmp) != NaN) && (Number(configTmp) != 0)){
		currentblock = Number(configTmp);
	}
	
	configTmp = jQuery("#autoslide").html();
	if(configTmp == 'false'){
		autoslide = false;
	}
	
	configTmp = jQuery("#slideanimated").html();
	if(configTmp == 'false'){
		slideAnimated = false;
	}
	// End init
	
	jQuery('#slider-pager-right').click(function(){
		setCurrentBlock(currentblock + 1,slideAnimated);
	});

	jQuery('#slider-pager-left').click(function(){
		setCurrentBlock(currentblock - 1,slideAnimated);
	});
	
	console.log(jQuery('#slider-pager-wrapper'));
	jQuery('#slider-pager-wrapper').width(blockcount * pagerWidth);
	jQuery('#slider-pager-wrapper').html(getPagerHtml());
	
	setCurrentBlock(currentblock,false);
	
	if(autoslide){
		startTimer();
	}
});
