
//______Config Settings__________ 

var polling = 2000; //polling time
var max_idle = 300; //max number of polling before idle
var poll_stats = 15;



//___________________________________________________DO NOT EDIT BELOW THIS LINE___________________________________________________________
var $j = jQuery.noConflict();
var state_chatrooms;
var state_chathome;
var state_chatting;
var chat_room_name;
var new_message;
var search_key_words;
var logged_in;
var logged_stat;
var version;
var chat_room_id;
var lastmessage = 1;
var idle_counter = 0;
var this_month;
var this_day;
var this_hour;
var this_minute;
var idle_out;
var online = '-';
var first_load;
var update_my_status;
var first_load_chat_window;
var last_load_chat_window;
var ping_stats_counter = 0;
var check_online;
var timestamp = 0;
var ajax_newmessage = null;
var ajax_polling = null;
var ajax_newmessage_feed = null;
var keep_polling;
var first_load = 0;
var online_check_counter = 0;

$j(document).ready(function(){


    //______AJAX - Login Check__________ 
    Login_Check();
    //______Load some langs______________
    $j('#Links-Chat-Room').html(lang_select_chatroom);
    $j('#MT-Footer-Menu-Search-Input').val(lang_search_videos);
    //_____Staring Window Sizing and div widths________________	
    window_starting_size = $j(window).width();
    //check if widnows in too small - load Resizing function
    if (window_starting_size < 880) {
        Resizing_Window();
    }
    else {
        //if its ok lets load it as 100% elastic		
        $j('#MT-Footer-Container').removeClass();
        $j('#MT-Footer-Container').addClass('Window-Default');
        $j('#MT-Footer-Container2').removeClass();
        $j('#MT-Footer-Container2').addClass('Window-Default');
    }
    //_____Events - Dealing with window resizing________________
    $j(window).resize(function(){
        Resizing_Window();
    });
    //_____Events - Click Chatroom - Task bar________________
    $j('#Links-Chat-Room').click(function(){
        Clik_ChatRoom_Taskbar();
    });
    
    //_____Events - Click Home - Task bar________________
    $j('#Links-Menu-Home').click(function(){
        Clik_ChatHome_Taskbar();
    });
    //_____Events - Hover Chatroom - Task bar________________
    $j('#Links-Chat-Room').hover(function(){
        Hover_ChatRoom_Taskbar();
    }, function(){
        UnHover_ChatRoom_Taskbar();
    });
    //_____Events - Click Chatroom - links (LIVEQUERY)________________
    clicked_room = function(){
        //let get the chatroom id
        chat_room_id = $j(this).attr('rel');
        chat_room_name = $j(this).html();
        //error control - no chatroom name
        if (chat_room_name == '') {
            chat_room_name = 'Undefined';
        }
        //run function to start process
        Clik_ChatRoom_Links();
    };
    // monitor event and use livequery
    $j('#MT-Chat-Room-List a').livequery('click', clicked_room);
    //_____Events - Click Chatroom Header - Minimize________________
    $j('#Links-Chat-Room-Minimize').click(function(){
        Clik_ChatRoom_Minimize();
    });
    
    //_____Events - Click Home Header - Minimize________________
    $j('#Links-Chat-Home-Minimize').click(function(){
        Clik_ChatHome_Minimize();
    });
    
    //_____Events - Click Chatting Header - Minimize________________
    $j('#Links-Chat-Chatting-Minimize').click(function(){
        Clik_ChatChatting_Minimize();
    });
    //_____Events - Click Chatting - Task bar________________
    $j('#Links-Chat-Chatting').click(function(){
        Clik_ChatChatting_Taskbar();
    });
    //_____Events - Click Chatting-Close - Task bar________________
    $j('#Links-Chat-Chatting-Close').click(function(){
        Clik_ChatChatting_Close_Taskbar();
    });
    //_____Events - Hover Chatting - Task bar________________
    $j('#Links-Chat-Chatting').hover(function(){
        Hover_ChatChatting_Taskbar();
    }, function(){
        UnHover_ChatChatting_Taskbar();
    });
    //_____Events - Hover Chatting-Close - Task bar________________
    $j('#Links-Chat-Chatting-Close').hover(function(){
        Hover_ChatChatting_Taskbar();
    }, function(){
        UnHover_ChatChatting_Taskbar();
    });
    //_____Events - Hover Menu-Home - Task bar________________
    $j('#Links-Menu-Home').hover(function(){
        Hover_Menu_Home();
    }, function(){
        UnHover_Menu_Home();
    });
    //_____Events - Hover Menu-Close - Task bar________________
    $j('#Links-Menu-Close').hover(function(){
        Hover_Menu_Close();
    }, function(){
        UnHover_Menu_Close();
    });
    //_____Events - Hover Menu-Email Pic - Task bar________________
    $j('#Links-Menu-Email').hover(function(){
        Hover_Menu_Email();
    }, function(){
        UnHover_Menu_Email();
    });
    //_____Events - Hover Menu-Email-Count - Task bar________________
    $j('#Links-Menu-Email-Count').hover(function(){
        Hover_Menu_Email();
    }, function(){
        UnHover_Menu_Email();
    });
    //_____Events - Chat Text submitted________________   
    $j('#MT-Input-Typing').keyup(function(e){
        //check if key hit is "ENTER" (key 13)
        if (e.keyCode == 13) {
            // store the message in a var	
            new_message = $j('#MT-Input-Typing').attr('value');
            //reset the input
            $j('#MT-Input-Typing').attr('value', '');
            //Send to Ajax function if not empty	
            if (new_message != '') {
                Ajax_Post_New();
            }
        }
    });
    //_____Events - Reset Search Form________________   
    $j('#MT-Footer-Menu-Search-Input').focus(function(){
        //start by resetting the input
        $j('#MT-Footer-Menu-Search-Input').attr('value', '');
    })
    //_____Events - Submit Search Form________________   
    $j('#MT-Footer-Menu-Search-Submit').click(function(){
        //get the search keywords
        search_key_words = $j('#MT-Footer-Menu-Search-Input').attr('value');
        //Set the form to POST instead of GET (for V2)
        if (version == 2) {
            $j('#MT-Footer-Menu-Search-Form').attr('method', 'POST');
        }
        Search_Videos();
    })
    //_____Events - Click Logout________________
    $j('#Links-Menu-Close').click(function(){
        Ajax_Session_Logout();
    });
    //End of Doc Ready   
});





