var numModules, modules, xmlDocReq;


function ParseXML () {
    
    xmlDocReq = null;
    
    if ( window.XMLHttpRequest ) { // code for Safari, Firefox, Opera, IE7, etc.
        
        xmlDocReq = new XMLHttpRequest();
        
    } else if ( window.ActiveXObject ) { // code for IE6, IE5
        
        xmlDocReq = new ActiveXObject( "Microsoft.XMLHTTP" );
        
    }
    
    if ( xmlDocReq != null ) {
        
        xmlDocReq.onreadystatechange = updateState;
        
        xmlDocReq.open( "GET", "resources/content/content.xml", true );
        
        xmlDocReq.setRequestHeader( "Content-Type", "text/xml" );
		
        xmlDocReq.send( null );
        
    } else {
        
        alert( "Your browser does not support XMLHTTP.  Please consider using " +
              "another, current browser such as IE 7, Firefox 2.x, Safari 2.x, " +
              "or Opera 9.x." );
        
    }
    
    
}


function updateState () {
    
    if ( xmlDocReq.readyState == 4 ) {
    
        if ( xmlDocReq.status == 200 ) {
    
            modules = xmlDocReq.responseXML.getElementsByTagName( "modules" )[ 0 ].getElementsByTagName( "module" );
            
            numModules = modules.length;
            
            enableDropDown( document.getElementById( 'contentDropDown' ) );
            populateContentDropDownAll();
            
        } else {
            
            alert("There was a problem retrieving the XML data:\n" +
                xmlDocReq.statusText
            );
            
        }
    
    }
    
}


function updateContentBrowse () {
    
    var selectedModule;
    
    selectedModule = document.getElementById( 'contentDropDown' ).value;
    
    updateContentText( selectedModule );
    
    clearFlashArea( );
	
	ViewContent();
    
}


function populateContentDropDownAll () {
    
    for ( var i = 0; i < numModules; i++ ) {
        
        var item = modules[ i ].getElementsByTagName( "url" )[ 0 ].childNodes[ 0 ].nodeValue;
        var itemTitle = modules[ i ].getElementsByTagName( "title" )[ 0 ].childNodes[ 0 ].nodeValue;
        
        document.getElementById( 'contentDropDown' ).options[ i ] = new Option( itemTitle, item );
        
    }
    
    updateContentBrowse();
    
//    enableButton( document.getElementById( 'viewButton' ) );
    
}


function updateContentText ( aContentModule ) {
    
    var contentArea;
    var moduleURL, moduleCategories, moduleDescription;
    var moduleHTML, categoryHTML, descriptionHTML;
    
    contentArea = document.getElementById( 'contentArea' );
    
    moduleCategories = getModuleCategoriesByURL( aContentModule );
    
    moduleDescription = getModuleDescriptionByURL( aContentModule );
    
    moduleHTML = '<div><strong>Content module:</strong><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + aContentModule + '</div>';
    
    descriptionHTML = '<div><strong>Content descritpion:</strong><br />' +
        '<div style="margin-left:15px;width:90%;">' + moduleDescription + '</div></div>';
    
    
    if ( moduleCategories.length == 1 ) {
        
        categoryHTML = '<div><strong>Category:</strong><br /><div style="margin-left:15px;width:90%;">' + moduleCategories[ 0 ] + '</div></div>';
        
    } else { 
        
        categoryHTML = '<div><strong>Categories:</strong><br /><div style="margin-left:15px;width:90%;">';
        
        for ( var i = 0; i < moduleCategories.length; i++ ) {
            
            categoryHTML = categoryHTML + moduleCategories[ i ] + '&nbsp;&nbsp;&nbsp;';
            
        }
        
        categoryHTML = categoryHTML + '</div></div>';
        
    }
    
    contentArea.innerHTML = /*moduleHTML +*/ categoryHTML + descriptionHTML;
    
    
}



