var currentimage = 0;
var maximage;
var pause = 5000;
var timerins = 700;
var timerout = 300;
var autoplay = true;

$(document).ready(function () {
	// *** CAROUSEL *** //
	if ($(".gallery").length > 0) {
	    var imgs = $('.gallery .item');
	    maximage = imgs.size()-1;
	
	    //create links
	    generate_links();
	    $('.gallerylinks li a.num').click(function(){
	        autoplay = false;
	        currentimage = parseInt($(this).html())-1;
	        move_image(timerins);
	        return false;
	    });
	    $('.gallerylinks li a.leftright').click(function() {
	        autoplay = false;
	        if ($(this).html() == 'left') {
	           currentimage -= 1;
	           if (currentimage < 0) {
	               currentimage = 0;
	           }
	       } else {
	            currentimage += 1;
	            if (currentimage >= maximage) {
	                currentimage = maximage;
	            }
	       }
	       move_image(timerins);
	       return false;
	    });
	
	    move_image(1); // ie - NOW! - initial image
	
	    //set playing - but only if a manual click hasn't happened'
	    if (autoplay == true) {
	        setTimeout(function() { auto_play() }, pause);
	    }
	}

	// *** TERMS POP UP *** //
	$('.toggleterms').click(function(){
		$('.termsofuse').toggle();
	});
	$('.toggleprivacy').click(function(){
		$('.privacy').toggle();
	});

	// *** CLIENT LOGO *** //
	$(".ourcustomers ul").css({
		"position": "relative",
		left: 0
		
	});
	
	$int = setInterval("move_customers('right', true)", 5000);
	
	$(".ourcustomers .moveright").click( function(e) {
		e.preventDefault();
		move_customers("right", false);
	});
	$(".ourcustomers .moveleft").click( function(e) {
		e.preventDefault();
		move_customers("left", false);
	});

});

function move_customers($direction, $auto) {
	$slidesperclick = 3;
	$slidewidth = $(".ourcustomers ul li").width() + 1;
	$clickdistance = $slidewidth * $slidesperclick;
	
	$left = parseFloat($(".ourcustomers ul").css("left"));
	$ulwidth = parseFloat($(".ourcustomers ul").width());
	
	$newdirection = $direction;
	$animate = true;
	
	switch ($direction) {
		case "left":
			if ($left <= -$clickdistance) {
				$left = '+=' + $clickdistance + 'px';
			} else if ($auto) {
				$left = '-=' + $clickdistance + 'px';
				$newdirection = 'right';
			} else {
				$animate = false;
			}
		/*
		
			if ($left > -$clickdistance) {
				$left = -($ulwidth - $clickdistance) + "px";
			} else {
				$left = '+=' + $clickdistance + 'px';
			}*/
			break;
		case "right":
			if (-$left < ($ulwidth - $clickdistance)) {
				$left = '-=' + $clickdistance + 'px';
			} else if ($auto) {
				$left = '+=' + $clickdistance + 'px';
				$newdirection = 'left';
			} else {
				$animate = false;
			}
			break;
	}
	if ($animate) {
		$(".ourcustomers ul").animate( { left: $left }, 500 )
	}
	
	clearInterval($int);
	if ($auto) {
		$int = setInterval("move_customers('" + $newdirection + "', true)", 5000);
	} else {
		$int = setInterval("move_customers('right', true)", 15000);
	}
	
}

function generate_links()
{
    li_list = '';
    for (i = 1; i <= maximage+1; i++) {
        li_list += '<li class="nums"><a href="#" class="num" title="Go to slide '+(i)+'">'+(i)+'</li>';
    }
    $('.gallery_links').append('<ul class="gallerylinks"><li><a class="left leftright" href="#">left</a></li>' + li_list + '<li><a class="right leftright" href="#">right</a></li></ul>');
}

function auto_play()
{
    if (autoplay) {
        currentimage++;
        if (currentimage > maximage) currentimage = 0;
        move_image(timerins);
        setTimeout(function() { auto_play() }, pause);
    }
}


function move_image(timerins)
{

    $('.gallery').scrollTo($('.gallery .galleryholder .item:eq('+ currentimage +')'), timerins);
    update_links();
}

function update_links() {
    $('.gallerylinks a.num').removeClass('on');
    $('.gallerylinks a.num:eq('+currentimage+')').addClass('on');
}
