var played = false;
var currentItem = -1; 
var previousItem = -1;
var playlist_id = 0;
var playListArray = new Array();
var ids = new Array();
var idsr = new Array();
var playlist;
var opts =  { path: '/', expires: 0.05 };
var COOKIE_NAME = 'rtv_ad';

function playerReady(thePlayer) {
    player = window.document[thePlayer.id];
	addListeners();
}

function addListeners() {
	if (player) { 
		player.addControllerListener("ITEM", "itemListener");
		player.addModelListener("STATE", "stateListener");
	} else {
		setTimeout("addListeners()",100);
	}
}

function itemListener(obj) { 
	if (obj.index != currentItem) {
 		previousItem = currentItem;
		currentItem = obj.index;
	}
}

function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	currentState = obj.newstate; 
	previousState = obj.oldstate; 
	
	
	if ((currentState == "COMPLETED") && (previousState == "PLAYING")) 
	{
	   
	   lst = player.getPlaylist();
	   (lst.length == 1) ? items = 0 : items = 1;
	   
	   if(playlist_id)
	   {
	    if(currentItem == items)
	   {
	     next_playlist = parseInt(playlist_id)+1;
	     if(next_playlist >= playListArray.length )
	      {
	        next_playlist = 0;
		  }
		playMovie('movie'+next_playlist,playListArray[next_playlist],ids[next_playlist],idsr[next_playlist]);
	   }
	  }
    }
	
}

