//chyba pri ajaxe
$(document).ajaxError(function (request,settings,e) {
    alert('Error requesting URL: '+e.url);
});

//funkcia na ziskanie URL routera
var Router = function (route,params) {
    if (typeof(params) == 'object') {
        var p = '';
        $.each(params,function (name,value) {
            if (p != '') {
                p += '&';
            }
            p += escape(name)+'='+escape(value);
        });
        return Router(route)+'?'+p;
    }
    else {
        return '/'+route;
    }
};
Router.route = function (route,params) {
    var url = Router(route,params);
    location.href = url;
}



$(function(){
   
/*
    $('#online_booking').submit(function () {
     $.ajax({
                    url: $('#online_booking').action,
                    data: $('#online_booking').serialize(),
                    success: function(data) {
                        $('#onlineBooking-bottom').html(data);
                        alert('Load was performed.');
                    } 
          });
     return false;
    }); 
*/

    /*
	$('#mainMenu > ul > li').each(function (idx,el){
        var hidden = $(this).attr('class').split(' ',2);
        if (hidden[1] != 'hidden'){
            $(el).hover(function(){
                $.classAtt = $(this).attr('class');
                $(this).removeClass();
                $(this).addClass($.classAtt.split(' ',1) + ' ' + $.classAtt.split(' ',1) + '-hover');
            },function(){
                $(this).removeClass();
                $(this).addClass($.classAtt);
            });
        }
	});
	*/
	$('#mainMenu li').hover(
	    function(){ $(this).addClass('hover'); }
       ,function(){ $(this).removeClass('hover'); }
	);

    makeSloganAnimation();

    bookingInit()
    
    $('#printMenu a.ask').click(function () {
        ask();
        return false;
    });
    
});
/* Plugin na input hint */
$.fn.inputHint = function () {
    
    this.each(function () {
        var self = $(this);
        if (self.is('input[type=text],textarea')) {
            $.inputHintShow(self);
            self.focus(function () {
                $.inputHintHide(this);
            }).blur(function () {
                $.inputHintShow(this);
            }).closest('form').submit(function () {
                $.inputHintHide(self);
                return true;
            });
        }
    });
    return this;
};
$.inputHintShow = function (inpt) {
    inpt = $(inpt);
    inpt.focus(function () {$.inputHintHide(inpt);});
    if (inpt.val() == inpt.attr('title') || inpt.val() == '') {
        inpt.val(inpt.attr('title'));
    }
};
$.inputHintHide = function (inpt) {
    inpt = $(inpt);
    inpt.unbind('focus');
    if (inpt.val() == inpt.attr('title')) {
        inpt.val('');
    }
};


function makeSloganAnimation() {
    var animationImages = $('#sloganAnimationImages').val();
    if (animationImages) {
        eval('animationImages = '+animationImages+';');
        var ul = $('<ul></ul>').attr('id','sloganAnimation');
        $.each(animationImages,function (idx,imgsrc) {
				ul.append('<li><img src="'+imgsrc+'" alt=""></img></li>');
        });
        $('#sloganAnimationImages').after(ul);
        ul.innerfade({
            animationtype: 'fade',
            speed: 'slow',
            timeout: 4000
        });
    }
}

function ask() {
    if (!$('div#ask').length) {
        var askf = $('<div>').attr('id','ask').hide();
        if ($('#printMenu a.ask').attr('id') == 'askHome'){
        	$('#contentContainer div.text h2').after(askf);
        	}
        else if($('#printMenu a.ask').attr('id') == 'askPage'){
        	$('#printMenu').after(askf);
        	}
        askf.load(LANGROOT + '/ask/',function () {
            askf.fadeIn();
            $('#askForm').submit(function () {
                //zmena value buttonu
                var v1 = $('#askFormSubmit').val();
                var v2 = $('#askFormSubmit').attr('rel');
                $('#askFormSubmit').attr('disabled','disabled').val(v2).attr('rel',v1);
                $('#askFormUrl').val(window.location.href);
                $.post(this.action,$(this).serialize(),function (response) {
                    
                    //response ako JSON
                    eval('var r = '+response+';');
                    
                    //odstranenie reportov
                    $('div.report',askf).remove();
                    
                    if (r.status == 'OK') {
                        askf.prepend('<div class="report report_INFO"><span class="ico ico_INFO">&nbsp;</span>'+r.message+'</div>');
                        $('#askFormSubmit').val(v1).attr('rel',v2);
                        window.setTimeout(function () {
                            askf.fadeOut(function () {
                                $(this).remove();
                            });
                        },1500);
                    } else {
                        askf.prepend('<div class="report report_ERROR"><span class="ico ico_ERROR">&nbsp;</span>'+r.message+'</div>');
                        $('#askFormSubmit').removeAttr('disabled').val(v1).attr('rel',v2);
                    }
                });
                return false;
            });
        });
    } else {
        $('#ask').fadeOut(function () {
            $(this).remove();
        });
    }
}


