// General functions

function ACMSobj()
{
	this.imageCount = 0;
	
	this.imageStartPos = 0;
	this.imagesPerPage = 2;

    this.submitForm = function submitForm(form_name)
    {
    	document.getElementById(form_name).submit();	
    }
    
    this.findPosX = function(obj)
	{
	  var curleft = 0;
	  if(obj.offsetParent)
	      while(1) 
	      {
	        curleft += obj.offsetLeft;
	        if(!obj.offsetParent)
	          break;
	        obj = obj.offsetParent;
	      }
	  else if(obj.x)
	      curleft += obj.x;
	  return curleft;
	}

	this.findPosY = function(obj)
	{
		var curtop = 0;
		if(obj.offsetParent)
			while(1)
			{
				curtop += obj.offsetTop;
				if(!obj.offsetParent)
					break;
				obj = obj.offsetParent;
			}
		else if(obj.y)
		curtop += obj.y;
		return curtop;
	}
	
	this.loadImageTags = function(){
		$.post("/admin/assets/images/feeds/get-tags/",{},
		function(xml){
			var output = "";
			$("/response/tag", xml).each(function(){
				_tag = $("tag", this).text();
				_count = $("count", this).text();
				output = output + '<li><a href="javascript:ACMS.loadImagesForTag(\''+_tag+'\',0);">'+_tag+' ('+_count+')</a></li>';
			});
		  	$("ul#tags").html(output);
		});
	}
	
	this.loadImagesForTag = function(tag,startPos){
		this.imageStartPos = parseInt(startPos);
		page_end = this.imageStartPos + this.imagesPerPage;
		page_start = this.imageStartPos;
		page_size = this.imagesPerPage;
		$.post("/admin/assets/images/feeds/load-images/?tag="+tag+"&start="+page_start+"&limit="+this.imagesPerPage,{},
		function(xml){
			var output = "";
			$("/response/image", xml).each(function(){
				_imagename = $("imagename", this).text();
				_imagedescription = $("imagedescription", this).text();
				_imagefile = $("imagefile", this).text();
				_width = $("width", this).text();
				_height = $("height", this).text();
				output = output + '<div id="image_preview"><img src="/admin/image-preview/?file='+_imagefile+'&width=100" alt="'+_imagedescription+'" title="'+_imagedescription+'" /></div>';
			});
			
			$("/response/count", xml).each(function(){
				total_images = $("total", this).text();
			});
			var navigation = "";
			
			if(this.page_start != 0){
				navigation = navigation + "<a href=\"javascript:ACMS.loadImagesForTag('"+tag+"',"+(page_start-page_size)+")\"><img src=\"/static/images/admin/buttons/previous.png\" alt=\"Previous\"></a>&nbsp;";	
			}
			if(page_end<total_images){
				navigation = navigation + "<a href=\"javascript:ACMS.loadImagesForTag('"+tag+"',"+page_end+")\"><img src=\"/static/images/admin/buttons/next.png\" alt=\"Next\"></a>";	
			}
			//if(page_end<total_images){
				//page_end = '<a href="javascript:ACMS.loadImagesForTag(\''+tag+'\','+page_end+')">Next</a>';	
			//}
			$("div#image-total").html("Images tagged with <strong>"+tag+"</strong>: "+total_images+" in total");
			$("div#image-navigation").html(navigation);
		  	$("div#image-list").html(output);
		});
	}

}

function paintTableStripes(){
	$("#zebra tr:nth-child(even)").addClass("alt");
}

var ACMS = new ACMSobj();