var slide_timeout = 3000;
var current_tab_index = 0;

function select_tab() {
    var tab_index = $(".rotator_item_container").index(this);
	 $(".rotator_selected").removeClass("rotator_selected");
    $(".rotator_tabs:eq(" + tab_index + ")").addClass("rotator_selected");
}

function switch_if_last() {
    var current_slide = $(":visible", this).parent("a");
    var last_slide = current_slide.parent().children("a:last-child")
    if (current_slide.children("img").attr("src") == last_slide.children("img").attr("src") && current_slide.children("img").attr("src") != undefined) {
        current_tab_index += 1;
        if (current_tab_index > 2) {
            current_tab_index = 0;
        };
        setTimeout("cycle_tabs(current_tab_index)",slide_timeout-1000);
    };
}

function cycle_tabs(start) {
	 $("#rotator_content").cycle({
		  timeout: 0,
        startingSlide: start,
        before: select_tab
	 });

	 $(".rotator_item_container").cycle({
		  timeout: slide_timeout,
        after: switch_if_last
	 });
}

$(document).ready(function(){
    $(".rotator_tabs").click(function() {
        var index = $(".rotator_tabs").index(this);
        current_tab_index = index
        cycle_tabs(current_tab_index);
    });
        
	 cycle_tabs(current_tab_index);
});