function createPlayer() {
	var flashvars = {
		file:base_url+"img/tlo_player.jpg",
		image:base_url+"img/tlo_player.jpg",
		autostart:"true",
		skin:skin_url,
		playlist:"bottom",
		playlistsize:"0",
		shuffle:"false",
		repeat:"always",
		displayclick:"none",
		item:"0"
		}

	var params = {
		allowfullscreen:"true", 
		allowscriptaccess:"always",
		wmode:"transparent"
	}

	var attributes = {
		id:"player1",  
		name:"player1"
	}

	swfobject.embedSWF(player_url, "playerplace", "490", "400", "9", false, flashvars, params, attributes);
}

 function allMovies()
  {
    var category = $('#category').val();
    var place    = $('#place').val();
	
	var dataString = 'category='+ category +'&place='+ place;
	$("#allmovies").hide();
	$.ajax({   
		  type: "POST",   
		  url: all_url,   
		  data: dataString,   
		  success: function(html) {   
			$("#allmovies").html(html);
			$("#allmovies").fadeIn(300);
		  }   
		});
	}

 function pageSearch()
  {
   $("#search_results").hide();
   var phrase = $('#phr').val();
   var section = $('#sec').val();
   var dataString = 'phrase='+ phrase +'&section='+ section;
   $.ajax({   
		  type: "POST",   
		  url: search_url,   
		  data: dataString,   
		  success: function(html) {   
			$("#s_result").html(html);
			$("#search_results").slideDown(300);
		     
		  }   
		});
   return false;
  }
 
 function searchClose()
  {
	$("#search_results").slideUp(300);  
	  }
 
 function queueAdd(id)
  {
    $("#movie_bar_r_1").hide();
	$("#movie_bar_r_2").show();
	$("#nowosci_but").attr({src : base_url+'/img/tv3_ciety_17_sz.jpg'});
	$("#przechowalnia_but").attr({src : base_url+'/img/tv3_ciety_18.jpg'});
	$("#search_results").slideUp(700);
	$("#movies_list").hide();  
	$.ajax({
	  type:"GET",
	  url: queue_url+'/'+id+'/',
	  cache: false,
	  success: function(html){
	  $("#movies_list").html(html);
	  $("#movies_list").show(500);  
		
     },
	  error : function(){
	    $("#movies_list").html('Wystąpił błąd');
	  }
	});
  }
 
 function submitForm(id)
  {
    $("#comment_form").hide();
	var inp = $('#comm_inp').val();
	var txt = $('#comm_txt').val();
	var dataString = 'id_filmu='+ id +'&autor='+ inp +'&koment='+txt;
	$.ajax({   
		  type: "POST",   
		  url: a_com_url,   
		  data: dataString,   
		  success: function(html) {   
			$("#comment_form").html(html);
		    $("#comment_form").fadeIn(300); 
		  }   
		});
	return false;	   
  }
 
 function commentForm(id)
  {
    $('#comms').hide();
	$.ajax({
	  type:"GET",
	  url: comf_url+'/'+id+'/',
	  cache: false,
	  success: function(html){
	    $("#comment_form").html(html);
		$("#comment_form").fadeIn(300,function(){
        
			p = player.getPlaylist();
			if(p.length > 1)
			{
			  $("#comm_titlex").html('<span>'+p[1]['title']+'</span>');
			 }
			 else
			{ 
			  $("#comm_titlex").html('<span>'+p[0]['title']+'</span>');
			  }
	   });
		
     },
	  error : function(){
	   $("#comment_form").fadeIn(300);
	   $("#comment_form").html('<p>Wystąpił błąd podczas ładowania formularza</p>'); 
	  }
	});
	
	
  }
 
 function showComments(id)
  {
    $("#comms").fadeOut(100);
	$('#comment_form').fadeOut(100);
	$.ajax({
	  type:"GET",
	  url: com_url+'/'+id+'/',
	  cache: false,
	  success: function(html){
	    $("#comms").html(html);
		$("#comms").fadeIn(300);
		$('#comments_table').jScrollPane({showArrows:true, scrollbarWidth: 12});  	
     },
	  error : function(){
	   $("#comms").fadeIn(300);
	   $("#comms").html('<p>Wystąpił błąd podczas pobierania komentarzy</p>'); 
	  }
	});
  }
 
 function playMovie(m,p,id,ad_id)
  {  
      
	  if(m){playlist_id = m.substr(5,1);}
	  else {playlist_id = false;}
	  play(m,p,id,ad_id);
   }
 
 function play(movie,p,id,ad_id)
  {
	$('#movie_link').html('<table style="width:100%"><tr><td><strong>Link do filmu:</strong></td><td><input type="text" name="link" id="link" readonly="yes"/></td></tr></table>');
	$('#movie_link #link').val('http://www.tv.naszraciborz.pl/site/movie/'+ id +'.html');
	$("#search_results").slideUp(700);
	$('.movie_o_a').hide();
	$('.movie_o').show();
	if(movie)
	{
	 $('#'+movie).hide();
	 $('#'+movie+'_h').show();
	} 
	
	$.ajax({
	  type:"GET",
	  url: up_url+'/'+id+'/'+ad_id+'/',
	  cache: false,
	  success: function(html){
		return true;	  	
     },
	  error : function(){
	   return false;
	  }
	});
	
	playlist = p;
	
	setTimeout('goPlaying()',1000);
	
	//showComments(id);
    }
 
  function goPlaying()
   {
	
	if($.cookie(COOKIE_NAME) != null){
		$.cookie(COOKIE_NAME,parseInt($.cookie(COOKIE_NAME))+1,opts);	
	 }else{
		$.cookie(COOKIE_NAME, 1 , opts);	
 	}	
	
	 if(playlist.length > 1 && $.cookie(COOKIE_NAME) > 1){
	    // playlist.splice(0,1);
     }
	
	player.sendEvent('LOAD',playlist); 	
		
	if(playlist.length > 1)
	{
	  $("#movie_title_bar").html(playlist[1]['title']);
	  if(currentItem == 1 ) player.sendEvent('PREV');  
	 }
	 else
	{ 
	  $("#movie_title_bar").html(playlist[0]['title']);
	  }
	
	}
 
 function showDesc(i)
  {
     $('.movie_desc').hide();
	 $('.b_r_play').show();
	 $('#b_desc_'+i).hide();
	 $('#m_desc_'+i).show(500);
	 $('#m_desc_'+i).css('color','#808080');
  }
  
  function closeDesc(i)
  {
     $('#m_desc_'+i).css('color','#000');
	 $('#m_desc_'+i).hide();
	 $('#b_desc_'+i).fadeIn(500);
  }
  
  function showMovies(i,el,str)
  {
   if(el)
	 {
	   id1 = $('.act1').attr('id');
	   id2 = $('.act2').attr('id');
	   if(id1 && id2)
	   {
	    $('#'+id1).attr({src : $('#'+id1).attr('src').replace(/over/,"out") });
	    $('#'+id1).removeClass('act1');
		$('#'+id2).attr({src : $('#'+id2).attr('src').replace(/over/,"out") });
	    $('#'+id2).removeClass('act2');
	   }
	   $('#t_'+el).attr({src : $('#t_'+el).attr('src').replace(/out/,"over") });
	   $('#b_'+el).attr({src : $('#b_'+el).attr('src').replace(/out/,"over") });
	   $('#t_'+el).addClass('act1');
	   $('#b_'+el).addClass('act2');
	 }
	
	$("#movie_bar_r_1").show();
	$("#movie_bar_r_2").hide();
	$("#nowosci_but").attr({src : base_url+'/img/tv3_ciety_17.jpg'});
	$("#przechowalnia_but").attr({src : base_url+'/img/tv3_ciety_18_sz.jpg'});
	
	$("#cat_title_bar").html(str);
	
	$("#movies_list").hide();
	$.ajax({
	  type:"GET",
	  url: movie_url+'/'+i+'/',
	  cache: false,
	  success: function(html){
		$("#movies_list").html(html);
		$("#movies_list").show(500);
	  	
    },
	  error : function(){
	   $("#movies_list").html('Wystąpił błąd');
	  }
	});
  }

$(document).ready(function()
  {
   
   createPlayer();
   showMovies(0,false);
   })

function charCount(maximum,area_id)
 {
   chars = $('#'+area_id).val().length;
   if(chars > maximum)
    {
	  str = $('#'+area_id).val();
	  $('#'+area_id).val('');
	  str = str.substring(0,maximum);
	  $('#'+area_id).val(str);
 	}
    left  = maximum - chars;
    if(left <0 ) left = 0;
	$('#zostalo').html('');
    $('#zostalo').html('pozostało '+left+' znaków');	
  }
  
  function clear(id)
   {
	   $('#'+id).val('');
	   }
