$(function(){
    $('a.abre-menu').click(function(e){
        e.preventDefault();
        $(this).parent().find('ul.sub-menu').slideToggle("slow");
    });
    $('a.abre-contato').click(function(e){
        e.preventDefault();
        $(this).parent().find('#contato-on').slideToggle("slow");
    });
    //$.nmObj({});
    $('.nyroModal').nyroModal();
});

(function($){
    
    $.fn.galeria = function() {
        var self = this;
            
        self.initialize = function(opts) {
            opts = $.extend({
                thumbs_container: '#miniatura',
                thumb_class: 'min-foto',
                image_container: '#foto-container',
                title_container: '#foto > h3:first',
                details_link: '#veja-mais',
                base_url: '',
                items: []
            }, opts);
                
            self.opts = opts;
            self.current = -1;
                
            self.data('galeria', self);
                
            if (self.opts.items.length == 0)
                return;
                
            self.createObjects();
            self.addEvents();
        }
            
        self.createObjects = function() {
            if (!self.opts.thumbs_container.jquery)
                self.opts.thumbs_container = $(self.opts.thumbs_container);
            if (!self.opts.thumb_class.jquery)
                self.opts.thumb_class = $(self.opts.thumb_class);
            if (!self.opts.image_container.jquery)
                self.opts.image_container = $(self.opts.image_container);
            if (!self.opts.title_container.jquery)
                self.opts.title_container = $(self.opts.title_container);
            if (!self.opts.details_link.jquery)
                self.opts.details_link = $(self.opts.details_link);
        }
            
        self.addEvents = function() {
            self.opts.thumbs_container.find('a').live('click', self.slotThumbClick);
            self.opts.details_link.click(self.slotDetailsLinkClick);
        }

        self.slotThumbClick = function() {
            _preventDefault.apply(this, arguments);
            var index = $(this).data('galeria-index');
            self.opts.image_container.children().hide().eq(index).show();
        }
        
        self.slotDetailsLinkClick = function() {
            _preventDefault.apply(this, arguments);
            $.nmManual($(this).attr('href'));
        }
            
        self.display = function(index) {
            var data = self.opts.items[index] ? self.opts.items[index] : null;
            if (data == null)
                return;
            self.current = index;
            self.opts.title_container.text(data.title);
            self.opts.details_link.attr('href', self.opts.base_url + data.details);
            self.opts.thumbs_container.children().remove();
            self.opts.image_container.children().remove();
            for (var i in data.images) {
                var image = data.images[i];
                if (!image.length || image.length != 4)
                    continue;
                self.createMain(self.opts.base_url + image[2], self.opts.base_url + image[3], index);
                if (data.images.length > 1)
                    self.createThumbnail(self.opts.base_url + image[0], self.opts.base_url + image[1], i);
            }
            self.opts.image_container.children().filter(':not(:first)').hide();
            self.show();
            self.opts.image_container.children().filter('.nyroModal').nyroModal();
        }
            
        self.createThumbnail = function(url_s, url_m, i) {
            var div = $('<div><a><img/></a></div>');
            if (self.opts.thumb_class)
                div.addClass(self.opts.thumb_class);
            div.find('a').attr('href', url_m).data('galeria-index', i);
            div.find('img').attr('src',url_s);
                
            div.appendTo(self.opts.thumbs_container);
        }
            
        self.createMain = function(url_l, url_o, i) {
            var a = $('<a><img/></a>');
            a.addClass('nyroModal').appendTo(self.opts.image_container);
            a.attr({
                href: url_o,
                rel: 'galeria-' + i
            });
            a.find('img').attr('src', url_l);
        }
            
        self.initialize.apply(self, arguments);
            
        return self;
    }
        
    $.fn.slider = function() {
        var self = this;
            
        self.initialize = function(opts) {
            opts = $.extend({
                left: '#slider-seta-direita',
                right: '#slider-seta-esquerda',
                mask: '#slider-mascara',
                container: '#slider-imagens',
                items: 3
            }, opts);
                
            self.opts = opts;
                
            self.createObjects();
                
            self.opts.right.css('opacity', .5);
            self.current = 0;
            
            if (self.opts.container.children().eq(1).length && self.opts.container.children().eq(1).length)
                self.item_width = self.opts.container.children().eq(1).offset().left - self.opts.container.children().eq(0).offset().left;
            else
                self.item_width = 0;
            self.item_limit = self.opts.container.children().length - self.opts.items;
            self.left_limit = self.item_width * self.item_limit;
            self.opts.container.width(self.item_width * self.opts.container.children().length);
                
            self.addEvents();
        }
            
        self.createObjects = function() {
            if (!self.opts.left.jquery)
                self.opts.left = $(self.opts.left);
            if (!self.opts.right.jquery)
                self.opts.right = $(self.opts.right);
            if (!self.opts.mask.jquery)
                self.opts.mask = $(self.opts.mask);
            if (!self.opts.container.jquery)
                self.opts.container = $(self.opts.container);
        }
            
        self.addEvents = function() {
            if (self.opts.left)
                self.opts.left.click(self.slotLeftClick);
            if (self.opts.right)
                self.opts.right.click(self.slotRightClick);
        }
            
        self.slotLeftClick = function() {
            _preventDefault.apply(this, arguments);
            var left = parseInt(self.opts.container.css('left'));
            if (left <= -self.left_limit)
                return;
            if (self.current >= self.item_limit)
                return;
            self.current++;
            var target = -(self.current * self.item_width);
            if (target <= -self.left_limit)
                self.opts.left.css('opacity', .5);
            self.opts.right.css('opacity', 1);
            self.opts.container.animate({
                left: target
            }, {
                queue: false
            });
        }
            
        self.slotRightClick = function() {
            _preventDefault.apply(this, arguments);
            var left = parseInt(self.opts.container.css('left'));
            if (left >= 0)
                return;
            if (self.current <= 0)
                return;
            self.current--;
            var target = -(self.current * self.item_width);
            if (target >= 0)
                self.opts.right.css('opacity', .5);
            self.opts.left.css('opacity', 1);
            self.opts.container.animate({
                left: target
            }, {
                queue: false
            });
        }
            
        self.initialize.apply(self, arguments);
            
        return self;
    }
    
    $.fn.magic_background = function() {
        var self = this;
        
        self.initialize = function(opts) {
            opts = $.extend({
                container: '#fotos-home',
                images: '.background-key',
                interval: 5000
            }, opts);
            
            self.opts = opts;
            self.animating = false;
            
            self.createObjects();
            
            self.image_count = self.opts.images.length;
            self.last_image = self.image_count - 1;
            
            self.imageWidth = 1800;
            self.imageHeight = 900;
            
            self.opts.images.attr('title', '').filter(':gt(0)').hide();
            
            $(window).resize(self.resizeImage);
            self.resizeImage();
            
            self.timeout_id = setTimeout(self.slotTimeout, self.opts.interval);
        }
        
        self.slotTimeout = function() {
            var image = self.opts.images.filter(':visible');
            var next = image.next();
            if (next.length != 1)
                next = self.opts.images.filter(':first');
            image.fadeOut('fast');
            next.fadeIn('fast', function(){
                clearTimeout(self.timeout_id);
                self.timeout_id = setTimeout(self.slotTimeout, self.opts.interval);
            });
        }
        
        self.createObjects = function() {
            if (!self.opts.container.jquery)
                self.opts.container = $(self.opts.container);
            if (!self.opts.images.jquery)
                self.opts.images = $(self.opts.images);
        }
        
        self.resizeImage = function() {
            var navWidth = $(window).width();
            var navHeight = $(window).height();
            
            var navRatio = navWidth / navHeight;
            var imageRatio = self.imageWidth / self.imageHeight;
            
            var newWidth, newHeight;
            if (navRatio > imageRatio) {
                newHeight = (navWidth / self.imageWidth) * self.imageHeight;
                newWidth = navWidth;
            } else {
                newHeight = navHeight;
                newWidth = (navHeight / self.imageHeight) * self.imageWidth;
            }
            
            var newTop = - ((newHeight - navHeight) / 2);
            var newLeft = - ((newWidth - navWidth) / 2);
            
            self.opts.images.css({
                top: newTop,
                left: newLeft,
                width: newWidth,
                height: newHeight
            });
            self.opts.container.css({
                width: newWidth,
                height: newHeight
            });
        }
        
        self.initialize.apply(self, arguments);

        return self;
    }

    function _preventDefault() {
        arguments[0] ? arguments[0].preventDefault() : null;
    }
})(jQuery);
