var first = true;

jQuery.fn.brandCarousel = function(contentId,stateId,startIndex,interval,delay,fade)
{
	// store state id in container element
	$('#'+contentId).data('stateId',stateId);
	// store slider id in container element
	$('#'+contentId).data('sliderId',$(this).attr('id'));
	// store interval in container element
	if(interval) {
		$('#'+contentId).data('interval',interval);
	} else {
		$('#'+contentId).data('interval',6000);
	}
	// store delay in container element
	if(delay) {
		$('#'+contentId).data('delay',delay);
	} else {
		$('#'+contentId).data('delay',2000);
	}
	// store delay in container element
	if(fade) {
		$('#'+contentId).data('fade',fade);
	} else {
		$('#'+contentId).data('fade',1000);
	}
	// show content
	showContent(contentId,startIndex);
	return this;
}

function cycle(contentId)
{
    var id = $('#'+contentId).data('stateId');
    if(!($('#'+id)).hasClass('pause'))
        return true; 
    return false;
}

function showContent(contentId,activeIndex)
{
    if (cycle(contentId)==true)
    {    
        contentNext(contentId,activeIndex);
        
        slideNext(contentId,activeIndex);
    
        $('#'+contentId).data('actSlide',$('#'+contentId).data('currentContent')+1);
        $('#'+contentId).data('nextContent',$('#'+contentId).data('currentContent')+1);
        
        if($('#'+contentId).data('nextContent')>$('#'+contentId+' ul li').length) 
            $('#'+contentId).data('nextContent',1);
            
        var interval = $('#'+contentId).data('interval')+(first?$('#'+contentId).data('delay'):0);
        first=false;
        
        $('#'+contentId).data('cycleTimer'
            ,setTimeout(
                'showContent("'+contentId+'",$("#'+contentId+'").data("nextContent"));'
                ,interval));
    }
}

function contentNext(contentId,activeIndex)
{
    var fade = $('#'+contentId).data('fade');

    if(!$('#'+contentId).data('currentContent')) 
        $('#'+contentId).data('currentContent',1);
    $('#'+contentId+' ul li:nth-child('+activeIndex+')').fadeIn(fade); // time in milliseconds
    
    if(activeIndex==$('#'+contentId).data('currentContent')) 
        return;
    
    $('#'+contentId+' ul li:nth-child('+$('#'+contentId).data('currentContent')+')').fadeOut(fade); // time in milliseconds
    $('#'+contentId).data('currentContent',activeIndex);
}


// move slider
function slideNext(contentId,activeIndex) 
{
    var sliderId = $('#'+contentId).data('sliderId');
    
	$('#'+sliderId).queue(function(){
	
	    // set li styles
		$('#'+sliderId+' ul li').removeClass('active');
		$('#'+sliderId+' ul li').eq(activeIndex-1).addClass('active');
		
        // set rollover
        $('#'+sliderId+' ul li a img').each(function(i){
            if (i==activeIndex-1) {
                if (!$(this).attr("src").match("mouseover") && !$(this).attr("src").match("active"))
                {
                    // replace the image only if user's mouse is not currently over the image 
                    var src = $(this).attr("src").match(/[^\.]+/) + "_active.gif";
                    $(this).attr("src", src);
                }
                $(this).addClass('active'); // store state in css class
            }
            else {
                var src = $(this).attr("src").replace("_active", "");
                $(this).attr("src", src);
                $(this).removeClass('active'); // store state in css class
            }
        });
        
		$(this).dequeue();
	});
}
