/*
* Custom jQuery functions
**/
var currentProjectGroupId = 0;

	
// Needed for ajax overview
var nProjectGroupId = 0;
var nPageId = 0;
var sImageFolder = 0;
var sImageModuleInputTypeName = 0;

/* Sitemap */
function toggleSitemap(id, aId) {
	if($('#'+id).css('display') == 'none') {
		$('#'+id).css('display','block');
		$('a#'+aId+' img.toggleButton').attr('src',imgPath+'sitemap/min.png');
	}else{
		$('#'+id).css('display','none');
		$('a#'+aId+' img.toggleButton').attr('src',imgPath+'sitemap/plus.png');
	}
}


$(document).ready(function() {	
	// zoekveld
	var searchFieldText = 'Typ het zoekwoord...';
	returnValue('searchField', searchFieldText);
	
	$('#searchField').bind('focus', function(event) {
		emptyValue('searchField', searchFieldText);
	});
	$('#searchField').bind('blur', function(event) {
		returnValue('searchField', searchFieldText);
	});
	/*
	$('#search').bind('submit', function(event) {
		emptyValue('searchField', searchFieldText);
	});
	*/
	
	// tabs
	$('.tabLinks a').bind('click', function(event) {
		event.preventDefault();
		nr = $(this).attr('className').substr(7,1);
		//alert(nr);
		toggleTab(nr);
	});
	
	// rating email
	var emailFieldText = 'Typ jouw e-mailadres...';
	try{
		returnValue('emailField', emailFieldText);
	}catch(e){ }
	
	$('#emailField').bind('focus', function(event) {
		$('#emailField').removeClass('error');
		emptyValue('emailField', emailFieldText);		
	});
	$('#emailField').bind('blur', function(event) {
		returnValue('emailField', emailFieldText);
	});
	$('#commentText').bind('focus', function(event) {
		$('#commentText').addClass('white');
	});
	$('#commentText').bind('mouseEnter', function(event) {
		$('#commentText').addClass('white');
	});
	$('#commentText').bind('blur', function(event) {
		$('#commentText').removeClass('white');
	});
	
	// Print stuff
	addExtraHTMLforPrint();
});


function emptyValue(elm, tekst){
	if(document.getElementById(elm).value==tekst){
		document.getElementById(elm).value="";
	}
}
function returnValue(elm, tekst){
	if(document.getElementById(elm).value==""){
		document.getElementById(elm).value=tekst;
	}
}

function toggleTab(nr){
	// set active tab
	$('.tabLinks a').removeClass('active');
	$('.tabLinks a.tablink'+nr).addClass('active');
	// show relevant content
	$('.kladblok .tabContent').hide();
	$('#tab_'+nr).show();
	sifrTabTitle();
	setCurrentTabInSession(nr);
}

function rate(n){
	$('#commentRating').removeClass().addClass('rating'+n);
	$('#inputRating').val(n);
}



/*
 * Load projectsoverview through ajax
 */
function loadProjectsOverview(projectGroupId,pageId,imageFolder,imageModuleInputTypeName) {
	nProjectGroupId = projectGroupId;
	nPageId = pageId;
	sImageFolder = imageFolder;
	sImageModuleInputTypeName = imageModuleInputTypeName;
	
	generateOverviewHTML();
}

function generateOverviewHTML() {
	var serializedData ='';
	serializedData = 'action=getProjects&projectGroupId='+nProjectGroupId+'&pageId='+nPageId+'&imageFolder='+sImageFolder+'&imageModuleInputTypeName='+sImageModuleInputTypeName;
	
	displayOverviewAjaxLoaders();
	
	$.ajax({
		type: 'GET',
		url: rootPath+'pages/ajax_functions.ajax.php',
		data: serializedData,
		dataType: 'json',
		success: function(msg) {
			$('#overviewContainer').html(msg['overviewHTML']);
			$('#filterForm').html(msg['filtersHTML']);
			$('#filterAmount').html(msg['totalProjectsFound']+' gevonden');
			// Make overlay labels clickable
			makeOverlayLabelsClickable();
			// Add pagebreak for print on every 16th element
			$('#overviewContainer .traktatieItem:nth-child(16n)').css('page-break-after','always');
		}
	});
}

/**
 *	Loop through all div.traktatieItem, find link in it and make div.overlayLabel in it clickable
 */
function makeOverlayLabelsClickable() {	
	$('#overviewContainer .traktatieItem a').each(function(index) {
		var aObject = $(this);
		var thisUrl = $(aObject).attr('href');
		// Find all div.overlayLabel in the parent of this a tag (the traktatieitem div and add the click to link)
		var traktatieItem = $(aObject).parent();
		$(traktatieItem).children("div.overlayLabel").click(function() {
			location.href = thisUrl;
		});
		// Also maintain the hover state
		/*$(traktatieItem).children("div.overlayLabel").mouseenter(function() {
			$(aObject).addClass('hover');
		});
		$(traktatieItem).children("div.overlayLabel").mouseleave(function() {
			$(aObject).removeClass('hover');
		});*/
	});
}

