var forms={};

$(document).ready(function(){
    // If the user tried to reply, but the got the captcha wrong.
    var comment_id = parseInt($('div#reply_form :input:eq(0)').val());
    if(comment_id > 0) {
        var form = $('div#reply_form').html();
        $('#reply-' + comment_id).html(form);
        $('input.reply_submit').click(function() { return reply_submit_click(this); });
        $('#reply-' + comment_id).animate({height: 'show', opacity: 'show'}, 'fast');
        
        // Clear the original form.
        $("div#reply_form :input[name='in_reply_to']").val('');
        $("div#reply_form :input[name='name']").val('');
        $("div#reply_form :input[name='job_title']").val('');
        $("div#reply_form :input[name='organisation']").val('');
        $("div#reply_form :input[name='email']").val('');
        $("div#reply_form :input[name='comment']").html('');
        $("div#reply_form :checkbox[name='future_promotions']").attr('checked', '');
        $("div#reply_form :checkbox[name='newsletter']").attr('checked', '');
    }
    
    // If the user tried to report, but the got the captcha wrong.
    var comment_id = parseInt($('div#report_comment :input:eq(0)').val());
    if(comment_id > 0) {
        var form = $('div#report_comment').html();
        $('#report_comment-' + comment_id).html(form);
        $('input.report_submit').click(function() { return report_submit_click(this); });
        $('#report_comment-' + comment_id).animate({height: 'show', opacity: 'show'}, 'fast');
        
        if($("div#report_comment :input[name='type']").val() == 'reply') {
            $('#report_comment-' + comment_id).parent().animate({height: 'show', opacity: 'show'}, 'fast');
        }
        
        // Clear the original form.
        $("div#report_comment :input[name='name']").val('');
        $("div#report_comment :input[name='email']").val('');
        $("div#report_comment :input[name='type']").val('');
        $("div#report_comment :input[name='comment_id']").val('');
        $("div#report_comment :input[name='reason']").html('');
    }
	
	$('#more_gallery').hide();
	
	$('#more_gallery_link').toggle(function() {
		$('#more_gallery').animate({
			height: 'show',
			opacity: 'show'
		}, 'slow');
		$('#more_gallery_link').find('p').children('img').attr('src','/images/arrow-down-blue.png');
		$('#more_gallery_link').find('p').children('span').text('Close');
	},function() {
		$('#more_gallery').animate({
			height: 'hide',
			opacity: 'hide'
		}, 'slow');
		$('#more_gallery_link').find('p').children('img').attr('src','/images/arrow-right-blue.png');
		$('#more_gallery_link').find('p').children('span').text('More');
	});
	
	$('#gallery a').lightBox({fixedNavigation:true});
	
	$('#gallery a img').hover(function () {
		$(this).animate({opacity: '0.3'},'fast');
	},function() {
		$(this).animate({opacity: '1.0'},'fast');
	});
	
	$.each(forms, function(key,val) {
        if(val == undefined) {
            return false;
        }
        
		formname=val['name'];
		$('form#'+formname).find('input[@type="submit"]').click(function() {
			var valid=true;
			$.each(val['fields'], function (key,val) {
				if ($('form#'+formname).find('textarea[@name='+val+'],input[@name='+val+']').val()=='') { 
					valid=false; 
					$('form#'+formname).find('textarea[@name='+val+'],input[@name='+val+']').addClass('highlight');
				} else {
					$('form#'+formname).find('textarea[@name='+val+'],input[@name='+val+']').removeClass('highlight');
				}
			});
			if (valid==false) {
				alert('There had been a problem with your form submission, please check all highlighted fields and try again');
				return false;
			}
		});

	});
	
	$('.node_list').find('.left').each(function () {
		if ($(this).height()>$(this).next().height()) {
			$(this).next().css({height: ($(this).height())+'px'});
			$(this).next().find('.inner1').css({height: ($(this).height())+'px'});
			$(this).next().find('.inner2').css({height: ($(this).height()-1)+'px'});

			$(this).css({height: ($(this).height())+'px'});
			$(this).find('.inner1').css({height: ($(this).height())+'px'});
			$(this).find('.inner2').css({height: ($(this).height()-1)+'px'});
		} else if ($(this).height()<$(this).next().height()) {
			$(this).css({height: ($(this).next().height())+'px'});
			$(this).find('.inner1').css({height: ($(this).next().height())+'px'});
			$(this).find('.inner2').css({height: ($(this).next().height()-1)+'px'});

			$(this).next().css({height: ($(this).next().height())+'px'});
			$(this).next().find('.inner1').css({height: ($(this).next().height())+'px'});
			$(this).next().find('.inner2').css({height: ($(this).next().height()-1)+'px'});
		} else {
			$(this).css({height: ($(this).height())+'px'});
			$(this).find('.inner1').css({height: ($(this).height())+'px'});
			$(this).find('.inner2').css({height: ($(this).height()-1)+'px'});
			
			$(this).next().css({height: ($(this).next().height())+'px'});
			$(this).next().find('.inner1').css({height: ($(this).next().height())+'px'});
			$(this).next().find('.inner2').css({height: ($(this).next().height()-1)+'px'});
			
		}
	});
    
    $('select.keyword_select').change(function() {
        var select_unchanged = 'keyword_2';
        if($(this).attr('name') == 'keyword_2') {
            select_unchanged = 'keyword_1';
        }
        
        var keywords_1_length = $('select#keyword_1 option').length;
        var keywords_2_length = $('select#keyword_2 option').length;
        var selected = $(this).val();
        if(selected != '' || keywords_1_length != keywords_2_length) {
            var other_selected = $('select#' + select_unchanged).val();
            $('select#' + select_unchanged).empty();
            $('select#' + select_unchanged).append('<option value="">- Select -</option>');
            var previous = '';
            $(this).contents().each(function() {
                var value = $(this).next().val();
                if(value != undefined && value != selected && value != previous) {
                    $('select#' + select_unchanged).append('<option value="' + value + '">' + value + '</option>');
                }
                previous = value;
            });
            if(other_selected != '') {
                $('select#' + select_unchanged).append('<option value="' + other_selected + '">' + other_selected + '</option>');
            }
            $('select#' + select_unchanged + ' option[value="' + other_selected + '"]').attr('selected', 'selected');
        }
    });
    
    $('span.reply_to_comment').click(function() {
        var div_id = $(this).attr('id').substr(5);
        if($('#' + div_id).is(':hidden')) {
            var html = $('#' + div_id).html();
            if(html == '') {
                var form = $('div#reply_form').children().eq(0).clone()
                $('#' + div_id).append(form);
                $('#' + div_id + ' :input:eq(0)').val(div_id.substr(6))
                
                $('input.reply_submit').click(function() { return reply_submit_click(this); });
            }
    		$('#' + div_id).animate({height: 'show', opacity: 'show'}, 'slow');
        }
        else {
    		$('#' + div_id).animate({height: 'hide', opacity: 'hide'}, 'slow');
        }
    });
    
    $('span.report_comment').click(function() {
        var div_id = $(this).attr('id').substr(5);
        if($('#' + div_id).is(':hidden')) {
            var html = $('#' + div_id).html();
            if(html == '') {
                var form = $('div#report_comment').children().eq(0).clone()
                $('#' + div_id).append(form);
                $('#' + div_id + ' :input:eq(0)').val(div_id.substr(15))
                
                var type = 'comment';
                if($(this).hasClass('reply')) {
                    type = 'reply';
                }
                $('#' + div_id + ' :input:eq(1)').val(type);
                
                $('input.report_submit').click(function() { return report_submit_click(this)});
            }
    		$('#' + div_id).animate({height: 'show', opacity: 'show'}, 'slow');
        }
        else {
    		$('#' + div_id).animate({height: 'hide', opacity: 'hide'}, 'slow');
        }
    });
    
    $('span.link_show_replies').click(function() {
        var link_text = jQuery.trim($(this).html()).substr(5);
        var div_id = $(this).attr('id').substr(5);
        if($('#' + div_id).is(':hidden')) {
    		$('#' + div_id).animate({height: 'show', opacity: 'show'}, 'slow');
            $(this).html('Hide ' + link_text);
        }
        else {
    		$('#' + div_id).animate({height: 'hide', opacity: 'hide'}, 'slow');
            $(this).html('View ' + link_text);
        }
    });

    $('a.pdf_download').click(function() {
        isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
        if(isIE6) {
            alert("It appears that you are using Internet Explorer 6. Please note that directly opening PDFs will not work in this browser; you must save the document before opening it. We apologise for the inconvenience.");
        }
    });
});

