// --------------------------------------------------
// This file contains intellectual property of YKM (http://www.ykm.co.il/).
// End users are permitted to use this code for their own internal use
// but are prohibited from redistributing this data to third parties.
// Contact: info@ykm.co.il
// --------------------------------------------------


var isDrag = false;
var grabY = 0;
var grabYR = 0;
var draggy;
var isIE = ((document.all)?true:false);

// Droplist handles

function AutoCloseAllDL() {
	CloseDL("dl_1");
	CloseDL("dl_2");
	CloseDL("dl_3");
	CloseDL("dl_4");
	CloseDL("dl_5");
}

function CloseDL(which,ev) {
	if (isIE) {
		try {
			if (!IsChildOf(event.srcElement,which)) {
				var myList = document.getElementById(which+"_list");
				var myDroplist = document.getElementById(which);
				try {
					var caller = document.getElementById(which+"_btn");
					myList.style.display = "none";
					caller.src = "/img/btn_drop_0.gif";
				} catch(e) {
					myList.style.display = "none";
				}
			}
		} catch(err) {
		}
	} else {
		try {
			if (!IsChildOf(ev.target,which)) {
				var myList = document.getElementById(which+"_list");
				var myDroplist = document.getElementById(which);
				try {
					var caller = document.getElementById(which+"_btn");
					myList.style.display = "none";
					caller.src = "/img/btn_drop_0.gif";
				} catch(e) {
					myList.style.display = "none";
				}
			}
		} catch(err) {
		}
	}
}

function ToggleList(caller) {
	var myListName = caller.getAttribute("mylist");
	var myList = document.getElementById(myListName+"_list");
	var myDroplist = document.getElementById(myListName);
	if (myDroplist.getAttribute("dis") == "1") {
	} else {
		if (myList.style.display == "none" || myList.style.display == "") {
			myList.style.top = GetRealY(myDroplist) + myDroplist.offsetHeight - 1;
			myList.style.left = GetRealX(myDroplist);
			myList.style.display = "block";
			caller.src = "/img/btn_drop_2.gif";
			PrepDLItems(myListName+"_cont");
			if (NeedScroll(document.getElementById(myListName+"_cont"))) {
				// Setting up scrollbar
				PrepScrollbar(document.getElementById(myListName+"_cont"));
			} else {
//				try {
					HideScrollBar(document.getElementById(myListName+"_cont"));
//				} catch(err) {
//				}
			}
		} else {
			myList.style.display = "none";
			caller.src = "/img/btn_drop_0.gif";
		}
	}
}

function DLButton(who, state) {
	if (who.src.indexOf("drop_2")==-1) {
		who.src = "/img/btn_drop_"+state+".gif";
	}
}

function DLHilite(ev) {
	var who = ((isIE)?event.srcElement:ev.target);
	if (who.getAttribute("selected")!="1") {
		who.className = "DropList_Item_Hilited";
	}
}
function DLNormal(ev) {
	var who = ((isIE)?event.srcElement:ev.target);
	if (who.getAttribute("selected")!="1") {
		who.className = "DropList_Item";
	}
}
function DLClick(ev) {
	var who = ((isIE)?event.srcElement:ev.target);
	var myText = document.getElementById(who.getAttribute("mylist")+"_text");
	var myButton = document.getElementById(who.getAttribute("mylist")+"_btn");
	myText.innerHTML = who.innerHTML;
	// Resetting "selected" states
	var myList = document.getElementById(who.getAttribute("mylist")+"_cont");
	if (isIE) {
		for (var i=0; i<myList.children.length; i++) {
			var theItem = myList.children[i];
			if (theItem == who) {
				theItem.setAttribute("selected","1");
				theItem.className = "DropList_Item_Selected";
			} else {
				theItem.setAttribute("selected","0");
				theItem.className = "DropList_Item";
			}
		}
	} else {
		for (var i=0; i<myList.childNodes.length; i++) {
			var theItem = myList.childNodes[i];
			if (theItem.tagName && theItem.tagName.toUpperCase() == "DIV") {
				if (theItem == who) {
					theItem.setAttribute("selected","1");
					theItem.className = "DropList_Item_Selected";
				} else {
					theItem.setAttribute("selected","0");
					theItem.className = "DropList_Item";
				}
			}
		}
	}
	ToggleList(myButton);
	ExceptionHandle(who);
}

function ExceptionHandle(theItem) {
}

function PrepDLItems(droplist_name) {
	var droplist = document.getElementById(droplist_name);
	var topName = droplist.getAttribute("mydroplist");
	if (isIE) {
		for (var i=0; i<droplist.children.length; i++) {
			var theItem = droplist.children[i];
			theItem.onmouseover = DLHilite;
			theItem.onmouseout = DLNormal;
			theItem.onclick = DLClick;
			theItem.setAttribute("myname","item_"+i);
			theItem.setAttribute("mylist",topName);
		}
	} else {
		for (var i=0; i<droplist.childNodes.length; i++) {
			var theItem = droplist.childNodes[i];
			if (theItem.tagName && theItem.tagName.toUpperCase() == "DIV") {
				theItem.onmouseover = DLHilite;
				theItem.onmouseout = DLNormal;
				theItem.onclick = DLClick;
				theItem.setAttribute("myname","item_"+i);
				theItem.setAttribute("mylist",topName);
			}
		}
	}
	document.onmousedown = AutoCloseAllDL;
}


// Scrollbar functions

function NeedScroll(container) {
	if (isIE) {
		return container.offsetHeight <= container.children.length*20;
	} else {
		return container.offsetParent.offsetHeight <= container.childNodes.length*20;
	}
}

