var interval = timeout = loading = false;
var view = 0, prev = 0;
var mnu_running = mnu_timeout = vars_defined = false;
var loaded = [];

function rightBannerPos() {
    if ($('#r_banner').length) {
        var browserwidth = $(window).width();
        var i_w = 0;
        
        $('.sf-menu li ul:visible').each(function(i, o) {
            i_w = i_w + parseInt($(o).width()) + 1;
        });
        
        $('#r_banner').css({
            'width': (browserwidth),
            'right': - (browserwidth) + 576 - i_w
        });
    }
}

(function($){
    
    $.fn.fullscreen = function() {
        
        var options = $.extend($.fn.fullscreen.defaults, $.fn.fullscreen.options);
        var con = $.fn.fullscreen.container = $(this);
        
        if (options.images.length > 1) {
            $(document).keyup(function(event){
                if (event.keyCode == 37) {
                    go_left();
                }
                if (event.keyCode == 39) {
                    go_right();
                }
            });
        }
        
        if (options.slideshow == 1 && con.children('img').length > 1) {
            if (options.images.length > 0) {
                //preload and insert images
                $(window).bind('load', function() {
                    $.cacheImage(options.images, {
                        load : function (e) {
                            $(this).appendTo(con);
                            con.adjustSize(); 
                        }
                    });
                    /*
                    if ($('#image_thumbs')) {
                        var j = $('#image_thumbs').find('a').length;
                        $.create('a', {
                            'href': '#', 
                            'id': 'imtmb'+j, 
                            'onclick': 'return false;'
                        }).appendTo($('#image_thumbs')).bind("click", function(e) {
                            jumpToImage(j);
                            return false;
                        }).hide().fadeIn('slow');
                    }
                    */
                });
            }
            $.fn.autoplay();
        }
        
        $(document).ready(function() {
            con.adjustSize(); 
        });
        $(window).bind("resize", function(){
            con.adjustSize(); 
        });
        
        if ($('#toggle_left').length) {
            $('#toggle_left').hover(
                function() { $(this).animate({ 'width': '48px', 'left': '16px' }, {queue: false, duration: 120}); },
                function() { $(this).animate({ 'width': '28px', 'left': '26px' }, {queue: true, duration: 80}); }
            );
        }
        if ($('#toggle_right').length) {
            $('#toggle_right').hover(
                function() { $(this).animate({ 'width': '48px', 'right': '16px' }, {queue: false, duration: 120}); },
                function() { $(this).animate({ 'width': '28px', 'right': '26px' }, {queue: true, duration: 80}); }
            );
        }
        
        if ($('#menu_toggler').length && $('#menu_toggler').is(':visible')) {
            $('#menu_toggler').bind('click', function() {
                if (!$("#inner_wrap").hasClass('hidden')) {
                    hideMenu();
                } else {
                    showMenu();
                }
            });
            
            if ($('#menu_toggler').hasClass('autohide')) {
                if (!mnu_timeout) {
                    mnu_timeout = window.setTimeout("hideMenu()", 520);
                }
            }
        }
        
        if (options.slidemenu === true) {
            
            $().mousemove(function(e){
                var h = $('#wrap').height();
                var w = $('#wrap').width();
                var x = e.pageX;
                var y = e.pageY;
                var m = ($('#bottom_nav').outerHeight(true) * 1.1) + ($('#inner').length ? $('#inner').outerHeight(true) : 0);
                var in_area = 0;
                
                if (y < h - m) 
                { // (y/h) < (5/8)
                    if (mnu_running === false && ($("#inner_wrap").hasClass('visible') || !vars_defined)) {
                        if (!mnu_timeout) {
                            mnu_timeout = window.setTimeout("hideMenu()", 520);
                        }
                    }
                    
                    if ($('#background-togglers').length) {
                        var area = h - m;
                        if (y > 50 && y/area <= 7/8) {
                            if (x/w < 1/4) {
                                in_area = -1;
                            }
                            else if (x/w > 3/4) {
                                in_area = 1;
                            }
                        } else {
                            in_area = 0;
                        }
                    }
                }
                else 
                {
                    in_area = 0;
                    if (mnu_running === false && ($("#inner_wrap").hasClass('hidden') || mnu_timeout || !vars_defined)) {
                        window.clearTimeout(mnu_timeout);
                        mnu_timeout = false;
                        showMenu();
                    }
                }
                
                if ($('.disable_overlay:visible').length) { in_area = 0; }
                
                if (in_area != 0) {
                    if (in_area < 0) {
                        if(!$('#toggle_left').hasClass('hov')) {
                            $('#toggle_right').animate({ 'width': '28px', 'right': '26px' }, {queue: false, duration: 80});
                            $('#toggle_left').animate({ 'width': '48px', 'left': '36px' }, {queue: false, duration: 120});
                        }
                        
                        $('#toggle_right').removeClass('hov');
                        $('#toggle_left').addClass('hov');
                        
                        $('#toggle_left').bind('click', none);
                        
                        $('body').unbind('click', go_right);
                        $('body').bind('click', go_left);
                    } else {
                        if(!$('#toggle_right').hasClass('hov')) {
                            $('#toggle_right').animate({ 'width': '48px', 'right': '36px' }, {queue: false, duration: 120});
                            $('#toggle_left').animate({ 'width': '28px', 'left': '26px' }, {queue: false, duration: 80});
                        }
                        
                        $('#toggle_left').removeClass('hov');
                        $('#toggle_right').addClass('hov');
                        
                        $('#toggle_right').bind('click', none);
                        
                        $('body').unbind('click', go_left);
                        $('body').bind('click', go_right);
                    }
                    
                    $('body').css('cursor', 'pointer');
                } else {
                    $('body').unbind('click', go_left);
                    $('body').unbind('click', go_right);
                    $('body').css('cursor', 'default');
                    
                    $('#toggle_right').unbind('click', none);
                    $('#toggle_left').unbind('click', none);
                    
                    if ($('#background-togglers .hov').length) {
                        $('#background-togglers .hov').removeClass('hov');
                        $('#toggle_right').animate({ 'width': '28px', 'right': '26px' }, {queue: false, duration: 80});
                        $('#toggle_left').animate({ 'width': '28px', 'left': '26px' }, {queue: false, duration: 80});
                    }
                }
                
            });
        }
    };
    
    $.fn.autoplay = function() {
        var options = $.extend($.fn.fullscreen.defaults, $.fn.fullscreen.options);
        interval = window.setInterval("startSlideshow()", options.slideinterval);
    }
    
    $.fn.adjustSize = function() {
        
        var options = $.extend($.fn.fullscreen.defaults, $.fn.fullscreen.options);
        
        return this.each(function() {
            
            //Define image ratio
            var ratio = options.startheight/options.startwidth;
            
            //Gather browser and current image size
            var imagewidth = $(this).width();
            var imageheight = $(this).height();
            var browserwidth = $(window).width();
            var browserheight = $(window).height();
            
            if ($('#l_banner').length) {
                $('#l_banner').css({
                    'width': (browserwidth / 2),
                    'left': -(browserwidth / 2)
                });
            }
            
        
            rightBannerPos();
            
            // Check ratio
            if ((browserheight/browserwidth) > ratio){
                $(this).height(browserheight);
                $(this).width(browserheight / ratio);
                $(this).children().height(browserheight);
                $(this).children().width(browserheight / ratio);
            } else {
                $(this).width(browserwidth);
                $(this).height(browserwidth * ratio);
                $(this).children().width(browserwidth);
                $(this).children().height(browserwidth * ratio);
            }
            $(this).children().css('left', (browserwidth - $(this).width())/2);
            $(this).children().css('top', (browserheight - $(this).height())/2);
            
            return false;
        });
    };
    
    $.fn.fullscreen.defaults = { 
        startwidth: 1024,
        startheight: 768,
        slideshow: 0,
        slideinterval: 5000,
        duration: false,
        wait: 10000,
        slidemenu: false,
        images: []
    };
    
    $.fn.fullscreen.container = null;
    
})(jQuery);

