////////////////////////////////////////////////////////////

var WinUtils = {
	
	objBody: null,
	admin_win: null,
	overlay_win: null,
	pageSize: null,
	windowScroll: null,
	overlayZindex: 100,
	adminZindex: 101,
	
	overlayScreen: function() {
		this.objBody = document.body;
		this.overlay_win = document.createElement("div");	
		this.overlay_win.setAttribute('id', 'admin_overlay');
		this.overlay_win.className = 'overlay';
		
		this.pageSize = this.getPageSize(this.overlay_win.parentNode);	
		
		this.overlay_win.style.display = 'block';
		this.overlay_win.style.position = 'absolute';
		this.overlay_win.style.height = this.pageSize.pageHeight + 'px';
		this.overlay_win.style.width = '100%';
		this.overlay_win.style.top = 0 + 'px';
		this.overlay_win.style.left = 0 + 'px';
		this.overlay_win.style.zIndex = this.overlayZindex;
		this.objBody.insertBefore(this.overlay_win, this.objBody.firstChild);
		return this.overlay_win;
	},
	addWindow: function(w) {
		this.overlayScreen();
		
		this.objBody = document.body;
		this.admin_win = document.createElement("div");
		this.admin_win.setAttribute('id','admin_win');
		this.admin_win.className = 'overlay_win';
		
		this.pageSize = this.getPageSize(this.admin_win.parentNode);
		
		this.admin_win.style.display = 'block';
		this.admin_win.style.position = 'absolute';
		this.admin_win.style.width = w + "px";
		this.admin_win.style.top = 0 + 'px';
		this.admin_win.style.left = ((this.pageSize.pageWidth - w)/2) + 'px';
		this.admin_win.style.zIndex = this.adminZindex;
		this.admin_win.innerHTML = '<div id="overlay_response"><div style="padding:10px;"><strong><i>Loading Data ...</i></strong></div></div>';
		
		this.objBody.appendChild(this.admin_win);		
	},
	addContent: function(content) {
		$('overlay_response').innerHTML = content;		
		/* Hack to fix padding issue */
		if(this.pageSize.pageHeight < $('admin_win').getHeight()) {
			// Hack to fix centering window when scrollbar appears.
			$('admin_win').style.left = ((this.pageSize.pageWidth - $('admin_win').getWidth())/2) - 8 + 'px';
		} else {
			// Hack to fix centering window when scrollbar appears.
			$('admin_win').style.left = ((this.pageSize.pageWidth - $('admin_win').getWidth())/2) + 'px';			
		}
		this.resizeOverlay();
	},
	resizeOverlay: function() {
		if(this.pageSize.pageHeight < $('admin_win').getHeight()) {
			$('admin_overlay').style.height = $('admin_win').getHeight() + 40 + 'px';
		}
	},
	closeAll: function() {
		this.objBody.removeChild(this.overlay_win);
		this.objBody.removeChild(this.admin_win);
	},
   	getPageSize: function(parent){
    	parent = parent || document.body;              
    	var windowWidth, windowHeight;
    	var pageHeight, pageWidth;
    	if (parent != document.body) {
      		windowWidth = parent.getWidth();
      		windowHeight = parent.getHeight();                                
      		pageWidth = parent.scrollWidth;
      		pageHeight = parent.scrollHeight;                                
    	} else {
      		var xScroll, yScroll;
      		if (window.innerHeight && window.scrollMaxY) {  
        		xScroll = document.body.scrollWidth;
        		yScroll = window.innerHeight + window.scrollMaxY;
      		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        		xScroll = document.body.scrollWidth;
        		yScroll = document.body.scrollHeight;
      		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        		xScroll = document.body.offsetWidth;
        		yScroll = document.body.offsetHeight;
      		}
	      	if (self.innerHeight) {  // all except Explorer
        		windowWidth = self.innerWidth;
        		windowHeight = self.innerHeight;
      		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        		windowWidth = document.documentElement.clientWidth;
        		windowHeight = document.documentElement.clientHeight;
      		} else if (document.body) { // other Explorers
        		windowWidth = document.body.clientWidth;
        		windowHeight = document.body.clientHeight;
      		}
      		// for small pages with total height less then height of the viewport
      		if(yScroll < windowHeight){
        		pageHeight = windowHeight;
      		} else { 
        		pageHeight = yScroll;
      		}
      		// for small pages with total width less then width of the viewport
      		if(xScroll < windowWidth){  
        		pageWidth = windowWidth;
      		} else {
        		pageWidth = xScroll;
      		}
    	}             
    	return {pageWidth: pageWidth ,pageHeight: pageHeight , windowWidth: windowWidth, windowHeight: windowHeight};
  	}
};

