﻿// Copyright 2008-2009 City City Inc. All Rights Reserved.

// Open and Close Cloud Tag Divisions
// Cloud Tags for: POPULAR, AVATARS, VIDEOS, LINKS, etc.
// User may Open (Expand) or Close (Collapse) individual divisions

// Function to Load Initial Toggle Status for Cloud Tag Divisions on MAIN/DEFAULT Page
// Reads User Cookies, and sets according to last visit/setting
function LoadInitialToggleMoreStatus() {
    // Tags open and Close
    
    // For each Cloud Tags TAB, must set INITIAL Value based on Cookies
    
    // Varb for Cookie Results
    var CookieValue = null;
    
    // Varbs for TABS to be Toggled
    var tab1 = null;
    var tab2 = null;
    
    // Varb for Dynamic Tab Names
    var DynamicTab = null;
    
    // ---------- POPULAR Tab ----------
    CookieValue = ReadCloudTagCookie('CloudTagsPopularTab');
    tab1 = document.getElementById('CloudTagsPopularTabOpen');
    tab2 = document.getElementById('CloudTagsPopularTabClosed');
    
    // Check Open/Closed Status
    if (CookieValue != null && CookieValue == 'open') {
        // Cookie exists, and OPEN
        tab1.style.display = 'block';
        tab2.style.display = 'none';
    }
    else if (CookieValue != null && CookieValue == 'closed') {
        // Cookie exists, and CLOSED
        tab1.style.display = 'none';
        tab2.style.display = 'block';
    }

    // ---------- AVATARS Tab(s) ----------
    // ***** IMPORTANT LOGIC! *****
    // There may be an unlimited number
    // of Avatar MENUS, and EACH is generated
    // as a SEPARATE TAB of Avatars. These
    // Menus are used to Group Avatars into
    // Categories. There is always at least
    // ONE Default Avatar Menu, but may be
    // any number. Therefore, loop through
    // until NOT FOUND! This will ensure
    // all possible Avatar Menu(s) are handled.
    
    // Loop through possible 10 Avatar Menus
    // Could be more, but wanted to limit
    // Client Side processing to max 10
    // as usually just one or two!
    for(var i=1;i < 11;i++) {
        // Assemble Dynamic Tab Name
        // Syntax: AvMenuX where X = Loop Counter
        // (i.e. Avatars Menu 1 through 10)
        DynamicTab = 'AvMenu' + i;
                
        // Get Cookie Value (If exists)
        // Will NOT throw error if does not exist
        CookieValue = ReadCloudTagCookie(DynamicTab);
    
        // Create Reference to OPEN and CLOSED ver of Tab
        // Will NOT throw error if does not exist
        tab1 = document.getElementById(DynamicTab + 'Open');
        tab2 = document.getElementById(DynamicTab + 'Closed');

        // See if TABS Exist
        // Avatar Menus are DYNAMIC and may be unlimted number
        // If we have EXCEEDED the Actual Menus,
        // then will EXIT to save resources
        if (tab1 != null && tab2 != null) {
            // Tabs Exist!
            // Safe to Open/Close based on User Cookie Values
        
            // See if Cookie Value exists
            if (CookieValue != null) {
                // Cookie Value exists
            
                // See if value is either OPEN or CLOSE
                if (CookieValue == 'open') {
                    // Cookie exists, and OPEN
                    tab1.style.display = 'block';
                    tab2.style.display = 'none';
                }
                else if (CookieValue == 'closed') {
                    // Cookie exists, and CLOSED
                    tab1.style.display = 'none';
                    tab2.style.display = 'block';
                }
            }
        }
        else {
            // Tabs do NOT exist!
            // We have exceeded actual number
            // Exit Loop early to save resources!
            // Simply increment Loop Counter to exceed MAX
            i=11;
        }
    }
    
    // ---------- NOTES Tab ----------
    CookieValue = ReadCloudTagCookie('PortalNotesTab');
    tab1 = document.getElementById('PortalNotesTabOpen');
    tab2 = document.getElementById('PortalNotesTabClosed');
    
    // Check Open/Closed Status
    if (CookieValue != null && CookieValue == 'open') {
        // Cookie exists, and OPEN
        tab1.style.display = 'block';
        tab2.style.display = 'none';
    }
    else if (CookieValue != null && CookieValue == 'closed') {
        // Cookie exists, and CLOSED
        tab1.style.display = 'none';
        tab2.style.display = 'block';
    }
}

