$(document).ready( function() {
    //function doing the switching of items
    function switchTabs(){
        //init variables
        var me = $(this);
        var goNext = !!$(me).attr("id").match("next");
        var goPrev = !!$(me).attr("id").match("prev");
        
        //if next/prev are pressed use the currently selected tab as reference
        if(goNext || goPrev)
            me = $("[class*=_tabselected]");
        
        //init variables
        var myWrapperType = $(me).attr("id");
        myWrapperType = myWrapperType.substring(0, myWrapperType.indexOf('_tab'));
        var myID = $(me).attr("id").substring((myWrapperType + "_tab").length);
        var myItemArr = new Array();
        var willRotate = false;
        var otherMe = null;
        var otherMeID = 0;
        
		$('#' + myWrapperType + '_tabs').children().each(function(){
			if(!!$(this).attr('id').match(myWrapperType + '_tab'))
			{
				myItemArr[myItemArr.length] = this;
			}
		});
		
        //get the next/prev tab
        if(goNext)
        {
            otherMe = $("#" + myWrapperType + "_tab" + (parseInt(myID) + 1));
            if(!$(otherMe).html())
                myID = 1;
            else
                myID = parseInt(myID) + 1;
        }
        else if(goPrev)
        {
            otherMe = $("#" + myWrapperType + "_tab" + (parseInt(myID) - 1));
            if(!$(otherMe).html())
            {
                otherMe = $("*[id*=" + myWrapperType + "_tab]");
                $(otherMe).each(function(){
                    otherMeID = parseInt($(this).attr("id").substring((myWrapperType + "_tab").length));
                    if(otherMeID > 0 && otherMeID > myID)
                        myID = otherMeID;
                });
            }
            else
                myID = parseInt(myID) - 1;
        }
        
        for(var i=0; i<myItemArr.length; i++)
        {
            var currID = (i+1);
            var currItem = $('#' + myWrapperType + '_wrap' + currID);
            var currTab = $('#' + myWrapperType + '_tab' + currID);
            if(myID == currID)
            {
                currItem.addClass(myWrapperType + '_wrapselected');
                currTab.addClass(myWrapperType + '_tabselected');
                (fadeInDuration > 0 ? currItem.fadeIn(fadeInDuration) : currItem.show());
            }
            else
            {
                (fadeInDuration > 0 ? currItem.fadeOut(fadeInDuration) : currItem.hide());
                currItem.removeClass(myWrapperType + '_wrapselected');
                currTab.removeClass(myWrapperType + '_tabselected');
            }
        }
        
        //IE does not support array.indexOf, search manually
        for(var i=0; i<rotateArr.length; i++)
        {
            if(rotateArr[i] == myWrapperType)
                willRotate = true;
        }
        
        if(willRotate)
        {
            //function for doing the fading
            var fadeTime = function()
            {
                var nextID = (myID == myItemArr.length ? 1 : (parseInt(myID)+1));
                var next = $('#' + myWrapperType + '_tab' + nextID);
                
                next.click();
            };
            
            //stop the current switching, and start a new switching on the next item as set above
            switchStop(timerFade);
            switchStart(fadeTime);
        }
        return false;
    }
    
    //stop a specific switch
    var switchStop = function(e){
        clearTimeout(e);
        e = 0;
    };
    
    //begin a switch at some point in the future
    var switchStart = function(e){
        timerFade = window.setTimeout(e, fadeInWait);
    };
	
	
	//$('#mycarousel').jcarousel();
	//$("#mycarousel a").click(function()
	//{
	//	$("#loader").children("img").attr({src: $(this).children("img").attr("src")});
	//	$(".jcarousel_image_heading").html($(this).children("img").attr("alt"));
		//showImage(imageSource);
	//	return false;
	//});
    
    //variables that hold timeout values and timers
    var fadeInDuration = 300;
    var fadeInWait = 9000; 
    var timerFade = 100;
    var rotateArr = ['banner'];
    
    //set the proper actions on the items that need switching/fading
    $('#features_tabs li').click(switchTabs);
    $('#features_tab1').click();
	$('#banner_tabs li').click(switchTabs);
    $('#banner_tab1').click();

});
