$(document).ready(function() {

    $.localns = { callfromlogin: "no"}  // info whether the method is called form login page or not

    //on submit link click event
    $('#add_url').keydown(function() {

        var send_keywords = $('#q').val();
        send_keywords = send_keywords.replace(/&/g, '___');
        send_keywords = send_keywords.replace(/\n/g, '$=====$');


        $(this).attr("alt", '/crawl.php?address=' + encodeURI($('#url').val().replace('&', '___').replace('#', '!=====!')) + '&keywords=' + encodeURI(send_keywords) + '&height=590&width=490');
    });

    $('#add_url').mousedown(function() {

        var send_keywords = $('#q').val();
        send_keywords = send_keywords.replace(/&/g, '___');
        send_keywords = send_keywords.replace(/\n/g, '$=====$');



        $(this).attr("alt", '/crawl.php?address=' + encodeURI($('#url').val().replace('&', '___').replace('#', '!=====!')) + '&keywords=' + encodeURI(send_keywords) + '&height=590&width=490');
    });

    $('#add_url_profile').click(
        function() {
            var send_keywords = $('#q').val();
            send_keywords = send_keywords.replace(/&/g, '___');
            send_keywords = send_keywords.replace(/\n/g, '$=====$');

            $('#loading1').show();

            $.post("/ad_multikeywords.php", { address: encodeURI($('#url').val().replace('&', '___')), keywords: encodeURI(send_keywords) },
                function(data) {
                    $('#loading1').hide();
                    if (data != 'OK') {
                        alert(data);
                        if (data.indexOf('successfull') != -1)
                            tb_remove();

                    }
                    else {
                        alert('Keyword(s) has been submitted successfully!');
                        tb_remove();
                    }
                });

            return false;
        }
    );

    $('#frmlogin').click(
        function() {
            $('#loading1').show();

            $.post("/users/check.php", { usrusername: $('#usrusername').val(), usrpassword: $('#usrpassword').val() },
                function(data) {
                    $('#loading1').hide();
                    if (data != 'OK') {
                        alert(data);
                    }
                    else {
                        $('#pmenu').hide();
                        $('#umenu').show();
                        $('#umenu2').show();

                        if ($('#extern').val() == '1') {
                            $.post("/check_url.php", { address: encodeURI($('#url').val().replace('&', '___')) },
                             function(data) {
                                 if (data == 'NOK') {
                                     tb_show('Submit Link', '/crawl_extern.php?address=' + encodeURI($('#url').val().replace('&', '___')) + '&height=590&width=480')
                                 }
                                 else
                                     window.location = '/urlprofile/' + $('#url').val().replace('http://', '').replace('https://', '');
                             });
                        }
                        else {
                            //the value of the  field advertise_keyword_red has the following meaning
                            //1 - comes from another web site
                            //0 - comes from stumpedia (results page)

                            if ($("#advertise_keyword_red").length > 0 && $("#advertise_keyword_red").val() != "") {
                                tb_show('Ad Advertise', '/advert_keywords.php?height=280&width=480')
                            }
                            else {
                                tb_show('Submit Link', '/crawl.php?address=' + encodeURI($('#url').val().replace('&', '___')) + '&keywords=' + encodeURI($('#q').val().replace('&', '___')) + '&height=590&width=480')
                            }
                        }
                    }
                });

            return false;
        }
    );

    $('#frmlogin1').click(
        function() {
            $('#loading1').show();

            $.post("/users/check.php", { usrusername: $('#usrusername').val(), usrpassword: $('#usrpassword').val() },
                function(data) {
                    $('#loading1').hide();
                    if (data != 'OK') {
                        alert(data);
                    }
                    else {
                        $('#pmenu').hide();
                        $('#umenu').show();
                        $('#umenu2').show();


                        if ($('#red_to_ad').val() == "yes") {

                            $("#ad_check").click();
                            //tb_remove();
                            tb_show("Submit Ad keywords", "/advert_keywords.php?height=280&width=480&inlineId=newurl");
                        }
                        else {
                            if ($('#red_to_ad_mess').val() == "yes") {
                                $.localns.callfromlogin = "yes";
                                tb_remove();
                                $("#submitadmessage").click();
                            }
                            else {
                                tb_remove();
                                window.location.reload(true);
                            }
                        }
                    }
                });

            return false;
        }
    );


    $('#frmloginf').click(
        function() {
            $('#loading1').show();
            var uid = $('#userurl').val();
            $.post("/users/check.php", { usrusername: $('#usrusername').val(), usrpassword: $('#usrpassword').val() },
                function(data) {
                    $('#loading1').hide();
                    if (data != 'OK') {
                        alert(data);
                    }
                    else {
                        $('#pmenu').hide();
                        $('#umenu').show();
                        $('#umenu2').show();

                        tb_remove();
                        window.location = '/joinfriends/?uid=' + uid;
                    }
                });

            return false;
        }
    );

    $('#frmadvertise').click(
        function() {
            $('#loading1').show();


            $.get("/advert_keywords.php", { ad_check: "yes", from_page: "advertise" },
                function(data) {

                    $('#loading1').hide();

                    if (data == "logged") {
                        tb_show('Submit Ad keywords', '/advert_keywords.php?height=280&width=480&k=1');
                    }
                    else
                        tb_show('Add Advertise', '/users/index.php?advertise_keyword_red=' + $('#advertise_keyword_red').val() + '&height=590&width=490');

                });





        }
    );


    $('#frmremovefriend').click(
        function() {
            $('#loading1').show();

            $.post("/remove.php", { fuser_id: $('#fuser_id').val(), ffriend_id: $('#ffriend_id').val() },
                function(data) {
                    $('#loading1').hide();
                    if (data != 'OK') {
                        alert('something wrong');
                        //alert(data);
                    }
                    else {

                        tb_remove();
                        alert('Friend has been sucessfully removed!');
                        window.location.reload(true);
                    }
                });

            return false;
        }
    );

    $('#frmremoveblock').click(
        function() {
            $('#loading1').show();

            $.post("/remove_block.php", { blocked_user_id: $('#smsblocked_user_id').val(), user_id: $('#smsuser_id').val() },
                function(data) {
                    $('#loading1').hide();
                    if (data != 'OK') {
                        alert('something wrong');
                        //alert(data);
                    }
                    else {

                        tb_remove();
                        alert("User has been sucessfully removed from your Blocked SMS users list!");
                        window.location.reload(true);
                    }
                });

            return false;
        }
    );

    $('#frmremovecancel').click(
        function() {
            $('#loading1').show();

            tb_remove();

            $('#loading1').hide();
            return false;
        }
    );



    $('#frmsubmitlink').click(
        function() {
            $('#loading1').show();

            var media_value = $('[name=admmedia]:checked').val();
            if (media_value == undefined) media_value = "";

            var share_value = $('[name=admshare]:checked').val();
            if (share_value == undefined) share_value = "0"; else share_value = "1";

            var extern_value = $('#extern').val();
            if (extern_value == undefined) extern_value = "0"; else extern_value = "1";

            $.post("/url_post.php", { admaddress: $('#admaddress').val(), admtitle: $('#admtitle').val(), admdescription: $('#admdescription').val(), admid: $('#admid').val(), admkeyword: $('#admkeyword').val(), admmedia: media_value, admshare: share_value,
                admlanguage_id: $('#admlanguage_id').val(), admcategory_id: $('#admcategory_id').val(), suggest_category: $('#suggest_category').val(), comment: $('#admcomment').val()
            },
                function(data) {
                    $('#loading1').hide();
                    if (data != 'OK') {
                        alert(data);
                    }
                    else {
                        alert('Link has been submitted successfully!');
                        tb_remove();
                        if (extern_value == 0)
                            window.location.reload(true);
                        else
                            window.location = 'index.php';


                    }
                });

            return false;
        }
    );


    $('#frmsubmitadvert').click(
        function() {
            $('#loading1').show();


            var extern_value = $('#extern').val();
            if (extern_value == undefined) extern_value = "0"; else extern_value = "1";

            $.post("/ad_keywords_post.php", { admkeyword: $('#admkeyword').val() },
                function(data) {
                    $('#loading1').hide();
                    if (data != 'OK') {
                        alert(data);
                    }
                    else {
                        alert('Keywords have been submitted successfully!');
                        tb_remove();

                        if ($('#advertise_keyword_red').length > 0 && $('#advertise_keyword_red').val() == 1)
                            document.location = "/";
                        if ($('#advertise_keyword_red').length > 0 && $('#advertise_keyword_red').val() == 0)
                            history.back();

                    }
                });

            return false;
        }
    );

    $("#submitadmessage").unbind("click");
    $('#submitadmessage').click(
        function() {

            $('#loading1').show();
            if ($('#admessage').val() != $('#admessage_compare').val()) {
                $.post("/ad_messages_post.php", { admessage: $('#admessage').val(), adkeyword_match: $('#adkeyword_match').val(),
                    callfromlogin: $.localns.callfromlogin
                },
                function(data) {

                    $('#loading1').hide();
                    if (data != 'OK') {
                        if (data == 'not logged')
                            tb_show('Submit Ad keywords', '/ad_messages_post.php?height=280&width=480&k=1');
                        else {
                            alert(data.replace(/<OK>/, ""));

                            if (data.indexOf('<OK>') != -1) {
                                $('#admessage').val("");
                                window.location.reload(true);
                            }

                        }
                    }
                    else {
                        alert('The message has been successfully sent!');

                        $('#admessage').val("");


                    }


                });
            }
            else {
                $('#loading1').hide();
                alert('Please enter a message!');
            }
            return false;
        }
    );


    $("#ad_check").unbind("click");
    $('#ad_check').click(
        function() {


            $('#loading1').show();

            $.get("/advert_keywords.php", { ad_check: "yes" },
                function(data) {

                    $('#loading1').hide();

                    if (data == "redirect") {
                        tb_remove();
                        document.location = "/advertise/";
                    }
                    else
                        tb_show('Submit Ad keywords', '/advert_keywords.php?height=280&width=480&k=1');

                });

            return false;
        }
    );

    $('#submitreplymessage').click(
        function() {
            $('#loading1').show();
            $.post("/ad_messages_post.php", { admessage: $('#admessage').val(), adkeyword_match: $('#adkeyword_match').val(),
                admessage_parent_id: $('#admessage_parent_id').val(), adreceiver_id: $('#adreceiver_id').val()
            },
                function(data) {
                    $('#loading1').hide();
                    if (data != 'OK') {
                        alert(data);
                    }
                    else {
                        alert('The message has been successfully sent!');
                        tb_remove();
                        window.location.reload(true);


                    }
                });

            return false;
        }
    );



    $('#frmcomment').click(
        function() {
            $('#loading1').show();
            if ($('#comments').val() == '') {
                alert(' - Please enter a Comment');
                return false;
            }
            $.post("/comment_post.php", { aurl_id: $('#aurl_id').val(), acomments: $('#acomments').val() },
                function(data) {
                    $('#loading1').hide();
                    if (data != 'OK') {
                        alert(data);
                        // tb_remove();
                    }
                    else {
                        alert('Comment has been submitted successfully!');
                        tb_remove();
                        window.location.reload(true);
                    }
                });
            return false;
        }
    );

    $('#frmfriend').click(
        function() {
            $('#loading1').show();

            $.post("/req_friend.php", { auser_id: $('#auser_id').val(), afriend_id: $('#afriend_id').val(), amessage: $('#amessage').val() },
                function(data) {
                    $('#loading1').hide();
                    if (data != 'OK') {
                        alert(data);
                        tb_remove();
                    }
                    else {
                        alert('Friend Request has been successfully sent!');
                        tb_remove();
                    }
                });
            return false;
        }
    );

    $('.frmjoinfriend').click(
        function() {
            $('#loading1').show();

            var key = $(this).attr('id');

            var user_id = $('#user_id' + key).val();
            var friend_id = $('#friend_id' + key).val();

            $.post("/confirm_friend.php?id=1", { auser_id: user_id, afriend_id: friend_id },
                function(data) {
                    $('#loading1').hide();
                    if (data != 'OK') {
                        alert(data);
                    }
                    else {
                        alert('Your Friend Confirmation has been successfull!');
                        tb_remove();
                        window.location = '/friends/?id=' + friend_id;
                    }
                });
            return false;
        }
    );

    $('.frmdenyfriend').click(
        function() {
            $('#loading1').show();

            var key = $(this).attr('id');

            var user_id = $('#user_id' + key).val();
            var friend_id = $('#friend_id' + key).val();

            $.post("/confirm_friend.php?id=0", { auser_id: user_id, afriend_id: friend_id },
                function(data) {
                    $('#loading1').hide();
                    if (data != 'OK') {
                        alert(data);
                    }
                    else {
                        alert('Your Friend Rejection has been successfull!');
                        tb_remove();
                        //window.location.reload(true);              
                        window.location = '/friends/?id=' + friend_id;
                    }
                });
            return false;
        }
    );



    $('#frmsubmittag').click(
        function() {
            $('#loading1').show();

            $.post("/users/tag_post.php", { admaddress: $('#admaddress').val(), admtitle: $('#admtitle').val(), admdescription: $('#admdescription').val(), admid: $('#admid').val(), admkeyword: $('#admkeyword').val(), admtags: $('#admtags').val(), admstatus: $('#admstatus').val(), admmember_id: $('#admmember_id').val()
            , admmedia: $('#admmedia').val(), admlanguage_id: $('#admlanguage_id').val(), admcategory_id: $('#admcategory_id').val()
            },
                function(data) {
                    $('#loading1').hide();
                    if (data != 'OK') {
                        alert(data);
                    }
                    else {
                        alert('Link has been submitted successfully!');
                        tb_remove();
                        window.location.reload(true);
                    }
                });

            return false;
        }
    );



    $('#frmforgot').click(
        function() {
            $('#loading1').show();

            $.post("/users/forgot.php", { usremail: $('#usremail').val() },
                function(data) {
                    $('#loading1').hide();
                    if (data != 'OK') {
                        alert(data);
                    }
                    else {
                        alert('Your password has been emailed to provided email address. Please check your email!');
                    }
                });

            return false;
        }
    );

    $('#frmrep').click(
        function() {
            $('#loading1').show();
            $.post("/report_post.php", { aurl_id: $('#aurl_id').val(), areason: $('#areason').val(), adescription: $('#adescription').val() },
                function(data) {
                    $('#loading1').hide();
                    if (data != 'OK') {
                        alert(data);
                    }
                    else {
                        alert('Report has been submitted successfully!');
                        tb_remove();
                    }
                });
            return false;
        }
    );

    $('#frmregister').click(function() {
        $('#loading1').show();
        $.post("/users/a_register.php", { usrusername: $('#usrnewusername').val(), usremail: $('#usrnewemail').val(), usrcodevalidator: $('#usrcodevalidator').val() },
                function(data) {
                    $('#loading1').hide();
                    alert(data);
                });
        return false;
    });

    $('#frmpassword').click(function() {
        $('#loading1').show();
        $.post("/users/a_password.php", { new_password: $('#new_password').val(), repeat_password: $('#repeat_password').val() },
                function(data) {
                    $('#loading1').hide();
                    alert(data);
                });
        return false;
    });


    $('#frm_invite_friends').click(function() {
        $('#loading1').show();
        $.post("/users/a_invite_friends.php", { email_address1: $('#email_address1').val(), email_address2: $('#email_address2').val(), email_address3: $('#email_address3').val(), email_address4: $('#email_address4').val(), message: $('#message').val() },
                function(data) {
                    $('#loading1').hide();
                    alert(data);
                });
        return false;
    });



    //if not logged in
    $('.vu1').click(function() {
        alert('Please Login or Sign Up first!');
    });
    $('.vd1').click(function() {
        alert('Please Login or Sign Up first!');
    });
    //if already voted
    $('.vu2').click(function() {

        return ClickEvent(this);
        //  $(this).removeClass().addClass('vd');
        // alert('This post is previously voted by you!');
        //  return false;
    });
    $('.vd2').click(function() {

        return ClickEventDown(this);
        //alert('This post is previously voted by you!');
        // return false;
    });



    function ClickEvent(obj) {
        var _this = obj;

        var classid = $(_this).children('.blue').attr('id');
        if ($(_this).hasClass("vu2") == true)
            alert('This post is previously voted by you!');
        else {
            $(_this).removeClass().addClass('vu2');
            $.post("/vote.php", { vurl_id: $(_this).children('.blue').attr('id'), vvote: 1, vclass: $(_this).next().hasClass("vd2") },
            function(data) {
                if (data == 'OK') {
                    //$(_this).removeClass().addClass('vu2');
                    //$(_this).parent().children('.vd').removeClass().addClass('vd');
                    //$('#vu'+classid).removeClass().addClass('vu');
                    //$(_this).children('.blue').text(parseInt($(_this).children('.blue').text())+1); 
                    //if($('#votedel').val()!=undefined && $('#votedel').val()!=null)
                    //{
                    //window.location.reload(true);
                    //                               
                    //}
                    //                        
                    //if($('#profile').val()!=undefined && $('#profile').val()!=null)
                    //{
                    //window.location.reload(true);
                    //                               
                    //}
                    window.location.reload(true);

                }
                //                else
                //                {
                //                    if(data=='OK1')
                //                    {
                ////                        $(_this).parent().children('.vd2').children('.red').text(parseInt($(_this).parent().children('.vd2').children('.red').text())-1); 
                ////                     
                ////                        $(_this).removeClass().addClass('vu');
                ////                        //$('#vd'+classid).removeClass().addClass('vd');
                ////                        $(_this).parent().children('.vd2').removeClass().addClass('vd'); 
                ////                        if($('#profile').val()!=undefined && $('#profile').val()!=null)
                ////                        {
                ////                            window.location.reload(true);
                ////                               
                ////                        }
                //                          window.location.reload(true);
                //                         
                //                    }
                else {
                    $(_this).unbind();
                    $(_this).removeClass().addClass('vu2');
                    alert('Error occurs while voting. Please try again later!');
                }
                //                }  
            });
        }



    }

    function ClickEventDown(obj1) {

        var _this = obj1;
        var classid = $(_this).children('.red').attr('id');

        if ($(_this).hasClass("vd2") == true)
            alert('This post is previously voted by you!');
        else {
            $(_this).removeClass().addClass('vd2');
            $.post("/vote.php", { vurl_id: $(_this).children('.red').attr('id'), vvote: 0, vclass: $(_this).prev().hasClass("vu2") },
            function(data) {
                if (data == 'OK') {

                    //                 $(_this).removeClass().addClass('vd2');
                    //                 $(_this).parent().children('.vu2').removeClass().addClass('vu');
                    //                // $('#vu'+classid).removeClass().addClass('vu');
                    //                 
                    //                 $(_this).children('.red').text(parseInt($(_this).children('.red').text())+1);
                    //                  if($('#profile').val()!=undefined && $('#profile').val()!=null)
                    //                    {
                    //                         window.location.reload(true);
                    //                           
                    //                    }
                    window.location.reload(true);
                }
                //                else
                //                {
                //                    if(data=='OK1')
                //                    { 
                //                        $(_this).parent().children('.vu2').children('.blue').text(parseInt($(_this).parent().children('.vu2').children('.blue').text())-1);
                //                        
                //                        $(_this).removeClass().addClass('vd'); 
                //                        //$('#vu'+classid).removeClass().addClass('vu');
                //                        $(_this).parent().children('.vu2').removeClass().addClass('vu');  
                //                       // $('.vu2').removeClass().addClass('vu'); 
                //                        
                //                       
                //                       if($('#profile').val()!=undefined && $('#profile').val()!=null)
                //                       {
                //                          window.location.reload(true);
                //                       }
                //                         window.location.reload(true);                             
                //                                                                    
                //                    }
                else { //alert('notok');
                    $(_this).unbind();
                    $(_this).removeClass().addClass('vd2');
                    alert('Error occurs while voting. Please try again later!');
                }
                //                }  
            });
        }
    }

    //vote up
    $('.vu').click(function() {

        var _this = this;
        return ClickEvent(this);

    });


    //vote down
    $('.vd').click(function() {
        var _this = this;

        return ClickEventDown(this);

    });

    
    

});


