var map_place;
var localSearch = new GlocalSearch();
var geocoder = new GClientGeocoder(); 


$(document).ready(function() {
	$("#courses-users").accordion({
		collapsible: true,
		animated: 'bounceslide',
		autoHeight: false
	});
	
	$(".subAccordion").accordion({
		collapsible: true,
		animated: 'bounceslide',
		autoHeight: false
	});
	
	jQuery('#courseSlider').jcarousel({
        vertical: true,
        scroll: 2, 
		animation:"slow",
		visible:4
    });
	
	jQuery('#atozslider').jcarousel({
        vertical: true,
        scroll: 2, 
		animation:"slow",
		visible:3
    });
	
	/*AJAx validation for course registration form */
	
	$("#courseRegistrationForm").validate({
		rules:{
			name:"required",
			tel:"required",
			email: {
				required:true,
				email:true
			},
			noOfPeople:"required",
			code: "required"
		},
		messages:{
			name:"<br/>Please enter your full name",
			tel:"<br/>Please enter your the contact number",
			email:{
				required: "<br/>Please enter your email address",
				email: "<br/>Please enter correct email id"
			}, 
			noOfPeople:"<br/>Please enter total numberof people attending",
			code:"<br/>The please fill the text in the image"
			
		}
	}); 
	
	
	$("#eventRegistrationForm").validate({
		rules:{
			name:"required",
			tel:"required",
			email: {
				required:true,
				email:true
			},
			noOfPeople:"required",
			code: "required"
		},
		messages:{
			name:"<br/>Please enter your full name",
			tel:"<br/>Please enter your the contact number",
			email:{
				required: "<br/>Please enter your email address",
				email: "<br/>Please enter correct email id"
			}, 
			noOfPeople:"<br/>Please enter total numberof people attending",
			code:"<br/>The please fill the text in the image"
			
		}
	}); 

	
	if($("#map_place").length!=0){
		mapLoad();	
	}else{
		GUnload();		
	}
	
});

function mapLoad() {
	if (GBrowserIsCompatible()) {
		map_place = new GMap2(document.getElementById("map_place"));
		gdir = new GDirections(map_place, document.getElementById("directions"));
		GEvent.addListener(gdir, "error", handleErrors);
		map_place.addControl(new GLargeMapControl());
		var postCode=$("#postCode").val();
		var firstLine=$("#firstLine").val();
		var fullAddres=firstLine+" "+postCode;
		var coord=$("#coord").val();
		var zoom=$("#zoom").val();
		if(coord!="" && coord!=undefined){
			showAddress(coord, zoom);	
		}else{
			showAddress(postCode, zoom);
		}
	}
} 

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addUnLoadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
	  window.onunload = func;
	} else {
	  window.onunload = function() {
	    oldonunload();
	    func();
	  }
	}
}

function showAddress(address, zoom) {
	zoom=parseInt(zoom);
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
        map_place.setCenter(point, zoom);
        var marker = new GMarker(point);
		var mT=$("#markerText").val();
		
        map_place.addOverlay(marker);
		GEvent.addListener(marker, "click", function(){
		 if(mT!=""){
		 	map_place.openInfoWindowHtml(point, mT);
		 }
		});
		if(mT!=""){
        	marker.openInfoWindowHtml(mT);
		}
      }
    }
  );
}

function clicked(){

	var from=document.getElementById("fromPcode").value;
	var coord=$("#coord").val();
	if(coord==""){
		var to=$("#postCode").val();	
	}else{	
		var to=coord;
	}
	setDirections(from, to, "en");

}



function setDirections(fromAddress, toAddress, locale) {

  gdir.load("from: " + fromAddress + " to: " + toAddress,

			{ "locale": locale });

}

function handleErrors(){

   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)

	 alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);

   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)

	 alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);

   

   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)

	 alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);



//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong

//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);

	 

   else if (gdir.getStatus().code == G_GEO_BAD_KEY)

	 alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);



   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)

	 alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
   

}


