/* check for screenname */
$(document).ready(function(){

	// Get the source directory of this script
	scriptSrc = $('script[@src$=jquery.js]').attr('src').replace('js/jquery.js', '');

	if ($("#checkScreenname").length) {
		$("#checkScreenname").click(
			function() {
				$("#alert").hide();
				$.ajax({
					type: "POST",
					url: scriptSrc+"scripts/checkScreenname.php",
					data: $("#userform").formSerialize(),
					dataType: "json",
					success: function(msg){
						if (msg.event==1) {
							// foutmelding weergeven
							alert(msg.eventText);
						}
					}
				});
				return false;
			}
		);
	}

	$('a.saveForm').click(function(){
		var thisForm = $('form#favform');
		thisForm[0].submit();
		return false;
	});

	if($('.handle').length){
		// Remove a project from the guide list
		$('a.remove_project').click(function(){
			$(this).parent().parent().remove();
			$('#div#favorites').SortableDestroy();
			initSorting();
		});
		initSorting();
	}
	
	// Display or hide the sepparate billing address for business accounts
	if($('#billingtype').length){
		if($('#billingtype').is(':checked')){ $('.billing').hide(); }
		
		$('#billingtype').click(function(){
			if(!$(this).is(':checked')){ $('.billing').show(); }
			else{ $('.billing').hide(); }
		});
	}
	
	// Enable account selection
	if($('#accountselect').length){
		$('#accountselect').change(function(){
			if($(this).val()){ window.location.href = $(this).val(); }
		});
	}
	
	// Enable dragging account pages
	if($('#accountpages').length){
		initPageSorting();
		initPageDelete();
	}
	
	// Make the pagetitle safe html
	$('#pagetitle').blur(function(){
		$(this).val(makeSafe($(this).val(), 1));
	});

});

//Initialise deleting of pages
function initPageDelete(){
	$('#accountpages .deletepage').click(function(){
		var thisItem = $(this).parents('.pageItem');
		var pageId = $('input.pageid', thisItem).val();
		var str = 'pageId=' + pageId;
		if(pageId){
			$.ajax({
				type: "POST",
				url: "/scripts/delete_page.php",
				data: str,
				success: function(msg){
					if(msg){
						thisItem.remove();
						$('.birdbutton').show();
					}
				}
			});
		}
	});
}

// Initialise the page sorting
function initPageSorting(){
	$('#accountul').Sortable({
		accept : 'pageItem',
		activeclass : 'extra_active',
		hoverclass : 'extra_hover',
		helperclass : 'extra_helper',
		handle: '.handle',
		axis: 'vertically',
		opacity: 	0.5,
		onStop: function(){ sortPageProgression() }
	});
}

function sortPageProgression(){
	$('#accountpages ul li').each(function(i){
		$('input.position', this).val(i+1);
		$('td.firstcol', this).html(''+(i+1));
	});
	
	var str = $('form#accountpages').formSerialize();

	$.ajax({
		type: "POST",
		url: "/scripts/save_page_progression.php",
		data: str,
		success: function(msg){
			//alert(msg);
		}
	});
}

// Make guide elements sortable
function initSorting(){
	$('div#favorites').Sortable({
		accept : 'infoblock',
		activeclass : 'extra',
		hoverclass : 'extra',
		helperclass : 'extra',
		handle: '.handle',
		axis: 'vertically',
		opacity: 	0.5,
		fit :	true,
		onStop: function(){ sortProgression(); }
	});
}

// Sort the page progression
function sortProgression(){
	$('#favorites input.position').each(function(i){
		$(this).val(i+1);
	});
}

// Add something to the filelist
function addToFilelist(text){
	$('#imageSelector').html(text);
	$('#imageSelector .thumb').hover(function(){
		$('div', this).show();
	},function(){
		$('div', this).hide();
	});
	
	$('#imageSelector .thumb .add').click(function(){
		img = $(this).siblings('.tag').val();
		str = '<img src="' + img + '" alt="" border="0" />';
		addToTiny(str);
	});
	
	$('#imageSelector .thumb .remove').click(function(){
		if(confirm('Are you sure you want to remove this image?')){
			window.frames['uploadframe'].window.removeImage($(this).siblings('.image_id').val());
		}
	});
}

// Adding content to the end of the tinymce editor
function addToTiny(newContent){
	var inst = tinyMCE;
	inst.execCommand('mceFocus',false,'contentedit');
	inst.execCommand('mceInsertContent', true, newContent);
}