﻿



function Prijava() {
    //najprije idemo centrirati i sakriti

    $('div.Cloud2').hide().css('z-index', "200");

    $('div.Login a.PrijavaOpen,div.Cloud2 a#LinkButtonClose').click(function () {
        $('div.Cloud2').slideToggle();
    });
}



function IfLogin(pom) {
    if (isLogin == true) {
        pom();
    } else {
        $('div.Cloud2').show();
    }
}


function Minimize() {
    $("div#sortable div.NewsBox, div#sortable2 div.NewsBox").each(function (index, domEle) {
        var $self = $(domEle);
        var id = $(domEle).attr('id');

        if ($.cookie('NaslovnicaShow' + id) == "none") {
            if (isLogin) {
                $self.find(".Heading  .showhide").toggleClass('arrow_open');
                $self.find(".Heading  .showhide").toggleClass('arrow_closed');
                $self.find(".Content").hide();
            }
        }

        //alert(id);
        $self.find(".Heading  .showhide").unbind('click').click(function () {
            $(this).toggleClass('arrow_open');
            $(this).toggleClass('arrow_closed');
            $self.find(".Content").slideToggle("slow", function () {
                //alert($(this).css('display'));


                if (isLogin)
                    $.cookie('NaslovnicaShow' + id, $(this).css('display'));
                else
                    $('div.Cloud2').show();
            });
        });
    });
}

function SviLinkovi() {
    $("div#SviLinkovi .Container .Link a").click(function () {
        var id = $(this).attr("id");
        var pom = $("div#SviLinkovi div.Menu");
        var kategorijanaziv = $(this).html();
        if (kategorijanaziv.toLowerCase().indexOf("sex") > -1) {
            if ($.cookie("18") == null) {
                var answer = confirm("Upozorenje \n \n Stranice koje se nalaze u ovoj kategoriji imaju erotski sadržaj i nisu namijenjene maloljetnim osobama (mlađim od 18 godina). \n \n Izjavljujem da imam više od 18 godina.")
                if (answer) {
                    $.cookie("18", true);
                }
                else {
                    return;
                }
            }
        }

        if ($(this).hasClass("slc")) {
            $this = $(this);
            pom.hide("slide", { direction: "up" }, 350, function () {
                $this.removeClass("slc");
                $this.parent().parent().removeClass("slc");
                $this.parent().removeClass("slc");
            });
            return;
        }

        $("div#SviLinkovi .Container .Link a,div#SviLinkovi .Container div").removeClass("slc");

        var $this = $(this);
        //alert(id);
        $this.addClass("slc");
        $this.parent().parent().addClass("slc");
        $this.parent().addClass("slc");


        if (pom.css("display") == "none") {
            pom.find("li").remove();
            //sada moramo dohvatiti podatke

            JedanKlik.OrderSave.SviLinkovi(id, 4, kategorijanaziv, function (data) {
                //alert(data);
                $.each(data, function (index, item) {
                    //alert(index + ': ' + value);
                    var ul = pom.find("ul#" + item.Stupac);
                    if (index != 0) {
                        $("<li class=\"title\">" + item.Naziv + "</li>").appendTo(ul);
                    }

                    $.each(item.Linkovi, function (index, link) {
                        $("<li><a class=\"linkstat\" target=\"_blank\" href=\"" + link.Url + "\" id=\"" + link.Id + "\"  linkid=\"" + link.Id + "\" ><img src=\"controls/imgIcons.aspx?w=16&h=16&img=" + link.Id + "\"  />" + link.Naziv + "</a></li>").appendTo(ul);
                    });
                });

                pom.hide().detach();
                pom.insertAfter($this.parent().parent().parent()).show("slide", { direction: "up" }, 350, function () {
                    InitStatistic();
                    pom.parent().find(".ui-effects-wrapper").remove();
                });
            });
        } else {

            pom.hide("slide", { direction: "up" }, 350, function () {
                pom.find("li").remove();
                //sada moramo dohvatiti podatke
                StartLoading()
                JedanKlik.OrderSave.SviLinkovi(id, 4, kategorijanaziv, function (data) {
                    //alert(data);
                    StopLoading();
                    $.each(data, function (index, item) {
                        //alert(index + ': ' + value);
                        var ul = pom.find("ul#" + item.Stupac);
                        if (index != 0) {
                            $("<li class=\"title\">" + item.Naziv + "</li>").appendTo(ul);
                        }

                        $.each(item.Linkovi, function (index, link) {
                            $("<li><a class=\"linkstat\" target=\"_blank\" href=\"" + link.Url + "\" linkid=\"" + link.Id + "\" id=\"" + link.Id + "\"  ><img src=\"controls/imgIcons.aspx?w=16&h=16&img=" + link.Id + "\"  />" + link.Naziv + "</a></li>").appendTo(ul);
                        });
                    });
                    pom.hide().detach();
                    pom.insertAfter($this.parent().parent().parent()).show("slide", { direction: "up" }, 350, function () {
                        InitStatistic();
                        pom.parent().parent().find(".ui-effects-wrapper").remove();
                    });
                });
                //kada smo ga ispraznili
            });
        }
    });

    //sada simuliramo klik na prvi
    //$("div#SviLinkovi .Container .Link a:first").click();

    $("div#SviLinkovi div.Menu").hide();
}

