var sponItemsAuto = new Array();
var sponLeftLength;
var sponAutoClick;

jQuery(document).ready(function (){
	
//	jQuery('.spon_box .spon_all .ce_image:last').after(jQuery('.spon_box .spon_all .ce_image').clone());
	
	jQuery('.spon_box .spon_all .ce_image').each(function() {
		sponItemsAuto.push('1');
	});
	
	sponLeftLength = sponItemsAuto.length - 2;
	jQuery('.spon_box .spon_all').css('marginLeft',-1*(115*Math.round(Math.random()*sponLeftLength+1)));
	
	jQuery('#right .box_title').click(function() {
		
		if(jQuery(this).hasClass('open')) {
			//is open, close it
			jQuery(this).parent().find('.box_container').slideUp();
			jQuery(this).removeClass('open');
		} else {
			//is closed, open it
			jQuery(this).parent().find('.box_container').slideDown();
			jQuery(this).addClass('open');			
		}
	});
	
	
	jQuery('.spon_box .spon_arrow_right').click(function() {
		
		window.clearInterval(sponAutoClick);
		temp = parseInt(jQuery('.spon_box .spon_all').css('marginLeft')) - 115;
		if(temp < (-1*(sponLeftLength)*115) ) {
			temp = 0;
		}
		jQuery('.spon_box .spon_all').animate({marginLeft: temp},500);
		
	});
	
	jQuery('.spon_box .spon_arrow_left').click(function() {
		
		window.clearInterval(sponAutoClick);
		temp = parseInt(jQuery('.spon_box .spon_all').css('marginLeft')) + 115;
		jQuery('.spon_box .spon_all').animate({marginLeft: temp},500);		
		
	});
	
	sponAutoClick = window.setInterval("autoPlaySpon()", 2000);
});

function autoPlaySpon() {
	temp = parseInt(jQuery('.spon_box .spon_all').css('marginLeft')) - 115;
	if(temp < (-1*(sponLeftLength)*115) ) {
		temp = 0;
	}
	jQuery('.spon_box .spon_all').animate({marginLeft: temp},500);
}

function needMore(nextVal) {
	var curVal = parseInt(jQuery('.spon_box .spon_all').css('marginLeft'));
	var checkNext = ''; 
	
	jQuery('.spon_box').attr('data-test',jQuery('.spon_box').attr('data-test')+' next: '+(nextVal % ((sponLeftLength)*115))+' cur: '+(curVal % ((sponLeftLength)*115))+' '+checkNext);
	
}

