﻿$(document).ready(function(){
 //start fuctions(onload)//

    //Binders
    m.WallRebind();
    m.VoteRebind();

    //nav
    $(".nav_drop").click(function(){

        var strHref = this.href
        var arrH = strHref.split("#"); 
        var strRefK = arrH[1]
    
        var strCss = $(this).attr("class");
        var arrS = strCss.split(" "); 
        var strSel = arrS[1]
        if (strSel == "sel")
        {
            $(this).removeClass("sel");
            $(".v_" + strRefK +" ul").hide();
        }
        else
        {
            $(this).addClass("sel");
            $(".v_" + strRefK +" ul").show();
        }

    return false;
    });
    
    //Show question box    
    $("a#ask_question").click(function(){
        $(".submit_box").show(); 
        $(this).hide(); 
    return false;
    });
    
     //Tabs on the detail page
      $("#detail_tabs a").click(function(){
            var strTabId = this.title;

            $(".tab_content").hide();
            $("#" + strTabId).show();
            $("#detail_tabs a").removeClass("current");
            $(this).addClass("current");
        return false;
     });


     //calls ajax page DELETE
     $("a.ax_logout").click(function(){
        var url = this.href;
            $.get(url, function(data){
            $("#login_wrapper").fadeOut("fast");
            window.location = "default.aspx";
        });
        //$(this).hide();
        //$(".del_friends").after($(this).parent());
        return false;
     });
    
    //Messages
    $("#messageForm a#send").click(function(){ m.SendMessage($("#messageForm #mTo").val(), $("#messageForm #mSubject").val(),$("#messageForm #mMessage").val()); $("#messageW").slideUp("fast"); $(".user").show(); $("#messages_table").show(); return false; });
    $(".ping").click(function(){ var strId = this.href; var arrS = strId.split("#"); m.SendMessage(arrS[1], "Ping od " + arrS[2], "" + arrS[3]); $(this).fadeOut("slow"); return false; });
    $("#messageForm a#cancel").click(function(){$("#messageW").slideUp("fast"); $("#messages_table").show(); return false; });

     $(".SendMessage").click(function(){
        $("#messageW").slideDown("slow");
		$(".user").hide();
        var strTo = this.title;
        $("#mTo").val("");
        $("#mSubject").val("");
        $("#mMessage").val("");
        $("#mTo").val(strTo);
        return false;
     });

    $(".del_message").click(function(){
        var strHref = this.href;
        var arrS = strHref.split("#"); 
        $("#l" + arrS[1]).addClass("alpha");
        var delMsg = m.deleteMessage(arrS[1]);
        if (delMsg == "yes")
        {$("#l" + arrS[1]).hide();}
        else
        {$("#l" + arrS[1]).removeClass("alpha");}
        return false;
    });

     //Tabs on the detail page
      $("#detail_tabs a").click(function(){
            var strTabId = this.title;
            $(".tab_content").hide();
            $("#" + strTabId).show();
            $("#detail_tabs a").removeClass("current");
            $(this).addClass("current");
        return false;
     });
     
    //show message
    $("#show_message1").click(function(){
        $(this).hide();
        $("#message1_body").slideDown("fast");
        return false;
    });

    // Load messages body
    $(".m_l").hover(function(){
        $(this).children('td').addClass("msg_hover");
    },function(){
        $(this).children('td').removeClass("msg_hover");
    });

    // Load messages body
    $(".m_l").click(function(){
        var strMT
        var strId = $(this).attr("id");
        strId = strId.slice(1);
        //alert(strId)
        var strClass = m.Right($(this).attr("class"), 4);
        
        //creating string for updating message# session
        if (strClass == " new")
        {
            var url = "ajax_get.aspx?m=GetMessageBody&p=" + strId + "&p2=new"
        }
        else
        {
            var url = "ajax_get.aspx?m=GetMessageBody&p=" + strId + "&p2=old"
        }
        

        //alert(strClass);
        if (strClass == "open")
        {
            $("#ax_tr_" + strId).hide();
            $(this).removeClass("open");
        }
        else
        {
            //loader image
            $("#l" + strId + " .status").empty();
            $("#l" + strId + " .status").append("<img src='images/progress.gif' alt='progress' />");
            
            $(this).addClass("read");
            $(this).addClass("open");
            $.get(url, function(data){
            if(data.toString() == "chyba!")
            {
                alert(data);
            }
            else
            {
                $("#l" + strId).after("<tr id='ax_tr_" + strId +"' class='ex_tr'><td colspan='5'>" + data + "</td></tr>");
                $("#l" + strId + " .status").empty();
            }
            });    
        }
        $(this).removeClass("m_nr");
        return false;
    });
    
    $(".foto_link").hover(function(){
        //var strDesc = $(this).attr("title");
        var strId = $(this).attr("id");
        strId = strId.slice(1);
        $("#i" + strId + " span").fadeIn("fast");

    },function(){
        var strId = $(this).attr("id");
        strId = strId.slice(1);
        //alert(strId);
        $("#i" + strId + " span").fadeOut("fast");
    });
    
    
    //inline editing
      $(".edit").click(function(){
            var strId = $(this).attr("id");
            var strfCode = strId.slice(0, 2);
            strId = strId.slice(3);
            var strEditText = $(this).text();
            $(this).after("<span id='w" + strId + "' class='inEditW'><input class='edit' id='i" + strId + "' type='text' value='" + strEditText + "' /><a onclick='m.postEdit(" + strId + ", " + strfCode + ");' class='editSave'>hotovo</a></span>")
            $(this).hide()
        return false;
     });
     
    $(".edit").hover(function(){
        $(this).addClass("edit_hover");
    },function(){
        $(this).removeClass("edit_hover");
    });
     
    $(".edit_TA").click(function(){
        var strId = $(this).attr("id");
        var strfCode = strId.slice(0, 2);
        strId = strId.slice(3);
        var strEditText = $(this).text();
        $(this).after("<span id='w" + strId + "' class='inEditW'><textarea class='edit' id='i" + strId + "' type='text'>" + strEditText + "</textarea><a onclick='m.postEdit(" + strId + ", " + strfCode + ");' class='editSave'>hotovo</a></span>")
        $(this).hide()
        return false;
    });
    
    $(".edit_TA").hover(function(){
        $(this).addClass("edit_TA_hover");
    },function(){
        $(this).removeClass("edit_TA_hover");
    });
    
    $(".DeleteImage").click(function(){
        var strId = $(this).attr("id");
        strId = strId.slice(1);
        var strHref = this.href
        var arrS = strHref.split("#"); 
        strHref = arrS[1]
        $("#o" + strId).addClass("alpha");
        var r=confirm("smazat?");
        if (r==true)
        { m.DeleteImage(strId, strHref); $("#o" + strId).fadeOut("fast"); }
        else{$("#o" + strId).removeClass("alpha");}
        return false;
    });
        $(".del_friend").click(function(){
            var strHref = this.href
            var arrS = strHref.split("#"); 
            $("#u-" + arrS[1]).addClass("alpha");
            var r=confirm("smazat?");
            if (r==true)
            {
                m.GPost("delete_friend", arrS[1]);
                $("#u-" + arrS[1]).fadeOut("fast");
            }
            else
            {$("#u-" + arrS[1]).removeClass("alpha");}
        return false;
    });
    
    
    $(".access_change a").click(function(){
        
        var strHref = this.href
        var arrS = strHref.split("#"); 
        var strId = arrS[1]
        
        m.ChangePhotoAcess(arrS[2], strId);
        $("#chid_" + strId).hide();
        $("#chid_" + strId).empty();
        if (arrS[2] == "public")
        {   
            $("#chid_" + strId).append("<strong>Hotovo!</strong>").removeClass("icon_privat").addClass("icon_public");
        }
        else
        {$("#chid_" + strId).append("<strong>Hotovo!</strong>").addClass("icon_privat").removeClass("icon_public");}
        $("#chid_" + strId).fadeIn("fast");
    return false;
    });

    //pw retrive
    $("#sendPw").click(function(){$("#send_mail_wrp").fadeIn();return false;});

    $("#send_mail_wrp a").click(function(){
        m.ReSendPw($("#send_mail_wrp input").val());
        return false;
    });
    
    $("#join_school").click(function(){
        var strHref = this.href
        var arrS = strHref.split("#"); 
        m.GPost("join_school", arrS[1]);
        return false;
    });

    $(".add_wp").click(function(){
        var strHref = this.href
        var arrS = strHref.split("#"); 
        m.GPost("addWebPart", arrS[1], arrS[2]);
        $("#wp_l_" + arrS[1]).fadeOut("fast");
        return false;
    });

    $("#inv_friend .button").click(function(){
        m.GPost("InviteFriend", $("#inv_name").val(), $("#inv_email").val(), $("#inv_message").val());
        $("#inv_friend").empty();
        $("#inv_friend").append("<div class='message_call'>poslal jsem email...</div>");
        $("#inv_friend").fadeIn("fast");
    return false;
    });

     //populate web part#
     $(".web_part a.wp_cx_fr").click(function(){
         var strCssClass = $(this).attr("class");
         var url = this.href;
         var arrS = url.split("#");
         if (strCssClass == "wp_cx_fr wp_stc_0")
         {
            m.PopulateWp("ajax_web_part.aspx?" + arrS[1], arrS[2], arrS[3])
            $(this).removeClass("wp_stc_0");
            $(this).addClass("wp_stc_1");
            return false;
         }
         else
         {
            $("#wp-" + arrS[2] + " .wp_body").hide();
            $(this).removeClass("wp_stc_1");
            $(this).addClass("wp_stc_0");
         }
         return false;
     });
     
     $(".link_com a").click(function(){
         var url = this.href;
         var arrS = url.split("#");
         m.GPost("PostLinkComment", $("#link_com_wrp_" + arrS[1] + " textarea").val(), arrS[1]); 
         $("#link_com_wrp_" + arrS[1]).fadeOut("fast");
         $("#link_com_wrp_" + arrS[1] + " textarea").val("");
         return false;
     });
     
    $(".show_com").click(function(){
        var url = this.href;
        var arrS = url.split("#");
        $("#link_com_wrp_" + arrS[1]).fadeIn("fast");
        return false;
    });
     
  
     
//     $(".share a").click(function(){
//         var url = this.href;
//         var arrS = url.split("#");
//         
//         $.get("ajax_get.aspx?m=GetShareBox&p=" + arrS[1], function(data){
//            $("#SharePhoto_" + arrS[1]).append(data);
//        });
//        
//         return false;
//     });
     


//end fuctions//
//**********************************************
});