//__________________________________________________________________________________FUNCTION____________________________________________________________________


//______AJAX - Login Check__________

function Login_Check(){

    $j.ajax({
        type: "POST",
        url: mtb_base_url + "ajax_env.php",
        data: "query=logincheck",
        success: function(xml){
        
            //____get the feed___
            logged_stat = $j("logged", xml).text();
            menulinks = $j("menulinks", xml).text();
            //menulinks = Encoder.htmlDecode(menulinks);
            
            //add links to menu
            //$j('#MT-Home-Links').html('');
            $j('#MT-Home-Links').html(menulinks);
            
            //run rest of login routine
            Login_Results(logged_stat);
        }
    });
}


function Login_Results(){

    if (logged_stat == 2 || logged_stat == 3) {
        logged_in = 1; //Sanity status for logged in
        //Make things visible
        $j('#MT-Footer-Container').css('visibility', 'visible');
        $j('#MT-Footer-Container').css('display', 'block');
        $j('#MT-Spacer').css('display', 'block');
        //Check Emails
        Ajax_Check_email();
        //Get Categories
        Ajax_Rooms_list();
    }
    //Version Status
    if (logged_stat == 2) {
        version = 2;
    }
    if (logged_stat == 3) {
        version = 3;
    }
    
    if (version == 3 && chat_ajax == 1) {
        $j('#MTB_Linked').css('display', 'none');
    }
}




//______________Ajax - Check Email____________________
function Ajax_Check_email(){

    $j.ajax({
        type: "POST",
        url: mtb_base_url + "ajax_env.php",
        data: "query=email",
        success: function(msg){
            $j('#Links-Menu-Email-Count').html(msg);
        }
    });
}




//______________Ajax - get Rooms List____________________
function Ajax_Rooms_list(){

    $j.ajax({
        type: "GET",
        url: mtb_base_url + "ajax_rooms.php",
        data: "",
        success: function(msg){
            $j('#MT-Chat-Room-List ul').html(msg);
        }
    });
}



//______________Ajax - Post New Text____________________
function Ajax_Post_New(){

    ajax_newmessage = $j.ajax({
        type: "POST",
        url: mtb_base_url + "ajax_chat.php",
        processData: false,
        data: 'dev1=' + new_message + '&chat_room_id=' + chat_room_id,
        complete: function(){
            //get instant poll
            Ajax_Feed_Instant();
        }
    });
}