function deleteTag(tag_id){
    $.post("/users/tag_del.php", {id: tag_id}, 
        function(data){                                				                
            $('#loading1').hide();
            if(data != 'OK')
            {
                alert(data);
            }   
            else
            {                                                  
                window.location.reload(true);                        
            }                        
    });
}


function deleteComment(comment_id){
    $.post("/users/comment_del.php", {id: comment_id}, 
        function(data){                                				                
            $('#loading1').hide();
            if(data != 'OK')
            {
                alert(data);
            }   
            else
            {                                                  
                window.location.reload(true);                        
            }                        
    });
}
 $('#frmsharelink').click(
        function()
        {                   
            $('#loading1').show(); 
                      
                      
            var share_value = $('[name=admshare]:checked').val();
           if(share_value == undefined) share_value = "0"; else share_value = "1";
                     
            $.post("/share_post.php", {admemail1: $('#admemail1').val(), admemail2: $('#admemail2').val(), admemail3: $('#admemail3').val(), admemail4: $('#admemail4').val(), admshare: share_value, admid: $('#admid').val(),admmessage: $('#admmessage').val() }, 
                function(data){                                				                
                    $('#loading1').hide();
                    if(data != 'OK')
                    {
                        alert(data);
                    }   
                    else
                    {
                        alert('Link has been successfully shared!');
                        tb_remove();
                        //window.location.reload(true);
                    }                        
            });
            
			return false;
        }
    );    
    
 
 function deleteLink(id,elem){
    $.post("/users/delete_link.php", {id: id,action:"delete"}, 
        function(data){                                				                
            $('#loading1').hide();
            if(data != 'OK')
            {
                alert(data);
                 window.location.reload(true);  
                //$(elem).parent().parent().parent().hide();
                //$('#delete_link').parent().parent().hide();
            }   
            else
            {       
            tb_remove();
            window.location.reload(true);                                             
            }                        
    });
}
 function DeleteMessage(id){
    $.post("/users/delete_message.php", {id: id}, 
        function(data){                                				                
            $('#loading1').hide();
            if(data != 'OK')
            {
                alert(data);
                 window.location.reload(true);  
                //$(elem).parent().parent().parent().hide();
                //$('#delete_link').parent().parent().hide();
            }   
            else
            {       
            tb_remove();
            window.location.reload(true);                                             
            }                        
    });
}


