var MINIPLAYER = "miniplayer";
var MAINPLAYER = "mainplayer";
var POPUPPLAYER = "popupPlayer";

var _fileID = "";

function setPopupId(win)
{
	xoombox_popupPlayer=win;
}

function thisMovie(movieName) 
{
	//var isIE = navigator.appName.indexOf("Microsoft") != -1;
	//return (isIE) ? window[movieName] : document[movieName];
	return document.getElementById(movieName);
}


function pausePlayer(swfName)
{
//alert(swfName);
	var win = window;
	if (swfName==POPUPPLAYER) win = window.opener;
	
	if (swfName!=MINIPLAYER && win.thisMovie(MINIPLAYER)) win.thisMovie(MINIPLAYER).pausePlayer();
	
	if (swfName!=MINIPLAYER && win.thisMovie(MINIPLAYER)) win.thisMovie(MINIPLAYER).pausePlayer();
	if (swfName!=MAINPLAYER && win.thisMovie(MAINPLAYER)) win.thisMovie(MAINPLAYER).pausePlayer();
	if (swfName!=POPUPPLAYER && win.xoombox_popupPlayer && win.xoombox_popupPlayer.thisMovie(POPUPPLAYER)) win.xoombox_popupPlayer.thisMovie(POPUPPLAYER).pausePlayer();
	
}

function setVolume(current_volume, swfName)
{
	var win = window;
	SetCookie('current_volume', current_volume);
	
	if (swfName==POPUPPLAYER) win = window.opener;
	
	if (swfName!=MINIPLAYER && win.thisMovie(MINIPLAYER)) win.thisMovie(MINIPLAYER).setVolume(current_volume);
	if (swfName!=MAINPLAYER && win.thisMovie(MAINPLAYER)) win.thisMovie(MAINPLAYER).setVolume(current_volume);
	if (swfName!=POPUPPLAYER && win.xoombox_popupPlayer && win.xoombox_popupPlayer.thisMovie(POPUPPLAYER)) win.xoombox_popupPlayer.thisMovie(POPUPPLAYER).setVolume(current_volume);
}

function updateProgressBar(position, duration, prog, swfName)
{
	var win = window;
	SetCookie('sound_position', position);
	SetCookie('sound_duration', duration);
	SetCookie('sound_progress', prog);
	if (swfName==POPUPPLAYER) win = window.opener;
	
	if (swfName!=MAINPLAYER && win.thisMovie(MAINPLAYER)) win.thisMovie(MAINPLAYER).updateProgressBar(position, duration, prog);
	if (swfName!=POPUPPLAYER && win.xoombox_popupPlayer && win.xoombox_popupPlayer.thisMovie(POPUPPLAYER)) win.xoombox_popupPlayer.thisMovie(POPUPPLAYER).updateProgressBar(position, duration, prog);
	
}

function setTempUserRating(user_rating, swfName)
{
	var win = window;
	if (swfName==POPUPPLAYER) win = window.opener;
	
	if (swfName!=MAINPLAYER && win.thisMovie(MAINPLAYER)) win.thisMovie(MAINPLAYER).setTempUserRating(user_rating);
	if (swfName!=POPUPPLAYER && win.xoombox_popupPlayer && win.xoombox_popupPlayer.thisMovie(POPUPPLAYER)) win.xoombox_popupPlayer.thisMovie(POPUPPLAYER).setTempUserRating(user_rating);
}

function setUserRating(user_id, track_id, user_rating, swfName)
{
	//alert("wywolanie zewnetrznej f-cji ustawienia userRating dla track'u: user_id: "+user_id+" track_id: "+track_id+" user_rating: "+user_rating);
	raiseAsyncPostback(user_id,track_id,user_rating);
	var win = window;
	if (swfName==POPUPPLAYER) win = window.opener;
	
	if (swfName!=MAINPLAYER && win.thisMovie(MAINPLAYER)) win.thisMovie(MAINPLAYER).setUserRating(track_id, user_rating);
	if (swfName!=POPUPPLAYER && win.xoombox_popupPlayer && win.xoombox_popupPlayer.thisMovie(POPUPPLAYER)) win.xoombox_popupPlayer.thisMovie(POPUPPLAYER).setUserRating(track_id, user_rating);
}
	