function InitStatistic() {
    $("a.linkstat").unbind('click').click(function () {
        id = $(this).attr("linkid");

        $.get('/OrderSave.asmx/Statistika?linkid=' + id + '&tekstid=NULL');
        //JedanKlik.OrderSave.Statistika(id, "NULL");
    });

    $("a.StatistikaTekst").attr("target", "_blank").unbind('click').click(function () {
        id = $(this).attr("id");
        $.get('/OrderSave.asmx/Statistika?linkid=NULL&tekstid=' + id);
        //JedanKlik.OrderSave.Statistika("NULL", id);
    });
}



function ShowHide() {
    //alert($.cookie('NaslovnicaKategorijeShow'));
    if (isLogin == false) {

        return;


        if ($.cookie('NaslovnicaKategorijeShow') != null) {


            //alert("showhide has cookie"+$.cookie('NaslovnicaKategorijeShow') );                
            $("ul#naslovnica-kategorije input").removeAttr("checked");
            var pom = $.cookie('NaslovnicaKategorijeShow').split("-");
            $.each(pom, function (index, value) {
                if (value > 0) {

                    $("ul#naslovnica-kategorije input#naslovnica-katid-" + value).attr("checked", "checked");
                }
            });
            $('div#sortable div.NewsBox,div#sortable2 div.NewsBox').each(function () {
                var pomi = "-" + $.cookie('NaslovnicaKategorijeShow') + "-";
                if (pomi.indexOf("-" + $(this).attr("id") + "-") < 0) {
                    $(this).hide();
                } else {
                    $(this).show();
                }
            });
        }
    }
}

function IzdvojeniSortable() {
    //$('div.Cloud').center().fadeIn("slow");    
    $("li.izdvojenilink").each(function () {
        var $this = $(this);
        var $thisId = $this.attr('id');
        $(this).find("a.del").click(function () {
            JedanKlik.OrderSave.DeleteIzdvojeniLinkovi($.cookie('user'), $thisId);
            $this.fadeOut("slow", function () {
                $this.remove();
            });
        });
    });


    //najprije gledamo da li smo logirani, odnosno ako nismo onda ne radimo iz cookija sort
    //JedanKlik.OrderSave.isLogin($.cookie('user'), function(i) {
    //alert(i); sortiranje iz cookija radimo samo za neregistrirane
    if (isLogin == false) {
        return;
        $('div.izdvojenisort').each(function () {
            var $this = $(this);
            var thisId = $this.attr("rel");
            if ($.cookie('izdvojenisort' + thisId) != null) {
                var pom = $.cookie('izdvojenisort' + thisId).split("-");
                pom.reverse();
                var myDiv = null;
                $.each(pom, function (index, value) {
                    if (value > 0) {
                        //alert($thisId + " -> " + value);
                        myDiv = $('div.izdvojenisort > div[id=' + value + ']').remove();
                        myDiv.prependTo('div.HasEditBoxIzdvojeni div.Content>div[id=' + thisId + ']');
                        myDiv = null;
                    }
                });
            }
        });
    }
    //});

    $('div.izdvojenisort').each(function () {
        var attrId = $(this).attr('id');
        var $this = $(this);

        $(this).find('.IzdvojenaKategorija ul').sortable({
            handle: 'a.move',
            placeholder: "Palceholder",
            update: function () {

                all = [];
                $this.find("div.IzdvojenaKategorija ul li.izdvojenilink").each(function () {
                    all.push($(this).attr("id"));
                });
                JedanKlik.OrderSave.SaveIzdvojeniLinkovi($.cookie('user'), all.join("-"), attrId);

            }
        });
    });


    $('div.izdvojenisort').sortable({
        connectWith: "div.izdvojenisort",
        placeholder: "Palceholder",
        handle: 'div.Headline',
        dropOnEmpty: true,
        update: function () {

            if (isLogin) {
                var attrId = $(this).attr('id');
                var $this = $(this);
                var all = [];
                $this.delay(100).find("div.IzdvojenaKategorija").each(function () {
                    all.push($(this).attr("id"));
                });
                $.cookie('izdvojenisort' + attrId, all.join("-"));
                if (all.length < 1 || isLogin) {
                    $.cookie('izdvojenisort' + attrId, null);
                }
                //alert($.cookie('izdvojenisort' + attrId));
                all = [];
                $this.find("div.IzdvojenaKategorija ul li.izdvojenilink").each(function () {
                    all.push($(this).attr("id"));
                });
                JedanKlik.OrderSave.SaveIzdvojeniLinkovi($.cookie('user'), all.join("-"), attrId);
                InitSaveLink();
            } else
                $('div.Cloud2').show();

        }
    });
};




