var mozilla=document.getElementById && !document.all
var opera = window.opera && document.getElementById; 
var ie=document.all && !opera;

var divIsVisible=0;
//barpicture=new Image(100,9);
//barpicture.src="/files/gr/progress.gif";
picture=new Image();
pxl=new Image();
pxl.src="/files/gr/pxl.gif";

function iebody(){
return (document.compatMode && document.compatMode!="BackCompat")?document.documentElement:document.body;
}

function displayImg(e){
try{
if (!e) var e = window.event;
el=document.getElementById("show");

id=mozilla?e.target.id:e.srcElement.id;
img = "/files/img/"+ id + ".jpg";
//if(document.images['im'].src != barpicture.src){
//document.images['im'].src=barpicture.src;
//clearTimeout(id);
//}

if(((ie||opera) && e.srcElement.className=='s')||(mozilla && e.target.className=='s')){
document.getElementById('show').style.display='block';
picture.src=img;
document.images['im'].src=picture.src
//if(document.images['im'].src == barpicture.src){
//id=setTimeout("document.images['im'].src=picture.src", 500);
//}
}

	divIsVisible=1;
	if (mozilla){
		el.style.left=pageXOffset+e.clientX+10+"px";
		el.style.top=pageYOffset+e.clientY+"px";

if(window.innerHeight-e.clientY < 450){
el.style.top=(pageYOffset + window.innerHeight - 400 + "px");
}

		el.style.visibility="visible";
		document.addEventListener("mouseout", hideImg, true);
		return false;
	}
	else if (ie){
		el.style.left=iebody().scrollLeft+event.clientX+10;
		el.style.top=iebody().scrollTop+event.clientY;

if(iebody().clientHeight-e.clientY < 450){
el.style.top=(iebody().scrollTop+iebody().clientHeight - 400 + "px");
}

 		el.style.visibility="visible";
		document.attachEvent("onmouseout", hideImg);
		return false;
	}
	else if (opera) {
		el.style.left=iebody().scrollLeft+event.clientX+10;
		el.style.top=iebody().scrollTop+event.clientY;

if(document.body.clientHeight-e.clientY < 450){
el.style.top=(iebody().scrollTop+document.body.clientHeight - 450 + "px");
}
		el.style.visibility="visible";
		document.addEventListener("mouseout", hideImg, true);
		return false;
	}
}
catch(e){}	
	
}



function hideImg(){
clearTimeout(id);
	if (typeof el!="undefined" && divIsVisible){
		el.src = pxl.src;
		document.getElementById('show').style.display='none';
		//el.src = '';
		divIsVisible=0;
	}
		divIsVisible=0
}




function highlightRow(e){
try {
if (!e) var e = window.event;
//alert(e.srcElement.parentNode.className);
id=mozilla?e.target.parentNode.id:e.srcElement.parentNode.id;

if(id.substring(0,2) == 'tr'){
if (((ie||opera) && e.srcElement.parentNode.className=='hl_on')||(mozilla && e.target.parentNode.className=='hl_on')){
document.getElementById(id).className='hl_off';
ajaxHighlightItem(id, '0');
}else{
document.getElementById(id).className='hl_on';
ajaxHighlightItem(id, '1');
}}
}
catch(e){}


}




function begin(){
if(mozilla){
	document.addEventListener("mouseover", displayImg, true);
	document.addEventListener("click", highlightRow, true);
}
else if(ie){
	document.attachEvent("onmouseover", displayImg);
	document.attachEvent("onclick", highlightRow);
}
else if(opera){
	document.addEventListener("mouseover", displayImg, true);
	document.addEventListener("click", highlightRow, true);
}
}

//------------------------------------------------------------------ AJAX RELATED --------------------------------------------------------------------

var ajaxObjects = new Array();

function ajaxHighlightItem(id,par)
{
	var ajaxIndex = ajaxObjects.length;
	var item_id = id.substring(2,10);
	ajaxObjects[ajaxIndex] = new sack();
	//alert("item_id=" + item_id + "class_name=" + class_name); 
	ajaxObjects[ajaxIndex].setVar('item_id', item_id);
	ajaxObjects[ajaxIndex].setVar('highlight', par);
	ajaxObjects[ajaxIndex].requestFile = '/highlightitem/';	// URL to request
	//ajaxObjects[ajaxIndex].onCompletion = function(){ ajaxValidateHighlightItem(ajaxIndex); };	// Specify function that will be executed after URL has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function

return false; //to prevent jumping to top of the page through htef="#"	
} 

//function ajaxValidateHighlightItem(ajaxIndex)
//{	if(ajaxObjects[ajaxIndex].response) {alert(ajaxObjects[ajaxIndex].response + '...') };}


function ReserveItem(obj){
	tr_id = obj.parentNode.parentNode.id;
	var ajaxIndex = ajaxObjects.length;
	var item_id = tr_id.substring(2,10);
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].setVar('item_id', item_id);
	ajaxObjects[ajaxIndex].requestFile = '/reserveitem/';	
	ajaxObjects[ajaxIndex].onCompletion = function(){ RebuildCell(1,obj); };	
	ajaxObjects[ajaxIndex].runAJAX();	
} 

function RebuildCell(isreserve,obj,ajaxIndex){
if(isreserve){
obj.parentNode.innerHTML = '<img src="/files/gr/lockedbyme.gif" alt="Отменить Ваш резерв" title="Отменить Ваш резерв" onclick="UnReserveItem(this)"> <span class="res">on hold 5д.0ч.0м.</span>';
}else{
obj.parentNode.innerHTML = '<img src="/files/gr/unlocked.gif" alt="Зарезервировать" title="Зарезервировать" onclick="ReserveItem(this)">';
}
}

function UnReserveItem(obj){
	tr_id = obj.parentNode.parentNode.id;
	var ajaxIndex = ajaxObjects.length;
	var item_id = tr_id.substring(2,10);
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].setVar('item_id', item_id);
	ajaxObjects[ajaxIndex].requestFile = '/unreserveitem/';	
	ajaxObjects[ajaxIndex].onCompletion = function(){ RebuildCell(0,obj,ajaxIndex); };	
	ajaxObjects[ajaxIndex].runAJAX();	
} 

function bwinvert(){
var elem = document.getElementById("main");
if(elem.style.backgroundColor != 'rgb(204, 204, 204)'){
elem.style.backgroundColor = 'rgb(204, 204, 204)';
elem.style.color = 'black';
document.body.style.backgroundColor = '#cccccc';
}else{
elem.style.color = '#999';
elem.style.backgroundColor = 'black';}
document.body.style.backgroundColor = 'black';
}