/**
 * Handles what happens when the "Submit Reply" button is clicked.
 *
 * @param object clicked_this The "this" object for the button clicked.
 * @return boolean True if the form is complete and can be submitted, else false.
 */
function reply_submit_click(clicked_this) {
    var children = $(clicked_this).parent().children();
    var name = children.eq(3).children().eq(0);
    var body = children.eq(12).children().eq(0);
    var captcha = children.eq(23).children().eq(0);
    var fields = new Array(name, body, captcha);
    var failed = false;
    
    for(i = 0; i < fields.length; i++) {
        if(jQuery.trim(fields[i].val()) == '') {
            fields[i].addClass('highlight');
            failed = true;
        }
        else {
            fields[i].removeClass('highlight');
        }
    }
    
    if(failed == true) {
        alert('Please fill in all required fields');
        return false;
    }
}

/**
 * Handles what happens when the "Submit Reply" button is clicked.
 *
 * @param object clicked_this The "this" object for the button clicked.
 * @return boolean True if the form is complete and can be submitted, else false.
 */
function report_submit_click(clicked_this) {
    var children = $(clicked_this).parent().children();
    var email = children.eq(2).children().eq(1).children().eq(0);
    var name = children.eq(4).children().eq(0);
    var body = children.eq(6).children().eq(0);
    var captcha = children.eq(11).children().eq(0);
    var fields = new Array(name, email, body, captcha);
    var failed = false;
    
    for(i = 0; i < fields.length; i++) {
        if(jQuery.trim(fields[i].val()) == '') {
            fields[i].addClass('highlight');
            failed = true;
        }
        else {
            fields[i].removeClass('highlight');
        }
    }
    
    if(failed == true) {
        alert('Please fill in all required fields');
        return false;
    }
}

function pageload(hash) {
	// hash doesn't contain the first # character.
	if(hash) {
		$('html, body').animate({scrollTop:0}, 'fast');
		$("div[id^='content']").hide();
		$("a[id^='contentselector']").parent().parent().removeClass('selected');
		$link=$('.content_selectors.').find('a[href="#' + hash + '"]');
		$link.parent().parent().addClass('selected');
		$("div[id='contentpanel"+$link.attr('id').substring(15)+"']").show();		
		$selected=$link.parent().parent();

	}
}
