
	// funkcija eventams prisegti
function ets_addEvent(obj,event_name,callback)
{
	
	if ( document.addEventListener ) {		
		obj.addEventListener( event_name, callback, false );	
	// jeigu IE
	} else if ( document.attachEvent ) {
		obj.attachEvent( "on" + event_name, callback );	
	}	
}

	// tooltips

var ets_magnifier  = {
	settings : {		
		offX : 15, 
		offY : 15,
		checkIn : new Array('a'),
		_offsetX : 2,
		_offsetY : 2,		
		_tooltip : null,
		_node : null,
		_loader : null,
		_className : null
	},
	tempas : null,
	set : function (className){
		// nustatome offseta tooltipo
		var pat= new RegExp(className,"i");
		if(!$$('magnifierBlock')){
			this.tempas = document.createElement("div");			
			this.tempas.className = "magnifier";	
			this.tempas.id = 'magnifierBlock';
			this.tempas.style.display = 'none';
			this.tempas.innerHTML = '<div id="magn-content" ></div>';			
			document.body.appendChild(this.tempas);
	    }
		this.settings._offsetX = this.settings.offX;
		this.settings._offsetY = this.settings.offY;		
		// nuskaitome elementus
		for(var i = 0; i < this.settings.checkIn.length; i++)
		{
			var elements = document.getElementsByTagName(this.settings.checkIn[i]);
			for(var j = 0; j < elements.length; j++)
			{
				var element = elements[j];
				if(pat.test(element.className) && element.getAttribute('rel'))
				{							
					this.settings._className = className;
					var self = this;
					ets_addEvent(element,'mouseover',function(event) {self._over(this,event)});
					ets_addEvent(element,'mouseout',function(event) {self._out(this)});
					ets_addEvent(element,'mousemove',function(event) {self._move(event)});
				}										
			}
		}				
	},
	_over : function(a,e)
	{				
		
		var pat= new RegExp(this.settings._className,"i");
		a = (window.event) ? window.event.srcElement : a;
		while(!pat.test(a.className))
		{			
			a = a.parentNode;			
			if(a.tagName == 'BODY')
				break;
		}
		if(!a)
			return false;
		
		if(!this.settings._tooltip)
		{			
			
			this.settings._tooltip = a.rel ? a.rel : a.getAttribute('rel');			
			var image = new Image();
			var self = this;
			var XY = this._getCursorPos(e).split('x');				
		
			this.tempas.style.top = XY[1] + "px";
			this.tempas.style.left = XY[0] + "px";
			this.tempas.style.display = 'block';				
			image.onload = function(){ 				
				var obj = document.getElementById('magn-content');
				obj.style.backgroundImage = 'url(/' + self.settings._tooltip + ')';				
			}
			image.src = '/' + this.settings._tooltip;
		}
	},	
	_out : function(a)
	{
		a = (window.event) ? window.event.srcElement : a;			
		if(!a)
			return false;
		
		if(this.settings._tooltip)
		{
			this.tempas.style.display = 'none';
			document.getElementById('magn-content').style.backgroundImage = '';
			this.settings._tooltip = null;
		}
	},
	_getCursorPos : function(e)
	{		
		var x = (!window.event) ? e.pageX : window.event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
		var y = (!window.event) ? e.pageY : window.event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);		
		x += this.settings._offsetX;
		y += this.settings._offsetY;		
		return x + "x" + y;				
	},
	_move : function(e)
	{
		if(this.settings._tooltip)
		{
			var XY = this._getCursorPos(e).split('x'),				
				iebody = ( document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body,
				scrolledDown = document.all ? iebody.scrollTop : pageYOffset,							
				window_w = document.all ? iebody.clientWidth : window.innerWidth,
				window_h = document.all ? iebody.clientHeight : window.innerHeight;	
			
			var horizontal_pos = XY[0];
			if( ( parseInt(XY[0]) + 200) >= (window_w - 30) ){
				horizontal_pos = XY[0] - 30 - 200;
			}
			
			var vertical_pos = XY[1];					
			if((parseInt(XY[1]) - scrolledDown + 200) >= (window_h - 10) ){						
				if( XY[1] - scrolledDown - 30 - 200 > 0 )					
					vertical_pos = XY[1] - 30 - 200;			
			}			
		
			this.tempas.style.top = vertical_pos + "px";
			this.tempas.style.left = horizontal_pos + "px";			
		}
	}
}
/////// funkcija nuotraikai keisti //////
// path - norimos nuotraukos URL
// w - plotis, h - aukstis, id - IMG elementas kuriam bus priskirti nustatymai

function eshopChangeImg(path, opath, w, h, alt)
{
	$$("main-pic").style.backgroundImage = 'url('+ path +')';
	//$$("main-pic-a").onclick=Function("return jbd('/admin/bg/eshop_showimg?id="+id+"',"+wo+","+ho+");");
	$$("main-pic").href = opath;
	$$("main-pic").style.width=w+'px';
	$$("main-pic").style.height=h+'px';
}

/////////////////////////////////////////////////////////////////////
/////// funkcija uzslepti / rodyti bloka ( priskiriama checkboxui )//
/////////////////////////////////////////////////////////////////////
function eshopBlockVis(button,id){
	var block = document.getElementById(id);
	if(button.checked == true){
		block.style.display = "block";
	}else{
		block.style.display = "none";
	}
}
/////////////////////////////////////////////////////////////////////
///// funkcija nustato pradzioje ar rodyti blockus ar ne       //////
/////////////////////////////////////////////////////////////////////
function eshopSetBlockVis(cid,id){
	var check = document.getElementById(cid);
	var block = document.getElementById(id);
	if(check.checked == true){
		block.style.display = "block";
	}else{
		block.style.display = "none";
	}
}