function changeTrack(current_list, current_track, swfName)
{	
	//alert(swfName);
	
	var cookie_current_track = GetCookie('current_track');
	
	if (String(current_track)!=String(cookie_current_track))
	{	
		var win = window;
		if (swfName!=MAINPLAYER) win = window.opener;
		if (win.thisMovie(MAINPLAYER)) 
		{	win.thisMovie(MAINPLAYER).stopUpdateProgressBar();}
		if (win.xoombox_popupPlayer && win.xoombox_popupPlayer.thisMovie(POPUPPLAYER)) 
		{	win.xoombox_popupPlayer.thisMovie(POPUPPLAYER).stopUpdateProgressBar();}
		
		sound_position='0';
		SetCookie('sound_position', sound_position);
	}
	
	SetCookie('current_list', current_list);
	SetCookie('current_track', current_track);
	

	
	var win = window;
	if (swfName!=MAINPLAYER) win = window.opener;
	
	if (swfName!=MAINPLAYER && win.thisMovie(MAINPLAYER)) win.thisMovie(MAINPLAYER).changeTrack(GetCookie('current_list'), GetCookie('current_track'));
	if (swfName!=POPUPPLAYER && win.xoombox_popupPlayer && win.xoombox_popupPlayer.thisMovie(POPUPPLAYER))
	{
		win.xoombox_popupPlayer.thisMovie(POPUPPLAYER).changeTrack(GetCookie('current_list'), GetCookie('current_track'));
		//win.xoombox_popupPlayer.markDiv(GetCookie('current_track'));
	}
	
	win.xoombox_popupPlayer.markDiv(GetCookie('current_track'));
}

function initializePlayer(swfName)
{
	var win = window;
	if (swfName==MINIPLAYER && win.thisMovie(MINIPLAYER)) 
	{
		
		thisMovie(MINIPLAYER).stopInitialize();
		if(GetCookie('current_volume')!=undefined)
		{	thisMovie(MINIPLAYER).setVolume(GetCookie('current_volume'));}
		
		// NEW
		thisMovie(MINIPLAYER).setCurrentPlayer(GetCookie('current_player'));
		if (GetCookie('current_player')!=MINIPLAYER)
		{	thisMovie(MINIPLAYER).deleteSound();}
	}
	if (swfName==MAINPLAYER && win.thisMovie(MAINPLAYER)) 
	{
		
		thisMovie(MAINPLAYER).changeTrack(GetCookie('current_list'), GetCookie('current_track'));
		
		if(GetCookie('current_volume')!=undefined)
		{	thisMovie(MAINPLAYER).setVolume(GetCookie('current_volume'));}
		
		// NEW
		thisMovie(MAINPLAYER).setCurrentPlayer(GetCookie('current_player'));
		if (GetCookie('current_player')!=MAINPLAYER)
		{	thisMovie(MAINPLAYER).deleteSound();}
		
	}
	if (swfName==POPUPPLAYER && win.thisMovie(POPUPPLAYER))
	{
		//alert("in");
		thisMovie(POPUPPLAYER).changeTrack(GetCookie('current_list'), GetCookie('current_track'));
		
		if(GetCookie('current_volume')!=undefined)
		{	thisMovie(POPUPPLAYER).setVolume(GetCookie('current_volume'));}
		
		
		// NEW
		thisMovie(POPUPPLAYER).setCurrentPlayer(GetCookie('current_player'));
		if (GetCookie('current_player')!=POPUPPLAYER)
		{	thisMovie(POPUPPLAYER).deleteSound();}
		
		//alert ('init');
		
		setTimeout("window.markDiv(GetCookie('current_track'))", 300);
	}
}

function updateCurrentList(current_list)
{
	SetCookie('current_list', current_list);
	
	var win = window;

	win.initializePlayer(MAINPLAYER);
	if (xoombox_popupPlayer!=null)
		xoombox_popupPlayer.initializePlayer(POPUPPLAYER);
}

function managePlaylist(user_id, swfName)
{
	//alert("wywolanie zewnetrznej f-cji managePlaylist user_id: "+user_id+" swfName: "+swfName);
	// wywolania asp na bazie + zmiana listy
	
	var win = window;
	if (swfName!=MAINPLAYER) win = window.opener;
	
	win.openPopup('/flash/PopupPlayer.aspx');
	
	// dalsze wywolania na oknie popup'a setTimeout?
	
	//updateCurrentList('data_remove.xml');
}

