// JavaScript Document
function compare()
{
	this.last_compare_item="";
	this.last_compare_time=0;
	this.addItem = function(id, title)
	{
		//check time, and add item is clicked twice in 5 sec goto the document.
		var ts = Math.round(new Date().getTime() / 1000);
		if(this.last_compare_item==id&&this.last_compare_time>(ts-10))
		{
			document.location="/li/"+id+"/"+title;
			return;
		}
		this.last_compare_item=id;
		this.last_compare_time=ts;
		
		
		var varclass='odd';
		var parent=$('compare-ul');
		var ni = document.createElement('li');
		var children = parent.childNodes;
		var already_set=false;
		var remove_basic=false;
		var li_id="compare-item-"+id;
		for(var x in children)
		{
			if(typeof(children[x])=="function"){continue;}
			try
			{
				if(children[x].id==li_id)
				{
					already_set=true;
				}
				if(children[x].id=='add-compare')
				{
					remove_basic=true;
				}
			}
			catch(e)
			{
			}
		}
		if(!already_set)
		{
			parent.appendChild(ni);
			ni.innerHTML="Adding...";
			ni.id="compare-item-"+id;
			li_id=ni.id;
		}
		new Ajax.Updater(li_id, '/ajax/compare.php', {
			asynchronous: true,
			method: 'post',
			parameters: 'id='+id+'&action=add'
			});
		if(remove_basic)
		{
			this.remove();
		}
	}
	this.remove = function (id)
	{
		var parent=$('compare-ul');
		var cnt=0;
		for(var x in parent.childNodes)
		{
			if(typeof(parent.childNodes[x])=="function"){continue;}
			try
			{
				if(parent.childNodes[x].id!=undefined)
				{
					cnt++;
				}
			}
			catch(e)
			{
			}
		}
		if(cnt==1)
		{
			var ni = document.createElement('li');
			parent.appendChild(ni);
			ni.id="add-compare";
			ni.innerHTML="Add items to compare";
		}
		if(id!=undefined)
		{
			new Ajax.Request('/ajax/compare.php', {
				asynchronous: true,
				method: 'post',
				parameters: 'id='+id+'&action=remove'
				});
			var el = $('compare-item-'+id);
			el.parentNode.removeChild(el);
		}
		else
		{
			var el = $('add-compare');
			el.parentNode.removeChild(el);
		}
	}
	this.isEven = function(num)
	{
		if (num%2)
		{
			return false;
		}
		else
		{
			return true;
		}
		
	}
	this.images = new Array();
	this.changeImage = function(num)
	{
		for(var i=0; i<9; i++)
		{
			if($('image-num-'+i)==null)
			{
				continue;
			}
			if(num==i)
			{
				$('image-num-'+i).style.display="inline";
				$('cell-'+i).style.border='1px solid #0397d6';
			}
			else
			{
				$('image-num-'+i).style.display="none";
				$('cell-'+i).style.border='none';
			}
		}
	}
	this.changeLargeImage = function(num)
	{
		if(this.images[num]==undefined)
		{
			num = 0;
		}
		this.setBorder(num);
		var value = this.images[num];
		var next = num+1;
		$('item-detail').style.display="none";	
		$('item-image-large').style.display="block";
		var html = "<a href='javascript:void(0);' onclick='compare.changeItemDetail();' style='float:right;'>Close <img src='/images/delete.gif' alt='X' /></a>";
		html+="<a href='javascript:void(0);' onclick='compare.changeLargeImage("+next+");'><img src='/show_image.php?a="+value+"' alt=''></a>";
		$('item-image-large').innerHTML=html;
	}
	this.changeItemDetail = function ()
	{
		$('item-detail').style.display="block";	
		$('item-image-large').style.display="none";
	}
	this.setBorder = function(num)
	{
		for(var i=0; i<9; i++)
		{
			if(parseInt(i)==parseInt(num))
			{
				//$('cell-'+i).style.backgroundColor="#0397d6";
				$('cell-'+i).style.border='1px solid #0397d6';
			}
			else
			{
				//$('cell-'+i).style.backgroundColor="#FFFFFF";
				$('cell-'+i).style.border='none';
			}
		}
	}
}

var compare = new compare;

