var slide_timeout = 5000;
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)", 2000);	
        }else{
			setTimeout("cycle_tabs(current_tab_index)", 3000);	
		}
        
    };
	
}

function cycle_tabs(start){
	//alert('start = ' + 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(){
	
	var linkIndex = 0; // make global variable to save value between function calls
	
    $(".rotator_tabs").click(function(){
        var index = $(".rotator_tabs").index(this);
		
		if (current_tab_index == index){ // allow user to click through tab content		
			//alert('linkIndex = ' + linkIndex);	    
			$('#' + $(this).attr('id')  + '_content a').css('display','none'); // hide all links
			$('#' + $(this).attr('id')  + '_content a').css('opacity','0'   ); // hide all links
			
			linkIndex += 1;
			
			if($('#' + $(this).attr('id')  + '_content a:eq(' + linkIndex + ')').css('display') == undefined){
				
				linkIndex = 0; // reset to first link
			}			
						
			$('#' + $(this).attr('id')  + '_content a:eq(' + linkIndex + ')').css('display','block'); // show next link
			$('#' + $(this).attr('id')  + '_content a:eq(' + linkIndex + ')').css('opacity','1'    ); // show next link
					
		}else{			
		   linkIndex = 0; // reset to first link for new tab
           current_tab_index = index;
		   cycle_tabs(current_tab_index);
		}        
    });
    cycle_tabs(current_tab_index);
	
});

