var searchStores = [];
var browser = navigator.appName;


function searchByPostcode(){   
	if ($('searchByPostcodeInput')){
		
		//console.log($('searchByPostcodeInput').getValue());

		var postcode = $('searchByPostcodeInput').value; 
		var container = $('searchByPostcodeResult');
		if (postcode){
			var request = new Ajax( 
					"./load.php?module=moduleLocationsHB&action=searchByForm&postcode="+postcode+"&itemsPerPage=5",
					{
						method: "get",
						headers: {"Content Length": 1024},
						evalScript:true, 
						onComplete:function(obj){ 
								var response = eval(request.response.text);
								container.innerHTML='';
								for(i in response){
									if (response[i].id && response[i].name && $('searchByPostcodeResult')){
										searchStores.push(response[i]);
										var new_element = document.createElement('li');
										new_element.innerHTML = '<a href="javascript:void(0);" onclick="showLocationDetails('+response[i].id+'); return false;" >'+response[i].name+'</a>';
										container.appendChild(new_element);
										
										/*new_element.innerHTML = response[i].name;
										new_element.setAttribute('onclick','showLocationDetails("'+response[i].id+'")');
										container.insertBefore(new_element, container.firstChild);*/
										$('searchByPostcodeResultTitle').style.display="block";										
									}
								}								
						}
					}
				).request();		
		}
	}
}


function searchBySuburb(){   
	if ($('searchBySuburbInput')){
		
		var suburb = $('searchBySuburbInput').value; 
		var state = '';
		var test = suburb.split(', '); 
		
		if (test && test[0]){ suburb = test[0];	} 
		if (test && test[1]){ state = test[1];	} 
		
		var container = $('searchBySuburbResult');
		if (suburb){
			var request = new Ajax( 
					"./load.php?module=moduleLocationsHB&action=searchByForm&suburb="+suburb+"&state="+state+"&itemsPerPage=5",
					{
						method: "get",
						headers: {"Content Length": 1024},
						evalScript:true, 
						onComplete:function(obj){ 
								var response = eval(request.response.text);
								container.innerHTML='';
								for(i in response){ 
									if (response[i].id && response[i].name && container){ 
										searchStores.push(response[i]);
										var new_element = document.createElement('li');
										new_element.innerHTML = '<a href="javascript:void(0);" onclick="showLocationDetails('+response[i].id+'); return false;" >'+response[i].name+'</a>';
										container.appendChild(new_element);
										
										/*new_element.innerHTML = response[i].name;
										new_element.setAttribute('onclick','showLocationDetails("'+response[i].id+'")');*/
										//container.insertBefore(new_element, container.firstChild);
										$('searchBySuburbResultTitle').style.display="block";
									}
								}								
						}
					}
				).request();		
		}
	}
}


function searchByState(state, statename){   
	if (state){
		
		var container = $('searchByStateResult');
		var request = new Ajax( 
				"./load.php?module=moduleLocationsHB&action=searchByForm&state="+state,
				{
					method: "get",
					headers: {"Content Length": 1024},
					evalScript:true, 
					onComplete:function(obj){ 
							var response = eval(request.response.text);
							container.innerHTML='';
							container.innerHTML = "<h2>Hog's Breath <span>in "+statename+"</span></h2>";
							var c=0;
							var columns = 4; 
							var column=1;
							var sizeofResponse=0;
							for(i in response){ 
								if (response[i].name){
									sizeofResponse++;
								}
							}
							var itemsPerColumn= sizeofResponse/columns;		
							//console.log(sizeofResponse);
							for(i in response){ 
								if (response[i].id && response[i].name && container){									
									searchStores.push(response[i]);
									if (c==0){var ul_element = document.createElement('ul');}

									if (column==columns) { 
										if (browser=="Microsoft Internet Explorer")	{
											ul_element.setAttribute('className', 'hogsBreath2');
										} else {
											ul_element.setAttribute('class', 'hogsBreath2');	
										}
									}else 
									if (column>1){ 
										if (browser=="Microsoft Internet Explorer")	{
											ul_element.setAttribute('className', 'hogsBreath1');
										} else {
											ul_element.setAttribute('class', 'hogsBreath1');	
										}
									}
									
									
									var li_element = document.createElement('li');
									li_element.innerHTML = '<a href="javascript:void(0);" onclick="showLocationDetails('+response[i].id+'); return false;" >'+response[i].name+'</a>';
									/*var a_element = document.createElement('a');
									a_element.innerHTML = response[i].name;
									a_element.setAttribute('onclick','showLocationDetails('+response[i].id+'); return false;');
									a_element.setAttribute('href','javascript:void(0);');
									a_element.setAttribute('title',response[i].name);									
									li_element.appendChild(a_element);*/
									
									ul_element.appendChild(li_element);
									c++;
									if (c>=itemsPerColumn){container.appendChild(ul_element); c=0; column++;}
								}
							}								
							if (c>0 && ul_element){container.appendChild(ul_element);}
							
							adjustSearchByHeight();
					}
				}
			).request();		
	}
}

