var openForgotEmail, closeForgotEmail, loginForm;
/// <reference path="jquery-1.5.2.min.js" />

$(document).ready(function ()
{
    $('div.gallery').gallery({
        duration: 500,
        listOfSlides: '.gallery-inf > ul > li',
        switcher: '.box-text ul>li'
    });
    /* fonqi js */
    loginForm = $("#memberLogin");
    $("#openForgotPassword", loginForm).unbind().click(openForgotEmail);
    $("#loginSubmit", loginForm).data("defaultVal", $("#loginSubmit", loginForm).val());
    $(".tipsy-trigger").tipsy({ title: 'alt', fade: true, gravity: $.fn.tipsy.autoNE });
    $("#siteSearch").keyup(function (e)
    {
        e.preventDefault();
        var query = $(this).val();
        if (e.keyCode == 13 && !$.trim(query) == "") {
            location.href = "/soegeresultater?query=" + query;
        }

    }).defaultInput();
    $(".search-blogs, .search-blog").submit(function (e)
    {
        return false;
    });
    $(".search-blogs input[type=text]").keyup(function (e)
    {
        e.preventDefault();
        var query = $(this).val();

        if (e.keyCode == 13 && !$.trim(query) == "") {
            $(".block-post").html("<p><img class='loader-img' src='/images/ajax-loader.gif' alt=''></p>");
            $.ajax({
                url: "/forside/SearchPosts?query=" + query,
                success: function (data)
                {
                    if ($.trim(data) != "") {
                        $(".block-post").html(data);
                    }
                    else {
                        $(".block-post").html("<p>Ingen resultater fundet på: '" + query + "'</p>");
                    }
                }
            });
        }
    }).defaultInput();

    $(".search-blog input[type=text]").keyup(function (e)
    {
        e.preventDefault();
        var query = $(this).val();

        if (e.keyCode == 13 && !$.trim(query) == "") {
            $(".block-post").html("<p><img class='loader-img' src='/images/ajax-loader.gif' alt=''></p>");
            var blogId = $("#blogId").val();
            $.ajax({
                url: "/forside/SearchPosts?blog=" + blogId + "&query=" + query,
                success: function (data)
                {
                    if ($.trim(data) != "") {
                        $(".block-post").html(data);
                    }
                    else {
                        $(".block-post").html("<p>Ingen resultater fundet på: '" + query + "'</p>");
                    }
                }
            });
        }
    }).defaultInput();
    /* fonqi js */
});

openForgotEmail = function (e) {
    e.preventDefault();
    $(".forgotEmail", loginForm).addClass("open");
    $("#loginSubmit", loginForm).val("Send");
    $(".frame .row:lt(2)", loginForm).hide();
    $("#openForgotPassword").unbind().click(closeForgotEmail);
};

closeForgotEmail = function (e) {
    e.preventDefault();
    $(".forgotEmail", loginForm).removeClass("open");
    $(".frame .row:lt(2)", loginForm).show();
    $("#loginSubmit", loginForm).val($("#loginSubmit", loginForm).data("defaultVal"));
    $("#openForgotPassword").unbind().click(openForgotEmail);
};

