/*****************************************************************************************************************
 * Global Vars
 *****************************************************************************************************************/

  var isResizing = false;
  var $obj = [];
  var $win;
  var isMobile = (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))||(navigator.userAgent.match(/Android/i));


/*****************************************************************************************************************
 * DOM-Ready
 *****************************************************************************************************************/
 
  $(function(){
    initStaticComponents();
  });
  
  
/***************************************************************************************************
 * Create static Components
 ***************************************************************************************************/
  
  function initStaticComponents(){
    $win = $(window).bind('resize',resizeHandler);
    $obj['body'] = $('body');
    $obj['master'] = $obj['body'].children('#master');
    $obj['header'] = $obj['master'].children('#header');
    $obj['footer'] = $obj['master'].children('#footer');
    $obj['content'] = $obj['master'].children('#content');
    $obj['header-navi'] = $obj['header'].children('.navi');
    $obj['navi-level-0-li'] = $obj['header-navi'].children('li.level-0');
    $obj['navi-level-1-a'] = $obj['header-navi'].find('a.level-1');
    $obj['navi-level-1-ul'] = $obj['header-navi'].find('ul.level-1').not('.active');
    $obj['navi-level-2-a'] = $obj['header-navi'].find('a.level-2');
    $obj['navi-level-2-ul'] = $obj['header-navi'].find('ul.level-2').not('.active');
    $obj['navi-level-3-a'] = $obj['header-navi'].find('a.level-3');
    
    $obj['footer-navi'] = $obj['footer'].children('.navi');
    $obj['box'] = $obj['master'].find('.box').append('<div class="box-corner" />');
    
    // Intro
    $obj['#intro'] = $('#intro');
    if($obj['#intro'].length)    showIntro();
    
    // HTML5 Videos ---------------------------------------------------------------
    $obj['video'] = $obj['content'].find('video');
    $obj['video'].each(function(index,el){
      if(isMobile) el.controls = 'true';
    });
    
    // Navigation -------------------------------------------------------------------
    $obj['navi-level-1-ul'].not('.active').hide();
    $obj['navi-level-2-ul'].hide();
    
    $obj['navi-level-0-li'].each(function(index,el){
      var $el = $(el);
      var $ul = $el.children('ul');
      
      if($ul.length){
        $el.bind({
          mouseover : function(e){
            $obj['navi-level-1-ul'].hide();
            $ul.show();
          },
          mouseout : function(e){
            $obj['navi-level-1-ul'].hide();
          }
        });
      }
    });
    
    // Slidehsow ------------------------------------------------------------------------
    $obj['mod-slideshow'] = $('.mod-slideshow');
    $obj['mod-slideshow'].each(function(){
      var $this = $(this);
      var $firstChild = $this.find('img:first');
      var interval = parseInt($this.attr('data-interval'));
      $this.css({overflow:'hidden',width:$firstChild.attr('width')+'px',height:$firstChild.attr('height')+'px'});
      $this.children(':gt(0)').hide();
      setInterval(function(){
        $this.children(':first-child').fadeOut(700).next().fadeIn(700).end().appendTo($this);
      },interval);
    });
    
    // Text Wrap
    $obj['mod-wrap'] = $('.mod-wrap');
    if($obj['mod-wrap'].length)    initWrap();
    
    // Showrooms
    $obj['mod-showrooms'] = $('.mod-showrooms');
    if($obj['mod-showrooms'].length)  initShowrooms();
    
    // AddThis -------------------------------------------------------------------------------
    $obj['body'].append('<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4d6fee5a43a770b5"></script>');
    
    //GMap
    $obj['mod-gmap'] = $('.mod-gmap');
    if($obj['mod-gmap'].length) initGMap();
    
    // Clear Fix
    if($.browser.msie && parseInt($.browser.version)<8) clearFix();
  }
  
  function clearFix(){
    var c = '<div class="cfix" />';
    $('.mod-text').append(c);
    $('.mod-definition').append(c);
    $('.mod-definition-100').append(c);
    $('.mod-showrooms').append(c);
    $('.mod-dates').append(c);
    $('.form').append(c);
  }
  

/***************************************************************************************************
 * Intro
 ***************************************************************************************************/
 
  function showIntro(){
    var count = 0;
    var introInterval;
    
    $obj['master'].hide();
    $obj['#intro'].appendTo($obj['body']).css({width:$win.width(),height:$win.height()}).show().click(function(){
      stopIntro();                                      
    });
    $obj['#intro'].children(':gt(0)').hide();
    
    introInterval = setInterval(function(){               
      count++;
      if(count>$obj['#intro'].children().length-1){
        stopIntro();
      }                 
      $obj['#intro'].children(':first-child').fadeOut(700).next().fadeIn(700).end().appendTo($obj['#intro']);
      
    },1500);
    
    function stopIntro(){
      clearInterval(introInterval);
      $obj['master'].show();
      $obj['#intro'].hide().remove();
    }
  }
  