function InitSaveLink() {
    //alert("tu sam");
    var dlg = $("div.Cloud");
    //dlg.appendTo(jQuery("form:first"));

    $("div.Cloud #linkNaziv").keyup(function (event) {
        if (event.keyCode == '13') {
            $("div.Cloud #linkUrl").focus();
            event.preventDefault();

        }
    });

    $("div.Cloud #linkUrl").keyup(function (event) {
        if (event.keyCode == '13') {
            event.preventDefault();

            $("div.Cloud input#spremi").click();
        }
    });


    dlg.hide();
    $("div.izdvojenisort").each(function () {
        var attrId = $(this).attr('id') + $(this).find("div.IzdvojenaKategorija").length;
        $(this).find("div.IzdvojenaKategorija").each(function () {
            var $this = $(this);
            var kategorija = $(this).attr('id');
            //dlg.center();
            $this.find("a.DodajNoviLink").unbind('click').click(function () {
                var naziv = $("div.Cloud #linkNaziv").val("").focus();
                var url = $("div.Cloud #linkUrl").val("http://www.");
                dlg.find("#linkNaziv").focus();
                dlg.css("z-index", "100").hide(0, function () {
                    dlg.show().find("input#spremi").unbind('click').click(function () {
                        var naziv = dlg.find("#linkNaziv").val();
                        var url = dlg.find("#linkUrl").val();
                        if (naziv.length > 3 && url.length > 7) {
                            //$("div.Cloud").fadeTo("fast",0.5);
                            StartLoading();
                            JedanKlik.OrderSave.DodajLink($.cookie("user"), kategorija, naziv, url, attrId, function (i) {
                                StopLoading();
                                if (i == true) {
                                    LinkUpdatePanelId = $("div[rel=LinkUpdatePanelId]").attr("class");
                                    __doPostBack(LinkUpdatePanelId, "novilink");
                                    //alert("tu sam");
                                } else {
                                    alert("Problem");
                                }
                                dlg.fadeOut("fast");
                            });
                        } else {
                            alert("Unesite naslov i link");
                        }
                    });
                });

            });
        });
    });


    dlg.find(".odustani").unbind('click').click(function () {
        dlg.fadeOut("fast");
    });
}


function EditBox() {
    $("div.HasEditBox").each(function (index, domEle) {
        $ui = $(domEle);
        $ui.find("div.EditShow").unbind('click').click(function () {
            $ui.find("div.EditBox").slideToggle("slow");
        });

        $ui.find("#NaslovnicaOdustani,#NaslovnicaOdustaniKorisnici ").unbind('click').click(function () {
            $ui.find("div.EditBox").slideToggle("slow");
        });

        $ui.find("#NaslovnicaSpremi").unbind('click').click(function () {
            if (isLogin) {
                var allVals = [];
                $("ul#naslovnica-kategorije input[type=checkbox]:checked").each(function () {
                    //alert($(this).val());
                    allVals.push($(this).val());
                });
                //spremamo u cookie
                //alert(allVals.join("-"));
                $.cookie('NaslovnicaKategorijeShow', allVals.join("-"));
                $ui.find("div.EditBox").slideToggle("slow");
                ShowHide();
            } else {
                $('div.Cloud2').show();
            }
        });
    });


    initShowHideIzdvojenaKategorija();

    $("input.IzdvojeniSpremi").unbind('click').click(function () {
        $("input.IzdvojeniOdustani").click();
    });



    $("div.HasEditBoxIzdvojeni").each(function (index, domEle) {
        $ui2 = $(domEle);
        if ($.cookie('LinkUrediShow') != "1") {
            $ui2.find("div.EditBoxIzdvojeni").hide();
            $ui2.find(".LinkUredi").hide();
        } else {
            $ui2.find("div.EditBoxIzdvojeni").show();
            $ui2.find(".LinkUredi").show();
        }


        $ui2.find("div.EditShowIzdvojeni, input.IzdvojeniOdustani").unbind('click').click(function () {

            if (isLogin) {
                $ui2.find("div.EditBoxIzdvojeni").slideToggle("slow", function () {
                    if ($(this).css('display') == 'none') {
                        $ui2.find(".LinkUredi").hide();
                    } else {
                        $ui2.find(".LinkUredi").show();
                    }
                });

                if ($.cookie('LinkUrediShow') == "1") {
                    $.cookie('LinkUrediShow', "0");
                } else {
                    $.cookie('LinkUrediShow', "1");
                }
            } else {
                $('div.Cloud2').show();
            }
        });
    });
};



