var baseUrl = '';
var backHash = '';
var currentVideo = '';
var currentParamsPlayer = '';
var jmStyle = '';
var lang = '';
var country = '';
var autostart = false;

var JMDisplay =
{
  options: function()
  {
    return {
      width: 598,
      opacity: 0.7,
      zIndex: 9996,
      fade: true,
      fadeDuration: 0.5,
      onSuccess: function() {
         rewriteJMDisplayLink();
      },
      afterOpen: function() {
		    var height = $('container').getHeight();

		    if (document.viewport.getHeight() > height) {
          height = document.viewport.getHeight();
        }

        $('modal_overlay').setStyle({height: height+'px'});
		  }
		};
  },

  modalCss: function(msg)
  {
    return '<a href="#" class="popup_close" onclick="Control.Modal.close();return false">CLOSE<img src="/images/front/ico_del_big_white.gif"></a><div class="clear"></div><div class="popup_title">JACK MAESTRO</div><div class="popup_content"><p class="center">'+msg+'</p></div>';
  },

  modal: function(url, msg, logged)
	{
		var link = document.createElement('a');

		var options = JMDisplay.options();
		if(url)
		{
		  link.href = baseUrl+url;
		}
  	else if(msg)
  	{
  	  options.contents = function(){ return JMDisplay.modalCss(msg); };
  	}

    if(!logged)
    {
      var modal = new Control.Modal(link, options);
      modal.open();
    }
    else
    {
      new Ajax.Request(baseUrl+'/users/is_logged.php', {
    		onSuccess: function() {
    			var modal = new Control.Modal(link, options);
          modal.open();
    		},

    		onFailure: function() {
    			JMDisplay.modal('/users/login.php');
    		}
    	});
    }

    return false;
	},

	modalConfirm: function(method, msgs, logged)
	{
	  var link = document.createElement('a');

		var myTemplate = new Template('<a href="#" class="popup_close" onclick="Control.Modal.close();return false">CLOSE<img src="/images/front/ico_del_big_white.gif"></a><hr class="clear" /><div class="popup_content"><p class="center">#{msg}</p><p class="center"><input type="button" value="#{cancel}" class="button_light" onclick="Control.Modal.close()" /> <input type="button" value="#{submit}" class="button_light" onclick="#{method}" /></p></div>');

		var options = JMDisplay.options();
		options.contents = myTemplate.evaluate({msg: msgs[0], cancel: msgs[1], submit: msgs[2], method: method.replace('"', '\"')});

    if (!logged)
    {
      var modal = new Control.Modal(link, options);
      modal.open();
    }
    else
    {
      new Ajax.Request(baseUrl+'/users/is_logged.php', {
    		onSuccess: function() {
    			var modal = new Control.Modal(link, options);
          modal.open();
    		},

    		onFailure: function() {
    			JMDisplay.modal('/users/login.php');
    		}
    	});
    }

    return false;
	},

	hoverbox: function(elmt, message)
	{
	  if(JMAjax.dragInProgress)
	    return false;

	  if(message)
		{
		  if(elmt.hoverbox == undefined)
		  {
		    	var modal = new Control.Modaal(elmt, {
    				hover: true,
    				position: 'mouse',
    				offsetTop: -20,
    				offsetLeft: 20,
    				containerClassName: 'hoverbox',
    				contents: function(){
    			        return message;
    			  },
    			  afterClose: function() {
    			    modal = null;
    			  }
    			});

    			elmt.hoverbox = modal;
    			modal.open();
		  }
		}

		return false;
	},

	infos: function(msg)
	{
	  var modal = Control.Modal.current;

	  if(modal)
	  {
	    var elmt = $('modal_container');

	    Effect.Fade(elmt, { from: 1, to: 0.1, duration: 0.5, queue: 'end', afterFinish: function(){ elmt.update(JMDisplay.modalCss(msg)); }});
	    Effect.Fade(elmt, { from: 0.1, to: 1, duration: 0.5, queue: 'end'});
	  }
	  else
	  {
	    JMDisplay.modal(null, msg);
	  }

	  setTimeout("Control.Modal.close()", 5000);

	  return false;
	},

	errors: function(elmtId, msg)
	{
	  var elmt = $(elmtId);

	  if(elmt)
	  {
	    msg = '<a href="#" id="alert_close" onclick="return JMDisplay.close(\''+elmtId+'\');">'+msg+'</a>';
	    elmt.update(msg);
  	  Effect.BlindDown(elmt);
	  }
	  else
	  {
	    JMDisplay.infos(msg);
	  }

	  return false;
	},

	close: function(elmt)
	{
	  var elmt = $(elmt);

	  if(elmt)
	    Effect.BlindUp(elmt);

	  return false;
	},

	refreshMenu: function()
	{
	  return JMAjax.updater('menu', '/_menu.php', {
	    onComplete:function() {
	      rewriteJMDisplayLink();
	  }});
	},
	
	home_login: function()
	{
	  return JMAjax.updater('onair', '/users/login_home_success.php', {
	    onComplete:function() {
	      rewriteJMDisplayLink();
	  }});
	},
	
	greetings: function()
	{
	  return JMAjax.updater('onair', '/jackpot/greetings/index.php', {
	    onComplete:function() {
	      rewriteJMDisplayLink();
	  }});
	},

	scrollFocus: function()
	{
	  var scrolls = getScrollXY();

	  if(scrolls[1] > 340)
	    $('content').scrollTo();
	},

	home: function()
	{
	  $('profile').hide();

	  backHash = window.location.hash;

	  var target = 'home';
	  var elmt = $(target);

	  document.fire('jmdisplay:begun');

	  if (elmt) {
	    elmt.remove();
    }

    JMDisplay.styleTitle();

    document.fire('jmdisplay:loading');

    JMAjax.updater('content', '/_content_home.php', {
      onComplete: function(){
        JMDisplay.scrollFocus();
        $('content').removeClassName('minimized');
        JMDisplay.refreshMenu();
        swfobject.embedSWF("/home_tabs.swf", 'home_tabs', "676", "354", "9.0.0", "", {country:country, style:jmStyle}, {wmode:"transparent", menu:"false", allowScriptAccess:'sameDomain'});
        document.fire('jmdisplay:finished', {target: target});
      },
      insertion:'bottom'
    });

	  return false;
	},

	generic: function(url, options)
	{
	  JMDisplay.profile();

	  var target = 'generic_panel';
	  var elmt = $(target);

    var	options = options != undefined ? options : {};
    options.onComplete = function(){
      JMDisplay.scrollFocus();
      document.fire('jmdisplay:finished', {target: target});
    };

    options.insertion = 'bottom';

	  backHash = window.location.hash;

	  document.fire('jmdisplay:begun');

	  if(elmt)
	    elmt.remove();

	  document.fire('jmdisplay:loading');

    JMAjax.updater('content', url, options);

	  return false;
	},

	profile: function()
	{
	  var elmt = $('profile').show();

	  $('content').addClassName('minimized');

	  JMAjax.updater(elmt, '/users/_content_profile.php', {
	    onComplete: function()
	    {
	      if($('home') && $('home').visible()) {
	        $('content').removeClassName('minimized');
	      }

	      rewriteJMDisplayLink();
	    }
	  });

	  return false;
	},

	account: function(withProfile)
	{
	  var withProfile = withProfile == undefined ? true : withProfile;
	  var hide = false;
	  if(window.location.hash != '#account') {
      hide = true;
    }else{
      backHash = window.location.hash;
    }

    if(withProfile)
	    JMDisplay.profile();

    var target = 'account_panel';
    var elmt = $(target);

	  if(!hide)
	    document.fire('jmdisplay:begun');

	  if(!elmt)
	  {
	    document.fire('jmdisplay:loading');

	    JMAjax.updater('content', '/users/_content_account.php', {
	      onComplete: function(){
	        if(!hide)
	        {
	          JMDisplay.scrollFocus();
	          document.fire('jmdisplay:finished', {target: target});
	        }
	        else
	        {
	          $(target).hide();
	        }
	      },
	      insertion:'bottom'
	    }, true);
	  }
	  else
	  {
	    if(!hide)
	      document.fire('jmdisplay:finished', {target: target});
	  }

	  return false;
	},
	my_tags: function(withProfile)
	{
	  var withProfile = withProfile == undefined ? true : withProfile;
	  var hide = false;
	  if(window.location.hash != '#my-tags') {
      hide = true;
    }else{
      backHash = window.location.hash;
    }

    if(withProfile)
	    JMDisplay.profile();

    var target = 'my_tags_panel';
    var elmt = $(target);

	  if(!hide)
	    document.fire('jmdisplay:begun');

	  if(!elmt)
	  {
	    document.fire('jmdisplay:loading');

	    JMAjax.updater('content', '/users/_content_my_tags.php', {
	      onComplete: function(){
	        if(!hide)
	        {
	          JMDisplay.scrollFocus();
	          document.fire('jmdisplay:finished', {target: target});
	        }
	        else
	        {
	          $(target).hide();
	        }
	      },
	      insertion:'bottom'
	    }, true);
	  }
	  else
	  {
	    if(!hide)
	      document.fire('jmdisplay:finished', {target: target});
	  }

	  return false;
	},
	my_stats: function(withProfile)
	{
	$('profile').hide();
	
	  var withProfile = withProfile == undefined ? true : withProfile;
	  var hide = false;
	  if(window.location.hash != '#my-stats') {
      hide = true;
    }else{
      backHash = window.location.hash;
    }
	
    var target = 'my_stats_panel';
    var elmt = $(target);

	  if(!hide)
	    document.fire('jmdisplay:begun');

	  if(!elmt)
	  {
	    document.fire('jmdisplay:loading');

	    JMAjax.updater('content', '/users/stats/_content_stats.php', {
	      onComplete: function(){
	        if(!hide)
	        {
	          JMDisplay.scrollFocus();
	          document.fire('jmdisplay:finished', {target: target});
	        }
	        else
	        {
	          $(target).hide();
	        }
	      },
	      insertion:'bottom'
	    }, true);
	  }
	  else
	  {
	    if(!hide)
	      document.fire('jmdisplay:finished', {target: target});
	  }

	  return false;
	},
	my_messages: function(withProfile)
	{
	  var withProfile = withProfile == undefined ? true : withProfile;
	  var hide = false;
	   JMDisplay.profile();

	  var target = 'messages_panel';
	  var elmt = $(target);
	  var message = window.location.hash.match(/my-messages\/([^\/]+)/);
	  if(message)
	  {
		var message_id = 1;
		var message_controller=unescape(message[1]);
	  }
	  document.fire('jmdisplay:begun');
	  if(elmt){
	    elmt.remove();
	  }
	  document.fire('jmdisplay:loading');

    JMAjax.updater('content', 'users/message/_content_my_messages.php', {
      parameters: {id: message_id, controller: message_controller},
      method: 'get',
      onComplete: function(){
        JMDisplay.scrollFocus();
        document.fire('jmdisplay:finished', {target: target});
      },
      insertion:'bottom'
    });

	  return false;
	},
	my_loteries: function(withProfile)
	{
	  var withProfile = withProfile == undefined ? true : withProfile;
	  var hide = false;
	  if(window.location.hash != '#my-loteries') {
      hide = true;
    }else{
      backHash = window.location.hash;
    }

    if(withProfile)
	    JMDisplay.profile();

    var target = 'my_loteries_panel';
    var elmt = $(target);

	  if(!hide)
	    document.fire('jmdisplay:begun');

	  if(!elmt)
	  {
	    document.fire('jmdisplay:loading');

	    JMAjax.updater('content', '/users/jackpot/loteries/_content_my_loteries.php', {
	      onComplete: function(){
	        if(!hide)
	        {
	          JMDisplay.scrollFocus();
	          document.fire('jmdisplay:finished', {target: target});
	        }
	        else
	        {
	          $(target).hide();
	        }
	      },
	      insertion:'bottom'
	    }, true);
	  }
	  else
	  {
	    if(!hide)
	      document.fire('jmdisplay:finished', {target: target});
	  }

	  return false;
	},
	my_jackbox: function(withProfile)
	{
	  var withProfile = withProfile == undefined ? true : withProfile;
	  var hide = false;
	  if(window.location.hash != '#my-jackbox') {
      hide = true;
    }else{
      backHash = window.location.hash;
    }

    if(withProfile)
	    JMDisplay.profile();

    var target = 'my_jackbox_panel';
    var elmt = $(target);

	  if(!hide)
	    document.fire('jmdisplay:begun');

	  if(!elmt)
	  {
	    document.fire('jmdisplay:loading');

	    JMAjax.updater('content', '/users/jackbox/index.php', {
	      onComplete: function(){
	        if(!hide)
	        {
	          JMDisplay.scrollFocus();
	          document.fire('jmdisplay:finished', {target: target});
	        }
	        else
	        {
	          $(target).hide();
	        }
	      },
	      insertion:'bottom'
	    }, true);
	  }
	  else
	  {
	    if(!hide)
	      document.fire('jmdisplay:finished', {target: target});
	  }

	  return false;
	},
	test: function(withProfile)
	{
	$('profile').hide();
	
	  var withProfile = withProfile == undefined ? true : withProfile;
	  var hide = false;
	  if(window.location.hash != '#test') {
      hide = true;
    }else{
      backHash = window.location.hash;
    }
	
    var target = 'test_panel';
    var elmt = $(target);

	  if(!hide)
	    document.fire('jmdisplay:begun');

	  if(!elmt)
	  {
	    document.fire('jmdisplay:loading');

	    JMAjax.updater('content', '/test/test.php', {
	      onComplete: function(){
	        if(!hide)
	        {
	          JMDisplay.scrollFocus();
	          document.fire('jmdisplay:finished', {target: target});
	        }
	        else
	        {
	          $(target).hide();
	        }
	      },
	      insertion:'bottom'
	    }, true);
	  }
	  else
	  {
	    if(!hide)
	      document.fire('jmdisplay:finished', {target: target});
	  }

	  return false;
	},
	
	testresult: function(withProfile)
	{
	$('profile').hide();
	
	  var withProfile = withProfile == undefined ? true : withProfile;
	  var hide = false;
	  if(window.location.hash != '#test') {
      hide = true;
    }else{
      backHash = window.location.hash;
    }
	
    var target = 'test_panel';
    var elmt = $(target);

	  if(!hide)
	    document.fire('jmdisplay:begun');

	  if(!elmt)
	  {
	    document.fire('jmdisplay:loading');

	    JMAjax.updater('content', '/test/testresult.php', {
	      onComplete: function(){
	        if(!hide)
	        {
	          JMDisplay.scrollFocus();
	          document.fire('jmdisplay:finished', {target: target});
	        }
	        else
	        {
	          $(target).hide();
	        }
	      },
	      insertion:'bottom'
	    }, true);
	  }
	  else
	  {
	    if(!hide)
	      document.fire('jmdisplay:finished', {target: target});
	  }

	  return false;
	},
	
	my_public_profile: function(withProfile)
	{
	$('profile').hide();
	
	  var withProfile = withProfile == undefined ? true : withProfile;
	  var hide = false;
	  if(window.location.hash != '#my-public-profile') {
      hide = true;
    }else{
      backHash = window.location.hash;
    }
	
    var target = 'my_public_profil_panel';
    var elmt = $(target);

	  if(!hide)
	    document.fire('jmdisplay:begun');

	  if(!elmt)
	  {
	    document.fire('jmdisplay:loading');

	    JMAjax.updater('content', '/users/public/_content_public_profile.php', {
	      onComplete: function(){
	        if(!hide)
	        {
	          JMDisplay.scrollFocus();
	          document.fire('jmdisplay:finished', {target: target});
	        }
	        else
	        {
	          $(target).hide();
	        }
	      },
	      insertion:'bottom'
	    }, true);
	  }
	  else
	  {
	    if(!hide)
	      document.fire('jmdisplay:finished', {target: target});
	  }

	  return false;
	},
	payment: function()
{
	  //$('page_title').innerHTML='Compte Premium';
	  JMDisplay.profile();
		
	  var target = 'payment_panel';
	  var elmt = $(target);
	  var jackpot = window.location.hash.match(/jackpot\/([^\/]+)/);
	  document.fire('jmdisplay:begun');
	  if(elmt){
	    elmt.remove();
	  }
	  document.fire('jmdisplay:loading');

    JMAjax.updater('content', 'users/payment/investment.php', {
      onComplete: function(){
        JMDisplay.scrollFocus();
        document.fire('jmdisplay:finished', {target: target});
      },
      insertion:'bottom'
    });

	  return false;
	},
	
	
	my_jackpot: function(withProfile)
	{
	  var withProfile = withProfile == undefined ? true : withProfile;
	  var hide = false;
	  if(window.location.hash != '#my-jackpot') {
      hide = true;
    }else{
      backHash = window.location.hash;
    }

    if(withProfile)
	    JMDisplay.profile();

    var target = 'my_jackpot_panel';
    var elmt = $(target);

	  if(!hide)
	    document.fire('jmdisplay:begun');

	  if(!elmt)
	  {
	    document.fire('jmdisplay:loading');

	    JMAjax.updater('content', '/users/_content_jackpot.php', {
	      onComplete: function(){
	        if(!hide)
	        {
	          JMDisplay.scrollFocus();
	          document.fire('jmdisplay:finished', {target: target});
	        }
	        else
	        {
	          $(target).hide();
	        }
	      },
	      insertion:'bottom'
	    }, true);
	  }
	  else
	  {
	    if(!hide)
	      document.fire('jmdisplay:finished', {target: target});
	  }

	  return false;
	},
	my_jackpot_validate: function(withProfile)
	{
	  var withProfile = withProfile == undefined ? true : withProfile;
	  var hide = false;
	  if(window.location.hash != '#validate-gift') {
      hide = true;
    }else{
      backHash = window.location.hash;
    }

    if(withProfile)
	    JMDisplay.profile();

    var target = 'my_jackpot_panel_validate';
    var elmt = $(target);

	  if(!hide)
	    document.fire('jmdisplay:begun');

	  if(!elmt)
	  {
	    document.fire('jmdisplay:loading');

	    JMAjax.updater('content', '/users/jackpot/_content_jackpot_validate.php', {
	      onComplete: function(){
	        if(!hide)
	        {
	          JMDisplay.scrollFocus();
	          document.fire('jmdisplay:finished', {target: target});
	        }
	        else
	        {
	          $(target).hide();
	        }
	      },
	      insertion:'bottom'
	    }, true);
	  }
	  else
	  {
	    if(!hide)
	      document.fire('jmdisplay:finished', {target: target});
	  }

	  return false;
	},
	

	onair: function(checksum)
	{
	  var elmt = $('onair');

	  elmt.hide();
	  JMAjax.updater(elmt, '/_content_onair.php', {
	    parameters:{'checksum': checksum},
	    onComplete: function()
	    {
	      rewriteJMDisplayLink();
	      $('style_title').hide();
	      elmt.appear({duration: 1});
	    }
	  });
	
	  return false;
	},
	
	
	onair_tag: function(checksum)
	{
	  var elmt = $('advertiser_right_top');

	  elmt.hide();
	  //JMAjax.updater(elmt, '/_content_onair.php', {
	  JMAjax.updater(elmt, '/tag/_content_tag_song_right.php', {
	    parameters:{'checksum': checksum},
	    onComplete: function()
	    {
	      rewriteJMDisplayLink();
	      
	      elmt.appear({duration: 1});
	    }
	  });
	  $('header_hit').hide();
	  JMAjax.updater('advertiser_top', baseUrl+'/_ad.php', {parameters: {place: 1}});
	
	  return false;
	},

	styleTitle: function()
	{
	  $('title_general').hide();
	  $('title_classical').hide();
	  $('title_live').hide();
	  $('title_'+(jmStyle ? jmStyle : 'general')).show();
	  $('style_title').show();

	  $('onair').hide();
	},

	results: function()
	{
	  backHash = window.location.hash;

	  JMDisplay.profile();

	  var target = 'result_panel';
	  var elmt = $(target);

	  var search = window.location.hash.match(/\/(.+)\/(.+)$/);
	  if(search)
	  {
	    var search_type = search[1];
	    var search_query = unescape(search[2]);
	  }

	  document.fire('jmdisplay:begun');

	  if(!elmt)
	  {
	    document.fire('jmdisplay:loading');

	    JMAjax.updater('content', '/_content_results.php', {
	      onComplete: function(){

          if(search_query)
	          JMDisplay.searchTab(search_type, search_query);

	        document.fire('jmdisplay:finished', {target: target});
	      },
	      insertion:'bottom'
	    });
	  }
	  else
	  {
	    if(search_query)
	      JMDisplay.searchTab(search_type, search_query);

	    document.fire('jmdisplay:finished', {target: target});
	  }

	  return false;
	},

	searchTab: function(type, query, tags)
	{
	  var active_tab = false;

	  $$('.search_tab').each(function(elmt) {
	    if (elmt.hasClassName('search_'+type) && elmt.down('span').innerHTML == query) {
	      active_tab = elmt;
	    }
	  });

	  if (!active_tab || tags != undefined)
	  {
	    $('music_search_loading').show();
      $('music_search_submit').hide();

	    if (active_tab && tags != undefined) {
        JMAjax.updater(active_tab.next('.song_list'), '/_content_result_tabs.php', {
  	      onComplete: function(response)
  	      {
  	        if (response.responseText.match(/song_list/)) {
  	          active_tab.next('.song_list').remove();
  	        }

  	        JMDisplay.scrollFocus();
  	        rewriteJMDisplayLink();
  	        JMDisplay.showTab();
  	      },
  	      onSuccess: function()
  	      {
  	        $('music_search_loading').hide();
            $('music_search_submit').show();
  	      },
  	      method: 'get',
  	      parameters: {query: query, type: type, tags: tags, has_tags: 1},
          insertion:'after'
        });
      }
	    else {
	      JMAjax.updater('result_panel', '/_content_result_tabs.php', {
  	      onComplete: function()
  	      {
  	        JMDisplay.scrollFocus();
  	        rewriteJMDisplayLink();
  	        JMDisplay.showTab();
  	      },
  	      onSuccess: function()
  	      {
  	        $('music_search_loading').hide();
            $('music_search_submit').show();
  	      },
  	      method: 'get',
  	      parameters: {query: query, type: type},
          insertion:'bottom'
        });
	    }
	  }
	  else {
	    JMDisplay.showTab(active_tab);
	  }

	  return false;
	},

	showTab: function(elmt, type)
	{
	  if (Object.isString(elmt)) {
	    $$('result_panel').select('.search_'+type).each(function(elt) {
  	    if (elt.down('span').innerHTML == elmt) {
  	      elmt = elt;
  	    }
  	  });
	  }

    var lastElmt;
    var firstElmt;
    var width;
    var top;
	  var tabCount = $('result_panel').select('.search_tab').length;

	  if (tabCount <= 4) {
	    width = 126;
	  }
	  else if (tabCount >= 5) {
	    width = 99;
	  }

    $('result_panel').select('.search_tab').each(function(elt) {
	    if (firstElmt == undefined) {
	      if(Prototype.Browser.IE)
	        elt.setStyle({borderLeft: 0});
	      else
	        elt.setStyle({borderLeft: '1px solid transparent'});

	      firstElmt = elt;
	      top = elt.getStyle('top');
	    }

	    elt.setStyle({width:width+'px'});

	    lastElmt = elt;
	    elt.removeClassName('tab_selected');

	    elt.next().hide();
	  });

	  if (elmt != undefined) {
	    elmt.addClassName('tab_selected');
	    elmt.next().show();
	  }
	  else {
	    lastElmt.addClassName('tab_selected');
	    lastElmt.next().show();
	  }

	  var sep = $('result_panel').down('.box_title').next();

	  if (sep.hasClassName('tab_separator')) {
	    sep.hide();
	  }

	  JMDisplay.scrollFocus();

	  return false;
	},

	deleteTab: function(elmt)
	{
	  var parent = elmt != undefined ? elmt.up() : $('result_panel').down('.search_tab');

	  parent.onclick = function(){};
	  parent.previous().remove();
	  parent.next().remove();

	  JMAjax.request('/result_tabs_a.php', {
      parameters:{query: parent.down('span').innerHTML, type: parent.hasClassName('search_all') ? 'all' : 'radio'},
      onComplete: function(){
        parent.remove();
    	  JMDisplay.showTab();
      }
    });

	  return false;
	},

	misSearch: function()
	{
	  //backHash = window.location.hash;
	  backHash = "http://www.jackmaestro.com/";

	  JMDisplay.profile();

	  var target = 'mis_search_panel';
	  var elmt = $(target);

	 	document.fire('jmdisplay:begun');

	  if(!elmt)
	  {
	    document.fire('jmdisplay:loading');

	    JMAjax.updater('content', '/_content_search.php', {
	      onComplete: function(){
	        document.fire('jmdisplay:finished', {target: target});
	      },
	      insertion:'bottom'
	    });
	  }
	  else
	  {
	    document.fire('jmdisplay:finished', {target: target});
	  }

	  return false;
	},
	misSearchMidi: function()
	{
	  backHash = window.location.hash;

	  JMDisplay.profile();

	  var target = 'mis_search_panel';
	  var elmt = $(target);

	 	document.fire('jmdisplay:begun');

	  if(!elmt)
	  {
	    document.fire('jmdisplay:loading');

	    JMAjax.updater('content', '/_content_search_midi.php', {
	      onComplete: function(){
	        document.fire('jmdisplay:finished', {target: target});
	      },
	      insertion:'bottom'
	    });
	  }
	  else
	  {
	    document.fire('jmdisplay:finished', {target: target});
	  }

	  return false;
	},

	misResult: function()
	{
	  $('music_search_loading').show();
    $('music_search_submit').hide();

	  var options = {
	    parameters: $('mis_search_panel').down('form').serialize(),
	    onComplete: function() {
	      truncateLabel();
	      $('music_search_loading').hide();
		  $('music_search_submit').show();

	    }
	  };
	  //alert('bonjour');
	  //return JMAjax.updater($('mis_search_panel').down('.song_list'), '_content_search_result.php', options);
	  return JMAjax.updater($('mis_search_panel').down('.validate2'), '_content_search_result.php', options);
	},
	misResultInfo: function()
	{
	  $('music_search_loading').show();
    $('music_search_submit').hide();

	  var options = {
	    parameters: $('mis_search_panel').down('form').serialize(),
	    onComplete: function() {
	      truncateLabel();
	      $('music_search_loading').hide();
		  $('music_search_submit').show();

	    }
	  };
	  //alert('bonjour');
	  //return JMAjax.updater($('mis_search_panel').down('.song_list'), '_content_search_result.php', options);
	  return JMAjax.updater($('mis_search_panel').down('.validate2'), '_content_search_resultInfo.php', options);
	},

	accountPlaylist: function()
	{
	  backHash = window.location.hash;

	  JMDisplay.profile();

	  var target = 'account_playlist_panel';
	  var elmt = $(target);

	  var playlist = window.location.hash.match(/\/(\d+)-(.+)$/);
	  var playlist_id = playlist[1];
	  var playlist_label = unescape(playlist[2]);

	  document.fire('jmdisplay:begun');

	  if(elmt){
	    elmt.remove();
	  }

	  document.fire('jmdisplay:loading');

    JMAjax.updater('content', '/users/_content_playlist.php', {
      parameters: {playlist: playlist_id},
      onComplete: function(){
        JMDisplay.scrollFocus();
        document.fire('jmdisplay:finished', {target: target});
      },
      insertion:'bottom'
    }, true);

	  return false;
	},

	accountRadio: function()
	{
	  backHash = window.location.hash;

	  JMDisplay.profile();

	  var target = 'account_radio_panel';
	  var elmt = $(target);

	  document.fire('jmdisplay:begun');

	  if(elmt){
	    elmt.remove();
	  }

	  document.fire('jmdisplay:loading');

    JMAjax.updater('content', '/users/_content_radio.php', {
      onComplete: function(){
        JMDisplay.scrollFocus();
        if(currentParamsPlayer && currentParamsPlayer.type.match(/R$/))
          JMDisplay.highlightPlaying(currentParamsPlayer.type, currentParamsPlayer.key);
        document.fire('jmdisplay:finished', {target: target});
      },
      insertion:'bottom'
    }, true);

	  return false;
	},

	radios: function()
	{
	  backHash = window.location.hash;

	  JMDisplay.profile();

	  var target = 'radio_panel';
	  var elmt = $(target);

	  document.fire('jmdisplay:begun');

	  if(elmt)
	    elmt.remove();

	  document.fire('jmdisplay:loading');

    JMAjax.updater('content', '/_content_radios.php', {
      onComplete: function(){
        JMDisplay.scrollFocus();
        if(currentParamsPlayer && currentParamsPlayer.type.match(/R$/))
          JMDisplay.highlightPlaying(currentParamsPlayer.type, currentParamsPlayer.key);
        document.fire('jmdisplay:finished', {target: target});
      },
      insertion:'bottom'
    });

	  return false;
	},

	youngs: function()
	{
	  backHash = window.location.hash;

	  JMDisplay.profile();

	  var target = 'young_panel';
	  var elmt = $(target);

	 	document.fire('jmdisplay:begun');

	  if(!elmt)
	  {
	    document.fire('jmdisplay:loading');

	    JMAjax.updater('content', '/youngs/_content_youngs.php', {
	      onComplete: function(){
	        document.fire('jmdisplay:finished', {target: target});
	      },
	      insertion:'bottom'
	    });
	  }
	  else
	  {
	    document.fire('jmdisplay:finished', {target: target});
	  }

	  return false;
	},
	jackpot: function()
{
	  JMDisplay.profile();

	  var target = 'jackpot_panel';
	  var elmt = $(target);
	  var jackpot = window.location.hash.match(/jackpot\/([^\/]+)/);
	  if(jackpot)
	  {
		var jackpot_id = 1;
		var jackpot_controller=unescape(jackpot[1]);
	  }
	  document.fire('jmdisplay:begun');
	  if(elmt){
	    elmt.remove();
	  }
	  document.fire('jmdisplay:loading');

    JMAjax.updater('content', 'jackpot/_content_jackpot.php', {
      parameters: {id: jackpot_id, controller: jackpot_controller},
      method: 'get',
      onComplete: function(){
        JMDisplay.scrollFocus();
        document.fire('jmdisplay:finished', {target: target});
      },
      insertion:'bottom'
    });

	  return false;
	},
	
	/*
	Normalement, cela n'est plus nécessaire, directory est toujours appelé via du pur html...pour le référencement google !
	directory: function()
{
	  $('profile').hide();

	  var target = 'jackpot_panel';
	  var elmt = $(target);
	  var directory = window.location.hash.match(/directory\/([^\/]+)/);
	  if(directory)
	  {
		var directory_id = 1;
		var directory_controller=unescape(directory[1]);
	  }
	  document.fire('jmdisplay:begun');
	  if(elmt){
	    elmt.remove();
	  }
	  document.fire('jmdisplay:loading');

    JMAjax.updater('content', 'artistv2/directory/artist/_content_directory.php', {
      parameters: {id: directory_id, controller: directory_controller},
      method: 'get',
      onComplete: function(){
        JMDisplay.scrollFocus();
        document.fire('jmdisplay:finished', {target: target});
      },
      insertion:'bottom'
    });

	  return false;
	},
	*/
	
	
	
	jackpot_moregames: function()
{
	  JMDisplay.profile();

	  var target = 'jackpot_panel';
	  var elmt = $(target);
	  var jackpot = window.location.hash.match(/jackpot\/([^\/]+)/);
	  if(jackpot)
	  {
		var jackpot_id = 1;
		var jackpot_controller=unescape(jackpot[1]);
	  }
	  document.fire('jmdisplay:begun');
	  if(elmt){
	    elmt.remove();
	  }
	  document.fire('jmdisplay:loading');

    JMAjax.updater('content', 'jackpot/_content_more_games_parrainage.php', {
      parameters: {id: jackpot_id, controller: jackpot_controller},
      method: 'get',
      onComplete: function(){
        JMDisplay.scrollFocus();
        document.fire('jmdisplay:finished', {target: target});
      },
      insertion:'bottom'
    });

	  return false;
	},
	shop: function()
{
	  JMDisplay.profile();

	  var target = 'shop_panel_content';
	  var elmt = $(target);
	  var shop = window.location.hash.match(/shop\/([^\/]+)/);
	  if(shop)
	  {
		var shop_id = 1;
		var shop_controller=unescape(shop[1]);
	  }
	  document.fire('jmdisplay:begun');
	  if(elmt){
	    elmt.remove();
	  }
	  document.fire('jmdisplay:loading');

    JMAjax.updater('content', 'shop/_content_shop.php', {
      parameters: {id: shop_id, controller: shop_controller},
      method: 'get',
      onComplete: function(){
        JMDisplay.scrollFocus();
        document.fire('jmdisplay:finished', {target: target});
      },
      insertion:'bottom'
    });

	  return false;
	},
	
	tag: function()
{
	  JMDisplay.profile();

	  var target = 'generic_panel';
	  var elmt = $(target);

	  //var artist = window.location.hash.match(/\/(\d+)-([^\/]+)\/?(.*)$/);
	  var jackpot = window.location.hash.match(/jackpot\/([^\/]+)/);
	  
	  if(jackpot)
	  {
	  
		var jackpot_id = 1;
	    
		var jackpot_controller=unescape(jackpot[1]);
		//alert(artist[2]);
	  }

	  document.fire('jmdisplay:begun');

	  if(elmt){
	    elmt.remove();
	  }

	  document.fire('jmdisplay:loading');

    JMAjax.updater('content', 'tag/_content_tag.php', {
      parameters: {id: jackpot_id, controller: jackpot_controller},
      method: 'get',
      onComplete: function(){
        JMDisplay.scrollFocus();
        document.fire('jmdisplay:finished', {target: target});
      },
      insertion:'bottom'
    });

	  return false;
	},
	

	tagsong: function()
	{
	  backHash = window.location.hash;

	  JMDisplay.profile();

	  var search2 = window.location.hash.match(/\/(.+)\/(.+)$/);
	  //var search2 = window.location.hash.match(/\/(.+)$/);
	  if(search2)
	  {
	    var search_type2 = search2[1];
	    var search_query2 = unescape(search2[2]);
		//var search_query2 = search2[1];
	  }

	  var target = 'account_panel';
	  var elmt = $(target);

	 	document.fire('jmdisplay:begun');

	  if(!elmt)
	  {
	    document.fire('jmdisplay:loading');

	    JMAjax.updater('content', '/tag/tag_song.php', {


	      onComplete: function(){
	        document.fire('jmdisplay:finished', {target: target});
	      },

		  method: 'get',
  	      parameters: {query2: search_query2, type2: search_type2},
	      insertion:'bottom'
	    });
	  }
	  else
	  {
	    document.fire('jmdisplay:finished', {target: target});
	  }

	  return false;
	},
	artist: function()
	{
	  //JMDisplay.profile();
	  $('profile').hide();

	  var target = 'artist_panel';
	  var elmt = $(target);

	  var artist = window.location.hash.match(/\/(\d+)-([^\/]+)\/?(.*)$/);
	  if(artist)
	  {
	    var artist_id = artist[1];
	    var artist_nickname = unescape(artist[2]);
	    var artist_controller = unescape(artist[3]);
	  }

	  document.fire('jmdisplay:begun');

	  if(elmt){
	    elmt.remove();
	  }

	  document.fire('jmdisplay:loading');

    JMAjax.updater('content', 'artistv2/_content_artist.php', {
      parameters: {id: artist_id, controller: artist_controller},
      method: 'get',
      onComplete: function(){
        JMDisplay.scrollFocus();
        document.fire('jmdisplay:finished', {target: target});
      },
      insertion:'bottom'
    });

	  return false;
	},
	
	admin_artist: function()
	{
	  JMDisplay.profile();
	  //$('profile').hide();

	  var target = 'artist_panel';
	  var elmt = $(target);

	  var artist = window.location.hash.match(/\/(\d+)-([^\/]+)\/?(.*)$/);
	  if(artist)
	  {
	    var artist_id = artist[1];
	    var artist_nickname = unescape(artist[2]);
	    var artist_controller = unescape(artist[3]);
	  }

	  document.fire('jmdisplay:begun');

	  if(elmt){
	    elmt.remove();
	  }

	  document.fire('jmdisplay:loading');

    JMAjax.updater('content', '_content_artist.php', {
      parameters: {id: artist_id, controller: artist_controller},
      method: 'get',
      onComplete: function(){
        JMDisplay.scrollFocus();
        document.fire('jmdisplay:finished', {target: target});
      },
      insertion:'bottom'
    });

	  return false;
	},
	
	fan: function()
	{

	  $('profile').hide();
	  var target = 'general';
	  var elmt = $(target);

	  var fan = window.location.hash.match(/\/([^\/]+)\/?(.*)$/);
	  if(fan)
	  {
	    var fan_nickname = fan[1];
	    var fan_controller = unescape(fan[2]);
	  }

	  document.fire('jmdisplay:begun');

	  if(elmt){
	    elmt.remove();
	  }

	  document.fire('jmdisplay:loading');

    JMAjax.updater('content', 'users/public/_content_public_profile.php', {
      parameters: {fan_nickname: fan_nickname, controller: fan_controller},
      method: 'get',
      onComplete: function(){
        JMDisplay.scrollFocus();
        document.fire('jmdisplay:finished', {target: target});
      },
      insertion:'bottom'
    });

	  return false;
	},
	
	jules: function()
	{
	  $('profile').hide();
	  
	   
	  

	  var target = 'artistv2_panel';
	  var elmt = $(target);

	  var artist = window.location.hash.match(/\/(\d+)-([^\/]+)\/?(.*)$/);
	  if(artist)
	  {
	    var artist_id = artist[1];
	    var artist_nickname = unescape(artist[2]);
	    var artist_controller = unescape(artist[3]);
	  }

	  document.fire('jmdisplay:begun');

	  if(elmt){
	    elmt.remove();
	  }

	  document.fire('jmdisplay:loading');
	  
    
	
	JMAjax.updater('content', 'artistv2/_content_artist.php', {
      parameters: {id: artist_id, controller: artist_controller},
      method: 'get',
      onComplete: function(){
        JMDisplay.scrollFocus();
        document.fire('jmdisplay:finished', {target: target});
      },
	  
	  
	  
      insertion:'bottom'
	  });
	  
	  
	 
		

	  return false;
	},

	album: function(id)
	{
	  var target = 'album_song_list_'+id;
	  var elmt = $(target);

	  $('artist_panel').select('.album_song_list').each(function(elt){
      if(!elt.empty())
        elt.hide();
    });
	  elmt.hide();

	  if(!elmt.empty())
	  {
	    new Effect.SlideDown(elmt);
	  }
	  else
	  {
	    JMAjax.updater(target, '_content_album.php', {
        parameters: {id: id},
        method: 'get',
        onComplete: function(){
          new Effect.SlideDown(elmt);
        }
      });
	  }

	  return false;
	},

	login: function()
  {
    JMDisplay.refreshMenu();
    JMDisplay.account(false);
    document.fire('location:changed', {controller: window.location.hash.substr(1)});

    return false;
  },

	begin: function(evt)
	{
	  $$('#content > div').invoke('hide');
	},

	loading: function(evt)
	{
	  $('content').setStyle({background:'url(../images/front/loading.gif) no-repeat 50% 25%'});
	},

	finish: function(evt)
	{
	  rewriteJMDisplayLink();
	  $('content').setStyle({background:'none'});

	  if(evt.memo.target != undefined) {
      $(evt.memo.target).appear({ duration: 0.5 });
    }
	},

	ad: function(evt)
	{
	  var filter = window.location.hash.match(/^#?(live|classical)?\/?$/) ? 'top' : '';

	  if(filter.indexOf('top') == -1) {
	    $('header_hit').hide();
	    JMAjax.updater('advertiser_top', baseUrl+'/_ad.php', {parameters: {place: 1}});
	  }
	  else{
	    //$('advertiser_top').hide();
	    //$('header_hit').show();
	  }

	  if(filter.indexOf('right_top') == -1) {
	    //JMAjax.updater('advertiser_right_top', baseUrl+'/_ad.php', {parameters: {place: 2}});
	  }else{
	    //$('advertiser_right_top').hide();
	  }

	  if(filter.indexOf('right_bottom') == -1) {
	    JMAjax.updater('advertiser_right_bottom', baseUrl+'/_ad.php', {parameters: {place: 3}});
	  }else{
	    $('advertiser_right_bottom').hide();
	  }
	},

	goBack: function(mode)
	{
	  if(mode == undefined || mode == 'history')
    {
      window.history.back();
    }
    else if(mode = 'hash')
    {
      window.location.hash = backHash;
    }

	  return false;
	},

	sort: function(token, type, link)
	{
	  var reference = eval('referenceSong'+token);
	  var order = eval('order'+type+token);

	  order.sort(function(a, b) {
	    return a[1] > b[1] ? 1 : a[1] < b[1] ? -1 : 0;
	  });

    $(link).up('table').select('td').invoke('setStyle', {'background-position': '5px -40px'});

    if (orderAscending) {
      $(link).up().setStyle({'background-position': '5px 0'});
      order.reverse();
    }
    else {
      $(link).up().setStyle({'background-position': '5px -20px'});
    }

    orderAscending = !orderAscending;

	  $('search_'+token).select('li').invoke('remove');

	  var newOrder = [];
	  var i = 0;

	  order.each(function(v) {
	    newOrder.push(v[0]);

	    var li = reference[v[0]];
	    li.removeClassName('odd').removeClassName('even').addClassName(i%2 ? 'odd' : 'even');
	    li.select('.song_button a', '.song_title a').each(function(a) {
	      a.writeAttribute('onclick', a.readAttribute('onclick').replace(/idstart\:\d+/, 'idstart:'+i));
	    });
	    $('search_'+token).insert(li);

	    i++;
	  });

	  if ($(link).up('#mis_search_panel')) {
	    var query = $(link).up('.header_list').next('.content_list').id.match(/_(.+)$/)[1];
	    var type = 'MP';
	    var tags = '';
	  }
	  else {
	    var query = $(link).up('.song_list').previous('.tab_selected').down('span').innerHTML;
	    var type = 'SP';
	    var tags = '';

	    if ($(link).up('.song_list').down('select')) {
	      tags = $F($(link).up('.song_list').down('select'));
	    }
	  }

	  JMAjax.request('/player/playlist_update_a.php', {
	   parameters: {checksums: newOrder.join(','), query: query, type: type},
	   onComplete: function() {
	     JMPlayer.reorderPlaylist(newOrder, query, type, tags);
	   }
	  });

	  return false;
	},

  playVideo: function(id)
  {
    if(currentVideo)
      JMDisplay.stopVideo(currentVideo);

    if(JMPlayer.currentState == 'play')
      JMPlayer.pause();

    var player = $('video_'+id).down('.player');
    var list = $('video_'+id).down('.box_video');

    player.show();
    list.hide();

    currentVideo = id;

    if($('swf_video_'+id))
      setTimeout(function(){ $('swf_video_'+id).sendEvent('playpause'); }, 100);

    return false;
  },

  stopVideo: function(id)
  {
    var player = $('video_'+id).down('.player');
    var list = $('video_'+id).down('.box_video');

    if($('swf_video_'+id))
      $('swf_video_'+id).sendEvent('stop');

    player.hide();
    list.show();

    currentVideo = null;

    return false;
  },

  highlightPlaying: function(type, key, checksum)
  {
    $$('.playing').each(function(elt) {
      elt.removeClassName('playing');
      if(elt.hasClassName('radio_onair'))
      {
        elt.hide();
      }
	  if(elt.hasClassName('radio_onair_bis'))
      {
        elt.hide();
      }
    });

    if(type == 'SP')
    {
      $$('.search_tab').each(function(elmt) {
  	    if (elmt.down('span').innerHTML == key) {
  	      elmt.next().select('li').each(function(elt) {
  	        var checksumTmp = elt.id.match(/song_(\w+)$/)[1];
  	        if(checksum == checksumTmp)
  	          elt.addClassName('playing');
  	      });
  	    }
  	  });
    }
    else if(type == 'MP')
    {
      $('mis_search_panel').select('li').each(function(elt) {
        var checksumTmp = elt.id.match(/song_(\w+)$/)[1];
        if(checksum == checksumTmp)
          elt.addClassName('playing');
      });
    }
    else if(type == 'UP')
    {
      $('account_playlist_panel').select('li').each(function(elt) {
        var checksumTmp = elt.id.match(/song_(\w+)$/)[1];
        if(checksum == checksumTmp)
          elt.addClassName('playing');
      });
    }
    else if(type == 'AP')
    {
      $('artist_panel').select('li').each(function(elt) {
        var checksumTmp = elt.id.match(/song_(\w+)$/)[1];
        if(checksum == checksumTmp)
         elt.addClassName('playing');
      });
    }
    else if(type == 'NP')
    {
      $('artist_panel').select('li').each(function(elt) {
        var checksumTmp = elt.id.match(/song_(\w+)$/)[1];
        if(checksum == checksumTmp)
          elt.addClassName('playing');
      });
    }
    else if(type == "JR" || type == "UR")
    {
      $$('.radio_onair').each(function(elt) {
        if(elt.up().hasClassName('radio_'+key))
        {
          elt.show();
          elt.addClassName('playing');
        }
      });
	  //Ajout
	  
	  $$('.radio_onair_bis').each(function(elt) {
        if(elt.up().hasClassName('radio_bis_'+key))
        {
          elt.show();
          elt.addClassName('playing');
        }
      });
	  
	  //Fin Ajout
    }
    else if(type == "SR")
    {
      $$('.radio_onair').each(function(elt) {
        if(elt.up().hasClassName('radio_'+currentParamsPlayer.variation) && elt.previous('.none').innerHTML == key)
        {
          elt.show();
          elt.addClassName('playing');
        }
      });
    }
	else if(type == 'SPR')
    {
      $$('.search_tab').each(function(elmt) {
  	    if (elmt.down('span').innerHTML == key) {
  	      elmt.next().select('li').each(function(elt) {
  	        var checksumTmp = elt.id.match(/song_(\w+)$/)[1];
  	        if(checksum == checksumTmp)
  	          elt.addClassName('playing');
  	      });
  	    }
  	  });
    }

    return false;
  },

  toggleMISPanel: function(a, variation) {
    var radio_list = $(a).up('.radio_list');

    radio_list.select('.mis_panel').invoke('hide');

    if (variation != undefined) {
      radio_list.down('.mis_panel_'+variation).show();
    }

    return false;
  },

  toggleMISOption: function(a, group_id) {
    var mis_panel = $(a).up('.mis_panel');

    mis_panel.select('.mis_menu a').invoke('removeClassName', 'selected');
    mis_panel.select('.mis_options').invoke('hide');

    $(a).addClassName('selected');

    mis_panel.select('.mis_options').each(function(elmt) {
      if (elmt.down('.none').innerHTML == group_id) {
        elmt.show();
      }
    });

    return false;
  },
  //Lorsque l'on clique sur une combo box, on la passe en paramètre et on execute la fonction suviante
  updateMISSummary: function(select) {
    var form = select.tagName == 'FORM' ? select : select.form;
    var summary = $(form).down('.mis_summary span');

    summary.update();



    $(form).getElements().each(function(input) {
      if (input.tagName == 'SELECT') {
        var label = input.up().previous('.label');
        var option = input.options[input.selectedIndex];

        if (option.selected && option.value) {
          if (summary.empty()) {
            summary.update(label.innerHTML+' '+option.text);
          }
          else {
            summary.insert(', '+label.innerHTML+' '+option.text);
          }
        }
      }

    });

    if (summary.empty()) {
      summary.update('&nbsp;');

    }

    return false;
  },

  updateMISSummary2: function(checkbox) {
	var form = checkbox.tagName == 'FORM' ? select : checkbox.form;
    var summary = $(form).down('.mis_summary span');

	summary.update();
	var option=checkbox.value;
	$(form).getElements().each(function(input) {
	var option=input.value;
	if(option==("focusInstrumentisPiano")){option="Piano";}
	if(option==("focusInstrumentisGuitare")){option="Guitare";}
	if(option==("sexVoiceisMale")){option="Voix Masculine";}
	if(option==("sexVoiceisFemale")){option="Voix Féminine";}
	if(option==("formationisSolo")){option="Solo";}
	if(option==("formationisBand")){option="Groupe";}
	if(input.checked){
          if (summary.empty()) {
		  summary.update(option);
          }
          else {
		  summary.insert(', ' + option );
          }
	}
	//alert (option);
	}
	)

    if (summary.empty()) {
      summary.update('&nbsp;');

    }

    return false;
  }

};

var JMAjax =
{
  dragInProgress: false,

  updater: function(container, url, options, logged)
  {

    options = options == undefined ? {} : options;
    options.parameters = options.parameters == undefined ? {} : options.parameters;
    options.parameters.jm_style = jmStyle;

    logged = logged == undefined ? false : logged;
    options.evalScripts = true;

    if(!logged)
    {

      new Ajax.Updater(container, baseUrl+url, options);

    }
    else
    {
      new Ajax.Request(baseUrl+'/users/is_logged.php', {
    		onSuccess: function() {

    			new Ajax.Updater(container, baseUrl+url, options);
    		},

    		onFailure: function() {

    			JMDisplay.modal('/users/login.php');
    		}
    	});
    }

  	return false;
  },

  request: function(url, options, logged)
  {
    options = options == undefined ? {} : options;
    logged = logged == undefined ? false : logged;

    if(!logged)
    {
      new Ajax.Request(baseUrl+url, options);
    }
    else
    {
      new Ajax.Request(baseUrl+'/users/is_logged.php', {
    		onSuccess: function() {
    			new Ajax.Request(baseUrl+url, options);
    		},

    		onFailure: function() {
    			JMDisplay.modal('/users/login.php');
    		}
    	});
    }

    return false;
  },

  parseDroppablePlaylists: function()
  {
    var options = {
      hoverclass:'droparea_hover',
      onDrop: function(dragElmt, dropElmt) {
        if(dragElmt.id) {
          var checksum = dragElmt.id.match(/song_(\w+)$/)[1];
          JMAjax.addToPlaylist(dropElmt.id.match(/_(\d+)$/)[1], checksum);
        }
      }
    };

    $$('.droppable_playlist').each(function(elmt) {
	    Droppables.add(elmt, options);
	  }).invoke('removeClassName', 'droppable_playlist');
  },

  parseDraggableSongs: function()
  {
    $$('.draggable_song').each(function(elmt) {

     	new Draggable(elmt, {
  	    handle: 'song_title',
  	    revert: true,
  			ghosting: Prototype.Browser.IE ? false : true,
  			onStart: function()
        {
          Control.Modal.close();

          if(Prototype.Browser.IE)
            elmt.up('.content_list').setStyle({position: 'static'});

          elmt.addClassName('ghost');
          JMAjax.dragInProgress = true;
        },
        onEnd: function()
        {
          if(Prototype.Browser.IE)
            elmt.up('.content_list').setStyle({position: 'relative'});

          elmt.removeClassName('ghost');
          setTimeout(function(){ JMAjax.dragInProgress = false }, 20);
        }
  	  });

	  }).invoke('removeClassName', 'draggable_song');

	  $$('.disable_button').invoke('setOpacity', 0.5);
  },

  parseSortableSongs: function(elmtId)
  {
    var options = {
      tag: 'li',
      constraint: false,
      onUpdate: function() {
        JMAjax.orderSong(elmtId.match(/_(\d+)$/)[1], Sortable.serialize(elmtId, {name: 'songs'}));
        var i = 0;
        var newOrder = [];
        $$('#'+elmtId+' li').each(function(elmt) {
    	    elmt.removeClassName('odd').removeClassName('even').addClassName(i%2 ? 'odd' : 'even');
    	    elmt.innerHTML = elmt.innerHTML.replace(/idstart\:\d+/, 'idstart:'+i);
    	    elmt.down('span.position').update(i+1);
      	  newOrder.push(elmt.id.match(/song_(\w+)$/)[1]);
    	    i++;
    	  });
    	  JMPlayer.reorderPlaylist(newOrder, elmtId, 'UP', '');
      }
    };

   Sortable.create(elmtId, options);
  },

  orderSong: function(playlist, songs)
  {
    return JMAjax.request('/users/playlist_order_a.php', {parameters: {playlist: playlist, songs: songs}}, true);
  },

  addToPlaylist: function(playlist, song)
  {
    return JMAjax.request('/users/playlist_add_a.php', {parameters: {playlist: playlist, song: song}}, true);
  },

  removeFromPlaylist: function(playlist, song, text)
  {
    var method = 'JMAjax.request(\'/users/playlist_remove_a.php\', {parameters: {playlist: \''+playlist+'\', song: \''+song+'\'}}, true)';

    return JMDisplay.modalConfirm(method, text, true);
  },

  addRadio: function(key, type, variation)
  {
    return JMAjax.request('/users/radio_add_a.php', {parameters: {key: key, type: type, variation: variation}}, true);
  },

  deletePlaylist: function(playlist, text)
  {
    var method = 'JMAjax.request(\'/users/playlist_delete_a.php\', {parameters: {playlist: \''+playlist+'\'}}, true)';

    return JMDisplay.modalConfirm(method, text, true);
  },

  deleteAccountImage: function(text)
  {
    if(window.confirm(text))
    {
      return JMAjax.request('/users/image_delete_a.php', {}, true);
    }

    return false;
  }
};

var JMPlayer =
{
  id:'music_player',
  currentState:'init',

  refreshCurrentSong: function(type, key, checksum)
  {
    JMDisplay.onair(checksum);
	JMDisplay.onair_tag(checksum);

    JMDisplay.highlightPlaying(type, key, checksum);

    $$('.mis_footer_legend span').invoke('fade', {duration:0.5});

    document.fire('jmdisplay:ad');
  },

  changeCover: function(href, src)
  {
    var img_cache = $('music_cover').down('img');
    href=href.replace('http://www.jackmaestro.com','');
    if (!src || img_cache.src == src) {
      return;
    }

    img_cache.src = src;
	
    var link = $('music_cover').down('a');
	
    if (!link) {
      $('music_cover').insert(new Element('a', {href:href, style:'background-image: url('+src+')'}));
      var link = $('music_cover').down('a');
      link.addClassName('jmdisplay_link');
      rewriteJMDisplayLink();
      return;
    }

    setTimeout(function(){
      link.fade({
        duration: 1.75,
        afterFinish: function() {
          link.href = href;
          link.addClassName('jmdisplay_link');
          link.setStyle({backgroundImage: 'url('+src+')'});
          link.appear({duration: 1.75});
          rewriteJMDisplayLink();
        }
      });
    }, 500);
  },

  buy: function(checksum)
  {
    JMDisplay.modal('/buy.php?checksum='+checksum);
  },

  addToFavorite: function(song)
  {
    JMAjax.addToPlaylist(null, song);
  },

  load: function(params)
  {
    currentParamsPlayer = params;

    $(JMPlayer.id).load(params);

    return false;
  },

  pause: function()
  {
    $(JMPlayer.id).pause();

    return false;
  },

  updateTags: function(form)
  {
    var tags = '';

    $(form).select('select').each(function(elmt) {

      if (elmt.value) {
        tags += tags ? ' '+elmt.value : elmt.value;
      }
    });
	var option=$(form).checkbox.value;
	$(form).getElements().each(function(input) {
	if(input.checked){
	tags += tags ? ' '+input.value : input.value;
	//alert(input.value);
	}
	})
	//alert($(form)[0]);
	//alert(option);
	 $(form).select('checkbox').each(function(elmt) {
	 //alert("jai trouvé checkbox");
      if (elmt.value) {
        tags += tags ? ' '+elmt.value : elmt.value;
      }

    });
    //alert(tags);
    $(form).down('.mis_footer_legend span').appear({duration:0.5});

    $(JMPlayer.id).updateTagsV2(tags);
	//$(JMPlayer.id).next();

    return false;
  },
  
  updateTagsV2: function(form)
  {
    var tags = '';
	
   $(form).select('hidden').each(function(elmt) {

      if (elmt.value) {
        tags += tags ? ' '+elmt.value : elmt.value;
		//alert(tags);
      }
    });
	
	
	$(form).getElements().each(function(input) {
	if(input.value!=0 & input.value!='VALIDER' ){
	tags += tags ? ' '+input.value : input.value;
	//alert(input.value);
	}
	})
	
	
	/*
	 $(form).select('checkbox').each(function(elmt) {
	 
      if (elmt.value) {
        tags += tags ? ' '+elmt.value : elmt.value;
      }

    });
	*/
    //alert(tags);
	//alert("tags:" + tags);
    $(JMPlayer.id).updateTags(tags);
	$(JMPlayer.id).next();

    return false;
  },
  
  updateTagsWord: function(tag)
  {
    var tags = tag;


    $(JMPlayer.id).updateTags(tags);

    return false;
  },

  failedUpdateTags: function(msg)
  {
    JMDisplay.modal('', msg);

    $$('.mis_footer_legend span').invoke('fade', {duration:0.5});
  },

  login: function()
  {
    $(JMPlayer.id).login();
    return false;
  },

  logout: function()
  {
    $(JMPlayer.id).logout();
    return false;
  },

  reorderPlaylist: function(order, query, type, tags)
  {
    $(JMPlayer.id).reorderPlaylist(order, query, type, tags);
  },

  getCookie: function(cookie)
  {
    var results = document.cookie.match('(^|;) ?'+cookie+'=([^;]*)(;|$)');

    return results ? unescape(results[2]) : null;
  },

  setCurrentState: function(state)
  {
    JMPlayer.currentState = state;
  },

  navigateToAlbum: function(url)
  {
    window.location.hash = '#'+url;
  },

  loadDefault: function()
  {
    JMAjax.request('_home_playlist.php', {parameters:{jm_style: jmStyle}});
  }
};

var JMTabs =
{
  playAlbum: function(id)
  {
    JMPlayer.load({key:id, type:'AP', autostart:true});
  },

  playRadio: function(id)
  {
    JMPlayer.load({key:id, type:'JR', autostart:true, idstart:'random'});
  },

  playRadioMIS: function(query)
  {
    autostart = true;
    window.location.hash = "#result";
    JMDisplay.searchTab('radio', query);
  },

  goToURL: function(hash)
  {
    window.location.hash = hash;
  }
};

function truncateLabel() {
  function _truncate(elmt, length) {
    elmt = $(elmt);

    if (elmt.innerHTML.length > length) {
      var str = elmt.innerHTML;
      elmt.observe('mouseover', function(){ JMDisplay.hoverbox(elmt, str); });
      elmt.innerHTML = elmt.innerHTML.truncate(length);
    }
  }

  $$('#account_playlist_panel .song_title a', '#result_panel .song_title a', '#mis_search_panel .song_title a').each(function(elmt) {
    _truncate(elmt, 24);
  });

  $$('#account_playlist_panel .song_artist a', '#result_panel .song_artist a', '#mis_search_panel .song_artist a').each(function(elmt) {
    _truncate(elmt, 20);
  });

  $$('#account_playlist_panel .song_album a', '#result_panel .song_album a', '#mis_search_panel .song_album a').each(function(elmt) {
    _truncate(elmt, 18);
  });

  $$('#artist_panel .song_list_new_artist .song_title a').each(function(elmt) {
    _truncate(elmt, 40);
  });

  $$('#artist_panel .song_list_new_artist .song_album a').each(function(elmt) {
    _truncate(elmt, 30);
  });

  $$('#home_top_albums .box_artist .title', '#home_new_albums .box_artist .title').each(function(elmt) {
    _truncate(elmt, 24);
  });

  $$('#home_top_albums .box_artist .subtitle', '#home_new_albums .box_artist .subtitle').each(function(elmt) {
    _truncate(elmt, 35);
  });

   $$('#artist_panel .song_list .song_title a').each(function(elmt) {
    _truncate(elmt, 70);
   });

   $$('#artist_panel .box_artist .title', '#onair .box_artist .title').each(function(elmt) {
     _truncate(elmt, 38);
   });

   $$('#result_panel .search_tab .title').each(function(elmt) {
     _truncate(elmt, 15);
   });
}

function rewriteJMDisplayLink() {
  $$('.jmdisplay_link').each(function(elmt) {
    if (elmt.href.match(/^http:\/\/[^\/]+\/.{2}\//) || elmt.href.match(/^http:\/\/[^\/]+\/.{2}$/)) {
      //elmt.href = elmt.href.replace(/^http:\/\/[^\/]+\/.{2}\/?/, '#');
	  elmt.href = elmt.href.replace(/^http:\/\/[^\/]+\/.{2}\/?/, 'http://www.jackmaestro.com/#');
    }
    else {
      //elmt.href = elmt.href.replace(/^http:\/\/[^\/]+\/(classical|live)\/.{2}(\/.+)?$/, '#$1$2');
	  elmt.href = elmt.href.replace(/^http:\/\/[^\/]+\/(classical|live)\/.{2}(\/.+)?$/, 'http://www.jackmaestro.com/#$1$2');
    }
  }).invoke('removeClassName', 'jmdisplay_link');

  truncateLabel();
}

function rewriteCountryLink(link) {
  link.href += window.location.hash.replace(/#(classical|live)/, '');
}

function token()
{
  var chars = '0123456789abcdefghiklmnopqrstuvwxyz';
  var length = 40;
  var token = '';

  for (var i=0; i<length; i++) {
  	var rnum = Math.floor(Math.random() * chars.length);
  	token += chars.substring(rnum, rnum+1);
  }

  return token;
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

var uploadStart = function(id, fileObj, bytesLoaded)
{

}

var uploadProgress = function(id, fileObj, bytesLoaded)
{
  var percent = Math.ceil((bytesLoaded / fileObj.size) * 100);

	if (percent < 10) {
	  percent = '0'+percent;
	}
  
  if($(id+'-output'))
	  $(id+'_output').update(percent+'%');
}

var uploadCompleteYoungSubscription = function(id, fileObj)
{
  if($(id+'-output'))
	  $(id+'_output').update(fileObj.name);
	  
  $(id+'_file').value = encodeURIComponent(fileObj.name);
  $('bt_submit').removeAttribute('disabled');
  $('bt_submit').setOpacity(1);
}

var uploadCompleteProposal = function(id, fileObj)
{
  if($(id+'-output'))
	  $(id+'_output').update(fileObj.name);
	  
  $(id+'_file').value = encodeURIComponent(fileObj.name);
}

var uploadCompleteUserImage = function(id, fileObj)
{
  JMDisplay.profile();
}

function setJmStyle(controller) {
  var oldJmStyle = jmStyle;

  jmStyle = '';

  if (controller.match(/^classical/)) {
    jmStyle = 'classical';
  }
  else if (controller.match(/^live/)) {
    jmStyle = 'live';
  }

  if (oldJmStyle != jmStyle && $('mis_search_panel')) {
    $('mis_search_panel').remove();
  }
}

function mkw(str)
{
  var search  = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ';
  var replace = 'AAAAAAaaaaaaOOOOOOooooooEEEEeeeeIIIIiiiiUUUUuuuuyNn';

  for (var i=0; i<search.length; i++) {
    str = str.replace(search.charAt(i), replace.charAt(i));
  }

  return str.toLowerCase();
}


document.observe('location:changed', function(evt) {
  pageTracker._trackPageview(window.location.hash.replace('#', '/'));

  var controller = evt.memo.controller;

  if (controller == 'account') {
    JMDisplay.account();
  }
  else if (controller.match(/^account\/playlist/)) {
    JMDisplay.accountPlaylist();
  }
  else if (controller.match(/^account\/radio/)) {
    JMDisplay.accountRadio();
  }
  else if (controller.match(/^((classical|live)\/)?radio/)) {
    setJmStyle(controller);
    JMDisplay.radios();
  }
  else if (controller.match(/^((classical|live)\/)?top_album/)) {
    setJmStyle(controller);
    JMDisplay.generic('/_content_top_albums.php');
  }
  else if (controller.match(/^((classical|live)\/)?top_radio/)) {
    setJmStyle(controller);
    JMDisplay.generic('/_content_top_radios.php');
  }
  else if (controller.match(/^((classical|live)\/)?new_album/)) {
    setJmStyle(controller);
    JMDisplay.generic('/_content_new_albums.php');
  }
  else if (controller.match(/^new_artist/)) {
    JMDisplay.youngs();
  }
  else if (controller.match(/^jackpot/)) {
    JMDisplay.jackpot();
  }
  else if (controller.match(/^payment/)) {
    JMDisplay.payment();
  }
  else if (controller.match(/^directory/)) {
    JMDisplay.directory();
  }
  else if (controller.match(/^shop/)) {
    JMDisplay.shop();
  }
  else if (controller.match(/^midimis/)) {
    //JMDisplay.misSearchMidi();
  }
  else if (controller.match(/^my-jackpot/)) {
    JMDisplay.my_jackpot();
  }
  else if (controller.match(/^jules/)) {
    JMDisplay.jules();
  }
  else if (controller.match(/^validate-gift/)) {
    JMDisplay.my_jackpot_validate();
  }
  else if (controller.match(/^test/)) {
    JMDisplay.test();
  }
  else if (controller.match(/^my-tags/)) {
    JMDisplay.my_tags();
  }
  else if (controller.match(/^my-public-profile/)) {
    JMDisplay.my_public_profile();
  }
  else if (controller.match(/^my-stats/)) {
    JMDisplay.my_stats();
  }
  else if (controller.match(/^my-messages/)) {
    JMDisplay.my_messages();
  }
  else if (controller.match(/^my-loteries/)) {
    JMDisplay.my_loteries();
  }
  else if (controller.match(/^my-jackbox/)) {
    JMDisplay.my_jackbox();
  }
  else if (controller.match(/^tagsong/)) {
    JMDisplay.tagsong();
  }
   else if (controller.match(/^tag/)) {
    JMDisplay.tag();
  }
  else if (controller.match(/^artist/)) {
    JMDisplay.artist();
  }
  else if (controller.match(/^admin-artist/)) {
    JMDisplay.admin_artist();
  }
   else if (controller.match(/^fan/)) {
    JMDisplay.fan();
  }
  else if (controller.match(/^result/)) {
    JMDisplay.results();
  }
  else if (controller.match(/^page/)) {
    var page = window.location.hash.match(/page\/([^\/]+)/);
    if(page) {
      JMDisplay.generic('/pages/index.php', {parameters:{controller: page[1]}});
    }
  }
  else if (controller.match(/^((classical|live)\/)?search/)) {
    setJmStyle(controller);
    JMDisplay.misSearch();
  }
  else {
    setJmStyle(controller);
    JMDisplay.home();
  }
  document.fire('jmdisplay:ad');

  $('content').show();
});

document.observe('jmdisplay:begun', JMDisplay.begin);
document.observe('jmdisplay:loading', JMDisplay.loading);
document.observe('jmdisplay:finished', JMDisplay.finish);
document.observe('jmdisplay:ad', JMDisplay.ad);
JMAjax.updater('advertiser_right_top', baseUrl+'/_ad.php', {parameters: {place: 2}});

Event.observe(window, 'load', function() {
  if (Prototype.Browser.IE) {
    window.location.old_fragment = undefined;

    setInterval(function() {
      if (window.location.old_fragment == undefined) {
        $$('body').first().insert('<iframe id="history_frame" class="clear" src="/blank.html?' + token() + window.location.hash + '"></iframe>');
      }
      else if (window.location.hash != window.location.old_fragment) {
        $('history_frame').src = '/blank.html?' + token() + window.location.hash;
      }
      window.document.title = 'Jack Maestro';
    }, 200);
  }
  else {
    if(!window.location.hash)
      document.fire('jmdisplay:ad');

    var old_fragment = '';

    setInterval(function() {
      if (window.location.hash != old_fragment) {
        document.fire('location:changed', {controller: window.location.hash.substr(1)});
        old_fragment = window.location.hash;
      }
    }, 100);
  }

  rewriteJMDisplayLink();

  $('music_search').down('input.text').observe('focus', function() {
    if (!this._cleared) {
      this.clear();
      this._cleared = true;
    }
  }).focus();

  $('music').insert('<div id="'+JMPlayer.id+'"><a href="http://www.adobe.com/go/getflashplayer">Get the Flash Player</a> to see this player.</div>');
  swfobject.embedSWF("/player/maestro.swf", JMPlayer.id, "676", "74", "9.0.0", "", {}, {wmode:"transparent", menu:"false", allowScriptAccess:'sameDomain'});

  $('music_search_form').onsubmit = function(evt){
    var elmt = $('music_search_form');

    if(elmt.serialize(true).type == 'radio')
      autostart = true;

    var hash = '#result/'+elmt.serialize(true).type+'/'+elmt.serialize(true).query;

    if(hash == window.location.hash)
      JMDisplay.searchTab(elmt.serialize(true).type, elmt.serialize(true).query);
    else
      window.location.hash = hash;

    return false;
  };

  $('music_search_form').select('input[type=radio]').invoke('observe', 'change', function() {
    $('music_search_form').select('h3').invoke('toggle');
  });

  

});