var polling = 2000; 
var max_idle = 300; 
var poll_stats = 15;
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(){
    
    Login_Check();
    
    $j('#Links-Chat-Room').html(lang_select_chatroom);
    $j('#MT-Footer-Menu-Search-Input').val(lang_search_videos);
    
    window_starting_size = $j(window).width();
    
    if (window_starting_size < 880) {
        Resizing_Window();
    }
    else {
        
        $j('#MT-Footer-Container').removeClass();
        $j('#MT-Footer-Container').addClass('Window-Default');
        $j('#MT-Footer-Container2').removeClass();
        $j('#MT-Footer-Container2').addClass('Window-Default');
    }
    
    $j(window).resize(function(){
        Resizing_Window();
    });
    
    $j('#Links-Chat-Room').click(function(){
        Clik_ChatRoom_Taskbar();
    });
    
    
    $j('#Links-Menu-Home').click(function(){
        Clik_ChatHome_Taskbar();
    });
    
    $j('#Links-Chat-Room').hover(function(){
        Hover_ChatRoom_Taskbar();
    }, function(){
        UnHover_ChatRoom_Taskbar();
    });
    
    clicked_room = function(){
        
        chat_room_id = $j(this).attr('rel');
        chat_room_name = $j(this).html();
        
        if (chat_room_name == '') {
            chat_room_name = 'Undefined';
        }
        
        Clik_ChatRoom_Links();
    };
    
    $j('#MT-Chat-Room-List a').livequery('click', clicked_room);
    
    $j('#Links-Chat-Room-Minimize').click(function(){
        Clik_ChatRoom_Minimize();
    });
    
    
    $j('#Links-Chat-Home-Minimize').click(function(){
        Clik_ChatHome_Minimize();
    });
    
    
    $j('#Links-Chat-Chatting-Minimize').click(function(){
        Clik_ChatChatting_Minimize();
    });
    
    $j('#Links-Chat-Chatting').click(function(){
        Clik_ChatChatting_Taskbar();
    });
    
    $j('#Links-Chat-Chatting-Close').click(function(){
        Clik_ChatChatting_Close_Taskbar();
    });
    
    $j('#Links-Chat-Chatting').hover(function(){
        Hover_ChatChatting_Taskbar();
    }, function(){
        UnHover_ChatChatting_Taskbar();
    });
    
    $j('#Links-Chat-Chatting-Close').hover(function(){
        Hover_ChatChatting_Taskbar();
    }, function(){
        UnHover_ChatChatting_Taskbar();
    });
    
    $j('#Links-Menu-Home').hover(function(){
        Hover_Menu_Home();
    }, function(){
        UnHover_Menu_Home();
    });
    
    $j('#Links-Menu-Close').hover(function(){
        Hover_Menu_Close();
    }, function(){
        UnHover_Menu_Close();
    });
    
    $j('#Links-Menu-Email').hover(function(){
        Hover_Menu_Email();
    }, function(){
        UnHover_Menu_Email();
    });
    
    $j('#Links-Menu-Email-Count').hover(function(){
        Hover_Menu_Email();
    }, function(){
        UnHover_Menu_Email();
    });
    
    $j('#MT-Input-Typing').keyup(function(e){
        
        if (e.keyCode == 13) {
            
            new_message = $j('#MT-Input-Typing').attr('value');
            
            $j('#MT-Input-Typing').attr('value', '');
            
            if (new_message != '') {
                Ajax_Post_New();
            }
        }
    });
    
    $j('#MT-Footer-Menu-Search-Input').focus(function(){
        
        $j('#MT-Footer-Menu-Search-Input').attr('value', '');
    })
    
    $j('#MT-Footer-Menu-Search-Submit').click(function(){
        
        search_key_words = $j('#MT-Footer-Menu-Search-Input').attr('value');
        
        if (version == 2) {
            $j('#MT-Footer-Menu-Search-Form').attr('method', 'POST');
        }
        Search_Videos();
    })
    
    $j('#Links-Menu-Close').click(function(){
        Ajax_Session_Logout();
    });
    
});
function Login_Check(){
    $j.ajax({
        type: "POST",
        url: mtb_base_url + "ajax_env.php",
        data: "query=logincheck",
        success: function(xml){
        
            
            logged_stat = $j("logged", xml).text();
            menulinks = $j("menulinks", xml).text();
            
            
            
            
            $j('#MT-Home-Links').html(menulinks);
            
            
            Login_Results(logged_stat);
        }
    });
}
function Login_Results(){
    if (logged_stat == 2 || logged_stat == 3) {
        logged_in = 1; 
        
        $j('#MT-Footer-Container').css('visibility', 'visible');
        $j('#MT-Footer-Container').css('display', 'block');
        $j('#MT-Spacer').css('display', 'block');
        
        Ajax_Check_email();
        
        Ajax_Rooms_list();
    }
    
    if (logged_stat == 2) {
        version = 2;
    }
    if (logged_stat == 3) {
        version = 3;
    }
    
    if (version == 3 && chat_ajax == 1) {
        $j('#MTB_Linked').css('display', 'none');
    }
}
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);
        }
    });
}
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);
        }
    });
}
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(){
            
            Ajax_Feed_Instant();
        }
    });
}
function Ajax_Feed_Instant(){
    
    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){
            
            timestamp_new = $j("timestamp", xml).text();
            message = $j("message", xml).text();
            
            if (online_check == 1) {
                online = $j("online", xml).text();
            }
            
            
            if (timestamp_new > timestamp || first_load == 1) {
                $j("#MT-Output-Window").html('');
                $j("#MT-Output-Window").html(message);
                timestamp = timestamp_new;
            }
            
            $j('#MT-Output-Window').animate({
                scrollTop: $j('#MT-Output-Window')[0].scrollHeight
            });
        },
        complete: function(){
            first_load = 0; 
            online_check = 0; 
            Chatting_Stats(); 
            
            online_check_counter = online_check_counter + 1;
            if (online_check_counter == 6) {
                online_check = 1;
                online_check_counter = 0;
            }
            
            keep_polling = setTimeout(Ajax_Feed_Instant, 5000);
        }
    });
}
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');
        }
    });
    
    System_Total_Shut_down();
}
function Clik_ChatRoom_Taskbar(){
    $j('#MT-Box-Rooms').removeClass();
    $j('#MT-Footer-Who').removeClass();
    
    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;
    }
}
function Clik_ChatHome_Taskbar(){
    $j('#MT-Box-Home').removeClass();
    
    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;
    }
}
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');
}
function Clik_ChatRoom_Minimize(){
    
    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;
    }
}
function Clik_ChatHome_Minimize(){
    
    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;
    }
}
function Clik_ChatRoom_Links(){
    
    online_check = 1; 
    first_load = 1; 
    
    $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');
    
    $j('#MT-Output-Window').html('');
    
    $j('#Links-Chat-Chatting-Minimize').html(chat_room_name);
    $j('#Links-Chat-Chatting').html(chat_room_name);
    
    clearTimeout(keep_polling);
    Ajax_Feed_Instant();
}
function Clik_Chatting_Taskbar(){
    $j('#MT-Box-Chatting').removeClass();
    $j('#MT-Footer-Chatting').removeClass();
    
    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;
    }
}
function Clik_ChatChatting_Minimize(){
    
    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;
    }
}
function Clik_ChatChatting_Taskbar(){
    $j('#MT-Box-Chatting').removeClass();
    $j('#MT-Footer-Chatting').removeClass();
    
    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; 
    }
}
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; 
    
    $j('#MT-Output-Window').html('');
    
    last_load_chat_window = 1;
    
    
    clearTimeout(keep_polling);
    
    Chatting_Stats_Reset();
}
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');
}
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');
}
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');
}
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');
}
function Resizing_Window(){
    window_width_resized = $j(window).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 {
        
        $j('#MT-Footer-Container').removeClass();
        $j('#MT-Footer-Container').addClass('Window-Default');
        $j('#MT-Footer-Container2').removeClass();
        $j('#MT-Footer-Container2').addClass('Window-Default');
    }
}
function Search_Videos(){
    
    document.toolbarsearch.submit();
}
function System_Total_Shut_down(){
    
    clearTimeout(keep_polling);
    
    Chatting_Stats_Reset();
}
function Chatting_Stats(){
    stats = '<strong>' + lang_people_online + '</strong> ' + online;
    $j('#MT-Box-Chatting-Last-Message').html(stats)
}
function Chatting_Stats_Rest(){
    
    
}
function Chatting_Stats_Reset(){
    stats = '';
    $j('#MT-Box-Chatting-Last-Message').html(stats)
}
$j(window).unload(function(){
    
    clearTimeout(keep_polling);
});

