/**	Create by olmiha@gmail.com	**/
/**
*	$Id: main.js,v 1.1 2008-03-31 14:59:23 miha Exp $
*/

var PosWinY;
var PosWinX;

Loading = {
	Init : function(e)
	{
		PosWinY = Event.pointerY(e);
		PosWinX = Event.pointerX(e);
	},
	StartLoading : function()
	{
		if($('Loading'))
			Loading.EndLoading();
		var d = document.createElement("div");
		d.setAttribute("id", "Loading");
		d.className = "Loading";
		if(PosWinX)
			d.style.left = (PosWinX + 20) + "px";
		else
			d.style.left = document.body.scrollWidth / 2 - 70 + "px";
		if(PosWinY)
			d.style.top = PosWinY + "px";
		else
			d.style.top = document.body.scrollTop + (document.body.clientHeight / 2) - 40 + "px";

		var text = document.createTextNode("Loading...");
		d.appendChild(text);

		document.body.appendChild(d);
		document.body.style.cursor = "wait";
	},
	EndLoading : function()
	{
		if($('Loading'))
		{
			var d = $("Loading");
			document.body.removeChild(d);
			document.body.style.cursor = "default";
		}
	},
	errors : function(s)
	{
		alert(s);
	},
	ErrorView : function(t)
	{
		alert('Error ' + t.status + ' -- ' + t.statusText);
	},
	processResponse : function(req)
	{
		var i;
		if($('innerblock'))
			i = $('innerblock');
		else if($('inner'))
			i = $('inner');
		if(req.responseText)
		{
			if(i)
			{
				i.innerHTML = '';
				i.innerHTML = req.responseText;
			}
			/*else
				alert(req.responseText);*/
		}
		Loading.EndLoading();
	},
	showDialogDiv: function(txt)
	{
		Loading.StartLoading();
		if($('DialogText'))
			Loading.close('DialogText');

		var d = document.createElement("div");
		d.setAttribute("id", "DialogText");
		d.className = "LoginBlock";
		d.style.width = "300px";

		if(PosWinX)
			d.style.left = (PosWinX + 20) + "px";
		else
			d.style.left = document.body.scrollWidth / 2 - 70 + "px";

		if(PosWinY)
			d.style.top = PosWinY + "px";
		else
			d.style.top = 10 + "px";

		var text = '<table cellpadding="0" cellspacing="0" border="0" style="border: solid 1px #b0b0b2;" width="100%">';
		text += '<tr><td style="background-color: #c0c0c0; height: 26px;" align="right" valign="middle"><img src="/img/admin/red_close.gif" border="0" width="20" height="20" onclick="Loading.close(\'DialogText\');" style="cursor: pointer;""/>';
		text += '</td></tr><tr bgcolor="#ffffff">';
		text += '<td onclick="Loading.close(\'DialogText\');" align="center" style="cursor: pointer;">';
		text += txt;
		text += '</td></tr></table>';

		//var t = document.createTextNode(text);
		//d.appendChild(t);
		d.innerHTML = text;
		document.body.appendChild(d);

		Effect.Grow(d);
		new Draggable('DialogText', {} );

		Loading.EndLoading();
	},
	popup: function(query_str)
	{
		window.open(query_str, '' , 'width=10,height=10,status=no,toolbar=no,menubar=no,scrollbars=yes,location=no,resizable=yes');
	},
	close: function(name)
	{
		if($(name))
		{
			Effect.Shrink($(name));
			//document.body.removeChild($(name));
		}

		if($("MaskBody"))
		{
			document.body.removeChild($("MaskBody"));
		}
	}
}


Cart = {
	ToCountUp: function(f)
	{
		var fi = f.getElementsByTagName("input");
		var total = 0;

		for(var i=0; i<fi.length; i++)
		{
			var di = fi[i].getAttribute("name");
			if(di.match(/^cnt.*/gi))
			{
				var vl = fi[i].value;
				if(!vl.match(/^\d+$/g))
				{
					alert("Error! Not numeric!");
					fi[i].style.border = "1px solid red";
				}
				else
				{
					var id = di.match(/\d+/gi);
					if(id)
					{
						var cena = $('cena' + id).innerHTML;
						cena = this.trim(cena);
						if(cena.match(/^\d+/g))
						{
							//alert(cena);
							var s = vl * cena;
							s = Math.round(s);
							total = Math.round(total + s);
							$('suma' + id).innerHTML = s;
							s = $('suma' + id).innerHTML;
							if(s.indexOf(".") == -1)
								s += ".00";
							else if(s.indexOf(".") == (s-1))
								s += "0";
							$('suma' + id).innerHTML = s;
							$('total').innerHTML = total;

						}
					}
				}
			}
		}

		if(total > 0)
		{
			total = $('total').innerHTML;
			if(total.indexOf(".") == -1)
				total += ".00";
			else if(total.indexOf(".") == (total-1))
				total += "0";
			$('total').innerHTML = total;
		}
	},
	LTrim: function( value )
	{
		// Removes leading whitespaces
		var re = /\s*((\S+\s*)*)/;
		return value.replace(re, "$1");
	},
	RTrim: function( value )
	{
		// Removes ending whitespaces
		var re = /((\s*\S+)*)\s*/;
		return value.replace(re, "$1");
	},
	trim: function( value )
	{
		// Removes leading and ending whitespaces
		return this.LTrim(this.RTrim(value));
	}
}


TextSlide = {
	show: function(id)
	{
		if($(id))
		{
			if($(id).style.display == 'none')
				$(id).style.display = 'block';
			else
				$(id).style.display = 'none';
		}
	}
}


Gallery = {
	shownewwin: function(src, bwidth, bheight)
	{
		window.open(src, '' , 'width='+ bwidth +',height='+ bheight +',toolbar=no,location=no,titlebar=no,scrollbars=yes,status=no,resizable=yes');
	}
}

