/********************************************************************************************
	Expanding Block Menu (onMouseover)
        Based on "Dynamic Expanding Star Menu by Jim Stiles (www.jdstiles.com)
 *******************************************************************************************/

	// Debugging this mug?
	var debug = false;
	var debug2 = false;
	var menuCount = 0;			// Used in  menuObject(name,x,y,caption, r, parent)
	var pi = Math.PI;			// Used extensively in Math Functions
	var xClick, yClick, yHeight, xWidth;
	
	setShit();
	
	// Initiation Function
	function setShit(){
		if(debug) alert('settingFunctions');
		
		var el = getObjectRef("link");
				
		if( el.offsetParent ){
			xClick = parseInt(el.offsetLeft);
			yClick = parseInt(el.offsetTop);
			yHeight = parseInt(el.offsetHeight);
			xWidth = parseInt(el.offsetWidth);
			
		} 
		else
		{
			alert("Do I need this?");
		}
		
		document.onmousemove = getMouseXY;
	}

	function getObjectRef(name){
	  if(document.getElementById) return document.getElementById(name);
	  else if(document.all) return document.all[name];
	  else return null;
	}
	
	/*** General Functions to assist the script  ***/
	function show(name) {
	  var el = getObjectRef(name);
	  if(el) el.style.visibility = "visible";
	}
	function hide(name) {
	  var el = getObjectRef(name);
	  if(el) el.style.visibility = "hidden";
	}
	function getWidth(name) {
	  var el = getObjectRef(name);
	  return el.offsetWidth;
	}
	function getHeight(name) {
	  var el = getObjectRef(name);
	  return el.offsetHeight;
	}
	function moveMe(name,x,y) {
	  var el = getObjectRef(name);
	  if(el) 
	  { 
	  	el.style.top = parseInt(y); 
		el.style.left = parseInt(x); 
	  }  
	}
	function moveBy(name,x,y) {
	  	var el = getObjectRef(name);
	  	if(el) {
		  el.style.top = parseInt(el.style.top) + parseInt(y); 
		  el.style.left = parseInt(el.style.left) + parseInt(x); 
		}  
	}
	
	// Creates the menu objects
	function menuObject(name,x,y,caption, r, parent)
	{
		if(debug) alert('menuObject()' + "(," + x + ", " + y + ")");
		
		if ( (!document.getElementById&&!document.all) || navigator.userAgent.indexOf("Opera")>-1) return;
		document.write('<div id="divDot' + menuCount + '" class="dotempty" style="top: ' + y + '; left: ' + x + '"></div>');
		document.write('<div id="divCap' + menuCount + '" class="caption" style="top: 0; left: 0">' + caption + '</div>');
	  
		this.name = name;
		this.parent = parent
		this.ref = "divDot" + menuCount;
		this.caption = "divCap" + menuCount
		this.odd = -1;
		if(r) this.radius = r;
		else this.radius = 600;
		this.subMenus = new Array();
		this.state = 0;
		this.moving = false;
		
		if(this.parent)
		{
			this.action = "null";
			this.action2 = "null";
			this.action3 = "null";
		}
		
		else{
			this.action = "toggle";
			this.action2 = "null";
			this.action3 = "null";
		}
		
		if(this.parent) this.startAngle = parent.startAngle;
		else this.startAngle = 0;
		
		getObjectRef(this.ref).objRef = this.name;
		
		this.show = function() { show(this.ref); }
		this.hide = function() { hide(this.ref); }
		
		this.moveMe = function(x,y) { 
			if(this.parent) { 
				x += this.parent.x(); 
				y += this.parent.y(); 
			} 
			moveMe(this.ref,x,y); 
		}
		
		this.moveBy = function(x,y) { moveBy(this.ref,x,y); }
		this.x = function() { return parseInt(getObjectRef(this.ref).style.left); }
		this.y = function() { return parseInt(getObjectRef(this.ref).style.top); }
		this.w = function() { return getWidth(this.ref); }
		this.h = function() { return getHeight(this.ref); }
		
		this.showCaption = function() {
			if(debug) alert(this.caption);
			
			var xLoc, yLoc;
			
			if(this.caption == "divCap0" || this.radius % 2 == 1 || this.parent.caption == "divCap0" || this.odd == -1 || this.subMenus.length < 5)
			{
				xLoc = this.x() - (getWidth(this.caption)/2) + (this.w()/2);
				yLoc = (this.y() + this.h()+2);
			}
			
			else{
				xLoc = this.x() - (getWidth(this.caption)/2) + (this.w()/2);
				yLoc = (this.y() - this.h()- 6);
			}
						
			moveMe(this.caption, xLoc, yLoc);
			show(this.caption);
		}
		this.hideCaption = function() {
			hide(this.caption);
		}
		this.setCaption = function(c) { getObjectRef(this.caption).innerHTML = c;}
		
		this.addItem = function(c,action, action2, action3, r) {
			getObjectRef(this.ref).className = "dot";
			if(!r) r = this.subMenus.length + 1;
			var sub = new menuObject(this.name + ".subMenus[" + this.subMenus.length + "]",0,0,c,r,this);
			sub.parent = this;
			sub.action = action;
			sub.action2 = action2;
			sub.action3 = action3;
			sub.moveMe(0,0);
			sub.odd = this.odd * -1;
			this.subMenus[this.subMenus.length] = sub;
			return sub;
		}
	  
		this.expand = function()
		{
			if(debug2) alert('expand');
		
			if(this.subMenus.length > 0)
			{	
				if(debug2) alert(this.subMenus.length);
			
				var p = true;
				if(this.parent)
				{
					if(debug2) alert("this.parent = true");
					
					p = !this.parent.moving;
					for(var i=0; i<this.parent.subMenus.length;i++)
					{
						p = p && ((this.parent.subMenus[i].state==0) || (this.parent.subMenus[i].state==this.parent.subMenus[i].subMenus.length)) && (this.parent.subMenus[i].moving==false);
					}
				} 
				
				else var o = false;
			  
				if((!this.parent || this.parent.state == this.parent.subMenus.length) && p)
				{
					if(debug2) alert("inside");
					
					if(this.parent) this.collapseAll(this.name);
				
					var diff;
					
					if(this.odd == 1)
					{
						diff = (pageHeight - this.y())  / (this.subMenus.length * 5/3) ;
					}
					
					else
					{
						diff = (pageWidth - this.x())  / (this.subMenus.length * 5/3) ;
					}
					
					if(debug2) alert('Diff: ' + diff);
					
					for(var i=0; i<this.subMenus.length; i++)
					{
						  this.subMenus[i].moveMe(0, 0);
						  this.subMenus[i].show();
						  this.moving = true;
						  
						  if(this.odd == 1)
						  {
							  if(debug2) alert('parent.parent');
							  this.subMenus[i].slide(0, diff * (i+1), this.name + ".subMenus[" + i + "].showCaption();" + this.name + ".moving=false;" + this.name + ".state+=1;");
						  }
						  
						  else {
							  if(debug2) alert('parent.parent.parent');
							  this.subMenus[i].slide(diff * (i+1), 0, this.name + ".subMenus[" + i + "].showCaption();" + this.name + ".moving=false;" + this.name + ".state+=1;");
						  }
					}
				
					if(this.parent)
					{
						getObjectRef(this.parent.ref).style.filter = "alpha (opacity=66)";
						getObjectRef(this.parent.ref).style.MozOpacity = "66%";
						
						getObjectRef(this.parent.ref).className = "dotoff";
						getObjectRef(this.parent.caption).style.filter = "alpha (opacity=66)";
						getObjectRef(this.parent.caption).style.MozOpacity = "66%";
						for(i=0;i<this.parent.subMenus.length;i++)
						{
							getObjectRef(this.parent.subMenus[i].ref).style.filter = "alpha (opacity=66)";
							getObjectRef(this.parent.subMenus[i].ref).style.MozOpacity = "66%";
							getObjectRef(this.parent.subMenus[i].caption).style.filter = "alpha (opacity=66)";
							getObjectRef(this.parent.subMenus[i].caption).style.MozOpacity = "66%";        
						}
						
						if(this.parent.parent)
						{
							for(i=0;i<this.parent.parent.subMenus.length;i++)
							{
								getObjectRef(this.parent.parent.subMenus[i].ref).style.filter = "alpha (opacity=66)";
								getObjectRef(this.parent.parent.subMenus[i].ref).style.MozOpacity = "66%";
								getObjectRef(this.parent.parent.subMenus[i].ref).className = "dotoff";
								getObjectRef(this.parent.parent.subMenus[i].caption).style.filter = "alpha (opacity=66)";        
								getObjectRef(this.parent.parent.subMenus[i].caption).style.MozOpacity = "66%";
							}
						
							getObjectRef(this.parent.parent.ref).style.MozOpacity = "66%";
							getObjectRef(this.parent.parent.ref).style.MozOpacity = "66%";
							getObjectRef(this.parent.parent.ref).className = "dotoff";
						}
					}
					
					getObjectRef(this.ref).style.filter = "alpha (opacity=66)";
					getObjectRef(this.caption).style.filter = "alpha (opacity=66)";  
					getObjectRef(this.ref).style.MozOpacity = "66%";
					getObjectRef(this.caption).style.MozOpacity = "66%"; 
				}
			}  
		}
	
	  this.collapse = function()
	  {
		if(debug2) alert('collapse');
	  
		var p = true;
		p = !this.moving;
		for(var i=0; i<this.subMenus.length;i++)
		  p = p && (this.subMenus[i].state==0)  && (this.subMenus[i].moving==false);
		
		if(p && this.subMenus.length > 0) {
		  for(var i=0;i<this.subMenus.length;i++){
			this.subMenus[i].hideCaption();
			this.moving = true;
			this.subMenus[i].slide(0, 0, this.name + ".subMenus[" + i + "].hide();" + this.name + ".moving=false;" + this.name + ".state-=1;");
		  }
		  if(this.parent){
			getObjectRef(this.parent.ref).style.filter = "alpha (opacity=66)";
			getObjectRef(this.parent.ref).style.MozOpacity = "66%";
			if(this.parent.subMenus.length != 0)
			  getObjectRef(this.parent.ref).className = "dot";
			else
			  getObjectRef(this.parent.ref).className = "dotempty";
			getObjectRef(this.parent.caption).style.filter = "alpha (opacity=66)";
			getObjectRef(this.parent.caption).style.MozOpacity = "66%";
			for(i=0;i<this.parent.subMenus.length;i++) {
			  getObjectRef(this.parent.subMenus[i].ref).style.filter = "alpha (opacity=100)";
			  getObjectRef(this.parent.subMenus[i].ref).style.MozOpacity = "100%";
			  if(this.parent.subMenus[i].subMenus.length != 0) 
				getObjectRef(this.parent.subMenus[i].ref).className = "dot";
			  else
				getObjectRef(this.parent.subMenus[i].ref).className = "dotempty";
			  getObjectRef(this.parent.subMenus[i].caption).style.filter = "alpha (opacity=100)";        
			  getObjectRef(this.parent.subMenus[i].caption).style.MozOpacity = "100%";
			}
			if(this.parent.parent) {
			  for(i=0;i<this.parent.parent.subMenus.length;i++) {
				getObjectRef(this.parent.parent.subMenus[i].ref).style.filter = "alpha (opacity=66)";
				getObjectRef(this.parent.parent.subMenus[i].ref).style.MozOpacity = "66%";
				if(this.parent.parent.subMenus[i].subMenus.length != 0)
				  getObjectRef(this.parent.parent.subMenus[i].ref).className = "dot";
				else
				  getObjectRef(this.parent.parent.subMenus[i].ref).className = "dotempty";
				getObjectRef(this.parent.parent.subMenus[i].caption).style.filter = "alpha (opacity=66)";        
				getObjectRef(this.parent.parent.subMenus[i].caption).style.MozOpacity = "66%";
			  }
			}
		  }
		  getObjectRef(this.ref).style.filter = "alpha (opacity=100)";
		  getObjectRef(this.caption).style.filter = "alpha (opacity=100)";       
		  getObjectRef(this.ref).style.MozOpacity = "100%";
		  getObjectRef(this.caption).style.MozOpacity = "100%";
		}
	  }
	
	  this.collapseAll = function(except)
	  {
	  	if(debug2) alert('collapse all: ' +  this.parent.subMenus.length);
	  
		for(var i=0; i<this.parent.subMenus.length;i++)
		{
			if(debug2) alert('collapse ' + i + ': ');
			
			if(this.parent.subMenus[i].name!=except && this.parent.subMenus[i].state==this.parent.subMenus[i].subMenus.length)
			{
				if(debug2) alert('somefin' + this.parent.subMenus[i].caption);
				this.parent.subMenus[i].collapse();
		  	}    
		}
	  }
	  
	  this.toggle = function(e)
	  {
	  		if(debug) alert('toggle!');
	  
			if(document.all) id = window.event.srcElement;
			else id = e.target;
			var dot = eval(id.objRef);
			
			if(dot.state==0 && !dot.moving) dot.expand();
			else if(dot.state==dot.subMenus.length && !dot.moving) dot.collapse()
	  }
	  
	  this.doAction = function(e) {
		if(document.all) id = window.event.srcElement;
		else id = e.target;
		var dot = eval(id.objRef);
		
		if(dot.action == "toggle") dot.toggle(e);
		else eval(dot.action);
	  }
	  
	  // (G$) Change  - Allow For Hover Interaction
	  this.doAction2 = function(e) {		  
		if(document.all) id = window.event.srcElement;
		else id = e.target;
		var dot = eval(id.objRef);
		
		if(dot.action2 == "toggle") dot.toggle(e);
		else eval(dot.action2);
	  }
	  
	  // (G$) Change  - Allow For Hover Interaction Part II
	  this.doAction3 = function(e) {		  
		if(document.all) id = window.event.srcElement;
		else id = e.target;
		var dot = eval(id.objRef);

		eval(dot.action3);
	  }
	
	/***   Changes here change the events that trigger the actions   ***/

	 getObjectRef(this.ref).onmouseup = this.doAction2;
	 getObjectRef(this.ref).onmouseover = this.doAction;
	 getObjectRef(this.ref).onmouseout = this.doAction3;
	  
	  this.slide = function(xx,yy,func) {
		if(!func) func = "";
		var px = this.parent.x();
		var py = this.parent.y();
		
		var x = xx - this.x() + px;
		var y = yy - this.y() + py;
		
		var d = sqrt(square(xx-this.x() + px) + square(yy-this.y() + py));
		
		var v = d/8;
		if(v<1) v = 1;
			
		if( (Math.abs(x) < v) && (Math.abs(y) < v) ) {
			if(debug) alert('Math.abs: ' + x + "--" + y + "--" + v + "::" + xx + "::" + yy);
		
		  	moveMe(this.ref,xx + px,yy + py);      
		  	if(func != "") eval(func);
		} 
		
		else {   
		  	var a = round(atan(x,y));
		  	dx = round(v * cos(degToRad(a)));
		 	dy = round(v * sin(degToRad(a)));
		     
		  	this.moveBy(dx,dy);
		  	setTimeout(this.name + ".slide(" + xx + "," + yy + ", '" + func + "');",10);    
		}
	  }
		
	  // Preloading the windows filters.
	  if (menuCount==0 && document.all) 
	  {
		  document.all[this.ref].style.filter = "alpha (opacity=100)";
	  }
	  
	  menuCount++;
	  return this;
	}
		
	/***   Math functions   ***/
	function square(x) { return (x*x); }
	function sqrt(x) { return Math.sqrt(x); }
	function round(x) { return Math.round(x); }
	function rand(x,y) { return (round(Math.random()*(y-x)) + x); }
					
	function cos(x) { return Math.cos(x) }
	function sin(x) { return Math.sin(x) }
		
	function degToRad(x) { return ( x/(360/(2*pi)) ); }
	function radToDeg(x) { return ( x*(360/(2*pi)) ); }
	
	function atan(s,t) {
	  if( s == 0.0 && t > 0.0)
		angle = 90.0;
	  else if(s == 0.0 && t < 0.0) 
		angle = 270.0;
	  else if (s < 0.0 ) 
		angle = 180.0 + radToDeg(Math.atan(t/s));
	  else if (s > 0.0 && t < 0.0)
		angle = 360.0 + radToDeg(Math.atan(t/s));
	  else {
		if(s==0.0) s=0.00001;
		angle = radToDeg(Math.atan(t/s));
	  }
	  if(angle < 0.0) angle += 360.0;
	  return angle;
	}
	
	
	/***   Menu Stuff  ***/
	if(debug) alert('setMenuUp()');
	
	//get dimentions of the page
	if(document.all) pageWidth = document.body.offsetWidth-20;
	else pageWidth = innerWidth;
	if(document.all) pageHeight = document.body.offsetHeight-4;
	else pageHeight = innerHeight;
	
	//Create main menu
	var menu = new menuObject("menu", xClick + xWidth/2 ,yClick + yHeight/2,"Links",pageHeight/2);
	
	//show menu and caption
	menu.show();
	menu.showCaption();
	
	//Add submenus
	menu.addItem("Across Bulgaria","toggle", "toggle", "null", 1);
	menu.addItem("Home","toggle", "window.open('index.htm','_top')", "null", 2);
	menu.addItem("Refresh","toggle", "window.open('search-Across-Bulgaria.htm','_top')", "null", 3);
	
	// Still Images
	menu.subMenus[0].addItem("Travel Info","toggle", "toggle", "null", 1);
	menu.subMenus[0].addItem("Travel News","toggle", "toggle", "null", 2);
	menu.subMenus[0].addItem("","toggle", "toggle", "null", 3);
	menu.subMenus[0].addItem("","toggle", "toggle", "null", 4);
	menu.subMenus[0].addItem("","toggle", "toggle", "null", 5);
	menu.subMenus[0].addItem("","toggle", "toggle", "null", 6);



	// Still Images >> End of Semester Shows
	menu.subMenus[0].subMenus[0].addItem("Info","showImage('../Bulgarian-pictures/pirin-mountain-2.jpg', 'Pirin Mountain, Unesco Natural site')", "window.open('Bulgaria-travel-information.htm','_top')", "hideImage()", 1);
	menu.subMenus[0].subMenus[0].addItem("Advice","showImage('../Bulgarian-pictures/sofia-the-parliament.jpg', 'Sofia, The Parliament')","window.open('Bulgaria-travel-advice.htm','_top')", "hideImage()", 2);
	menu.subMenus[0].subMenus[0].addItem("Tips","showImage('../Bulgarian-pictures/tryavna-bridge.jpg', 'Tryavna, The Upper Bridge (1844)')","window.open('Bulgaria-travel-tips.htm','_top')",  "hideImage()",  3);
	menu.subMenus[0].subMenus[0].addItem("Landmarks","showImage('../Bulgarian-pictures/tryavna-view.jpg', 'Tryavna, view')","window.open('Bulgaria-landmarks.htm','_top')", "hideImage()",  4);
	
	// Still Images >> Groove Appearances
	menu.subMenus[0].subMenus[1].addItem("Tours","showImage('../Bulgarian-pictures/sofia-st-nicolas-church.jpg', 'Sofia, The St Nicolas Church')", "window.open('Bulgaria-cultural-tours.htm','_top')", "hideImage()",  1);
	menu.subMenus[0].subMenus[1].addItem("Videoclips","showImage('../Bulgarian-pictures/bansko-house.jpg', 'Bansko, Bansko-style house')", "window.open('Bulgaria-video-clips.htm','_top')", "hideImage()", 2);
	menu.subMenus[0].subMenus[1].addItem("News","showImage('../Bulgarian-pictures/shumen-history-museum-thracian-tomb.jpg', 'Shumen, The History Museum, Thracian-tomb')", "window.open('Bulgaria-travel-news.htm','_top')", "comingSoon('Image')", "hideImage()",3);

	

		