function showSpremi() {
    if ($("div.ShowHideIzdvojenaKategorija ul").css('display') == 'none' && $("div.ShowHideIzdvojenaKategorijaMoja ul").css('display') == 'none') {
        $(".EditBoxIzdvojeni .Btns").hide();
    } else {
        $(".EditBoxIzdvojeni .Btns").show();
    }
}


function initShowHideIzdvojenaKategorija() {
    $("div.ShowHideIzdvojenaKategorija h2").unbind('click').click(function () {
        $("div.ShowHideIzdvojenaKategorija h2").toggleClass("open");
        $("div.ShowHideIzdvojenaKategorija ul").slideToggle("slow", showSpremi);
    });

    $("div.ShowHideIzdvojenaKategorijaMoja h2").unbind('click').click(function () {
        $("div.ShowHideIzdvojenaKategorijaMoja h2").toggleClass("open");
        $("div.ShowHideIzdvojenaKategorijaMoja ul").slideToggle("slow", showSpremi);
    });

}


function NovostiSort() {
    //JedanKlik.OrderSave.isLogin($.cookie('user'), function(i) {
    //alert(i); sortiranje iz cookija radimo samo za neregistrirane
    if (isLogin == false) {

        return;
        if ($.cookie('NaslovnicaSort') != null) {
            //alert($.cookie('NaslovnicaSort'));
            var pom3 = $.cookie('NaslovnicaSort').split("-");
            pom3.reverse();
            $.each(pom3, function (index, value) {
                //alert("cookie1 " + index + ': ' + value);
                var m = $("div.NaslovnicaSort > div[id=" + value + "]").detach();
                m.prependTo("div#sortable");
            });
        }
        if ($.cookie('NaslovnicaSort2') != null) {
            //alert($.cookie('NaslovnicaSort2'));
            var pom2 = $.cookie('NaslovnicaSort2').split("-");
            pom2.reverse();
            $.each(pom2, function (index, value) {
                //alert("cookie2 " + index + ': ' + value);
                var v = $("div.NaslovnicaSort > div[id=" + value + "]").detach();
                v.prependTo("div#sortable2");
            });
        }
    }
    //});
}


function UpdateVijesti(id, brojVijesti) {
    var $this = $("div.VijestiNewsBox[id='" + id + "']");
    var trenutniBrojVijesti = $this.find(".InsideVijestiBox > dl").size();

    //alert(trenutniBrojVijesti);

    if (isLogin == false) {

        $.cookie('trenutniBrojVijesti-' + id, brojVijesti);
    }

    StartLoading();
    JedanKlik.OrderSave.DajVijesti($.cookie('user'), id, brojVijesti, function (data) {
        StopLoading();
        $this.find(".InsideVijestiBox > dl").remove();
        $.each(data, function (index, item) {
            //alert(index + ': ' + value);
            var doom = $("<dl><dd class=\"img\"><a target=\"_blank\" href=\"" + item.href + "\" title=\"" + item.title + "\"><img src=\"" + item.src + "\" alt=\"" + item.izvor + "\"></a></dd><dd class=\"time\">" + item.time + "</dd><dt ><a href=\"" + item.href + "\" title=\"" + item.title + "\" target=\"_blank\">" + item.titleSubstring + "</a></dt></dl>");
            var inside = $this.find(".InsideVijestiBox");
            doom.appendTo(inside);
        });
        MakniCrtu();
    });
};



