var loading = '<div style="text-align:center; width:100%; position:relative; top:5px;" id="loading"><img src="_load/images/loading.gif" /></div>';
var id;
var response;
function openIn(url, id){
//	alert(id);
//  alert(document.getElementById(id));
try
{
//		window.clearTimeout(z);
//		_hide(id, 0);
		$(id).innerHTML = loading;
//		$h = $(id).getHeight();
//		$h = $h/2;
//		$('loading').setStyle({'top':$h+'px'});
		$(id).show();
	  	_show(id, 0);
	  
	  // $(id).show.delay(5);

	  
	  
	  
	  new Ajax.Request(url,
	  {
		method:'get',
		onSuccess: function(transport){
		  var response = transport.responseText || "";
		  $(id).innerHTML =  response;
		  //loadingE();
		},
	//	onLoading: loadingS(),
		onFailure: function(){ alert('Something went wrong...') }
	  });
} catch(e){}

return false;
}



function main($p){
	var url = '_load/'+$p.type+'/'+$p.template+'/_load.php?parent='+$p.parent;

	openIn(url, $p.div);
	
}

function update($p){
	var url = '_load/functions/get.content.php?div='+$p.div+'&id='+$p.id;
	openIn(url, $p.div);

}

function uc($p){

	var url = '_load/functions/check.content.php?div='+$p.div+'&id='+$p.id;
	
	new Ajax.Request(url,
	  {
		method:'get',
		onSuccess: function(transport){
		  var response = transport.responseText || "";
		  if(response == 1) { update($p);}
		  
		// alert(response);
		},
	
		onFailure: function(){ alert('Something went wrong...') }
	  });
	
	
}

function loadProducts($p){
	var div = 'escriptasCatalog['+$p.div+']';
	
	var url = '_load/products/'+$p.template+'/_load.php?id='+$p.id+'&parent='+$p.parent;

	openIn(url, div);
}

function loadProduct($p){
	var div = 'escriptasCatalog['+$p.div+']';
	
	var url = 'product/'+$p.template+'/_load.php?id='+$p.id+'&parent='+$p.parent;

	openIn(url, div);
}

function loadSubCatalog($p){
	var div = 'escriptasCatalog['+$p.div+']';
	
	var url = '_load/catalog/'+$p.template+'/_load.php?id='+$p.id+'&parent='+$p.parent;

	openIn(url, div);
}

function cart($p){
	var url = '_load/cart/functions/'+$p.action+'.cart.php?id='+$p.id+'&amount='+$p.amount+'&price='+$p.price+'&template='+$p.template;

	openIn(url, $p.div);
}

function offer($p){
	var url = '_load/cart/'+$p.template+'/_load.php?load=_load&action=step'+$p.step+'&template='+$p.template;
//	alert(url);

	openIn(url, $p.div);
	setCenter($p.div);
}

function setCenter(id){
	filterStart();
	$(id).show();
	$(id).style.top = 50 + document.body.scrollTop +'px';
	
}

function filterStart(){
		var docwidth = window.top.document.body.clientWidth; 
		var docheight = window.top.document.body.clientHeight;
		
		$('black').style.width = docwidth+'px';
		$('black').style.height = docheight+'px';
		
		$('black').show();
}

function filterEnd(){
		$('float').hide();
		$('black').hide();
}

function isNumberKey(evt){
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57)) return false;
	
	return true;
}

function _reload($p){
	var url = '_load/functions/reload.php?div='+$p.div;

	openIn(url, $p.div);
}

function check(val){
	
	if(val.value.length > 0){
		val.className = 'g';
	}
	else{
		val.className = 'r';
	}
}

function addOffer($p){
	var form = 'offerForm';
	var ur = '_load/cart/'+$p.template+'/_load.php?load=_load&action=step'+$p.step+'&template='+$p.template+'&';

	if(checkform(form)){
		
		var t = $(form);
//		alert(t);
		for(i=0; i<t.elements.length; i++){
			if(t.elements[i].type == 'checkbox'){
				
				if(t.elements[i].checked){t.elements[i].value='check';}
				else{t.elements[i].value='';}
				
			}
			var ur = ur+''+t.elements[i].name+'='+t.elements[i].value+'&';
		}
		//alert(ur);
		openIn(ur, $p.div);
	}
	
	
}