function applyFilterOption(groupId,filtergroupName,optionName,optionValue) {
	var serializedData ='';
	serializedData = 'action=applyFilterOption&projectGroupId='+groupId+'&filtergroupName='+filtergroupName+'&optionName='+optionName+'&optionValue='+optionValue;
	
	$.ajax({
		type: 'GET',
		url: rootPath+'pages/ajax_functions.ajax.php',
		data: serializedData,
		dataType: 'json',
		success: function(msg) {
			generateOverviewHTML();
		}
	});
	
	
}

function removeAppliedFilter(groupId,filtergroupName,optionValue) {
	var serializedData ='';
	serializedData = 'action=removeFilterOption&projectGroupId='+groupId+'&filtergroupName='+filtergroupName+'&optionValue='+optionValue;

	$.ajax({
		type: 'GET',
		url: rootPath+'pages/ajax_functions.ajax.php',
		data: serializedData,
		dataType: 'json',
		success: function(msg) {
			generateOverviewHTML();
		}
	});
}

function loadComments(nItemId, nMid, nLimit) {
	// Get data and fill in overview
	var dataFeed = {
		action: 'getProjectComments',
		itemId: nItemId,
		limit: nLimit,
		mId : nMid
	}
	
	$.ajax({
		type: 'GET',
		url: rootPath+'pages/ajax_functions.ajax.php',
		data: dataFeed,
		dataType: 'json',
		success: function(msg) {
			$('#commentsContent').html(msg['content']);
			if(msg['totalComments'] < 4 || (msg['totalComments'] == msg['foundComments'])) {
				$('a#moreComments').hide();
			}else{
				$('a#moreComments').show();
			}
		}
	});
}

function flagAsOffensive(commentId) {
	// Flag it
	var dataFeed = {
		action: 'flagAsOffensive',
		itemId: commentId
	}
	
	$.ajax({
		type: 'GET',
		url: rootPath+'pages/ajax_functions.ajax.php',
		data: dataFeed,
		success: function(msg) {}
	});
	// Hide the link
	$('a#notOK_'+commentId).remove();
}

function resetFilters(groupId) {
	var serializedData ='';
	serializedData = 'action=removeAllFilterOptions&projectGroupId='+groupId;
	
	$.ajax({
		type: 'GET',
		url: rootPath+'pages/ajax_functions.ajax.php',
		data: serializedData,
		dataType: 'json',
		success: function(msg) {
			generateOverviewHTML();
		}
	});
}

/* Ecard functions */
function deleteExtraReceiver(extraFormId) {
	$('#extraForm_'+extraFormId).remove();
	
	// If there's only one form left delete the last 'remove extra form' button so we can't delete all forms
	if($('.extraForm').size() == 1) {
		$('a.extraFormDelete').remove();
	}
}

function addExtraReceiver() {
	totalExtraForms++;
	
	var html = '<div class="extraForm" id="extraForm_'+totalExtraForms+'"><div class="formRow"><label for="nameReceiver_'+totalExtraForms+'">Naam ontvanger</label><input type="text" name="nameReceiver[]" id="nameReceiver_'+totalExtraForms+'" value="" class="ecardField"/><div class="clear"></div></div><div class="formRow"><label for="emailReceiver_'+totalExtraForms+'">E-mail ontvanger</label><input type="text" name="emailReceiver[]" id="emailReceiver_'+totalExtraForms+'" class="ecardField"/><div class="clear"></div></div><a class="extraFormDelete" href="#" onclick="deleteExtraReceiver(\''+totalExtraForms+'\');return false;">X</a></div>';
	$('#extraFormsContainer').append(html);
}

function filterType(value,nProjectGroupId,nPageId,sImageFolder,sImageModuleInputTypeName) {
	$('select#senderTypeFilter').val(value);
	// Reapply filter
	loadProjectsOverview(nProjectGroupId,nPageId,sImageFolder,sImageModuleInputTypeName);
}

function addExtraHTMLforPrint() {
	var extraHTML = '<div id="printHeader"><img src="'+imgPath+'print_header.gif" alt="printHeader"/></div>';
	$('div#page').prepend(extraHTML);
}

function expandComment(commentId) {
	$('#comment_'+commentId+' span.dots').hide();
	$('#comment_'+commentId+' span.commentLonger').show();
	$('#comment_'+commentId+' a.commentReadMore').hide();	
}

function setCurrentTabInSession(nTabnr) {
	var dataFeed = {
		action: 'setCurrentTabInSession',
		projectGroupId: currentProjectGroupId,
		tabnr : nTabnr
	}

	$.ajax({
		type: 'GET',
		url: rootPath+'pages/ajax_functions.ajax.php',
		data: dataFeed,
		success: function(msg) {}
	});
}

function emptyCommentField(startingText) {
	if($('#commentText').val() == startingText) {
		$('#commentText').val('');
	}
}

function blurCommentField(startingText) {
	if($('#commentText').val() == '') {
		$('#commentText').val(startingText);
	}
}

function displayRating(nr) {
	$('#commentRating').removeClass().addClass('rating'+nr);
}

function resetRating() {
	$('#commentRating').removeClass().addClass('rating'+$('#inputRating').val());
}

function displayOverviewAjaxLoaders() {
	$('#overviewContainer').html('<div id="overviewAjaxLoader"></div>');
	$('#filterForm').html('<div id="filtersAjaxLoader"></div>');
}

function hideOverviewAjaxLoaders() {
}
