var pointer_within = false;
var subTimeout;
var firstpage_spinner;
var case_spinner;

$(function() {

	if($("#topImagesSpinner").is("*"))
		firstpage_spinner = spinner.initSpinner("topImagesSpinner", "");
});

function escapeStr(str) {
	if(str)
		return str.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g,'\\$1');
	else
		return str;
}

var spinner = {
	current_slide: 0,
	wrapper: null,
	spinnerID: null,
	timer: null,
	url: '',
	isAnimating: false,
	
	initSpinner: function(which, url) {
		
		var self = this;
		this.spinnerID = which;
		this.url = url;
	    
	    /*$.get(this.url, function(callback){
	        $wrapper.html($(callback)[1].innerHTML);
			$("#Mittenspalt", $wrapper).remove(); // Remove ugly code
	        $("#"+ self.spinnerID).append($wrapper);
	        Cufon.refresh();
	    });*/
	    
	    this.wrapper = $('#topImagesWrapper');
	    
	    // Bind buttons to actions
		var $parent = $("#"+ this.spinnerID).parent();
	    $(".arrow_left").bind("click", function(obj){ self.rotateSpinner(obj) });
		$(".arrow_right").bind("click", function(obj){ self.rotateSpinner(obj) });

	    this.startTimer();
	    
	    return this;
	},
	
	resetAnimation: function(){
		spinner.isAnimating = false;
	},
	
	rotateSpinner: function(obj) {
		var self = this;
		var ixof = (obj.currentTarget.className.indexOf("arrow_left") === 0);
		var $spinner = $("#"+ this.spinnerID);
		var $wrapper = $spinner.children().eq(0);
		var $last = $wrapper.children().last();
		var $first = $wrapper.children().first();
		
		trace(this.isAnimating)
		
		if(this.isAnimating)
			return false;
		
		this.isAnimating = true;
		
		if(ixof) { // Spinner left
			this.current_slide--;
			
			if(this.current_slide < 0) {
				$last.insertBefore($first);
				this.current_slide = 0;
				$spinner.scrollTo($wrapper.children().eq(1));
				thenext = $last;
			}
			
			thenext = $wrapper.children().eq(this.current_slide);
			
			$spinner.scrollTo(thenext, 1400, { easing: 'swing', onAfter: spinner.resetAnimation });
		} else { // Spinner right		
			this.current_slide++;
			thenext = $wrapper.children().eq(this.current_slide);
			
			if(this.current_slide >= $wrapper.children().length) {
				//this.current_slide = 0;
				$first.insertAfter($last);
				this.current_slide = $wrapper.children().length-1;
				//trace($wrapper.children());
				$spinner.scrollTo($last);
				thenext = $first;
			}
			
			$spinner.scrollTo(thenext, 1400, { easing: 'swing', onAfter: spinner.resetAnimation });
		}
		
		clearTimeout(this.timer);
	},
	
	startTimer: function() {
		var self = this;
		this.timer = setTimeout(function() { self.timerTick() }, 4000);
	},
	
	timerTick: function() {
		var $spinner = $("#"+ spinner.spinnerID);
		var $wrapper = $spinner.children().eq(0);
		
		spinner.current_slide++;
		if(spinner.current_slide >= $wrapper.children().length)
			spinner.current_slide = 0;
				
		$spinner.scrollTo($wrapper.children().eq(spinner.current_slide), 1200, { easing: 'swing' });
		
		spinner.startTimer();
	}
};

function trace(str) {
	/*try {
		console.log(str);
	} catch(e) {
		//
	}*/
}
