// JavaScript Document
var CURIND=0;
var myTimer = {};

jQuery.timer = function(time,func,callback){
	var a = {timer:setTimeout(func,time),callback:null}
	if(typeof(callback) == 'function'){a.callback = callback;}
	return a;
};

jQuery.clearTimer = function(a){
	clearTimeout(a.timer);
	if(typeof(a.callback) == 'function'){a.callback();};
	return this;
};


jQuery.preloadImages = function()
{
	return;
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

 // The plugin
        $.fn.image = function(src, f){
        return this.each(function(){
                var i = new Image();
                        i.src = src;
                        i.onload = f;
						i.style.display="none";
						
						   
                        this.appendChild(i);
                });
        }

function getFid(id){
	 for(var i=0;i<FILUT.length;i++){
		 	if(FILUT[i].id==id){
				CURIND=i;
			return FILUT[i];	
			}
	 }
				  }
function bindThumbs(){
  for(var i=0;i<FILUT.length;i++){
	
		var gal=FILUT[i];
		  $.preloadImages(gal.url);
		$("#"+gal.id).click(function(){
				var me=getFid($(this).attr("id"));
				$("#forceheight").hide();
				$("#left").hide();
				$("#content .cleft").hide();
				$("#right").hide();
				 $("#allgal").remove();
				 var curri="";
				 if(FILUT.length!=1){
				 curri="&nbsp;("+(CURIND+1)+"/"+FILUT.length+")";
				 }
				$("#content").append("<div id='allgal'><h2>"+me.topic+""+curri+"</h2><div id='close'>SULJE</div><div id='gally_img' style='width:100%;height:"+me.height+"px;' > <div style='text-align:center;padding-top:"+(Math.round(me.height/2-20))+"px;' id='preloadia'>Ladataan</div></div></div>");
				
				
				 $("#gally_img").image(me.url,function(){
      // $("#preloadia").remove();
	  // $("#gally_img > img").show();
	
	   myTimer = $.timer(800,function(){

      // Display hello message when timer goes off
      $("#preloadia").remove();
	 $("#gally_img > img").show();
 $.clearTimer(myTimer);

	
    // Optional function to call when timer is canceled

									   });
        }); 
				  
				$("#close").click(function(){
									$("#left").show();
									$("#content .cleft").show();
								$("#right").show();		
								$("#forceheight").show();
			$("#allgal").remove();
											});
				 
				$("#allgal").click(function(){
								if(CURIND+1>=FILUT.length){
									CURIND=0;
									if(FILUT.length==1){
									return false;	
									}
								}else{
										CURIND++;
										
								}
								$("#"+FILUT[CURIND].id).click();
								
								//	$("#left").show();
								//$("#right").show();		
					//$("#allgal").remove();
											});
		});
  }
}
$(document).ready(function(){
						   
	bindThumbs();		
	
	
	$(".tblock h2").click(function(){
				if($(this).parent().hasClass("isopen")){
						$(this).parent().removeClass("isopen");
				$(this).parent().addClass("bhidden");	
				}else{
					$(this).parent().parent().find(".tblock").removeClass("isopen").addClass("bhidden");			   
				$(this).parent().removeClass("bhidden");
				$(this).parent().addClass("isopen");	
				
				}
								   });
 });