function checkform(form){
	
	var t = $(form);
		
	for(i=0; i<t.elements.length; i++){
		if( (t.elements[i].className == 'r') ) {
			try
			{
				$('error_e_'+t.elements[i].id).show();
				$('error_m_'+t.elements[i].id).show();
				$('error_'+t.elements[i].id).show();
				
			}
			catch (e) 
			{}
			return false;
		}
	}
	return true;
}

function _load($p){

	
	var url = '_load/'+$p.type+'/'+$p.template+'/_load.php?load=_load&parent='+$p.parent+'&id='+$p.id+'&action='+$p.action;

	openIn(url, $p.div);
}

function bigFoto(href){
	
	var url = href;
	
	filterStart();
	
	openIn(url, 'float');
	
	
}
document.onmousemove = getXY;
var posx = 0;
var posy = 0;
function getXY(e) {
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
}
function floatText(text){
 
 $('floatText').innerHTML = text;
 $('floatText').show();

 y = posy + 15;
 x = posx + 15;
 
 
$('floatText').setStyle({
  top: y + 'px',
  left: x + 'px'
});
// alert($('floatText').innerHTML);

 z = setTimeout("floatText('"+text+"')", 10);
}

function destroyFloatText(){
 div = document.getElementById('floatText');
 $('floatText').innerHTML = '';
 $('floatText').hide();
 
 window.clearTimeout(z)
}

function email(em){

	if (em.value.match(/^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}/)==null) {
		$('error_m_'+em.id).show();
		
		em.className = 'r';
	
		return false;
	} else {
		$('error_m_'+em.id).hide();
		em.className = 'g';
	}


}
function help(p){

	$('help_'+p.id).show();

}

function tree(p){
	
	var url = '_load/tree/functions/getTree.php?parent='+p;
	
	new Ajax.Request(url,
	  {
		method:'get',
		onSuccess: function(transport){
		  var response = transport.responseText || "";
		  var br = new Array();
		  var br = response.split('||');
		  
		  for(i=0; i<br.length; i++){
			var temp = new Array();
			temp = br[i].split('|');
			
			if(temp.length == 3){
				var ur = '_load/'+temp[1]+'/'+temp[2]+'/_load.php?load=_load&div='+temp[0]+'&parent='+p;
//				alert(ur);
				openIn(ur, temp[0]);
			}

		  }
		  
		},
		onFailure: function(){ alert('Something went wrong...') }
	  });
	
}

function _hide(id, c){
		
		var c = c+1;
		o = 1-(c*0.1);
		t = 50*c;
		
		$(id).setOpacity(o);
		
		z = window.setTimeout(function() { _hide(id, c); }, t);
		if(c == 10){
			window.clearTimeout(z);

		}
		

}

function _show(id, c){
		if(c==0) window.clearTimeout(z);
		var c = c+1;
		o = (c*0.1);
		t = 10*c;
		
		$(id).setOpacity(o);
		
		var z = window.setTimeout(function() { _show(id, c); }, t);
		if(c == 10){
			window.clearTimeout(z);
			
		}

}

function save($p){
	//alert($p.form);
	if(checkform($p.form)){
		
		var ur = '_load/'+$p.type+'/'+$p.template+'/_load.php?load=_load&action=result&template='+$p.template+'&';

		var t = $($p.form);
//		alert(t);
		for(i=0; i<t.elements.length; i++){
			if(t.elements[i].type == 'checkbox'){
				
				if(t.elements[i].checked){t.elements[i].value='check';}
				else{t.elements[i].value='';}
				
			}
			var ur = ur+''+t.elements[i].name+'='+t.elements[i].value+'&';
		}
		//alert(ur);
		openIn(ur, $p.div);
	}

}

function setBig(o,w,h){
	

	
	var url = o.src.replace('get.php','get_.php');
	url = url + '&_y='+h+'&_x='+w;
//	alert(url);
	filterStart();
	
	openIn(url, 'float');
	


}