(function ($) {
    $.fn.gallery = function (options) {
        var args = Array.prototype.slice.call(arguments);
        args.shift();
        this.each(function () {
            if (this.galControl && typeof options === 'string') {
                if (typeof this.galControl[options] === 'function') {
                    this.galControl[options].apply(this.galControl, args);
                }
            } else {
                this.galControl = new Gallery(this, options);
            }
        });
        return this;
    };

    function Gallery(context, options) {
        this.init(context, options);
    }
    Gallery.prototype = {
        options: {},
        init: function (context, options) {
            this.options = $.extend({
                duration: 700,
                slideElement: 1,
                autoRotation: false,
                effect: false,
                listOfSlides: '.list > li',
                switcher: false,
                autoSwitcher: false,
                disableBtn: false,
                nextBtn: 'a.link-next, a.btn-next, a.next',
                prevBtn: 'a.link-prev, a.btn-prev, a.prev',
                circle: true,
                clone: false,
                direction: false,
                event: 'click',
                IE: false
            }, options || {});
            var self = this;
            this.context = $(context);
            this.els = this.context.find(this.options.listOfSlides);
            this.list = this.els.parent();
            this.count = this.els.length;
            this.autoRotation = this.options.autoRotation;
            this.direction = this.options.direction;
            this.duration = this.options.duration;
            if (this.options.clone) {
                this.list.append(this.els.clone());
                this.list.prepend(this.els.clone());
                this.els = this.context.find(this.options.listOfSlides);
            }
            this.wrap = this.list.parent();
            if (this.options.nextBtn) {
                this.nextBtn = this.context.find(this.options.nextBtn);
            }
            if (this.options.prevBtn) {
                this.prevBtn = this.context.find(this.options.prevBtn);
            }

            this.calcParams(this);

            if (this.options.autoSwitcher) {
                this.switcherHolder = this.context.find(this.options.switcher).empty();
                this.switchPattern = $('<ul class="' + (this.options.autoSwitcher === true ? '' : this.options.autoSwitcher) + '"></ul>');
                for (var i = 0; i < this.max + 1; i++) {
                    $('<li><a href="#">' + i + '</a></li>').appendTo(this.switchPattern);
                }
                this.switchPattern.appendTo(this.switcherHolder);
                this.switcher = this.context.find(this.options.switcher).find('li');
                this.active = 0;
            } else {
                if (this.options.switcher) {
                    this.switcher = this.context.find(this.options.switcher);
                    this.active = this.switcher.index(this.switcher.filter('.active:eq(0)'));
                }
                else this.active = this.els.index(this.els.filter('.active:eq(0)'));
            }
            if (this.active < 0) this.active = 0;
            this.last = this.active;
            if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
            if (this.options.clone) this.active += this.count;

            if (this.options.effect) this.els.css({
                opacity: 0
            }).removeClass('active').eq(this.active).addClass('active').css({
                opacity: 1
            }).css('opacity', 'auto');
            else {
                if (this.direction) this.list.css({
                    marginTop: -(this.mas[this.active])
                });
                else this.list.css({
                    marginLeft: -(this.mas[this.active])
                });
            }


            if (this.options.nextBtn) this.initEvent(this, this.nextBtn, true);
            if (this.options.prevBtn) this.initEvent(this, this.prevBtn, false);

            this.initWindow(this, $(window));

            if (this.autoRotation) this.runTimer(this);

            if (this.options.switcher) this.initEventSwitcher(this, this.switcher);
            if (this.options.disableBtn && !this.options.circle && !this.options.clone) this.disableControls();
        },
        calcParams: function (self) {
            this.mas = [];
            this.sum = 0;
            this.max = this.count - 1;
            this.width = 0;
            this.els.each(function () {
                self.mas.push(self.width);
                self.width += self.direction ? $(this).outerHeight(true) : $(this).outerWidth(true);
                self.sum += self.direction ? $(this).outerHeight(true) : $(this).outerWidth(true);
            });
            this.finish = this.direction ? this.sum - this.wrap.outerHeight() : this.sum - this.wrap.outerWidth();
            for (var i = 0; i < this.count; i++) {
                if (this.mas[i] >= this.finish) {
                    this.max = i;
                    break;
                }
            }
        },
        changeSettings: function (set, val) {
            this[set] = val;
        },
        fadeElement: function () {
            this.els.eq(this.last).animate({
                opacity: 0
            }, {
                queue: false,
                duration: this.duration
            });
            this.els.removeClass('active').eq(this.active).addClass('active').animate({
                opacity: 1
            }, {
                queue: false,
                duration: this.duration,
                complete: function () {
                    $(this).css('opacity', 'auto');
                }
            });
            if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
            this.last = this.active;
        },
        scrollElement: function (f) {
            if (this.direction) this.list.animate({
                marginTop: f ? -this.finish : -(this.mas[this.active])
            }, {
                queue: false,
                duration: this.duration
            });
            else this.list.animate({
                marginLeft: f ? -this.finish : -(this.mas[this.active])
            }, {
                queue: false,
                duration: this.duration
            });
            if (this.options.switcher) this.switcher.removeClass('active').eq(this.options.clone ? this.active < this.count ? this.active / this.options.slideElement : this.active >= this.count * 2 ? (this.active - this.count * 2) / this.options.slideElement : (this.active - this.count) / this.options.slideElement : this.active / this.options.slideElement).addClass('active');
        },
        runTimer: function ($this) {
            if ($this._t) clearTimeout($this._t);
            $this._t = setInterval(function () {
                $this.nextStep();
            }, this.autoRotation);
        },
        initEventSwitcher: function ($this, el) {
            el.bind($this.options.event, function () {
                if (!$(this).hasClass('active')) {
                    $this.active = $this.switcher.index($(this)) * $this.options.slideElement;
                    if ($this.options.clone) $this.active += $this.count;
                    $this.initMove();
                }
                return false;
            });
        },
        initEvent: function ($this, addEventEl, dir) {
            addEventEl.bind($this.options.event, function () {
                if (dir) $this.nextStep();
                else $this.prevStep();
                if ($this._t) clearTimeout($this._t);
                if ($this.autoRotation) $this.runTimer($this);
                return false;
            });
        },
        disableControls: function () {
            this.prevBtn.removeClass(this.options.disableBtn);
            this.nextBtn.removeClass(this.options.disableBtn);
            if (this.active >= this.max) this.nextBtn.addClass(this.options.disableBtn);
            if (this.active <= 0) this.prevBtn.addClass(this.options.disableBtn);
        },
        initMove: function () {
            var f = false;
            if (this.active >= this.max && !this.options.clone) {
                f = true;
                this.active = this.max;
            }
            if (this._t) clearTimeout(this._t);
            if (!this.options.effect) this.scrollElement(f);
            else this.fadeElement();
            if (this.autoRotation) this.runTimer(this);
            if (this.options.disableBtn && !this.options.circle && !this.options.clone) this.disableControls();
        },
        nextStep: function () {
            var f = false;
            this.active = this.active + this.options.slideElement;
            if (this.options.disableBtn && !this.options.circle && !this.options.clone) this.disableControls();
            if (this.options.clone) {
                if (this.active > this.count * 2) {
                    if (this.direction) this.list.css({
                        marginTop: -this.mas[this.count]
                    });
                    else this.list.css({
                        marginLeft: -this.mas[this.count]
                    });
                    this.active = this.count + this.options.slideElement;
                }
            } else {
                if (this.active >= this.max) {
                    if (this.options.circle) {
                        if (this.active > this.max) this.active = 0;
                        else {
                            this.active = this.max;
                            f = true;
                        }
                    }
                    else {
                        this.active = this.max;
                        f = true;
                    }
                }
            }
            if (!this.options.effect) this.scrollElement(f);
            else this.fadeElement();
        },
        prevStep: function () {
            var f = false;
            this.active = this.active - this.options.slideElement;
            if (this.options.disableBtn && !this.options.circle && !this.options.clone) this.disableControls();
            if (this.options.clone) {
                if (this.active < 0) {
                    if (this.direction) this.list.css({
                        marginTop: -this.mas[this.count]
                    });
                    else this.list.css({
                        marginLeft: -this.mas[this.count]
                    });
                    this.active = this.count - 1;
                }
            } else {
                if (this.active < 0) {
                    if (this.options.circle) {
                        this.active = this.max;
                        f = true;
                    }
                    else this.active = 0;
                }
            }
            if (!this.options.effect) this.scrollElement(f);
            else this.fadeElement();
        },
        initWindow: function ($this, $window) {
            $window.focus($.proxy(this.play, this));
            $window.blur($.proxy(this.stop, this));
        },
        stop: function () {
            if (this._t) clearTimeout(this._t);
        },
        play: function () {
            if (this._t) clearTimeout(this._t);
            if (this.autoRotation) this.runTimer(this);
        }
    };
} (jQuery));

