/** 
 Global functions
**/
function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

$('document').ready(function(){
	setTimeout(function(){
		//document.getElementById('player_PlaylistBar').style.right = '0px';
		document.getElementById('PlaylistBar_flash').width = '650';
		document.getElementById('PlaylistBar_flash').height = '349';
	}, 1000);
});

/**
 Functions for Main player 
**/
        
function fromMasterToJs() {
	var box_id = arguments[0];
	if(!isLogged()&&(box_id=='PlaylistBar'||box_id=='AddBar')&&arguments[1]=="_boxToggle"){
		/**
 		do nothing 
		**/
	}else{
		toFlashChild(arguments[0] + "_flash", arguments[1], arguments[2]);
	}
}
function controleLoaded(){
	var box_id = arguments[0];
	var controlBox = document.getElementById('player_'+box_id);
	controlBox.style.right=6000;
}

var plZIndex = 300;
function showPlayerControl(){

  plZIndex ++;
	var box_id = arguments[0];
	if(!isLogged()&&(box_id=='PlaylistBar'||box_id=='AddBar')){
		initAKForm('login');
	}else{
		var on = arguments[1];
		var controlBox = document.getElementById('player_'+box_id);

		if(!on)
			controlBox.style.right = (!on && controlBox.style.right.indexOf('000') != -1 ? '6000px' : '0px');
		else{
			var right = controlBox.className.replace('r','');
			controlBox.style.right = right + 'px';
		}
    
    $('#player_'+box_id).css('z-index', plZIndex);//.css('right', '0px');
    //controlBox.style.right = '0px';
		//controlBox.style.right = (!on && controlBox.style.right.indexOf('000') != -1 ? '6000px' : '0px');
	}
}
function toFlashMaster(funcName, args){
	getFlashMovie('BuffaPlayer').callFlashFunc(funcName, args);	
}
		
/**
 Function for all the child controls 
**/

function toFlashChild(movieName, funcName, args){
	getFlashMovie(movieName).callFlashFunc(funcName, args);
}
function hidePlayerControl(){
	var box_id = arguments[0];
	document.getElementById('player_'+box_id).style.right = '6000px';
}
function fromFlashToJs(){
	toFlashMaster(arguments[0], arguments[1]); 
}

function bindHover(){
  $(document).bind('mouseover', onHover);
}
function unbindHover(){
  $(document).unbind('mouseover', onHover);
}

function onHover(){
  toFlashMaster('stopSeekDrag');
  getFlashMovie('VolumeBar_flash').callFlashFunc('_parent._endDrag',null);
}

function onChangePlayList(playlist_id) {
    player.setCurrentPlaylist(playlist_id);
}

function playSong(song_id, playlist_id){
	
	//it's old
  //getFlashMovie('PlaylistBar_flash').callFlashFunc('_parent._playSong', [song_id, playlist_id]);


  //it's new
  player.onStartSong(song_id, function(){
      getFlashMovie('PlaylistBar_flash').callFlashFunc('_parent._playSong', [song_id, playlist_id]);
  });
  /*
  $.ajax({
    data:{idAlbum: idAlbum},
    dataType : 'json',
    type: 'POST',
    url: '/Playlists/AjaxPlaySong',
    success:function(data){
      if ('undefined' != typeof data.result && data.result){
        player.playSong(data.idSong, data.idHistory);
      }else{
        alert('Не удалось воспроизвести альбом!', 'Ошибка')
      }
		},
    error : function(XMLHttpRequest, textStatus, errorThrown){
      alert('во время воспроизведения альбома произошла ошибуа.', 'Ошибка...')
    }

  });
  */

}

function alertFromFlash(str){
  console.log('fromFlash: ', str);
}


function refreshPlaylist(idPlayList){

   //toFlashChild('PlaylistBar','_parent.refreshPlaylist',null);

  if ('undefined' == typeof idPlayList){
    refreshPlaylistList();
  }else{
    getFlashMovie('PlaylistBar_flash').callFlashFunc('_parent.refreashPlaylist', idPlayList);
  }
	//getFlashMovie('PlaylistBar_flash').callFlashFunc('_parent.refreashPlaylist', null);
    
}


function refreshPlaylistList(){
  getFlashMovie('PlaylistBar_flash').callFlashFunc('_parent.getPlaylists', null);
	getFlashMovie('AddBar_flash').callFlashFunc('_parent.getPlaylists', null);
}

function startSong(song_id, pListType){
	//xmlrpc_start_song(song_id);
  if (pListType != 'history'){
    player.onStartSong(song_id);
    //alert(song_id);
  }
	xmlrpc_reload_top_similar(song_id);
  
}

function updateSongStatistic(song_id){
	xmlrpc_update_song_statistic(song_id);
}

function addSongToNewList(playlist_name, song_id){
	//xmlrpc_add_to_new_playlist(playlist_name, song_id);
  player.addToNewPlayList('song', song_id, playlist_name);
}

function addSongToExistingList(playlist_id, song_id){
	//xmlrpc_add_to_playlist(playlist_id, song_id);
  player.addToPlayList(playlist_id, 'song', song_id);
}


function isLogged(){
	return is_logged;
}

function PlayerLogoClick(){
  $.Ajaxor.goToLink('/');
}