//---------------------------
function setCurrentPlayer(swfName)
{
	
	//if (swfName==MINIPLAYER)
	//{	return;}
	
	SetCookie('current_player', String(swfName));

	var win = window;
	if (swfName==POPUPPLAYER) win = window.opener;
	
	
	if (swfName!=MINIPLAYER && win.thisMovie(MINIPLAYER)) 
	{	
		win.thisMovie(MINIPLAYER).deleteSound();
	}
	
	if (swfName!=MAINPLAYER && win.thisMovie(MAINPLAYER)) 
	{	
		win.thisMovie(MAINPLAYER).deleteSound();
	}
	
	
	if (swfName!=POPUPPLAYER && win.xoombox_popupPlayer && win.xoombox_popupPlayer.thisMovie(POPUPPLAYER))
	{
		win.xoombox_popupPlayer.thisMovie(POPUPPLAYER).deleteSound();
	}
	
	
	
	if (win.thisMovie(MINIPLAYER))
		win.thisMovie(MINIPLAYER).setCurrentPlayer(swfName);
	
	if (win.thisMovie(MAINPLAYER))
		win.thisMovie(MAINPLAYER).setCurrentPlayer(swfName);
	
	
	if (win.xoombox_popupPlayer && win.xoombox_popupPlayer.thisMovie(POPUPPLAYER))
		win.xoombox_popupPlayer.thisMovie(POPUPPLAYER).setCurrentPlayer(swfName);
	
	//alert(swfName);
}


function miniplayer_appendToLoad(url, swfName)
{
	var win = window;
	var current_player = GetCookie('current_player');
	var current_url = GetCookie('miniplayer_current_url');
	var sound_position = GetCookie('miniplayer_sound_position');
	if (String(url)!=String(current_url))
	{	
		sound_position='0';
		SetCookie('miniplayer_current_url', url);
		SetCookie('miniplayer_sound_position', sound_position);
	}
	if (win.thisMovie(MINIPLAYER))
		win.thisMovie(MINIPLAYER).loadMySound(url, sound_position);
}

function miniplayer_updateProgressBar(position, duration, prog, swfName)
{
	SetCookie('miniplayer_sound_position', position);
	SetCookie('miniplayer_sound_duration', duration);
	SetCookie('miniplayer_sound_progress', prog);
}

var MAINPLAYER_reload = true;
function appendToLoad(url, swfName)
{
	var win = window;
	var current_player = GetCookie('current_player');
	var current_url = GetCookie('current_url');
	var sound_position = GetCookie('sound_position');
	
	if (current_player!=POPUPPLAYER && MAINPLAYER_reload==true)
	{	
		sound_position='0';
		SetCookie('sound_position', sound_position);
	}
	
	MAINPLAYER_reload=false;
	
	if (String(url)!=String(current_url))
	{	
		sound_position='0';
		SetCookie('current_url', url);
		SetCookie('sound_position', sound_position);
	}
	if (win.thisMovie(current_player) && current_player!=MINIPLAYER)
		win.thisMovie(current_player).loadMySound(url, sound_position);
}

function appendToLoadVideo(url, swfName)
{
	var win = window;
	var current_player = GetCookie('current_player');
	var current_url = GetCookie('current_url');
	var sound_position = GetCookie('sound_position');
	//var sound_position = '0';
	if (String(url)!=String(current_url))
	{	
		sound_position='0';
		SetCookie('current_url', url);
		SetCookie('sound_position', sound_position);
	}
	if (win.thisMovie(current_player) && current_player!=MINIPLAYER)
		win.thisMovie(current_player).loadMyVideo(url, sound_position);
}

function setFlashOnTop(bool)
{
	var flash = document.getElementById("flash");
	var flashZIndex = flash.style.zIndex;
	
	var content = document.getElementById("content");
	var contentZIndex = content.style.zIndex;
	
	var topIndex = contentZIndex;
	var bottomIndex = flashZIndex;
	
	if (flashZIndex>topIndex)
	{
		topIndex = flashZIndex;
		bottomIndex = contentZIndex;
	}
		
	if (bool==true)
	{
		flash.style.zIndex = topIndex;
		content.style.zIndex = bottomIndex;
	}
	else
	{
		flash.style.zIndex = bottomIndex;
		content.style.zIndex = topIndex;
	}
}

function getURL(url, target, swfName)
{	
	if (target==undefined)
		target="_self";
	var win = window;
	if (swfName!=MAINPLAYER) win = window.opener;
	
	//2010-06-04 DEN Added
	url = url.replace('~', '');
	
	if (!win)
	{	xoombox_page = window.open(url, 'xoombox_page', "status=1, toolbar=1, location=1, menubar=1, directories=1, resizable=1, scrollbar=1" );}
	else
	{	xoombox_page = win.open(url, target);}
	xoombox_page.focus();
}
	
