var checkGroups = new Array('form_doruceni', 'form_platba');

var firstGroupRules = new Array('1', '2');
var secondGroupRules = new Array('1', '3');

var groupRules = new Array(0, firstGroupRules,  secondGroupRules);

function enableCheckboxGroup(className)
{
	$('.' + className).click(function(){
		var clickedOn = $(this).get(0);

		$('input[name=' + $(this).attr('name') + ']').each(function(){
			if (clickedOn == $(this).get(0))
				return;
			$(this).removeAttr('checked');
		});
	});
}

function setGroupDisable(elementName, disable)
{
	$('input[name=' + elementName + ']').each(function(){
		if (!disable) {
			$(this).removeAttr('disabled');
			$(this).parent('td').parent('tr').removeClass('disabled');
		} else {
			$(this).attr('disabled', 'disabled');
			$(this).parent('td').parent('tr').addClass('disabled');
		}
	});
}


$(document).ready(function(){

	$('.radaZbozi').each(function(){
		var lineHeight = 0;
		
		$(this).children('div').children('div').children('p.perex').each(function(){
			if (lineHeight < $(this).height())
				lineHeight = $(this).height();			
		});

		$(this).children('div').children('div').children('p.perex').each(function(){
			if (lineHeight != $(this).height())
				$(this).height(lineHeight);			
		});
		
	});

	// zajistuje funkcnost skupiny checkboxu jako radio buttonu
	enableCheckboxGroup('radioGroup');
	
	// check kombinacnich pravidel - pokud zadna pravidla pro danou moznost nejsou, tak se polozky disabluji
	$('.radioGroup').click(function(){
		var groupIndex = jQuery.inArray($(this).attr('name'), checkGroups);
		var clickedOn = $(this).get(0);

		if (groupIndex > -1) {
			var groupCheckIndex = (groupIndex == 0) ? 1 : 0;

			if ($(this).attr('checked')) {

				// check stavu parent > children
				if (groupCheckIndex == 1) {
					if (groupRules[$(clickedOn).val()] != undefined) {
						$('input[name=' + checkGroups[groupCheckIndex] + ']').each(function(){
							if (jQuery.inArray($(this).val(), groupRules[$(clickedOn).val()]) > -1) {
								$(this).removeAttr('disabled');
								$(this).parent('td').parent('tr').removeClass('disabled');								
							} else {
								$(this).attr('disabled', 'disabled');
								$(this).parent('td').parent('tr').addClass('disabled');
							}
						});			
					} else {
						setGroupDisable(checkGroups[groupCheckIndex], true);
					}
				}
				
				// check stavu children > parent
				else {
					$('input[name=' + checkGroups[groupCheckIndex] + ']').each(function(){
						if (groupRules[$(this).val()] != undefined) {
							if (jQuery.inArray($(clickedOn).val(), groupRules[$(this).val()]) > -1) {
								$(this).removeAttr('disabled');
								$(this).parent('td').parent('tr').removeClass('disabled');	
							} else {
								$(this).attr('disabled', 'disabled');
								$(this).parent('td').parent('tr').addClass('disabled');
							}
						} else {
							$(this).attr('disabled', 'disabled');
							$(this).parent('td').parent('tr').addClass('disabled');
						}	
					});				
				}
				
			} else {
				setGroupDisable(checkGroups[groupCheckIndex], false);
			}

		}
	});

});








