/**
 *
 * @param {function pointer} function_
 */
function addLoadEvent(function_){
//    $("#preLoadDiv").show();
//    var old = window.onload;
//    document.getElementById("preLoadDiv").style.display = "block";
//    if (typeof window.onload != 'function') {
//        window.onload = function_;
//   }
//    else {
//        window.onload = function(){
//            if (old) {
//                old();
//            }
//            function_();
//        }
//    }
}

/**
 * This function only should use under ie.
 * @param idname string
 * @param jqobject Object
 **/
function ieShowMenuPopup(idname,jqobject) {
    setDisplayIn(idname);
    jqobject.addClass('selected');
}

/**
 * Hides the given menu
 **/
function ieHideMenuPopup(idname,jqobject) {
    setDisplayOut(idname);
    jqobject.removeClass('selected');
}


/**
 * Shows a new element which the event happens via ajax
 **/
function addAjaxEvent(function_){
    var old = $("#mainPicture").onload;
    $("#preLoadDiv").style.display = "block";
    if (typeof $("mainPicture").onload != 'function') {
        $("#mainPicture").onload = function_;
    }
    else {
        $("#mainPicture").onload = function(){
            if (old) {
                old();
            }
            function_();
        };
    }
}

/**
 * Votes to the given user
 * @param remote_user_id
 * @param current_user_id
 * @return void
 **/
function voteProfile(remote_user_id,current_user_id) {
    $.ajax({
        type: 'POST',
        url: 'function/profil/profile_vote.php',
        data: 'ud=' + remote_user_id + '&ad=' +  current_user_id ,
        success: function(msg){
            $('#user_profile_points_number').html(msg);
        }
    });
}


/**
 * Loads the events to the given profile
 * @param uid The given user id
 * @return void
 */
function loadDefaultEvents(uid) {
    $.ajax({
        type: 'POST',
        url: 'Extensions/Event/ListEvents.php',
        data: 'isProfileV=true&getuid='+uid,
        success: function(msg){
            $("div[nameIs='userEvents']").html('');
            $("div[nameIs='userEvents']").html(msg);
        }
    });
}





/**
 * Sends a message to the given user
 * @return void
 **/
function profile_messagewall_send_message() {
    var error = 0;
    var prof_message = $("#prof_mes");
    if ( prof_message.value == "" ) {
        error = 1;
        alert("Nem írtál be üzenetet!");
    }

    if ( giuser_id == 0 ) {
        error = 1;
        alert("Üzenet küldéshez be kell jelentkezned!");
    }
    
    if (!error) {
        document.MWALLMS.MWALL_MESS.value = $("#prof_mes").val();
        document.MWALLMS.submit();
    }
}

var isShowPersonalMenu = false;
var isShowSystemMenu = false;
var isShowContentMenu = false;
var isShowUserContentMenu = false;

