YT_PREV = '';
BACK_HTML = '';
load = function() {
	$('.senddisc').click(function() {
		BACK_HTML = $('#change').html();
		$('#change').html('<div class="loading"></div>');
		$('#change').load('ajax-send-mail.php',function(){
			$('#back').show();
			$('#inputEmail').focus();
			autohide();
		});
		
		return false;
	});
	$('#formSendMail').submit(function(){
		var email = $('#inputEmail').val();
		$('#inputEmail').parent().replaceWith('<div class="loading"></div>');
		$('#change').load('ajax-send-mail.php',{email:email},function(){
			$('#inputEmail').select();
			$('#inputEmail').focus();
			autohide();
		});
		return false;
	});
	$('.youtubeclip').click(function() {
		YT_PREV = $('#youtubereplace').html();
		$('#youtubereplace').html('<div class="loading"></div>');
		$('#youtubereplace').show();
		$('#youtubeclose').hide();
		$('#youtubereplace').load('ajax-youtube.php',function(){
			$('.email').load('ajax-email.php');
			autohide();
			handle_close();
		});
		
		return false;
	});
	$('#back').click(function(){
		$('#change').html(BACK_HTML);
		$('#back').hide();
		load();
	});
	autohide();
	$('#showComments').click(function() {
		YT_PREV = $('#youtubereplace').html();
		$('#youtubereplace').html('<div class="loading"></div>');
		$('#youtubereplace').show();
		$('#youtubeclose').hide();
		$('#youtubereplace').load('ajax-comments.php',function(){
			handle_comments();
			handle_close();
		});
		return false;
	});
	$('.email').load('ajax-email.php');
}

function autohide() {
	try{
		clearTimeout(ID_AH);
	}catch(e){}
	ID_AH=setTimeout("$('.autohide').fadeOut('slow');",5000);
}

function handle_close() {
	$('#closeyoutube').click(function(){
		$('#youtubereplace').html(YT_PREV);
		$('#youtubereplace').hide();
		$('#youtubeclose').show();
		return false;
	});
}
function handle_comments() {
	$('#newComment').click(function(){
		$('#youtubereplace').html('<div class="loading"></div>');
		$('#youtubereplace').load('ajax-comments.php?form',function(){
			handle_comments();
			handle_close();
			autohide();
		});
		return false;
	});
	$('#sendComment').submit(function(){
		var vals = {};
		var errors = 0;
		$('#comments input,#comments textarea').each(function(){
			var val = $(this).val();
			if(val=='') {
				errors = $(this).attr('name');
				return false;
			}
			eval('vals.'+$(this).attr('name')+'=val;');
		});
		if(errors) {
			alert(TXT_ERROR_FIELD);
			$('#'+errors).focus();
			return false;
		}
		//alert(vals.name + "|" + vals.email + '|' + vals.comment);
		$('#youtubereplace').html('<div class="loading"></div>');
		$('#youtubereplace').load('ajax-comments.php',vals,function(){
			handle_comments();
			handle_close();
			autohide();
		});
		return false;
	});
	$('#comments input.focus,#comments textarea.focus').focus();
	$('#comments .count a').click(function(){
		$('#youtubereplace').html('<div class="loading"></div>');
		$('#youtubereplace').load('ajax-comments.php',{offset:$(this).attr('rel')},function(){
			handle_comments();
			handle_close();
		});
		return false;
	});
}
$(document).ready(load);