function VijestiPlusMinus() {
    $("div.VijestiNewsBox").each(function () {
        $this = $(this);
        var id = $this.attr("id");
        var trenutniBrojVijesti = $this.find(".InsideVijestiBox > dl").size();

        //alert(trenutniBrojVijesti);
        if (isLogin == false) {
            return;
            if ($.cookie('trenutniBrojVijesti-' + id) != null && $.cookie('trenutniBrojVijesti-' + id) != trenutniBrojVijesti) {
                UpdateVijesti(id, $.cookie('trenutniBrojVijesti-' + id));
                trenutniBrojVijesti = $.cookie('trenutniBrojVijesti-' + id);
            }
        }

    });

    $("div.VijestiNewsBox .Edit a.plus").unbind('click').click(function () {
        if (isLogin) {
            var id = $(this).attr("id");
            var $this = $("div.VijestiNewsBox[id='" + id + "']");

            var trenutniBrojVijesti = $this.find(".InsideVijestiBox > dl").size();
            if (trenutniBrojVijesti < 10) {
                trenutniBrojVijesti++;
                UpdateVijesti(id, trenutniBrojVijesti);
            }
        } else {
            $('div.Cloud2').show();
        }
    });

    $("div.VijestiNewsBox .Edit a.minus").unbind('click').click(function () {
        if (isLogin) {
            var id = $(this).attr("id");
            var $this = $("div.VijestiNewsBox[id='" + id + "']");

            var trenutniBrojVijesti = $this.find(".InsideVijestiBox > dl").size();
            if (trenutniBrojVijesti > 0) {
                trenutniBrojVijesti--;
                UpdateVijesti(id, trenutniBrojVijesti);
            }
        } else {
            $('div.Cloud2').show();
        }
    });
};


function toogleIzdvojenaKategorija() {
    $("div.IzdvojenaKategorija div.Edit>a.toogle").unbind('click').click(function () {
        $this = $(this).parent().parent().parent();
        //alert($this.attr("class"));
        $this.find(".LinkUredi").slideToggle("slow");
    });
}

function ReBind(sender, args) {
    //alert("tu sam");
    InitStatistic();

    NovostiSort();
    ShowHide();
    Minimize();
    EditBox();
    IzdvojeniSortable();
    InitSaveLink();
    toogleIzdvojenaKategorija();
    VijestiPlusMinus();



    $("div#sortable, div#sortable2").sortable({
        placeholder: "NewsBoxSort",
        handle: ".Heading .Move",
        connectWith: ".Content",
        delay: 500,
        dropOnEmpty: true,
        update: function () {
            var pom = new Array();
            var pom2 = new Array();


            if (isLogin) {

                if ($(this).attr('id') == 'sortable') {
                    $("div#sortable").find("div.NewsBox").each(function () {
                        pom.push($(this).attr("id"));
                    });

                    JedanKlik.OrderSave.SaveNaslovnicaNovostiOrder(pom.join("-"), "", $.cookie('user'), function (i) {
                        if (i == 0) {
                            if (pom.length > 0) {
                                $.cookie('NaslovnicaSort', pom.join("-"));
                            } else {
                                $.cookie('NaslovnicaSort', null);
                            }
                            //alert($.cookie('NaslovnicaSort'));
                        }
                    });

                }


                if ($(this).attr('id') == 'sortable2') {
                    $("div#sortable2").find("div.NewsBox").each(function () {
                        pom2.push($(this).attr("id"));
                    });

                    JedanKlik.OrderSave.SaveNaslovnicaNovostiOrder("", pom2.join("-"), $.cookie('user'), function (i) {
                        if (i == 0) {
                            if (pom2.length > 0) {
                                $.cookie('NaslovnicaSort2', pom2.join("-"));
                            } else {
                                $.cookie('NaslovnicaSort2', null);
                            }
                            //alert($.cookie('NaslovnicaSort2'));
                        }
                    });
                }
            } else {
                $('div.Cloud2').show();
            }
        }
    });
}


function AddLoadHandler() {
    IzvoriTab();
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_pageLoaded(ReBind);
    prm.add_pageLoaded(function () {
        $("a.SearchLink[href]").click(function () {
            window.open(this.href, this.target, '');
            $(this).removeAttr("href");
            return false;
        }).click();
    });

    //

    prm.add_pageLoaded(function () {
        var $TwitterLoginLink = $("a.TwitterLoginLink[href]");
        if ($TwitterLoginLink.size() > 0) {


            var url = $TwitterLoginLink.attr("href");
            //alert(url);
            window.open(url, "TwitterLogin", "location=0,status=0,scrollbars=0,width=800,height=400");

        } else {

        }
    });

    prm.add_pageLoaded(function () {
        var $GoogleLoginLink = $("a.GoogleLoginLink[href]");
        if ($GoogleLoginLink.size() > 0) {


            var url = $GoogleLoginLink.attr("href");
            //alert(url);
            window.open(url, "GoogleLogin", "location=0,status=0,scrollbars=0,width=800,height=400");

        } else {

        }
    });

    prm.add_beginRequest(function () {
        StartLoading();
    });

    prm.add_endRequest(function () {
        StopLoading();
    });


}