$(document).ready(function(){
    $("#preLoadDiv").hide();
    
    $( "#mainaccordion" ).accordion();
    
    // Optimal settings
    if ( screen.width == 1024 ) {
        if (  ! $.browser.msie ) {
            $("div#ux_resolution_hack").attr('style','height:32px;');
        }
    }
    
    // Removes the elements on the site if the browser is msie.......
    jQuery.each(jQuery.browser, function(browser, cond) {
    		if ( browser == 'msie' && cond == true ) {
    			$("div#ux-taskbar").attr('style','height:0px !important;');
    		}
    });
    
    if ( $("td.default_menu") != undefined && $("td.default_menu") != null && ! $.browser.msie  ) {
        $("td.default_menu").buildMenu({
            template:"",
            additionalData:"",
            menuWidth:200,
            openOnRight:false,
            menuSelector: ".menuContainer",
            iconPath:"jquery/mbmenu/ico/",
            hasImages:false,
            fadeInTime:100,
            fadeOutTime:100,
            adjustLeft:2,
            minZindex:"auto",
            adjustTop:10,
            opacity:.95,
            shadow:false,
            shadowColor:"#ccc",
            hoverIntent:0,
            openOnClick:false,
            closeOnMouseOut:true,
            closeAfter:50,
            submenuHoverIntent:100
        });
    }

    $("div[id='system_suggestion']").effect('shake', {}, 500, function(){
        $("#sys_welcome").removeAttr('style');
    });


    $("select[name='templates']").change(function(){
        self.location.href = "index.php?template=set&id=" + $(this).val();
    });

     // adds a new class to tables which use the default background
    $(document).find('table').each(function() {
       if ( $(this).attr('style') != null && $(this).attr('style') != undefined ) {
            if ( $(this).attr('style').indexOf("box_2.gif") != -1 ) {
                $(this).addClass("inputs_tables_version_1");
            } 
       }
    });

    // Fixes the css to elements which use old properties
    $(document).find('td').each(function(){

        if ( $(this).attr('utype') != undefined && $(this).attr('utype') != null ) {
            if ( $(this).attr('utype') == 'TD_CELL_FIRST_STYLE_ATTRIBUTE' ) {
                $(this).addClass('default_first_short_colum');
            } else if ( $(this).attr('utype') == 'TD_CELL_SECOND_STYLE_ATTRIBUTE' ) {
                $(this).addClass('default_second_short_colum');
            } else if ( $(this).attr('utype') == 'TD_CELL_LONG_FIRST_STYLE_ATTRIBUTE' ) {
                $(this).addClass('default_first_long_colum');
            } else if ( $(this).attr('utype') == 'TD_CELL_LONG_SECOND_STYLE_ATTRIBUTE' ) {
                $(this).addClass('default_second_long_column');
            }
        }


        if ($(this).attr('class') != null && $(this).attr('style') != null) {
            if ($(this).attr('class') == 'mainHeader' && $(this).attr('style').indexOf('box_head_1.gif') == -1) {
                $(this).attr('style', '');
            }
        }

        if ($(this).attr('style') != null) {
            if ($(this).attr('style').indexOf('box_head_1.gif') != -1) {
                $(this).attr('style', ($(this).attr('style')) + 'border-bottom: 1px solid #d1d1d1;');
            }
        }

        if ($(this).attr('class') != null) {
            if ($(this).attr('class') == 'inputsTdS') {
                $(this).parent('tr').parent('tbody').parent('table').addClass('inputs_tables_version_1');
                $(this).addClass('inputs_cells_overflow');

            }
        }

        if ($(this).attr('id') != null) {
            if ($(this).attr('id') == 'inputsTdS') {
                $(this).parent('tr').parent('tbody').parent('table').addClass('inputs_tables_version_1');
                $(this).addClass('inputs_cells_overflow');
            }
        }
    });

    $("div[uclickkey='content']").click(function(){
        showContentMenu();
    });

    $("div[uclickkey='user_content']").click(function(){
        showUserContentsMenu();
    });

    $("div[uclickkey='system']").click(function(){
        showSystemMenu();
    });

    $("div[uclickkey='person_menu']").click(function(){
        showPersonalMenu();
    });

    $(document).find('div').each(function(){
        if ($(this).attr('ulkey') != null) {
            if ($(this).attr('ulkey') == 'person_menu' || $(this).attr('ulkey') == 'system' ||
                $(this).attr('ulkey') == 'content' ||
                $(this).attr('ulkey') == 'user_content') {
                $(this).hide();
            }
        }
    });

    $("select[name='cat_MOD_PHP']").change(function(){
        self.location.href = document.location + "&s_cat_mode=" + $("select[name='cat_MOD_PHP']").val();
    });

    //checks whether the given mail is valid
    if ( $("#us_mail_re") != undefined && $("#us_mail_re") != null) {
        $("#us_mail_re").focusout(function(){
            if ( $("#usMail").val() != $("#us_mail_re").val() ) {
                $("#usMail").attr("style","border: 2px solid red;width:170px;");
                $("#us_mail_re").attr("style","border: 2px solid red;width:170px;");
                $("#uslabel_error").attr("style","color:red;text-decoration:underline;font-size:12px;");
                $("#uslabel_error").html("Az email címek nem egyeznek meg");
            } else {
                $("#uslabel_error").html("");
                checkEmailValid();
            }
        });

         $("#usMail").focusout(function(){
            if ( $("#us_mail_re").val() != "" ) {
                if ( $("#usMail").val() != $("#us_mail_re").val() ) {
                    $("#usMail").attr("style","border: 2px solid red;width:170px;");
                    $("#us_mail_re").attr("style","border: 2px solid red;width:170px;");
                    $("#uslabel_error").attr("style","color:red;text-decoration:underline;font-size:12px;");
                    $("#uslabel_error").html("Az email címek nem egyeznek meg");
                } else {
                    $("#uslabel_error").html("");
                    checkEmailValid();
                }
            }
        });

        $("#usName").focusout(function(){
            if ( $("#usName").val().length >= 3 && $("#usName").val().length <= 12 ) {
                  $("#preLoadDiv").show();
                         $.ajax({
			    type: "POST",
			    url: "Extensions/checkusername.php",
			    data: "uname=" + $("#usName").val(),
			    dataType: "html",
			    success: function(res) {
                                var resp = eval('(' + res + ')');
                                if ( resp.retreive == "true" ) {
                                    $("#usName").attr("style","border: 2px solid green;width:170px;");
                                    $("#unamelabel_err").html("");
                                } else if (resp.retreive == "false" ) {
                                    $("#usName").attr("style","border: 2px solid red;width:170px;");
                                    $("#unamelabel_err").attr("style","color:red;text-decoration:underline;font-size:12px;");
                                    $("#unamelabel_err").html(resp.mess);
                                }
                                $("#preLoadDiv").hide();
                            }
                         });
            }
        });
    }

});

