window.contentWidth = 960;
window.contentHeight = 670
window.bodyWidth
window.bodyHeight
window.separatorWidth
window.current = 0
window.container
window.time = 1000
window.spet = 100
window.animating = false

function embedPlayers(){
	swfobject.embedSWF("/Flvplayer.swf", "plasmaPlayer", "376", "212", "9", "/js/expressinstall.swf", {
    xml: '/portfolio.xml',
    color: "0x85CC12",
    bgColor: "0x000000",
    alpha: '0.85',
    volume: '0.5'
  }, {
	  menu:"false", 
	  allowFullScreen:"true", 
	  allowScriptAccess:"always" 
  }, {
	  id:"plasmaPlayer", 
	  name:"plasmaPlayer", 
	  bgcolor:"#000000" 
  });
  
  swfobject.embedSWF("/Flvplayer.swf", "screenPlayer", "376", "212", "9", "/js/expressinstall.swf", {
    xml: '/directors.xml',
    color: "0x85CC12",
    bgColor: "0x000000",
    alpha: '0.85',
    volume: '0.5'
  }, {
	  menu:"false", 
	  allowFullScreen:"true", 
	  allowScriptAccess:"always" 
  }, {
	  id:"screenPlayer", 
	  name:"screenPlayer", 
	  bgcolor:"#000000" 
  });
}

window.addEvent('resize',function(e){
  
  window.bodyWidth = $(document).getWidth();
  window.bodyHeight = $(document).getHeight();
  window.separatorWidth = (window.bodyWidth - window.contentWidth) / 2
  window.step = window.bodyWidth / 10
  
  $('wrapper').setStyle('margin-top', (window.bodyHeight - window.contentHeight) / 2)
  $('logo').setStyle('left', window.separatorWidth + 50)
  $('navHolder').setStyle('left', window.separatorWidth + 50)
  $('webdesignby').setStyle('left', window.separatorWidth + window.contentWidth - 50)
  $$('.content').each(function(el, index){
    el.set('styles', { width: window.bodyWidth /*window.contentWidth + window.separatorWidth*2*/, marginLeft: (index ? el.getStyle('margin-left').toInt() : window.separatorWidth)})
  })
  $('content2').setStyle('background-position', -((window.bodyWidth - 200 - 122)%197)+"px 0")
  $('trabant').setStyle('left', window.bodyWidth - window.separatorWidth - 229)
  
  
  setTimeout(function(){
    window.container.setStyle('margin-left', - $('content'+(window.current)).getPosition(window.container).x + window.separatorWidth)  
  }, 100)
  
});



window.addEvent('domready', function () {
  
  var loadImages = new Asset.images(['/images/0.gif', '/images/1.gif', '/images/2.gif', '/images/3.gif', '/images/4.gif', '/images/5.gif', '/images/address.png', '/images/board.png', '/images/limes.png', '/images/plasma.png', '/images/table.png'], {
    onComplete: function(){
        $('loading').empty().tween('opacity', 0)
        embedPlayers()
    }
  });
  

  if(location.hash){
    window.current = window.navigation.indexOf(location.hash.slice(1))
    if(window.current < 0) window.current = 0
  }
  document.title = window.titles[window.current]
	
  
  window.container = $('container').set('tween', {duration: window.time, link: 'cancel', transition: 'sine:out', onStart: function(){window.animating = true},  onComplete: function(){window.animating = false}})
  this.fireEvent('resize', null)
  
  
  var contents = $$('.content').each(function(el, content_index){
    el.getChildren('.object').each(function(o){
      o.store('content_index', content_index).set({
        tween: {duration: window.time, link: 'cancel', transitoin:'sine:out'},
        styles: {'margin-left': ((window.current - content_index) * step) }
      })
    })
  })
  
  $('arrowPrev').setStyle('display', window.current < 1 ? 'none' : 'block')
  $('arrowNext').setStyle('display', window.current > 4 ? 'none' : 'block')
  
  var objects = $$('.content .object')
  
  $$("#nav a").each(function(el, index, elements){
    el.store('index', index).addClass(index == window.current ? 'selected' : '').addEvent('click', function(e){
      if($defined(e))e.preventDefault()
      if(this.retrieve('index') == window.current) return
      
      elements[window.current].removeClass('selected')
      window.current = this.retrieve('index')
      this.addClass('selected')      
      window.container.tween('margin-left', - $('content'+(window.current)).getPosition(window.container).x + window.separatorWidth)
      
      location.hash = window.navigation[window.current]
      document.title = window.titles[window.current]
      
      $('arrowPrev').setStyle('display', window.current < 1 ? 'none' : 'block')
      $('arrowNext').setStyle('display', window.current > 4 ? 'none' : 'block')
      
      objects.each(function(o,i){
        o.tween('margin-left', ((window.current - o.retrieve('content_index')) * step))
      })
    })
  })
  
  
  $$('.portfolioLink').each(function(el){
    el.addEvents({
      'click': function(e){
        getFlashMovie("plasmaPlayer").sendToActionScript('list', this.rel)
      },
      'mouseenter': function(e){
        this.addClass('over')
      },
      'mouseleave': function(e){
        this.removeClass('over')
      }
    })
  })
  
  $$('.pictureLink').each(function(el){
    el.addEvents({
      'click': function(e){
        getFlashMovie("screenPlayer").sendToActionScript('list', this.rel)
      },
      'mouseenter': function(e){
        this.addClass('over')
      },
      'mouseleave': function(e){
        this.removeClass('over')
      }
    }).getFirst('span').addClass('balloon'+Math.ceil(Math.random()*4))
  })
  
  
  var pushFX = new Fx.Tween(window.container, {duration:200, link: 'cancel'});

  $$('.arrowNav').each(function(el, isNext){
    el.addEvents({
      'click': function(e){
        if((isNext && window.current > 4) || (!isNext && window.current < 1)) return
        $$("#nav a")[window.current + (isNext ? +1 : -1)].fireEvent('click', null)
      },
      'mouseenter': function(e){
        if(!window.animating) pushFX.start('margin-left', window.container.getStyle('margin-left').toInt() + (isNext ? -20 : +20))
      },
      'mouseleave': function(e){
        if(!window.animating) pushFX.start('margin-left', - $('content'+(window.current)).getPosition(window.container).x + window.separatorWidth)
      }
    })
  })
    
})

function sendToJavaScript(movieName, val){
  switch(movieName){
    case 'plasmaPlayer':
      var id = val.split('-')[0]
      $$('.portfolioLink').each(function(el){
        el.removeClass('selected')
        if(el.rel == id) el.addClass('selected')
      })
    break
    case 'screenPlayer':
      var parts =  val.split('-')
      var id = parts.length > 2 ? parts[0] + '-' + parts[1] : val
      $$('.pictureLink').each(function(el){
        el.removeClass('selected')
        if(el.rel == id) el.addClass('selected')
      })
    break
  }
  
}
