$(document).ready(function(){
	// email anti-spam
	$('span.snd_eml').each(function(i){
		var c = $(this).text().split(' (0) ').join('@');
		/*
		var t = $(this).attr('title');
		if (t)	$(this).replaceWith('<a href="mailto:' + c + '">' + t + '</a>');
		else*/  $(this).replaceWith('<a href="mailto:' + c + '">' + c + '</a>');
	});
	
	// open/close "info about studio"
	$('.searchbox > .box .right_h .expand','#center').click(function(){
    var thisElm = $(this);
		thisElm.parent().find('.addinfo').slideToggle();
		//thisElm.parent().children().children('.addinfo').slideToggle();
		thisElm.toggleClass('shrink');
		if(thisElm.text() == 'свернуть') thisElm.text('подробнее о студии');
		else thisElm.text('свернуть');
		return false;
	});
	
	//	$('#container').append('<a href="bmru.htm" id="bmru"></a>');
	
	$('a.outerlink').click(function(){
    this.target = "_blank";
  });
  
	
});

window.onload = function() {
  var playerCont = $('.jquery_jplayer_class','#center');
	if (playerCont.length > 0) {
		playerCont.each(function(){
      var playerID = $(this).attr('id');
      var mp3 = $('.voicebox a[rel="'+playerID+'"]','#center').attr('href');
      $('#'+playerID).jPlayer({
        ready: function() {this.element.jPlayer("setFile", mp3);},
        swfPath: "/js/",
        nativeSupport: true,
        volume: 40,
        customCssIds: true,
        preload: 'none'
      })
      .jPlayer("cssId", "pause", "jqp_pause_"+playerID)
      .jPlayer("cssId", "play", "jqp_play_"+playerID)
      .jPlayer("cssId", "stop", "jqp_stop_"+playerID)
      .jPlayer("cssId", "loadBar", "jqp_load_bar_"+playerID)
      .jPlayer("cssId", "playBar", "jqp_play_bar_"+playerID)
      .jPlayer("cssId", "volumeMin", "jqp_volume_min_"+playerID)
      .jPlayer("cssId", "volumeMax", "jqp_volume_max_"+playerID)
      .jPlayer("cssId", "volumeBar", "jqp_volume_bar_"+playerID)
      .jPlayer("cssId", "volumeBarValue", "jqp_volume_bar_value_"+playerID)
      .jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
        $('#jqp_play_time_'+playerID).text($.jPlayer.convertTime(playedTime));
        $('#jqp_total_time_'+playerID).text($.jPlayer.convertTime(totalTime));
      })
      .jPlayer("onSoundComplete", function() {
        this.element.jPlayer("stop");
      });
      
      $("#jqp_play_"+playerID).click(pausePlayer);
      $("#jqp_load_bar_"+playerID).click(pausePlayer);
      $("#jqp_play_bar_"+playerID).click(pausePlayer);
		});
	}
	
	function pausePlayer(e) {
    var playingCont = $('#playing');
		if(playingCont.length > 0) {
      playingCont.find('.jquery_jplayer_class').jPlayer('pause');
      playingCont.removeAttr('id');
    }
    var initiator = $(e.target);
    initiator.parents('.playercont').attr('id','playing');
    $('#jqp_total_time_'+initiator.attr('rel')).show();
	}
}
