$(document).ready(function(){
	//$('#navegacion .mapa').css({height:"380px",width:"450px",border:"3px solid blue"});
	//$('#navegacion').css({height:"400px",width:"950px",display:"none"});
	
	$.ffParams = {
		init : {
				height:"0px",
				border:"0px solid #e4e6e7",
				margin:"0px"
			},
		end : {
				height: "500px",
				border:"10px solid #e4e6e7",
				marginBottom:"50px",
				marginTop:"10px"
		}			
	};
	
	$.ieParams = {
		init : {
				height:"0px"
			},
		end : {
				height: "500px"
		}
	};
	
	$.unicid = function(){
		$.unicid.prefix = "jQ-";
		!$.unicid.count?$.unicid.count=1:$.unicid.count++;
		return $.unicid.prefix+$.unicid.count;
	}
	
	$.urldecode = function( str ){
	    // http://kevin.vanzonneveld.net
	    // +   original by: Philip Peterson
	    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	    // +      input by: AJ
	    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	    // +   improved by: Brett Zamir
	    // %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
	    // *     example 1: urldecode('Kevin+van+Zonneveld%21');
	    // *     returns 1: 'Kevin van Zonneveld!'
	    // *     example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
	    // *     returns 2: 'http://kevin.vanzonneveld.net/'
	    // *     example 3: urldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a');
	    // *     returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a'
	    
	    var histogram = {};
	    var ret = str.toString();
	    
	    var replacer = function(search, replace, str) {
	        var tmp_arr = [];
	        tmp_arr = str.split(search);
	        return tmp_arr.join(replace);
	    };
	    
	    // The histogram is identical to the one in urlencode.
	    histogram["'"]   = '%27';
	    histogram['(']   = '%28';
	    histogram[')']   = '%29';
	    histogram['*']   = '%2A';
	    histogram['~']   = '%7E';
	    histogram['!']   = '%21';
	    histogram['%20'] = '+';
	 
	    for (replace in histogram) {
	        search = histogram[replace]; // Switch order when decoding
	        ret = replacer(search, replace, ret) // Custom replace. No regexing   
	    }
	    
	    // End with decodeURIComponent, which most resembles PHP's encoding functions
	    ret = decodeURIComponent(ret);
	 
	    return ret;
	}
	
	geocoder = new GClientGeocoder();


	$("#listado tr td a[href^='http://maps.google']").each(function(){
		$(this).bind('click',function(){
			/*
			$('#navegacion').slideDown('slow');
			*/
			/* TODO */
			/**
			 * Si existe capa no crear de nuevo, poner boton de cerrar
			 */
			
			var params = {};
			var dls = $(this).attr('href').split('?')[1];
			if (dls != undefined) {
				var dlss = dls.split('&');
				$(dlss).each(function(i, el){
					var key = el.split('=')[0];
					var val = el.split('=')[1];
					params[key] = val!=undefined?val:"";
				})
			}
			anc = $(this);
			
			//;
			
			if ($('.mapa').length > 0) {
				var dflt = {};
				dflt = $.browser.msie?$.ieParams["init"]:$.ffParams["init"];
				var nm = $('.mapa').parent().attr('name');
				$('.mapa').animate(dflt,700,"easeInOutQuint",function(){
					//$.browser.msie?$(this).css($.ffParams["init"]):false;
					$(this).parent().parent().remove();
					$(this).parent().parent().parent().find('a[name='+nm+']').removeAttr('name');
				})
				if( anc.attr('name') == nm )return false
			}
			
			var id = $.unicid();
			$(this).attr({
				name: id
			});
			$(this).parent().parent().after('<tr><td colspan="9" name="' + id + '"><div class="mapa"></div></td></tr>');
			var n = $(this).parent().parent().parent().find('td[name=' + id + '] .mapa');
		
			//console.log($.browser.msie?$.ieParams["end"]:$.ffParams["end"])
			var dflt = {};
			dflt = $.browser.msie?$.ieParams["end"]:$.ffParams["end"]
			n.animate(dflt, 900,"easeInOutQuint", function(){
				
			$.browser.msie?$(this).css($.ffParams["end"]):false;
				
				if (GBrowserIsCompatible()) {
					var tdmrkIcon = new GIcon(G_DEFAULT_ICON);
					tdmrkIcon.image = "http://server.xtranet.es:8000/masvision/media/jpeg/tiendas/marker.png";
					tdmrkIcon.iconSize = new GSize(24, 38);
					
					markerOptions = {
						icon: tdmrkIcon
					};
					
					var map = new GMap2(n[0]);
					
					map.addControl(new GMapTypeControl());
					map.addControl(new GLargeMapControl());
					
					//map.setCenter(new GLatLng(37.4419, -122.1419), 13);
					var address = $.urldecode(params["q"]);
					if (geocoder) {
						geocoder.getLatLng(address, function(point){
							if (!point) {
							//alert('doh!')
							}
							else {
								map.setCenter(point, 17);
								var marker = new GMarker(point, markerOptions);
								map.addOverlay(marker);
								marker.openInfoWindowHtml(address);
							}
						})
					}
				}
				
				$(this).css({position:"relative"}).append('<div class="close"><a class="Lucida12-666" href="javascript:void(0);">cerrar</a></div>').find('.close').css({
					position:"absolute",
					top:"0px",
					right:"-70px"
				}).bind('click',function(){
					anc.trigger('click');
					return false;
				})
				
				$('html,body').animate({scrollTop:$(this).offset()["top"]-50},'slow');
			});
			return false;
		})
	});
})
