/*
Product Popups - Based on Sweet Titles (http://www.dustindiaz.com)
*/
var productPopups = { 
	xCord : 0,						// @Number: x pixel value of current cursor position
	yCord : 0,						// @Number: y pixel value of current cursor position
	tipElements : ['a'],	// @Array: Allowable elements that can have the toolTip
	obj : Object,					// @Element: That of which you're hovering over
	tip : Object,					// @Element: The actual toolTip itself
	active : 0,						// @Number: 0: Not Active || 1: Active
	
	init : function() {
		if ( !document.getElementById ||
			!document.createElement ||
			!document.getElementsByTagName ) {
			return;
		}
		var i,j;
		this.tip = $("productPopup");		// from $("id") is from [prototype.js]
		
		this.tip.style.top = '0';
		this.tip.style.visibility = 'hidden';
		var current = document.getElementsByClassName("prodtip");		
		// Returns an array of elements with the class name "prodtip" [prototype.js]
		var curLen = current.length;
				
		for ( j=0; j<curLen; j++ ) {
			//alert(current[j].href);	// but this one is more specific
			if( m = current[j].href.match( /^(.*)product_details\.php\?prod=(.+)$/ )) {
				var prdcode = m[2];
				//alert(prdcode);	// WORKS!!! - this is just the product code bit of the link... HURRAY!
				// Need to add some code here to extract the product code from the link
				// So that it can be passed to the ajax call function
				//addEvent(current[j],'mouseover',productPopups.getProductData( m[2] ));
				//addEvent(current[j],'mouseover',this.tipOver);	[addEvent.js]
				//addEvent(current[j],'mouseout',this.tipOut);
				//Event.observe(current[j], 'mouseover', this.getProductData(prdcode), false);
				Event.observe(current[j], 'mouseover', this.tipOver, false);
				Event.observe(current[j], 'mouseout', this.tipOut, false);
			} 
		}
	},
	
	updateXY : function(e) {
		if ( document.captureEvents ) {
			productPopups.xCord = e.pageX;
			productPopups.yCord = e.pageY;
		} else if ( window.event.clientX ) {
			productPopups.xCord = window.event.clientX+document.documentElement.scrollLeft;
			productPopups.yCord = window.event.clientY+document.documentElement.scrollTop;
		}
	},
	
	tipOut: function() {
		if ( window.tID ) {
			clearTimeout(tID);
		}
		if ( window.opacityID ) {
			clearTimeout(opacityID);
		}
		productPopups.tip.style.visibility = 'hidden';
	},
	
	checkNode : function() {
		var trueObj = this.obj;
		if ( this.tipElements.inArray(trueObj.nodeName.toLowerCase()) ) {	// inArray() [addEvent.js]
			return trueObj;
		} else {
			return trueObj.parentNode;
		}
	},
	
	tipOver : function(e) {
		productPopups.obj = this;
		tID = window.setTimeout("productPopups.tipShow()",500);
		productPopups.updateXY(e);
	},
	
	tipShow : function() {		
		//alert("tipShow() called");
		var scrX = Number(this.xCord);
		var scrY = Number(this.yCord);
		var tp = parseInt(scrY+15);
		var lt = parseInt(scrX+10);
		/*
		//var anch = this.checkNode();
		var anch = this.obj;
		var addy = '';
		var access = '';
		if ( anch.nodeName.toLowerCase() == 'a' ) {
			addy = (anch.href.length > 25 ? anch.href.toString().substring(0,25)+"..." : anch.href);
			var access = ( anch.accessKey ? ' <span>['+anch.accessKey+']</span> ' : '' );
		} else {
			addy = anch.firstChild.nodeValue;
		}
		this.tip.innerHTML = "<p>"+anch.getAttribute('tip')+"<em>"+access+addy+"</em></p>";
		//this.tip.innerHTML = "<p>"+anch.getAttribute('tip')+"</p>";
		*/
		if ( parseInt(document.documentElement.clientWidth+document.documentElement.scrollLeft) < parseInt(this.tip.offsetWidth+lt) ) {
			this.tip.style.left = parseInt(lt-(this.tip.offsetWidth+10))+'px';
		} else {
			this.tip.style.left = lt+'px';
		}
		if ( parseInt(document.documentElement.clientHeight+document.documentElement.scrollTop) < parseInt(this.tip.offsetHeight+tp) ) {
			this.tip.style.top = parseInt(tp-(this.tip.offsetHeight+10))+'px';
		} else {
			this.tip.style.top = tp+'px';
		}
		
		this.tip.style.visibility = 'visible';
		this.tip.style.opacity = '0.1';	// Define initial visibility e.g. 0.1 = barely visible; 1 = fully visible
		this.tipFade(10);
	},
	
	tipFade: function(opac) {
		var passed = parseInt(opac);
		var newOpac = parseInt(passed+10);
		var curFilter = ' progid:DXImageTransform.Microsoft.Shadow(color=gray,direction=135) '; // from CSS
		if ( newOpac < 100 ) {
			this.tip.style.opacity = '.'+newOpac;																		// Firefox/Mozilla
			this.tip.style.filter = "alpha(opacity:"+newOpac+") "+curFilter;				// Internet Explorer
			opacityID = window.setTimeout("productPopups.tipFade('"+newOpac+"')",40);	// Keep fading in until 100% visible
		}
		else { 
			this.tip.style.opacity = '1.0';
			this.tip.style.filter = "alpha(opacity:100) "+curFilter;
		}
	},
	
	getProductData: function(prodcode) {
		//alert("getProductData(" + prodcode + ") called.");
		var myAjax = new Ajax.Request(
			'productpopups.php?prod='+prodcode, 
			{
				method: 'get', 
				parameters: '', 
				onComplete: function(response) {
					//alert("returned data!");
					//alert(response.responseText);
					productData = eval('(' + response.responseText + ')');
					productPopups.loadProductData(productData);	// Show Product Tool Tip
				}
			});
	},
	
	loadProductData: function(data) {
		//alert("loadProductData() called");
		//alert(data);
		$("productPopupTitle").innerHTML 			= data['title'];
		$("productPopupCopyright").innerHTML 	= data['copyright'];
		$("productPopupFormat").innerHTML 		= data['format'];
		$("productPopupPrice").innerHTML 			= data['price'];
		$("productPopupSynopsis").innerHTML 	= data['synopsis'];	
	}

};

function popupLoader() {
	productPopups.init();
}

Event.observe(window, 'load', popupLoader, false);

