// JavaScript Document
$(function(){
	function FocusBlur(){
		$('input,textarea').filter('.focus').focus(function(){
			$('label[for='+$(this).attr('id')+']').hide();
		}).blur(function(){
			if ($(this).val()=='')
				$('label[for='+$(this).attr('id')+']').show();
		}).each(function(){
			if ($(this).val()!='')
				$('label[for='+$(this).attr('id')+']').hide();
		});
	}
	FocusBlur();
	
	
	function inputFile(){
		$('form.submit input[type=file]').change(function(){
			var $val = $(this).val();
			reWin = /.*\\(.*)/;
			var fileTitle = $val.replace(reWin, "$1");
			reUnix = /.*\/(.*)/;
			fileTitle = fileTitle.replace(reUnix, "$1");
			$(this).siblings('input[type=text]').val(fileTitle);
			
			var $fn = $('form.submit input[type=file]')
			var noEmptyInput = true;
			for( var i = 1; i < $fn.length; i++ )
				noEmptyInput &= $fn.eq(i).val() != '';
			if ( noEmptyInput ){
				var $pf = $fn.eq(0).parents('p:first');
				var $pl = $fn.eq($fn.length-1).parents('p:first');
				$pf.clone(true).insertAfter($pl).show();
			}
		});
	}
	inputFile();
	
	
	function inputVideo(){
		$('form.submit input[name^=work_video]').change(function(){
			var $in = $('form.submit input[name^=work_video]');
			var noEmptyInput = true;
			for( var i = 1; i < $in.length; i++ )
				noEmptyInput &= $in.eq(i).val() != '';
			if ( noEmptyInput ){
				var $pf = $in.eq(0).parents('p:first');
				var $pl = $in.eq($in.length-1).parents('p:first');
				var $pc = $pf.clone(true);
				var id = 'work_video' + $in.length;
				$pc.find('input').attr('id',id).val('');
				$pc.find('label').attr('for',id).show();
				$pc.insertAfter($pl);
			}
		});
	}
	inputVideo();
	
	
	function submitForm(){
		var $form = $('form');
		$form.each(function(){
			var $obj = $(this);
			$obj.submit(function(){
				return CheckForm.validate( $obj.context );
			});
			var $submit = $obj.find('a.r-button, a.b-button');
			$submit.each(function(){
				var $link = $(this);
				$link.click(function(){
					if ( CheckForm.validate( $obj.context ) )
						$obj.context.submit();
					return false;
				});
			});
		});
	}
	submitForm();
	
	
	$('#remember').click(function(e) {
		e.preventDefault();
		var content = $('#remember-password').html();
		$.fn.nyroModalManual({
			bgColor: '#000',
			content: content,
			width: '200',
			minHeight: 0
		});
		return false;
	});
	
	
	$('#video a').click(function(e){
		e.preventDefault();
		$.fn.nyroModalManual({
			bgColor: '#000',
			content: '<center>' + $(this).attr('video') + '</center>',
			width: '500',
			minHeight: 0
		});
		return false;
	});
});

function vote( link, mark ){
	var $link = $(link);
	$.post( $link.attr('href'), { 'mark': mark, 'action': 'vote' }, function(data){
		$link.parents('div:first').fadeOut();
	});
	return false;
}


