var myAccord
var lastBullet = false;
var lastElem = false;
var plus = new Image();
	plus.src = "/templates/images/structure/plus.gif";
var minus = new Image();
	minus.src = "/templates/images/structure/minus.gif";	
var hideIT = new Image();
	hideIT.src = "/templates/images/structure/thumb_upB.gif";
function showThis(elem, elem2) {
	lastElem = this.elements[elem2];
	if (elem != lastBullet) {
		$E('img', elem).src = minus.src;
		if (lastBullet) $E('img', lastBullet).src = plus.src;
		lastBullet = elem;
	} else {
		$E('img', elem).src = plus.src;
		lastBullet = false;
	}
}

function hideThis(elem, elem2) {
		$E('img', elem).src = plus.src;
}

var popIt;
var aBox2;
window.onload = function() {
	
	popIt = new popOver( $("userBar"), $("popMenu"),$("userBar"),  250); 
	popIt.setMethods(0,0).setEventMethod(3).setBorder("2px solid #cccccc");
	
	aBox2 = new popOver( $("notelaySurvey"), $("aBox2"), "none",  350); 
	aBox2.setMethods(2,2).setEventMethod("none").setBorder("2px solid #666666").setModal(true);
	
	var bullets = $S(".catBullets");
	for (var i =0; i<bullets.length; i++) {
		bullets[i].src = plus.src;
	}
	myAccord = new Fx.Accordion( $S(".catHandler"), $S(".catHolder") , {'alwaysHide' : true, "duration":300, 'start' : 'all-closed', 'onActive': showThis, 'onBackground' : hideThis}  );
	
	if ( catOpen != "false" ) {
		var Hanlders = new $S(".catHolder");
		for (var i = 0; i<Hanlders.length; i++) {
			secHandlers = Hanlders[i].getElementsByTagName("a");
			for (var j=0; j<secHandlers.length; j++) {
				if ( $(secHandlers[j]).hasClass(catOpen) ) {
					secHandlers[j].setAttribute("class", "catSelected")
					secHandlers[j].setAttribute("id", "catSelected")					
					$S(".catHandler")[i].getElementsByTagName("img")[0].src = minus.src;
					myAccord.showThisHideOpen(i);
				}
			}
		}
	}
	
	var hideElements = $S(".hideIT");
	for (var i = 0; i<hideElements.length; i++) {
		hideElements[i].src = hideIT.src;
		hideElements[i].parentNode.href = "javascript:aBox2.show();"
	}
	

    var myTips = new Tips($S('.openToolTip'), {
        maxTitleChars: 50, //I like my captions a little long
        maxOpacity: .95, //let's leave a little transparancy in there
		className: "toolTipClass"
    });
	
}



	var popIt
	function popOver(opener, opened, handler, duration) {
		/*Top Method:
			0: Relative to openers top
			1: Relative to openers bottom
			2: Relative to page (centered)
			
		  Left Method:
			0: Relative to openers left
			1: Relative to openers center
			2: Relative to page (centerd)
			3: Relative to openers right
		
		*/		
		//Constructor
			//-----------------------------------------------------------------------------------------------
			this.opener = opener;
			this.opened = opened;
			this.handler = handler;
			this.topMethod = 0;
			this.leftMethod = 0;
			this.inMotion = false;
			this.opend = false;
			this.eventMethod = 0;
			this.modal = false;
			this.modalR;



			//Making wrapper element & setting css style
			//-----------------------------------------------------------------------------------------------
			this.wrapper = new Element("div").injectAfter(this.opened).adopt(this.opened).setStyles({
				visibility: "visible",
				position:"absolute",
				top: "-1000px",
				left: "-1000px",
				border: "2px solid #000000",
				"background-color": "transparent",
				overflow: "hidden"
				
			});
			this.opened.setStyles({
				width: "auto",
				height: "auto",
				visibile: "visible",
				display: "block",
				position: "static"
			})
			this.opened.setOpacity(0);
			//-----------------------------------------------------------------------------------------------
			//Making the effect 
			//-----------------------------------------------------------------------------------------------			
			this.effect1 = this.wrapper.effects({duration: duration || 300, wait:true ,  transition: Fx.Transitions.quartIn });					
			this.effect2 = this.wrapper.effects({duration: duration || 300, wait:true ,  transition: Fx.Transitions.cubicIn });								
			
			this.effect1.options.instance = this;
			this.effect2.options.instance = this;
			
			//opacity object
			this.opacity = { omin: 0.5, omax: 1 }
			
			//setting elements needed styles
			//-this.wrapper.setStyles({ display:"none" } );
			//-this.opened.setStyles( { display: "none", width:"0", height:"0", position:"absolute", visibility:"visible" } )
		//End constructor
		
		//Both Methods setter
		this.setMethods = function(topMethod, leftMethod) {this.leftMethod = leftMethod; this.topMethod = topMethod; return this;}
		
		//TopMethod setter
		this.setTopMethod = function(method) 	{this.topMethod = method; return this;}

		//LeftMethod setter
		this.setLeftMethod = function(method) 	{this.leftMethod = method; return this;}
		
		//Setter for minimal opacity
		this.setMinOpacity = function(numb) 	{this.opacity.omin = this.bet01(numb); return this;}
		
		//Setter for maximal opacity 
		this.setMaxOpacity = function(numb)		{this.opacity.omax = this.bet01(numb); return this;}
		
		//Set both opacity
		this.setOpacity = function(numb1,numb2) {this.opacity.omin = this.bet01(numb1); this.opacity.omax = this.bet01(numb2); return this;}
		
		//Wrapper border setter
		this.setBorder = function(bDeclaration) {this.wrapper.setStyles( {border: bDeclaration} ); return this;}
		
		//Add modal property
		this.setModal = function(boolean) 		{this.modal = boolean; return this;}
		
		//This function returns value between 0 and 1
		this.bet01 = function(numb) {
				 if (numb > 1)					{return 1}
			else if (numb < 0) 					{return 0}
						  else					{return numb}
		}
		
		//LeftMethod getter
		this.getLeftMethod = function() {
				 if (this.leftMethod == 3)		{return this.openerPos.left + this.openerPos.width - this.openedPos.width; }
			else if (this.leftMethod == 2)		{return (Window.getWidth() - this.openedPos.width)/2;} 
			else if (this.leftMethod == 1)		{return this.openerPos.left}
								 	  else		{return (  (this.openerPos.left + (this.openerPos.width/2)) - (this.openedPos.width/2)  ) }
		}	
		
		//TopMethod getter
		this.getTopMethod = function()	{
			     if ( this.topMethod == 2 ) 	{return  ((Window.getHeight() - this.openedPos.height)/2)+Window.getScrollTop();} 
			else if ( this.topMethod == 1 ) 	{return  this.openerPos.top + this.openerPos.height;} 
			   						   else 	{return this.openerPos.top;}	
		}	

		//Recalculate objects positions
		this.recalculate = function() {
			this.openerPos =	{ 
				top: 	this.opener.getOffset("top"),
				left: 	this.opener.getOffset("left"),
				width:	this.opener.offsetWidth,
				height: this.opener.offsetHeight
			}
			this.openedPos = 	{  
				top:	this.opened.getOffset("top"),
				left:	this.opened.getOffset("left"),
				width:	this.opened.offsetWidth,
				height:	this.opened.offsetHeight
			}
		}
		
		//Opens the popOver menu
		this.show = function() {
			$clear(this.timer);
			if (!this.opend && this.inMotion == false) {
				this.opend = true;
				this.inMotion = true;
				this.effect1.options.onComplete = function() {
					this.options.instance.inMotion = false;
					this.options.instance.opened.setOpacity(1);
					if (this.options.instance.modal) {
						var modalN = new Element("div")	
						modalN.setStyles({ "background-color": "#000000", position:"absolute", left:"0px", top:"0px", width:"100%", height: Window.getScrollHeight()+"px" })
						modalN.setOpacity(0.7);
						this.options.instance.modalV = modalN.injectBefore(this.options.instance.wrapper);
					}					
				}
				this.recalculate();					
				this.effect1.custom({
					'height':	[this.openerPos.height,this.openedPos.height], 
					'width': 	[this.openerPos.width,this.openedPos.width], 
					'top': 		[this.openerPos.top-parseInt(this.wrapper.getStyle("border-top-width")), this.getTopMethod()], 
					'left': 	[this.openerPos.left-parseInt(this.wrapper.getStyle("border-left-width")), this.getLeftMethod()], 
					'opacity': 	[this.opacity.omin,this.opacity.omax]
				});
			}
		}
		
		//Hides the popOver menu
		this.hide2 = function() {
			$clear(this.timer);
			if (this.opend && this.inMotion == false) {
				this.inMotion = true
				this.effect2.options.onComplete = function() {
					this.options.instance.inMotion = false;
					this.options.instance.opend = false;
					this.options.instance.wrapper.setStyles({ 
						top: "-1000px",
						left: "-1000px",
						width: "auto",
						height: "auto"
					}) ;
				}
				if (this.modal) this.modalV.remove();
				this.opened.setOpacity(0);
				this.effect2.custom({ 
					'height': 	[this.openedPos.height, this.openerPos.height], 
					'width': 	[this.openedPos.width, this.openerPos.width], 
					'top': 		[this.opened.getOffset("top") ,this.openerPos.top-parseInt(this.wrapper.getStyle("border-top-width"))], 
					'left': 	[this.opened.getOffset("left"),this.openerPos.left-parseInt(this.wrapper.getStyle("border-left-width"))], 
					'opacity': 	[this.opacity.omax,this.opacity.omin] 
				});
			}
		}
		
		this.toggle = function() {
			if (this.opend) {
				this.hide2();
			} else {
				this.show();
			}
		}
		
		this.hide = function() {
			this.timer = this.hide2.delay(300, this);
		}
		
		//Set eventSetter
		this.setEventMethod = function(method) {
			if (method != "none" && this.handler != "none") {
				this.eventMethod = method;
				this.handler.onclick = {}; this.handler.onmouseover = {}; this.handler.onmouseout = {};
				this.opened.onclick = {}; this.opened.onmouseover = {}; this.opened.onmouseout = {};
				if (this.eventMethod == 1) {
					this.handler.onclick = 		this.toggle.bind(this);
				} else if (this.eventMethod == 3) {
					this.handler.onmouseover = 	this.show.bind(this);
					this.opened.onmouseover = 	this.show.bind(this);					
					this.opened.onmouseout = 	this.hide.bind(this);
				} else {
					this.handler.onmouseover = 	this.show.bind(this);
					this.opened.onmouseover = 	this.show.bind(this);
					this.opened.onmouseout = 	this.hide.bind(this);
					this.handler.onmouseout = 	this.hide.bind(this);
				}
			}
			return this;
		}
		this.setEventMethod(0);
	}
	
	function sBoxGetFocus(elem) {
		if (elem.value == "Search Notelay...")	{
			elem.value = "";
		}
	}
	
	function sBoxLostFocus(elem) {
		var values = elem.value
		if (values.trim()=="") {
			elem.value = "Search Notelay..."
		}
	}
	
	function submitSearch() {
		$("sForm").submit();
	}
