var xmlDoc;
var currentState = 0;
var currentItem  = 0;
var currentPosition = 0;
var statsState = 0;
var isIE = !!(window.attachEvent && !window.opera);

	Effect.QueueThis = Class.create();
	Object.extend( Object.extend( Effect.QueueThis.prototype, Effect.Base.prototype ), 
	{
	    initialize: function(element) {
	        this.element = element;
	        this.func = arguments[1];
	        this.start(arguments[2]);
	    },
	    finish: function(position){
	        this.func(this.element);
    }
	});


	
	function mfGetData(action,id, statusId,data, containerId)
	{	
		
			//containerId = typeof(containerId) != 'undefined' ? containerId : 'filesContent_' + id.substring(id.indexOf('-')+1);
		
			if ($(id).visible() )
			{
				return true;
			}
			
			elements = $$('div.ajax');
			
			if (elements.size() > 0)
			{
				elements.each
				(
					function(i)
					{
						if ( i.id != id  && i.visible())
						{
							if (i.id.substr(0,5) != 'video' && i.id.substr(0,5) != 'sound'){
								Effect.BlindUp(i,{queue: 'front'});}
							else
							{
								var swf = i.id.substr(0,5) +'mp_'+ i.id.substring(i.id.indexOf('_')+1);  
                                sendEvent(swf,'stop');
								$(i.id.substr(0,5) + 'Player-' + i.id.substring(i.id.indexOf('-')+1)).hide();
								$(i.id.substr(0,1) +'Close_' + i.id.substring(i.id.indexOf('_')+1)).hide();
								Effect.BlindUp(i.id.substr(0,5) +'s-'+ i.id.substring(i.id.indexOf('-')+1),{queue: 'front'});
							}
						}
						
					}
				);
		
					
				if ( elements.indexOf($(id) )> -1 )
					Effect.BlindDown(id,{queue: 'end'});
				else
				    handleMfRequest(action,id,statusId, data);
			}
			else
			{
				handleMfRequest(action,id,statusId, data);
			}
		}
		
		function handleMfRequest(action, id,statusId, data)
		{
			contentId = id.substring(id.lastIndexOf('_')+1);
			new Ajax.Request(action,
			{
				method:'post',
				parameters: {
					content_id: contentId,
					data: data
				},   
				onLoading: function(){
					$(statusId).show();
				},
				onSuccess: function(obj){
					$(statusId).hide();
					$(id).innerHTML = obj.responseText;  
					Effect.BlindDown(id,{queue: 'end'});
					if (!$(id).hasClassName())
						$(id).addClassName("ajax"); 
					 
				},     
				onFailure: function(){
					 alert('Ajax error');
					}   
			});
		}
		
		function mfShowHide(id,playerPath, xmlPath, type)
		{
			
			//containerId = typeof(containerId) != 'undefined' ? containerId : 'filesContent_' + id;
			
			if ($(type+'s-'+id).visible() )
			{
				return true;
			}
			
			elements = $$('div.ajax');
			
			if (elements.size() > 0)
			{
				elements.each
				(
					function(i)
					{
					
						if ( i.id != id  && i.visible())
						{
								
							if ($(type+'s-'+id))
							{
								if (!$(type+'s-'+id).visible())
								{
									
									switch (type)
									{
										case 'video':
											$('soundPlayer-'+id).hide();
										break;
										
										case 'sound':
											$('videoPlayer-'+id).hide();
										break;
									}
									
									if (i.id.substr(0,5) != 'video' && i.id.substr(0,5) != 'sound'){
									{
										Effect.BlindUp(i,{queue: 'front'});}
									}	
									else
									{
										var swf = i.id.substr(0,5) +'mp_'+ i.id.substring(i.id.indexOf('_')+1);  
										sendEvent(swf,'stop');
										$(i.id.substr(0,5) + 'Player-' + i.id.substring(i.id.indexOf('-')+1)).hide();
										$(i.id.substr(0,1) +'Close_' + i.id.substring(i.id.indexOf('_')+1)).hide();
										Effect.BlindUp(i.id.substr(0,5) +'s-'+ i.id.substring(i.id.indexOf('-')+1),{queue: 'front'});
									}
									
								}
							}
							else
							{
								if (i.id.substr(0,5) != 'video' && i.id.substr(0,5) != 'sound'){
								{
									Effect.BlindUp(i,{queue: 'front'});} 
								}	
								else
								{
									var swf = i.id.substr(0,5) +'mp_'+ i.id.substring(i.id.indexOf('_')+1);  
									sendEvent(swf,'stop');
									$(i.id.substr(0,5) + 'Player-' + i.id.substring(i.id.indexOf('-')+1)).hide();
									$(i.id.substr(0,1) +'Close_' + i.id.substring(i.id.indexOf('_')+1)).hide();
									Effect.BlindUp(i.id.substr(0,5) +'s-'+ i.id.substring(i.id.indexOf('-')+1),{queue: 'front'});
								}
								Effect.BlindDown(type+'s-'+id,{queue: 'end'});
								statsContentId = id.substring(id.lastIndexOf('_')+1);
								statsMfType = type;
							}
							
						}
					}
				);
				
				Effect.BlindDown(type+'s-'+id,{queue: 'end'});
				statsContentId = id.substring(id.lastIndexOf('_')+1);
				statsMfType = type;
				switch (type)
				{
					case 'video':
						new Effect.QueueThis( 
					    	$(type+'s-'+id), 
						    function () 
							{
								showVideoPlayer(id,playerPath, xmlPath);
								if (isIE) {
					                
									var jsonPath = xmlPath.replace('xmlContent_id','jsonContent_id');
					                jsonPath = jsonPath.replace('xmlRealization_id','jsonRealization_id');
					                createPlaylist(jsonPath, "videomp_" + id.substring(id.lastIndexOf('_') + 1));
					            }
							} ,
						    { queue: 'end'   }
						);
									
					break;
										
					case 'sound':
						new Effect.QueueThis( 
						    $(type+'s-'+id), 
						    function () 
							{ 
								showSoundPlayer(id,playerPath, xmlPath);
								if (isIE) {
                
					                var jsonPath = xmlPath.replace('xmlContent_id','jsonContent_id');
					                jsonPath = jsonPath.replace('xmlRealization_id','jsonRealization_id');
					                createPlaylist(jsonPath, "soundmp_" + id.substring(id.lastIndexOf('_') + 1));
					              
					            }  
							} ,
						    { queue: 'end'   }
						);
					break;
				}
					if (!$(type+'s-'+id).hasClassName())
						$(type+'s-'+id).addClassName("ajax"); 
				
			}
			else
			{
				Effect.BlindDown(type+'s-'+id,{queue: 'end'});
									
					switch (type)
					{
						case 'video':
							new Effect.QueueThis( 
						    $(type+'s-'+id), 
						    function ()
							{ 
								showVideoPlayer(id,playerPath, xmlPath);
								if (isIE) {
									
                                    var jsonPath = xmlPath.replace('xmlContent_id','jsonContent_id');
                                    jsonPath = jsonPath.replace('xmlRealization_id','jsonRealization_id');
                                    createPlaylist(jsonPath, "videomp_" + id.substring(id.lastIndexOf('_') + 1));
                                }
							} ,
						    { queue: 'end'   }
							);
							
						break;
						
						case 'sound':
							new Effect.QueueThis( 
						    $(type+'s-'+id), 
						    function ()
							{
								 showSoundPlayer(id,playerPath, xmlPath);
								 if (isIE) {
                
                                    var jsonPath = xmlPath.replace('xmlContent_id','jsonContent_id');
                                    jsonPath = jsonPath.replace('xmlRealization_id','jsonRealization_id');
                                    createPlaylist(jsonPath, "soundmp_" + id.substring(id.lastIndexOf('_') + 1));
                                  
                                }  
							} ,
						    { queue: 'end'   }
							);
							
						break;
					}
					statsContentId = id.substring(id.lastIndexOf('_')+1);
					statsMfType = type;
				if (!$(type+'s-'+id).hasClassName())
					$(type+'s-'+id).addClassName("ajax"); 
			}
		
		
		}
		
		function showVideoPlayer(id,playerPath, xmlPath, frontColor)
		{
      
			if (frontColor == undefined)
				frontColor = '0x999999';

			$("videoPlayer-"+id).show();
			
			var s1 = new SWFObject(playerPath,
			         "videomp_"+id.substring(id.lastIndexOf('_')+1),
			         "720","676","7");
			
			s1.addParam("allowfullscreen",'false');
			s1.addParam('allowscriptaccess','sameDomain');
			s1.addParam("bgcolor","#0F0E0E");
		    s1.addParam('wmode',"transparent");
			
			s1.addVariable("usefullscreen","false");
			s1.addVariable("enablejs","true");
			s1.addVariable("javascriptid","videomp_"+id.substring(id.lastIndexOf('_')+1));
			s1.addVariable("displayheight","576");
			s1.addVariable("displaywidth","720");
			
			if (!isIE){
                s1.addVariable("file",xmlPath);
                s1.addVariable("autostart",'true');
            }
			
			s1.addVariable("screencolor",'0x0F0E0E');
			s1.addVariable("backcolor",'0x000000');
			s1.addVariable("frontcolor",frontColor);
			s1.addVariable("lightcolor",'0xFFFFFF');
			s1.addVariable("autostart",'false');
			s1.addVariable("thumbsinplaylist",'false');
			s1.addVariable('width','720');
            s1.addVariable('height','676');
            s1.addVariable('searchbar','false');
			s1.addVariable('autoscroll','true');
			
			s1.write("videoPlayer-"+id);
		    
			if (isIE) {
				
				var jsonPath = xmlPath.replace('xmlContent_id','jsonContent_id');
				jsonPath = jsonPath.replace('xmlRealization_id','jsonRealization_id');
				Event.observe(window, 'load', function(){
					createPlaylist(jsonPath, "videomp_" + id.substring(id.lastIndexOf('_') + 1));
				});
			}
		      
			if ( $('vClose_'+id.substring(id.lastIndexOf('_')+1)) )
				$('vClose_'+id.substring(id.lastIndexOf('_')+1)).show();
		}
		
		function showSoundPlayer(id,playerPath, xmlPath, frontColor)
		{
			if (frontColor == undefined)
				frontColor = '0x999999';
			$("soundPlayer-"+id).show();
			var s1 = new SWFObject(playerPath,"soundmp_"+id.substring(id.lastIndexOf('_')+1),"300","50","7");
			
			s1.addParam("allowfullscreen",'false');
			s1.addParam('allowscriptaccess','sameDomain');
			s1.addParam('wmode',"transparent");
            
			s1.addVariable("usefullscreen","false");
			s1.addVariable("enablejs","true");
			s1.addVariable("javascriptid","soundmp_"+id.substring(id.lastIndexOf('_')+1));
			s1.addVariable("displayheight","0");
			
			if (!isIE){
				s1.addVariable("file",xmlPath);
				s1.addVariable("autostart",'true');
			} 
			
			s1.addVariable("screencolor",0x0F0E0E);
			s1.addVariable("backcolor",0x000000);
			s1.addVariable("frontcolor",frontColor);
			s1.addVariable("lightcolor",0xFFFFFF);
			//s1.addVariable("autostart",'false');
			s1.addVariable("thumbsinplaylist",'false');
			s1.addVariable("displaywidtht","300");
			s1.addVariable('width','300');
            s1.addVariable('height','50');
			s1.write("soundPlayer-"+id);
			
			if (isIE) {
                
                var jsonPath = xmlPath.replace('xmlContent_id','jsonContent_id');
                jsonPath = jsonPath.replace('xmlRealization_id','jsonRealization_id');
                Event.observe(window, 'load', function(){
                    createPlaylist(jsonPath, "soundmp_" + id.substring(id.lastIndexOf('_') + 1));
                });
            }
			
			if ($('sClose_'+id.substring(id.lastIndexOf('_')+1)))
				$('sClose_'+id.substring(id.lastIndexOf('_')+1)).show();
		}
		
		function handleMfStats(action,file,type)
		{
			new Ajax.Request(action , {
			method: 'post',
			parameters: 'mode=stats&file='+file+'&type='+type,
			onComplete: function()
			{
				return true;
			},
			onFailure: function()
			{
				return false;
			}
		});
		}
		function closeMfDiv(idd)
		{
			idx = $(idd).up('div').id;
		
			if (!idx)
				return false;
			$(idx).addClassName('ajaxClosed');
			$(idx).removeClassName('ajax');
			Effect.BlindUp(idx,{queue: 'front'});
		}
		
		function closeMfPlayerDiv(idd)
		{
			idx = $(idd).up('div').id;
	
			if (!idx)
				return false;
			var swf = idx.substr(0,5) +'mp_'+ idx.substring(idx.indexOf('_')+1);	
			sendEvent(swf,'stop');
			$(idx).addClassName('ajaxClosed');
			$(idx).removeClassName('ajax');
			$(idx.substr(0,5) + 'Player-' + idx.substring(idx.indexOf('-')+1)).hide();
			$(idx.substr(0,1) +'Close_' + idx.substring(idx.indexOf('_')+1)).hide();
			Effect.BlindUp(idx.substr(0,5) +'s-'+ idx.substring(idx.indexOf('-')+1),{queue: 'front'});
		}
	
	function sendEvent(swf,typ,prm)
	{ 
	  thisMovie(swf).sendEvent(typ,prm); 
	};
	
	function getUpdate(typ, pr1, pr2, swf){
	
		if (typ == 'state') 
		{
			if (pr1 == 2)
				statsState = 1;
			
			currentState = pr1;
		}	
		else if (typ == 'item') {
			currentItem = pr1;
		}else if (typ == 'time'){
			currentPosition = pr1;
			if (pr1 < 1)
				statsState = 1;
		}
		
		if (currentPosition == 1 && statsState == 1)
		{
			var itemData = getItemData(currentItem,swf);
			//statystyki
			//alert('Dodaj kilka dla filmu, muzy o id: '+itemData['file_id']);
			handleMfStats(baseUrl+'filmy.html',itemData['file_id'],swf.substring(0,5));
			statsState = 0;
		}
	
	};
	
	function getItemData(idx,swf){
		return obj = thisMovie(swf).itemData(idx);
	}
		
	function thisMovie(swf)
	{
        if(navigator.appName.indexOf("Microsoft") != -1) {
            return window[swf];
        } else {
            return document[swf];
        }
    };
	
	function getLength(swf)
	{ 
	  var len = thisMovie(swf).getLength();
	};
	
	function addItem(swf,obj,idx)
	{ 
	  thisMovie(swf).addItem(obj,idx);
	};
	
	function removeItem(swf,idx) { 
	  thisMovie(swf).removeItem(idx); 
	};
	
	function itemData(swf,idx)
	{ 
	  var obj = thisMovie(swf).itemData(idx);
	  
	};
	
	function loadFile(swf,obj)
	{ 
	  thisMovie(swf).loadFile(obj); 
	};


	function createPlaylist(url,swf)
	{
	      new Ajax.Request(url, {
		 	method: 'get',
			onComplete: function(transport)
			{
				try {
					var json = transport.responseText.toString().evalJSON();
				
				} 
				catch (err) {
					var json = transport.responseText.evalJSON();
					
				}
				
				for (var i = 0 ; i < json.playlist.length; i++ )
				{
					
					addItem(swf,{
							file: json.playlist[i].location,
							title: json.playlist[i].title,
							file_id: json.playlist[i].file_id
						},i); 
				}
				if (json.playlist.length > 0)
					sendEvent(swf,'playitem',0);
		  
			}
		});
	}
			
