// Global 
MYMO_REGISTER = "http://my.modojo.com/register/";

// Sponsorship
var sponsorOpen = 0;
function openSponsor( userId ) {
    var open = "";
    var close = "";
    
    if( userId > 0 ) {
        open = "topSponsorship_expand";
        close = "sponsor_topBar";
    }
    else {
        open = "topSponsorship_expand";
        close = "topBarExpanded";
    }
    if( sponsorOpen == 0 ) {
        document.getElementById( open ).style.display = "block";
        document.getElementById( close ).style.display = "none";
        sponsorOpen = 1;
    }
    else {
        document.getElementById( close ).style.display = "block";
        document.getElementById( open ).style.display = "none";
        sponsorOpen = 0;
    }
    
    return false;
}

// Top Bar Login
function openLogin( open ) {
    try {
        if( open == 1 ) {
            document.getElementById( "topBarStub" ).style.display = "none";
            document.getElementById( "topBarExpanded" ).style.display = "block";
        }
        else {
            document.getElementById( "topBarStub" ).style.display = "block";
            document.getElementById( "topBarExpanded" ).style.display = "none";
        }
    }
    catch( e ) {
        //
    }
    
    return false;
}

// Open Quick Form
function displayQuickForm( openForm ) {
    try {
        document.getElementById( openForm ).style.display = "block";
        document.getElementById( 'gamesPostLinks' ).style.display = "none";
    }
    catch( e ) {
        //
    }
    
    return false;
}

// Remove Game
function removeGame( field, type, skuId ) {
    makeRequest( "/?updateCollections=1&skuId=" + skuId + "&type=" + type + "&value=0" );
    try {
        document.getElementById( field ).style.display = "none";
    }
    catch( e ) {
        //
    }
}

// Remove Friend
function removeFriend( field, friendUserId ) {
    makeRequest( "/?updateUserFriend=1&friendUserId=" + friendUserId + "&value=0" );
    try {
        document.getElementById( field ).style.display = "none";
    }
    catch( e ) {
        //
    }
}

// Remove Tag
function removeTag( field, tagId, skuId ) {
    makeRequest( "/?removeTag=1&tagId=" + tagId + "&skuId=" + skuId );
    try {
        document.getElementById( field ).style.display = "none";
    }
    catch( e ) {
        //
    }
}


// Latest Updates
var updateSections = new Array( "featured", "latest", "mymo", "biz" );

function changeUpdate( newUpdate ) {
    try {
        // Close old tabs
        for( i = 0; i < updateSections.length; i++ ) {
            document.getElementById( "homeUpdates_" + updateSections[ i ] ).style.display = "none";
        }
        
        // Open new tab
        document.getElementById( "homeUpdates_" + newUpdate ).style.display = "block";
    }
    catch( e ) {
        //
    }
    
    return false;
}

// Top Games Lists
var topPlatforms = new Array( "all", "ds", "psp", "ng", "gba", "mobile", "ipod", "iphone" );

function changeTopGames( newPlatform ) {
    try {
        // Close old tabs
        for( i = 0; i < topPlatforms.length; i++ ) {
            document.getElementById( "homeTopGames_" + topPlatforms[ i ] ).style.display = "none";
        }
        
        // Open new tab
        document.getElementById( "homeTopGames_" + newPlatform ).style.display = "block";
    }
    catch( e ) {
        //
    }
    
    return false;
}

// Browse Select
function browseSelect( form ) {
    window.location = form.options[ form.selectedIndex ].value;
}

// Browse Select Platform
function browseSelectPlatform( form, field, action ) {
    if( field.options[ field.selectedIndex ].value == "" ) {
        form.action = action;
    }
    else {
        form.action = action + field.options[ field.selectedIndex ].value + "/";
    }
    
    form.submit();
}

// Search Submit
function searchSubmit( form, url ) {
    try {   
        form.action = url;
        form.submit();
    }
    catch( e ) {
        alert( e.message );
    }
}

// Add Content
previousValue = "";
hasGame = true;
var checkboxOff = new Image();
checkboxOff.src = "/img/icons/checkbox.gif";

var checkboxOn = new Image();
checkboxOn.src = "/img/icons/checkbox_on.gif";

function submitAddForm( ) {
    var theForm = eval( "document.addForm_" + previousValue );
    
    theForm.skuId.value = document.addForm_menu.skuId.value;
    theForm.submit();
    
    return false;
}

