var page_width = null;
var animate_in_process = false;
var playing = false;
var speed = 1000;
var direction = null;
var basecount = 0;
var thetimer = null;

// FOOTER SLIDER ---------------------------------------------- //

var moveit = function(curpos) {
	curpos = (curpos - (page_width/2));
	if(Math.abs(curpos) < 300) {
		return stopit();
	}
	direction = (curpos < 0) ? true : false ;
	curpos = Math.abs(curpos);
	speed = Math.round((1-(curpos / (page_width/2))) * 1500);
	speed = (speed < 700) ? 700 : speed ;
	animate_in_process = true;
	play();
	return true;
}

var stopit = function() {
	if(thetimer){ clearTimeout(thetimer); }
	animate_in_process = false;
	return false;
}

var play = function() {
	if(playing) { return false; }
	the_movement = (direction) ? '+=365' : '-=365';
	playing = true;
	jQuery("#footer_scroller ul").animate({
		left: the_movement
	    }, speed, function() {
			//alert(playing); return false;
			var the_offset = jQuery(this).css("left").replace("px","") * 1;
			if(the_offset==0) {
				jQuery(this).css("left","-" + (basecount * 365) + "px");
			} else if((the_offset - 365) <= (-(jQuery(this).css("width").replace("px","") * 1) + page_width)) {
				jQuery(this).css("left","-" + ((the_offset + (basecount * 365)) *-1 + "px"));
			}
			playing = false;
			if(animate_in_process) {
				play();
			}
	});
}



// SLIDER MAIN ---------------------------------------------- //

var sm_in_process = false;
var sm_direction = 'rev';

var init_ts = function() {
	var $ = jQuery;
	var original_count = $(".ts_item").length;
	var pre_copy = $(".ts_item:first").clone();
	var pos_copy = $(".ts_item:last").clone();
	$("#ts_content").append(pre_copy).prepend(pos_copy).css("width",((original_count + 2)*960) + "px").css("left","-960px");
	$('.ts_next').click(function() {
		af_prev();
		return false;
    });
	$('.ts_prev').click(function() {
		af_next();
		return false;
    });
}

var af_next = function() {
	if(sm_in_process) { return false; }
	sm_in_process = true;
	sm_direction = 'fwd';
	jQuery("#ts_content").animate({
		left: '+=960'
	    }, 1500, function() {
			var the_offset = jQuery(this).css("left").replace("px","") * 1;
			if(the_offset==0) {
				jQuery(this).css("left","-" + ((jQuery(".ts_item").length - 2) * 960) + "px");
			}
			sm_in_process = false;
	});
}


var af_prev = function() {
	if(sm_in_process) { return false; }
	sm_in_process = true;
	sm_direction = 'rev';
	jQuery("#ts_content").animate({
		left: '-=960'
	    }, 1500, function() {
		var the_offset = jQuery(this).css("left").replace("px","") * 1;
		var max_offset = -(jQuery(this).css("width").replace("px","") * 1) + 960;
		if(the_offset==max_offset) {
			jQuery(this).css("left","-960px");
		}
		sm_in_process = false;
	});
}

var autoplay = function() {
	switch(sm_direction) {
		case 'fwd':
			af_next();
			break;
		case 'rev':
			af_prev();
			break;
	}
}




// VERTICAL SLIDER ---------------------------------------------- //
// This one is not prepared for controls nor reverse direction

var verticalslider = function() {
	var ul = jQuery("#vert_scroller ul");
	if(jQuery("li",ul).length > 2) {
		ul.append(jQuery("li:lt(2)",ul).clone());
		setInterval("dovs()",6000);
	}
}

var dovs = function() {
	jQuery("#vert_scroller ul").animate({
		top: '-=185' },
		1000,
		'linear',
		function() {
			var the_offset = Math.abs((jQuery(this).css("top").replace("px","")) * 1);
			var max_offset = (jQuery("#vert_scroller ul li").length - 2) * 185;
			//alert("the: " + the_offset + "\nmax: " + max_offset);
			if(the_offset == max_offset) {
				jQuery(this).css("top","0px");
			}
		}
	);
}