var subpage;
var AR = (function($) {
    return {
        login: function() {
            $('#signin').submit(function(){
                var user=$('#username');
                var pass=$('#password');
                var url='../_ajax/login.php';
                var pars='&username='+encodeURIComponent(user.val());
                pars+='&password='+encodeURIComponent(pass.val());
                $.post(url,pars,Response);
            });
            function Response(data){
                if(data=="OK"){
                    window.location.href='/news/';
                }else{
                    $('#log_msg').html(data);
                }
            }
        },
        setMask: function(){
            if($('.fader .fader_imgs li.current a.img_adr').attr('href')){
                $('.mask').click(function(){
                    window.location.href=$('.fader .fader_imgs li.current a.img_adr').attr('href');
                });
            }
        },
        go: function(page){
            var current=$('.fader-small .fader_imgs li.current');
            var next = $('.fader-small .fader_imgs li:nth-child('+page+')');
            current.fadeOut("fast",function(){
                next.fadeIn('fast').addClass('current');
                $('.pagination li.selected').removeClass('selected');
                $('.pagination li:nth-child('+page+')').addClass('selected');
                $('.fader_msg').html(next.find('.img_msg').html());
            }).removeClass('current');
            window.clearInterval(subpage);
            subpage = window.setInterval( function(){
                AR.faderAnim('.fader-small')
            }, 5000 );
            return false;
        },
        faderAnim: function(fader){
            var active = $(fader+' .fader_imgs li.current');

            if ( active.length == 0 ) active = $(fader+' .fader_imgs li:last');

            var next =  active.next().length ? active.next() : $(fader+' .fader_imgs li:first');

            if($('.pagination').length!=0){
                var now = $('.pagination li.selected');
                if ( now.length == 0 ) now = $('.pagination li:last');
                var pagn =  now.next().length ? now.next() : $('.pagination li:first');
            }
            active.fadeOut(2000,function(){
                active.removeClass('current');
                if($('.pagination').length!=0){
                    now.removeClass('selected');
                    pagn.addClass('selected');
                    $('.fader_msg').html(next.find('.img_msg').html());
                }
            });
            next.delay(600).fadeIn(2000,function(){
                next.addClass('current');
                AR.setMask();
            });
        },
        mycarousel_initCallback: function(carousel)
        {
            // Pause autoscrolling if the user moves with the cursor over the clip.
            carousel.clip.hover(function() {
                carousel.stopAuto();
            }, function() {
                carousel.startAuto();
            });

            jQuery('.arr_right').bind('click', function() {
                carousel.next();
                carousel.startAuto(0);
                return false;
            });

            jQuery('.arr_left').bind('click', function() {
                carousel.prev();
                carousel.startAuto(0);
                return false;
            });
        },
        stupidIE6: function(){
            $(".menu li").hover(
                function () {
                    $(this).children('ul').show();
                },
                function () {
                    $(this).children('ul').hide();
                }
                );
        },
        init_main: function() {
            $(document).ready(function(){
                AR.login();
                /* $("#commentForm").validate(); */
					 
                if($('#accordion').length>0){
                    /* Accordion menu: http://docs.jquery.com/UI/Accordion */
                    $("#accordion").accordion({
                        icons: {
                            'header': 'ui-icon-plus',
                            'headerSelected': 'ui-icon-minus'
                        },
                        autoHeight: false,
                        collapsible: true
                    });
                }
					
                AR.setMask();
                
                if($('.fader_imgs li').length>1){ //if there is more than one image specified
                
                    window.setInterval( function(){ //fade the banner (home page)
                        AR.faderAnim('.fader')
                    }, 6000 ); // seconds

                    subpage = window.setInterval( function(){ // fade the banner (inside page)
                        AR.faderAnim('.fader-small')
                    }, 6000 ); //seconds
                }
                
                if($('.slide_imgs').length>0)
                    jQuery('.slide_imgs').jcarousel({
                        scroll: 4, //4 imgs at a time
                        auto: 6, //scroll every x seconds automatically
                        wrap: "circular",
                        buttonNextHTML: null,
                        animation: 2250, //duration of slide transition
                        buttonPrevHTML: null,
                        initCallback: AR.mycarousel_initCallback
                    });

                $("a[rel^='lightbox']").prettyPhoto({
                    theme: 'dark_square'
                }); /* light_rounded / dark_rounded / light_square / dark_square / facebook */

                //for IE6 we make the menu work!
                if(typeof document.body.style.maxHeight === "undefined")
                    AR.stupidIE6();
					
                $("#login-but").click(function(e) {
                    e.preventDefault();
                    $("fieldset#signin_menu").toggle();
                    $("#login-but").toggleClass("menu-open");
                });
						
                $("fieldset#signin_menu").mouseup(function() {
                    return false
                });
						
                $(document).mouseup(function(e) {
                    if($(e.target).parent("a#login-but").length==0) {
                        $("#login-but").removeClass("menu-open");
                        $("fieldset#signin_menu").hide();
                    }
                });

            });
        }
    };
// Pass in jQuery.
})(jQuery);
AR.init_main();


