/**
*	Overlay.js
*	
*	@author William Moffett aka Que~
*	.@version 0.2
*
*	Some code was graciously lifted from litbox source based on scriptaculous and prototype.
*/

gameHover = function(){
	/**
	*	gameHover	
	*	add open in new window for anchors with the gameview targetAttribute
	*/
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName('a');
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];
		var relAttribute = String(anchor.getAttribute('rel'));
		var targetAttribute = String(anchor.getAttribute('target'));
		var uriAttribute = String(anchor.getAttribute('href'));
		if (anchor.getAttribute('href') && (targetAttribute.toLowerCase().match('gameview')))
		{	// we found one lets add our onclick function
			anchor.onclick = function () { 
				var pgamewindow=window.open(String(this.getAttribute('href')),'pgameview','location=yes,top=100,left=100,height=550,width=768,resizable=yes,scrollbars=yes,menubar=yes');	
				if (window.focus) { 
					pgamewindow.focus(); 
				} 
				return false;
			}
		}
	}
}			

Object.extend(Element, {
	/**
	*	Object.extend
	*	See Prototype.js line 1108 for more information.<b>
	*	
	*	Added getInnerHTML 10/23/06 - William
	*/
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setLeft: function(element,t) {
	   	element = $(element);
    	element.style.left = t +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	},
	getInnerHTML: function(element) {
		element = $(element);
		return element.innerHTML;
	}
	
});

function getPageScroll(){
	/**
	*	getPageScroll()
	*	Returns yScroll distance
	*/
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
	return yScroll;
}

function getPageSize(){
	/**
	*	getPageSize()
	*	Returns array with page width, height and window width, height
	*	Core code from - quirksmode.org
	*	Edit for Firefox by pHaez
	*/	
	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;
	}
	var windowWidth, windowHeight;
	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;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function findPosX(obj){
	/**
	*	Find X Pos
	*/
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}else if (obj.x){
		curleft += obj.x;
	}
	return curleft;
}

function findPosY(obj){
	/**
	*	Find Y Pos
	*/
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}else if (obj.y){
		curtop += obj.y;
	}
	return curtop;
}

Array.prototype.removeDuplicates = function () {
	/**	Extending built-in Array object
	*	- array.removeDuplicates()
	*/
	for(i = 1; i < this.length; i++){
		if(this[i][0] == this[i-1][0]){
			this.splice(i,1);
		}
	}
}

Array.prototype.empty = function () {
	/**	Extending built-in Array object
	*	- array.empty()
	*/
	for(i = 0; i <= this.length; i++){
		this.shift();
	}
}

var Gamebox = Class.create();
/**
*	Gamebox Class
*
*	@author William Moffett aka Que~
*/	
Gamebox.prototype = {
		/**
		*	Gamebox
		*/
	initialize: function() {
		/**
		*	Initialize	Gamebox
		*
		*	Create the Gamebox div
		*	Assign the div an ID of gamebox
		*	Set initial display to none
		*	Set onclick and onmouseout function to end the box.
		*/
		var objBody = document.getElementsByTagName("body").item(0);
		var objGamebox = document.createElement("div");
		objGamebox.setAttribute('id','gamebox');
		objGamebox.style.display = 'none';
		objGamebox.onclick = function() { Gamebox.end(); return false; }
		objGamebox.onmouseout = function(){ Gamebox.end(); return false; }
		objBody.appendChild(objGamebox);
	},
	start: function(obj) {	
	    /*
	    *	Start Gamebox
	    *	
	    *	Set Position of Gamebox and Display Gamebox 
	    *
	    *		Y
	    *		|
	    *	X---|---
		*		|   
		*		    
	    *	set vertical position.
	    *	
	    *	if( box(y) + gamebox(height) is less then  Scroll(y) + window(height) ){
	    * 		set gamebox Top  = Scroll(y) + window(height) - gamebox + 20px
	    *	}else{
	    *		set gamebox Top = box(y)
	    *	}
	    */
		if ((findPosY(obj) + Element.getDimensions('gamebox').height) > (getPageScroll() + getPageSize()[3])) {
		 
			Element.setTop('gamebox', ((getPageScroll() + getPageSize()[3]) - (Element.getDimensions('gamebox').height + 20)));
		} else {
			Element.setTop('gamebox', findPosY(obj));
		}
		/*
		*	if( box(x) + box(width) + gamebox(width) is greater then window(width) ){
		*		set gambox left = box(x) - gambox(width)
		*	}else{
		*		set gamebox left = box(x) + box(width)
		*	}
		*/
		if ((((findPosX(obj) + Element.getDimensions(obj).width) + Element.getDimensions('gamebox').width)) > getPageSize()[2]) {
			Element.setLeft('gamebox', ((findPosX(obj)) - Element.getDimensions('gamebox').width));
		} else {
			Element.setLeft('gamebox', (findPosX(obj) + Element.getDimensions(obj).width));
		}
		Element.show('gamebox');
	},
	end: function() {
		/**
		*	Hide Gamebox
		*/
		Element.hide('gamebox');
	}
}

function getGameInfo (obj,gid){
	/**
	*	getGameInfo
	*
	*	@param obj
	*	@param gid
	* 
	*	obj caller object
	*	gid requested game_id data
	*
	*	insert data into Gamebox from a div with an id ( gameinfo+gid ) and then display. 
	*/
	if(!$('gamebox')){	return false; }
	if(obj){
		Element.setInnerHTML('gamebox', Element.getInnerHTML('gameinfo_'+gid));
		Gamebox.start(obj);
	}	
}
	
function hideGameInfo (){
	/**
	*	hideGameInfo
	*
	*	@param none
	*
	*	Hide Gamebox
	*/
	if(!$('gamebox')){ return false; }
	Gamebox.end();	
}

function featureReview(mode){
	/*
	*	Show/Hide Feature Review 
	*
	*/
	if(mode == 'expand'){
		Element.hide('feature_review_entry');
		Element.show('feature_review_more');
	}else{
		Element.show('feature_review_entry');
		Element.hide('feature_review_more');
	}
}

var reviewprocess = 'false';
var currev_id = '';
function getGameReview (rev_id) {
	/*
	*	Show SubFeature Review 
	*/
	if(reviewprocess == 'true'){ return; }
	if(currev_id == rev_id){ return; }
	currev_id = rev_id;
	reviewprocess = 'true';
	Effect.BlindUp($('sf_review'), {
		duration:.3, afterFinish: function(){ 
			Element.setInnerHTML('subfeature_review', Element.getInnerHTML('review_'+rev_id));	
  			Effect.BlindDown($('sf_review'), {
  				duration:.9, afterFinish: function(){
  					reviewprocess = "false";
  				}    
  			}); 
  		}
  	});
}

function closeGameReview(){
	/*
	*	Hide SubFeature Review 
	*/
	Effect.BlindUp('sf_review', {
		duration:.9, afterFinish: function(){ 
			reviewprocess = "false"; 
			currev_id = ''; 
		} 
	});
}

/**
*	Init Game Box Class function
*/  
function initGamebox() { Gamebox = new Gamebox(); }
/**
*	Attach Events for initGamebox and  gameHover
*/
Event.observe(window, 'load', initGamebox, false);
Event.observe(window, 'load',gameHover, false);