// Function to Load Initial Toggle Status for Cloud Tag Divisions on Left Menu
// Reads User Cookies, and sets according to last visit/setting
function LoadInitialToggleLeftMenuStatus() {
    // Tags open and Close
    
    // For each Cloud Tags TAB, must set INITIAL Value based on Cookies
    
    // Varb for Cookie Results
    var CookieValue = null;
    
    // Varbs for TABS to be Toggled
    var tab1 = null;
    var tab2 = null;
    
    // Varb for Dynamic Tab Names
    var DynamicTab = null;
    
    // ---------- POPULAR TAGS Tab ----------
    CookieValue = ReadCloudTagCookie('LeftMenuTagsTab');
    tab1 = document.getElementById('LeftMenuTagsTabOpen');
    tab2 = document.getElementById('LeftMenuTagsTabClosed');
    
    // See if TABS Exist
    // Left Menu Sections vary depending
    // upon Login Status, so ensure EXIST!
    if (tab1 != null && tab2 != null) {
        // Tabs Exist
        
        // Check Open/Closed Status
        if (CookieValue != null && CookieValue == 'open') {
            // Cookie exists, and OPEN
            tab1.style.display = 'block';
            tab2.style.display = 'none';
        }
        else if (CookieValue != null && CookieValue == 'closed') {
            // Cookie exists, and CLOSED
            tab1.style.display = 'none';
            tab2.style.display = 'block';
        }
    }
    
    // ---------- MEMBERS Tab ----------
    CookieValue = ReadCloudTagCookie('LeftMenuMembersTab');
    tab1 = document.getElementById('LeftMenuMembersTabOpen');
    tab2 = document.getElementById('LeftMenuMembersTabClosed');
    
    // See if TABS Exist
    // Left Menu Sections vary depending
    // upon Login Status, so ensure EXIST!
    if (tab1 != null && tab2 != null) {
        // Tabs Exist
        
        // Check Open/Closed Status
        if (CookieValue != null && CookieValue == 'open') {
            // Cookie exists, and OPEN
            tab1.style.display = 'block';
            tab2.style.display = 'none';
        }
        else if (CookieValue != null && CookieValue == 'closed') {
            // Cookie exists, and CLOSED
            tab1.style.display = 'none';
            tab2.style.display = 'block';
        }
    }
}








// Function to Load Initial Toggle Status for EXPLORE Cloud Tag Divisions
// Reads User Cookies, and sets according to last visit/setting
// Must be separate function to above, as will throw error if attempt
// to access Page Element that does NOT exist, and EXPLORE TABS
// ONLY Exist on EXPLORE Page!
function LoadInitialToggleExploreStatus() {
    // Tags open and Close
    
    // For EXPLORE Cloud Tag TAB, must set INITIAL Value based on Cookies
    
    // Varb for Cookie Results
    var CookieValue = null;
    
    // Varbs for TABS to be Toggled
    var tab1 = null;
    var tab2 = null;
    
    // EXPLORE Tab
    CookieValue = ReadCloudTagCookie('CloudTagsExplore');
    tab1 = document.getElementById('CloudTagsExploreOpen');
    tab2 = document.getElementById('CloudTagsExploreClosed');
    
    // Check Open/Closed Status
    if (CookieValue != null && CookieValue == 'open') {
        // Cookie exists, and OPEN
        tab1.style.display = 'block';
        tab2.style.display = 'none';
    }
    else if (CookieValue != null && CookieValue == 'closed') {
        // Cookie exists, and CLOSED
        tab1.style.display = 'none';
        tab2.style.display = 'block';
    }
}

