(function(jQuery) {
    var $ = jQuery;

    $(document).ready(function() {

        var $= jQuery;
        var timeout = 5500;
        var _gaq = _gaq || [];
    
        _gaq.push(['myTracker._setAccount', 'UA-23573028-1']);
        _gaq.push(function() {
            var pageTracker = _gat._getTrackerByName('myTracker');
            var link = document.getElementById('#refSlide2 a');
            link.href = pageTracker._getLinkerUrl('https://www.kronikoensarea.net/');
        });

        $('#register a').bind('click', function(){
            _gaq.push(['_setAccount', 'UA-23573028-1']);
            _gaq.push(['_trackPageview']);
        });

        $('#LoginWithAjax_Form').bind('submit',function(){
            _gaq.push(['_setAccount', 'UA-23573028-1']);
            _gaq.push(['_trackPageview']);
        });
        
        $('#LoginWithAjax_Links_Remember').click(function(){
            _gaq.push(['_setAccount', 'UA-23573028-1']);
            _gaq.push(['_trackPageview']);
        });

        $('#refSlide1 a').click(function(){
            _gaq.push(['_setAccount', 'UA-23573028-1']);
            _gaq.push(['_trackPageview']);
        });

        $('#refSlide2 a').click(function(){
            _gaq.push(['_setAccount', 'UA-23573028-1']);
            _gaq.push(['_setDomainName', '.kronikoensarea.net']);
            _gaq.push(['_trackPageview']);
        });

        $('#refSlide3 a').click(function(){
            _gaq.push(['_setAccount', 'UA-23573028-1']);
            _gaq.push(['_setDomainName', '.kronikoensarea.net']);
            _gaq.push(['_trackPageview']);
        });

        $('#refSlide4 a').click(function(){
                _gaq.push(['_setAccount', 'UA-23573028-1']);
                _gaq.push(['_setDomainName', '.kronikoensarea.net']);
                _gaq.push(['_trackPageview']);
        });

        function updateSlider() {
            
            var mSlide = $("#refSlide"+$.currentSlide);
            if (mSlide.length == 0) {
                $.currentSlide = 1;
                var mSlide = $("#refSlide"+$.currentSlide);
            }
            
            $(".intro-bottom-box").removeClass("selected");
            mSlide.addClass("selected");
            
            $(".intro-bottom-box .border-top img").attr("src",function(idx,cur) {
                if ((idx+1) == $.currentSlide) {
                    return cur.replace(/top\.png/,"top_over.png");
                } else {
                    return cur.replace(/top_over\.png/,"top.png");
                }
            });
            
            var title     = $("h4:eq(0)",mSlide).html();
            var desc      = $("p.desc:eq(0)",mSlide).html();
            var tclass    = "sb0"+$.currentSlide;
            var bgStyle   = $("input.bgrel.selected:eq(0)",mSlide).val();
            var targetDiv = $("#intro-slider");

            $(".sb01, .sb02, .sb03, .sb04","div.slider-box").css('display','none').removeAttr('title');
            $("." + tclass, "div.slider-box").css('display','block').attr('title',tclass);
            
            $("#intro-slider").fadeOut("fast",function() {
                
                $(this).attr("style","background:"+bgStyle);
                
                $(this).fadeIn()
                $("div.slider-box",$(this)).removeClass("sb01 sb02 sb03 sb04").addClass();
                
                var selectedPhrase = $("input.phrase.selected",mSlide);

                var nextBG = (parseInt($("input.bgrel.selected:eq(0)",mSlide).attr("rel")) == $("input.bgrel",mSlide).length -1 )? 0:parseInt($("input.bgrel.selected:eq(0)",mSlide).attr("rel"))+1;

                $("input.bgrel",mSlide).removeClass("selected");
                                $("input.bgrel",mSlide).eq(nextBG).addClass("selected");
                
                $("p:eq(0)",targetDiv).html(selectedPhrase.val());
                var next = (parseInt(selectedPhrase.attr("rel")) == $("input.phrase",mSlide).length -1 )? 0:parseInt(selectedPhrase.attr("rel"))+1;
                
                selectedPhrase.removeClass("selected");
                $("input.phrase",mSlide).eq(next).addClass("selected");
                
                if (clicked) return;
                $.timer = window.setTimeout(updateSlider,timeout);
            });
            
            $("#slider-counter img").attr("src",function(idx,cur) {
                if ($("#slider-counter img").filter(":eq("+idx+")").parent("a").attr("id") == "slideSelector"+$.currentSlide) {
                    return cur.replace(/_off\./,"_on.");
                } else {
                    return cur.replace(/_on\./,"_off.");
                }
            });
            
            $.currentSlide++;
            if ($.currentSlide == 5) $.currentSlide = 1;
            clicked = false;
        }
        
        if ($("#intro-slider").length > 0) {
            
            $.currentSlide = parseInt($("#intro-slider").attr("rel").replace(/[^0-9]/,'')) + 1;
            $.timer = window.setTimeout(updateSlider,timeout);
            var clicked = false;
            
            $(".ArrowLeft img").bind("click", function(){
                var $next = $('#slider-counter .slideSelector img[src$="on.png"]').parents('li').prev('li').find('a.slideSelector');
                if ($next.length>0) {
                    $next.click();
                } else {
                    $('#slider-counter .slideSelector img[src$="on.png"]').parents('li').nextAll('li').eq(-1).find('a').click();
                }
            });
            
            $(".ArrowRight img").bind("click", function(){
                var $next = $('#slider-counter .slideSelector img[src$="on.png"]').parents('li').next('li').find('a.slideSelector');
                if ($next.length>0) {
                    $next.click();
                } else {
                    $('#slider-counter .slideSelector img[src$="on.png"]').parents('li').prevAll('li').eq(-1).find('a').click();
                }
            });
            
            $("#slider-counter .slideSelector").bind("click",function(e) {
                clicked = true;
                e.preventDefault();
                e.stopPropagation();
                $.currentSlide = ($(this).index("#slider-counter .slideSelector") + 1);
                window.clearTimeout($.timer);
                updateSlider($.currentSlide);
            });
            
            $(".intro-bottom-box").bind("click",function(e) {
                e.preventDefault();
                e.stopPropagation();
                
            });
        }
    });
})(jQuery);