/**
 * Function to register the functions on dom ready.
 */

jQuery(document).ready(function(){
	
	jQuery("#contactlocatorId").click(function(){
		storeSearch();
	});
	

//	jQuery(".send_btn").click(function(){
//	    jQuery("#overlay_content").css("display","block");
//	});
	
	jQuery(".close_button").click(function(){
	    jQuery("#overlay_content").css("display","none");
	});
	
});

/**
 * is_array() function in JS
 * @param input
 * @return
 */
function is_array(input){
  return typeof(input)=='object'&&(input instanceof Array);
}

var delay = (function(){
  var timer = 0;
  return function(callback, ms){
    clearTimeout (timer);
    timer = setTimeout(callback, ms);
  };
})();

/**
 * search function
 */ 
function storeSearch(){
	
	var postalCode = jQuery("#postalcode_textbox").val();
	var selCountry = jQuery(".country_selectbox option:selected").html();
	var address = '';
	
	if(jQuery.trim(postalCode)!='' && jQuery.trim(selCountry)!='' && jQuery.trim(selCountry)!='Country'){
		address = postalCode+" "+selCountry;
	}else if(jQuery.trim(postalCode)!=''){
		address = postalCode;
	}	
	else if(jQuery.trim(postalCode)=='' && jQuery.trim(selCountry)!='' && jQuery.trim(selCountry)!='Country'){
		address = selCountry;
	}
	
	if(jQuery.trim(address)!=''){
		/**
		 * Initiating the GeoCoder
		 */  
		var geocoder = new GClientGeocoder();
		var latLang = geocoder.getLatLng(address,function(point){
			 if(point)	{
				 var str = '';
	//			 alert(address+"->"+point.y+"-"+point.x);
				 // Sending the latitude and Longitude and getting the relaed stores for particulat lat and long in JSON
				jQuery.ajax({
					type:"POST",
					data:{"lat":point.y,"long":point.x},
					url:W3FE_BASE_URL+"/"+FRNDLY_URL,
					success:function(msg){
	//						alert(msg);
						var result = eval('(' + msg + ')');
						buildDefaultResult(result);
					}
				});
			 }else{
				 /**
				  * If zip code or Country or City are wrong then execution will enter into this condition
				  */
				 //alert("Wrong data for Google Geocoder");
				 jQuery.alerts.yesButton = 'OK';
					
				 jConfirmmedia('Wrong data for Google Geocoder',"",function(msg) {
					if(msg){}
				  });
			 }
			 
		 });
	}else{
		jQuery.alerts.yesButton = 'OK';
		
		 jConfirmmedia('Enter the zipcode OR Select the Country',"",function(msg) {
			if(msg){}
		  });
	}
}

function buildDefaultResult(jsonObj){
	
	var defObj = jQuery("#store_locator_content");
	
	if(jsonObj.length>0){
		
		jQuery("#store_locator_content").html('');
		var stmlBuild = '<h1>STORE<br/>LOCATOR</h1>';
	
		var Objcount = 1;
		for(var i=0,j=1;i<jsonObj.length;i++,j++){
			
//			borderClass = (i < 4) ? "border_top" : "";
			if(Objcount<4){
				
			marginClass = (j!=0 && j%3 == 0) ? "no_margin" : "";
			
			/*var subDiv = jQuery.create('div', { class: 'store_locator_info '+marginClass }, '').appendTo(defObj);
			
			var addrDiv = jQuery.create('div', { class: 'street-address' }, jsonObj[i].Contact_Address).appendTo(subDiv);
			
			var titleDiv = jQuery.create('div', { class: 'name' }, '').appendTo(subDiv);
			
			var h3 = jQuery.create('h3', { id: 'title_'+i }, jsonObj[i].Contact_Title).appendTo(titleDiv);
			*/
			
			
	/*		var h3 = document.createElement("h3");
			h3.innerHTML = stripslashes(jsonObj[i].Contact_Title);
			
			var titleDiv = document.createElement("div");
			titleDiv.setAttribute("class","name");
			titleDiv.appendChild(h3);
			
			var addrDiv = document.createElement("div");
			addrDiv.setAttribute("class","street-address");
			addrDiv.innerHTML = jsonObj[i].Contact_Address;
			
			var subDiv = document.createElement("div");
			subDiv.setAttribute("class","store_locator_info "+marginClass);
			
			jQuery(subDiv).append(titleDiv);
			
			jQuery(subDiv).append(addrDiv);
			
			
			jQuery(defObj).append(subDiv);*/
			
			
//			stmlBuild += '<div class="store_locator_info '+marginClass+'"><div class="name"><h3>'+jsonObj[i].Contact_Title+'</h3></div><div class="street-address"><h4>Adres</h4><p>'+jsonObj[i].Contact_Address+'</p><p>'+jsonObj[i].Contact_Zip+'</p><p>'+jsonObj[i].Contact_Phone+'</p><p>'+jsonObj[i].Contact_Fax+'</p><p>'+jsonObj[i].Contact_Email+'</p><p>'+jsonObj[i].Contact_Website+'</p><p>'+jsonObj[i].Contact_Additionalinformation+'</p><p>'+jsonObj[i].Country_Name+'</p><p>'+jsonObj[i].City_Name+'</p></div></div>';
			stmlBuild += '<div class="store_locator_info '+marginClass+'"><div class="name"><h3>'+jsonObj[i].Contact_Title+'</h3></div><div class="street-address"><h4>Adres</h4><p>'+jsonObj[i].Contact_Address+'</p><p>'+jsonObj[i].Contact_Zip+'</p><p>'+jsonObj[i].Country_Name+'</p><p>'+jsonObj[i].City_Name+'</p></div></div>';
			}
			Objcount++;
		}
		
	}else{
		jQuery(defObj).html("<p class='no_stores'>No Stores Found</p>");
	}
	jQuery("#store_locator_content").html(stmlBuild);
	
	jQuery("#overlay_content").css("display","block");
}
function stripslashes (str) {
    return (str+'').replace(/\\(.?)/g, function (s, n1) {
        switch (n1) {
            case '\\':
                return '\\';
            case '0':
                return '\u0000';
            case '':
                return '';
            default:
                return n1;
        }
    });
}