// Function to Load Initial Toggle Status for AVATARS Divisions
// Reads User Cookies, and sets according to last visit/setting
// Must be separate function to above, as will throw error if attempt
// to access Page Element that does NOT exist, and AVATARS TABS
// ONLY Exist on AVATARS (INFO) Page!
function LoadInitialToggleAvatarsStatus() {
    // Tags open and Close
    
    // For AVATARS TAB, must set INITIAL Value based on Cookies
    
    // Varb for Cookie Results
    var CookieValue = null;
    
    // Varbs for TABS to be Toggled
    var tab1 = null;
    var tab2 = null;
    
    // INFO AVATARS Tab
    CookieValue = ReadCloudTagCookie('InfoAvatars');
    tab1 = document.getElementById('InfoAvatarsOpen');
    tab2 = document.getElementById('InfoAvatarsClosed');
    
    // Check Open/Closed Status
    if (CookieValue != null && CookieValue == 'open') {
        // Cookie exists, and OPEN
        tab1.style.display = 'block';
        tab2.style.display = 'none';
    }
    else if (CookieValue != null && CookieValue == 'closed') {
        // Cookie exists, and CLOSED
        tab1.style.display = 'none';
        tab2.style.display = 'block';
    }
}

// Fuction to TOGGLE Current Setting
function ToggleTab(TabID,OpenClose) {
    // Toggle (OPEN/CLOSE) Tab
    // Input Varbs identify TAB and Toggle Status
    
    // Tab ID Name is passed WITHOUT the 'Open' or 'Closed' Suffix
    // The 'Open' or 'Closed' Suffix is used to manage TWO TABS
    // for Each Tab Name.
    // (i.e. 'CloudTagsPopularTabOpen' and 'CloudTagsPopularTabClosed')
    
    // Create Reference to PAIR of Tabs based on TAB ID Passed
    // Will NOT Throw error if Tab does NOT Exist
    var tab1 = document.getElementById(TabID + 'Open');
    var tab2 = document.getElementById(TabID + 'Closed');
    
    // Ensure Tab EXISTS before attempt to Toggle or will Throw Error!
    if (tab1 != null && tab2 != null) {
        // Pair of Tabs EXIST!
        // Safe to Manage (i.e. OPEN/CLOSE)
        
        // Set for OPEN or CLOSE
        if (OpenClose == 'open') {
            // OPEN Status
            tab1.style.display = 'block';
            tab2.style.display = 'none';
        }
        else if (OpenClose == 'closed') {
            // CLOSED Status
            tab1.style.display = 'none';
            tab2.style.display = 'block';
        }
    }

    // Set COOKIE Status to preserve client side setting
    CreateCloudTagCookie(TabID, OpenClose);
} 

// Function to READ Current Setting via client side COOKIE
function ReadCloudTagCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

// Function to SET (Write) Client Side COOKIE to store current setting
function CreateCloudTagCookie(name,value) {
    // Cookie Syntax:
    //  1) name/value pair
    //      name=value;
    //  2) Expiry Date
    //      expires=GMT Date String
    //  3) Root Path (Just in case written from page within a SUB FOLDER, need to force to ROOT or will be different)
    //      path=/

    // Setup EXPIRES Date using 7 days from now 
    var ExpDate = new Date();
    // Set date to NOW plus 7 days (24 hours per day, 60 minutes per hour, 60 seconds per minute, tracked in 1000's of a second)
    ExpDate.setTime(ExpDate.getTime()+(7*24*60*60*1000));
    
    // String Varb for Expires parameter
    var expires = "expires="+ExpDate.toGMTString();

    // Write Cookie
    document.cookie = name+"="+value+"; "+expires+"; path=/";
}

