// random_cos.js

function ww_rotate_cos(selector, interval, speed, terminal_speed, effect, terminal_fadein)
{
	var $bannerList = $(selector).children();

	var current = null;
	var last = null;
	var last_stack = [];

	var animation_timer = null;
	var animation_runs = false;
	var startstop_state = false;

	var terminal_timeout = null;
	var terminal_trigger = false;
	var terminal_trigger_timeout = speed;

	var icn_next = '/static/img/vda10/rotate_cos/icn_next.gif';
	var icn_prev = '/static/img/vda10/rotate_cos/icn_prev.gif';
	var icn_stop = '/static/img/vda10/rotate_cos/icn_stop.gif';
	var icn_start = '/static/img/vda10/rotate_cos/icn_start.gif';
	var icn_w = '16';
	var icn_h = '16';

	var terminal_fadein = terminal_fadein;

	var debug = false;
	var wid_terminal = '<div id="rotate_cos_terminal">'
 					 + '<div class="co_terminal_btn" id="co_terminal_prev"><img src="' + icn_prev + '" alt="prev" width="' + icn_w + '" height="' + icn_h + '" border="0"/></div>'
 				     + '<div class="co_terminal_btn" id="co_terminal_startstop"><img id="co_terminal_startstop_icn" src="' + icn_stop + '" alt="start/stop" width="' + icn_w + '" height="' + icn_h + '" border="0"/></div>'
				     + '<div class="co_terminal_btn" id="co_terminal_next"><img src="' + icn_next + '" alt="next" width="' + icn_w + '" height="' + icn_h + '" border="0"/></div>'
				     + '</div>';

	function msg(str) {
		if (debug == true)
			console.log(str);
	}

	function startAnimationInterval() {
		if (!startstop_state && animation_timer == null)
			animation_timer = window.setInterval(function () { animate() }, interval);
			animation_runs = true;
		msg('start interval');
	}

	function stopAnimationInterval() {
		window.clearInterval(animation_timer);
		animation_timer = null;
		animation_runs = false;
		msg('stop interval');
	}

	function show_terminal_controls() {
		var b_offset = $(selector).offset();
		var b_width = $(selector).width();

        var t_width = $('#rotate_cos_terminal').width();
		var t_height = $('#rotate_cos_terminal').height();

		var t_top = b_offset.top + 4;
		var t_left = b_offset.left + b_width - t_width - 3;

		$('#rotate_cos_terminal').css("top", t_top + "px");
		$('#rotate_cos_terminal').css("left", t_left + "px");

		if (this.terminal_fadein)
		  $('#rotate_cos_terminal').fadeIn('fast');
		else
          $('#rotate_cos_terminal').show();

		//msg('show_terminal_controls');
	}

	var tcont_refresh = null;
	var tcont_refresh_rate = 100;

	function show_terminal() {
	    stopAnimationInterval();
        if (tcont_refresh == null)
        {
    	    show_terminal_controls();
    		tcont_refresh = window.setInterval(function () { show_terminal_controls() }, tcont_refresh_rate);
        }
    	msg('show_terminal interrupt interval');
	}

	function hide_terminal() {
	    if (tcont_refresh != null)
	    {
    		window.clearInterval(tcont_refresh);
    		tcont_refresh = null;
	    }
	    startAnimationInterval();
        if (this.terminal_fadein)
		  $('#rotate_cos_terminal').fadeOut();
		else
		  $('#rotate_cos_terminal').hide();
		msg('hide_terminal restart interval');
	}

	function triggerEnter() {
		if (terminal_timeout != null) {
			window.clearTimeout(terminal_timeout);
			terminal_timeout = null;
			msg('interrupt timeout');
		}
		terminal_trigger = true;
		show_terminal();
	}

	function triggerLeave() {
		if (terminal_trigger == true && terminal_timeout == null) {
			msg('restart interval');
			terminal_timeout = window.setTimeout(function () {  terminal_timeout = null;
	 															terminal_trigger = false;
	 															hide_terminal();
	 														 }, terminal_trigger_timeout);
		}
	}

	if ($bannerList.length > 0) {
		$(selector).after( wid_terminal );
		$('#co_terminal_prev').bind("click", function(e) { show_prev_frame() });
		$('#co_terminal_startstop').bind("click", function(e) { toggle_startstop() });
		$('#co_terminal_next').bind("click", function(e) { show_next_frame() });
		$(selector).bind("mouseenter", triggerEnter).bind("mouseover", triggerEnter).bind("mouseleave", triggerLeave);
	    $("#rotate_cos_terminal").bind("mouseenter", triggerEnter).bind("mouseleave", triggerEnter);
	    $("#co_terminal_prev").bind("mouseenter", triggerEnter).bind("mouseleave", triggerEnter);
	    $("#co_terminal_next").bind("mouseenter", triggerEnter).bind("mouseleave", triggerEnter);
	}

	function get_random() {
		if (last_stack.length == $bannerList.length)
			last_stack = [];

		do {
			pos = Math.random();
			pos *= $bannerList.length;
			pos = Math.ceil(pos);
		} while (jQuery.inArray(pos, last_stack) > -1 || pos == last);

		current = pos;

		last_stack.push(pos);
	}

	function init() {
		get_random();
		$(selector + " .co_" + current).css('display', 'block');
		last = current;
	}

	function show_next_frame()
	{
		if (last < $bannerList.length) {
			current = last + 1;
		} else {
			current = 1;
		}
		$(selector + " .co_" + last).fadeOut(terminal_speed);
		$(selector + " .co_" + current).fadeIn(terminal_speed);

		last = current;
		last_stack = [];
		last_stack.push(current);
	}

	function show_prev_frame()
	{
		if (last > 1) {
			current = last - 1;
		} else {
			current = $bannerList.length;
		}
		$(selector + " .co_" + last).fadeOut(terminal_speed);
		$(selector + " .co_" + current).fadeIn(terminal_speed);

		last = current;
		last_stack = [];
		last_stack.push(current);
	}

	function toggle_startstop()
	{
		if (!startstop_state) {
			stopAnimationInterval();
			startstop_state = true;
			/*'Start'*/
			$('#co_terminal_startstop_icn').attr('src', icn_start);
		} else {
			animate();
			startAnimationInterval();
			startstop_state = false;
			/*'Stop'*/
			$('#co_terminal_startstop_icn').attr('src', icn_stop);
		}
	}

	function animate() {
		if (last != null) {
			$(selector + " .co_" + last).fadeOut(speed);
		}

		get_random();
		$(selector + " .co_" + current).fadeIn(speed);
		last = current;

		//msg(last_stack);
	}

	function startAnimationFade() {
		if ($bannerList.length > 0) {
			init();
			startAnimationInterval();
		}
	}

	if (effect == 'fade') {
		startAnimationFade();
	} else {
		alert(effect + ' not implemented!');
	}
}