var application = {
    menu: function() {
        var speed = 350;
        $('.main-menu > ul > li:first').attr('rel', 'menu-showroom');
        $('.main-menu > ul > li').each(function(){
            var link = $(this);
            var subMenu = link.children('ul');
            var rel = link.attr('rel') ? $('.'+$(this).attr('rel')) : null ;
            var timer = null;
            $(this).hover(
                function(){
                    link.addClass('hover');
                    subMenu.slideDown(speed);
                    if (rel && !timer)
                        rel.slideDown(speed);
                    clearTimeout(timer);
                    timer = null;
                },
                function(){
                    link.removeClass('hover');
                    subMenu.hide();
                        if (rel) rel.hide();
                    clearTimeout(timer);
                    timer = null;
                }
            );
            if (rel)
                rel.hover(
                    function(){ link.addClass('hover'); rel.show(); },
                    function(){ timer = setTimeout(function(){link.removeClass('hover'); rel.hide(); timer=null;}, 100); }
                );
        });
    },
    characterictics: function()
    {
        $('.charact_item').each(function(){
            var targ = $(this).children('div');
            if (targ.length == 2)
                $(this).children('a').click(function(){$(this).toggleClass('open'); targ.toggle(0)});
        });
    },
    table: function()
    {
        $('table tr td:nth-child(odd)').addClass('td-odd');
    },
    showrooms: function()
    {
        var hash = window.location.hash.substring(1);
        var active = null;
        $('.h-info a').each(function(){
            var rel = $(this).attr('rel');
            if (rel)
            {
                $(this).click(function(){
                    
                    if (active != rel){
                        if (active)
                            $('#'+active).hide();
                        $('#'+rel).removeClass('map-row').show();
                    } else
                        $('#'+rel).removeClass('map-row').toggle();
                    active = rel;
                    return false;
                });
            }
            if (hash && $(this).parents('tr').attr('id') == hash){
                $(this).click();
                hash = null;
            }
        });
    },
    insertData: function()
    {
    
    },
    cuSel: function()
    {
        var base = document.location.href.replace(/nov\.|nsk\.|br\.|www\./g, ''); /** added by psp */
        if ((hash = window.location.hash.substring(1))) {
            $('.c-sel option[rel="'+hash+'"]').attr('selected', 'selected');
        }
        
        $('.c-sel select').change(function(){
            if ($(this).val()) {
        	var $t = $(this);
        	var _href = base;
        	if ($t.val().replace(/\//g,'').length > 0) {
        	    _href = $t.val();
        	}
                location.href = _href;
            }
        });
    },
    lavaLamp: function(){
        if (!(menu = $(".car-menu ul")).length)
            return;
        
        if (!menu.children('.current').length)
            $('<li>', {'class': 'current hidden'}).appendTo(menu);
        
        $(".car-menu ul").lavaLamp({
            fx: 'easeInOutExpo',
			speed: 600
        });
    },
    gallery: function()
    {
        var self = this;
        var gallery = $('.gallery');
        var slider = gallery.find('.slider');
        var mainImg = $('.main-img .car-img');
        
        if (!gallery.length)
            return;
        
        var gObject = new wppGallery({
            main: gallery,
            slider: slider,
            slideLeft: gallery.children('.btn-left'),
            slideRight: gallery.children('.btn-right')
        }, {autosize: true, slidesPerStep: 1});
        
        var slidesOnView = gObject.get('slidesOnView');
        var timer = null;
        gallery.find('.slider img').each(function(){
            var item = $(this);
            var parent = item.parent();
            var size = size = [item.innerWidth(), item.innerHeight()];
            var fSize = item.attr('rel').split('-');
            parent.width(item.outerWidth(true));
            item.hover (
                function(){
                    clearTimeout(timer);
                    timer = setTimeout(function(){
                        var pos = parseInt(slider.css('left')) + parent.position().left;
                        var left = Math.round((fSize[0]-item.parent().width())/-2);
                        if (pos < size[0]/2)
                            left = 0;
                        else if((gObject.get('slidesOnView') * size[0] - size[0]*1.5) < pos)
                            left = (fSize[0]-size[0]) * -1;
                        
                        item
                        .addClass('active')
                        .css({'position': 'absolute', 'z-index': 10, 'opacity': 1})
                        .animate({
                            'width': fSize[0],
                            'height': fSize[1],
                            'left': left,
                            'top': Math.round((fSize[1]-item.parent().height())/-2)
                        }, 200);
                    }, 200);
                },function(){
                    clearTimeout(timer);
                    $(this)
                    .animate({
                        'width': size[0],
                        'height': size[1],
                        'top': 0,
                        'left': 0
                    }, 200, function(){$(this).css({'position': 'static', 'z-index': 0, 'opacity': 0.7})});
            });
        }).fadeTo(0, 0.7);
        
        var extColors = $('.exterier-colors-list, .exterier-bg');
        
        gallery.find('.slider a').each(function(i){
            $(this).click(function(){
                mainImg.attr('src', $(this).attr('href')).removeClass('big-img');
                if (!extColors.length)
                    return false;
                if (i == 0){
                    extColors.show();
                    $('.exterier-colors-list a:eq(0)').click();
                } else
                    extColors.hide();
                return false;
            });
        });
    },
    exterierColors: function(i)
    {
        var mainImg = $('.main-img .car-img');
        var colorTitle = $('.exterier-colors-list > span');
        $('.exterier-colors-list a').click(function(){
            mainImg.attr('src', $(this).attr('href')).removeClass('big-img');
            colorTitle.html($(this).html()+$(this).attr('title'));
            return false;
        });
    }
}
$(document).ready(function(){
    (function() {
        this.menu();
        this.table();
        this.showrooms();
        this.cuSel();
        this.lavaLamp();
        this.gallery();
        this.exterierColors();
        this.characterictics();
    }).call(application);
});

function wppGallery(uNode, uOptions)
{
    this.node = {
        main: null,
        slider: null,
        slideLeft: null,
        slideRight: null
    };
    
    this.options = {
        btnEnableClass: 'gallery-button-enable',
        step: null,
        speed: 500,
        slidesPerStep: 2,
        slidesOnView: 1,
        current: 0,
        count: null,
        timer: null,
        slideShow: false,
        slideShowSpeed: 4000,
        vertical: false,
        autosize: false
    };    
    
    $.extend(this.node, uNode);
    $.extend(this.options, uOptions);
    
    this.init();
}
wppGallery.prototype = {
    /*
    uri: {
        slider: '.promo-slide',
        btnLeft: '.slide-navigation-left',
        btnRight: '.slide-navigation-right'
    },
    node: {
        slider: null,
        slideLeft: null,
        slideRight: null
    },*/
    
    init: function()
    {
        var self = this;

        if (!self.node.slider.length || !self.node.slideLeft.length || !self.node.slideRight.length)
            return;
        
        if (!self.options.step)
            self.options.step = self.node.slider.children(':first').outerWidth(true);
        self.options.slidesOnView = Math.round(self.node.slider.parent().width() / self.options.step);
        
        
        if (self.options.vertical) {
            self.options.step = self.node.slider.children(':first').outerHeight(true);
            self.options.slidesOnView = Math.round(self.node.slider.parent().height() / self.options.step);
        }
        
        self.options.count = self.node.slider.children().length;
        if ((self.options.count-self.options.slidesOnView) > self.options.current)
            self.node.slideRight.addClass(self.options.btnEnableClass);
        
        if (self.options.autosize && !self.options.vertical)
            self.node.main.width(self.options.step*self.options.slidesOnView);
        
        self.node.slideRight.click(function(){
            clearInterval(self.options.timer);
            self.slideRight(self);
            return false;
        });
        
        self.node.slideLeft.click(function(){
            clearInterval(self.options.timer);
            self.slideLeft(self);
            return false;
        });
        
        if (self.options.slideShow)
            self.slideShow(self);
    },
    slideLeft: function(self)
    {
        if (self.options.current <= 0)
            return false;
        
        if ((self.options.current - self.options.slidesPerStep) >= 0)
            self.options.current -= self.options.slidesPerStep;
        else
            self.options.current = 0;
        
        self.node.slider.stop();
        
        if (self.options.vertical)
            self.node.slider.animate({'top':  (self.options.current*self.options.step*-1) + 'px'}, self.options.speed);
        else
            self.node.slider.animate({'left':  (self.options.current*self.options.step*-1) + 'px'}, self.options.speed);
        
        if (!self.node.slideRight.hasClass(self.options.btnEnableClass))
            self.node.slideRight.addClass(self.options.btnEnableClass);
        
        if (self.options.current <= 0)
            self.node.slideLeft.removeClass(self.options.btnEnableClass);
        
        return true;
    },
    slideRight: function(self)
    {
        
        if (self.options.current >= (self.options.count - self.options.slidesOnView))
            return false;
        
        if ((self.options.current + self.options.slidesPerStep) <= (self.options.count - (self.options.slidesOnView + self.options.slidesPerStep - 1))){
            self.options.current += self.options.slidesPerStep;
        }
        else
            self.options.current = self.options.count - self.options.slidesOnView;
        
        self.node.slider.stop();
        
        if (self.options.vertical)
            self.node.slider.animate({'top' :  (self.options.current*self.options.step*-1) + 'px'}, self.options.speed);
        else
            self.node.slider.animate({'left' :  (self.options.current*self.options.step*-1) + 'px'}, self.options.speed);
            
        if (!self.node.slideLeft.hasClass(self.options.btnEnableClass))
            self.node.slideLeft.addClass(self.options.btnEnableClass);
        
        if (self.options.current >= (self.options.count-self.options.slidesOnView))
            self.node.slideRight.removeClass(self.options.btnEnableClass);
        
        return true;
    },
    slideShow: function(self)
    {
        self.options.timer = setInterval(function() {
            if (!self.slideRight(self)) {
                self.options.current = self.options.slidesPerStep;
                self.slideLeft(self);
            }
        }, self.options.slideShowSpeed);
    },
    goTo: function(self, btn, pos)
    {
        self.node.slider.stop();
        self.node.slider.animate( { left: (self.options.step * pos * -1) + "px" }, self.options.speed );
        return true;
    },
    get: function(val)
    {
        if (typeof this.options[val] != undefined)
            return this.options[val];
        return null;
    }
}
$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
}
