/***** SEZNAM FUNKCIONALIT *****
#1 - Vymazání textu v inputu po kliknutí
#2 - Zpřístupnění tlačítka po odsouhlasení podmínek
#3 - Modální okna
#4 - Externi odkazy
#5 - Tisk stránek
#6 - 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", vyrobí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 - Modální okna
//  pro IE < 8 presuneme obsah modalnich oken nakonec HTML (problem se zatekanim)
$.each($.browser, function(i, val) {
	if(i=="msie" && jQuery.browser.version.substr(0,3)<"8")
		$('#page-wrapper').after($('.modal-window'));
	});

	//Inicializace jednotlivych oken
	$('#dialog-01').jqm();
*/


// #4 - Externi odkazy - cílovou stránku otevře v novém okně/panelu
$.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í okdaz)</span>')     // indikator externiho odkazu pro handikepované uživatele
	.click(function(){
		window.open(this.href);
		return false;
	});

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


// #6 - 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 a', calendarID).attr('href', detailURL);
	$('.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(){
	$('.list-date .list-item', calendarID).click(function(){
		var _this = $(this);
		adtStopInterval();
		var eventID = _this.attr('rel');
		adtChangeData(calendar[eventID]);
		_this.addClass('active');
		return false;
	});
});