//______________Ajax - Get Feed____________________
function Ajax_Feed_Instant(){

    //____clear time out_____
    clearTimeout(keep_polling);
    ajax_polling = $j.ajax({
        type: "POST",
        cache: false,
        url: mtb_base_url + "ajax_feed.php",
        data: 'chat_room_id=' + chat_room_id + '&timestamp=' + timestamp + '&first_load=' + first_load + '&online_check=' + online_check,
        success: function(xml){
            //____get the feed___
            timestamp_new = $j("timestamp", xml).text();
            message = $j("message", xml).text();
            
            if (online_check == 1) {
                online = $j("online", xml).text();
            }
            
            //____Update chat box (only new and if first load)________
            if (timestamp_new > timestamp || first_load == 1) {
                $j("#MT-Output-Window").html('');
                $j("#MT-Output-Window").html(message);
                timestamp = timestamp_new;
            }
            //scroll down the window
            $j('#MT-Output-Window').animate({
                scrollTop: $j('#MT-Output-Window')[0].scrollHeight
            });
        },
        complete: function(){
            first_load = 0; //reset
            online_check = 0; //reset
            Chatting_Stats(); //update who isonline
            //__count before checking online gain___
            online_check_counter = online_check_counter + 1;
            if (online_check_counter == 6) {
                online_check = 1;
                online_check_counter = 0;
            }
            //__poll the server again______
            keep_polling = setTimeout(Ajax_Feed_Instant, 5000);
        }
    });
}


//______________Session Logout____________________
function Ajax_Session_Logout(){

    $j.ajax({
        type: "POST",
        url: mtb_base_url + "ajax_env.php",
        data: "query=logout",
        success: function(msg){
            $j('#MT-Footer-Container').css('display', 'none');
            $j('#MT-Box-Chatting').css('display', 'none');
            $j('#MT-Box-Rooms').css('display', 'none');
            $j('#MT-Box-Home').css('display', 'none');
            $j('#MT-Spacer').css('display', 'none');
        }
    });
    //Shown down other things
    System_Total_Shut_down();
}


//______________Action on clicked chatrooms - Button____________________
function Clik_ChatRoom_Taskbar(){

    $j('#MT-Box-Rooms').removeClass();
    $j('#MT-Footer-Who').removeClass();
    //check if its live or idle
    if (state_chatrooms == 1) {
        $j('#MT-Box-Rooms').addClass('MT-Footer-ChatRooms-Idle').slow;
        $j('#MT-Box-Rooms').css('display', 'none');
        $j('#MT-Footer-Who').addClass('MT-Footer-Who-Highlight-Live');
        state_chatrooms = 0;
    }
    else {
        $j('#MT-Box-Rooms').addClass('MT-Footer-ChatRooms-Live').slow;
        $j('#MT-Box-Rooms').css('display', 'block');
        $j('#MT-Footer-Who').addClass('MT-Footer-Who-Highlight-Live');
        state_chatrooms = 1;
    }
}


//______________Action on clicked Home - Button____________________
function Clik_ChatHome_Taskbar(){

    $j('#MT-Box-Home').removeClass();
    //check if its live or idle
    if (state_chathome == 1) {
        $j('#MT-Box-Home').addClass('MT-Footer-ChatHome-Idle').slow;
        $j('#MT-Box-Home').css('display', 'none');
        state_chathome = 0;
    }
    else {
        $j('#MT-Box-Home').addClass('MT-Footer-ChatHome-Live').slow;
        $j('#MT-Box-Home').css('display', 'block');
        state_chathome = 1;
    }
}




//______________Action on hover and Unhover chatrooms - Button____________________
function Hover_ChatRoom_Taskbar(){

    if (state_chatrooms != 1) {
        $j('#MT-Footer-Who').removeClass();
    }
    $j('#MT-Footer-Who').addClass('MT-Footer-Who-Highlight-Live');
}

function UnHover_ChatRoom_Taskbar(){

    if (state_chatrooms != 1) {
        $j('#MT-Footer-Who').removeClass();
    }
    $j('#MT-Footer-Who').addClass('MT-Footer-Who-Highlight-Idle');
}



