var currentTestimonial;
var currentPlaudit;
var clicked;
var remove;
var signinorg;
var removeid;

$(function() {

	$('.hidden').hide();
	$('.more-news').show();
	$('.more-news').click(showHiddenNews);
	$('.less-news').click(hideHiddenNews);
	
	$('.more').show();
	$('.more').click(showHidden);
	$('.less').click(hideHidden);
	
	if ($('.testimonial').length > 0)
		swapTestimonial();

	if ($('.plaudit').length > 0)
		swapPlaudit();

	$('.panel-member').click(function() { $(this).children('.panel-hidden').slideToggle(); });
		
	$('.like_me_news a').click(likeMeNews);
	$('.like_me_diary a').click(likeMeDiary);
	
	$('#news').addClass('selected');
	$('#news').click(function() { hideTabs(); $('#news').addClass('selected'); $('.like-news-tab').show(); });
	$('#diary').click(function() { hideTabs(); $('#diary').addClass('selected'); $('.like-diary-tab').show(); });
	$('#messages').click(function() { hideTabs(); $('#messages').addClass('selected'); $('.messages-tab').show(); });
	$('#friends').click(function() { hideTabs(); $('#friends').addClass('selected'); $('.friends-tab').show(); });
	$('#upload').click(function() { hideTabs(); $('#upload').addClass('selected'); $('.upload-tab').show(); });
	
	$('.like-diary-tab').hide();
	$('.messages-tab').hide();
	$('.friends-tab').hide();
	$('.upload-tab').hide();
	$('.new-message').hide();
	
	$('#new-message-button').click(function() { hideTabs(); $('.new-message').show(); });
	$('#cancel').click(function() { hideTabs(); $('#messages').addClass('selected'); $('.messages-tab').show();})
	$('#send').click(sendMessage);
	
	$('.show-hide').click(showHideMessage);
	
	$('.remove-this-element').click(removeThisElement);

	$('#stop-delete').click(function() {$('.are-you-sure').hide();});
	$('#delete-item').click(completeRemove);

	$('.standard-more').mouseover(function() { $(this).find('img').attr('src', 'images/white_more_button.png'); });
	$('.standard-more').mouseout(function() { $(this).find('img').attr('src', 'images/red_more_button.png'); });
	
	$('.more').mouseover(function() { $(this).find('img').attr('src', 'images/white_more_expand_button.png'); });
	$('.more').mouseout(function() { $(this).find('img').attr('src', 'images/red_more_expand_button.png'); });
	
	$('.less').mouseover(function() { $(this).find('img').attr('src', 'images/white_less_button.png'); });
	$('.less').mouseout(function() { $(this).find('img').attr('src', 'images/red_less_button.png'); });
	
	$(".sortable").sortable({ connectWith: '.sortable' }).disableSelection();
	
	$('#sign-in').click(showsignin);
	$('.sign-in-submit').click(signin);
	
	$('#upload-video').click(uploadvideo);
	
	$('.showcase-sub').attr('href','javascript:void(0);');
	$('.showcase-sub').click(clickedVideoThumb);
	
	$('#image_list a').lightBox({fixedNavigation:true});
//$('.profile-image').lightBox({fixedNavigation:true});	

$('a[class^="gallerylist-"]').lightBox({fixedNavigation:true});

	$('.more-news a').attr('href','javascript:void(0);');
	
	signinorg = $('.sign-in-box').html();
	$('.forgotten a').click(requestnewpassword);
	
	$('.back_to_search').html('<p><a href="javascript:void(0);">&lt;&lt; Click here to return to search</a></p>');
	$('.back_to_search').click(function() { history.go(-1); });
	
	$('#follow-form').submit(registerEmail);
	
	$('.delete-video').click(deleteVideo);
$('.remove-image').click(deleteImage);

});

function deleteImage()
{
	removeid = $(this).attr('rel');
	
	$.ajax({
	   type: "POST",
	   url: "index.php?id=549",
	   data: "imageid="+removeid,
	   success: function(html){
		   $('.image-'+removeid).remove();
		   	//$(this).parent().parent().remove();
	   },
	   error:function(XMLHttpRequest, textStatus, errorThrown) {
			alert('Sorry. There was a problem removing your image, please try again later');   
	   }
	 });			
}

function deleteVideo()
{
	removeid = $(this).attr('rel');
	
	$.ajax({
	   type: "POST",
	   url: "index.php?id=500",
	   data: "videoid="+removeid,
	   success: function(html){
		   $('.video-'+removeid).remove();
		   	//$(this).parent().parent().remove();
	   },
	   error:function(XMLHttpRequest, textStatus, errorThrown) {
			alert('Sorry. There was a problem removing your video, please try again later');   
	   }
	 });			
}