function initPage() {
//    clearFormFields({
//        clearInputs: true,
//        clearTextareas: true,
//        passwordFieldText: true,
//        addClassFocus: "focus",
//        filterClass: "default"
//    });
}

/*function clearFormFields(o) {
    if (o.clearInputs === null) o.clearInputs = true;
    if (o.clearTextareas === null) o.clearTextareas = true;
    if (o.passwordFieldText === null) o.passwordFieldText = false;
    if (o.addClassFocus === null) o.addClassFocus = false;
    if (!o.filter) o.filter = "default";
    if (o.clearInputs) {
        var inputs = document.getElementsByTagName("input");
        for (var i = 0; i < inputs.length; i++) {
            if ((inputs[i].type == "text" || inputs[i].type === "password") && inputs[i].className.indexOf(o.filterClass)) {
                inputs[i].valueHtml = inputs[i].value;
                inputs[i].onfocus = function () {
                    if (this.valueHtml === this.value) this.value = "";
                    if (this.fake) {
                        inputsSwap(this, this.previousSibling);
                        this.previousSibling.focus();
                    }
                    if (o.addClassFocus && !this.fake) {
                        this.className += " " + o.addClassFocus;
                        this.parentNode.className += " parent-" + o.addClassFocus;
                    }
                };
                inputs[i].onblur = function () {
                    if (this.value === "") {
                        this.value = this.valueHtml;
                        if (o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
                    }
                    if (o.addClassFocus) {
                        this.className = this.className.replace(o.addClassFocus, "");
                        this.parentNode.className = this.parentNode.className.replace("parent-" + o.addClassFocus, "");
                    }
                };
                if (o.passwordFieldText && inputs[i].type == "password") {
                    var fakeInput = document.createElement("input");
                    fakeInput.type = "text";
                    fakeInput.value = inputs[i].value;
                    fakeInput.className = inputs[i].className;
                    fakeInput.fake = true;
                    inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
                    inputsSwap(inputs[i], null);
                }
            }
        }
    }
    if (o.clearTextareas) {
        var textareas = document.getElementsByTagName("textarea");
        for (var j = 0; j < textareas.length; j++) {
            if (textareas[j].className.indexOf(o.filterClass)) {
                textareas[j].valueHtml = textareas[j].value;
                textareas[j].onfocus = function () {
                    if (this.value == this.valueHtml) this.value = "";
                    if (o.addClassFocus) {
                        this.className += " " + o.addClassFocus;
                        this.parentNode.className += " parent-" + o.addClassFocus;
                    }
                };
                textareas[j].onblur = function () {
                    if (this.value === "") this.value = this.valueHtml;
                    if (o.addClassFocus) {
                        this.className = this.className.replace(o.addClassFocus, "");
                        this.parentNode.className = this.parentNode.className.replace("parent-" + o.addClassFocus, "");
                    }
                };
            }
        }
    }

    function inputsSwap(el, el2) {
        if (el) el.style.display = "none";
        if (el2) el2.style.display = "inline";
    }
}*/
if (window.addEventListener) {
    window.addEventListener("load", initPage, false);
}
else if (window.attachEvent) {
    window.attachEvent("onload", initPage);
}
/**
* jQuery Cookie plugin
*
* Copyright (c) 2010 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires,
                t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IEoptions.path ? '; path=' + options.path : '',options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
            ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ?
    function (s) {
        return s;
    } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

jQuery.fn.extend({
    defaultInput: function () {
        return $(this).each(function () {
            var defaultText = $(this).val();
            $(this).focus(function () {
                if ($(this).val() == defaultText) {
                    $(this).val("");
                }
            });
            $(this).blur(function () {
                if ($.trim($(this).val()) === "") {
                    $(this).val(defaultText);
                }
            });
        });
    }
});
