// File: readXML.js
// Start function when DOM has completely loaded 
$(document).ready(function(){


	//setTimeout(
	//	function(){
			//your code that isn't firing in IE here

	
			xmlpath = "xml/";//"";//
			
			myxmlpath = xmlpath + "storedata.xml";
			
			// Open the xml file
			$.get(myxmlpath,{},function(xml){
										
				//function getQueryVariable(variable) {
				  var query = window.location.search.toString().substring(1);
				  var vars = query.split("&");
				  for (var i=0;i<vars.length;i++) {
					var pair = vars[i].split("=");
					//if (pair[0] == variable) {
						varname = pair[0];
						varval  = pair[1];
						varlength = pair[1].length;
					//}
				  }
			//	}
			
				// Build an HTML string
				myHTMLOutput = '';
				//myHTMLOutput += '<form name="myform">';
		//		myHTMLOutput += '<select name="allstores">';
				myGMapOutput = '';
				
//				myHTMLOutput2 = '';
				myRotatorOutput = '';
				
				count = 0;
				
				//errormsg = '<p>Nobody home.</p>';
				
				errormsg = '<h2>No results were found.</h2><p>We couldn\'t find a BWR in your area,';
				errormsg += 'but <a href=franchise.php>Franchise Opportunities</a> may be available! </p>';
				errormsg += '<p>Would you like to <a href=javascript:history.back(1)>try your search again?</p>';
				
				
				
				
				//get the variables
				$('Row',xml).each(function(i, val) {
					
					id = $(this).find("id").text();
					
					//variables	
							
					address = $(this).find("address").text();
					address_map = replaceSpaces(address);
					
					city = $(this).find("city").text();
					city_pics = replaceSpaces2(city);
					city_pic = replaceSpaces3(city);
					
					state = $(this).find("state").text();
					zip = $(this).find("zip").text();
					zip = Math.round(Number(zip));
					
					//rama custom code for locations not google enabled
					//073 - charlotte
					if(id=="073")
					{
						address_gmap = 'Buffalo+Wings+%26+Rings&cid=6073373930062096046';
					} else
					{
						address_gmap = address_map +  '+' + city + ',+' + state + '+' + zip;
					}

					phone = $(this).find("phone").text();
					
					hours = $(this).find("hours").text();
					pics = $(this).find("pics").text();
					
					fax = $(this).find("fax").text();
					address2 = $(this).find("address2").text();
					
					/*		
					
					//phone = replaceSpaces3(phone);
					//fixed this in the xml
					
					email = $(this).find('email').text();
					*/		
		
					// Build row HTML data and store in string
					
					//legacy
					//if(eval(varname).substring(0,varlength) == eval(varval)){
					//if(eval(varname) == eval(varval)){
					
					if(varname == 'id'){
						if(eval(varname) == varval){
							
							mydata = BuildStoresHTML();
							myHTMLOutput = myHTMLOutput + mydata;
							
							
							
							//image and map
							
							//mypics = function(){
								
								myrotator = '<ul>';
								
								for(var p=1;p<=pics;p++){
									myrotator += '<li';
									if(p == 1){
										//myrotator += 'class="show"';
									}
									myrotator += '>';
									//myrotator += '<a href=javascript:void(0)>';
									myrotator += '<a href="photo_gallery.php?id=' + id + '">';
									
									myrotator += '<img src="locations/' + state + '-' + id + '-' + city_pics + '/restaurant-' + state.toLowerCase() + '-' + city_pic.toLowerCase() + '-' + p + 'a.jpg" width="300" height="220"  alt="pic' + p + '" />';
									
									myrotator += '</a>';
									myrotator += '</li>';
								}
								
								myrotator += '</ul>';
								//return myrotator;
								
							//};
							/*
							mypics = BuildRotatorHTML();
							*/
							myRotatorOutput = myRotatorOutput + myrotator;//mypics;
							
							//mygmap = BuildGMapHTML();											//	two
							//myGMapOutput = myGMapOutput + mygmap;								//	three
							myGMapOutput = '<iframe width="500" height="500" frameborder="1" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;hl=en&amp;geocode=&amp;q=' + address_gmap + '&amp;hnear=' + city + ", " + state + '&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe>';
							
							count++;
							
						}
					}
			
					/*
					else
					if(varname == 'zip'){
						//if(eval(varname).substr(0,2) == varval.substr(0,2){
						//if(eval(varname).toString.substr(0,2) == eval(varval).toString.substr(0,2)){
						if(eval(varname).toString().substr(0,2) == varval.toString().substr(0,2)){
							
							mydata = BuildStoresHTML2();
							myHTMLOutput2 = myHTMLOutput2 + mydata;
							
							mystate = state;
							
							mygmap = BuildGMapStateHTML();
							myGMapOutput = myGMapOutput + mygmap;
							
							count++;
							
						}
						
						
					}else
					if(varname == 'state'){
						if(eval(varname).toLowerCase() == varval.toLowerCase()){
							
							mydata = BuildStoresHTML2();
							myHTMLOutput2 = myHTMLOutput2 + mydata;
							
							mystate = state;
							
							mygmap = BuildGMapStateHTML();
							myGMapOutput = myGMapOutput + mygmap;
							
							count++;
							
						}
					}
					*/
					
				});
				
				
				
				//close the html output
		//		myHTMLOutput += '</select>';
				//myHTMLOutput += '</form>';
				
				if(count == 0){
					$("#ContentArea").append(errormsg);
				}else{
					
					if(varname == 'id'){
					
						$("#ContentArea").append(myHTMLOutput);
						// Update the DIVs with the HTML string
						
						$("#rotator").append(myRotatorOutput);
						
						
						$("#gmap").append(myGMapOutput);									//	one
					}
					
					/*
					else
					if(varname == 'zip'){
						
						$("#ContentArea").append('<h2>' + mystate + '</h2>');
					
						$("#ContentArea").append(myHTMLOutput2);
						$("#gmap").append('<iframe width="368" height="420" frameborder="1" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;hl=en&amp;geocode=&amp;q=' + mystate + '&amp;output=embed"></iframe>');
						
					}else
					if(varname == 'state'){
						
						$("#ContentArea").append('<h2>' + mystate + '</h2>');
					
						$("#ContentArea").append(myHTMLOutput2);
						$("#gmap").append('<iframe width="368" height="420" frameborder="1" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;hl=en&amp;geocode=&amp;q=' + mystate + '&amp;output=embed"></iframe>');
						
					}
					*/
					
				}
			});
		
		//font replacement
		//Cufon.now();
				
		// close off the timer here
		//}
		//, ( $.browser.msie ) ? 500 : 0
	//)
			
});


