// JavaScript Document


/* DATE PICKER */
$(function(){
	$('.datepicker .thedate').datepicker({dateFormat: 'dd-mm-yy'});
})

/* MAP */
$(document).ready(function(){
	$('.googlemap').each(function(){
		var address = $(this).text();
		$(this).replaceWith('<iframe width="100%" height="360" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.it/maps?q='+address+'&amp;oe=utf-8&amp;client=firefox-a&amp;ie=UTF8&amp;hq=&amp;hnear='+address+'&amp;gl=it&amp;ei=RTq3TPDOB8LIswbggN2QCQ&amp;ved=0CBsQ8gEwAA&amp;t=h&amp;z=16&amp;output=embed"></iframe><br /><small><a href="http://maps.google.it/maps?q='+address+'&amp;oe=utf-8&amp;client=firefox-a&amp;ie=UTF8&amp;hq=&amp;hnear='+address+'&amp;gl=it&amp;ei=RTq3TPDOB8LIswbggN2QCQ&amp;ved=0CBsQ8gEwAA&amp;t=h&amp;z=16&amp;source=embed" target="_blank">Visualizza la mappa su google maps</a></small>');
	});
	
	$(function() {
		$('.lightbox a').lightBox();
		$('a.lgb').lightBox();
	});	
	
	setTimeout(function(){
		$('#strillo').fadeIn(1500, function(){
										$('#strillo .box').fadeIn();
									});
	}, 1000);
	
	$('#strillo .close').click(function(){
		$('#strillo').fadeOut();
		var id = 'strillo';
		var params = {id: id, 'getExtra': true, 'getRank': true, 'getGroups': true}; 
		ajaxCall('', params, 'close_strillo', '', 'strillo', 'return_true');
		return true;
	})
	
	$('#submenu li').each(function(){
		$(this).hide()
		var index = $(this).index('#submenu li');
		var to = setTimeout(function(){
								$('#submenu li:eq('+index+')').stop().fadeIn();
							}, 500+(300*index))
	})
	
	$('#container,#footer,#header').pngFix();

});

var admin = false;
$(document).ready(function(){
	$('.tpb_date').click(function(){
		if(!$(this).hasClass('inactive')){
			var val = $(this).attr('id');
			$('input.thedate').val(val);
			resetTime()
			updateFullDate();
			$(this).addClass('selected');
		}
	})
	$('.thedate.tpb').change(function(){
		//alert('thedate');
		var val = $(this).val();
		resetTime()
		updateFullDate();
	})
	$('.tpb_time').click(function(){
		var time = $(this).attr('id');
		var time = time.replace('time_', '');
		var hs = time.split(":");
		var h = hs[0];
		var qtr = hs[1];
		var prenotati = $(this).attr('pax');
		var pax = $('#pax .input').val();
		if((parseInt(prenotati)+parseInt(pax)<21)||(admin === true)){
			$('.thetime').val(time);
			$('.time,.tpb_time').removeClass('selected');
			$('#h_'+h).addClass('selected');
			$('#i_'+qtr).addClass('selected');
			$(this).addClass('selected');
		}
	})
	$('#pax .input').change(function(){
		var prenotati = $('.tpb_time.selected').attr('pax');
		var pax = $(this).val();
		resetTime()
		updateFullDate();
	})
})


function updateFullDate(){
	var time = $('.thedate').val().split('-');
	var Y = time[2];
	var m = time[1];
	var d = time[0];
	var date = Y+""+m+""+d;
	var obj = reservation_status[0][date];
	var size = Object.size(obj);
	var pax = $('#pax .input').val();
	$('.tpb_time').attr('pax', 0);
	for(var propertyName in obj) {
		var prenotati = obj[propertyName]['pax'];
		$('.time_'+propertyName.replace(':', '')).attr('pax', prenotati);
		if(parseInt(pax)+parseInt(prenotati)>20)
			$('.time_'+propertyName.replace(':', '')).addClass('inactive');
	}
}


function resetTime(){
	$('.tpb_date').removeClass('selected');
	$('.tpb_time').removeClass('inactive');
	$('.tpb_time').attr('pax', 0);
	$('.tpb_time, .time').removeClass('selected');
	$('.thetime').val('');
}

function existsArray(a, arr){
	for(var i = 0; i < arr.length; i++){
		if(arr[i]==a){
			return true;
		}
	}return false;
}

Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};