function registerEmail()
{
	$.ajax({
	   type: "POST",
	   url: "index.php?id=485",
	   data: "name="+$('#ebull_name').val()+'&email='+$('#ebull_email').val()+'&comp='+$('#ebull_comp').val(),
	   success: function(html){
		   	$('#follow-form').html('<p>Thank you. Your registration was received successfully.</p>');
	   },
	   error:function(XMLHttpRequest, textStatus, errorThrown) {
			alert('Sorry. There was a problem registering your e-mail address.');   
	   }
	 });
	return false;
}

function uploadvideo()
{
	$('#upload-video').unbind();//(uploadvideo);
	$.ajax({
	   type: "POST",
	   url: "index.php?id=165",
	   data: "video-code="+$('#video-code').val()+'&video-type='+$('#video-type').val(),
	   success: function(html){
		   	$('#video_list').append('<li>The video code <strong>'+$('#video-code').val()+'</strong> was successfully uploaded. You will need to refresh this screen in order to view it.</li>');
			if ($('#video_list li').length >= 2)
				$('#upload-videos').html('');
			else
				$('#upload-video').click(uploadvideo);
	   },
	   error:function(XMLHttpRequest, textStatus, errorThrown) {
			alert('Sorry. There was a problem uploading your video code.\n'+textStatus);   
	   }
	 });		
}

function showsignin()
{
	$('.sign-in-box').show();
}

function signin()
{
	$.ajax({
	   type: "POST",
	   url: "index.php?id=133",
	   data: "username="+$('#sign-in-username').val()+'&password='+$('#sign-in-password').val(),
	   success: function(html){
		 if (html.indexOf('success')!==-1)
		 {
			 $('.sign-in-box').hide();
			 $('#sign-in').html('Members');
			 $('#sign-in').attr('href','index.php?id=61');
			 $('#sign-in').unbind();
window.location = 'http://www.mediaacademywales.org/index.php?id=61';
//document.location.href();
		 }
		 else
		 {
			 $('.forgotten').html('<a href="javascript:void(0);">Your username or password was incorrect. If you\'ve forgotten your password, click here.</a>');
	 	 	 $('.forgotten a').click(requestnewpassword);
		 }
	   }
	 });	
}

function requestnewpassword()
{
$('.sign-in-box').html('<p>Please enter either your username or your e-mail addresses in the boxes below.</p><label for="forgotten-username">Username</label><input type="text" id="forgotten-username"><label for="forgotten-email">or Email</label><input type="text" id="forgotten-email"><a class="forgotten-password-submit" href="javascript:void(0);"><img src="images/continue.png"></a>');
$('.forgotten-password-submit').click(getnewpassword);
}

function getnewpassword()
{
	$.ajax({
	   type: "POST",
	   url: "index.php?id=230",
	   data: "forgotten-username="+escape($('#forgotten-username').val())+'&forgotten-email='+escape($('#forgotten-email').val()),
	   success: function(){
		 $('.sign-in-box').html(signinorg);
		 $('.forgotten').html('<p>Your new password has been sent to your email address</p>');
		$('.sign-in-submit').click(signin);
	   }
	 });	
}

function sendMessage()
{
	$.ajax({
	   type: "POST",
	   url: "index.php?id=136",
	   data: "target_id="+$('#to option:selected').val()+'&subject='+escape($('#subject').val())+'&message='+escape($('#message').val()),
	   success: function(){
		 hideTabs(); 
		 $('#messages').addClass('selected'); 
		 $('.messages-tab').show();
	   }
	 });			
}

function removeThisElement(e)
{
	clicked = $(this).parent();
	remove = $(this);
	
	$('.are-you-sure').css('top', e.pageY-10);
	$('.are-you-sure').css('left', 800);
	$('.are-you-sure').fadeIn();
}

function completeRemove()
{
	
	$('.are-you-sure').fadeOut();
	
	if (remove.hasClass('news-element'))
	{
		
		$.ajax({
		   type: "POST",
		   url: "index.php?id=135",
		   data: "target_id="+remove.attr('rel')+'&type=news',
		   success: function(){
			 clicked.remove();
		   }
		 });		
	}
	else if (remove.hasClass('diary-element'))
	{
	$.ajax({
	   type: "POST",
	   url: "index.php?id=135",
	   data: "target_id="+remove.attr('rel')+'&type=diary',
	   success: function(){
		 clicked.remove();
	   }
	 });		
	}
	else if (remove.hasClass('message'))
	{
		$.ajax({
		   type: "POST",
		   url: "index.php?id=137",
		   data: "target_id="+remove.attr('rel'),
		   success: function(){
			 clicked.remove();
		   }
		 });		
	}
	else if (remove.hasClass('friend'))
	{
		$.ajax({
		   type: "POST",
		   url: "index.php?id=138",
		   data: "target_id="+remove.attr('rel'),
		   success: function(){
			 clicked.remove();
		   }
		 });		
	}
}

function showHideMessage()
{
	$(this).siblings('.message').slideToggle();
}