function restoreLink(id,elem){
    $.post("/users/delete_link.php", {id: id,action:"restore"}, 
        function(data){                                				                
            $('#loading1').hide();
            if(data != 'OK')
            {
                alert(data);
                window.location.reload(true);  
                //$(elem).parent().parent().parent().hide();
                //$('#delete_link').parent().parent().hide();
            }   
            else
            {                                                  
                  tb_remove();
                  window.location.reload(true);                    
            }                        
    });
}

function rankUp(elem,page){

        var _this = elem; 
        var id = $(_this).attr('id'); 
        var  notExist =" - Promote on first page";
         
        
		 //get keyword for ranking
            var keyword = $('#q').val();
            keyword = keyword.replace(/&/g,'___');
            keyword = keyword.replace(/\n/g,'$=====$');
            
    $.post("/users/rank_up.php", {id: id ,keyword:encodeURI(keyword),action : 'up'}, 
        function(data){                                         				                                    
            if(data != "NOK")
            {     
                 //only on page=1 add class for rank down and change class for ranked urls
                 if(page == 1)
                 {
                     $(_this).removeClass().addClass('rank_up_rank'); 
                     if(!$(_this).next().hasClass("rank_down"))
                     $(_this).after("<a href='#' class='rank_down'  onclick='rankDown(this," + page +")' alt='Rank' id="+ id + " title='Rank'></a>");
	             }	
		 
		     
                    //return data as array -
                    // new_url(url after which the current url have to be show)
                    //rank for the current url       
                    var exit = eval(data);   
                    // class for the current url            
                    var strEval = "'.row_" + id + "'";                  
                    //class for the url where before or after the current url should be entered
                    var firstEval= "'.row_" + exit[0] + "'";
                  
                    //get content from the div that has to be moved  (content for the current url)                  
                    var newhtml="<div class='row_"+id+"' >";
                    newhtml+=$(eval(strEval)).html();
                    newhtml+="</div>";              
                       
                   
                       //on page > 1 to add only "promote on first page"
                       if(page > 1)
                       {
                         $(eval(strEval)).slideUp(3000); 
                            $(eval(strEval)).delay(3000,function(){

                                  var text = $(eval(strEval)).children('.row').children('.leftcol').children('.p').children('.link').html()+ notExist;
                                   $(eval(strEval)).empty();
                                     $(eval(strEval)).html("<br>"+text+"<br><br>");
                                     $(eval(strEval)).slideDown(3000);
                             }); 
                       
                       }
                       else
                       {
                                //exit[0] - new url
                               if(exit[0] != 0)
                               {
                                        //current url slide up
                                       $(eval(strEval)).slideUp(3000);                        
                                       
                                       //delay function - stop for finishing slide up on current url
                                       $(eval(strEval)).delay(3000,function(){
                                       
                                                        //clear content on the current url
                                                       $(eval(strEval)).empty();
                                                       //remove class row_#id# - (extra for no repeating on div with the same class row_#id#
                                                       $(eval(strEval)).removeClass();
                                                        
                                                        //if new location exist on this page
                                                       if( $(eval(firstEval)).html() != null)
                                                       {
                                                            //alert(exit[1]);
                                                           //exit[1] - rank on current url  == 0(this is case where there is no any ranking yet)
                                                           if(exit[1] == 0)
                                                                $(eval(firstEval)).after(newhtml);
                                                            else
                                                                $(eval(firstEval)).before(newhtml);                                                         
                                                            
                                                            //firt hide content on the new location and after that slide down
                                                            $(eval(strEval)).hide();
                                                            $(eval(strEval)).slideDown(3000);  
                                                            
                                                            
                                                            //sroll the window to the new place
                                                             $.scrollTo(  $(eval(strEval)), 1000,{offset:{top:-115}});     
                                                            
                                                       }
                                                       else
                                                       {
                                                             //if new location not exist on this page then add current content on temporaly div last_row
                                                              var lastdiv= "'.last_row'";
                                                               var current_html = $(eval(lastdiv)).html();
                                                               var newtext =  newhtml + current_html;
                                                              
                                                              $(eval(lastdiv)).html(newtext);
                                                               $(eval(lastdiv)).children(eval(strEval)).hide();
                                                                 $(eval(lastdiv)).children(eval(strEval)).slideDown(3000); 
                                                                 
                                                                  //sroll the window to the new place
                                                                  $.scrollTo(  $(eval(lastdiv)), 1000,{offset:{top:-115}});       
                                                       }
                                        
                                          
                                     

                                        });
                                        
                                           $.scrollTo(  $(eval(strEval)), 1000,{offset:{top:-115}});     
                                       
                                }
                                else
                                {
                                     //first time ranking, there is no ranking yet
                                     $(eval(strEval)).slideUp(3000);                        
                                       
                                       //delay function - stop for finishing slide up on current url
                                       $(eval(strEval)).delay(3000,function(){
                                       
                                                        //clear content on the current url
                                                       $(eval(strEval)).empty();
                                                       //remove class row_#id# - (extra for no repeating on div with the same class row_#id#
                                                       $(eval(strEval)).removeClass();
                                                       
                                                        var firstdiv= "'.first_row'";
                                                               var current_html = $(eval(firstdiv)).html();
                                                               var newtext =  newhtml + current_html;
                                                              
                                                              $(eval(firstdiv)).html(newtext);
                                                               $(eval(firstdiv)).children(eval(strEval)).hide();
                                                                 $(eval(firstdiv)).children(eval(strEval)).slideDown(3000); 
                                                                 //scroll window to the firstdiv-new location                                                                 
                                                                 $.scrollTo(  $(eval(firstdiv)), 1000,{offset:{top:-115}});    
                                                       
                                      });
                                      $.scrollTo(  $(eval(strEval)), 1000,{offset:{top:-115}});       

                                }
                        }
                
            }
            else
            {
                alert("You must Login or Sign Up to make your rank!!!");
            }   
                                   
    });
}