/**
 * Checks whether the given mail is valid
 * @return int
 */
function checkEmailValid() {
    if ( $("#us_mail_re").val() == "" )
        return -1;
    $("#preLoadDiv").show();
    $.ajax({
			    type: "POST",
			    url: "Extensions/CheckEmailValid.php",
			    data: "mail=" + $("#us_mail_re").val(),
			    dataType: "html",
			    success: function(res) {
                                var resp = eval('(' + res + ')');
                                if ( resp.retreive == "true" ) {
                                    $("#regbtn").removeAttr("disabled");
                                    $("#usMail").attr("style","border: 2px solid green;width:170px;");
                                    $("#us_mail_re").attr("style","border: 2px solid green;width:170px;");
                                    $("#uslabel_error").attr("style","color:red;text-decoration:underline;font-size:12px;");
                                    $("#uslabel_error").html("");
                                } else if (resp.retreive == "false" ) {
                                    $("#usMail").attr("style","border: 2px solid red;width:170px;");
                                    $("#us_mail_re").attr("style","border: 2px solid red;width:170px;");
                                    $("#uslabel_error").attr("style","color:red;text-decoration:underline;font-size:12px;");
                                    $("#uslabel_error").html(resp.mess);
                                }
                                $("#preLoadDiv").hide();
                            }
    });
    return 1;
}

function showUserContentsMenu(){
    if (!isShowUserContentMenu) {
        $(document).find('div').each(function(){
            if ($(this).attr('ulkey') != null) {
                if ($(this).attr('ulkey') == 'user_content') {
                    $(this).show(1000);
                }
            }
        });
        $("a[id='user_content_down_arrow']").html('<img src="images/arrow/up_arrow.gif" border="0"/>&nbsp;');
        isShowUserContentMenu = true;
    }
    else {
        $(document).find('div').each(function(){
            if ($(this).attr('ulkey') != null) {
                if ($(this).attr('ulkey') == 'user_content') {
                    $(this).hide(1000);
                }
            }
        });
        $("a[id='user_content_down_arrow']").html('<img src="images/arrow/down_arrow.gif" border="0"/>&nbsp;');
        isShowUserContentMenu = false;
    }

}