function updateDim( state ) {
    try {
        switch( state ) {
            case 0: // No game selected
                document.getElementById( "dimGame" ).className = "";
                document.getElementById( "dimText" ).className = "text";
                document.getElementById( "dimButton" ).className = "bigButton";
                document.getElementById( "dimMenuGame" ).className = ""; 
                document.getElementById( "dimMenuNoGame" ).className = "";
                document.getElementById( "dimSelectGame" ).className = "text";  
                document.getElementById( "dimSelectNoGame" ).className = "text";             
            break;
            case 1: // Selected a game 
                document.getElementById( "dimGame" ).className = "dim";
                document.getElementById( "dimText" ).className = "textdim";
                document.getElementById( "dimButton" ).className = "bigButtonDim";
                document.getElementById( "dimMenuGame" ).className = ""; 
                document.getElementById( "dimMenuNoGame" ).className = ""; 
                document.getElementById( "dimSelectGame" ).className = "text";  
                document.getElementById( "dimSelectNoGame" ).className = "text";    
            break;
            case 2: // Selected an option
                document.getElementById( "dimMenuGame" ).className = "dim"; 
                document.getElementById( "dimMenuNoGame" ).className = "dim"; 
                document.getElementById( "dimSelectGame" ).className = "textdim";  
                document.getElementById( "dimSelectNoGame" ).className = "textdim";  
            break;
        }
    }
    catch( e ) {
        //
    }
}

// proMO Version
function refreshProMO( value ) {
    urlAdd = "";
    if( value != "" ) {
        urlAdd = value + "/";
    }
    window.location = "/promo/add/" + urlAdd;
}

function openAddMenuProMO( value ) {
    if( value == "" ) {
        // Return to default state
        try {
            document.getElementById( "addForm_selectMenu" ).style.display = "none";
        }
        catch( e ) {
            //
        }
        updateDim( 0 );
    }
    else{
        // Open new game
        select = document.addForm_menu.skuId;
        for( i = 0; i < select.options.length; i++ ) {
            if( select.options[ i ].value == value ) {
                select.options[ i ].selected = true;
            }
        }
        try {
            document.getElementById( "addForm_selectMenu" ).style.display = "block";
            document.getElementById( "addForm_selectMenuNoGame" ).style.display = "none";
        }
        catch( e ) {
            //
        }
        updateDim( 1 );
    }
    
    // Make sure anything old is closed
    try {
        document.getElementById( "add_" + previousValue ).style.display = "none";
        document.getElementById( "add_submit" ).style.display = "none";
        document.addForm_menu.menuGame.options[ 0 ].selected = true;
    }
    catch( e ) {
        //
    }
}


// Called from pop-up
function openAddMenu( title ) {
    document.addForm_menu.skuId.value = title;
    document.getElementById( "addForm_selectMenu" ).style.display = "block";
    document.getElementById( "addForm_selectMenuNoGame" ).style.display = "none";
    
    if( previousValue == "blogNoGame" ) {
        document.getElementById( "add_" + previousValue ).style.display = "none";
        document.getElementById( "add_submit" ).style.display = "none";
    }
    
    if( hasGame == false ) {
        hasGame = true;
        updateHasGame();
    }
    
    try {
        opener.document.getElementById( "add_" + previousValue ).style.display = "none";
        opener.document.getElementById( "add_submit" ).style.display = "none";
    }
    catch( e ) {
        //
    }
    
    updateDim( 1 );
}

function updateHasGame() {
    checkboxImage = checkboxOff;
    if( ! hasGame ) {
        checkboxImage = checkboxOn;
    }
    
    document.images[ "imgCheckbox" ].src = checkboxImage.src;
}


function openAddNoGameMenu( ) { 
    hasGame = ! hasGame;
    
    updateHasGame();
           
    try {
        if( ! hasGame ) {
            if( previousValue != "" ) {
                document.getElementById( "add_" + previousValue ).style.display = "none";
                document.getElementById( "add_submit" ).style.display = "none";
            }
            document.getElementById( "addForm_selectMenu" ).style.display = "none";
            document.getElementById( "addForm_selectMenuNoGame" ).style.display = "block";
            document.addForm_menu.menuGame.options[ 0 ].selected = true;
            
            document.addForm_menu.skuId.value = "";
            
            updateDim( 1 );
        }
        else {
            if( previousValue != "" ) {
                document.getElementById( "add_" + previousValue ).style.display = "none";
                document.getElementById( "add_submit" ).style.display = "none";
            }
            document.getElementById( "addForm_selectMenu" ).style.display = "none";
            document.getElementById( "addForm_selectMenuNoGame" ).style.display = "none";
            document.addForm_menu.menuNoGame.options[ 0 ].selected = true;
            
            updateDim( 0 );
        }
    }
    catch( e ) {
        //
    }
        
    return false;
}

