/* This file defines the Pop-Up library
**
** The goal of this library is ease of use and speed
*/


//POP namespace
SKEL.POP = {

	//show the popup
	show: function (divId,anchorId,offsetX,offsetY,orientation,relative,insideRelativeObject){
		// insideRelativeObject should be set to true if your popup div is located inside an element marked as "position: relative;", false otherwise
		//orientation default = TOPLEFT
		//relative default = TOPLEFT

		//this will prevent windows popping past the right/bottom of the screen
		allowPopupPastScreen = false;

		if(arguments.length > 6){
			//they overrode it
			allowPopupPastScreen = arguments[6];
		}

		//div.cloneNode(true)
		if(typeof(divId) == 'string'){
			div = document.getElementById(divId);
		} else {
			div = $(divId).get(0);
		}
		if(typeof(anchorId) == 'string'){
			anchor = document.getElementById(anchorId);
		} else {
			anchor = $(anchorId).get(0);
		}

		//if the element or anchor is bad, return false
		if(!div || !anchor)
			return false;

		//popup the div attached to the anchor via the orientation and offset

		//if we need to figure out the offsets of the element, then we need to first display it... sucks but oh well
		// this will cause the element to "flash" at it's innitial position
		// to prevent this flashing let's set the element behind our regular display
		var oldZIndex = div.style.zIndex;
		div.style.display  = 'block';
		div.style.zIndex = '-20'; //hide the div behind our main display
		SKEL.POP.anchor(div,anchor,offsetX,offsetY,orientation,relative,allowPopupPastScreen,insideRelativeObject);
		div.style.display  = 'block';
		div.style.zIndex = oldZIndex; //set whatever the zindex was back to it
	},

	//hide the popup
	hide: function (divId){
		//div = document.getElementById(divId);
		//div.style.display  = 'none';
		if(typeof(divId) == 'string'){
			div = document.getElementById(divId);
		} else {
			div = $(divId);
		}
		$(div).hide();
	},

	//moves a div to the anchor'd div
	anchor: function (div,anchor,offsetX,offsetY,orientation,relative,allowPopupPastScreen,insideRelativeObject){
		if(insideRelativeObject){
			var linkPos = SKEL.POP.findRelativePos(anchor);
			var divPos = SKEL.POP.findRelativePos(div);
		} else {
			var linkPos = SKEL.POP.findAbsolutePos(anchor);
			var divPos = SKEL.POP.findAbsolutePos(div);
		}

		//we need to make the position absoute in order for it to move to the coord
		div.style.position = 'absolute';

		var addX = 0;
		var addY = 0;

		switch(orientation){
			case "TOPRIGHT":
				addX = linkPos.width;
				break;
			case "BOTTOMLEFT":
				addY = linkPos.height;
				break;
			case "BOTTOMRIGHT":
				addX = linkPos.width;
				addY = linkPos.height;
				break;
			case "LEFT":
				addY = (linkPos.height / 2);
				break;
			case "RIGHT":
				addX = linkPos.width;
				addY = (linkPos.height / 2);
				break;
			case "BOTTOM":
				addX = (linkPos.width / 2);
				addY = linkPos.height;
				break;
			case "TOP":
				addX = (linkPos.width / 2);
				break;
			default: //TOPLEFT
				break;
		}

		/*
		** TODO look at the TOP,LEFT,BOTTOM,RIGHT and compare the orientation with the relative
		**  determine if an adjustment should be made
		*/
		switch(relative){
			case "BOTTOMLEFT":
				//move the element UP as tall as the element is
				addY -= divPos.height;
				break;
			case "BOTTOMRIGHT":
				addY -= divPos.height;
				addX -= divPos.width;
				break;
			case "TOPRIGHT":
				addX -= divPos.width;
				break;
			case "LEFT":
				addY += (divPos.height / 2);
				break;
			case "RIGHT":
				addY += (divPos.height / 2);
				addX -= divPos.width;
				break;
			case "TOP":
				addX -= (divPos.width / 2);
				break;
			case "BOTTOM":
				addX -= (divPos.width / 2);
				addY -= divPos.height;
				break;
			default: //TOPLEFT
				break;
		}

		//now that we have all the offsets, move the element
		if(allowPopupPastScreen){
			div.style.top = linkPos.y + offsetY + addY + "px";
			div.style.left = linkPos.x + offsetX + addX + "px";
		} else {
			var dim = SKEL.UTIL.getDimensions();


			//see if the computed value is "off the screen"
			var computedX = linkPos.x + offsetX + addX;
			var computedY = linkPos.y + offsetY + addY;

			//alert(dim.screenX + " : " + computedX + " : " + div.offsetWidth + " : " + (computedX + div.offsetWidth) );

			if((computedX + div.offsetWidth) > dim.screenX){
				//it's overlapping, so nudge it back to the left + 20 pixels (for padding)
				computedX = computedX - (((computedX + div.offsetWidth) - dim.screenX) + 20);
			}

			div.style.top = computedY + "px";
			div.style.left = computedX + "px";
		}
	},

	//returns the absolute coords based on where it is on the page (works w/ scrolled content)
	findAbsolutePos: function(el){
		var SL = 0, ST = 0;
		var is_div = /^div$/i.test(el.tagName);
		if (is_div && el.scrollLeft)
			SL = el.scrollLeft;
		if (is_div && el.scrollTop)
			ST = el.scrollTop;
		var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
		r.width = el.offsetWidth;
		r.height = el.offsetHeight;

		if (el.offsetParent) {
			var tmp = this.findAbsolutePos(el.offsetParent);
			r.x += tmp.x;
			r.y += tmp.y;
		}
		return r;
	},

	//this is just like the findAbsolutePos, but stops if a parent's css is "position:relative;"
	findRelativePos: function(el){
		var SL = 0, ST = 0;
		var is_div = /^div$/i.test(el.tagName);
		if (is_div && el.scrollLeft)
			SL = el.scrollLeft;
		if (is_div && el.scrollTop)
			ST = el.scrollTop;
		var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
		r.width = el.offsetWidth;
		r.height = el.offsetHeight;

		if (el.offsetParent) {
			if($(el.offsetParent).css('position') != 'relative'){
				//stop if we have a relative parent
				var tmp = this.findRelativePos(el.offsetParent);
				r.x += tmp.x;
				r.y += tmp.y;
			} else {
				r.x += 0;
				r.y += 0;
			}
		}
		return r;
	}

}