function IzvoriTabClear(index) {

    $("ul#IzvoriTabLink li a:lt(" + index + ")").removeClass("slc");
    $("ul#IzvoriTabLink li a:gt(" + index + ")").removeClass("slc");
    $("ul#IzvoriTabLink li a:eq(" + index + ")").removeClass("rss");

    $("div.IzvoriTab > div:lt(" + index + ")").removeClass("slc");
    $("div.IzvoriTab > div:gt(" + index + ")").removeClass("slc");
    $("div.IzvoriTab > div:eq(" + index + ")").removeClass("rss");

    $("ul#IzvoriTabLink li a:lt(" + index + ")").addClass("rss");
    $("ul#IzvoriTabLink li a:gt(" + index + ")").addClass("rss");
    $("ul#IzvoriTabLink li a:eq(" + index + ")").addClass("slc");

    $("div.IzvoriTab > div:lt(" + index + ")").addClass("rss").hide();
    $("div.IzvoriTab > div:gt(" + index + ")").addClass("rss").hide();
    $("div.IzvoriTab > div:eq(" + index + ")").addClass("slc").show();
}

function IzvoriTab() {
    $("ul#IzvoriTabLink li a").each(function (index, doom) {
        $this = $(this);

        if (index == 0) {
            IzvoriTabClear(index);
        }

        $this.click(function () {
            IzvoriTabClear(index);
        })

    });


    if (isLogin == false) {
        $("div.IzvoriKlik input").click(function (event) {
            event.preventDefault();
            $("div.Login>a").click();
        });
    }

}


function IntPage() {
    if ($.cookie('Naslovnica') != null) {

        if (isLogin) {
            var pom = new Array();
            pom = $.cookie('Naslovnica').split("-");
            pom.reverse();
            $.each(pom, function (index, value) {
                var k = $("div.Col1 > div[id=" + value + "]").detach();
                k.prependTo("div.Col1");
            });
        }
    }


    var cssDisplay = "block";
    var attrid = null;

    $("div.Col1").sortable({
        handle: "div.Heading:eq(0) h1 a, div.Heading:eq(0) div.Move",
        placeholder: "PlaceHolder",
        update: function () {
            var pom = new Array();
            $(this).find("div.NaslovnicaBoxSort").each(function () {
                pom.push($(this).attr("id"));
            });
            if (isLogin) {
                $.cookie('Naslovnica', pom.join("-"));
                //alert(pom.join("-"));
            } else {
                $('div.Cloud2').show();
            }
        },
        start: function (event, ui) {

            cssDisplay = $(ui.item).find("div.Content:first").css("display");
            attrid = $(ui.item).attr("id");
            //            console.log("start " + cssDisplay);
            //            console.log("start id  " + attrid);

            $(ui.item).find("div.Content:first").css("display", "none");
        },
        stop: function (event, ui) {
            //console.log("stop "+cssDisplay);
            $(ui.item).find("div.Content:first").css("display", cssDisplay);
        }

    });

    $("div.Col1 div.NaslovnicaBoxSort").each(function (index, domEle) {
        var $self = $(domEle);
        var id = $(domEle).attr('id');

        if ($.cookie('NaslovnicaBoxSortShow' + id) == "none") {
            $self.find(".Heading:eq(0)  .showhide").toggleClass('arrow_open');
            $self.find(".Heading:eq(0)  .showhide").toggleClass('arrow_closed');
            $self.find(".Content:eq(0)").hide();
        }

        //alert(id);
        $self.find(".Heading:eq(0)  .showhide").click(function () {
            $(this).toggleClass('arrow_open');
            $(this).toggleClass('arrow_closed');
            $self.find(".Content:eq(0)").slideToggle("slow", function () {
                if (isLogin) {
                    $.cookie('NaslovnicaBoxSortShow' + id, $(this).css('display'));
                } else {
                    $('div.Cloud2').show();
                }
            });
        });
    });
}


function Register() {
    $(".Register a").click(function () {
        $("div.Login a").click();
    });
}


function StopLoading() {
    $('div#loading').hide().children().remove();
}

function StartLoading() {
    StopLoading();
    $('div#loading').append('<span><img alt="Loading..." src="images/loadingAnimation.gif"></span>');

    setTimeout(function () {
        $('div#loading').show();
    }, 1000);
}


function isd(p) {
    if (p == undefined || p == null) {
        return "";
    }
    return p;
}

function dajLink(link, iner) {
    if (link != undefined || link != null || link != "") {
        return '<a class="statusLink" href="' + link + ' "target="_blank" >' + isd(iner) + '</a>';
    }
    return isd(iner);
}