function initAddSelection( value ) {
    if( value == "blogNoGame" ) {
        select = document.addForm_menu.menuNoGame;
        for( i = 0; i < select.options.length; i++ ) {
            if( select.options[ i ].value == value ) {
                select.options[ i ].selected = true;
                openAddNoGameMenu();
                openAddSelection( select );
            }
        }
    }
    else {
        select = document.addForm_menu.menuGame;
        for( i = 0; i < select.options.length; i++ ) {
            if( select.options[ i ].value == value ) {
                select.options[ i ].selected = true;
                openAddSelection( select );
            }
        }
    }
}

function initError( field, message ) {
    try {
        document.getElementById( "addHeaderDefault" ).style.display = "none";
        document.getElementById( "addHeaderError" ).style.display = "block";
        
        document.getElementById( field + "_desc" ).className = "error";
        document.getElementById( field + "_desc" ).innerHTML = message;
        
        document.getElementById( field ).className = "texterror";
    }
    catch( e ) {
        //
    }
}

// Used for more than score!
function initScore( field, score ) {
    for( i = 0; i < field.options.length; i++ ) {
        if( field.options[ i ].value == score ) {
            field.options[ i ].selected = true;
        }
    }
}

function openAddSelection( select ) {
    value = select.options[ select.selectedIndex ].value;
    
    if( value[0] == "/" ) {
        window.location = value;
    }
    else if( value == "thread" ) {
        window.location = "http://forums.modojo.com";
    }
    else if( value != "" ) {
        try {
            // Close the old tab
            if( previousValue != "" ) {
                document.getElementById( "add_" + previousValue ).style.display = "none";    
            }
            
            // Open new tab
            document.getElementById( "add_" + value ).style.display = "block";
            document.getElementById( "add_submit" ).style.display = "block";
            
            // Scroll to it
            width = document.getElementById( "add_" + value ).clientWidth;
            height = document.getElementById( "add_" + value ).clientHeight;
            self.scrollTo( width, height );
            
            previousValue = value;
            
            updateDim( 2 );
        }
        catch( e ) {
            //   
        }
    }
    else {
        // Close the old tab
        if( previousValue != "" ) {
            document.getElementById( "add_" + previousValue ).style.display = "none";    
        }
        document.getElementById( "add_submit" ).style.display = "none";
        select.options[ 0 ].selected = true;
        updateDim( 1 );
    }
    
}

// Comments
function openComments() {
    try {
        document.getElementById( "commentStub" ).style.display = "none";
        document.getElementById( "commentFull" ).style.display = "block";
    }
    catch( e ) {
        //
    }
    
    return false;
}

// User Recommendations
userRecommendationValue = 0;

function initUserRecommendation( value ) {
    if( value == 1 ) {
        document.getElementById( "userRecommend" ).className = 'on';
    }
    userRecommendationValue = value;
}

function userRecommendation( userId, recommendUserId ) {
      if( userId == 0 ) {
        window.location = MYMO_REGISTER;
    }
    else {
        // Set new value. 
        value = 1;
        if( userRecommendationValue == 1 ) {
            value = 0;
        }
    
        makeRequest( "/?updateUserRecommendation=1&recommendUserId=" + recommendUserId + "&value=" + value );
        userRecommendationValue = value;

        total = document.getElementById( "totalRecommendations" );
    
        a = parseInt( total.innerHTML );
        
        try {
            if( value == 1 ) {
                document.getElementById( "userRecommend" ).className = 'on'; 
                total.innerHTML = a + 1;
                
            }
            else {
                document.getElementById( "userRecommend" ).className = '';
                total.innerHTML = a - 1;
            }
        }
        catch( e ) {
            //
        }
    }
    
    return false;
}

// User Friends
userFriendValue = 0;

function initUpdateFriend( value ) {
    if( value == 1 ) {
        document.getElementById( "userFriend" ).className = 'on';
    }
    userFriendValue = value;
}

function updateFriend( userId, friendUserId ) {
    if( userId == 0 ) {
        window.location = MYMO_REGISTER;
    }
    else {
        // Set new value. 
        value = 1;
        if( userFriendValue == 1 ) {
            value = 0;
        }
    
        makeRequest( "/?updateUserFriend=1&friendUserId=" + friendUserId + "&value=" + value );
        userFriendValue = value;
        
        try {
            if( value == 1 ) {
                document.getElementById( "userFriend" ).className = 'on'; 
                
            }
            else {
                document.getElementById( "userFriend" ).className = '';
            }
        }
        catch( e ) {
            //
        }
    }
    
    return false;
}



