$(document).ready(function(){





    $("#send").click(function(){

        $("#window .window_msg").html('<p class="c"><img src="/images/upload.gif" align="absmiddle"> Загрузка</p>');
        $("#window").css({
            display:''
        });
        var displaysize = getPageSize();
        $("#overlay").css({
            display:'',
            width:displaysize[0]
        });
        var username = $("#form_comment input[name=username]").attr("value");
        var usermail = $("#form_comment input[name=usermail]").attr("value");
        var cpt = $("#form_comment input[name=cpt]").attr("value");
        var msg = $("#form_comment textarea[name=msg]").val();
        var id = $("#form_comment input[name=id]").attr("value");

        $.post(
            "/ajax/addcomment/",
            {
                c_id:id,
                c_username:username,
                c_msg:msg,
                c_usermail:usermail,
                c_cpt:cpt
            },
            onAjaxSuccess
            );
        return false;
    });


    $("#window_close").click(function(){

        $("#window").css({
            display:'none'
        });
        $("#overlay").css({
            display:'none'
        });
        return false
    });

	


    $("table.matr tr:nth-child(odd)").css({
        'background':'#f8f8f8'
    });


    function onAjaxSuccess(xml)
    {
        var err = $("error",xml).text();

        if (!err)
        {
            $("#form_comment").find("textarea").val('').end().find("input[name=cpt]").attr("value","");
            var count = $("xml count",xml).text();
            var list = '<p class="ht">Комментарии (' + count + ')</p>';
            $("xml comments item",xml).each( function(){

                var msg = $("msg",this).text();
                var username = $("username",this).text();
                var pubdate = $("pubdate",this).text();
                var admin = $("class",this).text();
                list += '<div class="comment_id"><p><span class="username' + admin + '">' + username + '</span> написал(а) <span class="pubdate">' + pubdate + '</span>:</p><p>' + msg + '</p></div>'
            });
            $("#comments").html(list);

            $("#window").css({
                display:'none'
            });
            $("#overlay").css({
                display:'none'
            });

        }else{
            $("#window .window_msg").html('<p class="ht">Вы заполнили не все поля:</p><p>'+err+'</p>');
        }
    }


    /*LeftMenu*/
    $(".lmenu").hover(
        function(){
            $(this).css({
                background:'#f5f5f5'
            });
        },
        function(){
            $(this).css({
                background:'none'
            });
        }
        );

    $(".lmenu").click(function(){
        var url = $(this).find("a").attr("href");
        if (url)
            window.location = url;
    })




    if ($(document).width() > 1300) {
        $(".container").css({
            'width':'1300px'
        });
    }

    $(".cfl").css({
        'width':($(".container").width() -305)+'px'
    });


});


function ShowDiv(elemId) {
    var t=document.getElementById(elemId);
    t.style.display= "block";
}
function HideDiv(elemId) {
    var t=document.getElementById(elemId);
    t.style.display= "none";
}



function getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else {
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) {
        if(document.documentElement.clientWidth){
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) {
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }
    if(xScroll < windowWidth){
        pageWidth = xScroll;
    } else {
        pageWidth = windowWidth;
    }
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
    return arrayPageSize;
};
