﻿posvar = 0;
menudisplayed = "";
arrowflag = 0;
menutop = 0;
topaon = 0;
bottomaon = 1;
hasmousemoved = 0;
oldmousey = 0;
cdivwidth = 0;
stripdisplayed = 0;
framecount = 0;
framearray = new Array();
framedisplayed = 0;
//document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = mymousemove;
function mymousemove(myevent){
  var ev = (!myevent)?window.event:myevent;
	hasmousemoved = 1;
	if(menudisplayed != ""){
		theloc = getY("menu" + menudisplayed + "cell");
		topleftx = theloc.x;
		toplefty = theloc.y;
		toprightx = Number(topleftx) + Number(document.getElementById("menu" + menudisplayed + "cell").width);
		//alert(topleftx + " " + toprightx);
		toprighty = toplefty
		theloc = getY("menu" + menudisplayed);
		bottomleftx = theloc.x
		bottomlefty = Number(theloc.y) + Number(document.getElementById("menu" + menudisplayed).offsetHeight);
		//alert(Number(theloc.y) + " " + document.getElementById("menu" + menudisplayed).offsetHeight);
		bottomrightx = Number(bottomleftx) + Number(document.getElementById("menu" + menudisplayed).width);
		bottomrighty = bottomlefty;
		cursorx = ev.clientX;
		cursory = ev.clientY;
		//alert(document.getElementById("menu" + menudisplayed + "div").offsetHeight);
		if(cursorx < topleftx || cursorx > toprightx || cursory < toplefty || cursory > bottomlefty){
			hide_menu(menudisplayed);
		}else{
			//checkarrows(cursory);
			if(cursory != oldmousey && cursory > (toplefty + 95) && cursory < (bottomlefty - 70) ){
				if((cursory > oldmousey && menutop > -(document.getElementById("menu" + menudisplayed + "div").offsetHeight - 270)) || (cursory <  oldmousey && menutop < 0)){
					thediff = cursory - oldmousey;
					themult = (document.getElementById("menu" + menudisplayed + "div").offsetHeight / 45) / 6;
					document.getElementById("menu" + menudisplayed + "div").style.top = (menutop - (thediff * themult)) + "px";
					menutop -= thediff * themult;
				}
				oldmousey = cursory;
			}
			if(menutop < 0){
				document.getElementById("menu" + menudisplayed + "uptext").style.display = "inline";
				topaon = 1;
			}else if(menutop >= 0){
				document.getElementById("menu" + menudisplayed + "uptext").style.display = "none";
				topaon = 0;
			}
			if(menutop > -(document.getElementById("menu" + menudisplayed + "div").offsetHeight - 270)){
				document.getElementById("menu" + menudisplayed + "downtext").style.display = "inline";
				bottomaon = 1;
			}else if(menutop <= -(document.getElementById("menu" + menudisplayed + "div").offsetHeight - 270)){
				document.getElementById("menu" + menudisplayed + "downtext").style.display = "none";
				bottomaon = 0;
			}
			
		}
	}
}


function getY(tehobj){
	var objItem = document.getElementById(tehobj)
	var objParent = null
	var intX = 0
	var intY = 0
	do
	{ // Walk up our document tree until we find the body
	 // and add the distance from the parent to our counter.
		intX += objItem.offsetLeft;
		intY += objItem.offsetTop;
		objParent = objItem.offsetParent.tagName;
		objItem = objItem.offsetParent;}
	 while(objParent != 'BODY')
	return {x:intX, y:intY};
}

function hilight_menu(menuid){
	//alert("menu" + menuid + "cell");
	document.getElementById("menu" + menuid + "cell").style.backgroundImage = "url(test_images/menu_bg_on.jpg)";
	document.getElementById("menu" + menuid + "text").style.color = "#010134";
}

function delight_menu(menuid){
	//alert("delight");
	if(menudisplayed != menuid){
		document.getElementById("menu" + menuid + "cell").style.backgroundImage = "url(test_images/menu_bg.jpg)";
		document.getElementById("menu" + menuid + "text").style.color = "#7485ab";
	}
	//alert("x=" + window.event.clientX + " y=" + window.event.clientY + " offsettop=" + getY(menuid) + " height=" + document.getElementById("menu" + menuid + "cell").height);
 	//var theloc = getY("menu" + menuid + "cell");
 	//if(menudisplayed == menuid && !(window.event.clientY > (Number(theloc.y) + Number(document.getElementById("menu" + menuid + "cell").height)))){
		//hide_menu(menuid);
	//}
	//if menu is open then hide it	
}

function show_menu(menuid){
	if(menudisplayed == menuid){
		hide_menu(menuid);
	}else{
		var theloc = getY("menu" + menuid + "cell");
		
		document.getElementById("menu" + menuid).style.top = Number(theloc.y) + Number(document.getElementById("menu" + menuid + "cell").height) + "px";
		document.getElementById("menu" + menuid).style.left = theloc.x + "px";
		document.getElementById("menu" + menuid).style.display = "";
		menudisplayed = menuid;
		oldmousey = Number(theloc.y) + 95;
		//alert(document.getElementById("menu" + menuid + "div").offsetHeight + "px");
	}
}

function menu_mouseout(menuid){
	//theloc = getY("menu" + menuid);
	//mousey = window.event.clientY;
	//alert(mousey + " " + theloc.y);
	//if(mousey > (theloc.y + 3)){
		//hide_menu(menuid);
	//}
}