//string replacement
function replaceSpaces(arg){
	return (arg.replace(/ /gi, "+"));	
}

function replaceSpaces2(arg){
	return (arg.replace(/ /gi, ""));	
}

function replaceSpaces3(arg){
	return (arg.replace(/ /gi, "-"));	
}



 function BuildStoresHTML(){
	// Build HTML string and return

	output = '';

//	output += '<p>id = ' + id + ', with a < p > tag.</p>';
//	output += '<p>Here is a dropdown menu:</p>';
	
//	output += '<h1 id="' + id + '">This is a title: ' + id + '</h1>';
//	output += '<p id="' + id + '">This is a paragraph: ' + id + '</p>';
//	output += '<span name="' + id + '">This is a spam: ' + id + '</span><br />';
//	output += '<form id="' + id + '">This is a form: ' + id + '</form>';

//	output += '<form id="' + id + '"><p>This is a form with elements.</p>';
//	output += '<select>';
	output += '<h2>' + city + ', ' + state + '</h2>';
	output += '<h3>Location:</h3>';
	
	output += '<p>' + address + '<br />'
	if(address2 != "") {
		output += address2 + '<br />'
	}
	
	output += city + ', ' + state + ' ' + zip + '<br />Tel: ' + phone;
	
	if(fax != "") {
		output += "<br />Fax: " + fax;
	}
	
	/*
	if(email){
		output += '<br />email: ' + email + '</p>';
	}
	*/
	output += '<h3>Hours of Operation:</h3>';
	output += '<p>' + hours + '</p>';
	//Jasper specific code - Rama 20100118
	if(id=="087"){
		output += '<h3>Events: </h3>';
		output += '<p>' + ' <a href="Press/JASPERSportsProgrammingCalendar.pdf"> Sports Programming Calendar</a>' + '</p>';
	}

//	output += '</select>';
//	output += '</form>';

	//output += '<p>Option:</p><option value="' + id + '">' + id + '</option>';
	/*
	output += '<h2>' + city + ', ' + state + '</h2>';
	output += '<h3>Location:</h3>';
	output += '<p>' + address + '<br />' + city + ', ' + state + ' ' + zip + '<br />Tel: ' + phone;
	if(email){
		output += '<br />email: ' + email + '</p>';
	}
	output += '<h3>Hours of Operation:</h3>';
	output += '<p>' + hours + '</p>';
	*/
	return output;
}

/*


 function BuildStoresHTML2(){
	 //exact zip code match
	// Build HTML string and return
	output = '';
	//if(count != 0){
//	output += '<tr>';
//	output += '<td>';
	output += '<p><a href=locations_store.php?id=' + id + '>' + city + '</a><br />' + address + '<br />' + city + ', ' + state + ' ' + zip + '<br />' + phone + '</p>';
	
	return output;
}




 function BuildStoresHTML3(){
	 //state zip code match
	// Build HTML string and return
	output = '';
	//if(count != 0){
//	output += '<tr>';
//	output += '<td>';
	output += '<p><a href=locations_store.php?id=' + id + '>' + city + '</a><br />' + address + '<br />' + city + ', ' + state + ' ' + zip + '<br />' + phone + '</p>';
	
	return output;
}



function BuildRotatorHTML(){
	//enter li tags for jquery #rotator slideshow
	
	myrotator = '';
	
	myrotator += '<ul>';
	
	for(var p=1;p<=pics;p++){
	myrotator += '<li';
	if(p == 1){
		//myrotator += 'class="show"';
	}
	myrotator += '>';
	myrotator += '<a href=javascript:void(0)>';
	
	myrotator += '<img src="locations/' + state + '-' + id + '-' + city_pics + '/restaurant-' + state.toLowerCase() + '-' + city_pic.toLowerCase() + '-' + p + 'a.jpg" width="300" height="220"  alt="pic' + p + '" />';
	
	myrotator += '</a>';
	myrotator += '</li>';
	}
	
	myrotator += '</ul>';
	
	return myrotator;
}

*/


function BuildGMapHTML(){
	
	gmap = '';
	
	gmap += '<iframe width="500" height="500" frameborder="1" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;hl=en&amp;geocode=&amp;q=' + address_gmap + '&amp;z=14&amp;output=embed"></iframe>';
	
	return gmap;
}

/*


function BuildGMapStateHTML(){
	
	gmap = '';
	
	gmap += '<iframe width="368" height="420" frameborder="1" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;hl=en&amp;geocode=&amp;q=' + mystate + '&amp;z=6&amp;output=embed"></iframe>';
	
	return gmap;
}

*/