$.extend({
	evIE: function(command){
		if(!$.browser.msie){return command;}
		switch(command){
			case 'slideToggle':
				return 'toggle';
			case 'slideUp':
				return 'hide';
			case 'slideDown':
				return 'show';
		}
	}
})


$(document).ready(function(){
	C.init();
	
	$("#myForm2").validationEngine();

	var options = { 
		success:       showResponse,  // post-submit callback 
		// other available options: 
		url:       '/ajax_pricelist.php',        // override for form's 'action' attribute 
		clearForm: true,        // clear all form fields after successful submit 
		resetForm: true        // reset the form after successful submit 
    }; 
 
    // bind to the form's submit event 
    $("#myForm2").bind("submit", function(caller){
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit
		if ($("#myForm2").validationEngine({returnIsValid:true})) {
        	$("#myForm2").ajaxSubmit(options);
		}
 
        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false; 
    });
	
	$("p.price a").bind("click", function(caller){
        $("#hide1").toggle();
        return false; 
    });
	$("a.close").bind("click", function(caller){
        $("#hide1").toggle();
        return false; 
    });

});

function showResponse()
{
	$('#priceForm').hide();
	$('#myAnswer').show();
	$.validationEngine.closePrompt(".formError",true);
}

var C={
	init: function(){
		C.sale.init();
		C.maps.init();
	},

	sale: {
		init: function(){
			var $sale_boxes=$('div.block9');
			if($sale_boxes.length){
				$sale_boxes.each(function(i){
					C.sale.cityEvents($(this));
				})
			}
		},
		
		
		cityEvents: function($sale_box){
			$sale_box.find('form').find('select[name=city]').bind('change',function(evt){
				evt.target.blur();
				var $form=$sale_box.find('form');
				var device_id=1;
				//$('#googleMaps'+device_id)[$.evIE('slideUp')]('fast');
				$sale_box.find('div.offices').empty();
				$form.ajaxSubmit({
					url: '/ajax_eq_dealers.php',
					type: 'POST',
					dataType: 'json',
					success: function(json){
						$sale_box.find('div.offices').html(json.offices || '');

						$sale_box.find('div.offices span.gcoords').each(function(){
							$(this).wrap($(document.createElement('a')).attr({href:'#'}));
							$(this).parent().bind('click',function(evt){
								evt.preventDefault();
								$(this).parents('table').eq(0).find('a.active').removeClass('active');
								$(this).addClass('active');
								var coords=$(this).children().attr('rel');
								device_id=parseFloat(coords.split(',')[2]);
								$('#googleMap'+device_id)[$.evIE('slideUp')]('fast');
								C.maps.show(device_id,coords);
							});
						});
					}
				})
			})
		}
	},
	
	maps: {
		init: function(){
			$('body').unload(function(){GUnload()});
		},

		show: function(device_id,gcoords){
			device_id=parseFloat(gcoords.split(',')[2]);
			var $gmap=$('#googleMap'+device_id);
			if(!$gmap.length){return}
			if(GBrowserIsCompatible()){
				var x=parseFloat(gcoords.split(',')[0]);
				var y=parseFloat(gcoords.split(',')[1]);
				if($gmap[0].offsetHeight==0){
					$gmap[$.evIE('slideDown')]('fast',function(){
						C.maps.draw(device_id,x,y);
						$('#footer').hide().show();
					});
				}else{
					C.maps.move(device_id,x,y);
				}
			}
		},
		
		draw: function(device_id,x,y){
			var map=new GMap2($('#googleMap'+device_id)[0]);
			map.setCenter(new GLatLng(x,y), 15);
			map.addControl(new GLargeMapControl());

			//map.addControl(new GScaleControl());
			//map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			//map.setUIToDefault();
			map.enableScrollWheelZoom();

			C.maps.baseIcon = new GIcon(G_DEFAULT_ICON);
			//C.maps.baseIcon.image = "/img/marker.png";
			map.addOverlay(new GMarker(new GLatLng(x,y),new GIcon(C.maps.baseIcon)));
			C.maps['map'+device_id]=map;
		},
		
		move: function(device_id,x,y){
			C.maps['map'+device_id].panTo(new GLatLng(x,y), 15);
			C.maps['map'+device_id].addOverlay(new GMarker(new GLatLng(x,y),new GIcon(C.maps.baseIcon)));
		}
	}
}
