var dialogues = function (){
    var loading = false;
	var hidden = false;
	var mode = 1; // 1 = nieuw naar oud, 2 = oud naar nieuw

    function init(){
        $(window).scroll(onWindowScroll);

		$('a.subject-link').click(onChangeSubject);
		$('#dialogue-ordermodus-vnno').click(onChangeOrder);
		$('#dialogue-ordermodus-vonn').click(onChangeOrder);
		$('#showMoreSubjects').live('click', onShowMoreSubjects);

		mode = $('#mode').val() || 1;

		if(mode == 1){
			$('#dialogue-ordermodus-vnno').hide();
		} else {
			$('#dialogue-ordermodus-vonn').hide();
		}

		// hide dialogue links surplus
		if($('a.subject-link').length > 5){
			$('a.subject-link').each(function(i){
				if(i > 5){
					$('a.subject-link').eq(i).parent().hide();
				}
			});
			$('<a href="" id="showMoreSubjects" style="font-size:0.75em;">Toon meer</a>').insertAfter($('#subject-links'));
		}
    }

    function onWindowScroll(){
        if(loading) return false;

        // is the scrollbar on the bottom?
        var root = document.documentElement ? document.documentElement : document.body;
        var h = root.scrollHeight;
        var p = root.scrollTop;
        var ch = root.clientHeight;
        if((h - p) == ch){
            getDialogues();
        }
    }

    function getDialogues(){
        loading = true;

        var d = $('.dialogue:last .dialogue-publishdate').val();
		var s = $('#subject').val();
        var url = '/ajax.php?go=dialogen';
    	var arg = {'date' : d, 'subject' : s, 'm' : mode}
    	$.post(url, arg, onGetDialogues, 'json');
    }

    function getSubject(id){
        loading = true;

		$('#subject').val(id);

		// set the dialogue title to the correct subject

		//$('#currentSubjectName').text($('a.subject-' + id).text());
		$('#currentSubjectName').replaceWith('<h2 class="sifr2" id="currentSubjectName">' + $('a.subject-' + id).text() + '</h2>');

		// reboot sifr on title
		sIFR.replace(font2, {
		  selector: '#currentSubjectName',
		  wmode: ($.browser.msie)?'window':'transparent'
		});

		$('#dialogues').html('Bezig met laden...');

        var url = '/ajax.php?go=dialogen';
    	var arg = {'subject' : id, 'm' : mode}
    	$.post(url, arg, onGetSubject, 'json');
    }

	function changeOrder(m){
		mode = m;
		if(m == 1){
			$('#dialogue-ordermodus-vnno').hide();
			$('#dialogue-ordermodus-vonn').show();
		} else {
			$('#dialogue-ordermodus-vnno').show();
			$('#dialogue-ordermodus-vonn').hide();
		}
		getSubject($('#subject').val())
	}

	function show(){
		$('#dialogues').slideDown('fast');
		hidden = false;
	}

	function hide(){
		$('#dialogues').slideUp('fast');
		hidden = true;
	}

	function isHidden(){
		return hidden;
	}

	function onChangeSubject(e){
		e.preventDefault();

		var s = e.target.className;
		var m = s.match(/subject-(\d+)/);
		if(m){
			getSubject(m[1]);
		}
	}

	function onChangeOrder(e){
		e.preventDefault();
		var id = $(e.target).attr('id');
		if(id == 'dialogue-ordermodus-vnno'){
			changeOrder(1);
		} else {
			changeOrder(2);
		}
	}

	function onShowMoreSubjects(e){
		e.preventDefault();
		var t = $('a.subject-link:visible').length;
		$('a.subject-link').each(function(i){
			if(i > t && i <= t+5){
				$('a.subject-link').eq(i).parent().show();
			}
		});
		if(t+5 >= $('a.subject-link').length){
			$('#showMoreSubjects').hide('normal');
		}
	}

	function onGetSubject(json){
		$('#dialogues').html('');
		onGetDialogues(json);
	}

    function onGetDialogues(json){
        if(json.data != ''){
            $('#dialogues').append(json.data);
            loading = false;
        }
    }

    return {
		'init' : init,
		'getDialogues' : getDialogues,
		'getSubject' : getSubject,
		'show' : show,
		'hide' : hide,
		'isHidden' : isHidden,
	}
}();

$(document).ready(function(){
    dialogues.init();
});