Event.observe(window, 'load', init);
Event.observe(document,'mousemove', function(e){if(img)moveIt(); mouseX=e.pointerX(); mouseY=e.pointerY();});
var elements = new Array();
var products = "";
var mouseX, mouseY;
var img = null;

function init()
{
	components();
	var url = window.location.toString();
	if(url.match(/products\/[a-z]+/g))
		products = url.match(/products\/[a-z]+/g).toString().replace(/products\//,"");
	if(url.match(/\#.*/g))
	{
		var get = url.match(/\#.*/g).toString();
		get = get.replace(/\#/,'');
		request(get);
	}	
}
function components()
{	
	if($('right_page_select'))
		$$('#right_page_select select').each(function(s){s.onchange = sort});
		
	$$('.navigator a').each(function(s){s.onclick = stop;});
}
function stop()
{
	get = this.href.match(/\?.*/g).toString();
	get = get.replace(/\?/,'');
	get = get.replace(/\+/,' ');
	window.location.href = '#'+get;
	request(get);
	return false;
}
function moveIt()
{
	img.style.left = mouseX+10+"px";
	img.style.top = mouseY+10+"px"; 
}
function request(get)
{
	el = $("page_right");
	img = document.createElement("img");
	img.src = BASEURL+"system/application/images/loader.gif";
	img.style.position = 'absolute';
	img.style.left = mouseX+10+"px";
	img.style.top = mouseY+10+"px";
	document.body.appendChild(img);
	img.src = BASEURL+"system/application/images/loader.gif";
	var box = new animation(el)
	box.addAnimation("opacity",1,0,200,10);
	box.animate();
	delay = box.endframe;
	new Ajax.Request(BASEURL+"index.php/ajax/get_products_ajax/"+products, {
 		method: 'get',
 	 	parameters: "?"+get,
  		onSuccess: 	function(transport) 
  					{
  						response = transport.responseText
						setTimeout(inner,delay)
						function inner()
						{
							el.style.height = el.getHeight()+"px";
							el.innerHTML = response;
							el.style.height = '';
							var box = new animation(el);
							box.addAnimation("opacity",0,1,200,10);
							scroll(0,0);
							box.animate();
							components();
							document.body.removeChild(img);
							img = null;
						}
  					}
		});
}
function sort(variable)
{
	if(typeof(variable) == 'object' || typeof(variable) == 'undefined')
	{
		get_name = this.name;
		get_value = this.value;
	}
	else
	{
		arr = variable.split("=")
		get_name = arr[0];
		get_value = arr[1];
	}
	
	var url = window.location.toString();
	if(url.match(/\?.*/g) || url.match(/\#.*/g))
	{
		if(url.match(/\?.*/g))
		{
			var main_get = url.match(/\?.*/g).toString();
			if(main_get.match(/\#.*/g))
			{
				var get = main_get.match(/\#.*/g).toString();
				get = get.replace(/\#/,'');
			}
			else
				var get = main_get.replace(/\?/,'');
		}
		if(url.match(/\#.*/g))
		{
			var main_get = url.match(/\#.*/g).toString();
			var get = main_get.replace(/\#/,'');
		}
			
		var myregexp = new RegExp(get_name+'=[^&]*');
		if(get.search(myregexp)!=-1)
			if(get_value != "")
				link = get.replace(myregexp, get_name+"="+get_value);
			else
				link = get.replace(myregexp, "");
		else
			link = get+"&"+get_name+"="+get_value;
	}
	else
		link = get_name+"="+get_value;
	
	if(get_name=='show' || get_name=='order' || get_name=='filter')
		link = link.replace(/page_num=[\d]+/,"");
		
	link = link.replace(/&+/g,"&");
	link = link.replace(/(^&)|(&$)/g,"");
	
	window.location.href = '#'+link;
	request(link)
}
function dofilter()
{
	var get = "filter=";
	$$('#right_filter input').each(function(s){if(s.checked) get += s.name+":"+s.value+",";});	
	sort(get.replace(/,$/,""));
}
function addToCompare(el)
{		
	if(el.checked)
	{
		for(var j=0; j<elements.length; j++)
			if(elements[j] == el)
				elements.splice(j,1);
	}
	else
	{
		if(elements.length==3)
			elements.shift().checked = false;
	
		elements.push(el);
	}
}
function compare()
{
	if(elements.length<2)
		alert("Моля изберете минимум два продукта за сравнение")
	else
	{
		string='';
		for(var j=0; j<elements.length; j++)
		string += elements[j].value+",";
		
		get = string.replace(/,$/,"");
		window.open(BASEURL+"index.php/main/compare/?items="+encodeURIComponent(get),"Compare","width=800,height=600,resizable=yes,scrollbars=yes");
	}
}