function hideTabs()
{
	$('#news').removeClass('selected');
	$('#diary').removeClass('selected');
	$('#messages').removeClass('selected');
	$('#friends').removeClass('selected');
	$('#upload').removeClass('selected');
	
	$('.like-news-tab').hide();
	$('.like-diary-tab').hide();
	$('.messages-tab').hide();
	$('.friends-tab').hide();
	$('.upload-tab').hide();
	$('.new-message').hide();
}

function likeMeNews()
{
	clicked = $(this).parent();
	$.ajax({
	   type: "POST",
	   url: "index.php?id=134",
	   data: "target_id="+$(this).attr('rel')+'&type=news',
	   success: function(){
		 clicked.remove();
	   }
	 });	
}
function likeMeDiary()
{
	clicked = $(this).parent();
	$.ajax({
	   type: "POST",
	   url: "index.php?id=134",
	   data: "target_id="+$(this).attr('rel')+'&type=diary',
	   success: function(){
			clicked.remove();
	   }
	 });	
}

function clickedVideoThumb()
{
	$('.video-holder').html('<video autoplay="autoplay" controls="controls" width="469" height="297"><source src="'+$('#film_ogg_'+$(this).attr('rel')).html()+'" type="video/ogg"><source src="'+$('#film_mp4_'+$(this).attr('rel')).html()+'" type="video/mp4"><object width="469" height="297" type="application/x-shockwave-flash"><param name="movie" value="vidplayer.swf"><param name="flashvars" value="vidfile='+$('#film_flv_'+$(this).attr('rel')).html()+'"><embed src="vidplayer.swf" width="469" height="297" flashVars="vidfile='+$('#film_flv_'+$(this).attr('rel')).html()+'"></object></video> ');
	$('.video-description-holder').html($('#film_'+$(this).attr('rel')).html());
	$('.video-box').show();
	$('.video-close').click(closeVideo);
}

function clickedActive(obj)
{
	$('.video-holder').html('<video autoplay="autoplay" controls="controls" width="469" height="297"><source src="'+$('#film_ogg_'+obj.index).html()+'" type="video/ogg"><source src="'+$('#film_mp4_'+obj.index).html()+'" type="video/mp4"><object width="469" height="297" type="application/x-shockwave-flash"><param name="movie" value="vidplayer.swf"><param name="flashvars" value="vidfile='+$('#film_flv_'+obj.index).html()+'"><embed src="vidplayer.swf" width="469" height="297" flashVars="vidfile='+$('#film_flv_'+obj.index).html()+'"></object></video> ');
	$('.video-description-holder').html($('#film_'+obj.index).html());
	$('.video-box').show();
	$('.video-close').click(closeVideo);
}

function closeVideo()
{
	$('.video-holder').html('');
	$('.video-box').hide();
}

function newActive(obj)
{
	$('#project-details').html($('#film_'+obj.index).html());
}

function showHiddenNews()
{
	$(this).siblings('.hidden').slideDown();	
	$(this).hide();
	$(this).siblings('.less-news').show();
}
function hideHiddenNews()
{
	$(this).siblings('.hidden').slideUp();
	$(this).hide();
	$(this).siblings('.more-news').show();
}

function showHidden()
{
	$(this).siblings('.hidden').slideDown();	
	$(this).hide();
	$(this).siblings('.less').show();
}
function hideHidden()
{
	$(this).siblings('.hidden').slideUp();
	$(this).hide();
	$(this).siblings('.more').show();
}

function swapTestimonial()
{
	if (!currentTestimonial)
	{
		currentTestimonial = $('.testimonial:first');
		showNewTestimonial();
	}
	else
	{
		currentTestimonial.fadeOut(showNewTestimonial);
		if (currentTestimonial.next('.testimonial').length>0)
			currentTestimonial = currentTestimonial.next('.testimonial');
		else
			currentTestimonial = $('.testimonial:first');	
	}
}
function showNewTestimonial()
{
	currentTestimonial.fadeIn();
	setTimeout('swapTestimonial()',8000);
}


function swapPlaudit()
{
	if (!currentPlaudit)
	{
		currentPlaudit = $('.plaudit:first');
		showNewPlaudit();
	}
	else
	{
		currentPlaudit.fadeOut(showNewPlaudit);
		if (currentPlaudit.next('.plaudit').length>0)
			currentPlaudit = currentPlaudit.next('.plaudit');
		else
			currentPlaudit = $('.plaudit:first');	
	}
}
function showNewPlaudit()
{
	currentPlaudit.fadeIn();
	setTimeout('swapPlaudit()',8000);
}

function updateprofilepic(filename)
{
	$('.profile-picture').attr('src',filename);
	$('#profile-pic-url').val(filename);
}
function updatemediapic(filename)
{
$('#image_list').append('<li><img src="'+filename+'"></li>');
}