// Collections
var collections = new Array();
function initCollections( skuId, own, want, play ) {
    newIndex = collections.length;
    collections[ newIndex ] = new Array();
    collections[ newIndex ][ 1 ] = new Array();
    collections[ newIndex ][ 1 ]["value"] = 0;
    collections[ newIndex ][ 1 ]["name"] = "collectionOwn_" + skuId;
    collections[ newIndex ][ 2 ] = new Array();
    collections[ newIndex ][ 2 ]["value"] = 0;
    collections[ newIndex ][ 2 ]["name"] = "collectionWant_" + skuId;
    collections[ newIndex ][ 3 ] = new Array();
    collections[ newIndex ][ 3 ]["value"] = 0;
    collections[ newIndex ][ 3 ]["name"] = "collectionPlay_" + skuId;
    
    collections[ newIndex ][ 4 ] = new Array();
    collections[ newIndex ][ 4 ]["skuId"] = skuId;
    
    collections[ newIndex ][ 1 ]["value"] = own;
    collections[ newIndex ][ 2 ]["value"] = want;
    collections[ newIndex ][ 3 ]["value"] = play;
    
    if( own == 1 ) {
        document.getElementById( collections[ newIndex ][ 1 ]["name"] ).className = 'on';
    }
    if( want == 1 ) {
        document.getElementById( collections[ newIndex ][ 2 ]["name"] ).className = 'on';
    }
    if( play == 1 ) {
        document.getElementById( collections[ newIndex ][ 3 ]["name"] ).className = 'on';
    }
}

function updateCollections( userId, skuId, type ) {
    valid = 1;
    type = parseInt( type );
    
    if( userId == 0 ) {
        window.location = MYMO_REGISTER;
        valid = 0;
    }
    
    for( i = 0; i < collections.length; i++ ) {
        if( collections[ i ][ 4 ]["skuId"] == skuId ) {
            if( type <= 3 && type >= 1 && valid == 1 ) {
                // Set new value. 
                value = 1;
                if( collections[ i ][ type ]["value"] == 1 ) {
                    value = 0;
                }
            
                makeRequest( "/?updateCollections=1&skuId=" + skuId + "&type=" + type + "&value=" + value );
                collections[ i ][ type ]["value"] = value;
                
                if( value == 1 ) {
                    document.getElementById( collections[ i ][ type ]["name"] ).className = 'on'; 
                    
                }
                else {
                    document.getElementById( collections[ i ][ type ]["name"] ).className = '';
                }
            }
        }
    }
    
    return false;
}

// Article Recommendations
var recommended = new Array();
var recommendedIds = new Array();

function articleRecommendation( userId, articleId, articleType, value ) {
    valid = 1;
    
    if( userId == 0 ) {
        window.location = MYMO_REGISTER;
        valid = 0;
    }
    
    for( i = 0; i < recommendedIds.length; i++ ) {
        if( articleId == recommendedIds[i] ) {
            valid = 0;
        }
    }
    
    if( valid > 0 && articleId > 0 && ( value == 0 || value == 1 ) ) {
        lockArticleRecommendation( articleId, value, 1 );
        makeRequest( "/?articleId=" + articleId + "&articleType=" + articleType + "&value=" + value );
    }
    
    return false;
}

function initArticleRecommendations( ids ) {
    if( ids != "" ) {
        recommended = ids.split( "," );
        for( i = 0; i < recommended.length; i++ ) {
            info = recommended[i].split( "|" );
            lockArticleRecommendation( parseInt( info[0] ), parseInt( info[1] ), 0 );
        }
    }
}

function lockArticleRecommendation( articleId, value, update ) {
    try {
        if( value == 1 ) {
            document.getElementById( "thumbsdown_" + articleId ).className = 'buttonBorderOff';  
        }
        else {
            document.getElementById( "thumbsup_" + articleId ).className = 'buttonBorderOff';
        }
        
        if( update == 1 ) {
            helpfulness = document.getElementById( "helpfulness_" + articleId );
            
            helpful = document.getElementById( "helpful_" + articleId );
            total = document.getElementById( "total_" + articleId );
    
            a = parseInt( helpful.innerHTML );
            b = parseInt( total.innerHTML );
            
            if( b == 0 ) {
                helpfulness.style.display = 'block';
            }
            
            if( value == 1 ) {
                helpful.innerHTML = a + 1;
            }
            total.innerHTML = b + 1;
        }
        
        recommendedIds[ recommendedIds.length + 1 ] = articleId;
    }
    catch( e ) {
        // error
    } 
}


// Load Page
var http_request = false; 

function makeRequest(url) { 

   if (window.XMLHttpRequest) { // Mozilla, Safari,... 
       http_request = new XMLHttpRequest(); 
   } else if (window.ActiveXObject) { // IE 
       http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
   } 
   
   //http_request.onreadystatechange = alertContents; 
   http_request.open('GET', url, true); 
   http_request.send(null); 

} 

// Open Popup for Search
function searchPopup( URL ) {
    day = new Date();
    id = day.getTime();
    eval( "page" + id + " = window.open( URL, '" + id + "', 'toolbar=0, scrollbars=1, location=0, statusbar=0, menubar=0, resizable=1, width=600, height=300');");
}