$(document).ready(function() {
						   
	var elements_count = $("#slider > ul").children("li").length;
	$("#sliderControls .slide-count").text(elements_count);
	
	var content = '<li class="slide"><ul>';
	for(var i = 0; i < elements_count; i++) {
		if (i % 9 == 0 && i != elements_count - 1 && i != 0) {
			content += '</ul></li><li class="slide"><ul>';
		}
		content += '<li>' + $("#slides-1 li:eq(" + i + ")").html() + '</li>';
		if (i == elements_count - 1) {
			content += '</ul></li>';
		}
	}
	
	$("#slides-1").html(content);
	
	$(".controls-1 .control").click(function() {
		function isInteger(s) {
		  return (s.toString().search(/^-?[0-9]+$/) == 0);
		}
		var elements_count = $("#slides-1").children("li").length;
		var element_width = $("#slides-1").children("li").width();
		var margin = parseInt($("#slides-1").css('marginLeft'));
		var current_page = (-1) * margin / element_width;
		var total_elements = parseInt($("#sliderControls .slide-count").text());
		if (!isInteger(current_page)) { return ; }
		if ($(this).hasClass('prev')) {
			if (current_page != 0) {
				first = (current_page - 1) * 9 + 1;
				last = current_page * 9;
			} else {
				first = Math.floor(total_elements / 9) * 9 + 1;
				last = total_elements;
			}			
		} else {
			if (current_page != Math.floor(total_elements / 9)) {
				first = (current_page + 1) * 9 + 1;
				last = ((current_page + 2) * 9 < total_elements) ? (current_page + 2) * 9 : total_elements;
			} else {
				first = 1;
				last = 9;
			}
		}
		$("#sliderControls .slide-first").text(first);
		$("#sliderControls .slide-last").text(last);
	});
	
	$(".controls-2 .control").click(function() {
		function isInteger(s) {
		  return (s.toString().search(/^-?[0-9]+$/) == 0);
		}
		var elements_count = $("#slides-2").children("li").length;
		var element_width = $("#slides-2").children("li").width();
		var margin = parseInt($("#slides-2").css('marginLeft'));
		var current_page = (-1) * margin / element_width;
		
		//alert(elements_count);
		if (!isInteger(current_page)) { return ; }
		var total_elements = parseInt($("#sliderControls .slide-count").text());
		
		if ($(this).hasClass('prev')) {
		if (current_page != 0) {
			first = current_page;
		} else { first = total_elements; }
		} else {
		if (current_page + 1 != total_elements) {
			first = current_page + 2;
		} else {
			first = 1;
		}	
	}
	//alert(current_page);
	$("#sliderControls .slide-first").text(first);
	//$("#sliderControls .slide-last").text(last);
	});	
	
	jQuery.fn.delay = function(time,func){
		this.each(function(){
			setTimeout(func,time);
		});		
		return this;
	};

	$("#slider li a").hover(
		function () {
			$(this).append("<span class='hover'><p><strong>"+$(this).children("img").attr("alt")+"</strong></p></span>");
		}, 
		function () {
			$(this).find(".hover").remove();
		}
	);

	$(".controls-1").jFlow({
		slides: "#slides-1",
		controller: ".slide",
		prev: ".prev",
		next: ".next",
		easing: "swing",
		duration: 600,
		width: "588px",
		height: "372px"
	});

	$(".controls-2").jFlow({
		slides: "#slides-2",
		controller: ".slide",
		prev: ".prev",
		next: ".next",
		easing: "swing",
		duration: 600,
		width: "748px",
		height: "580px"
	});

	$("#info .control").click(function () { 
		$(this).parent().children('.popup').animate({
				width: 'toggle'
			}, 200);
		$(this).delay(300, function() {
			$("#info .close").animate({
				opacity: 'toggle'
			}, 400);
		});
    });
	
	$("#info .close").click(function () { 
		$("#info .popup").animate({
				width: 'toggle'
			}, 200);
		$("#info .close").animate({
				opacity: 'toggle'
			}, 400);
    });
	
	var config = {    
		sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
		interval: 150, // number = milliseconds for onMouseOver polling interval    
		over:  function () {
			$(this).children('.popup').animate({
				width: 'toggle'
			}, 500);
		},  // function = onMouseOver callback (REQUIRED)    
		timeout: 500, // number = milliseconds delay before onMouseOut    
		out: function () {
			$(this).children('.popup').animate({
				width: 'toggle'
			}, 500);
		}    
	};
	
	$('#projectList').hoverIntent(config);

});
