$(document).ready(function()
{
	$('.comments').hide();
	$('.commentlink').each(function()
	{
		if($(this).text()!="Comment on this article")
		{
			$(this).click(function()
			{
				var commentblock=$(this).parent().next('.comments');
				if(commentblock.is(':visible'))
				{
					commentblock.slideUp('fast');
					$(this).find('span').text("View");
				}
				else
				{
					commentblock.slideDown('fast');
					$(this).find('span').text("Hide");
				}
				return false;
			});
		}
	});
	$('#streams ul:first a').click(function()
	{
		var $this=$(this);
		var stream=$this.parent().attr('data-index');
		if(!$this.parent().hasClass('active'))
		{
			$('#streams ul:visible:not(#streams ul:first)').slideUp('fast',function()
			{
				$('#loadinggif').slideDown('fast');
				$.post("fetchstream.php",{stream:stream},function(data)
				{
					$('.active').removeClass('active');
					$this.parent().addClass('active');
					$('#loadinggif').slideUp('fast',function()
					{
						$('#streams #'+$this.parent().attr('data-index')).empty().append($(data)).slideDown();
					});
				});
			});
		}
		return false;
	});
	$('#streams').delegate(".showphoto","click",function()
	{
		showmodal($(this),function(url,wid,hei)
		{
			$('#container').append('<img src="'+url+'" width="'+(wid*2)+'" height="'+(hei*2)+'"/>');
		});
	});
	$('#streams, #videopage').delegate("#streams .showvideo, .showvideo img","click",function()
	{
		showmodal($(this),function(url,wid,hei)
		{
		    swfobject.embedSWF(url+"&enablejsapi=1&playerapiid=player1","container",wid*2,hei*2,"8",null,null,null,null);
		});
	});
	$('.makecomment').click(function()
	{
		showmodal($(this),function(url,wid,hei)
		{
			$('#container').append('<form action="submitcomment.php" method="post"><h2>Enter your comment</h2><textarea rows="6" name="comment"></textarea><label>Name:</label><input type="text" name="name" /><input type="submit" value="Post comment"><input type="hidden" name="article" value="'+url+'" /></form>');
		});
		return false;
	});
	$('#streams').delegate(".flag","click",function()
	{
		var $this=$(this);
		var postid=$this.parent().attr('data-id');
		$.post("flagpost.php",{postid:postid},function()
		{
			//$('#streams ul:first li:first a').parent().removeClass('active').end().click();
			$this.parent().animate({opacity: 0},600,function()
			{
				$this.parent().slideUp(300,function()
				{
					$this.parent().remove();
				});
			});
		});
		return false;
	});
	$('#streams').delegate("#natter","click",function()
	{
		showmodal($(this),function(url,wid,hei)
		{
			$('#container').append('<form action="submitcomment.php" method="post"><h2>Enter your comment</h2><textarea rows="6" name="comment"></textarea><label>Name:</label><input type="text" name="name" /><input type="submit" value="Post comment"><input type="hidden" name="article" value="0" /></form>');
		});
		return false;
	});
	$('#contact form').submit(function(e)
	{
		$this=$(this);
		$.post("handlecontacts.php",$(this).serialize(),function()
		{
			$this.hide('slow',function()
			{
				$this.empty().append('<h3>Thank you for your message.</h3>').show('slow');
			});
		});
		e.preventDefault();
	})
});

function showmodal($this,prepareframe)
{
    var url=$this.attr('data-url');
    var wid=$this.attr('data-wid');
    var hei=$this.attr('data-hei');
    
	$('body').append("<div id='frame'><a href='#' id='closemodal' title='Click to close' style='margin-left:"+(wid*2)+"px'>Close</a><div id='container'></div></div><div id='overlay' title='Click to close'></div>");

    var screenwid=$('body').outerWidth();
    $('#frame').css({width:(wid*2),height:(hei*2),marginTop:-hei,marginLeft:-(screenwid/2)-(wid*2)-100});
    
    prepareframe(url,wid,hei);
    $('#overlay').animate({marginTop: 0},500,function()
    {
    	$('#frame').animate({marginLeft:-wid},1000,'easeOutQuint');
    });
    $('#overlay, #closemodal').click(function()
    {
    	$('#frame,#overlay').fadeOut(500,function()
    	{
    		$(this).remove();
    	});
    	return false;
    });
};