function formatFBTime(fbDate) {
    var arrDateTime = fbDate.split("T");
    var strTimeCode = arrDateTime[1].substring(0, arrDateTime[1].indexOf("+"));
    var valid_date = new Date(arrDateTime[0])
    var arrTimeCode = strTimeCode.split(":");
    valid_date.setHours(arrTimeCode[0]);
    valid_date.setMinutes(arrTimeCode[1])
    valid_date.setSeconds(arrTimeCode[2])
    return valid_date;
}

function getTimeDistance(timee, i) {
    //console.log(timee);
    var now = new Date();
    var timee = formatFBTime(timee);
    var pom = Math.abs(now.getMinutes() - timee.getMinutes());
    if (pom < 5) {
        return pom + ' minute ';
    } else if (pom < 120) {
        return pom + ' minuta ';
    }

    if (i < 5) {
        return i + ' minute ';
    }

    return i + ' minuta ';
}


function dajSliku(img) {
    if (img != undefined) {
        return '<img class="statusSlika" src="' + img + '"> ';
    }
    return "";
}


function skrati(rijec, limit) {
    if (rijec.toString().length > limit) {
        return rijec.toString().substring(0, limit) + " .. ";
    }
    return rijec;
}


function getStatus() {

    $("ul#facebook li").remove();

    var $ul = $("ul#facebook");

    if (typeof (FB) == "undefined") { return; }
    FB.api('/me/home', { limit: 20 }, function (response) {
        StopLoading();
        var brojac = 6;
        if (!response || response.error) {
            //alert('nemate dopustenje'); potrebno staviti login facebook
        } else {
            $.each(response.data, function (index, item) {
                var html = '<li class="faceLi">' +
            '<div class="FaceLeft">' +
                '<img src="https://graph.facebook.com/' + item.from.id + '/picture" class="ProfilePic">' +
             '</div>' +
            '<div class="FaceRight">' +
                '<a target="_blank" class="profileLink" href="http://www.facebook.com/profile.php?id=' + item.from.id + '">' + item.from.name + '  </a> ';
                if (item.to != undefined) {
                    html = html + '<a class="profileLink ProfileLinkTo" href="http://www.facebook.com/profile.php?id=' + item.to.data[0].id + '"> ' + item.to.data[0].name + '</a>';
                }
                html = html + ' <span class="statusSpan"> ' + skrati(isd(item.message), 140) + ' </span>';
                html = html + '<div class="Status"> ';
                html = html + dajLink(item.link, dajSliku(item.picture) +
                    '<span class="nameSpan">' + isd(item.name) + ' </span>') +
                    '<span class="Status">' + skrati(isd(item.description), 90) + ' </span> ';

                html = html + '</div>' +
                '<div class="FacebookFunct">' + '<dl>' + '<dt class="time">';

                if (item.icon != undefined) {
                    html = html + ' <img src="' + item.icon + '" />';
                }

                html = html + ' Prije ' + getTimeDistance(item.created_time, index) + ' </dt>' +
                        '<dt class="addComment"><a class="faceKomentari" id="FaceComm' + brojac + '" > Komentari ';
                if (item.comments != undefined) {
                    html = html + '(' + item.comments.count + ')';
                }

                html = html + '</a></dt>' + '<dt class="addLike"> Like ';
                if (item.likes != undefined) {
                    html = html + '(' + item.likes + ')';
                }

                html = html + '</dt>' +
                    '</dl>' +
                '</div>' +
            '</div>' +
            '<div class="FaceBottom  FaceComm' + brojac + '" >' + '<div class="Comments">';
                try {
                    if (item.comments != undefined) {



                        html = html + '<ul>';

                        $.each(item.comments.data, function (index, comment) {
                            html = html + '<li class="comment">' + '<div class="commentsLeft"><img class="commentFrom" src="https://graph.facebook.com/' + comment.from.id + '/picture"></div>' +
                        '<div class="commentsRight"><a target="_blank" href="http://www.facebook.com/profile.php?id=' + comment.from.id + '" >' + comment.from.name + ' </a> ' + comment.message + ' </div><div style="clear:both;"></div></li>';
                        });
                        html = html + '</ul>';
                    }
                }
                catch (err) {

                    //console.log("Komentari exception");
                }

                html = html + '</div>' +
            '</div>' +
        '</li>';
                if (item.type != "swf") {
                    brojac--;
                    if (brojac > 0) {
                        $ul.append(html);
                    }
                }
            });


        }

        $("a.faceKomentari").click(function () {
            //alert($(this).attr("id"));
            $("div." + $(this).attr("id")).slideToggle("normal");
        });
    });
};


