$(document).ready(function()
{
    	 replaced_links();
});

function replaced_links()
{
	if( $('.fotka img').html() == null)
	{
	    $('.newsy_box a[target="_blank"]').each(function (event) 
	    {
		LinkAnalize.replace_link( $(this).html(), this );
	    });
	}
	else
	{
	    if($('.fotka img').attr('src') == 'images/')
	    {    
		$('.fotka img').attr('src', 'img/ajax-loader.gif');
		LinkAnalize.replace_link_object( $('.newsy_box a[target="_blank"]').html(), $('.fotka img') );
	    }
	}
	
	

};

var LinkAnalize = {
	
	 width: 480,
	 height: 385,
	 icon: {
			demotywatory: 'ikonki/dem.jpg',
			youtube: 'ikonki/youtube.jpg'
			},

	remove_link: function(elem)
	{
	        $(elem).children('a').each( function(){
	    	    var href =  $(this).attr('href');
	    	    if( href.match(/www.youtube.com/) )
			$(this).remove();	 
		    else if( href.match(/demotywatory.pl/) )
			$(this).remove();
	        });
	},
	
	remove_photo: function()
	{
	    if($('.fotka img').attr('src') == 'img/ajax-loader.gif')
		       $('.fotka img').remove();
	},

	replace_link_object: function(text, elem)
	{
		if(text == '' || text == null)
		{
		    LinkAnalize.remove_link( $('.newsy .txt') );
		    LinkAnalize.remove_photo();
		    return;
		}
		var words = text.split(' ');
		var results_text = '';
		for(var i in words)
		{
			var word = words[i];
			if(results_text != '')
			  results_text += ' ';
							
			if( word.match(/www.youtube.com/) )
			{
			    
				var results = word.match(/watch\?v=([a-zA-Z0-9_-]+)/);
				
				
				
				
				if( results[1] != null )
				{
					var link = results[1];
					var result =  "[youtube:" + link + "]";
					results_text += result;	 
				}
			}
			else if( word.match(/demotywatory.pl/) )
			{
				var results = word.split('/');
				var link = results[3];
				var result =  "[demotywatory:" + link + "]";
				results_text += result;
			}
			else
				results_text += word;
		}
		
		LinkAnalize.set_link_object( results_text, elem );
	},
	
	set_link_object: function(text, elem)
	{
		var remove = true;
		var results_text = '';

		if( text.match(/\[youtube:([a-zA-Z0-9_-]+)]/) )
		{
				var link = text.split('[youtube:').join('');
				link = link.split(']').join('');
				var result =  "<object width='" + LinkAnalize.width + "' height='" + LinkAnalize.height + "'>";
				result += "<param name='movie' value='http://www.youtube.com/e/" + link + "?hl=pl_PL&amp;fs=1'></param>";
				result += "<param name='allowFullScreen' value='true'></param>";
				result += "<param name='allowscriptaccess' value='always'></param>";
				result += "<embed src='http://www.youtube.com/e/" + link + "?enablejsapi=1&amp;version=3&amp;playerapiid=player1&amp;iv_load_policy=3' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='" + LinkAnalize.width + "' height='" + LinkAnalize.height + "'></embed>";
				result += "</object>";
				results_text += result;
				
				
				
				$(elem).parent().append(  results_text );
				$('.fotka img').attr('src', '');        
				$('.fotka img').hide();
				remove = false;
				
		}
		else if( text.match(/\[demotywatory:(\w+)]/) )
		{
				
				var link = text.split('[demotywatory:').join('');
				link = link.split(']').join('');
				
				var params = {
				  type: 'demotywatory',
				  link: link
				};

				$.ajax({
					type: "POST",
					url: "replace_link_ajax.php",
					data: params,
					success: function( msg ){
						$(elem).attr('src', msg );
					}
				});
				remove = false;
		}
		if(remove)
		    LinkAnalize.remove_photo();
		
		LinkAnalize.remove_link( $('.newsy .txt') );
		
		
	},
			
	replace_link: function(text, elem)
	{
		var words = text.split(' ');
		var results_text = '';
		for(var i in words)
		{
			var word = words[i];
			if(results_text != '')
				results_text += ' ';
					
			if( word.match(/www.youtube.com/) )
			{
				
				var results = word.match(/watch\?v=([a-zA-Z0-9]+)/);

				if( results[1] != null )
				{
				    var link = results[1];
				    var result =  "[youtube:" + link + "]";
				    results_text += '';
				    var id = $(elem).parent().parent().parent().attr('id');
				    $('#' + id + ' .aparat').css('background', 'url(img/film.gif)');
				    $('#' + id + ' .aparat').css('display', 'block');
				 }
			}
			else if( word.match(/demotywatory.pl/) )
			{
				var results = word.split('/');
				var link = results[3];
				var result =  "[demotywatory:" + link + "]";
				results_text += '';
				//results_text += result;
				var id = $(elem).parent().parent().parent().attr('id');
				$('#' + id + ' .aparat').css('display', 'block');
			}
			else
				results_text += word;
		}
		
		LinkAnalize.set_link( results_text, elem );
	},
		
	set_link: function(text, elem)
	{
		var words = text.split(' ');
		var results_text = '';
		var id = 1;
		for(var i in words)
		{
			var word = words[i];
			if(results_text != '')
				results_text += ' ';
				
			if( word.match(/\[youtube:(\w+)]/) )
			{
				results_text = '<img src="' + LinkAnalize.icon.youtube + '" />';
				$(elem).html(  results_text );
				$(elem).parent().children('a').attr('href', 'index.php?p=ads&k=1&o=' + $(elem).parent().parent().parent().attr('id').split('news_id-').join('') );
				 $(elem).parent().children('a').attr('target', '_self');
			}
			else if( word.match(/\[demotywatory:(\w+)]/) )
			{
				results_text = '<img src="' + LinkAnalize.icon.demotywatory + '" />';
				$(elem).html(  results_text );
				$(elem).parent().children('a').attr('href', 'index.php?p=ads&k=1&o=' + $(elem).parent().parent().parent().attr('id').split('news_id-').join('') );
				$(elem).parent().children('a').attr('target', '_self');

			}
			else
			    results_text += word;
		}
		
		
		$(elem).html(  results_text );
		id++;
	}
}
