$(document).ready(function() {

	jQTubeUtil.init({
		key : "AIzaSyCOwSRJODgNzA5SVc6Xqkf-a9mCNHpL9h4",
		orderby : "viewCount", // *optional -- "viewCount" is set by default
		time : "this_month", // *optional -- "this_month" is set by default
		maxResults : 10 // *optional -- defined as 10 results by default
	});

	jQTubeUtil.search($('[name=nike]').val(), function(response) {
		var html = "";
		for (v in response.videos) {
			var video = response.videos[v];
			// this is a 'friendly' YouTubeVideo object
			html += (parseInt(v) + 1) + " : " + video.title + " <br/>";
		}


		$("#basic_search_response_itemsPerPage").html(response.itemsPerPage);
		$("#basic_search_response_searchURL").html(response.searchURL);
		$("#basic_search_response_startIndex").html(response.startIndex);
		$("#basic_search_response_totalResults").html(response.totalResults);
		$("#basic_search_response_version").html(response.version);
		$("#basic_search_response").html(html);
	});



	$('#formContatto').submit(function() {
		var data = "action=invioMessaggio&" + $(this).serialize();
		$.ajax({
			type : "POST",
			url : '../../../ajax/function.php',
			data : data,
			dataType : "json",
			success : function(response) {

				if (response.failure) {
					$("#formresponse").text(response.failure);
					$("#formresponse").css('color', 'red');
				}

				if (response.success) {
					$("#formresponse").text(response.success);
					$("#formresponse").css('color', 'green');
					$("#taccanome").val('');
					$("#nome").val('');
					$("#cognome").val('');
					$("#messaggio").val('');
				}

				$("#taccanome").css('border', '1px solid #ccc');
				$("#nome").css('border', '1px solid #ccc');
				$("#cognome").css('border', '1px solid #ccc');
				$("#email").css('border', '1px solid #ccc');
				$("#messaggio").css('border', '1px solid #ccc');

				if (response.err) {
					$.each(response.err, function(key, value) {
						$("#" + key).css('border', '1px solid red');
					});
				}

			}
		});

		return false;
	});

	setInterval(function() {
		heightSidebar = $('.dealSidebar').height();
		heightContainer = $('.dealContainer').height();

		if (heightSidebar > heightContainer) {
			$('.main-container').height(heightSidebar - 20);
			$('.main-container').addClass("clearfix");
		}
	}, 1000);

	var latitudine = $('.latitude span').attr("title");
	var longitude = $('.longitude span').attr("title");
    
    /*
    if ($('#mappaEstero').length)
	{
			var indirizzo_estero= $('#mappaEstero').attr("data-address");
    
				map2 = new GMaps({
					div : '#mappaEstero',
					lat : latitudine,
					lng : longitude,
					scrollwheel : true
				}); 
			
			GMaps.geocode({
			  address: indirizzo_estero,
			  callback: function(results, status){
					console.log(status);
				if(status=='OK'){
				  var latlng = results[0].geometry.location;
				  map2.setCenter(latlng.lat(), latlng.lng());
				  map2.addMarker({
					lat: latlng.lat(),
					lng: latlng.lng()
				  });
				}
			  }
			});
	}else{
			map = new GMaps({
					div : "#map",
					lat : latitudine,
					lng : longitude,
					scrollwheel : true
				}); 
				
			var marker = map.addMarker({
						lat : latitudine,
						lng : longitude,
						title : 'Loop, Inc.',
						icon : "https://www.sihappy.it/img/map-pointer-icon.png"
					});
	}
    */
	$('#boxMappaLink').click(function() {
		setTimeout(function() {
			$('#mappa_big').height(($(window).height() - 180));

			var latitudine = $('.latitude span').attr("title");
			var longitude = $('.longitude span').attr("title");

			map = new GMaps({
				div : '#mappa_big',
				lat : latitudine,
				lng : longitude,
				scrollwheel : true
			});

			var marker = map.addMarker({
				lat : latitudine,
				lng : longitude,
				title : 'Loop, Inc.',
			icon : "https://www.sihappy.it/img/map-pointer-icon.png"
			});

		}, 500);
 
	});

}); 