$(function(){
    var lun = {
        "intervalID" : null,
        "delay" : 8000,
        "the_slides" : $('#slides_wrapper img'),
        "the_text" : $('#slide_text_wrapper .slide_text'),
        "the_buttons" : $('#slide_buttons_wrapper div'),
        "move_distance" : $('#slides_wrapper img').width() + 10,
        "current_slide" : 0,
        "prev_slide" : $('#slides_wrapper img').length - 1,
        "animating" : false,
        "started" : false,
        "num_slides" : $('#slides_wrapper img').length - 1,
        
        "transition" :  function(dir){
                
            function animateText(){
                $(lun.the_text).fadeOut('fast');
                $(lun.the_text[lun.current_slide]).fadeIn('slow');
            }
            
            function updateButtons(){
                $(lun.the_buttons).removeClass('active_button');
                $(lun.the_buttons[lun.current_slide]).addClass('active_button');
            }                       

            lun.animating = true;

            if(dir === undefined){
                dir = 'forward';
            }

            ////////SLIDE INDEX LOGIC//////////////////////////////////////////////////// 

            //if not called by setInterval, stop the timer and move to appropriate slide
            if(!this.window){
                clearInterval(lun.intervalID);
                
                if(dir === 'back'){
                    if(lun.current_slide <= 0){
                        lun.current_slide = lun.num_slides;
                        lun.prev_slide = 0;                        
                    }
                    else{
                        lun.prev_slide = lun.current_slide;
                        lun.current_slide--;
                    }
                    lun.started = true;
                
                }
                
                if(dir === 'forward'){
                    if(lun.current_slide >= lun.num_slides){
                        lun.current_slide = 0;
                        lun.prev_slide = lun.num_slides;
                    }
                    else if(lun.current_slide === 0 && lun.started === false){
                        lun.started = true;
                        lun.prev_slide = lun.current_slide;
                        lun.current_slide = 1;
                    }
                    else{
                        lun.prev_slide = lun.current_slide;
                        lun.current_slide++;
                    }
                }    
                
            }
            
            //if called by setInterval, do regular progression of slides
            else{
                if(lun.current_slide >= lun.num_slides){
                    lun.prev_slide = lun.current_slide;
                    lun.current_slide = 0;
                }
                else{
                    lun.prev_slide = lun.current_slide;
                    lun.current_slide++;
                }
            //console.log(lun.current_slide);    
            }
             
            ////////ANIMATION////////////////////////////////////////////////////
            
            //slideout
            $(lun.the_slides[lun.current_slide]).animate({
                left: lun.move_distance
                }, 400, 'swing', function(){                
                animateText();
                updateButtons();
                //increment z-index
                $(lun.the_slides[lun.current_slide]).css('z-index', (parseInt($(lun.the_slides[lun.prev_slide]).css('z-index'))) + 1);
                //slide on top
                $(lun.the_slides[lun.current_slide]).animate({
                    left: 0
                },  600, 'swing', function(){
                    lun.animating = false;
                });
            });                    
        },

        ////////BUTTON HANDLING////////////////////////////////////////////////////    

        "init" : function(){
            $('#left_slide_btn').bind('click', function(){
                if(lun.animating === false){
                    lun.transition('back');
                }
            });
            
            $('#right_slide_btn').bind('click', function(){
                if(lun.animating === false){
                    lun.transition('forward');    
                }
            });
            
            lun.intervalID = setInterval(lun.transition, lun.delay);
        }
    }

    lun.init();


    //////SEARCH BLOCK////////////////////////
    $('#edit-search-block-form-1').attr('value', 'Search');
    $('#edit-search-block-form-1').bind('focus', function(){
        $(this).attr('value', '');
    });
    
    
    /////MAKE EXTERNAL LINKS OPEN IN NEW WINDOW///////////////////
    var re = new RegExp(/^http:.*/);
    $('a').each(function(i){
        if(re.test($(this).attr('href'))){
            $(this).attr('target', '_blank');
        }
    });
    
    
    /////TWEAK FRONT PAGE CALLOUT ADMIN MENU//////////////////////
    if($('#menu-overview-form').attr('action') == '/admin/build/menu-customize/menu-callout-menu'){
        $('a.tabledrag-handle').each(function(){
            $(this).remove();
        });
        
        $('#menu-overview-form td:first-child a').each(function(){
            $(this).html($(this).attr('title'));
        });
    }
    
    /////ADD RIGHT QUOTE///////////////////////////////////////////
//    if($('p.quote')){
//        $('p.quote').append('<div class="r_quote"></div>');
//    }
});