/***************************************************************************************************
 * Init Wrap
 ***************************************************************************************************/
   
  function initGMap(){
    $obj['mod-gmap'].each(function(index){
      var $this = $(this);
      var id = (this.id) ? this.id : 'gmap'+Math.floor(Math.random()*1000000);
      this.id = id;
      var lat = parseFloat($this.attr('data-lat'));
      var lng = parseFloat($this.attr('data-lng'));
      var zoom = parseInt($this.attr('data-zoom'));
      if(isNaN(zoom)) zoom=11;
      
      var myLatlng = new google.maps.LatLng(lat,lng);
      var myOptions = {
        zoom: zoom,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      var map = new google.maps.Map(this, myOptions);
      var marker = new google.maps.Marker({
        position: myLatlng, 
        map: map,
        title:$this.attr('data-title')
      });
    });
  }
 
 
/***************************************************************************************************
 * Init Wrap
 ***************************************************************************************************/
 
   function initWrap(){
    $obj['mod-wrap'].addClass('mod-wrap-js').children('.content').hide();
    $obj['mod-wrap'].children('.title').click(function(){
      var $this = $(this);
      if($this.parent().hasClass('open')){
        $this.parent().removeClass('open').children('.content').stop().slideUp({duration:500,easing:'easeOutExpo'});
      } else {
        $obj['mod-wrap'].filter('.open').removeClass('open').children('.content').stop().slideUp({duration:500,easing:'easeOutExpo'});
        $this.parent().addClass('open').children('.content').stop().slideDown({duration:500,easing:'easeOutExpo'});  
      }
    });
  }
  
  
/***************************************************************************************************
 * Init Showrooms
 ***************************************************************************************************/
  
  function initShowrooms(){
    
    $obj['mod-showrooms'].append('<div class="mod-showrooms-infobox" />');

    $obj['mod-showroom'] = $obj['mod-showrooms'].find('.mod-showroom');
    $obj['mod-showrooms-container'] = $obj['mod-showrooms'].children('.mod-showrooms-container');
    $obj['mod-showrooms-infobox'] = $obj['mod-showrooms'].children('.mod-showrooms-infobox');
    $obj['mod-showrooms-group'] = $obj['mod-showrooms'].children('.mod-showrooms-infobox');
	
    $('.mod-showroom').addClass('mod-showroom-js').click(showroomInfo);
    
    function showroomInfo(el){
      var $this = (isNaN(el)) ? $(this) : $obj['mod-showroom'].eq(0);
      $obj['mod-showroom'].filter('.active').removeClass('active');
      $this.addClass('active');
      $obj['mod-showrooms-infobox'].hide().html('<strong>'+$this.children('.location').html()+'</strong><br>'+$this.children('.info').html().replace(/\(at\)/g,'@')).fadeIn(500);
    }
    
    showroomInfo(0);
  }
  
  
/***************************************************************************************************
 * Create dynamic Components
 ***************************************************************************************************/
  
  function initDynamicComponents(){

  }
  
  
/***************************************************************************************************
 * Resize Handler
 ***************************************************************************************************/
 
   function resizeHandler(){
    if(isResizing) return;
    isResizing=true;
    
    // do something
    isResizing=false;
  }


/***************************************************************************************************
 * Play Loop
 ***************************************************************************************************/
   
  function playLoop(video){
    video.currentTime=0;
    video.play();
  }
  
  function toggleVideo(video){
    (video.paused) ? video.play() : video.pause();
  }
  

/***************************************************************************************************
 * Test CSS Support
 ***************************************************************************************************/
 
  var supportsCSS = (function() {
     var div = document.createElement('div'),
      vendors = 'Khtml Ms O Moz Webkit'.split(' '),
      len = vendors.length;
  
     return function(prop) {
      if ( prop in div.style ) return true;
  
      prop = prop.replace(/^[a-z]/, function(val) {
       return val.toUpperCase();
      });
  
      while(len--) {
       if ( vendors[len] + prop in div.style ) {
        // browser supports box-shadow. Do what you need.
        // Or use a bang (!) to test if the browser doesn't.
        return true;
       } 
      }
      return false;
     };
  })();


/***************************************************************************************************
 * Trace function (Wrapper for Firebug console)
 ***************************************************************************************************/
      
  function trace(msg){try{console.log(msg);}catch(e){}}

