/*** global.js
	This file contains javascript that is used on all pages ***/

/* check to see if cookie is set (for flash) */
function checkAgeFlash() {	
	var c_name = "dragon_age";
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) { 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return true;
		} 
	}
	return false;
}


sfHover = function() {
	
	var navContainer = document.getElementById("navContainer");
	if( !navContainer )
	{
		return;
	}

	var sfEls = navContainer.getElementsByTagName("LI");

	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}

}


/*** TAB SWITCHING CLASS ***/
var tabSwap = new Class({
    initialize: function(containerID){
		this.tabItems = $$("#"+ containerID +" ul.toggleBtns li a");
		this.paneItems = $$("#"+ containerID +" .details .pane");
		this.setupTabs();
    },
    setupTabs: function() {
		this.tabItems.each(function(tabItem,index) {
			tabItem.addEvent("click", function(e) {
				e = new Event(e);
				this.clearTabs();
				this.setTab(tabItem);
				this.clearPanes(index);
				this.setPane(index);
				e.stop();
			}.bind(this));
		}.bind(this));	
	},
    clearTabs: function() {
		this.tabItems.each(function(tabItem,i) {
			if (tabItem.hasClass("On")) {
				tabItem.removeClass("On");
				return;
			}
		});
    },
	setTab: function(tabItem){
		if (!tabItem.hasClass("On")) {
			tabItem.addClass("On");
		}
	},
    clearPanes: function(activeIndex) {
		this.paneItems.each(function(paneItem,index) {
			if (activeIndex != index) {
				paneItem.setStyle("display","none");
			}
		});
    },
	setPane: function(activeIndex) {
		this.paneItems.each(function(paneItem,index) {
			if (activeIndex == index && paneItem.getStyle("display") != "block") {
				paneItem.setStyle("display","block");
			}
		}.bind(this));
	}
});

/*** Orgin Stories flash widget code ***/
var originFlash = new Class({
    initialize: function(flashVersion){
    	this.flashLayer = $("originFlash");
    	this.requiredVersion = flashVersion;
    	this.userVersion = swfobject.getFlashPlayerVersion().major;
    	this.checkFlashVersion(this.userVersion,this.requiredVersion);
    	this.sHeight = screen.height-60;
		this.sWidth = screen.width-10;
    },
    checkFlashVersion: function(version,required) {
		if (version >= required) {
			this.loadFlashLayer();
		}
    },
    loadFlashLayer: function() {
		this.flashRequest = new Ajax('/_commonext/html/_common/originWidget.html', {
			method: 'get',
			evalScripts: true,  
			update: this.flashLayer,
			onSuccess: function(data) {
				this.openFullWindow(this.flashLayer.getElements('a'));
			}.bind(this),
			onRequest: function(data) {
				this.flashLayer.innerHTML = '<span>loading Origin Stories widget...</span>';
			}.bind(this),
			onFailure: function(data) {
				this.flashLayer.innerHTML = '<span class="error">error retrieving widget data</span>';
			}.bind(this)
		});
		this.flashRequest.request();
    },
    openFullWindow: function(btns) {
    	btns.each(function(btn,index) {
			btn.addEvent("click", function(e) {
				e = new Event(e);
				window.open(btn.href,"Origins","height="+this.sHeight+",width="+this.sWidth+",top=0,left=0,resizable=yes,scrollbars=yes");
				e.stop();
			}.bind(this));
		}.bind(this));
    }
});



/* Clear the the default value of an input field */
function clearInputField( cur_element, default_value )
{
	if( cur_element.value == default_value )
	{
		cur_element.value = "";
		cur_element.style.color = "#FFF";
	}
}

/* re-insert the default value for an input field */
function redisplayDefaultInput( cur_element, default_value )
{
	if( cur_element.value == "" )
	{
		cur_element.value = default_value;
		cur_element.style.color = "#777";
	}
}

function loginFieldsOnLoad( )
{
	var username = document.getElementById( "username" );
	if( username == null || username.value != "" )
	{
		return;
	}

	redisplayDefaultInput( username, "Name" ); 

	var password = document.getElementById( "password" );
	var password_helper_span = document.getElementById( "password_helper_span" );

	var password_helper = document.createElement( "input" );
	password_helper.setAttribute( "type", "text" );
	password_helper.setAttribute( "id", "password_helper" );
	password_helper.setAttribute( "value", "Password" );
	password_helper.onfocus = showPasswordField;

	password.style.display = "none";
	password_helper.style.display = "inline";

	password_helper_span.appendChild(password_helper);
}

function showPasswordFieldNotEmpty( )
{
	var password = document.getElementById( "password" );
	if( password.value != "" )
	{
		showPasswordField( false );
	}
}

function showPasswordField( set_focus )
{
	if( typeof(set_focus)  == "undefined" )
	{
		var set_focus = true;
	}

	var password = document.getElementById( "password" );
	var password_helper = document.getElementById( "password_helper" );

	password_helper.style.display = "none";
	password.style.display = "inline";

	if( set_focus )
	{
		password.focus();
	}
}

// only want to do this for IE because it doesn't work in IE6 but if we do it for FF then it fails.
// this is also how it was working before I messed with it!


if(window.ie6)
{
	window.addEvent( "domready", sfHover );
}

window.addEvent( "domready", loginFieldsOnLoad );
