function geoSet(){
	geo = $.cookie('geolocation');
        if (geo == null) {
		$.cookie('geolocation', geoplugin_region(), { expires: 14 });
	}
}

function mapState(url, state, type){
	$.post(url, { state: state, type: type },
		function(data){
			if(data == null){return false;}
			var obj = jQuery.parseJSON(data);
			$('#stores').empty();
                        $(obj).each(function(key, value){
				var email = new String(value.email);
				var phone = new String(value.phone1);
				var street = new String(value.street);
				var name = new String(value.contact_name);
				var coord = "[{'latitude':'"+value.latitude+"','longitude':'"+value.longitude.replace("\n",'')+"','company_name':'"+value.company_name+"','phone1':'"+value.phone1+"','street':'"+value.street+"','city':'"+value.city+"'}]";
				$('#stores').append('<a href="#" onclick="mapSet('+coord+');detailStore($(this));return false;" class="stores" style="text-decoration:none;">                                                              <label style="cursor:pointer;color:BLACK;font-weight:bold;font-size:14px;float:left;">'+value.company_name+'</label>                                            	          <label style="cursor:pointer;color:BLACK;float:left;margin-top:10px;">Telefone: '+((phone != 'null') ? phone : "")+'</label>                                                                    <label style="cursor:pointer;color:BLACK;float:left;margin-top:2px;">E-mail: '+((email != 'null') ? email : "")+'</label>                		                                            <label style="cursor:pointer;color:BLACK;float:left;margin-top:2px;">Endere&ccedil;o: '+((street != 'null') ? street : "")+'</label>                                		                 <label style="cursor:pointer;color:BLACK;float:left;margin-top:10px;margin-bottom:20px;">Respons&aacute;vel: '+((name != 'null') ? name : "")+'</label></a>');
			});
			mapSet(obj);
			});
}

function cityGet(url, state, type){
        $.post(url, { state: state, type: type },
                function(data){
                        if(data == null){return false;}
                        var obj = jQuery.parseJSON(data);
			$('#city > option').remove();
                        $('#city').append($("<option></option>")
                                  .text('Selecione...'));			
			$(obj).each(function(key, value){
	                        $('#city').append($("<option></option>")
					  .attr('value', value.city)
                        		  .text(value.city.toLowerCase()));

			});
                });
}

function mapSet(coord){

	if(coord == null){
		$('#map').html('<div style="background-color: #CCC; margin-top:100px;">N&atilde;o foram encontrados dados.</div>');
	}
	else{
            //------- Google Maps ---------//
            
	    // Creating a LatLng object containing the coordinate for the center of the map
            var latlng = new google.maps.LatLng(coord[0].latitude, coord[0].longitude);
            var bounds = new google.maps.LatLngBounds();
            // Creating an object literal containing the properties we want to pass to the map
            var options = {
            //    zoom: 8, // This number can be set to define the initial zoom level of the map
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP // This value can be set to define the map type ROADMAP/SATELLITE/HYBRID/TERRAIN
            };
            // Calling the constructor, thereby initializing the map
            var map = new google.maps.Map(document.getElementById('map'), options);

            for (var i = 0; i < coord.length; i++) {
            // Add Marker
                var marker1 = new google.maps.Marker({
                        position: new google.maps.LatLng(coord[i].latitude, coord[i].longitude),
                        map: map
        //              icon: image // This path is the custom pin to be shown. Remove this line and the proceeding comma to use default pin
                });

                var infoWindow = null;
                marker1.html = '<div class="infowindow"><strong>'+coord[i].company_name+'</strong><br>Telefone: '+coord[i].phone1+'<br> Endere&ccedil;o: '+coord[i].street+', '+coord[i].city+'</div>';

                var infoWindow = new google.maps.InfoWindow({content: ''});
                    // Add listener for a click on the pin
                google.maps.event.addListener(marker1, 'click', function() {
                        infoWindow.setContent(this.html);
                        infoWindow.open(map, this);
                });
                bounds.extend(marker1.position);
            }
        map.fitBounds(bounds);
	}
}

function detailStore(obj){
	$('.stores').css('display','none');
	$(obj).css('display','block');
	$('#back').css('display','block');
}
function backStore(url, type){
	$('#back').css('display','none');
	mapState(url,$('#city option:selected').val(),type);
}

function changeImage(id, url){
	$('.image_product').attr('src', url+'produtos/'+id+'.jpg');
	$(".jqzoom").attr('href',  url+'produtos/'+id+'.jpg');
	$(".zoomWrapperImage img").attr('src', url+'produtos/'+id+'.jpg');
}