function hide_menu(menuid){
	//theloc = getY("menu" + menuid);
	//alert("hide");
	document.getElementById("menu" + menuid).style.display = "none";
	document.getElementById("menu" + menuid + "div").style.top = "0px";
	menudisplayed = "";
	menutop = 0;
	arrowflag = 0;
	topaon = 0;
	bottomaon = 1;
	oldmousey = 0;
	delight_menu(menuid);
}

function movediv(theamount){
	//alert(document.getElementById("cdiv").style.left);
	//alert(posvar + " " + theamount);
	//alert(cdivwidth);
	if(posvar + theamount >= -(cdivwidth - 651) && posvar + theamount <= 0){
		//document.getElementById("cdiv").style.left = (posvar + theamount) + "px";
		x = 1;
		y = 1;
		//alert(posvar + " " + theamount);
		while(x<=Math.abs(theamount)){
			if(x + 8 > Math.abs(theamount)){
				myamt = 1;
			}else{
				myamt = 8;
			}
			var dummy = myamt;
			//alert(myamt);
			if(theamount > 0){
				eval("setTimeout(function(){moveone('pos'," + dummy + ");}," + 5*y + ");");
			}else{
				//setTimeout(function(){moveone('neg',dummy);},5*y);
				eval("setTimeout(function(){moveone('neg'," + dummy + ");}," + 5*y + ");");
			}
			x += myamt;
			y += 1;
			//for(y=1;y<10000;y++){
				//nothing just timeing;
			//}
		}
		//posvar += theamount;
		
	}
}

function moveone(posorneg,howmuch){
	//alert(posvar + " " + howmuch);
	if(posorneg == "pos"){
		document.getElementById("cdiv").style.left = (posvar + howmuch) + "px";
		posvar += howmuch;
	}else{
		document.getElementById("cdiv").style.left = (posvar - howmuch) + "px";
		posvar -= howmuch;
	}
	if(posvar == 0){
		document.getElementById("leftarrow").src = "test_images/arrow_left_fade.png";
	}else if(document.getElementById("leftarrow").src == "http://www.judaism.com/vt/test_images/arrow_left_fade.png"){
		document.getElementById("leftarrow").src = "test_images/arrow_left_off.png";
	}
	if(posvar == -(cdivwidth - 651)){
		document.getElementById("rightarrow").src = "test_images/arrow_right_fade.png";
	}else if(document.getElementById("rightarrow").src == "http://www.judaism.com/vt/test_images/arrow_right_fade.png"){
		document.getElementById("rightarrow").src = "test_images/arrow_right_off.png";
	}
	//return 1;
}

function menuclick(whichstrip,whichpanel){
	//alert(posvar + " " + framedisplayed);
	//alert(whichstrip + " " + whichpanel)
	//alert(whichstrip);
	whichpanel = Number(whichpanel)
	if(whichstrip == stripdisplayed){
		//amtpanels = cdivwidth / 217;
		if(whichpanel != framedisplayed){
			dapanels = 0;
			for(x=framecount;x>=whichpanel;x--){
				dapanels += framearray[x];
				//alert(x + " " + framearray[x] + " " + dapanels);
			}
			needtobeat = dapanels - 651;
			//alert(dapanels + " " + needtobeat + " " + posvar + " " + cdivwidth);
			if(needtobeat < 0){
				needtobeat = 0;
			}
			if(needtobeat > cdivwidth){
				needtobeat = cdivwidth ;
			}
			thediff = -(posvar) - needtobeat;
			//alert(thediff);
			if(needtobeat < 651){
				findend = 0;
				thecounter = framecount;
				while(findend < 651){
					findend += framearray[thecounter];
					thecounter -= 1
				}
				whichpanel = thecounter + 1;
			}
			
			framedisplayed = whichpanel;
			//alert(posvar + " " + framedisplayed + " " + thediff);
			movediv(thediff);
			//alert(posvar + " " + framedisplayed + " " + thediff);
			//alert(framedisplayed);
		}
	}else{
		ajaxpage("make_strip.asp?sid=" + whichstrip + "&pid=" + whichpanel,"cdiv");
	}
}

function movebutton(leftorright){
	mymoveamt = 0;
	//alert(posvar + " " + framedisplayed);
	//alert(framedisplayed);
	if(leftorright == 1 && framedisplayed < framecount){
		//alert("in move left " + framedisplayed);
		//shouldnt go past 651 which is 3rd frame if all equal
		//alert(framedisplayed + " " + framearray[Number(framedisplayed) + 1]);
		if(posvar + framearray[framedisplayed + 1] <= 0){
			//alert(posvar);
			mymoveamt = framearray[framedisplayed];
			if(posvar + mymoveamt > 0){
				mymoveamt = framearray[framedisplayed + 1];
			}
			framedisplayed = framedisplayed + 1;
		}
	}else if(leftorright == -1 && framedisplayed > 0){
		widthtohere = 0;
		for(x = framecount;x >= framedisplayed;x--){
			widthtohere += framearray[x];
			//alert(widthtohere);
		}
		if(-(posvar) < widthtohere - 651){
			needtobeat = widthtohere - 651;
			//alert("test");
		}else{
			needtobeat = widthtohere - (651 - framearray[framedisplayed - 1]);
			framedisplayed = framedisplayed - 1;
		}
		//mymoveamt = -(needtobeat - Math.abs(posvar));
		mymoveamt = -(posvar) - needtobeat;
		
	}
	movediv(mymoveamt);
	//alert(framedisplayed);
	//alert(framedisplayed);
}

