/** 
Reports.js

Reports section functionality

**/

var clickedReport, reportInfo, reportText,
chosenReportsBox, lastItemChosen;

$(document).ready(function(){
	$('.boxContent').append('<ul class="reports"/>');
						   
	chosenReportsBox = $('.page35 .right .box');
						   	
	$('.page35 .left .down').live('click',showInfo);
	$('.page35 .left .up').live('click',hideInfo);

	$('.ajaxload').ajaxError(ajaxError);

	$('.page35 .left .download a').bind('click',addReport);
	$('.page35 .right input.remove').live('click',removeReport);
	$('.page35 .right .download span').bind('click',showBasket);
	$('#reportForm').hide();
	
	var options = { 
		target:     '.details',
		url:        '/assets/snippets/email/email.php'
	}; 
	
	$("#repForm").validate({
		submitHandler: function(form) {
			$(form).ajaxSubmit(options);
		}
	});
	
	$('#TB_window .close').live('click',tb_remove);
	$('#TB_window p.download input:button').live('click',processForm);	
});


function hideInfo() {
	reportInfo = $(this);
	reportInfo.parents('.box').stop();
	reportInfo.removeClass('up');
	reportInfo.addClass('down');
	newContentHeight = contentHeight - reportInfo.parents('.box').find('.boxContent').height() - 15;
	
	if (reportInfo.siblings('h2').height() == 43)
		reportInfo.parents('.box').animate({height: '40px'});
	else
		reportInfo.parents('.box').animate({height: '20px'});	
	
	contentWrap.animate({
						height: newContentHeight+"px"
						});
	$('#footer').animate({
						top: (newContentHeight+67)+"px"
						},
						function () {
							finishAnimation = false;
							linkClicked = false;
							checkQueue();
						});
	contentHeight = newContentHeight;
}

function showInfo() {
	reportInfo = $(this);
	reportInfo.parents('div.box').stop();
	reportInfo.removeClass('down');
	reportInfo.addClass('up');
	newHeight = reportInfo.parents('.box').find('.boxContent').height() + (reportInfo.parents('.box').find('.download a').css('display') == 'none' ? 65 : 35);
	reportInfo.parents('.box').animate({height: newHeight+'px'});
	
	newContentHeight = contentHeight + newHeight - 20;
	contentWrap.animate({
						height: newContentHeight+"px"
						});
	$('#footer').animate({
						top: (newContentHeight+67)+"px"
						},
						function () {
							finishAnimation = false;
							linkClicked = false;
							checkQueue();
						});
	contentHeight = newContentHeight;
}

function addReport() {
	$('#mainContent p.bookError').hide();
	if (allowAdditions == true) {
		allowAdditions = false;
		clickedReport = $(this);
		reportText = clickedReport.parents('.box').find('h2').text();
		
		newHeight = chosenReportsBox.height() + 40;
		chosenReportsBox.animate({height: newHeight+'px'});
		lastItemChosen = $('<li><span class="report">' + reportText + '</span><input type="button" class="remove" /></li>').appendTo('div.right ul').hide().fadeIn('slow').data('button',clickedReport);
		
		clickedReport.fadeOut('normal', function () {
			allowAdditions = true;								  
		});
	}
}

function removeReport() {
	if (allowAdditions == true) {
		
		allowAdditions = false;
		clickedReport = $(this).parents('li');
		clickedReport.data('button').fadeIn('normal');
		
		clickedReport.slideUp('normal').fadeOut('normal',function() {
			clickedReport.remove();
		});
		newHeight = chosenReportsBox.height() - 40;
		chosenReportsBox.animate({height: newHeight+'px'}, function () {
			allowAdditions = true;															 
		});
	}
}

function delReport() {
	if (allowAdditions == true) {
		allowAdditions = false;
		clickedReport = $(this).parents('li:first');
		var originalReport = clickedReport.data('original');
		var addReport   = originalReport.data('button');
	
		clickedReport.slideUp('normal',function() {
			clickedReport.remove();
		});
		newHeight = chosenReportsBox.height() - 40;
		chosenReportsBox.animate({height: newHeight+'px'}, function () {
			allowAdditions = true;
		});
		addReport.show();
		originalReport.slideUp('normal',function() {
			originalReport.remove();
			if ($('#TB_ajaxContent .reports li').length == 0)
				tb_remove();
		});
	}
}

function showBasket() {
	if ($('div.right .reports li').length > 0) {
		tb_show(null, '#TB_inline?height=440&width=570&inlineId=reportForm', false);
		
		var $ul = $('div#TB_window ul.basket');
		$ul.empty();
		
		$('.hidden').remove();
	
		$('div.right li').each(function() {
			var $this = $(this);
			var $new = $this.clone();
			$new.data('original',$this);
			$new.appendTo($ul);
			
			var index = $('div.right li').index(this);
			var title = $this.find('span').html();
			$('form#repForm fieldset').append('<input type="text" class="hidden" name="reports['+index+']" value="'+title+'" />');
		});
		$ul.find('li .remove').bind('click',delReport);
	} else {
		$('#mainContent p.bookError').fadeIn('fast');
	}

}

function processForm() {
	$('#TB_window').hide();

	var $ul = $('div#TB_window ul');

	var reports = '';
	$('div#TB_window ul li:not(.heading)').each(function() {
		var $spans = $(this).find('span');
		reports += '   * ' + $($spans[0]).text() + ' in ' + $($spans[1]).text() + ' on ' + $($spans[2]).text() + '\n';
	});

	$ul.append('<input type="hidden" name="reports" />');
	$ul.find('input[name=reportss]').val(reportss);

	var $post = $('div#TB_window input').serializeArray();

	$.post("http://v2.feesch.co.uk/postit.pl", $post, function(data){
		tb_remove();
	   alert("Data Loaded: " + data);
	});
}

function ajaxError(event, request, settings) {
	alert('Error requesting page ' + settings.url);
}

