var imgs = new Array();
var imgnames = new Array();
var cnt = new Array();
var t = new Array();
var m = new Array();
var changing_thumbs = new Array();
var ids = Array();
var lastVizited=0;

/////////////////////////////////////////////////
$(document).ready(function () 
{
	$(".layer").hide();

	
	var n=0;
	
	
	$(".thumb").each(function(t){
		th_id = this.id;
		ids[n] = th_id.substring(6);
   		n++;
 	});
 	/*
	for(j in ids)
	{
		//alert(ids[j]);
		if(typeof imgnames[ids[j]]==="undefined")
		{
		  	var th_res=getThumbsPath(ids[j]);
		  	var th_array = th_res.split('*');
		  	imgnames[ids[j]]=new Array();
		  	imgs[ids[j]]=new Array();
		  	for(var i=1; i<th_array.length; i++)
		  	{
		  		imgnames[ids[j]][i-1] = th_array[0] + th_array[i];
		  	}
		  	cnt[ids[j]] = imgnames[ids[j]].length;
		}
	}
	*/
	////////////////////////////////////////////////////////////////////
		$('.layer').Draggable(
					{
						zIndex: 	20,
						ghosting:	false,
						opacity: 	0.7,
						handle:	'.layer_handle'
					}
				);	
	////////////////////////////////////////////////////////////////////
});
$('#layer1_form').ajaxForm({
				target: '#content',
				success: function() 
				{
					$("#layer1").hide();
				}				
			});		
function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}			

function xCoo(x)
{
	  var plusx = 0;
	  if( typeof( window.scrollLeft ) == 'number' ) {
	    //Non-IE
	    plusx = window.scrollLeft;
	  } else if( document.documentElement && document.documentElement.scrollLeft) {
	    //IE 6+ in 'standards compliant mode'
	    plusx = document.documentElement.scrollLeft;
	  } else if( document.body && document.body.scrollLeft) {
	    //IE 4 compatible
	    plusx = document.body.scrollLeft;
	  }
	  return plusx + x;
}

function yCoo(y)
{
	  var plusy = 0;
	  if( typeof( window.scrollLeft ) == 'number' ) {
	    //Non-IE
	    plusy = window.scrollLeft;
	  } else if( document.documentElement && document.documentElement.scrollTop) {
	    //IE 6+ in 'standards compliant mode'
	    plusy = document.documentElement.scrollTop;
	  } else if( document.body && document.body.scrollTop) {
	    //IE 4 compatible
	    plusy = document.body.scrollTop;
	  }
	  return plusy + y;
}

function showPopup(id,x,y)
{
	$("#layer"+id).css('left', xCoo(x));
	$("#layer"+id).css('top', yCoo(y));
	$("#layer"+id).show();
	return false;
}

//////////////////////////////////////////////////

function startPreview(path, sc_path, id)
{
	lastVizited=id;
	if(typeof imgnames[id]==="undefined")
	{
	  	var th_res=getThumbsPath(id);
	  	var th_array = th_res.split('*');
	  	imgnames[id]=new Array();
	  	imgs[id]=new Array();
	  	for(var i=1; i<th_array.length; i++)
	  	{
	  		imgnames[id][i-1] = th_array[0] + th_array[i];
	  	}
	  	cnt[id] = imgnames[id].length;
	}
	for(j in ids)
	{
		clearTimeout(t[ids[j]]);
	}
	t[id]=setTimeout("loadimage('"+path+"', '"+id+"', '0'); showimage('"+path+"', '"+id+"', '0');", 400);
	
}


function getThumbsPath(id)
{
	var ret;
	$.ajax({
		type: "GET",
		url: "thumbs_ajax.php",
		async: false,
		data: "id=" + id,
		success: function(msg)
		{
			ret = msg;
		}
	});
	return ret;
}

function isloaded(img) 
{
  if (!img.complete)
    return false;
  if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0)
    return false;
  return true;
}

function showimage(path, id, num)
{
	if(lastVizited==id)
	{
	  if (!isloaded(imgs[id][num]))
	  { 
	    t[id]=setTimeout("showimage('"+path+"', '"+id+"', '"+num+"')", 50);
	    return false;
	  }
	  $('#image-' + id).css('background-image', 'url(' + imgs[id][num].src + ')');
	  if (++num==cnt[id])
	    num=0;
	  loadimage(path, id, num);
	  t[id]=setTimeout("showimage('"+path+"', '"+id+"', '"+num+"')", 500);
	  loadimage(path, id, 0);
	}
}

function loadimage(path, id, num)
{
  if (typeof(imgs[id][num])!='undefined')
    return false;  
  imgs[id][num]=new Image();
  imgs[id][num].src=path + 'image.php?img=' + imgnames[id][num];
 
}
function stopPreview(path, sc_path, id, th)
{
	if(t[id])
	{
		clearTimeout(t[id]);
	  	url = path + 'image.php?img=' + sc_path + th;
	  	$('#image-' + id).css('background-image', 'url(' + url + ')');
	}
	else
	{
		setTimeout("stopPreview('"+path+"', '"+sc_path+"', '"+id+"', '"+th+"')", 100);
	}
}