//______________Action on clicked chatrooms Minimize____________________
function Clik_ChatRoom_Minimize(){

    //check if its live or idle
    if (state_chatrooms == 1) {
        $j('#MT-Box-Rooms').removeClass();
        $j('#MT-Footer-Who').removeClass();
        $j('#MT-Box-Rooms').addClass('MT-Footer-ChatRooms-Idle').slow;
        $j('#MT-Box-Rooms').css('display', 'none');
        $j('#MT-Footer-Who').addClass('MT-Footer-Who-Highlight-Idle');
        state_chatrooms = 0;
    }
}


//______________Action on clicked Home Minimize____________________
function Clik_ChatHome_Minimize(){

    //check if its live or idle
    if (state_chathome == 1) {
        $j('#MT-Box-Home').removeClass();
        $j('#MT-Box-Home').addClass('MT-Footer-ChatRooms-Idle').slow;
        $j('#MT-Box-Home').css('display', 'none');
        state_chathome = 0;
    }
}


//______________Action on clicked chatrooms - Links___________________
function Clik_ChatRoom_Links(){

    //check who is online
    online_check = 1; //check who is online
    first_load = 1; //show text again
    //show and hide some things       
    $j('#MT-Box-Chatting').removeClass();
    $j('#MT-Footer-Chatting').removeClass();
    $j('#MT-Box-Chatting').addClass('MT-Box-Chatting-Live').slow;
    $j('#MT-Footer-Chatting').addClass('MT-Footer-Chatting-Highlight-Live');
    $j('#MT-Box-Chatting').css('display', 'block');
    //reset chat room text
    $j('#MT-Output-Window').html('');
    //display chatroom name
    $j('#Links-Chat-Chatting-Minimize').html(chat_room_name);
    $j('#Links-Chat-Chatting').html(chat_room_name);
    //____clear time out_____
    clearTimeout(keep_polling);
    Ajax_Feed_Instant();
}



//______________Action on clicked chattting - Buttons or Rooms links____________________
function Clik_Chatting_Taskbar(){

    $j('#MT-Box-Chatting').removeClass();
    $j('#MT-Footer-Chatting').removeClass();
    //check if its live or idle
    if (state_chatting == 1) {
        $j('#MT-Box-Chatting').addClass('MT-Footer-ChatChatting-Idle').slow;
        $j('#MT-Box-Chatting').css('display', 'none');
        $j('#MT-Footer-Chatting').addClass('MT-Footer-Chatting-Highlight-Live');
        state_chatting = 0;
    }
    else {
        $j('#MT-Box-Chatting').addClass('MT-Footer-ChatChatting-Live').slow;
        $j('#MT-Box-Chatting').css('display', 'block');
        $j('#MT-Footer-Chatting').addClass('MT-Footer-Chatting-Highlight-Live');
        state_chatting = 1;
    }
}


//______________Action on clicked chattting Minimize____________________
function Clik_ChatChatting_Minimize(){

    //check if its live or idle
    if (state_chatting == 1) {
        $j('#MT-Box-Chatting').removeClass();
        $j('#MT-Footer-Chatting').removeClass();
        $j('#MT-Box-Chatting').addClass('MT-Box-Chatting-Idle').slow;
        $j('#MT-Box-Chatting').css('display', 'none');
        $j('#MT-Footer-Chatting').addClass('MT-Footer-Chatting-Highlight-Idle');
        state_chatting = 0;
    }
}


//______________Action on clicked chatting - Button____________________
function Clik_ChatChatting_Taskbar(){

    $j('#MT-Box-Chatting').removeClass();
    $j('#MT-Footer-Chatting').removeClass();
    //check if its live or idle
    if (state_chatting == 1) {
        $j('#MT-Box-Chatting').addClass('MT-Box-Chatting-Idle').slow;
        $j('#MT-Box-Chatting').css('display', 'none');
        $j('#MT-Footer-Chatting').addClass('MT-Footer-Chatting-Highlight-Live');
        state_chatting = 0;
    }
    else {
        $j('#MT-Box-Chatting').addClass('MT-Box-Chatting-Live').slow;
        $j('#MT-Box-Chatting').css('display', 'block');
        $j('#MT-Footer-Chatting').addClass('MT-Footer-Chatting-Highlight-Live');
        state_chatting = 1;
        online_check = 1; //check who is online		
    }
}


