/* Function to addEventListener to onload
 * @param func - a function which should be executed once the page has loaded
 * http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 * it will work even if something has previously been assigned to window.onload
 * without using addLoadEvent itself. 
*/
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

/*
 * Function to clear a text field
 * @param thefield
 *     the field to clear
 */

function cleartext(thefield) {
	if (thefield.defaultValue == thefield.value) {
		thefield.value = "";
	}
}

/*
 * Function to change the URL via a select menu
 * @param elem
 *     the ID of the form to select box
 */

function changeurl(elem) {
	for (i = 0; i < elem.length; i++) {
		if(elem.options[i].selected == true) {
			window.location = elem.options[i].value;
		}
	}
}

/*
 * Function to open popup window
 * @param url
 *     The url to open
 * @param win_width
 *     The window width
 * @param win_height
 *     The window height
*/

function openWin(url, win_width, win_height) {
		
	win_left = (screen.width - win_width) / 2;
	win_top = (screen.height - win_height) / 2;
	
	window.open(url , '', 'width='+win_width+', height='+win_height+', left='+win_left+', top='+win_top+', resizable=1, scrollbars=1');
}

/*
 * Function to show/hide an element by ID
 * @param hide_elem
 *     returns the ID of the element to hide
 */
 
function show_hide_id(hide_elem) {
	
	elem_to_hide = document.getElementById(hide_elem);
	
	if (elem_to_hide != null) { // ensure the list exists...
		if (elem_to_hide.style.display == 'none') {
			elem_to_hide.style.display = 'block'; // show it
		}
		else {
			elem_to_hide.style.display = 'none'; // hide it
		}
	}
}

/*
 * Function for image rollovers
 * @param elem
 *     returns the anchor element
 */ 
function swap(root, elem) {
	if(root == '/')
		root = '';
	cur_src = elem.firstChild.src;
	cur_src = cur_src.slice(cur_src.lastIndexOf('/')+1);

	if (cur_src.indexOf('h_') == -1 && cur_src.indexOf('a_') == -1) {
		elem.firstChild.src = root + '/i/nav/' + 'h_' + cur_src;
		
	}
	else if(cur_src.indexOf('a_') == -1){
		elem.firstChild.src = root + '/i/nav/' + cur_src.slice(2);
	}
}

function resizeIFrame(frame_id) {
	parent.document.getElementById(frame_id).height = document.body.scrollHeight;
}

function favoritesAlert() {
	alert("Login to access your favorites.");
}
function mantleChooseIt() {
	alert('ChooseIt');
}

function mantleCreateIt() {
	alert('CreateIt');
	buildAlert();
}

function mantleBuyIt() {
	alert('BuyIt');
}

/* Added for Starwars Shop Menu navigation */
wireMenu = function(menu) {

    /* This is only necessary for IE6 since IE6 doesn't apply li:hover */
	if (document.all && document.getElementById(menu).currentStyle) {  
        var menuElement = document.getElementById(menu);
        
        /* Get all the list items within the menu */
        var listItems=menuElement.getElementsByTagName("LI");  
        for (i=0; i<listItems.length; i++) {
        
           /* If the LI has another menu level */
            if(listItems[i].lastChild.tagName=="UL"){
            
                /* assign the function to the LI */
             	listItems[i].onmouseover=function() {	
                
                   /* display the inner menu */
                   this.lastChild.style.display="block";
                }
                listItems[i].onmouseout=function() {                       
                   this.lastChild.style.display="none";
                }
            }
        }
    }
}