var Tips = {
	ldr_tip: null,
	tool_tip: null,
	pos: null,
	r_area: null,
	b_area: null,
	
	tip_show: function(e,tip_width) {
		var pageSize = WinUtils.getPageSize();
		// Create tip obj.
		var objBody = document.body;
		// Create tip div
		this.ldr_tip = document.createElement("div");
		this.ldr_tip.setAttribute('id','ldr_tip');
		this.ldr_tip.className = 'tool_tip';
		// Create temp tip div
		this.tool_tip = document.createElement("div");
		this.tool_tip.setAttribute('id','tool_tip');
		this.tool_tip.className = 'tool_tip';
		// Link Details
		var url = e.href;
		var id = e.id;
		var title = e.name;
		var tipW = tip_width;		
		// Position of Link
		this.pos = Position.page($(id));
		// Width of page
		var pageW = pageSize.pageWidth;
		var pageH = pageSize.pageHeight;
		// Get area from link to righ side of page
		this.r_area = pageW - this.pos[0];
		this.b_area = pageH - this.pos[1];		
		// Calculate Position
		if(this.r_area > (tipW + 75)) {
			this.tool_tip.innerHTML = "<div id='tip_close_left'>" + title + "</div><div id='tip_response'></div>";
			this.ldr_tip.innerHTML = "<div id='tip_close_left'>" + title + "</div><div id='ldr_response'><div class='tip_loader'></div></div>";
			var tOffset = $(id).getWidth() + 11;
			var posX = this.pos[0] + tOffset;
			var posY = this.pos[1] - 3;
		} else {
			this.tool_tip.innerHTML = "<div id='tip_close_right'>" + title + "</div><div id='tip_response'></div>";
			this.ldr_tip.innerHTML = "<div id='tip_close_right'>" + title + "</div><div id='ldr_response'><div class='tip_loader'></div></div>";
			var posX = this.pos[0] - (tipW + 5);
			var posY = this.pos[1] - 3;
		}		
		// Start Styling
		this.ldr_tip.style.position = 'absolute';
		this.ldr_tip.style.display = 'block';
		this.ldr_tip.style.width = tipW + "px";
		this.ldr_tip.style.left = posX + 'px';
		this.ldr_tip.style.top = posY + 'px';
		this.ldr_tip.style.zIndex = 103;
		objBody.appendChild(this.ldr_tip);
		// Start Styling Temp Tip
		this.tool_tip.style.position = 'absolute';
		this.tool_tip.style.display = 'block';
		this.tool_tip.style.visibility = 'hidden';
		this.tool_tip.style.width = tipW + "px";
		this.tool_tip.style.left = posX + 'px';
		this.tool_tip.style.top = 0 + 'px';
		this.tool_tip.style.zIndex = 105;
		objBody.appendChild(this.tool_tip);
		// Get Content
		new Ajax.Request(url, { 
			method: 'get', 
			onSuccess: function(transport){
				var response = transport.responseText || "no response text";
				$('tip_response').innerHTML = response;
				var tipH = $('tool_tip').getHeight();
				if(Tips.b_area < (tipH + 75)) {
					$('tool_tip').style.top = (Tips.pos[1] - ((tipH + 75) - Tips.b_area)) + 'px';										
				} else {
					$('tool_tip').style.top = Tips.pos[1] - 3 + 'px';
				}
				$('tool_tip').style.visibility = 'visible';
			},
			onFailure: function(){ alert('Something went wrong...') }
		});
		objBody.removeChild(this.ldr_tip);
	},
	tip_hide: function() {
		// Create tip obj.
		var objBody = document.body;
		objBody.removeChild(this.tool_tip);
	}
};