$(document).ready(function(){
	var viewer = MediaViewer();
	viewer.init();
		
	$("#detailsTabs").tabs({ selected: 0 });
	
	$(".deletePeople").live("click", function(event){
		event.preventDefault();
		event.stopPropagation();
		var url = $(this).attr('href');
		var splitUrl = url.split('/');
		var id = splitUrl[splitUrl.length-1];
		var deletePeople = function(){
			if (confirm('Are you sure you want to delete this people?')) {
				$('#fixed').css('cursor', 'wait');
				$.post(baseUrl + 'people/edit/delete/',{people:id}, 
						function(data, textStatus) {
					        $('#fixed').css('cursor', 'auto');
					        if(textStatus == 'success') {
					        	$("#people_info_block_"+id).hide("slow");
								$("#people_info_block_"+id).remove();
								alert('This people has been deleted');
					        } else if(textStatus == 'error') {
					        	alert(msg);
				            } 
				});
			}
		};
		loginCheck(deletePeople);
	});

	$('#pcategory-dialog .catCheckBox').click(function(e) {
		var c = $('#pcategory-dialog .catCheckBox:checked').length;
		if(c > 3) {
			$(this).attr('checked', false);
		} 
	});

	$('#pcategory-dialog').dialog({
		autoOpen: false,
		width: 750,
		height: 400,
		modal: true,
		closeOnEscape: false,
		overlay: {
			opacity: 0.5,
			background: 'black'
		},
		open: function(event, ui) { updatePeopleCategoryTree() },
		buttons: { "Ok": function() { updatePeopleCategories(this); }}
	});

	$('#paddCategory').click(function(event){
		$('#pcategory-dialog').dialog('open');
		return false;
	});

	function updatePeopleCategories(dialog) {
		var catcount = 1;
		var newHtml = '';
		$('#pcategory-dialog .catCheckBox:checked').each(function(i) {
			catcount++;
			var topCatName = $(this).parents('ul').find('.catTopCat').text();

			var subCatName = '';
			$(this).parents('li').each(function(i) {
				subCatName = $(this).children('label').text() + ' > ' + subCatName;
			});
			if(subCatName.length) subCatName = subCatName.substr(0, subCatName.length-2);

			newHtml += '<li>' + topCatName;
			newHtml += subCatName.length ? ' > ' + subCatName : '';
			newHtml += ' <a href="pcat'+ (i+1) +'" catId="'+ $(this).attr('cat') +'" value="'+ (i+1) +'" ><img src="' + baseUrl + 'images/icons/delete.png"></img></a><br />';			
			newHtml += '<input type="hidden" id="pcat'+ (i+1) +'" value="'+ $(this).attr('cat') +'" ptopCatName="'+topCatName+'" psubCatName="'+ $(this).siblings('label').text() +'" name="pcat'+ (i+1) +'" /></li>';
		});

		if(!newHtml) $('#pcategoryListMessage').show(); else $('#pcategoryListMessage').hide();

		$('#pcategoryListLinks').html(newHtml);
		$('#pcategoryListLinks a').wTooltip({style: {width: "20%"}, content: 'Click here to remove this category from your event.'});
		$('input#pcategoryId').attr('value', catcount);
		$(dialog).dialog('close');
	}

	function updatePeopleCategoryTree() {
		$('#pcategory-dialog .catCheckBox').attr('checked', false);
		$('#pcategoryListLinks a').each(function(i) {
			$('.catCheckBox[cat=' + $(this).attr('catid') + ']').attr('checked', true);
			$('.catCheckBox[cat=' + $(this).attr('catid') + ']').parents('li').each(function(i,n) {
				if($(this).hasClass('expandable')) {
					$(this).children('.hitarea').trigger('click');
				}
			});
		});
	}

	$('#pcategoryListLinks a').live('click', function(e) {
		var remCat = $(this).attr('catId');
		var newHtml = '';
		var newCount = 1; 

		$('#pcategoryListLinks a').each(function(i){
			//first get the id we are after
			var currentCat = $("a[href^='pcat']").eq(i).attr('value');
			//check if its the one we dont want
			var catId = $('input#cat'+currentCat).attr('value');

			if(catId != remCat) {					
				//first ge the id we are after
				var currentCat = $("a[href^='pcat']").eq(i).attr('value');
				//check if its the one we dont want
				var catId = $('input#pcat'+currentCat).attr('value');
				if(catId != remCat) {					
					var ptopCatName = $('input#pcat'+currentCat).attr('ptopCatName');
					var psubCatName = $('input#pcat'+currentCat).attr('psubCatName');
									
					newHtml += '<li>' + ptopCatName;
					newHtml += (psubCatName!='') ? ' > ' + psubCatName : ''; 
					newHtml += ' <a href="pcat" catId="'+currentCat+'" value="'+newCount+'" ><img src="' + baseUrl + 'images/icons/delete.png"></img></a>';
					newHtml += '<input type="hidden" id="pcat'+newCount+'" value="'+currentCat+'" ptopCatName="'+ptopCatName+'" psubCatName="'+psubCatName+'" name="pcat'+newCount+'" /></li>';
					
					newCount++;
				}
			}
		});		

		if(!newHtml) $('#pcategoryListMessage').show(); else $('#pcategoryListMessage').hide();
			
		$('#pcategoryListLinks').html(newHtml);
		$('input#pcategoryId').attr('value', newCount);
		return false;
	});


});
