﻿/* jQuery extensions */
(function ($) {
    $.fn.fillBackground = function (_buttonclick) {
        var self = this;
        self.children().not('.static').fadeOut('fast', function () {
            $(this).remove();
        });
        var rows = Math.ceil(screen.height / 166);
        var cols = Math.ceil(screen.width / 166);
        $.mvc_postJSON("/sv/Page.mvc/LoadBgImages/", { n: (cols * rows) - 1 }, function (d) {
            var r = 0;
            var c = 0;
            $(d.Images).each(function (i) {
                var h = '';
                if (i == 0) {
                    h = '<a href="/sv.html/start" class="page"><img src="/Content/img/transp.gif" /></a>';
                    $("#logo").attr("style", 'background-image:url(' + kp.url(this.Path) + ');').html(h);
                }
                else {
                    h = '<li'
                    h += ' style="background-image:url(' + kp.url(this.Path) + ');'
                    h += ' top:' + (r * 166) + 'px;';
                    h += ' left:' + (c * 166) + 'px;';
                    if (this.ElementId != 0) {
                        h += '"><a href="'
                        //if (this.Type == 1)
                        //    h += '/sv.html/start';
                        //else
                        if (this.Type == 5) {
//                            if (this.Visibility != 3) {
//                                h += '/sv/Account.mvc/LogOnForm?layout=lightbox&ReturnUrl=/sv.html/lightbox/?layout=lightbox&projectid=' + this.ElementId;
//                            } else {
                                h += '/sv.html/lightbox/?projectid=' + this.ElementId;
//                            }
                        }
                        else if (this.Type == 6) {
                            h += '/sv/Mywork.mvc/View/?workid=' + this.ElementId;
                        }
                        else {
//                            if (this.Visibility != 3) {
//                                h += '/sv/Account.mvc/LogOnForm?layout=lightbox&ReturnUrl=/sv/Tags.mvc/ViewTag/?layout=lightbox&tagid=' + this.ElementId;
//                            } else {
                                h += '/sv/Tags.mvc/ViewTag/?tagid=' + this.ElementId;
                            //}
                        }
                        h += '" class="'
                        //if (this.Type == 1)
                        //    h += 'page';
                        //else
                        h += 'popup';
                    }
                    h += '"><img src="/Content/img/transp.gif" />';
                    if (this.Elementid != 0)
                        h += '</a>';
                    h += '</li>';
                    self.append(h);
                }
                c++;
                if (i == 4)
                    c++;
                if (c * 166 > screen.width) {
                    r++;
                    c = 0;
                }
            });
            $.mvc_postJSON("/sv/Page.mvc/LoadBgTags/", { n: ((cols - 1) * (rows - 2)) - 1 }, function (d) {
                var r = 0;
                var c = 2;
                var w_len = d.Weights.length;
                var maxlen = 55, size = 0;
                var _bclick = _buttonclick;
                $(d.Tags).each(function (i) {
                    var len = this.Name.length;
                    var maxweight = d.MaxWeight;
                    var left = (c * 166) - (len / 2 * 9);
                    var rotate = Math.floor((Math.random() * 3) + 0);
                    var weight = this.Weight;
                    var tagsize = Math.floor((Math.random() * 5) + 0);
                    var tagsizes = Array(20, 25, 30, 35, 40, 45);
                    var ele_position = 0, pos = 0;
                    $(d.Weights).each(function () {
                        if (this == weight) {
                            ele_position = pos;
                            return;
                        }
                        pos++;
                    });
                    var h = '<li';
                    h += ' style=" top:' + (r * 166) + 'px;';
                    h += ' left:' + left + 'px; ';
                    h += 'height:25px;';
//                    if (this.Visibility != 3) {
//                        h += '"><a href="/sv/Account.mvc/LogOnForm?layout=lightbox&ReturnUrl=/sv/Tags.mvc/ViewTag/?layout=lightbox&tagid=' + this.Id + '" class="popup bgtag" ';
//                    } else {
                        h += '"><a href="/sv/Tags.mvc/ViewTag/?tagid=' + this.Id + '" class="popup bgtag" ';
                    //}
                    size = parseFloat((weight * maxlen) / maxweight);
                    if (rotate != 0)
                        h += rotate == 1 ? 'style="-moz-transform:rotate(20deg);-webkit-transform: rotate(20deg);' : 'style="-moz-transform:rotate(-20deg);-webkit-transform: rotate(-20deg);font-size:';
                    else
                        h += 'style="font-size:';
                    if (len >= 10) {
                        if (size > 30)
                            h += "20";
                        else
                            h += size < 20 ? "20" : size;
                    }
                    else
                        h += size < 20 ? "20" : size;
                    if (_bclick == undefined)
                        h += 'px;">' + this.Name;
                    else
                        h += 'px; display:block;">' + this.Name;
                    h += '</a></li>';
                    if (Math.floor((Math.random() * 2) + 0) == 0)
                        self.append(h);
                    c++;
                    if (i == 2)
                        c += 2;
                    if (c * 166 > screen.width) {
                        r++;
                        c = 1;
                    }
                });
            });
        });

        $("#pushbutton", self).unbind("click").bind("click", function (e) {
            $("#pagecontent").hide();
            $.site_closePopup();
            self.fillBackground(true);
        });
    };
})(jQuery);