function rankDown(elem,page){

        var _this = elem; 
         var id = $(_this).attr('id');
         
         //if page == 1 then 
          if(page == 1)
         {
            //remove image for rank down
             $(_this).removeClass();
            //change not rank image with ranked image 
             if($(_this).prev().hasClass("rank_up_rank"))
                $(_this).prev().removeClass().addClass("rank_up");
               $(_this).remove();
		 }		
       
        
      
          //get keyword for ranking
            var keyword = $('#q').val();
            keyword = keyword.replace(/&/g,'___');
            keyword = keyword.replace(/\n/g,'$=====$');
            
    $.post("/users/rank_up.php", {id: id ,keyword:encodeURI(keyword),action : 'down'}, 
        function(data){                                         				                                    
            if(data != "undefined")
            {   
            
                 //return data as array -
                // new_url(url after which the current url have to be show)
                //rank for the current url     
            
                var strEval = "'#row_" + id +"'";
                var exit = eval(data);
              //  alert(exit[0]);
                // class for the current url  
                var strEval = "'.row_" + id + "'";
                //class for the url where before or after the current url should be entered
                 var firstEval= "'.row_" + exit[0] + "'";
                 
                //get content from the div that has to be moved  (content for the current url)   
                var newhtml="<div class='row_"+id+"' >";
                     newhtml+=$(eval(strEval)).html();
                     newhtml+="</div>";                   
                      
                      
                     //only on first page we have rank down
                      if(page == 1)
                       {
                                 //current url slide up
                               $(eval(strEval)).slideUp(3000);                        
                               
                                 //delay function - stop for finishing slide up on current url                                     
                               $(eval(strEval)).delay(3000,function(){

                                                //clear content on the current url
                                               $(eval(strEval)).empty();
                                               //remove class row_#id# - (extra for no repeating on div with the same class row_#id#
                                               $(eval(strEval)).removeClass();
                                               
                                                 //if new location exist on this page
                                              if( $(eval(firstEval)).html() != null)
                                              {
                                                 //add content on new location after the returned new url_id
                                                          
                                                $(eval(firstEval)).after(newhtml);                                                
                                                $(eval(strEval)).hide();
                                                $(eval(strEval)).slideDown(3000); 
                                                //sroll the window to the new place
                                                 $.scrollTo(  $(eval(strEval)), 1000,{offset:{top:-115}});      
                                              } 
                                              else
                                              {
                                              //if new location not exist on this page
                                              //add content as last in the temporaly div last_row
                                               var lastdiv= "'.last_row'";
                                               var current_html = $(eval(lastdiv)).html();
                                               var newtext = current_html + newhtml;
                                             //from the list of elemrnts in div last_row first hide on new element that we add,then slide down
                                              $(eval(lastdiv)).html(newtext);
                                               $(eval(lastdiv)).children(eval(strEval)).hide();
                                                 $(eval(lastdiv)).children(eval(strEval)).slideDown(3000); 
                                                 //sroll the window to the new place
                                                 $.scrollTo(  $(eval(lastdiv)), 1000,{offset:{top:-115}}); 
                                              }         
                                 
                             

                                });
                                //sroll the window to the new place
                                 $.scrollTo(  $(eval(strEval)), 1000,{offset:{top:-115}}); 
                        }
                
            }   
                                   
    });
    
    
}
  $('#edit_sms_countries').click(function(){        
            $('#loading1').show();           
                    
            $.post("/post_ipstates.php",{id :$("#id").val(),user_id: $("#user_id").val(), country: $("#editcountry option:selected").val(), state: $('#editstate option:selected').val(), city: $('#editcity option:selected').val(),action: "update"}, 
                function(data){ 
                $('#loading1').hide();
                    if(data != 'OK')
                    {
                        alert(data);
                    }   
                    else
                    {
                        alert('Record has been succesfully updated!');
                        tb_remove();
                        window.location.href = "/users/messagecountries"; 
                    }                                  				                
                                  
            });            
			return false;


});

