/*
	Collection of scripts to swap contents, scale container and fade in new content
	for use on ekkoTV.com
	
	written by robertFrancis
	version 1 - 05.2008
*/


	function removeElement(toItem) {
		var el = toItem.getEl();
		el.parentNode.removeChild(el);
	}
// Dom object taken from http://www.dustindiaz.com/add-remove-elements-reprise/
var Dom = {
	get: function(el) {
		if (typeof el === 'string') {
			return document.getElementById(el);
		} else {
			return el;
		}
	},
	add: function(el, dest) {
		var el = this.get(el);
		var dest = this.get(dest);
		dest.appendChild(el);
	},
	remove: function(el) {
		var el = this.get(el);
		el.parentNode.removeChild(el);
	}
};

// base functions
function hideMe(toItem){
	YAHOO.util.Dom.setStyle(toItem, 'display', 'none');
};
function showMe(toItem){
	YAHOO.util.Dom.setStyle(toItem, 'display', 'block');
}
function fadeIn(toItem){
	YAHOO.util.Dom.setStyle(toItem, 'opacity', 0);
	var myAnim = new YAHOO.util.Anim(toItem, {opacity: { to: 1 }}, 1.5, YAHOO.util.Easing.easeOut);
	myAnim.animate();
};


function getHeight(toItem){
	// .offsetHeight
	var origMargin = YAHOO.util.Dom.getStyle(toItem, 'margin-top');
	YAHOO.util.Dom.setStyle(toItem, 'margin-top', '-2000px');
	showMe(toItem);
	var itemHeight = document.getElementById(toItem).offsetHeight;
	hideMe(toItem);
	YAHOO.util.Dom.setStyle(toItem, 'margin-top', origMargin);
	return itemHeight;
};

var swapTemp = function(){
	for (t=0; t < showArray.length; t++){
		if (t == 0){
			YAHOO.util.Dom.setStyle(showArray[t], 'display', 'block');
			Dom.remove('tmpDiv');
			fadeIn(showArray[t]);
		} else {
			YAHOO.util.Dom.setStyle(showArray[t], 'display', 'block');
		}
	}
};

var showItems = function() {
	for (t=0; t < showArray.length; t++){
		if (t == 0){
			YAHOO.util.Dom.setStyle(showArray[t], 'display', 'block');
			fadeIn(showArray[t]);
		} else {
			YAHOO.util.Dom.setStyle(showArray[t], 'display', 'block');
		}
	}
}
/* 
SwapExpand function
passed variables are Arrays of the DIV IDs to be effected by this function
the DIV in the [0] of the array should be the main item, as it's used to set the scale sizes and gets the fadeIn at the end
*/
function swapExpand(toHide, toShow){
	hideArray = toHide;
	showArray = toShow;
	// toHide
	var oldHeight = getHeight(toHide[0]);
	// hides elements
	for (q=0; q < toHide.length; q++){
		hideMe(toHide[q]);
	}
	// toShow
	var newHeight = getHeight(toShow[0]);
	// add temp div
	var tmpDiv = document.createElement('div');
	tmpDiv.id = "tmpDiv";
	if(oldHeight < newHeight){
		var tmpChild = document.getElementById(toShow[0]);
		Dom.add(tmpDiv, tmpChild.parentNode);
		var myExpand = new YAHOO.util.Anim('tmpDiv', {height: {from: oldHeight, to: newHeight}}, 0.5, YAHOO.util.Easing.easeOut);
		myExpand.onComplete.subscribe(swapTemp);
		myExpand.animate();
	} else {
		for (t=0; t < showArray.length; t++){
			if (t == 0){
				YAHOO.util.Dom.setStyle(showArray[t], 'display', 'block');
				fadeIn(showArray[t]);
			} else {
				YAHOO.util.Dom.setStyle(showArray[t], 'display', 'block');
			}
		}
	}
	
};
function goExpand (toScale, toHide, toShow){
	hideArray = toHide;
	showArray = toShow;
	// hides elements
	for (q=0; q < toHide.length; q++){
		hideMe(toHide[q]);
	}
	// expand to cPod_main
	var myExpand = new YAHOO.util.Anim(toScale, {height: {from: 215, to: 400}}, 0.5, YAHOO.util.Easing.easeOut);
	myExpand.onComplete.subscribe(showItems);
	myExpand.animate();
}
function goShrink (toScale, toHide, toShow){
	hideArray = toHide;
	showArray = toShow;
	// hides elements
	for (q=0; q < toHide.length; q++){
		hideMe(toHide[q]);
	}
	// expand to cPod_main
	var myExpand = new YAHOO.util.Anim(toScale, {height: {from: 400, to: 215}}, 0.5, YAHOO.util.Easing.easeOut);
	myExpand.onComplete.subscribe(showItems);
	myExpand.animate();
}

/*
functions for showing login/register overlays
*/
function showLogin (){
	hideMe('flash_div');
	showMe('overlay');
	hideMe('reg_div');
	showMe('box');
	initPop();
};
function showReg (){
	hideMe('flash_div');
	showMe('overlay');
	showMe('reg_div');
	showMe('box');
	initPop();
};
function closeLogin (){
	hideMe('overlay');
	hideMe('box');
	showMe('flash_div');
}
var loginItems = new Array(new Array('regFPO'),new Array('reg_div'), new Array('thanks_div'));

// center box horizontaly
function centerWidth(toCenter) {
	var clientW = YAHOO.util.Dom.getViewportWidth();
	var itemW = document.getElementById(toCenter).offsetWidth;
	
	var newX = (( clientW - itemW ) * 0.5 );
	YAHOO.util.Dom.setStyle(toCenter, 'display', 'block');
	YAHOO.util.Dom.setStyle(toCenter, 'position', 'absolute');
	YAHOO.util.Dom.setStyle(toCenter, 'zIndex', '220');
	YAHOO.util.Dom.setX(toCenter, newX);
}
function initPop(){
    centerWidth('box');
	YAHOO.util.Dom.setY('box', 90);
	// set overlay to document height from 100%
	var clientH = YAHOO.util.Dom.getDocumentHeight();
	YAHOO.util.Dom.setStyle('overlay', 'height', clientH + 'px');
}