function showContentMenu(){
    if (!isShowContentMenu) {
        $(document).find('div').each(function(){
            if ($(this).attr('ulkey') != null) {
                if ($(this).attr('ulkey') == 'content') {
                    $(this).show(1000);
                }
            }
        });

        $("a[id='content_down_arrow']").html('<img src="images/arrow/up_arrow.gif" border="0"/>&nbsp;');

        isShowContentMenu = true;
    }
    else {
        $(document).find('div').each(function(){
            if ($(this).attr('ulkey') != null) {
                if ($(this).attr('ulkey') == 'content') {
                    $(this).hide(1000);
                }
            }
        });
        $("a[id='content_down_arrow']").html('<img src="images/arrow/down_arrow.gif" border="0"/>&nbsp;');
        isShowContentMenu = false;
    }

}

function showSystemMenu(){
    if (!isShowSystemMenu) {
        $(document).find('div').each(function(){
            if ($(this).attr('ulkey') != null) {
                if ($(this).attr('ulkey') == 'system') {
                    $(this).show(1000);
                }
            }
        });

        $("a[id='system_down_arrow']").html('<img src="images/arrow/up_arrow.gif" border="0"/>&nbsp;');

        isShowSystemMenu = true;
    }
    else {
        $(document).find('div').each(function(){
            if ($(this).attr('ulkey') != null) {
                if ($(this).attr('ulkey') == 'system') {
                    $(this).hide(1000);
                }
            }
        });
        $("a[id='system_down_arrow']").html('<img src="images/arrow/down_arrow.gif" border="0"/>&nbsp;');
        isShowSystemMenu = false;
    }

}

function showPersonalMenu(){
    if (!isShowPersonalMenu) {
        $(document).find('div').each(function(){
            if ($(this).attr('ulkey') != null) {
                if ($(this).attr('ulkey') == 'person_menu') {
                    $(this).show(1000);
                }
            }
        });
        $("a[id='down_arrow']").html('<img src="images/arrow/up_arrow.gif" border="0"/>&nbsp;');
        isShowPersonalMenu = true;
    }
    else {
        $(document).find('div').each(function(){
            if ($(this).attr('ulkey') != null) {
                if ($(this).attr('ulkey') == 'person_menu') {
                    $(this).hide(1000);
                }
            }
        });
        $("a[id='down_arrow']").html('<img src="images/arrow/down_arrow.gif" border="0"/>&nbsp;');
        isShowPersonalMenu = false;
    }

}

/**
 * Set the DOM element display property in headmenu
 * @param idname
 * @return
 */
function setDisplayIn(idname){
    document.getElementById(idname).style.display = "block";
}

/**
 * Set the DOM element display property in headmenu
 * @param idname
 * @return
 */
function setDisplayOut(idname){

    document.getElementById(idname).style.display = "none";
}


/**
 * Adds the given user to the blocklist
 */
function pboxBlockUser(user_id) {
    $( "#dialog-pbconfirm" ).dialog({
			resizable: false,
			height:140,
			modal: true,
                        show: "blind",
			hide: "explode",
                        position: 'center', 
			buttons: {
				"Blokkolás": function() {
                                         $.ajax({
                                                    type: "POST",
                                                    url: "Extensions/messagewall/blockuser.php",
                                                    data: "ignoreuid=" + user_id,
                                                    dataType: "html",
                                                    success: function(response) {
                                                          rdata = eval( '(' + response + ')' );
                                                          if ( rdata.message != null ) {
                                                              alert(rdata.message);
                                                          }
                                                    }
                                         });
					$( this ).dialog( "close" );
				},
				"Mégsem": function() {
					$( this ).dialog( "close" );
				}
			}
    });
}