/* Ajax Search Suburb suggestion */
function searchSuggest(obj){
	if (obj && obj.value){
		
		var suburb = obj.value; 
		var container = $('searchSuggestResults');
		
		if (suburb && suburb.length>=3){
			var request = new Ajax( 
					"./load.php?module=moduleHogsBreath&action=suburbSuggest&suburb="+suburb,
					{
						method: "get",
						headers: {"Content Length": 1024},
						evalScript:true, 
						onComplete:function(obj){ 
								var response = eval(request.response.text);
								container.innerHTML = '';
								for(i in response){
									if (response[i].value){
										var new_element = document.createElement('li');
										
										
										new_element.innerHTML = '<a href="javascript:void(0);"'+ 
											'onclick="$(\'searchBySuburbInput\').value=\''+response[i].value+'\'"'+
											'onmouseover="this.style.background=\'#bbb\'"'+
											'onmouseout="this.style.background=\'#ddd\'">'+
											response[i].value+'</a>';
										container.appendChild(new_element);
										
										/*new_element.innerHTML = response[i].value;
										new_element.setAttribute('onclick','$("searchBySuburbInput").value="'+response[i].value+'"');
										new_element.setAttribute('onmouseover','this.style.background="#bbb"');
										new_element.setAttribute('onmouseout','this.style.background="#ddd"');*/
										//container.appendChild(new_element);
									}
								}								
						}
					}
				).request();		
		}else {
			container.innerHTML = '';
		}
	}
}

function updateStores(obj){
	if (obj && obj.value){
		
		target = $(obj.id+'_target');
		target.options.length = 0;
		var state = obj.value; 
		if (state && target){
			var request = new Ajax( 
					"./load.php?module=moduleHogsBreath&action=lookupStores&state="+state,
					{
						method: "get",
						headers: {"Content Length": 1024},
						evalScript:true, 
						onComplete:function(obj){ 
								var response = eval(request.response.text);
								target.style.display="block";
								for(i in response){
									if (response[i].value && response[i].text){													
										var option = document.createElement("OPTION");
										option.text = response[i].text;
										option.value = response[i].value;
										target.options.add(option);										
									}
								}								
						}
					}
				).request();		
		}
	}
}




