var BMW = {

  specifications: {
    
    accordion: function() {
      $('.pointer').live('click', function() {
        if($(this).hasClass('close'))
          BMW.specifications.openAccordion();
        else
          BMW.specifications.closeAccordion();
      });
    },
    
    openAccordion: function() {
      $('.pointer').removeClass('close').addClass('active');
      $('#specifications').animate({ top: '+=200px' });
    },
    
    closeAccordion: function() {
      $('.pointer').removeClass('active').addClass('close');
      $('#specifications').animate({ top: '-=200px' });
    },
    
    getContent: function(dt, opened) {
      var opened = (opened == null) ? false : opened;
      
      $('#specifications dt').removeClass('active');
      $(dt).addClass('active');
      var dd = $(dt).next();
      $('.cars.show #content_all').html(dd.html());
      $('#content').css({'background': 'url('+dd.attr('background') + ') no-repeat'});
      if(!opened)
        BMW.specifications.closeAccordion();
        
			BMW.gallery.init();
    },
    
    setNavigation: function() {
      $('#specifications dt').click(function() {
        BMW.specifications.getContent(this);
      });
      $('#specifications dt.url_pronta_entrega').unbind('click');
			$('#specifications dt.url_pronta_entrega').click(function() {
        window.open($(this).next().children('a').attr('href'));
			});
    },
    
    init: function() {
      this.accordion();
      this.setNavigation();
      this.getContent('#specifications dt:first', true);
    }
    
    
  },

	news: {
		init: function() {
			$('body.articles.index dl').accordion({
				header: 'dt',
				action: 1
			});
			
			$('#article-body').jScrollPane({
				scrollbarWidth:36,
				scrollbarMargin:10,
				scrollbarHeight:160
			});
		}
	},

	newsletter: {
	  loading: function() {
	    $('#newsletter-form .submit').hide();
	    var loader = document.createElement('img');			
	    $(loader).css('margin-top', '3px').css('margin-left', '5px');
	    $(loader).attr('src', '/images/bmwmotos/loaderSmall.gif');
	    $('#newsletter-form fieldset').append($(loader));
	    $('.box_underground_form').addClass('onLoading').css('background-position', '0 -172px');
	  },
		init: function() {
			$('.box_underground_form').not('onLoading').children('.submit').live('mouseover', function(){ 
			  $('.box_underground_form').css({background: "url(/images/bmwmotos/bg/news.png) no-repeat"}); 
			});
			
			$('.box_underground_form').not('onLoading').children('.submit').live('mouseout',function(){ 
			  $('.box_underground_form').css({background: "url(/images/bmwmotos/bg/news.png)  0 -86px no-repeat"}); 
			});
		}
	},

	banners: {

	  init: function() {
	    $('#thumbs li a').live('click', function() {

	      var li = document.createElement('li');
	      var link = document.createElement('a');
	      var image = new Image();
        var imageBanner = new Image();

        var a = this;
        
        var div = document.createElement('div');
		    var banner = $('#image_thumbs');
		    $(div).attr('id', 'loader');
		    $(banner).append(div);
		    $('#banner').hide();

        $(imageBanner).load(function() {
          $(image).attr('src', $('#banner').attr('name'));
          $(link).attr('name', $('#banner').attr('src'))
  	      $(link).attr('href', $('#banner').parent().attr('href'));
  	      $(link).append(image);
  	      $(li).append(link);

  	      $('#banner').attr('src', $(a).attr('name'));
          
          $('#banner-link').attr('href', $(a).attr('href'));
          
  	      $(a).parent().remove();
  	      $('#banner').attr('name', $(a).children('img').attr('src'));
  	      $('#thumbs').append(li);
  	      $('#loader').remove();
  	      $('#banner').fadeIn(1000);
        }).attr('src', $(a).attr('name'));
            		
	      return false;
	    });
	  }

	},
	
	menu: {
	  init: function() {
	    $('ul.list_cars li.series').hover(
			  function() { 
			    clearTimeout(BMW.timer);
			    $('ul.list_cars li.series').removeClass('active').children('ul').hide();
			    $(this).addClass('active').children('ul').show();
			  },
			  function() {
			    var menu = this;
			    
			    BMW.timer = setTimeout(function() {
			      $(menu).removeClass('active').children('ul').hide();
			    }, 500);
			});
	  }
	},
	
	gallery: {
	  init: function() {
			$('.scroll').jcarousel();
			
			BMW.gallery.loadImage(
				$('#content_all .scroll li:first a')
			);
			
			$('#content_all .scroll li a').click(function() {
				BMW.gallery.loadImage(this);
				return false;
			}); 
		},
		
		loadImage: function(link) {
		  $('.loading').show(); 
			
			var link = link;
			var img = new Image();
			
			$(img).load(function () {
				// BMW.timer = setTimeout(function() {
					$('.loading').hide();
				// }, 500000);
				$('.cars.show').css('background', 'url(' + $(link).attr('href') + ') center 105px no-repeat');
			}).attr('src', $(link).attr('href'));
		}
	},
	inviteForm: {
    init: function() {
     $("#invite .buttons input").click(function(){
        var button = $(this);
        button.hide();
        $(".form_loading").show();
        return true;
      });
    }    
  },
	
	images_links: {
		init: function() {
			$('.column_center img#peaces_img').hover( function() {
				$(".column_center #peaces_link").css({color: "#003399"});
			}, function(){
				$(".column_center #peaces_link").css({color: "#fff"});
			});
			$('.column_center img#concessionarias').hover( function() {
				$(".column_center #concessionarias_link").css({color: "#003399"});
			}, function(){
				$(".column_center #concessionarias_link").css({color: "#fff"});
			});
		}
	}
};

$(document).ready(function() {
	BMW.newsletter.init();
	BMW.images_links.init();
  BMW.specifications.init();
	BMW.news.init();
	BMW.banners.init();
	BMW.menu.init();
	BMW.inviteForm.init();
	$('input[name="contact[phone]"]').setMask('phone'); // telefone
	$("#contact-form").submit( function(){
    $(this).find(".submit").hide();
    $(this).find(".loader_submit").show();
  });
});