//______________Action on clicked closing chat box - Button____________________
function Clik_ChatChatting_Close_Taskbar(){

    $j('#MT-Box-Chatting').removeClass();
    $j('#MT-Footer-Chatting').removeClass();
    $j('#MT-Box-Chatting').addClass('MT-Box-Chatting-Idle').slow;
    $j('#MT-Footer-Chatting').addClass('MT-Footer-Chatting-Inactive');
    state_chatting = 1; //just used '1' to overide hover action
    //reset chat room text
    $j('#MT-Output-Window').html('');
    //set the var to close end chat
    last_load_chat_window = 1;
    
    //____clear time out_____
    clearTimeout(keep_polling);
    //reset chat text
    Chatting_Stats_Reset();
}


//______________Action on hover and Unhover chatting - Button____________________
function Hover_ChatChatting_Taskbar(){

    if (state_chatting != 1) {
        $j('#MT-Footer-Chatting').removeClass();
    }
    $j('#MT-Footer-Chatting').addClass('MT-Footer-Chatting-Highlight-Live');
}

function UnHover_ChatChatting_Taskbar(){

    if (state_chatting != 1) {
        $j('#MT-Footer-Chatting').removeClass();
    }
    $j('#MT-Footer-Chatting').addClass('MT-Footer-Chatting-Highlight-Idle');
}



//______________Action on hover and Unhover menu - Home____________________
function Hover_Menu_Home(){
    $j('#MT-Footer-Menu-Home').removeClass();
    $j('#MT-Footer-Menu-Home').addClass('MT-Footer-Menu-Home-Live');
}

function UnHover_Menu_Home(){
    $j('#MT-Footer-Menu-Home').removeClass();
    $j('#MT-Footer-Menu-Home').addClass('MT-Footer-Menu-Home-Idle');
}


//______________Action on hover and Unhover menu - Close____________________
function Hover_Menu_Close(){
    $j('#MT-Footer-Menu-Close').removeClass();
    $j('#MT-Footer-Menu-Close').addClass('MT-Footer-Menu-Close-Live');
}

function UnHover_Menu_Close(){
    $j('#MT-Footer-Menu-Close').removeClass();
    $j('#MT-Footer-Menu-Close').addClass('MT-Footer-Menu-Close-Idle');
}


//______________Action on hover and Unhover menu - Emaile____________________
function Hover_Menu_Email(){
    $j('#MT-Footer-Menu-Email').removeClass();
    $j('#MT-Footer-Menu-Email').addClass('MT-Footer-Menu-Email-Live');
}

function UnHover_Menu_Email(){
    $j('#MT-Footer-Menu-Email').removeClass();
    $j('#MT-Footer-Menu-Email').addClass('MT-Footer-Menu-Email-Idle');
}


//______________Resizing div widths when window size goes below threshold____________________
function Resizing_Window(){
    window_width_resized = $j(window).width();
    //check if widnows in too small - load it fixed width
    if (window_width_resized < 880) {
        $j('#MT-Footer-Container2').removeClass();
        $j('#MT-Footer-Container2').addClass('Window-Resizing');
        $j('#MT-Footer-Container').removeClass();
        $j('#MT-Footer-Container').addClass('Window-Resizing');
    }
    else {
        //if its ok lets load it as 100% elastic	
        $j('#MT-Footer-Container').removeClass();
        $j('#MT-Footer-Container').addClass('Window-Default');
        $j('#MT-Footer-Container2').removeClass();
        $j('#MT-Footer-Container2').addClass('Window-Default');
    }
}


//______________Search Form____________________
function Search_Videos(){

    //submit search
    document.toolbarsearch.submit();
}




//______________Various Shut Downs____________________
function System_Total_Shut_down(){

    //____clear time out_____
    clearTimeout(keep_polling);
    //reset chatting box
    Chatting_Stats_Reset();
}


///______________Chatting Stats____________________
function Chatting_Stats(){
    stats = '<strong>' + lang_people_online + '</strong> ' + online;
    $j('#MT-Box-Chatting-Last-Message').html(stats)
}

//______________Chatting Stats____________________
function Chatting_Stats_Rest(){


    //    stats = '<strong>Last activity:</strong> n/a ';
    //    $j('#MT-Box-Chatting-Last-Message').html(stats)
}

//______________Chatting Stats____________________
function Chatting_Stats_Reset(){

    stats = '';
    $j('#MT-Box-Chatting-Last-Message').html(stats)
}


//______________Lets be nice - clean up some things on window close____________________
$j(window).unload(function(){


    //____clear time out_____
    clearTimeout(keep_polling);
});



