/*                                    */
/* Hover                              */
/*                                    */


var Hover = {
  EXIT_DELAY  : 600,
  
  lastTimer   : null,
  lastCommand : null,
  inhibit     : false,
  
  clearCurrent: function(exec) {
    if(!this.lastTimer) return
    clearTimeout(this.lastTimer)
    if (exec) eval(this.lastCommand)
    this.lastTimer = this.lastCommand = null
  },
  
	endWith: function(command) {
    if(this.inhibit) return
    this.lastCommand = command
    this.lastTimer = setTimeout(command, this.EXIT_DELAY)
  },
  
  toggle: function(on, container_class) {
    if(this.inhibit) return
    
    // Cancel event if node is text node - let the container handle it.
		/*evt = (window.event) ? window.event : ""
		if(evt && evt.fromElement && evt.fromElement.nodeType != 1) return*/
		
   	if(on) {
   	  //if(finder.sort_active()) Element.hideIf(finder.sf_div)
   	  //Element.show(container)
   	  $$("ul."+container_class).each(function(elem){Element.show(elem)})  
   	  //if (!Element.hasClassName('menu-title', 'activated')) Element.addClassName('menu-title', 'activated') 
   	} else {
   	  //Element.hide(container)
   	  $$("ul."+container_class).each(function(elem){Element.hide(elem)})
   	  //if (Element.hasClassName('menu-title', 'activated')) Element.removeClassName('menu-title', 'activated')
   	  //if(finder.sort_active()) Element.showIf(finder.sf_div)
   	}
  }
}

/*                                    */
/* Menu                               */
/*                                    */

var menu = {
	hover: {
		begin: function(id) {	  
			Hover.clearCurrent(true)
			Hover.toggle(true,id)
			Element.addClassName(id+'_container', 'current')
		},
		
		keep: function(id) {
		  Hover.clearCurrent()
		},
		
		end: function(id, delay) {
			if (delay) Hover.endWith("menu.hover.end('"+ id +"')")
      else {
        Hover.toggle(false, id)
        Element.removeClassName(id+'_container', 'current')
      }
		}
	}
}


/*                                    */
/* Asset                              */
/*                                    */

var asset = {
  
	select: function(id) {
	  var all = $$('#thumbs .asset_thumb')
	  for (var i = 0, length = all.length; i < length; i++) {
	    Element.removeClassName(all[i], 'current')
	  }
	  Element.addClassName(id, 'current')	  	  
	},
	
	zoom: function(id) {

	},
	
	prepare_for_zoom: function() {
	  new Insertion.Bottom('stage', '<div id="dimmer"></div>')
    new Insertion.Bottom('stage', '<div id="resized"><span class="loading">Loading…</span></div>')

    var l = (document.viewport.getDimensions().width - 760) / 2
    $('resized').setStyle({left: l+'px'})
	  
	  $('dimmer').observe('click', asset.close);	  
    $('dimmer').setStyle({opacity: 0.85});
	},
	
	close: function(e) {

    Event.stopObserving($('dimmer'), 'click', asset.close);
    
    $('dimmer').remove()
    $('resized').remove()    
	}
}


document.observe("dom:loaded", function() {

  $$('ul.letters li.with').map(function(li){
    
    var menu_width = 1
    li.select('ul.artists').map(function(ul){
      menu_width += ul.getWidth() + 1
    })
    
    var right = 780 - (li.up().positionedOffset()[0] + li.positionedOffset()[0] + menu_width)

    if (right < 0) {
      li.select('ul.artists').each(function(m, i){
        m.setStyle({'left': ((i*219)+right) + 'px'})
      })
    }

  })


})


//
// Document ready
//
jQuery(function() {  

  ui.insert_players(jQuery('.yt, .vimeo'))
  
  jQuery("ul.artist-list a.read-more").click(function(e){
    jQuery(this).hasClass('less') ? jQuery(this).parent().hide().prev().show() : jQuery(this).parent().hide().next().show()

    e.preventDefault()
  })
  
  
})

var ui = {
    
  insert_players: function(containers) {

    if (containers.length > 0) {
      containers.each(function(){

        var v_id = jQuery(this).find("p:first").html()
        var c_id = jQuery(this).attr('id')    
        
        var autoplay = (c_id.split('_').length > 2) ? 1 : 0
        
        // YouTube
        if (jQuery(this).hasClass('yt')) {            
          jQuery(this).flash({ 
            swf: 'http://www.youtube.com/v/'+v_id,
            params: { allowScriptAccess: "always", wmode: 'opaque'}, 
            wmode: 'transparent',  
            flashvars: {enablejsapi: '0', autoplay: autoplay, allowScriptAccess: "always", playerapiid: c_id, showinfo: 0 },   
            height: jQuery(this).height(), width: jQuery(this).width() })
        }
        
        // Vimeo
        if (jQuery(this).hasClass('vimeo')) {
          jQuery(this).flash({ 
            swf: 'http://vimeo.com/moogaloop.swf',
            params: { allowScriptAccess: "always", wmode: 'opaque'},   
            flashvars: {js_onLoad: 'vimeo_player_loaded', clip_id: v_id, js_api: 0, js_swf_id: c_id, autoplay: autoplay, allowScriptAccess: "always", show_title: 0 },   
            height: jQuery(this).height(), width: jQuery(this).width() })
        }

      })
    }
  }
}
