/**
 * @author Alexander Farkas
 * v. 1.02
 */
(function($) {
    $.extend($.fx.step,{
        backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
            }
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

            function toArray(strg){
                strg = strg.replace(/left|top/g,'0px');
                strg = strg.replace(/right|bottom/g,'100%');
                strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
                var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
                return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
            }
        }
    });
})(jQuery);


/**
 * @author Elfet Melethron
 */
var Menu = function () {
    function Click()
    {
        atmenu = this.id;
        $.get(this.href + "?ajax=on", {}, function (data) {
            $("#ahah").html(data);
            Menu.Move(1000);
        });
        return false;
    }
    function Link(id,link) {
        atmenu = id;
        $.get(link + "?ajax=on", {}, function (data) {
            $("#ahah").html(data);
            Menu.Move(1000);
        });
        return false;
    }
    function Move(bytime)
    {
        at = GetPos(atmenu).position();
        $("#nav").stop();
        $("#nav").animate({
            backgroundPosition: '('+at.left+'px 0px)'
        }, bytime);
    }
    function GetPos(at)
    {
        var r = $("#" + at);
        return r;
    }
    return {
        Click: Click,
        Link: Link,
        Move: Move,
        GetPos: GetPos
    };
}();

var Slider = function () {
    var maxItem;
    var atItem = 1;
    function Click() {
        if(atItem == 1){
            $("#slider a.prev").addClass("inactive");
            $("#slider a.next").removeClass("inactive");
        }
        else if(atItem == maxItem) {
            $("#slider a.next").addClass("inactive");
            $("#slider a.prev").removeClass("inactive");
        }
        else {
            $("#slider a.next").removeClass("inactive");
            $("#slider a.prev").removeClass("inactive");
        }
    }
    return {
        Prev: function () {
            if(atItem > 1){
                atItem--;
                $("#slider ul#list").animate({
                    left: -(atItem-1) * $("#slider ul#list li.item").innerWidth()
                },1000);

                Click();
            }
            return false;
        },
        Next: function () {
            if(atItem < maxItem){
                atItem++;
                $("#slider ul#list").animate({
                    left: -(atItem-1) * $("#slider ul#list li.item").innerWidth()
                },1000);
                
                Click(); 
            }
            return false;
        },
        Init: function () {
            maxItem = $("#slider ul#list li.item").size();
            atItem = 1;
            
            $(window).resize(function () {
                $("#slider ul#list").stop();
                $("#slider ul#list").css({
                    left: -(atItem-1) * $("#slider ul#list li.item").innerWidth()
                },1000);
            });
        }
    };
}();

var Button = {
    Init: function (b) {
        $(b).mouseover(function()
        {
            $(this).stop(); $(this).animate({
                backgroundPosition: '(0px -1px)'
            });
        }).mouseout(function()
        {
            $(this).stop(); $(this).animate({
                backgroundPosition: '(0px -80px)'
            });
        });
    }
}

var Help = {
    Init: function (to) {
        $(to).each(function () {
            var title = $(this).attr('rel');
            if(title == undefined)
            {
                title = $(this).find(".title").html();
            }
            $(this).append('<div id="trip" class="trip_top"><div class="trip_bottom"><div class="trip_middle"><div class="trip_text">'+title+'</div></div></div></div>');
            $(this).hover(
                function () {
                    var pos = $(this).position();
                    if($.browser.msie) {
                        $(this).find('#trip')
                        .css({
                            top:pos.top,
                            left:pos.left
                        })
                        .show();
                    } else {
                        $(this).find('#trip')
                        .css({
                            top:pos.top+25,
                            left:pos.left,
                            opacity: 0
                        })
                        .show()
                        .stop()
                        .animate({
                            top:pos.top,
                            opacity: 1
                        }, "fast");
                    }

                },
                function () {
                    var pos = $(this).position();
                    if($.browser.msie) {
                        $(this).find('#trip').hide().css({
                            left:-999
                        });
                    } else {
                        $(this).find('#trip')
                        .stop()
                        .animate({
                            top:pos.top+5,
                            opacity: 0
                        }, "fast", function () {
                            $(this).hide().css({
                                left:-999
                            });
                        });
                    }
                  
                });
 
        });
    }
}

/**
 * READY
 */
$(document).ready(function ()
{
    at = Menu.GetPos(atmenu).position();
    $("#nav").css({
        backgroundPosition: at.left+'px 0px'
    });

    $(window).resize(function () {
        Menu.Move(1000);
    });

    $("a.link").click(Menu.Click);
    $("a.link").focus(function () {
        this.blur()
    });

});
