/**
 * Generates css controlled callout 
 * 
 * @author James Baker, Tom Bermel (25/07/08)
 */

if (!console) var console = {};
if (!console.log) console.log = {};

// set up Argos callout namespace
if (!argos) var argos = {};
if (!argos.callout) argos.callout = {};

argos.callout.calloutdisplay = {	

	//constants
	SITE_CONTAINER : "outerwrap",
	MAIN_CONTAINER : null, // all css inheritance from this id down
	SUB_CONTAINER : "calloutsub",
	DEFAULT_ALIGNED_CLASS : "calloutright",
	ALT_ALIGNED_CLASS : "calloutleft",
	SHOW_LINK : "calloutlink", 
	CLOSE_CLASS : "closebox", 
	CLOSE_LINK : "closelink",
	LEFT_OFFSET : 5, 	//x position from selected link
	TOP_OFFSET  : -20,	//y position from selected link
	ALERT_DELAY : 0,
	TITLE_TEXT : null,
	BODY_TEXT : null,
	LINK_TEXT : null,
	URL_TEXT : null,
	ACTIVE_CONTAINER : null,
		
	init : function() {
		// shorten namespace
		var callout = argos.callout.calloutdisplay;
		var activeLink = null;

		$("." + callout.SHOW_LINK).click(
			// grab content to display	
			function(){
			
				//assign matching identifier (rel value)
				var tempRel = $(this).attr("rel");
				
				//defensive: ensure identifier has been applied to callouts object within page
				//(link will follow href value if not)
				if (callouts[tempRel] != null) {
			
					//remove any existing messages if different link has been clicked
					if ((callout.MAIN_CONTAINER != null)&&(activeLink != this)) {
						$("#" + callout.MAIN_CONTAINER).remove();
					}
					
					// if new link clicked
					if (activeLink != this) {					
						//assign callout values from page
						callout.MAIN_CONTAINER = callouts[tempRel].MAIN_CONTAINER;
						callout.TITLE_TEXT =  callouts[tempRel].TITLE_TEXT;
						callout.BODY_TEXT =  callouts[tempRel].BODY_TEXT;
						callout.LINK_TEXT =  callouts[tempRel].LINK_TEXT;
						callout.URL_TEXT =  callouts[tempRel].URL_TEXT;

						callout.calloutBuild();	
					}
					
					$("#" + callout.MAIN_CONTAINER + " a." + callout.CLOSE_LINK).click(
						function(){

							$("#" + callout.MAIN_CONTAINER).hide();
							return false;
					   	}
					);
				
					callout.calloutPosition(this);
					
					// set last link clicked for re-positioning on window resize
					activeLink = this;
					$("#" + callout.MAIN_CONTAINER).show();	
	
					return false;
				}
			}
		);
				
		// left/right switch on resize
		window.onresize = function() {
			if (activeLink) {
				callout.calloutPosition(activeLink);
			}		
		}
	},
	
	calloutBuild : function() {
		// shorten namespace
 		var callout = argos.callout.calloutdisplay;
 		
		// generate the container
 		$('<div id="' + callout.MAIN_CONTAINER + '" ><div id="' + callout.SUB_CONTAINER + '" class="' + callout.DEFAULT_ALIGNED_CLASS + '"</div></div>').insertAfter($("#" + callout.SITE_CONTAINER));
 		
 		var calloutObject = document.getElementById(callout.MAIN_CONTAINER);
 		var calloutSubObject = document.getElementById(callout.SUB_CONTAINER);
		var subHTML = '<span class="top"></span>';
 		subHTML += '<span class="base"></span>';
 		subHTML += '<div class="body">';
 		subHTML += '<div class="content"><h2>' + callout.TITLE_TEXT + '</h2><p>' + callout.BODY_TEXT + '</p></div>';
 		subHTML += '<div class="calloutfwdurl"><a href="' + callout.URL_TEXT + '" title="Read more about the Producer Recycling Fund">' + callout.LINK_TEXT + '</a></div>';
 		subHTML += '<span class="closebox"><a href="#" title="close" class="closelink"></a></span>';
 		subHTML += '<span class="serif"></span>';
 		subHTML += '</div>';
  		
		// insert inner html 		
 		calloutSubObject.innerHTML = subHTML;
 	},
 	
	calloutPosition : function(obj) {
		// shorten namespace
 		var callout = argos.callout.calloutdisplay;
 		//establish base coords
		var posX = callout.getPageX(obj);
 		var posY = callout.getPageY(obj);
 		var posW = $(obj).width();
 		var posH = $(obj).height();
		var calloutObject = document.getElementById(callout.MAIN_CONTAINER);
 		var calloutWidth = $("#" + callout.MAIN_CONTAINER).width();
 			
 		posX += posW;
 		posX += callout.LEFT_OFFSET;
 		posY += callout.TOP_OFFSET;
 		
 		// x position - two tests for IE6 vs all other browsers 
 		var leftClearance = document.documentElement.clientWidth||window.innerWidth - posX;
 		
 		// if callout will not fit on the right of the link 
		if ((leftClearance - posX) < calloutWidth) {
			// set the callout to the left of link
			callout.toggleLeftRight(callout.ALT_ALIGNED_CLASS,callout.DEFAULT_ALIGNED_CLASS);
			calloutObject.style.left = posX - calloutWidth - posW - callout.LEFT_OFFSET + "px";
 		} else  { 
			// default: set callout to the right of link
			callout.toggleLeftRight(callout.DEFAULT_ALIGNED_CLASS,callout.ALT_ALIGNED_CLASS);
			
			//explicitly compensate for ff posX alignement bug in comparison table view only (
			if (($.browser.mozilla) && ($("body").hasClass("productcompare"))) {
			
				//adjust X		
				var ffWidthAdjust = ($(window).width() - $("#outerwrap").width());
				if (ffWidthAdjust >= 2) {
					posX = (posX+(ffWidthAdjust/2));
				}
			}
			calloutObject.style.left = posX + "px";
		}
		
		// y position
		//explicitly compensate for ff posY alignement bug in comparison table view only
		if(($.browser.mozilla) && ($("body").hasClass("productcompare"))) {
			//adjust y		
			posY = (posY+15);				 				 
		}
		
	
		calloutObject.style.top = posY + "px"
		//apply iframe
		$("#" + callout.MAIN_CONTAINER).bgiframe(); 
		
	},
	
	toggleLeftRight : function(aclass,rclass) {
		// shorten namespace
		var callout = argos.callout.calloutdisplay;
		$("#" + callout.SUB_CONTAINER).addClass(aclass);
		$("#" + callout.SUB_CONTAINER).removeClass(rclass);
	},
  	
  	//PPK's find element position (sets offset relative to page, not container)
	getPageX : function(obj) {
		var curleft = 0;
		if (obj.offsetParent) {
			while (obj.offsetParent) {curleft += obj.offsetLeft;obj = obj.offsetParent;}
		}
		else if (obj.x) curleft += obj.x;
		return curleft;
	},

	getPageY : function(obj) {
		var curtop = 0;
		if (obj.offsetParent) {
			while (obj.offsetParent) {curtop += obj.offsetTop;obj = obj.offsetParent;}
		}
		else if (obj.y) curtop += obj.y;
		return curtop;
	}
}