function toggleSlideshow() {
    if (interval !== false) {
        $('#playpause').removeClass('playing');
        stopSlideshow();
    } else {
        $('#playpause').addClass('playing');
        $.fn.autoplay();
    }
    return false;
}

function stopSlideshow() {
    window.clearInterval(interval);
    window.clearTimeout(timeout);
    interval = timeout = false;
}
//Slideshow

function go_right() {
    startSlideshow(1,false,true);
}

function go_left() {
    startSlideshow(-1,false,true);
}

function none() {
    return false;
}

function startSlideshow(direction, duration, stopauto, jump) {
    var con = $.fn.fullscreen.container;
    var options = $.extend($.fn.fullscreen.defaults, $.fn.fullscreen.options);
    
    if (options.images.length > 1) {
        
        duration = typeof duration == 'number' ? duration : options.duration;
        jump = jump == true ? true : false;
        direction = typeof direction == 'number' ? direction : 1;
        
        if (typeof stopauto != 'undefined' && stopauto == true && interval !== false) {
            window.clearInterval(interval);
            window.clearTimeout(timeout);
            timeout = window.setTimeout("$.fn.autoplay()", options.wait);
        }
        
        var currentslide = con.find('.activeslide');
        var tmb = false;
        
        if (currentslide.length == 0) currentslide = con.find(':last');
        prev = view;
        if (jump === true) 
        {            
            view = direction < options.images.length && direction > 0 ? direction : 0;
        } 
        else 
        {
            if (direction >= 0) {
                view = (view + 1) < options.images.length ? (view + 1) : 0;
            } else {
                view = (view - 1) >= 0 ? (view - 1) : (options.images.length - 1);
            }
        }
        
        if ($('#image_thumbs')) {
            var n_tmb = $('#image_thumbs').find('a:not(#playpause):eq('+view+')');
            tmb = n_tmb.length ? n_tmb : $('#image_thumbs').find(':first');
        }
        
        var nextslide = currentslide.next().length ? currentslide.next() : con.find(':first');
        var prevslide = nextslide.prev().length ? nextslide.prev() : con.find(':last');
    
        if (prev != view) {
            var img_src = options.images[view];
            if (jQuery.inArray(options.images[view], loaded) < 0) {
                loading = true;
                $('#loading').show();
                $.cacheImage(img_src, {
                    load : function (e) {
                        loaded.push($(this).attr('src'));
                        fireBackground(currentslide, nextslide, prevslide, tmb, duration, $(this).attr('src'));
                        loading = false;
                        $('#loading').hide();
                    }
                });
            } else {
                fireBackground(currentslide, nextslide, prevslide, tmb, duration, img_src);
            }
        }
    }
}