var spolupraceValidator = {

	valid: new Array(),
	
	check: function(e) {
		var classes = $(e).attr('class').split(' '); 
		for (var i=0; i<classes.length; i++) {
			var toCall = 'this.'+classes[i]+'(e)';
			if (this.existuje(classes[i])) {	
				if (eval(toCall) == false) {
					return false;
				}
			}
		}	
	},
	
	addForm: function(formId) {
		spolupraceValidator.valid[formId] = true;

		$('#' + formId + ' :input').blur(function () {
			spolupraceValidator.check(this);
		});

		$('#' + formId).submit(function() {
			spolupraceValidator.valid[formId] = true;
			$('#' + formId + ' :input').each(function () {
				spolupraceValidator.check(this);
			});
			return spolupraceValidator.valid[formId];
		});
	},
	
	existuje: function(funcName) {
		if (typeof funcName == 'string' && funcName.length > 0 && eval('typeof this.' + funcName) == 'function') {
			return true;
		} else {
			return false;
		}
	},
	
	reportChyby: function(e, chyba) {
		this.valid[$(e).parents('form').attr('id')] = false;
		$("#fe_" + $(e).attr('id')).empty();
		$("#fe_" + $(e).attr('id')).append(chyba);
	},
	
	reportOk: function(e) {
		$("#fe_" + $(e).attr('id')).empty();
	},

	povinny: function(e) {
		if ($(e).val().length == 0 || $(e).val() == null) {
			this.reportChyby(e, 'Položka je povinná');
			return false;
		} else {
			this.reportOk(e);
			return true;
		}
   },
   
	email: function(e) {
		if ($(e).val().length == 0 || $(e).val() == null || $(e).val().match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)) {
			this.reportOk(e);
			return true;
		} else {
			this.reportChyby(e, 'Neplatná emailová adresa');
			return false;
		}
   },
   
   telefon: function(e) {
		if ($(e).val().length == 0 || $(e).val() == null || $(e).val().match(/^(\+420)? ?\d{3} ?\d{3} ?\d{3}$/)) {
			this.reportOk(e);
			return true;
		} else {
			this.reportChyby(e, 'Vyplňte tel. číslo ve správném tvaru');
			return false;
		}
   },
   
   password: function(e) {
		if ($(e).val().length == 0 || $(e).val() == null || $(e).val().match(/^.{6,15}$/)) {
			this.reportOk(e);
			return true;
		} else {
			this.reportChyby(e, 'Délka hesla musí být 6 až 15 znaků');
			return false;
		}
   },
   
   pravidla: function(e) {
		if ($(e).attr('checked')) {
			this.reportOk(e);
			return true;
		} else {
			this.reportChyby(e, 'Musíte souhlasit s pravidly tohoto serveru');
			return false;
		}
   },
   
   uzivatel: function(e) {
		var serviceUrl = '/ajax/uzivatel';
		$("#fe_" + $(e).attr('id')).append('<img src="/s/default/img/progress.gif" width="16px" height="16px" />');
		return $.getJSON(serviceUrl, { uzivatel: $(e).val() }, function(json){
			if (json.error == '0') {
				spolupraceValidator.reportOk(e);
				return true;
			} else {
				spolupraceValidator.reportChyby(e, 'Tento uživatel je již v systému registrován');
				return false;
			}
		});
   }
}

/* prepinac */
	var activeSlide = 0;
	var slideLength;
	var slideTimeout;

$(document).ready(function() {
		$("a.thickbox").fancybox();
		$("a.hodnoceniThickbox").fancybox({
			frameHeight: 450,
			callbackOnClose: function(){hideHodnoceni();}
		});

		var activeHodnoceni;

		$('.slideshow').addClass('enabled');
		$('.offer').append('<ul id="slideNav"></ul>');

		$('.slideshow li').each(function(i){
			if (i != activeSlide)
				$(this).hide();
			
			$('#slideNav').append('<li><a href="#">' + (i + 1) + '</a></li>');
			$('#slideNav li').eq(0).addClass('active');
		});	
		
		$('#slideNav a').each(function(i){
			$(this).hover(function(){
					clearTimeout(slideTimeout);
					switchSlide(i);
					return false;
			});
		})
		
		slideLength = $('.slideshow li').length;
		slideTimeout = setTimeout('switchSlide()', 6000);
});


function switchSlide(neco) {
	if (neco != undefined) {
		activeSlide = neco - 1;
	}

	if (activeSlide + 1 >= slideLength)
		activeSlide = 0;
	else
		activeSlide++;	

	$('#slideNav .active').removeClass('active');
	$('#slideNav li').eq(activeSlide).addClass('active');
	$('.slideshow .active').fadeOut('slow');
	$('.slideshow .active').removeClass('active');
	$('.slideshow li').eq(activeSlide).fadeIn('slow');
	$('.slideshow li').eq(activeSlide).addClass('active');	
	slideTimeout = setTimeout('switchSlide()', 6000);
}


