$("#menu").ready(function(){
	menuClassBackup = '';
	$("#menu li").mouseover(function(){
		menuClassBackup = $(this).attr("class");
		$(this).attr("class", menuClassBackup+" hover");
	})
	$("#menu li").mouseout(function(){
		$(this).attr("class", menuClassBackup);
	})
})
$(document).ready(function(){
	$('#zButtWrap a').click(function(){
		if ($('#boxes_right_content').attr('class') == 'hidden') {
			$('#boxes_right_content').slideDown();
			$('#boxes_right_content').attr('class','');
		} else {
			$('#boxes_right_content').slideUp();
			$('#boxes_right_content').attr('class','hidden');
		}
		$.get('?swBoxes', function(data) {});
		return false;
	})
	$('#doporucenie a').click(function(){
		var odkaz = $(this).attr('href');
		odkaz += '&saveDoporucenie';
		$.get(odkaz, function(data) {
			$('#doporucenie').html('<h4>'+$('#doporucenie').attr('rel')+'</h4>');
		});
		return false;
	})
	/* form action */
	initForm();
	var objects = $('#boxes_right .FP_uniForm .fText');
	$.each(objects,function(){
		var id = $(this).attr('id');
		var content = $('#'+id+' label').html();
		content = content.split("<span>*</span>").join("*");
		content = content.split("<SPAN>*</SPAN>").join("*");
		if (content.split("",200).pop() != '*') {
			content += ' *';
		}
		if (!$('#' + id + ' input').val()) {
			$('#' + id + ' input').val(content);
		}
		$('#'+id+' input').attr('rel',content);
		if ($('#'+id+' .error').html()) {
			$('#' + id + ' input').attr('backup',$('#' + id + ' input').val());
			$('#' + id + ' input').val($('#'+id+' .error').html());
		}
	})
	$('#boxes_right .FP_uniForm .fText input').focus(function(){
		if ($(this).attr('backup')) {
			$(this).val($(this).attr('backup'));
			$(this).attr('backup','');
		}
		if ($(this).val() == $(this).attr('rel')) {
			$(this).val('');
		}
		
	})
	$('#boxes_right .FP_uniForm .fText input').blur(function(){
		if (!$(this).val()) {
			$(this).val($(this).attr('rel'));
		}
	})
	var objects = $('#boxes_right .FP_uniForm .fTextarea');
	$.each(objects,function(){
		var id = $(this).attr('id');
		var content = $('#'+id+' label').html();
		content = content.split("<span>*</span>").join("*");
		content = content.split("<SPAN>*</SPAN>").join("*");
		if (!$('#' + id + ' textarea').val()) {
			$('#' + id + ' textarea').val(content);
		}
		$('#'+id+' textarea').attr('rel',content);
		if ($('#'+id+' .error').html()) {
			$('#' + id + ' textarea').attr('backup',$('#' + id + ' textarea').val());
			$('#' + id + ' textarea').val($('#'+id+' .error').html());
		}
	})
	
	$('#boxes_right .FP_uniForm .fTextarea textarea').focus(function(){
		if ($(this).attr('backup')) {
			$(this).val($(this).attr('backup'));
			$(this).attr('backup','');
		}
		if ($(this).val() == $(this).attr('rel')) {
			$(this).val('');
		}
	})
	$('#boxes_right .FP_uniForm .fTextarea textarea').blur(function(){
		if (!$(this).val()) {
			$(this).val($(this).attr('rel'));
		}
	})
	$('.FP_uniForm form').submit(function(){
		var objects = $('#boxes_right .FP_uniForm .fText input, #boxes_right .FP_uniForm .fTextarea textarea');
		$.each(objects,function(){
			if ($(this).attr('backup')) {
				$(this).val($(this).attr('backup'));
				$(this).attr('backup','');
			}
			if ($(this).val() == $(this).attr('rel')) {
				$(this).val('');
			}
		})
		return true;
	})
	/* form posun */
	var href = document.location.href;
	href = href.split('?',2);
	if (href[1] == 'odoslat') {
		document.location.href = '#boxes_right';
	}
})
function initForm() {
	var objects = $('#stdFormElement_c_typ select option');
	var elementSubmitName = $('#stdFormElement_c_typ select').attr('name');
	var pole = new Array();
	$.each(objects,function(){
		var item = new Array();
		item.push($(this).attr('value'));
		item.push($(this).html());
		item.push($(this).attr('selected'));
		pole.push(item);
	})
	var poleObjektov = new Array();
	for (var a=0;a<pole.length;a++) {
		var obj = pole[a];
		if (!obj[2]) {
			var tpl = '<label><input name="'+elementSubmitName+'" type="radio" value="'+obj[0]+'"/> '+obj[1]+'</label>';
		} else {
			formSwFnc(obj[0]);
			var tpl = '<label><input name="'+elementSubmitName+'" type="radio" value="'+obj[0]+'" checked="checked"/> '+obj[1]+'</label>';
		}
		poleObjektov.push(tpl);
	}
	var tpl = poleObjektov.join(' '); 
	$('#stdFormElement_c_typ').html(tpl);
	$('#stdFormElement_c_typ input').click(function(){
		var id = $(this).attr('value');
		formSwFnc(id);
	})
}
function formSwFnc(id) {
	if (id == '1') {
		$('#stdFormElement_c_mesto').css('display','block');
		$('#stdFormElement_c_email').css('display','block');
		$('#stdFormElement_c_firma').css('display','block');
		$('#stdFormElement_c_ico').css('display','block');
		
		$('#stdFormElement_c_mesto input').val($('#stdFormElement_c_mesto input').attr('rel'));
		$('#stdFormElement_c_email input').val($('#stdFormElement_c_email input').attr('rel'));
		$('#stdFormElement_c_firma input').val($('#stdFormElement_c_firma input').attr('rel'));
		$('#stdFormElement_c_ico input').val($('#stdFormElement_c_ico input').attr('rel'));
	} else {
		$('#stdFormElement_c_mesto').css('display','none');
		$('#stdFormElement_c_email').css('display','none');
		$('#stdFormElement_c_firma').css('display','none');
		$('#stdFormElement_c_ico').css('display','none');

		$('#stdFormElement_c_mesto input').val('- '+$('#stdFormElement_c_mesto input').attr('rel'));
		$('#stdFormElement_c_email input').val('- email@email.sk');
		$('#stdFormElement_c_firma input').val('- '+$('#stdFormElement_c_firma input').attr('rel'));
		$('#stdFormElement_c_ico input').val('- '+$('#stdFormElement_c_ico input').attr('rel'));
	}
}

