var t=0;
var prevClick=-1;
var thumbsrc;
var flag=-1;


function changeFoto(ind) {
	if (t==0) {
		if (ind>0) $(".items img").filter(":eq("+(ind-1)+")").removeClass("showing");
		$(".items img").filter(":eq("+ind+")").addClass("showing").click();
		ind++;
		if (t==0) setTimeout ( "changeFoto("+ind+")", 5000 );
	}
}

function toggle_info(elem) {
	if (elem.style.display=='' ) {
		elem.style.display='block';
	}
	else 
		elem.style.display='';
}

function ajax_info(from,clickIndex,prevClick) {
		
		//alert(prevClick);
		src = $(from + " .foto img").attr('src');
		//thumbsrc = 'assets/snippets/phpthumb/phpThumb.php?src=/'+src+'&w=559&h=434&q=100&zc=1';
		//alert(thumbsrc);
		//image loading
		$("#main_promo").hide();
		if ($("#main_promo").html()=="") {
			$("#main_promo").append("<img width='559' height='434' />"); 
		}
		
		var img = new Image();
		$(img).load(function(){ 
			//alert("loaded");
			$("#main_promo img").attr('src',src);
			$("#main_promo").fadeIn(500);
			if (prevClick<0) {
				//alert("start show");
				setTimeout ( "changeFoto(1)", 5000 );
			}
		}).attr('src',src);
		
		$("#left_promo_heading").empty();

		//text loading
		var title = $(from + " .title").text();
		$('#left_promo_heading').append(title + ' <img src="images/right.png" alt="" />');
		$("#left_promo_text").empty();

		var annot = $(from + " .annotation").text();
		//alert(annot);
		//alert(document.getElementById('annotation').innerHTML);
		if (annot.length>350) annot = annot.substr(0,340) + '...';
		$("#left_promo_text").append(annot);
		var url = $(from + " .url").text();
		$("#left_promo_more a").attr('href',url);
		
		//cache save
		var id_cache = $(".items img").filter(":eq("+(clickIndex+1)+")").attr("alt");
		$('#cache').load('index.php?id=35&doc_id='+id_cache,
	  		function(){
				$('#cache').append('<div id="id_cache">'+id_cache+'</div>');
	  		});	
}

$(function() { 
 
 	var ind=0;
    // initialize scrollable 
    //var api = $("div.scrollable").scrollable({ size: 7, api:true }).mousewheel(); 
	var api = $("div.scrollable").scrollable({api:true, size:7}); 
	$("div.scrollable").mousewheel();
	$("body").append("<div id='ajax'></div>");
	$("body").append("<div id='cache'></div>");
	$("#ajax").hide();
	$("#cache").hide();
	
 
	$("#left_promo_nav a").click(function() { 
		 $(".items img").filter(":eq("+(api.getClickIndex()-1)+")").click();
	});	 
 
 	$("#left_promo_nav_button a").click(function() { 
		 $(".items img").filter(":eq("+(api.getClickIndex()-1)+")").click();
	});	
	
	$("#right_promo_nav a").click(function() { 
		 $(".items img").filter(":eq("+(api.getClickIndex()+1)+")").click();
	});	 
 
 	$("#right_promo_nav_button a").click(function() { 
		 $(".items img").filter(":eq("+(api.getClickIndex()+1)+")").click();
	});	
 
 	$(".items img").click(function() { 
	flag = prevClick;
	var clickIndex = api.getClickIndex();
	if (clickIndex==prevClick) return false;
	prevClick = clickIndex;
	
	if (!$(this).hasClass("showing")) t=1;
	var id = $(this).attr("alt");
	$("#main_promo").fadeOut(200);	
	//checking cache
    if ($("#cache #id_cache").text()==id) {
		ajax_info("#cache",clickIndex,flag); //alert("cache");
	}
	else { 
		$('#ajax').load('index.php?id=35&doc_id='+id, function(){ ajax_info("#ajax",clickIndex,flag); });
		//alert("ajax");
	}


	//$('#main_promo').load('index.php?id=35&doc_id='+id+' #foto');
	/*var wrap = $("#main_promo_image").fadeOut(200); 
	var img = new Image(); 
	$(img).load(function(){
		$('#main_promo_image').remove();
        $(this).hide();
        
        $('#main_promo').append(this);
		$(this).fadeIn(200);
	}).attr('src',url).attr('id','main_promo_image');
	*/
	});

 		$(".items img").filter(":first").addClass("showing").click();
 
});
