/**
 * @package unitb.com
 */
/**
 * carousell.js
 * 
 * routine zum blaettern in den news von unitb.com
 * @require jquery.js
 * @author Juergen Arne Klein
 * <juergen-arne.klein@unitb.com>
 */
var b_badBrowser  = /MSIE\ 7/.test(navigator.userAgent);
var o_car         = null;
var carousell = function() {
	/**
	 * members
	 */
	var a_cIDs     = new Array();
	var i_timeout  = 550;
	var i_nextID   = 0;
	var i_prevID   = 0;
	var i_actID    = 0;
	var s_actID    = '';
	var s_newCont  = '';
	
	/**
	 * functions
	 */
	var init = function(s_firstID) {
		$('#static_stage div.static').each(function() {
			a_cIDs.push($(this).attr('id'));
		});
		
		if(typeof(s_firstID) == 'undefined') {
			s_actID = a_cIDs[0];
		} else {
			s_actID = s_firstID;
		}
		
		$('#moving_stage div.main_cont').html($('#' + s_actID).html());
		$('#static_stage').hide();
		$('#moving_stage').show();
		
		if(a_cIDs.length > 1) {
			i_prevID = 1;
			i_nextID = a_cIDs.length -1;
			$('#newsnavi').show();
			
			// ueber die erste zahl wird die geschwindigkeit des schwenks gesteuert, uber die letzte zahl das intervall zwischen zwei schwenks:
			var cycle = window.setInterval("o_car.showPrev(550)", 45000);
			
			$('#newsnavi a').click(function() {
				window.clearInterval(cycle);
				cycle = window.setInterval("o_car.showPrev(550)", 45000);
		
				if($('#moving_stage').hasClass('lock')) {return false;}
		
				if($(this.parentNode).hasClass('actual_news')) {
					f_showLatest(i_timeout);
				} else if($(this.parentNode).hasClass('next_news')) {
					f_showPrev(i_timeout);
				}
			});
		}	
		f_setIDs(s_actID);
	}
	this.init = init;

	var f_showDist = function(s_imgID) {
		if(typeof(s_imgID) == 'undefined' || s_imgID == '') {
			s_imgID = a_cIDs[0];
		}

		if(!$('#moving_stage').hasClass('lock')) {
			f_setIDs(s_imgID);
			// die stage blockieren
			$('#moving_stage').addClass('lock');
			// den content der neuen box lesen
			s_newCont = $('#' + s_imgID).html();
			$('#moving_stage .prev_cont').html(s_newCont);
			
			// die stage verschieben
			$('#moving_stage').animate({left: -1000}, i_timeout, function(){
				// ueberhang-container entfernen
				$('#moving_stage .next_cont').remove();
				// umsetzen der container
				$('#moving_stage .main_cont').addClass('next_cont').removeClass('main_cont');
				$('#moving_stage .prev_cont').addClass('main_cont').removeClass('prev_cont');
				$('#moving_stage .main_cont').after('<div class="moving prev_cont"></div>');
				$('#moving_stage').css('left', '0');
				$('#moving_stage').removeClass('lock');
			});
		}
	}
	this.showDist  = f_showDist;

	// zeigt die vorherige meldung
	var f_showPrev = function() {
		f_showDist(a_cIDs[i_prevID]);
	}
	this.showPrev  = f_showPrev;
	
	// zeigt die naechste Meldung
	var f_showNext = function() {
		f_showDist(a_cIDs[i_nextID]);
	}
	this.showNext  = f_showNext;
	// zeigt die naechste Meldung

	var f_showLatest = function() {
		f_showDist();
	}
	this.showLatest  = f_showLatest;
	
	// setzt die ID herauf
	var f_incID = function(i_myID) {
		if(i_myID == (a_cIDs.length -1)) {
			i_myID = 0;
		} else {
			i_myID ++;
		}
		
		return i_myID;
	}
	
	// setzt die ID herunter
	var f_decID = function(i_myID) {
		if(i_myID > 0) {
			i_myID --;
		} else {
			i_myID = a_cIDs.length -1;
		}
		
		return i_myID; 
	}
	
	var f_getIndex = function(s_id) {
		for(var i = 0;i < a_cIDs.length; i++) {
			if(a_cIDs[i] == s_id) {

				return i;
			}
		}

		return 0;
	}
	
	var f_setIDs = function(s_myID) {
		i_actID  = f_getIndex(s_myID);
		i_prevID = f_incID(i_actID);
		i_nextID = f_decID(i_actID);
	}
}



/*
 * listener
 */
$().ready(function() {
	o_car = new carousell();
	o_car.init();
	
	var swipeOptions=
	{
		swipeLeft:swipeLeft,
		swipeRight:swipeRight,
		threshold:0
	};
	
	$(function()
	{			
		//Enable swiping...
		$("#moving_stage").swipe( swipeOptions );
	});

	//Swipe handlers.
	//The only arg passed is the original touch event object			
		
	var count=0;
	function swipeLeft(event, direction, distance)
	{
		o_car.showPrev();
	}

	function swipeRight(event, direction, distance)
	{
		o_car.showLatest();
	}
	
});