function NeedVisualScroll(container) {
	var total = 0;
	if (isIE) {
		for (var i=0;i<container.children.length;i++) {
			if (container.children[i].style.display == "block") total += 20;
		}
	} else {
		for (var i=0;i<container.childNodes.length;i++) {
			if (container.childNodes[i].style && container.childNodes[i].style.display == "block") total += 20;
		}
	}
	return ((isIE)?container.offsetHeight:container.offsetParent.offsetHeight) <= total;
}

function PrepScrollbar(container) {
	var tarX = container.offsetWidth-((isIE)?6:10);
//	var tarX = 3;
//	var tarY = GetRealY(container.offsetParent)-container.offsetHeight - 1;
	var tarY = ((isIE)?-3:3);
//	parent.document.title = "" + GetRealY(container.offsetParent) + ":" + container.offsetHeight;
	var myScroll = document.getElementById(container.getAttribute("mydroplist")+"_scroll");
	myScroll.style.top = tarY;
	myScroll.style.left = tarX;
	myScroll.style.display = "block";
	// Calculating scroller height
	var total = ((isIE)?container.children.length*20:container.childNodes.length*20);
	var visible = ((isIE)?container.offsetHeight:container.offsetParent.offsetHeight);
	var perc = visible / total;
	var myScroller = document.getElementById(container.getAttribute("mydroplist")+"_scroller");
	if (!isIE) myScroller.ondrag = DoNothing();
	myScroller.style.height = Math.round(78*perc)+"px";
	myScroller.style.width = "6px";
	alert(myScroller.style.height);
	// Setting functionality
	var myUp = document.getElementById(container.getAttribute("mydroplist")+"_up");
	var myDown = document.getElementById(container.getAttribute("mydroplist")+"_down");
	myDown.onclick = Dummy;
	myDown.ondblclick = Dummy;
	myDown.onmousedown = BtnScrollDown;
	myUp.onclick = Dummy;
	myUp.ondblclick = Dummy;
	myUp.onmousedown = BtnScrollUp;
	myScroller.onmousedown = StartDrag;
//	myScroller.onmouseup = EndDrag;
	myScroller.onmousemove = Dragger;
	document.onmouseup = EndDrag;
}

function HideScrollBar(container) {
	var myScroll = document.getElementById(container.getAttribute("mydroplist")+"_scroll");
	myScroll.style.display = "none";
}

function BtnScrollDown(ev) {
	var who = ((isIE)?event.srcElement:ev.target);
	var cont = document.getElementById(who.getAttribute("mycont"));
	cont.scrollTop += 20;
	UpdateScroller(cont);
}

function BtnScrollUp(ev) {
	var who = ((isIE)?event.srcElement:ev.target);
	var cont = document.getElementById(who.getAttribute("mycont"));
	cont.scrollTop -= 20;
	UpdateScroller(cont);
}

function UpdateScroller(container) {
	var scroller = document.getElementById(container.getAttribute("mydroplist")+"_scroller");
	var total = ((isIE)?(container.children.length*20 - container.offsetHeight):(container.childNodes.length*20 - container.offsetParent.offsetHeight));
	var perc = container.scrollTop / total;
	scroller.style.marginTop = Math.floor((78-scroller.offsetHeight)*perc);
}

function UpdateScroll(scroller) {
	var container = document.getElementById(scroller.getAttribute("mycont"));
	var mar = parseInt(scroller.style.marginTop);
	if (isNaN(mar)) {
		mar = 0;
	}
	var perc = mar / (78 - scroller.offsetHeight);
	container.scrollTop = Math.floor((((isIE)?container.children.length:container.childNodes.length)*20 - ((isIE)?container.offsetHeight:container.offsetParent.offsetHeight)) * perc);
}

function StartDrag(ev) {
	if (!isDrag) {
		isDrag = true;
		if (isIE) {
			draggy = event.srcElement;
			grabY = event.offsetY;
			grabYR = event.clientY;
		} else {
			draggy = ev.target;
			grabY = ev.y;
			grabYR = ev.clientY;
		}
	}
}
function EndDrag() {
	if (isDrag) {
		isDrag = false;
		draggy = null;
//		parent.document.title = "No drag";
	}
}

function Dragger(ev) {
	if (isDrag) {
		var myMargin = parseInt(draggy.style.marginTop);
		if (isNaN(myMargin)) {
			myMargin = 0;
		}
		var newMargin = myMargin + (((isIE)?event.clientY:ev.y) - grabYR);
		if (newMargin<0) {
			newMargin = 0;
		} else if (newMargin > (78-draggy.offsetHeight)) {
			newMargin = (78-draggy.offsetHeight);
		}
		draggy.style.marginTop = newMargin + "px";
		grabYR = ((isIE)?event.clientY:ev.y);
		UpdateScroll(draggy);
	}
}

/*******************/

function GetRealX(who) {
	try {
		if (who.tagName.toUpperCase() == "BODY" || who.tagName.toUpperCase() == "HTML") {
			return 0;
		} else {
			return (who.offsetLeft + GetRealX(who.offsetParent));
		}
	} catch(err) {
	}
}
function GetRealY(who) {
	try {
		if (who.tagName.toUpperCase() == "BODY" || who.tagName.toUpperCase() == "HTML") {
			return 0;
		} else {
			return (who.offsetTop + GetRealY(who.offsetParent));
		}
	} catch(err) {
	}
}
function Dummy() {
	return true;
}
function IsChildOf(who, ofwho) {
	if (who.tagName.toUpperCase() == "BODY" || who.tagName.toUpperCase() == "HTML") {
		return false;
	} else {
		if (who.id.indexOf(ofwho) > -1) {
			return true;
		} else {
			return IsChildOf(who.offsetParent,ofwho);
		}
	}
}

function DoNothing() {
}