function ucfirst (str) {
    // Makes a string's first character uppercase  
    // 
    // version: 1009.2513
    // discuss at: http://phpjs.org/functions/ucfirst    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: ucfirst('kevin van zonneveld');
    // *     returns 1: 'Kevin van zonneveld'    str += '';
    var f = str.charAt(0).toUpperCase();
    return f + str.substr(1);
}
$(document).ready(function() {
	
	/*LOAD ricerca*/
	$('select#hotel').change(function() {
		$value = $(this).val();
		if($value > 0){
			$.getJSON($base+'/loadOfferte.php',{'id':$value}, function(j) {
				var options = '<option value="">seleziona</option>';
				for (var i = 0; i < j.length; i++) {
					options += '<option value="' + j[i].id_vacanze + '">' + ucfirst(j[i].nome) + '</option>';
				}
				$("select#location_master").html(options);
				$("select#location").html("");
				$("select#location_master").attr("disabled",false);
					
			});
		}
		return false;
	});
	
	$('select#location_master').change(function() {
		$value = $(this).val();
		if($value > 0){
			$.getJSON($base+'/loadPrezzoOfferta.php',{'id':$value}, function(j) {
				var options = '';
				for (var i = 0; i < j.length; i++) {
					options += '<option value="' + j[i] + '">' + ucfirst(j[i]) + '</option>';
				}
				$("select#location").html(options);
				$("select#location").attr("disabled",false);
					
			});
		}
		return false;
	});
	
	$('select#r-regione').change(function() {
		$value = $(this).val();
		if($value > 0){
			$.getJSON($base+'/loadCerca.php',{'type':'provincia','idregione':$value}, function(j) {
				var options = '';
					options += '<option value="0">zona</option>';
				
				for (var i = 0; i < j.length; i++) {
					options += '<option value="' + j[i].id_localita + '">' + ucfirst(j[i].localita.toLowerCase()) + '</option>';
				}
				$("select#r-comune").html(options);
				$("select#r-comune").attr("disabled",false);
					
			});
		}
		return false;
	});
	
	$('select#r-comune').change(function() {
		$value = $(this).val();
		if($value > 0){
			$.getJSON($base+'/loadCerca.php',{'type':'localita','idcomune':$value}, function(j) {
				var options = '';
									options += '<option value="0">localit&agrave;</option>';

				for (var i = 0; i < j.length; i++) {
					options += '<option value="' + j[i].id_localita + '">' + ucfirst(j[i].localita.toLowerCase()) + '</option>';
				}
				$("select#r-localita").html(options);
				$("select#r-localita").attr("disabled",false);
					
			});
		}
		return false;
	});

	
	
	/*END LOAD ricerca*/
	
	$(".form-ricerca input").each(function(){
		$(this).defaultValue($(this).attr("rel"));
	});
	
	$(".form-newsletter input").each(function(){
		$(this).defaultValue($(this).attr("rel"));
	});
	
	$(".form-generico").each(function(i) { 
		$(this).validate();
	});
	
	$(".form-newsletter").each(function(i) { 
		$(this).validate();
	});

	$('a[rel=external]').click(function() {
		window.open($(this).attr('href'));
		return false;
	});

	if ( $('#map').length > 0 ) {
		showmaps();
	}
	
	if ( $('#slider-principale ul li a').length > 0 ) {
		$('#slider-principale ul li a').css('opacity',0.9);
	}
	
	$('.ui-tabs').tabs({fx: {opacity: 'toggle'}})
	$('#box2').cycle();
	$("a[rel='gallery']").colorbox({});
	$("a[rel='video']").colorbox({iframe:true, width:650, height:550, current: '',rel:'nofollow'});
	$("a[rel='map']").colorbox({iframe:true, width:650, height:450});
	$("a[rel='gallery_2']").colorbox({});

	dropdown('#menu_principale ul');
	
	Cufon.replace('.cufon');
	
	if($("#slider-principale ul li").length){
		$("#slider-principale ul").carouFredSel({
			items: {
				visible: 1
			},
			circular: true,
			auto: 8000,
			//auto: true,
			prev: '#slide .prev',
			next: '#slide .next'
		});
	}
	
	if($("#slider-catalogs ul li").length){
		$("#slider-catalogs ul").carouFredSel({
			items: {
				visible: 2
			},
			circular: true,
			auto: true,
			prev: '#slider-catalogs .prev',
			next: '#slider-catalogs .next'
		});
	}
	
	
	
});

function loadSliderOffer(pagin){
	if($("#slider-front").length){
		$("#slider-front").carouFredSel({
			items: {
				visible: 1
			},
			circular: true,
			auto: 8000,
			pagination: {
				container: $("#central-slider ul"),
				anchorBuilder: function (nr){
					return '<li class="'+((nr == 1)?"hover":"")+'"><a href="#"'+nr+'"><span>'+pagin[nr-1]+'</span></a></li>';	
				}
			},
			scroll: {
				onBefore: function( oldItems, newItems, newSizes ) {
				
					$elem = $('#central-slider li.hover');
					$elem.removeClass("hover");
					if($elem.next().length){
						$elem.next().addClass("hover");
					}else{
						$('#central-slider li:first').addClass("hover");	
					}
				}
			}
		});
	}	
}

