// Easing equation, borrowed from jQuery easing plugin
// http://gsgd.co.uk/sandbox/jquery/easing/
jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

var time = 9000;
var curTimer = null;
var CurPos = 1;
var MoveLeft = true;

jQuery(function( $ ){
    $('#slide_bar_hidden').show();
	var my_scroll = $('#slideshow');
    if (my_scroll.length) {
        my_scroll.serialScroll({
    		items:'li',
    		prev:'#prev',
    		next:'#next',
    		offset: 0, //when scrolling to photo, stop 230 before reaching it (from the left)
    		start:0, //as we are centering it, start at the 2nd
    		duration:3000,
    		force:true,
    		stop:true,
    		lock:false,
    		cycle:true, //don't pull back once you reach the end
    		easing:'easeOutQuart', //use this easing equation for a funny effect
    		jump: false, //click on the images to scroll to them
            
            onBefore:function( e, elem, $pane, $items, pos ){
                CurPos = pos;
            },
            onAfter:function( e ){
            }
    	});
    }
    
    $('.auto-hint').focus(function(){
        if($(this).attr('title') == $(this).val()) $(this).val('');
    });
    $('.auto-hint').blur(function(){
        if($(this).val() == '') $(this).val($(this).attr('title'));
    });
 
    var countItems = $('li.item').length;
    
    $('li.item').each(function() {
        var str_w = $('#slideshow').width();
        var new_width = ((parseInt(str_w) / 2) - 10) + 3;
        $(this).width(new_width+'px');
    });
    
    window.onresize = function() {
        $('li.item').each(function() {
            var str_w = $('#slideshow').width();
            var new_width = ((parseInt(str_w) / 2) - 10) + 3;
            $(this).width(new_width+'px');
            
        });
    };
    
    $('#next').click(function(){
        clearTimeout(curTimer);
        curTimer = setTimeout(function() {
            if (MoveLeft) $('#next').click()
            else $('#prev').click();
            
            if (CurPos == 0) MoveLeft = true;
            else if(CurPos >= (countItems-1)) MoveLeft = false;
        }, time)
    });
    
    $('#prev').click(function(){
        clearTimeout(curTimer);
        curTimer = setTimeout(function() {
            if (MoveLeft) $('#next').click()
            else $('#prev').click();
            
            if (CurPos == 0) MoveLeft = true;
            else if(CurPos >= (countItems-1)) MoveLeft = false;
        }, time)
    });
    
//    curTimer = setTimeout(function() {
//            if (MoveLeft) $('#next').click()
//            else $('#prev').click();
//            
//            if (CurPos == 0) MoveLeft = true;
//            else if(CurPos >= (countItems-1)) MoveLeft = false;
//    }, time)


});