/* *******************************************************************/
/*						Additions 
/* *******************************************************************/ 
	var mouseX;
	var mouseY;
	var debug3 = false;
	var debug4 = false;
	
	
	function comingSoon(message){	
		alert("The " + message + " is simply a test");
	}
	
	function showImage(image, caption){
		if (debug3) alert('showImage');
				
		if(debug4) alert("(" + mouseX + ", " + mouseY + ")");
	
		el = document.getElementById("space");
		el.innerHTML = "<div class=\"figure\" id=\"popup\" style=\"left:" + (mouseX) + "; top:" + (mouseY) + ";\"><p><img class=\"scaled\" src=\"images/" + image + "\"></img></p><p>" + caption + "</p></div>";		
	}
	
	function hideImage(){
		if (debug3) alert('hideImage');
		
		el = document.getElementById("space");
		el.innerHTML = "";
	}
	
	function getMouseXY(e) 
	{ 
		if (!e) 
		{
			if(debug3) alert("!e");
			e = window.event; // works on IE, but not NS (we rely on NS passing us the event)
		}
		
		if (e)
		{ 
			if (e.pageX || e.pageY)
			{ // this doesn't work on IE6!! (works on FF,Moz,Opera7)
			  mouseX = e.pageX;
			  mouseY = e.pageY;
			}
		
			else if (e.clientX || e.clientY)
			{ // works on IE6,FF,Moz,Opera7
			  mouseX = e.clientX + document.body.scrollLeft;
			  mouseY = e.clientY + document.body.scrollTop;
			}  
		}
		
		if(debug4) alert("(" + mouseX + ", " + mouseY + ")");
	}