// funkcija iskviecia popup langa su slinkimo juosta kurio plotis ow, aukstis oh, turinio adresas href
// naudojama mygtukuose onClick actionuose
function jbd_s( href,ow,oh )
{
	var dw = document.body.clientWidth;
	var dh = document.body.clientHeight;
	var x = (dw - ow)/2;
	var y = (dh - oh)/2;

	var ww = window.open( href,'','width='
		+ ow + ',height=' + oh + ',toolbar=no,left=' + x + ',top=' + y + ','
	    + 'location=no,directories=no,personalbar=no,status=no,menubar=no,scrollbars=yes,titlebar=no,'
	    + 'resizable=yes,dependent=yes,dialog=yes,close=no,chrome=yes'
	);
	if( !ww ) {
		alert( 'Popup langas buvo blokuotas. Prašome pakeisti nustatymus' );
		return false;
	} // if
	ww.focus();

	return false;
}

// onclick prideti
function addone(qt)
{
	if (!isNaN(qt.value))
		qt.value++;
	else
		qt.value=1;
	//qt.select();

}
// onclick atimti
function minusone(qt)
{
	if (!isNaN(qt.value) && qt.value>1)
		qt.value--;
	//qt.select();
	return false;
}
// onchange ( ant selecto ) is assoc masyvo ( input_name => input_value )
// gauna informacija ir sugruda i visus inputus kurie apibrezti masvye
function eshopDynamicOptions(obj,array)
{
	var index = obj.options[obj.selectedIndex].value;
	if(index)
	{
		for(i=0; i < array[index].length; i++)
		{

			var name = array[index][i][0];
			if($$$(name).length)
			{
				$$$(name).item(0).value = array[index][i][1];
			}		
		}
	}
}

var search_q = "";
var got_q = "";

function key(url,e)
{	
	if(je(e)){
		setTimeout('pressed_enter(\''+url+'\')',10);
		return false;
	}else{
		setTimeout('search_q = $$(\'eshop_search\').value',10);
	}
}
function pressed_enter(url)
{	
	got_q = $$('eshop_search').value;
	if(got_q != search_q){
		search_q = got_q;	
	}else{
		jg(url);		
	}

}

function show_login(obj)
{
	if($('#login-box').css('display') == 'none')
	{
		$('#login-box').slideDown();

		obj.style.cursor = 'default';
	}
}
function showSubs(caller)
{
	var opened = new Array();
	for(var i = 1 ; i <= 5; i++)
	{
		if($('.open-'+i).length)
			opened.push('open-'+i);
	}
	if(id = caller.id)
	{	
		var ok = true;
		
		var _open = true;
		for(var i = 1; i <= 5; i++)
		{
			if($('#'+id).hasClass('open-'+i))
			{
					
				_open = false;
			}
		}
		
		var classes = caller.parentNode.parentNode.className.split(' ');
		var parent = null;
		for(var i in classes)
		{
			i = Math.round(classes[i]);
			if(!isNaN(i) && i > 0)
			{
				parent = i;
				break;
			}	
		}
		
		if(parent)
		{
			
			for(var i = opened.length-1; i > 0; i--)
			{
				var oldId = $('.'+opened[i]+':eq(0)').attr('id');
				$('.'+opened[i]).removeClass('sel');
				$('.'+opened[i]).removeClass(opened[i]);
				$('.'+oldId).removeClass('sel');
				$('.'+oldId).slideUp();	
				
			}							
		}
		else
		{

			for(var i in opened)
			{
				
				var oldId = $('.'+opened[i]+':eq(0)').attr('id');

				$('.'+opened[i]).removeClass('sel');
				$('.'+opened[i]).removeClass(opened[i]);
				$('.'+oldId).removeClass('sel');
				$('.'+oldId).slideUp();
			}
			opened = new Array();
		}	
	
		if (_open) {
			$('#'+id).addClass('sel').addClass('open-' + (opened.length+1));
			$('.'+id).addClass('sel').slideDown();
			opened.push('open-' + (opened.length+1));
		}
		//alert(opened);
	}
	
	
	return false;
}
//////////////////////////////////////////////////////////////
// AJAX funkcijos ////////////////////////////////////////////
//////////////////////////////////////////////////////////////
function eshopAdd(id,sid,ln,baseq,base,eshop){
	var xmlHttp;	
	var count = $("#item-" + id).attr("value");
	if(id && sid && ln){
		xmlHttp = $.get("/admin/bg/ajax?idn=eshop_basket&ac=eshop_add&sid=" + sid + "&eshop_item=" + id ,
										{count: count,ln: ln,baseq: baseq, base: base, gl: ln,eshop_rep: eshop},
										function(){ 
			var basket = $("#eshop_basket");																																
			if(basket.css("display") == "none"){ // jeigu nematomas basketo frame'as																											
				basket.append(xmlHttp.responseText);			
				basket.show();
			}else{ // jeigu yra basketas jau isspausdintas			
				basket.empty(); // valom visa jo vidu lauk
				basket.append(xmlHttp.responseText); // parodom ka gavom :)
			}
		});
	}
}