function fireBackground(currentslide, nextslide, prevslide, tmb, duration, img_src) {
    var con = $.fn.fullscreen.container;
    nextslide.attr('src', img_src);
    con.adjustSize();
    
    currentslide.removeClass('activeslide');
    
    if (duration === false) {
        nextslide.addClass('activeslide');
    } else {
        nextslide.hide().addClass('activeslide').fadeIn(duration);
    }
    $('.prevslide').removeClass('prevslide');
    prevslide.addClass('prevslide');
    if (tmb !== false) {
        $('#image_thumbs .active').removeClass('active');
        tmb.addClass('active');
    }
}

function jumpToImage(num) {
    duration = false;
    num = typeof num == 'number' ? num : 0;
    
    startSlideshow(num, duration, true, true);
}

var i_margin = 0;

function showMenu() {
    mnu_running = true;
    
    $("#inner_wrap").stop();
    $("#inner_wrap").find("ul:first").show().fadeTo(200,1);
    
    if ($('.menu-pushup').length) {
        $('.menu-pushup').css('marginBottom', i_margin);
    }
    if ($('#menu_toggler').length) {
        $('#menu_toggler').removeClass('hidden');
    }
    
    if ($('#inner').length) {
        $('#inner').animate({ 'paddingBottom': 0 }, 120);
    }
    $("#inner_wrap").css('zIndex', 1000);
    $('#pasd, #menu_toggler').animate({ 'top': 0 }, 240, "linear", $("#inner_wrap").animate({ 
        bottom: "0px"
    }, 200, "linear", function() {
        mnu_running = false;
        $("#inner_wrap").removeClass('hidden');
        $("#inner_wrap").addClass('visible');
        vars_defined = true;
    }));
    $('#lightbox_footer').animate({ 'bottom': '-100%' }, 240, "linear");
}

function hideMenu() {
    mnu_running = true;
    
    $("#inner_wrap").stop();
    $("#inner_wrap").find("ul:first").fadeTo(200,0, function() { $(this).hide(); } );
    if ($('#inner').length) {
        $('#inner').animate({ 'paddingBottom': 14 }, 120);
    }
    
    if ($('.menu-pushup').length) {
        i_margin = $('.menu-pushup').css('marginBottom');
        $('.menu-pushup').css('marginBottom', 0);
    }
    if ($('#menu_toggler').length) {
        $('#menu_toggler').addClass('hidden');
    }
    
    $("#inner_wrap").animate({ 
        bottom: - $('#bottom_nav').outerHeight(true)
    }, 300, "linear", function() {
        mnu_running = false;
        if ($('#inner').length) {
            $('#inner').css('opacity', 1);
        }
        $("#inner_wrap").css('zIndex', 1);
        $("#inner_wrap").removeClass('visible');
        $("#inner_wrap").addClass('hidden');
        vars_defined = true;
        $('#lightbox_footer').animate({ 'bottom': 0 }, 120, "linear");
        $('#pasd, #menu_toggler').animate({ 'top': - 54 }, 240);
    });
}
