var lookbook_title = new Array();
var lookbook_desc = new Array();
var lookbook_width = 0;

var auto_image_change_interval = 0;
var auto_rotate_time = 5000;

function loadLookBook(category_id,link) {

	$('#imageNumber').hide();
	$('.activeCollections').attr('class','collLink');
	/*var links = document.getElementsByClassName('collLink activeCollections');
	for(var i=0;i<=links.length-1;i++)
	{
		links[i].className = 'collLink';
	}*/
	link.className = 'collLink activeCollections';

	$('#c_gallery').html('');
	//switch container left
	$('#c_gallery').css('left', '115px');
	$('#c_gallery').css('height', '600px');
	$('#loading_preview').hide();
	$("#collection_container").hide();
	req_url = 'index2.php?option=com_collections&task=lookbook&category_id='+category_id;
	$.ajax({
	  url: req_url,
	  success: function(data) {
			data = eval( "(" + data + ")" );
			lookbook_desc = null;
			lookbook_desc = new Array();
			for(var i in data.desc_array){
				lookbook_desc.push(data.desc_array[i]);
			}
			lookbook_title = null;
			lookbook_title = new Array();
			for(var i in data.title_array){
				lookbook_title.push(data.title_array[i]);
			}
			var html_data = String(data.html_content);
			$('#c_gallery').html(html_data);
			lookbook_width = $("#scroll").width();
			setTimeout(function(){initSlider()}, 500);
		}
	});
}
var lookbook_slider_move = -1;
function initSlider() {
	$("#slider").slider({
		max: (lookbook_title.length-1),
		min: 0,
		handle: '#myHandle',
		slide: function(event, ui) {
			//slide event returns value the number we scrolled from, not the one we scrolled to
			var val = $('#slider').slider('value');
			var curr_pos = event.clientX;
			var scrl = -1;
			//if we moved scroll right we come from a bigger value, show val - 1, else +1
			if(curr_pos > lookbook_slider_move) {
				scrl = -lookbook_width * (val+1);
				lookbook_slider_move = curr_pos;
				//console.log("moving right" + scrl);
			}else{
				scrl = -lookbook_width * (val-1);
				lookbook_slider_move = curr_pos;
				//console.log("moving left " + scrl);
			}
			$("#scroll").css("margin-left", scrl + 'px');
		},
		stop: function(event, ui) {
			var val = $('#slider').slider('value');
			//memorize the current handler position we stopped so we know if we slide left or right
			lookbook_slider_move = event.clientX;
			var scrl = -lookbook_width * (val);
			$("#scroll").css("margin-left", scrl + 'px');
		}
	});
	clearInterval(auto_image_change_interval);
	auto_image_change_interval = setInterval(function() { lookbookNext() }, auto_rotate_time);
}

function lookbookPrev() {
	var val = $('#slider').slider('value');
	val = val - 1;
	if(val < 0) {
		val = (lookbook_title.length-1);
	}
	$('#slider').slider('value', val);
	var scrl = -lookbook_width * (val);
	$("#scroll").css("margin-left", scrl + 'px');
}

function lookbookNext() {
	var val = $('#slider').slider('value');
	val = val + 1;
	if(val > (lookbook_title.length-1)) {
		val = 0;
	}
	$('#slider').slider('value', val);
	var scrl = -lookbook_width * (val);
	$("#scroll").css("margin-left", scrl + 'px');
}


