$(document).ready(function(){
	
	var sid = $('#session_id').val();
	var pid = $('#page_id').val();

	$('.naytaOhjelma').livequery('click', function () {
		$(this).parent('div').parent('div').children('div').removeClass('active');
		$(this).parent('div').addClass('active');
	});
	
	$('.naytaTaulukko').livequery('click', function () {
		$(this).parent('div').parent('div').children('div').removeClass('active');
		$(this).parent('div').addClass('active');
	});
	
	/* ---- FORUM */

	$('#addNewForumPost').livequery('click', function () {
		var comment = $(this).parent('div').children('textarea.forumPostBoxText').val();
		$.post('/intra/index.php?page='+pid+'&sid='+sid+'&action=addcomment&method=ajax&comment='+encodeURI(comment), function(data) {
			$('#forumBody').html(data);
		});
	});

	$('textarea.forumTopicBoxText').livequery('click', function () {
		$(this).addClass('active').val('').parent('div').children('.inputsubmit').css('display','inline','float','left');
	}).autogrow();
	
	$('#addNewForumTopic').livequery('click', function () {
		var comment = $(this).parent('div').children('textarea.forumTopicBoxText').val();
		$.post('/intra/index.php?page='+pid+'&sid='+sid+'&action=addtopic&method=ajax&comment='+encodeURIComponent(comment), function(data) {
			$('#forumBody').html(data);
		});
	});

	$('.showCommentBox').livequery('click', function () {
		$(this).parent('div').parent('div').children('.forumPostBox').css('display','block');
	});
	
	$('textarea.forumPostBoxText').livequery('click', function () {
		$(this).addClass('active').val('').parent('div').children('.inputsubmit').css('display','inline','float','left');
	}).autogrow();

	$('input.addNewForumPost').livequery('click', function () {
		var comment = $(this).parent('div').children('textarea.forumPostBoxText').val();
		var topicID = $(this).parent('div').parent('div').children('.topicRowId').val();
		$(this).parent('div').children('textarea.forumPostBoxText').removeClass('active').val('kirjoita kommentti').parent('div').children('.inputsubmit').css('display','none');
		$.post('/intra/index.php?page='+pid+'&sid='+sid+'&action=addcomment&method=ajax&comment='+encodeURIComponent(comment)+'&id='+topicID+"&show=all", function(data) {
			$('.postRow-'+topicID).html(data);
		});
	});
	
	$('.showAllComments').livequery('click', function () {
		var topicID = $(this).parent('div').parent('div').parent('div').children('.topicRowId').val();
		$.post('/intra/index.php?page='+pid+'&sid='+sid+'&action=showcomments&show=all&method=ajax&id='+topicID, function(data) {
			$('.postRow-'+topicID).html(data);
		});
	});

	/* ---- CALENDAR */

	$('.showEventCommentBox').livequery('click', function () {
		$(this).parent('div').parent('div').children('.calendarPostBox').css('display','block');
	});
	
	$('textarea.calendarPostBoxText').livequery('click', function () {
		$(this).addClass('active').val('').parent('div').children('.inputsubmit').css('display','inline','float','left');
	}).autogrow();

	$('input.addNewCalendarPost').livequery('click', function () {
		var comment = $(this).parent('div').children('textarea.calendarPostBoxText').val();
		var eventID = $(this).parent('div').parent('div').children('.eventRowId').val();
		$(this).parent('div').children('textarea.calendarPostBoxText').removeClass('active').val('kirjoita kommentti').parent('div').children('.inputsubmit').css('display','none');
		$.post('/intra/index.php?page='+pid+'&sid='+sid+'&action=addeventcomment&method=ajax&comment='+encodeURIComponent(comment)+'&id='+eventID+"&show=all", function(data) {
			$('.eventRow-'+eventID).html(data);
		});
	});
	
	$('.showAllEventComments').livequery('click', function () {
		var eventID = $(this).parent('div').parent('div').parent('div').children('.eventRowId').val();
		$.post('/intra/index.php?page='+pid+'&sid='+sid+'&action=showeventcomments&show=all&method=ajax&id='+eventID, function(data) {
			$('.eventRow-'+eventID).html(data);
		});
	});

	$('.moveToMonth').livequery('click', function () {
		var year = $(this).parent('div').children('.prevYear').val();
		var month = $(this).parent('div').children('.prevMonth').val();
		$.post('/intra/index.php?page='+pid+'&sid='+sid+'&action=changemonth&show='+year+'-'+month+'&method=ajax', function(data) {
			$('#calendarFrame').html(data);
		});
	});
	
	$('textarea.calendarComment').livequery('click', function () {
		$(this).addClass('active').val('');
	});

	
	$('.loadCalendarDate').livequery('click', function () {
		var time = $(this).children('input').val();
		$.post('/intra/index.php?page='+pid+'&sid='+sid+'&action=showevent&show='+time+'&method=ajax', function(data) {
			$('#calendarRightFrame').html(data);
		});
	});
	
	$('.calendarCreate').livequery('click', function () {
		$('.calendarPhaseRow').children('.create').removeClass('active');
		$('.calendarPhaseRow').children('.create').addClass('done');
		$('.calendarPhaseRow').children('.files').addClass('active');
		var d = $(this).parent('div').children('.day').val();
		var mo = $(this).parent('div').children('.month').val();
		var y = $(this).parent('div').children('.year').val();
		var h = $(this).parent('div').children('.hour').val();
		var mi = $(this).parent('div').children('.minute').val();
		var comment = $(this).parent('div').children('.calendarComment').val();
		$.post('/intra/index.php?page='+pid+'&sid='+sid+'&action=createevent&comment='+encodeURIComponent(comment)+'&time='+d+'-'+mo+'-'+y+'-'+h+'-'+mi+'&method=ajax', function(data) {
			$('.calendarAddDateRow').html(data);
		});
	});
	
	$('#createNewEvent').livequery('click', function () {
		$.post('/intra/index.php?page='+pid+'&sid='+sid+'&action=showeventform&method=ajax', function(data) {
			$('#calendarRightFrame').html(data);
			$('.calendarPhaseRow').children('.create').addClass('active');
		});
	});

	$('#uploadify').livequery(function() {
		var eventID = $(this).parent('div').children('.event_id').val();
		$(this).uploadify({
			'uploader'    : '/manager/uploadify/scripts/uploadify.swf',
			'script'      : '/manager/uploadify/scripts/uploadify.php?id='+eventID+'&item=calendar&page=rokokoo',
			'cancelImg'   : '/manager/uploadify/cancel.png',
			'folder'      : '/files',
			'buttonImg'   : '/kuvat/browse.png',
			'queueID'     : 'fileQueue',
			'height'      : '24',
			'width'       : '41',
			'multi'       : true
		});
	});
	
	$('.calendarMoveToConfirm').livequery('click', function() {
		var eventID = $(this).parent('div').children('.event_id').val();
		$.post('/intra/index.php?page='+pid+'&sid='+sid+'&action=showeventconfirm&id='+eventID+'&method=ajax', function(data) {
			$('.calendarAddDateRow').html(data);
			$('.calendarPhaseRow').children('.files').removeClass('active');
			$('.calendarPhaseRow').children('.files').addClass('done');
			$('.calendarPhaseRow').children('.confirm').addClass('active');
		});
	});
	
});

