var gmap, geocoder, infoWindow;
var marker = null;

function gmap_init() {
	var latlng = new google.maps.LatLng(51.776378, 19.487836);
	var myOptions = {
		zoom: 2,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControl: false,
		scrollwheel: false
	};
	gmap = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	infoWindow = new google.maps.InfoWindow();

	if($('.adding-lipdub').length) {
		geocoder = new google.maps.Geocoder();
		google.maps.event.addListener(gmap,'click',function(event)
		{
			if(event.latLng)
			{
				if(marker == null) {
					marker = gmap_marker_add(event.latLng, {title:'LipDub localization', draggable: true});
				} else {
					marker.setPosition(event.latLng);
				}
				geocoder.geocode({latLng: event.latLng, language: 'en'}, function(results, status) {
					if (status == google.maps.GeocoderStatus.OK) {
						if (results[0]) {
							for(var i=0; i<results[0].address_components.length; i++) {
								for(var j=0; j<results[0].address_components[i].types.length; j++) {
									if(results[0].address_components[i].types[j]=='country') {
										$('#short_name').val(results[0].address_components[i].short_name);
										$('#long_name').val(results[0].address_components[i].long_name);
									}
								}
							}
							$('#address').val(results[0].formatted_address).change();
						}
					}
				});
				$('#latlng').val(event.latLng.toUrlValue());
			}
		});
	}
}
function gmap_marker_add(latlng,myOptions) {
	myOptions.position = latlng;
	myOptions.map = gmap;
	var marker = new google.maps.Marker(myOptions);
	if(!$('.adding-lipdub').length) {
		google.maps.event.addListener(marker,"click",function()
		{
			infoWindow.setContent(gmap_info_content(myOptions));
			infoWindow.open(gmap,marker);
		});
	}
	return marker;
}
function gmap_info_content(myOptions) {
	return '<div class="list"><a href="'+myOptions.permalink+'" class="img"><img src="'+myOptions.thumb+'" width="60" height="40" alt="" /></a><div class="inf"><a href="'+myOptions.permalink+'"><strong>'+myOptions.title+'</strong><em class="views">views: '+myOptions.views+' | likes: '+myOptions.likes+'</em></a></div></div>';
	//return '<a href="'+myOptions.permalink+'"><strong>'+myOptions.title+'</strong></a>';
}

function gmap_geocoder(address) {
	if (geocoder) {
		geocoder.geocode( { 'address': address}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				gmap.setCenter(results[0].geometry.location);
				gmap.setZoom(16);
				google.maps.event.trigger(gmap,'click', {latLng: results[0].geometry.location});
			} else {
				alert("Geocode was not successful for the following reason: " + status);
			}
		});
	}
}

$(document).ready(function(){
	if($('.description').height()>210) {
		$('.description').css('height', '210px').css('overflow', 'hidden').css('cursor', 'pointer')
		.mouseenter(function(){ $(this).css('background', '#ddd'); }).mouseleave(function(){ $(this).css('background', 'none'); })
		.attr('title', 'Click to expand description')
		.click(function(){$(this).css('height', 'auto').css('overflow', 'visible')});
	}
	$(".scroll").jScrollPane({dragMaxHeight: 30,dragMinHeight:30});

	if($('#map_canvas').length) {
		gmap_init();
	}
	
	if($('.adding-lipdub').length) {

		var form_values = {
			name: 'Your name or nickname',
			email: 'Your email. We don\'t show it anywhere',
			title: 'Lipdub title',
			description: 'Some description about this video',
			url: 'Paste here URL to your lipdub video (we accept youtube, vimeo, dailymotion and others)',
			address: 'Sydney, NSW'
		}

		$('#name, #email, #title, #description, #url, #address').each(function(){
			var that = $(this);
			var id = that.attr('id');
			if(that.val()==form_values[id]) {
				that.css('color', '#bbb');
				//$(this).focus(function(){}).blur(function(){});
			}
			that.focus(function(){ if(that.val()==form_values[id]) that.val(''); that.css('color', '#000'); })
				.blur(function(){ if(that.val()=='') {that.val(form_values[id]); that.css('color', '#bbb');} })
				.change(function(){ if(that.val()!=form_values[id]) that.css('color', '#000'); });

		});

		if($('#latlng').val().length>0) {
			var latlng = $('#latlng').val().split(',');
			google.maps.event.trigger(gmap,'click', {latLng: new google.maps.LatLng(latlng[0], latlng[1])});
		}

		$("#address").bind("keypress", function(e) {
			if(e.keyCode == 13) {
				gmap_geocoder($('#address').val());
				return false;
			}
		});
		$('#geocode').click(function(e){
			gmap_geocoder($('#address').val());
			return false;
		});

		$('form a.add-lipdub').click(function(){
			$('#name, #email, #title, #description, #url, #address').each(function(){
				if($(this).val()==form_values[$(this).attr('id')]) {
					$(this).val('');
				}
			});
			$(this).parents('form').submit();
			return false;
		});
	}
});

var addthis_config = {
   ui_offset_top: 19
};
