//store locator google maps / jquery javascript file

$(function() {
	/* google maps goodness*/
    var map = new GMap2(document.getElementById('map'), { width: '449px', height: '394px' });
    var streetMap = new GStreetviewPanorama(document.getElementById('street-map'), { width: '449px', height: '394px' });
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	map.checkResize();
	map.setCenter(new GLatLng(40.725181,-73.992093),15);
    
	/*store locater controls*/
	$('ul#store-list a').click(function() {
		if( !$('#right-nav').is(':visible') ) {
			var navWidth = $('#right-nav').css('width');
			$('#right-nav').show().css('width',0);
			$('#right-nav').animate({width:navWidth}, 500);
		}
		$('ul#store-list a').removeClass('active');
		$(this).addClass('active');
		var id = $(this).attr('href');
		$('.store-info, #store-locator-printout').hide();
		$(id).fadeIn();
		$('#store-locator #map, #store-locator #street-map, .slideshow').css('z-index','10');
		$('.slideshow').cycle('stop');
		$(id+'_slideshow').css('z-index','30').cycle({timeout:3000});
		//set google map stuff before map is shown to avoid gray map loading
		var lat = $(id+'_lat').val(); var long = $(id+'_long').val();
		lat = parseFloat(lat); long = parseFloat(long);
	    var point = new GLatLng(lat,long);
		myPOV = {yaw:370.64659986187695,pitch:-20};//?????... might have to set for each store
		streetMap.setLocationAndPOV(point);
		map.clearOverlays();
		map.addOverlay(new GMarker(point));
    	map.checkResize();
    	map.setCenter(point, 15);
		
		//hide street-view for hard rock & costa mesa
		if( id.indexOf('3') != -1 || id.indexOf('8') != -1 ) {
			$(id).find('.show-street').hide();
		} else {
			$(id).find('.show-street').show();
		}
				
		return false;
	});
	
	$('a.show-map').click(function() {
		$('#store-locator #street-map, .slideshow').css('z-index','10');
		$('#store-locator #map').css('z-index','30');
    	map.checkResize();
		return false;
	});
	
	$('a.show-street').click(function() {
		$('#store-locator #map, .slideshow').css('z-index','10');
		$('#store-locator #street-map').css('z-index','30');
		return false;
	});	
	
	$('#address-input').click(function() {
		if( $(this).val() == 'Enter Address or Zip Code' ) {
			$(this).val('');
		}
	});
	
	$('#store-search').submit(function() {
		var countryVal = $('#country-select').val();
		if( countryVal == '0' ) { 
			alert('Please choose a country');
			return false; 
		}
		var address = $('#address-input').val();
		address += ', ' + countryVal;
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(address,function(point) {
 		    if (!point) {
 		        alert(address + " not found");
 		    } else {
 			    var latVal = point.lat();
 			    var lngVal = point.lng();
				if( !$('#right-nav').is(':visible') ) {
					var navWidth = $('#right-nav').css('width');
					$('#right-nav').show().css('width',0);
					$('#right-nav').animate({width:navWidth}, 500);
				}
				$('#store-locator #street-map, .slideshow').css('z-index','10');
				$('#store-locator #map').css('z-index','30');
 			    $('.store-info').hide();
 			    $('#store-locator-printout').remove();
 			    $.get('/wp-content/themes/jv/store-locator-script.php', { lat: latVal, lng: lngVal, country: countryVal }, 
 			    	function(data) {
 			    		dataSplit = data.split('::endjs::');
 			    		eval(dataSplit[0]);
 			    		$('#right-nav').append(dataSplit[1]); 
 			    	});
				map.checkResize();
 			}
 		});
 		return false;
	});
	
	$('a.map-pan').livequery('click',function() {
		var coor = $(this).attr('rel');
		coor = coor.split(',');
		var lat = coor[0];
		var lng = coor[1];
		map.clearOverlays(); 
		var point = new GLatLng(lat,lng);
		map.addOverlay(new GMarker(point));
		map.panTo(point);
		return false;
	});

	/******DELETE THIS AFTER GEOCODES ARE GOTTEN**********/
		$('#address-submit-temp').click(function() {
		var address = $('#address-input-temp').val();
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(address,function(point) {
 		    if (!point) {
 		        alert(address + " not found");
 		    } else {
 			    var lat = point.lat();
 			    var lng = point.lng();
 			    $('#data-temp').empty().html('Latitude: ' + lat + '<br />Longitude: ' + lng);
 		    }
 			});
		});
	/**************DELETE********************************/
        
    //hopefully putting this at the bottom stops the load order issue
    //$('.slideshow').cycle({ timeout: 3000 });
});