function showLocationDetails(id){
	if (id){
		for(c in searchStores){
			//console.log(searchStores[c].id+'='+id);
			if (searchStores[c].id && searchStores[c].id == id){ 
				//console.log('match');
				$('store_details').style.display="block";
				$('store_name').innerHTML = searchStores[c].name;
				
				$('store_contact').innerHTML = '';
				if (searchStores[c].phone){ $('store_contact').innerHTML += '<strong>Ph: </strong>'+searchStores[c].phone+'<br/>';}
				if (searchStores[c].fax){ $('store_contact').innerHTML += '<strong>Fax: </strong>'+searchStores[c].fax+'<br/>';}
				if (searchStores[c].email){ 
					$('store_contact').innerHTML += '<strong>Email: </strong>'+
					'<a href="mailto:'+searchStores[c].email+'">'+searchStores[c].email+'</a>';
				}

				$('store_address').innerHTML = '<h2> </h2>';
				if (searchStores[c].address){ 
					$('store_address').innerHTML += '<h4>Address</h4>';
					if (searchStores[c].building){
						$('store_address').innerHTML += searchStores[c].building+'<br>';
					}
					$('store_address').innerHTML += searchStores[c].address+'<br>'+
					searchStores[c].suburb+' '+searchStores[c].state+' '+searchStores[c].postcode;
				}
				if (searchStores[c].description){ $('store_address').innerHTML += '<p id="store_description">'+
					'<H4>Trading Hours:</H4>'+searchStores[c].description+'</p>';
				}
				
				if ($('store_location')){
					$('store_location').innerHTML = '<a href="javascript:void(0)" onclick="openGoogleMap('+searchStores[c].id+')" id="openGoogleMap"><img title="View location on a map" alt="View location on a map" src="modules/locations/templates/default/images/bgViewLocation.jpg"/></a>';
				}
				//$('openGoogleMap').setAttribute('onclick','openGoogleMap('+searchStores[c].id+'); alert("asas");');
				
				location.href="#store_details";
				break;
			}	
		}
	}	
}

function openGoogleMap(id){
	if (id){
		for(c in searchStores){
			if (searchStores[c].id && searchStores[c].id == id){ 
				window.open ("custom/storeGoogleMap.php?address="+searchStores[c].address+"&suburb="+searchStores[c].suburb+"&state="+searchStores[c].state+"","mywindow","menubar=0,resizable=1,location=0,width=400,height=500");
			}
		}
	}
	return false;
}

function showEnquiryForm(obj){

	if (obj && obj.value){	
		var children = $('contactUsRight').childNodes;
		for(var c=0; c<children.length; c++){
			if (children[c].className=='enquiryFormWrap'){
				children[c].style.display = "none";
			}
		}
		
		id = obj.value;
		//console.log(id);
		$('enquiryFormWrap_'+id).style.display="block";
		
	}
}


/* Image Map */
function Show_Over_Map(id,reg){
	document.getElementById("alllocations").style.display    = "none";
	document.getElementById(id).style.display  = "";
	
	if(reg){
	//Getting the Current Borwser name.
	var browserName = navigator.appName;

	if (browserName=="Microsoft Internet Explorer")	{
	    document.getElementById(reg).setAttribute('className', 'active');
	} else {
		document.getElementById(reg).setAttribute('class', 'active');	
	}
	}
}
function Show_On_Map(id,reg){
	document.getElementById("alllocations").style.display    = "";
	document.getElementById(id).style.display  = "none";
	
	if(reg){
	//Getting the Current Borwser name.
	var browserName = navigator.appName;

	if (browserName=="Microsoft Internet Explorer")	{
	    document.getElementById(reg).setAttribute('className', '');
	} else {
		document.getElementById(reg).setAttribute('class', '');	
	}
	}
}

function adjustSearchByHeight(){ 
	var children = $('searchByStateResult').getElementsByTagName('ul');
	var searchByPostCodeWrapper_height = $('searchByPostCodeWrapper').offsetHeight-71;
	var heightest = 0;
	for(var c=0; c<children.length; c++){  
		if (children[c] && children[c].offsetHeight && heightest<children[c].offsetHeight){
			heightest = children[c].offsetHeight;
		}
	}	
	if (heightest < searchByPostCodeWrapper_height){
		heightest = searchByPostCodeWrapper_height;	
	}else {
		$('searchByPostCodeWrapper').style.height= (heightest+71)+'px';	
		$('searchBySuburbWrapper').style.height= (heightest+71)+'px';	
	}
	for(var c=0; c<children.length; c++){  
		children[c].style.height = heightest+'px';
	}	

}