$('#edit_message_countries').click(function() {
    $('#loading1').show();

    $.post("/post_ipstates.php", { id: $("#id").val(), user_id: $("#user_id").val(), country: $("#editcountry option:selected").val(), state: $('#editstate option:selected').val(), city: $('#editcity option:selected').val(), action: "messageupdate" },
                function(data) {
                    $('#loading1').hide();
                    if (data != 'OK') {
                        alert(data);
                    }
                    else {
                        alert('Record has been succesfully updated!');
                        tb_remove();
                        window.location.href = "/users/messagecountries";
                    }

                });
    return false;

});

function checkHistory(lastactid,divobj,action) {
    var _divobj = divobj;
    var _lastactid = lastactid;
    var last_activity_id = $(_lastactid).val();
    $.post("/users/check_history_new_updates.php", { id: last_activity_id, action: action },
         function(data) {
             var exit = eval(data);

             if (exit[0] > 0) {
                 $(_divobj).html("<a href='/history/" + action + "' caption='refresh' >" + exit[0] + " more activities since you started searching.</a>");
                 $(_divobj).removeClass().addClass("activity_notification");
             }
             else {

                 $(_divobj).removeClass().addClass("activity_block");
                
             }
             //$(_lastactid).val(exit[1]);
         });
         
         
         return false;
     }    