/*============ SEZNAM FUNKCIONALIT =================
#1 - Vymazání textu v inputu po kliknutí
#2 - Zpřístupnění tlačítka po odsouhlasení podmínek
#3 - Externi odkazy
#4 - Tisk stránek
#5 - Animovaný kalendář
==================================================*/

// #1 - Vymazání textu v inputu po kliknutí
function inputHelpText(selector, defaultValue){
	
	var searchFocusedClass = 'search-focused';

	// Input s heslem - nelze přepisoval attr "value", vytvoříme náhradní input s type="text"
	if($(selector).attr('type') == 'password'){
		var fakePassInput = '<input id="password-clear" class="fi-text" type="text" value="' + defaultValue + '" autocomplete="off">'
		$(selector).hide().after(fakePassInput);
	
		$('#password-clear').focus(function(){
			$(this).hide();									
			$(selector).show().trigger('focus');
		});
		
		$(selector).blur(function(){
			if($(this).val() == ''){
				$(this).hide();
				$('#password-clear').show();
			}
		});
	}
	// Klasický input
	else {
		$(selector).each(function() {
			if($.trim(this.value) == ''){
				this.value = defaultValue;
			}
		});
		
		$(selector).focus(function(){
			if(this.value == defaultValue){
				this.value = '';
				$(this).parent().addClass(searchFocusedClass);
			}
		});
		
		$(selector).blur(function(){
			if($.trim(this.value) == ''){
				this.value = defaultValue;
				$(this).parent().removeClass(searchFocusedClass);
			}
		});
	}
}
	
// #2 - Zpřístupnění tlačítka po odsouhlasení podmínek
function agreeWithTherms(selectorCheckbox, selectorSubmit){
	var checkbox = $(selectorCheckbox);
	var button = $(selectorSubmit); // tlačítko "odeslat"
	
	button.attr('disabled', true).addClass('disabled'); // tlačítku přidáme atribut a třídu DISABLED
	
	checkbox.click(function(){
		if (checkbox.attr('checked')){
			button.attr("disabled", false).removeClass('disabled'); //checkbox zatrhnut => tlačítko se zpřístupní
		}
		else {
			button.attr("disabled", true).addClass('disabled');
		}
	});
};

// #3 - Externi odkazy - cílovou stránku otevře v novém okně/panelu
//	  -	Automaticky se vyberou odkazy s URL začínající http:// a odkazy označené class "newWindow"
var externalLinks = function() {
	$.expr[':'].external = function(obj){	// knihovnu jQuery rozšíříme o selektor "external"
		return !obj.href.match(/^mailto\:/) && (obj.hostname != location.hostname);     //Vybereme pouze externi odkzazy
	};
	
	$('a:external, a.newWindow')
		.addClass('link-external')
		//.append('<span class="h1dden"> (externí odkaz)</span>')     // indikator externiho odkazu pro handikepované uživatele
		.click(function(){
			window.open(this.href);
			return false;
		});
}

// #4 - Tisk stránek
$.fn.print = function() {
	$(this).click(function(){
		print();
		return false;
	});
};

// #5 - Animovaný kalendář
var calendarID = '#adt-calendar-anime';

var animeIndex = 0;	

function anime(){
	
	animeIndex ++;

	if (animeIndex >= calendar.length){
		animeIndex = 0;
	}
  
	adtChangeData(calendar[animeIndex]);
	$('.list-item[rel='+animeIndex+']', calendarID).addClass('active');
}

// Výměna dat
var adtChangeData = function(eventID){

	$('.list-date .active', calendarID).removeClass('active');

	var thumbURL = actionImageUrl[eventID];		
	var detailURL = actionTargetUrl[eventID];
	var title = actionTitle[eventID];
	var description = actionDescription[eventID];
	
	$('.thumb img', calendarID).attr('src', thumbURL);
	$('.title', calendarID).attr('href', detailURL).text(title);
	$('.description', calendarID).text(description);
}

// Nastavení časovače
 
var adtStartInterval = function(interval){
	if(calendar.length != 0){
		adtChangeInterval = setInterval("anime();",interval);
	}
}

var adtStopInterval = function(){
	clearInterval(adtChangeInterval);
}
 
function format_Of_Number (stat, num) {
  stat = stat.toLowerCase();
	nStr = num + '';
	var x = nStr.split('.');
	var x1 = x[0];
	var x2;
	if(stat=='cz')
	x2 = x.length > 1 ? ',' + x[1] : ''; // nahradime tecku carkou
	else if(stat=='us')
	x2 = x.length > 1 ? '.' + x[1] : ''; // us format
	
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		if(stat=='cz')
		x1 = x1.replace(rgx, '$1' + '.' + '$2'); // nahradime mezerou cesky format 1 000,00
		else if(stat=='us')
		x1 = x1.replace(rgx, '$1' + ',' + '$2'); // us format 1,000.00
	}
	return x1 + x2;
} 
 
function viewCashType(id, totalPrice, price){
	$('.notdisplay').hide();
	//$('#cash_type_1').hide();
	//$('#cash_type_2').hide();
	$('#cash_type_'+id).show();
	
	
	$('span.totalPay').html(format_Of_Number ('cz', Number(parseInt(totalPrice) + parseInt(price)))+",- Kč");

	//$('#totalPayId').html(format_Of_Number('cz',Number(parseInt(totalPrice)+parseInt(price)))+",- Kč");
}

$(function(){
	$('.list-date .list-item', calendarID).click(function(){
		var _this = $(this);
		adtStopInterval();
		var eventID = _this.attr('rel');
		adtChangeData(calendar[eventID]);
		_this.addClass('active');
		return false;
	});
	
});



/*	Rozpracovaná verze OOP
	- musí se vyřešít, pro fce anime není deklarována
var AnimateCalendar = function(calendarID, interval){
	if($(calendarID).length){

		var animeIndex = 0;	
	
		 function anime(){
			
			animeIndex ++;
	
			if (animeIndex >= calendar.length){
				animeIndex = 0;
			}
		  
			adtChangeData(calendar[animeIndex]);
			$('.list-item[rel='+animeIndex+']', calendarID).addClass('active');
		}
	
		// Výměna dat
		var adtChangeData = function(eventID){
	
			$('.list-date .active', calendarID).removeClass('active');
	
			var thumbURL = actionImageUrl[eventID];		
			var detailURL = actionTargetUrl[eventID];
			var title = actionTitle[eventID];
			var description = actionDescription[eventID];
			
			$('.thumb img', calendarID).attr('src', thumbURL);
			$('.title', calendarID).attr('href', detailURL).text(title);
			$('.description', calendarID).text(description);
		}
		
		// Nastavení časovače
		var adtStartInterval = function(interval){
			if(calendar.length != 0){
				adtChangeInterval = setInterval("AnimateCalendar.anime;",interval);
			}
		}
		
		var adtStopInterval = function(){
			clearInterval(adtChangeInterval);
		}
	
		$(function(){
			$('.list-date .list-item', calendarID).click(function(){
				var _this = $(this);
				adtStopInterval();
				var eventID = _this.attr('rel');
				adtChangeData(calendar[eventID]);
				_this.addClass('active');
				return false;
			});
		});
		//adtStartInterval(interval);
	}		
}
*/