function bookingInit(){
    $('input,textarea','#online_booking').inputHint();
    
    $('#online_booking').submit(function () {
        $('#onlineBooking-top').append("<div id='ajaxloading'></div>");
        $('#ajaxloading').css({
            'height' : ($('#onlineBooking-bottom').height() - 40) + 'px'
           ,'top' : $('#onlineBooking-bottom').position().top + 'px'
           ,'left' : ($('#onlineBooking-bottom').position().left + 10) + 'px'
           ,'opacity' : 0.8
        });
        if($.browser.msie && $.browser.version == '6.0'){
            $('#online_booking').find('select,input').css('visibility','hidden');
        }
        
        $('input.submit').attr('disabled','disabled');
        
        $('#onlineBooking-bottom').load(this.action, $(this).serialize(), function(){ 
            $('#ajaxloading').remove();
           if($.browser.msie && $.browser.version == '6.0'){
                $('#online_booking').find('select,input').css('visibility','visible');
           }
        });
        return false;
    });
    
    $('a.bookingNav').click(function () {
        //$('#ajaxloading').css({'height' : $('#onlineBooking-bottom').height() , 'width' : '200px'}).show();
        $('#onlineBooking-bottom').load(this.href, function(){ 
            //$('#ajaxloading').hide();
        });
        return false;
    });
    
    if(LANGROOT == '/en'){	
        $.datepicker.setDefaults($.datepicker.regional['en']);
    }
    else if(LANGROOT == '/de'){	
        $.datepicker.setDefaults($.datepicker.regional['de']);
    }
    else {	
        $.datepicker.setDefaults({
            dayNamesMin: ['Ne', 'Po', 'Ut', 'St', 'Št', 'Pi', 'So'],
			firstDay: 1,
			monthNames: ['Január','Február','Marec','Apríl','Máj','Jún','Júl','August','September','Október','November','December']
		});
    }

    var maxdate = new Date();
    maxdate.setDate(maxdate.getDate()+168); //date + 6 * 4 weeks
    
    $("option","#yearLeave,#yearArrive").each(function () {
            if($(this).attr('value') > maxdate.getFullYear()){
                $(this).remove();
            }
    });
    
    $.datepicker.setDefaults({
            minDate: new Date(),
            showOn: 'button',
            buttonImage: WEBROOT + '/public/images/calendar3.gif',
			buttonImageOnly:true,
			buttonText: 'kalendár',
			dateFormat: 'mm/dd/yy',
			maxDate: maxdate
	});

    $("#dateArrive").datepicker({ 
            onSelect: function(dateText, inst) { 
			    var date = new Date(dateText);
			    $('#dayArrive').val(date.getDate());
			    $('#monthArrive').val(date.getMonth()+1);
			    $('#yearArrive').val(date.getFullYear());
			    var minLeaveDate = date;
			    minLeaveDate.setDate(minLeaveDate.getDate()+1); 
			    $("#dateLeave").datepicker("option" , 'minDate' , minLeaveDate );
			}

	});

    $("#dateLeave").datepicker({
           onSelect: function(dateText, inst) { 
			    var date = new Date(dateText); 			    
			    $('#dayLeave').val(date.getDate());
			    $('#monthLeave').val(date.getMonth()+1);
			    $('#yearLeave').val(date.getFullYear());
			    var maxArriveDate = date;
			    maxArriveDate.setDate(maxArriveDate.getDate()-1);
			    $("#dateArrive").datepicker("option" , 'maxDate' , maxArriveDate );	
			}
	});
	$("#yearLeave,#monthLeave").change(function () {
	    var d = $('#dayLeave').val(); 
	    var m = $('#monthLeave').val();  
	    var y = $('#yearLeave').val();  
	    if(d && m && y){
	        var dateLeave = new Date(y,m,d);
	        if(dateLeave > maxdate){
	            $('#dayLeave').val(maxdate.getDate()); 
	            $('#monthLeave').val(maxdate.getMonth()+1);  
	            $('#yearLeave').val(maxdate.getFullYear());  
	        }
        }
    });
    $("#yearArrive,#monthArrive").change(function () {
	    var d = $('#dayArrive').val(); 
	    var m = $('#monthArrive').val();  
	    var y = $('#yearArrive').val();  
	    if(d && m && y){
	        var dateArrive = new Date(y,m,d);
	        if(dateArrive > maxdate){
	            $('#dayArrive').val(maxdate.getDate()); 
	            $('#monthArrive').val(maxdate.getMonth()+1);  
	            $('#yearArrive').val(maxdate.getFullYear());  
	        }
        }
    });

	
	/*
	$('#bookedRoomsSwitch, #resInfoSwitch').click(function() {
        var obj = $(this).find('div:first');
        if(obj !== undefined){
           if (obj.is(":hidden")){
               obj.slideDown('slow');
           } else {
               obj.slideUp('slow');
           }
        }
    });*/
    
    $('#bookedRoomsSwitch').css('cursor','pointer').click(function() {
       if ($('#bookedRooms').is(":hidden")){
           $('#bookedRooms').slideDown('slow');
       } else {
           $('#bookedRooms').slideUp('slow');
       }
       var text = $(this).text();
       $(this).text($(this).closest('span').find('span.hidden').text());
       $(this).closest('span').find('span.hidden').text(text);
    });
    
    $('#resInfoSwitch').css('cursor','pointer').click(function() {
       if ($('#resInfo').is(":hidden")){
           $('#resInfo').slideDown('slow');
       } else {
           $('#resInfo').slideUp('slow');
       }
       var text = $(this).text();
       $(this).text($(this).closest('span').find('span.hidden').text());
       $(this).closest('span').find('span.hidden').text(text);
    });
    
    $('#contactInfoSwitch').css('cursor','pointer').click(function() {
       if ($('#contactInfo').is(":hidden")){
           $('#contactInfo').slideDown('slow');
       } else {
           $('#contactInfo').slideUp('slow');
       }
       var text = $(this).text();
       $(this).text($(this).closest('span').find('span.hidden').text());
       $(this).closest('span').find('span.hidden').text(text);
    });
    
    //$('#bookedRooms').hide();
    //$('#resInfo').hide();
    $('#contactInfo').hide();
    $('#contactInfoSwitch').html('zobraziť');

    
    $('.msg-dialog').dialog({
        autoOpen: false,
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog('close');
            }
        }
    });
    
}




