DialogButtons = {
    OK : 0,
    YES_NO : 1,
    CANCEL_CONFIRM : 2
};

DialogIcons = {
	NONE: 0,
    ERROR : 1,
    OK : 2
};

//nadpisana reguła 'required' kompatybilna z plugine formHints(aka. watermark);
jQuery.validator.addMethod(
        "requiredWithFormHint",
        function(value, element, regexp) {
            return value.length > 0 && element.title != value;
        },
        "Please check your input."
);

function SetScrollAble(item, options)
{
	if ($(item).offset()){
		$(item).attr('old-pos', $(item).offset().top);
	}
	else
	{
		$(item).attr('old-pos', 0);
	}
	if(options)
	{
		if(options.scroll)
		{
			$(item).bind('scroll_able_scroll', options.scroll);
		}
		if(options.unscroll)
		{
			$(item).bind('scroll_able_unscroll', options.unscroll);
		}
	}
	

	$(window).scroll(function(){
		if ($(window).scrollTop() >=  $(item).attr('old-pos')) {
			$(item).css("width", $(item).css('width'));
			$(item).css("position", "fixed");
			$(item).css("top", 0);
			$(item).trigger('scroll_able_scroll');
		} else if ($(window).scrollTop() < $(item).attr('old-pos')) {
			$(item).css("position", "static");
			$(item).css("top", 0);
			$(item).trigger('scroll_able_unscroll');
		}
	});
}

function ShowDialog(options)
{
	var buttons='', cls='', title = '';
	switch (options.buttons) {
		case DialogButtons.CANCEL_CONFIRM: buttons='<div class="textRight"><a class="return popUpCancelBtn" href="javascript:;">Anuluj</a> <a class="formBtn popUpConfirmBtn" href="javascript:;">Potwierdź</a></a>'; break;
		case DialogButtons.YES_NO: buttons=''; break;
		case null: break;
		default: buttons='<div class="textCenter"><a class="formBtn popUpOkBtn" href="javascript:;">OK</a></div>'; break;
	}
	switch (options.icon) {
		case DialogIcons.OK: cls='popUpOkMsg'; break;
		case DialogIcons.ERROR: cls='popUpNOkMsg'; break;
		case null: break;
		default: cls='popUpMsg'; break;
	}
	if ( options.title != '' ) title = '<h2>'+options.title+'</h2>';
	if ( !options.callbacks ) options.callbacks = {};
	var close = function() { $(document).trigger('close.facebox'); };
	
	
	$(".popUpOkBtn").die().live('click', options.callbacks.ok ? function(){options.callbacks.ok.call(options.context);} : close);
	$(".popUpYesBtn").die().live('click', options.callbacks.yes ? function(){options.callbacks.yes.call(options.context);} : close);
	$(".popUpNoBtn").die().live('click', options.callbacks.no ? function(){options.callbacks.no.call(options.context);} : close);
	$(".popUpCancelBtn").die().live('click', options.callbacks.cancel ? function(){options.callbacks.cancel.call(options.context);} : close);
	$(".popUpConfirmBtn").die().live('click', options.callbacks.confirm ? function(){options.callbacks.confirm.call(options.context);} : close);
	
	$.facebox('<div class="popUp">'+title+'<div class="'+cls+'">'+options.html+'</div>'+buttons+'</div>');
}

function doFlash(item, count)
{
	if( ! $(item).is(':animated') )
	{
		bgColor = $(item).first().css('background-color');
		$(item).animate({ 'background-color': "#FFFBB8" }, 300 );
		$(item).animate({ 'background-color': bgColor }, 300 );
		if(count != 1)
		{
			$(item).delay(300).animate({ 'background-color': "#FFFBB8" }, 300 );
			$(item).delay(600).animate({ 'background-color': bgColor }, 300 );
		}
	}
}

function startLoading(field, side)
{
	if(side = 0 || !side)
	{
		$(field).addClass('ui-right-loading');
	}
	else
	{
		$(field).addClass('ui-left-loading');
	}
}

function stopLoading(field)
{
	$(field).removeClass('ui-left-loading');
	$(field).removeClass('ui-right-loading');
}

jQuery.fn.outerHTML = function(s) { 
	return (s) ? this.before(s).remove() : jQuery("&lt;p&gt;").append(this.eq(0).clone()).html();
}