function getModuleCategoriesByURL ( aURL ) {
    
    var arrCategories, module, moduleCategories;
    
    arrCategories = new Array();
    
    for ( var i = 0; i < numModules; i++ ) {
        
        module = modules[ i ]
        
        if ( module.getElementsByTagName( "url" )[ 0 ].childNodes[ 0 ].nodeValue == aURL ) {
            
            moduleCategories = module.getElementsByTagName( "category" );
            
            for ( var j = 0; j < moduleCategories.length; j++ ) {
            
                arrCategories.push( moduleCategories[ j ].childNodes[ 0 ].nodeValue );
            
            }
            
            break;
            
        }
        
    }
    
    arrCategories = arrCategories.sort();
    
    
    return arrCategories;
    
}


function getModuleDescriptionByURL ( aURL ) {
    
    var description, module;
    
    for ( var i = 0; i < numModules; i++ ) {
        
        module = modules[ i ]
        
        if ( module.getElementsByTagName( "url" )[ 0 ].childNodes[ 0 ].nodeValue == aURL ) {
            
            if ( module.getElementsByTagName( "description" )[ 0 ] ) {
            
                description = module.getElementsByTagName( "description" )[ 0 ].childNodes[ 0 ].nodeValue;
                
            } else { description = 'No description provided.'; }
            
            break;
            
        }
        
    }
    
    
    return description;
    
}


function disableDropDown ( aDropDown ) {
    
    aDropDown.setAttribute( 'disabled', 'disabled' );
    
}

function enableDropDown ( aDropDown ) {
    
    aDropDown.removeAttribute( 'disabled' );
    
}


function clearDropDown ( aDropDown ) {
    
    while ( aDropDown.length > 0 ) {
        
        aDropDown.remove( aDropDown.length - 1 );
        
    }
    
}


function enableButton ( aButton ) {
    
    aButton.removeAttribute( 'disabled' );
    
}


function disableButton ( aButton ) {
    
    aButton.setAttribute( 'disabled', 'disabled' );
    
}


function clearContentArea () {
    
    document.getElementById( 'contentArea' ).innerHTML = '';
    
    
}


function clearFlashArea () {
    
    swfobject.removeSWF( "flashAreaInner" );
    //document.getElementById( 'flashArea' ).innerHTML = 'Awaiting new choice....';
    
    
}

/*
function ViewContent () {
    
    var licenseOutput = document.getElementById( 'flashArea' );
    licenseOutput.innerHTML = 
        '<object width="640" height="480">' +
            '<param name="movie" value="60seconds.swf">' +
            '<param name=FlashVars value="pathToResource=resources/">' +
                '<embed src="60seconds.swf" FlashVars="contentToView=' + document.getElementById( 'contentDropDown' ).value +
                    '" width="640" height="480">' +
            '</embed>' +
        '</object>';
    
}
*/


function ViewContent () {
    
    var flashVars;
    
    flashVars = { contentToView: document.getElementById( 'contentDropDown' ).value  };
    
    //swfobject.embedSWF( "60seconds.swf", "flashArea", "640", "480", "8.0.0", "", flashVars );
    
    var currentSWF = document.getElementById( "flashAreaInner" );
        if ( ! currentSWF ) {
            
            var newSWF = document.createElement( "div" );
            newSWF.setAttribute( "id", "flashAreaInner" );
            document.getElementById( "flashArea" ).appendChild( newSWF );
            
        }
    
    if ( swfobject.hasFlashPlayerVersion( "8.0.0" ) ) {
        
            var fnSWF = function() {
                
                var att = { data:"60Seconds.swf", width:"640", height:"480" };
                var par = { flashvars:"contentToView=" + document.getElementById( 'contentDropDown' ).value, wmode: "transparent" };
                var id = "flashAreaInner";
                var myObject = swfobject.createSWF( att, par, id );
                
            };
            
        swfobject.addDomLoadEvent( fnSWF );
        
    } else {
        
        document.getElementById( 'flashArea' ).innerHTML =
            'This content requires the Adobe Flash Player.<br />'
            + '<a href=http://www.macromedia.com/go/getflash/>Get Flash</a>';
        
    }
    
}