function initFacebookTab() {
    $("ul.SocialTabs li").click(function () {

        if ($(this).hasClass("sel")) {
            $(this).parent().find("li").removeClass("sel");
            $("div.SocialNet > div").hide();
            $.cookie('socielNet', "hidden");

        } else {
            $(this).parent().find("li").removeClass("sel");
            $(this).addClass("sel");

            $("div.SocialNet > div").hide().removeClass("hidden");
            $("div.SocialNet > div." + $(this).attr("id")).show();

            $.cookie('socielNet', $(this).attr("id"));
        }
    });

    if ($.cookie('socielNet') != null) {
        $("ul.SocialTabs li#" + $.cookie('socielNet')).click();
    } else {
        $("ul.SocialTabs li:first").click();
    }


    $("div.WelcomeRight").click(function () {
        //$.cookie('user', null) ;
        $("input.FacebookLogoutButton").click();
    });


    $("div.WelcomeRefresh a").click(function () {
        getStatus();
        StartLoading();
    });
};




function InitSearch() {
    $("ul.SearchSelect li a").click(function (event) {
        $this = $(this);
        event.preventDefault();

        //alert($this.attr("rel"));
        $("ul.SearchSelect li a").removeClass("slc");

        $this.addClass("slc");
        $("div#Selected input").val($this.attr("rel"));

    });


    $("div.Search div.Formarea input.button").click(function (event) {
        var tekst = $("div.Search div.Formarea input").val() + "";
        var tip = $("div#Selected input").val();
        //alert(tekst);
        event.preventDefault();

        if (tekst.length > 0) {
            StartLoading();
            JedanKlik.OrderSave.Trazilica(tip, tekst, function (i) {
                //alert(i);
                window.location = i;
            });
        }
    });


    $("div.Search div.Formarea input").keyup(function (event) {
        if (event.keyCode == '13') {
            event.preventDefault();
            $("div.Search div.Formarea input.button").click();
        }
    });




};

function MakniCrtu() {
    $("div.InsideVijestiBox dl").removeClass("last");
    $("div.InsideVijestiBox").each(function () {
        $(this).find("dl:last").addClass("last");
    });
};



function InitFavPop() {
    if ($.cookie("favPopup") == null) {
        //$("div.AddToFavoritePoput").show();

        $("div.AddToFavoritePoput .Iner .closeFavorite").click(function () {
            $("div.AddToFavoritePoput").hide();
        });

        $("div.AddToFavoritePoput .Iner .SetAsHomePage").click(function () {
            $("div.AddToFavoritePoput").hide();
            $("div#Top_Links a.home").click();
        });

        $("div.AddToFavoritePoput .Iner .SetAsFavorite").click(function () {
            $("div.AddToFavoritePoput").hide();
            $("div#Top_Links a.favorites").click();
        });
    }

    $.cookie("favPopup", "1");
}

function InitFacebookOdjava() {
    $("a.FacebookOdjava").click(function (event) {
        if ($(".FacebookOdjavaClick").size() > 0) {
            event.preventDefault();
            $(".FacebookOdjavaClick").click();
        }
    });
}


$(document).ready(function () {

    InitFacebookOdjava();

    InitFavPop();
    ReBind();
    MakniCrtu();
    InitSearch();
    $("a.del").attr("title", "Obriši");
    getStatus();
    IntPage();

    $("div#loading").click(function () {
        StopLoading();
    }).hide();

    $("div.ShowHideIzdvojenaKategorija ul,div.ShowHideIzdvojenaKategorijaMoja ul").hide();
    $("div.ShowHideIzdvojenaKategorija h2,div.ShowHideIzdvojenaKategorijaMoja h2").toggleClass('open');
    Register();
    SviLinkovi();
    //$("div.Formarea input:first").focus();
    $("img#FacebookLogin, div.FacebookPrijaviSe, div.FaceLoginButton").click(function () {
        $("span#FacebookLogin a").click();
    });

    //linkovi da se u novom tabu otvaraju

    $(".NaslovnicaBoxSort div.Content ul li a,.NaslovnicaBoxSort .Content dt a").attr("target", "_blank");
    $("div.HasEditBox div.EditBox").css("display", "block").hide();
    $(".LinkUredi").css("display", "block").hide();
    Prijava();


    initFacebookTab();
    $("a[name='openclose']").attr("title", "Pokaži/Sakrij");
    //osvježavanje stranice
    setTimeout(function () { location.reload(); }, 60 * 60 * 1000);
    //sada confirm provjeru punoljetnosti
    setTimeout(function () { $('div#Cont div.HideAll').hide(); }, 300);

    if (isLogin == false) {
        $("div#Cont .Col1 .Box .Content .Box .izdvojenilink a.move").css("cursor", "default");
    }

    AddLoadHandler();

});