// Class Library
//**********************************
//**********************************
function UI(id) {this._id = id;}
UI.prototype._id;
UI.prototype.getName = function() {return this._n;}
var m = new UI();

UI.prototype.Right = function(str, n){
    //trims right
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

UI.prototype.Left = function(str, n){
    //trims left
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else
        return String(str).substring(0,n);
}

UI.prototype.reply = function(strTo){
    //set link
    $("#messageW").slideDown("fast");
    $("#messages_table").hide();
    
    //clears form
    $("#mTo").val("");
    $("#mSubject").val("");
    $("#mMessage").val("");
    //pouplatest TO        
    $("#mTo").val(strTo);
}

UI.prototype.InsertLog = function(strLink, strHeader, strText, strImgLing){
    $.post("ajax_post.aspx",
    { m: "InsertLog", 
    p: strLink, 
    p2: strHeader, 
    p3: strText, 
    p4: strImgLing}, 
    function(data){
            m.AlertMessage(data);
        }
    );
 return false;
}

UI.prototype.ChangeWpSt = function(strWpRId, strChTo, strPageId){
            $.post("ajax_post.aspx",
            { m: "ChangeWpSt", 
            p: strWpRId, 
            p2: strChTo,
            p3: strPageId}, 
            function(data){
                //m.AlertMessage(data);
        }
    );
 return false;
}

UI.prototype.DeleteFolder = function(strFlId){
            $.post("ajax_post.aspx",
            { m: "DeleteFolder", 
            p: strFlId}, 
            function(data){
                m.AlertMessage(data);
        }
    );
 return false;
}


UI.prototype.ReSendPw = function(strEmail){
            $.post("ajax_post.aspx",
            { m: "ReSendPw", 
            p: strEmail}, 
            function(data){
                m.AlertMessage(data);
                if (data == "poslal ti jsem helso!")
                {
                    $("#send_mail_wrp").fadeOut();
                }
        }
    );
 return false;
}


UI.prototype.InserIntoFolder = function(strImgId, strAlbId){

            $.post("ajax_post.aspx",
            { m: "InserIntoFolder", 
            p: strImgId, 
            p2: strAlbId}, 
            function(data){
                m.AlertMessage(data);
        }
    );
 return false;
}

UI.prototype.DeleteFromFolder = function(strImgId, strAlbId){

            $.post("ajax_post.aspx",
            { m: "DeleteFromFolder", 
            p: strImgId, 
            p2: strAlbId}, 
            function(data){
                m.AlertMessage(data);
        }
    );
 return false;
}

UI.prototype.approveFriend = function(strId){

            $.post("ajax_post.aspx",
            { m: "ApproveFriend", 
            p: strId}, 
            function(data){
                m.AlertMessage(data);
        }
    );
 return false;
}

UI.prototype.AddFriend = function(strUserName){

            $.post("ajax_post.aspx",
            { m: "AddFriend", 
            p: strUserName}, 
            function(data){
                $("#i-" + strUserName).fadeOut("slow");
                m.AlertMessage(data);
        }
    );
 return false;
}

UI.prototype.ChangePhotoAcess = function(strChTo, strId){
            $.post("ajax_post.aspx",
            { m: "ChangePhotoAcess", 
            p: strChTo, 
            p2: strId}, 
            function(data){
                m.AlertMessage(data);
        }
    );
 return false;
}

UI.prototype.JoinGroup = function(GroupId, gAccess, gAdmin, gName){

            $.post("ajax_post.aspx",
            { m: "joinGroup", 
            p: GroupId, 
            p2: gAccess, 
            p3: gAdmin, 
            p4: gName}, 
            function(data){
                m.AlertMessage(data);
        }
    );
 return false;
}
UI.prototype.JoinCity = function(CityId){

            $.post("ajax_post.aspx",
            { m: "JoinCity", 
            p: CityId}, 
            function(data){
                m.AlertMessage(data);
        }
    );
 return false;
}

UI.prototype.GPost = function(strMethod, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10){
            $.post("ajax_post.aspx",
            { m: strMethod, p: p1, p2: p2, p3: p3, p4: p4, p5: p5, p6: p6, p7: p7, p8: p8, p9: p9, p10: p10}, 
            function(data){m.AlertMessage(data);}
    );
 return false;
}

UI.prototype.AGM = function(GroupId, userId){

            $.post("ajax_post.aspx",
            { m: "AGM", 
            p: GroupId, 
            p2: userId}, 
            function(data){
                m.AlertMessage(data);
        }
    );
 return false;
}

UI.prototype.postEdit = function(intid, intTId){
    var strChTX = $("#i" + intid).val();
    $.post("ajax_post.aspx",
    { m: "UpdateField", 
        p: intid, 
        p2: strChTX,
        p3: intTId}, 
        function(data){
            m.AlertMessage(data);
        }
    );
    
    $("#w" + intid).remove();
    $("#" + intTId + "-" + intid).empty();
    $("#" + intTId + "-" + intid).append(strChTX);
    $("#" + intTId + "-" + intid).show();

    
 return false;
}

UI.prototype.WriteToWall = function(refId, strMessage, strType){

            $.post("ajax_post.aspx",
            { m: "WriteToWall", 
            p: refId,
            p2: strMessage,
            p3: strType}, 
            function(data){
                m.AlertMessage(data);
        }
    );
 return false;
}

UI.prototype.DeleteImage = function(strImgId, strImgPath){
            $.post("ajax_post.aspx",
            { m: "DeleteImage", 
            p: strImgId,
            p2: strImgPath}, 
            function(data){
                m.AlertMessage(data);
        }
    );
 return false;
}

UI.prototype.SendMessage = function(strSendTo, strSubject, strMessage){

    $.post("ajax_post.aspx",
        {   m: "SendMessage", 
            p: strSendTo, 
            p2: strSubject,
            p3: strMessage}, 
                function(data){
                    m.AlertMessage(data);
                }
    );       
 return false;
}

UI.prototype.deleteMessage = function(strId){
//delete confirmation
var r=confirm("smazat?");
if (r==true)
    {
        //loader image
        $("#l" + strId + " .status").empty();
        $("#l" + strId + " .status").append("<img src='images/progress.gif' alt='progress' />");
        $("#ax_tr_" + strId).hide();
        $("#l" + strId).hide();
        var url = "ajax_get.aspx?m=DeleteMessage&p=" + strId
        $.get(url, function(data){
            $("#l" + strId + " .status").empty();
            m.AlertMessage(data);
        });
        return "yes";
    }
    else
    {return "no";}
    
}

UI.prototype.AlertMessage = function(strMessage){
    //displays message
    if (strMessage == "chyba!")
    {
        $("#right_col").prepend("<div class='message_call' style='position:absolute;'><p><strong>" + strMessage + "</strong></p>  </div>");
    }
    else
    {
        $("#st").empty()
        var strMessageHTML
        strMessageHTML = "<p><strong>" + strMessage + "</strong></p>"
        $("#st").append(strMessageHTML);
        $("#st").fadeIn("slow");
        $("#st").fadeOut("slow");
    }
}

UI.prototype.Loader = function(WPId, PWState, p1, p2, p3, p4, p5, pgId){
    if (PWState == 1)
    {
        $.get("ajax_web_part.aspx?i=" + WPId + "&s=" + PWState + "&p1=" + p1 + "&p2=" + p2 + "&p3=" + p3 + "&p4=" + p4 + "&p5=" + p5 + "&pi=" + pgId, function(data){
            $("#wp-" + WPId + " .wp_body").append(data);
            $("#wp-" + WPId + " .progress").hide()
            if(WPId == 8){wp.UpdtSchId();} //rebind schid clk
            if(WPId == 10){m.WallRebind();}//rebind wall click clk
            if(WPId == 7){m.UpdtRSSLink();} //rebind RSS linker
            if(WPId == 15){m.UpdtMp3Link();}//rebind media clk
            if(WPId == 3){m.VoteRebind();}//rebind log voter
            if(WPId == 14){m.VoteRebind();}//rebind log voter
            //if(WPId == 5){m.VoteRebind();}//rebind log voter
            
        });
    }
    else
    {$("#wp-" + WPId + " .progress").hide();}
    
 return false;
}

UI.prototype.PopulateWp = function(url, strId1, strId2){
    $("#wp-" + strId1 + " .progress").show();
    $("#wp-" + strId1 + " .wp_body").empty();
    $.get(url, function(data){
        $("#wp-" + strId1 + " .wp_body").show();
        $("#wp-" + strId1 + " .wp_body").append(data);
        $("#wp-" + strId1 + " .progress").hide();
        
    });
   return false;
}

UI.prototype.VoteRebind = function(){
    $(".vote").click(function(){
        var strHref = this.href
        var arrS = strHref.split("#"); 
        var intNumb = parseInt(arrS[2]);
		if(arrS[1] == "plus")
		{
			if ($("#v_vrp_" + arrS[3] + " .v1").text() == arrS[2])
			{
			    m.GPost(arrS[4], arrS[1], intNumb, arrS[3]);
			    $("#v_vrp_" + arrS[3] + " .v1").addClass("alpha");
			    $("#dg_vrp_" + arrS[3] + "").addClass("alpha");
			}
			else
			{alert("co co?")}
			    $("#v_vrp_" + arrS[3] + " .v1").empty(); 
			    $("#dg_vrp_" + arrS[3] + " .dg_v1").empty();
			if (arrS[2] == 0)
			{$("#v_vrp_" + arrS[3] + " .v1").append(1);}
			else
			{   
			    $("#v_vrp_" + arrS[3] + " .v1").append(intNumb + 1); 
			    $("#dg_vrp_" + arrS[3] + " .dg_v1").append(intNumb + 1); 
			}
		}
		else
		{
			if ($("#v_vrp_" + arrS[3] + " .v2").text() == intNumb)
			{
			    m.GPost(arrS[4], arrS[1], intNumb, arrS[3]);
			    $("#v_vrp_" + arrS[3] + " .v2").addClass("alpha");
			}
			else
			{alert("co co?")}
			$("#v_vrp_" + arrS[3] + " .v2").empty();
			if (intNumb == 0)
			{$("#v_vrp_" + arrS[3] + " .v2").append(1);}
			else
			{$("#v_vrp_" + arrS[3] + " .v2").append(intNumb + 1);}
		}
        return false;
    });
}

UI.prototype.WallRebind = function(){
//send wall User message
    $("#wallMessageBox .sendToWall").click(function(){
        var strId = this.href
        var arrS = strId.split("#"); 
        strId = arrS[1]
        var strBtnId = $(this).attr("id");
        var strWallType = ""
        
        //wall type selector
        if (strBtnId == "btn_")
        {
            strWallType = "user";
        }
        if  (strBtnId == "btn_group")
        {
            strWallType = "group";
        }
        if (strBtnId == "btn_image")
        {
            strWallType = "image";
        }
        
        //zero value checker
        if ($("#WallMessage").val() == "")
        {
            m.AlertMessage("co?");
        }
        
        else
        {
            m.WriteToWall(strId, $("#WallMessage").val(), strWallType);
            $("#wallMessageBox").fadeOut("slow");
            $("#WallMessage").val("");
        }
        return false;
    });
}

UI.prototype.UpdtRSSLink = function(){
    $("#RSS_link_wrp a").click(function(){
        var strVal = $("#RSS_link_wrp input").val()
        m.GPost("UpdtRSSLink", strVal);
        $("#wp-7 .wp_body").empty();
        wp.PopulateWp("ajax_web_part.aspx?i=7&s=1&p1=5&p3=" + strVal, 7, 1);
        return false;
    });
}

UI.prototype.UpdtMp3Link = function(){
    $("#media_link_wrp a").click(function(){
        var strVal = $("#media_link_wrp input").val()
        var strMediaType = ""
        var arrS = strVal.split("."); 
        
        if(arrS[arrS.length - 1] == "mp3")
        {strMediaType = "audio"}
        else
        {strMediaType = "video"}
        
        m.GPost("UpdtMediaLink", strVal, strMediaType);
        $("#wp-15 .wp_body").empty();
        wp.PopulateWp("ajax_web_part.aspx?i=15&s=1&p1=" + strVal + "&p3=" + strMediaType, 15, 1);
        return false;
    });
}

UI.prototype.RebindsShare = function(){
    
    $(document).ready(function(){
        $(".m_popup_wrp fieldset").prepend("<img class='thumb_image' src='img/?f=" + strPhShUrl + "&amp;w=220' alt='fotka' />");
        $("#share_send").click(function(){
            if($("#share_email").val() == "")
            {
                alert("email?");
            }
            else
            {
                $(".m_popup_wrp").hide();
                $("#share_pop_wrp").append("<img src='images/loader2.gif' alt='loading' />");
                $.post("ajax_post.aspx",
                    { m: "SharePhoto", 
                    p: $("#share_name").val(), 
                    p2: $("#share_email").val(),
                    p3: strPhShId, 
                    p4: $("#share_msg").val(), 
                    p5: strPhShUrl}, 
                    function(data){
                       $("#share_pop_wrp img").hide();
                       $("#share_pop_wrp").append("<p class='p_msg'><strong>" + data + "</strong></p>");
                });  
            }
        return false;
        });
    });
}