$(document).ready(function(){
	$('.msg a').live('click', function()
	{
		$(this).parent().stop(true, true);
		$(this).parent().fadeOut(500, function () { $(this).remove(); });
	});
	$(".msg").delay(12000).fadeOut(500, function () { $(this).remove(); });

	$("#facebox-close").live('click', function(){ $(document).trigger('close.facebox'); });
	
	$(".breadcrumb ul").hover(function(){
		$(this).stop().animate({"opacity": 1});
	},
	function(){
		$(this).stop().animate({"opacity": 0.4});
	});
	
	jQuery.validator.addMethod("greaterThanZero", function(value, element) {
		return this.optional(element) || (parseFloat(value) > 0);
	}, "Wartość musi być większa od 0");
	
	$('a.show-notes').live('click', function(event)
	{
		event.preventDefault();
		$.ajax(
			{
				url: '/pracodawca/notatki/'+$(this).attr('data-candidate-id'),
				global: false,
				type: 'post',
				data: ({getNotesList: true}),
				beforeSend: function()
				{
					$.facebox('<div class="notes-wrapper"><div class="notes-box"></div><div class="add-notes"></div></div>');
					addNotesBox = $('.add-notes');
					addNotesBox.append('<span class="bold">Nowa notatka:</span>');
					addNotesBox.append('<textarea></textarea>');
					addNotesBox.append('<input type="submit" value="Dodaj notatkę" class="formBtn" id="add-note"/>');
					notesBox = $('div.notes-box');
					notesBox.append('<div class="loading-notes">Ładuję notatki</div>');
					$('#facebox').css('position', 'fixed');
					$('#facebox').css('top',Math.round($(window).height()/2 - $('#facebox').height()/2));
					$('#facebox').css('left',Math.round($(window).width()/2 - $('#facebox').width()/2));
				},
				success: function(data)
				{
					notesBox = $('div.notes-box');
					notesBox.children('.loading-notes').remove();
					notesBox.html(data.notesList);
					$('#facebox').css('position', 'fixed');
					$('#facebox').css('top',Math.round($(window).height()/2 - $('#facebox').height()/2));
					$('#facebox').css('left',Math.round($(window).width()/2 - $('#facebox').width()/2));
				},
				error: function()
				{
					$('div.notes-box').children('.loading-notes').removeClass('loading-notes').addClass('loading-notes-error').html('Nie udało się pobrać notatek');
				}
				
			});
	});
	
	$('#add-note').live('click', function(event)
	{
		var notesList = $(this).closest('.notes-wrapper').children('.notes-box').children('.notes');
		var candidateId = notesList.data('candidate-id');
		var note = $(this).siblings('textarea').val();
		
		if($.trim(note).length < 1)
		{
			ShowDialog({
				title: 'Nowa notatka',
				html: 'Podaj treść notatki',
				icon: DialogIcons.ERROR
			});
			
			return;
		}
		
		$.ajax({
			url: '/pracodawca/notatki/dodaj/'+candidateId,
			type: 'post',
			data: ({note: note, getNotesList: true}),
			context: notesList,
			beforeSend: function()
			{
				this.mask();
			},
			success: function(data)
			{
				this.unmask()
				if(data.status == 'ok')
				{
					$(this).parent().html(data.notesList);
					var candidateId = $(this).data('candidate-id');
					$('.show-notes').each(function(index, element){
						if($(element).data('candidate-id') == candidateId)
						{
							$(element).html(parseInt($(element).html())+1);
						}
					});
					$('.add-notes textarea').val('');
					doFlash($('.notes .note:first-child'));
				}
			},
			error: function()
			{
				this.unmask();
				ShowDialog({
					buttons: DialogButtons.OK,
					icon: DialogIcons.ERROR,
					title: 'Błąd',
					html: 'Nie udało się dodać nowej notatki.'
				});
			}
			
		});
	});
	
	$('.note-action.delete').live('click', function(event)
			{
				var note = $(this).closest('.note');
				if(confirm('Czy na pewno chcesz usunąć tę notatkę?'))
				{
					$.ajax({
								url: '/pracodawca/notatki/'+note.data('note-id')+'/usun',
								context: note,
								global: false,
								beforeSend: function()
								{
									this.mask();
								},
								success: function(data)
								{
									this.unmask();
									if(data.status == 'ok')
									{
										var candidateId = $(this).closest('.notes').data('candidate-id');
										doFlash(this);
										$(this).fadeOut(500, function(){$(this).remove()});
										$('.show-notes').each(function(index, element){
											if($(element).data('candidate-id') == candidateId)
											{
												$(element).html(parseInt($(element).html())-1);
											}
										});
									}
								},
								error: function()
								{
									this.unmask();
									alert('Nie udało się usunąc notatki');
								}
							});
				}
			});
	
	$('input, select, textarea').focus(function()
	{
		var tootltip = $(this).siblings('label').children('.tooltip');
		if(tootltip)
		{
			intleftpos = parseInt($(this).outerWidth() + $(this).siblings().outerWidth());
			intleftpad = parseInt($(this).parent().css('padding-left'));
			leftpos = intleftpos + intleftpad + 20 + "px";
			$(tootltip).css("left", leftpos);
			$('.tooltip').hide();
			$(tootltip).show();
		}
	});
	
	$('input, select, textarea').blur(function() {
		if($(this).siblings('label').children('.tooltip'))
		{
			$(this).siblings('label').children('.tooltip').hide();
		}
	});
	
	/*
	$(".subMenu a").hover(
		function()
		{
			if( !$(this).hasClass('staticTab') )
				$('.subMenu a.staticTab').removeClass('subActiveTab');
		},
		function()
		{
			if( !$(this).hasClass('staticTab') )
				$('.subMenu a.staticTab').addClass('subActiveTab');
		}
	);
	
	$('#mainMenuWrapper li').mouseout(
		function(){
			if(!$(this).hasClass('specialTab') && !$(this).hasClass('staticTab'))
			{
				$('.staticTab').addClass('activeTab');
			}
		}
	);

	$('#mainMenuWrapper li').mouseover(
		function(){
			if(!$(this).hasClass('specialTab') && !$(this).hasClass('staticTab'))
			{
				$('.staticTab').removeClass('activeTab');
			}
		}
	);
	
	$("#loginForm").formHints();
	*/
});
