﻿/// <reference path="jquery-1.3.2.min.js"/>

/*
* Disposable() lägger till en cleanup function (till dispose) som tar
* bort bindningar från bundet element vid unload. Bindningarna som 
* inte städas bort kan leda till minnesläckor i IE. 
* (Funktionen bunden till dispose kallas av Microsoft vid cleanup)
*/
(function($) {
    jQuery.fn.Disposable = function(cln) {
        return this.each(function() {
            var el = this;
            if (!el.dispose) {
                el.dispose = cleanup; // will be called by 
                // Microsoft for cleanup
                $(window).bind("unload", cleanup);
            }
            function cleanup() {
                if (!el)
                    return;
                $(el).unbind();
                $(window).unbind("unload", cleanup);
                el.dispose = null;
                el = null;
            };
        });
    };
})(jQuery);

$(document).ready(function() {

    // photos load *************************************************************
    var _photosLink = $('a.more-photos');
    _photosLink.click(function() {
        var _lastUl = $('ul.picture:last');
        var _url = this.href;
        var _this = $(this);
        _this.text('Visa 20 fler loading ...');
        $.ajax({
            url: _url,
            success: function(_html) {
                var _holder = $('<div class="photo-holder"></div>');
                _holder.hide();
                _lastUl.after(_holder);
                _holder.html(_html);
                _holder.slideDown(800);
                _this.text('Visa 20 fler ...');
            }
        });
        return false;
    });

    // input flexible **********************************************************
    //var _form = $('.form1');
    var $submit = $('.btn-qsearch');
    var $input = $('input.text');
    var init_text = $input.val();

    $input.css({
        width: 0,
        paddinLeft: 0,
        paddinRight: 0
    }).click(function() {
        if (this.value == init_text) {
            this.value = '';
        }
    });

    $submit.click(function() {
        if ($input.is(':hidden')) {
            $input.animate({
                width: 159,
                paddinLeft: 5,
                paddinRight: 5
            }, { duration: 500, queue: false });
            return false
        }
        return true;
    });
});
function AvanSearch() {
    this.$btn = null;
    this.$txt = null;
    this.$fb = null;
    this.$ul = null;
    this.$ov = null;
    this.$more = null;
    this.scope = '';
    this.def_scope = '';
    this.userNavigated = true;
    this.histText = '';
    this.noResText = '';
}

AvanSearch.prototype.Setup = function() {
    var _as = this;
    _as.def_scope = _as.scope;

    _as.$btn.click(function(e) {
        e.preventDefault();
        txt = _as.$txt.val();
        if (txt != '') {
            _as.GetCategories(txt);
            _as.$ul.empty();
            _as.Search(txt, 1, _as.def_scope, false);
            _as.addHistoryPoint({ q: txt, s: _as.def_scope, p: 1 }, _as.histText);
        }
    });

    _as.$txt.keypress(function(e) {
        if (e.keyCode == 13) {
            e.preventDefault();
            _as.$btn.click();
        }
    });

    _as.$more.find(':first-child').click(function(e) {
    e.preventDefault();
        var index = parseInt(_as.$ul.children().size() / 10) + 1;
        var txt = _as.$txt.val();
        _as.Search(txt, index, _as.scope, false);
        _as.addHistoryPoint({ q: txt, s: _as.scope, p: index }, _as.histText);
    }).Disposable();

    Sys.Application.add_navigate(function(sender, e) {
        if (_as.userNavigated) {
            state = e.get_state();
            if (state.q) {
                txt = state.q;
                _as.$txt.val(txt);
                if (txt != '') {
                    _as.$ul.empty();
                    _as.Search(txt, state.p, state.s, true);
                    _as.GetCategories(state.q);
                }
                else {
                    _as.$fb.empty().append(_as.noResText);
                    _as.$ov.empty();
                }
            }
        }
    });

    Sys.Application._navigate(Sys.Application.get_stateString());
};

AvanSearch.prototype.addHistoryPoint = function(params, msg) {
    this.userNavigated = false;
    Sys.Application.addHistoryPoint(params, msg);
    this.userNavigated = true;
};

AvanSearch.prototype.Search = function(text, startat, scope, getall) {
    var _as = this;
    _as.scope = scope;
    $.ajax({
        type: "POST",
        url: "/Templates/Pages/SearchPage.aspx/GetResult",
        data: "{text:'" + text + "',startat:'" + startat + "',scope:'" + scope + "',getall:'" + getall + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            var hits = '';
            _as.$fb.empty();

            $.each(msg.d.L, function(i, item) {
                hits += '<li><h4><a href="' + item.P + '">' + item.T + '</a></h4><p>' + item.S + '</p><div class="breadcrumbs"><ul>' + item.B + '</ul></div></li>';
            });
            if (msg.d.C > 0) {
                $(hits).hide().appendTo(_as.$ul).slideDown('fast');
                _as.$fb.append('Sökning gav ' + (msg.d.E ? '' : 'ca ') + '<b>' + msg.d.T + '</b> träff' + (msg.d.T > 1 ? 'ar' : '') + ' på <b>' + text + '</b> från <b>' + msg.d.S + '</b>');

                $('#hiddenProp3').val(text);
                $('#hiddenProp4').val('true');
            }
            else {
                _as.$fb.append(_as.noResText);
                $('#hiddenProp3').val(text);
                $('#hiddenProp4').val('false');
            }
            SendCatalystData();
            (msg.d.C < 10 || msg.d.C == msg.d.T) ? _as.$more.hide() : _as.$more.show();

        }
    });
};
AvanSearch.prototype.GetCategories = function(text) {
    var _as = this;
    $.ajax({
        type: "POST",
        url: "/Templates/Pages/SearchPage.aspx/GetCategories",
        data: "{text:'" + text + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            var cats = '';
            $.each(msg.d, function(i, item) {
                if (item.H > 0)
                    cats += '<li><a href="' + item.S + '" rel="' + item.S + '">' + item.S + ' (' + item.H + ')</a></li>';
            });
            _as.$ov.empty().append(cats);
            _as.HookUpLinks();
        }
    });
};
AvanSearch.prototype.HookUpLinks = function(text) {
    var _as = this;
    $.each(_as.$ov.find('a'), function(i, item) {
        $(item).click(function(e) {
            txt = _as.$txt.val();
            scope = $(item).attr('rel');
            e.preventDefault();
            _as.addHistoryPoint({ q: txt, s: scope, p: 1 }, _as.histText);
            _as.$ul.hide('fast')
            _as.$ul.empty();
            _as.Search(txt, 1, scope, false);
            _as.$ul.show('fast');

        }).Disposable();
    });
};
