var INTERVAL = 3000; // Milliseconds: 1 sec = 1000 ms.
var clickReady = true;

function trim(str) {
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i > 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}

function trim_px(str) {
	str = str.replace(/px$/, '');
	return str;
}

function default_value(content)
{
	if (trim(content.value) == content.defaultValue)
	{
		content.value = '';
	}
};

function new_value(content)
{
	if (trim(content.value) == '')
	{
		content.value = content.defaultValue;
	}
};

function showNextItem() {
    var ul = $("ul.gallery").css('left');
    ul = trim_px(ul);
    var ulmod = (ul % 300), uladj=0;
    
    if(ulmod != 0)
    {
        uladj= (ul-ulmod);
        
        if(uladj < -3600)
        {
            $('ul.gallery').css('left', '-3900px');
        }
    }
    else
    {
        uladj = ul;
    }
      
    if (uladj < -300)
    {
        $("#PrevFeature").hover(
            function(){
                $(this).css({'color':'#689751','text-decoration':'underline'});
            }, 
            function(){
                var cssObj = {'color':'#96999C','text-decoration':'none'}
                $(this).css(cssObj);
            }
        );
    }

    if (uladj< -3600)
    {
//        $("#NextFeature").css({'color':'rgb(205,203,202)','text-decoration':'none'});
        var first_item = $("ul.gallery li:first").clone();
        $("ul.gallery").append(first_item);
        $("ul.gallery").animate({'left':(uladj-300)}, 500 );
        var wait2a = setTimeout( '$("ul.gallery li:first").remove()', 600);
        var wait2b = setTimeout( "$('ul.gallery').css('left', '-3900px');", 601);
        return;
    }
/*
    if (uladj<=-3900)
    {
        uladj = -3900;
        $("#NextFeature").css({'color':'rgb(205,203,202)','text-decoration':'none'});
        return false;
    }
*/  
    $("ul.gallery").animate({'left':(uladj-300)}, 500 );
    return;   
}

function showPrevItem() {
    var gallery = $("ul.gallery");
    var pos = gallery.css('left');
    pos = trim_px(pos);
    var posmod = (pos % 300), posadj=0;
        
    if(posmod != 0)
    {
        posadj= (pos-posmod);
    }
    else
    {
        posadj = pos;
    }

    if (posadj < -300)
    {
        $("#NextFeature").hover(
            function(){
                $(this).css({'color':'#689751','text-decoration':'underline'});
            }, 
            function(){
                var cssObj = {'color':'#96999C','text-decoration':'none'}
                $(this).css(cssObj);
            }
        );
    }

    if (posadj >= 0)
    {
        posadj = 0;
        $("#PrevFeature").css({'color':'rgb(205,203,202)','text-decoration':'none'});
        return false;
    }

    posadj = (+posadj) + 300;
    $("ul.gallery").animate({'left':posadj}, 500);
    var wait5 = setTimeout( "nextClickReady=true; prevClickReady=true;", 501);
    return;
};

function startAutoPlay() {
    showNextItem();
    window.setStart = setTimeout('startAutoPlay()', (INTERVAL));
}

function stopAutoPlay() {
        clearTimeout(window.setStart);
}

function determineNext() {
    stopAutoPlay();
    if (clickReady)
    {
        clickReady = false;
        showNextItem();
        var wait5 = setTimeout("clickReady=true;",601);
    }
}

function determinePrev() {
    stopAutoPlay();
    if (clickReady)
    {
        clickReady = false;
        showPrevItem();
        var wait5 = setTimeout("clickReady=true;",601);
    }
}

$(document).ready(function(){
    var begin = setTimeout('startAutoPlay()', 1750);
});
