$(document).ready( function() {
			
	$('.stockist:last').after('<div id="popup" style="display: none;"><div id="themap" style="width: 640px; height: 480px;"><\/div><\/div>');
	
	active = false;
	
	//create array to store coordinates of each location
	stockistInfo = new Array();
	//set variable index to keep tabs on which button is clicked
	index = 0;
	
	//grab url	
	$('.nyroModal').each(function(i){
		
		var currentUrl = $(this).attr("href");
		//seperate items via ampersand
		var urlComponents = currentUrl.split("&");
				
		if (urlComponents.length == 9 ) {
			
			$(this).addClass("nyro_success");
					
			//grab address item
			var q = urlComponents[4];
			
			//divide the values and variables to allow access to values
			var splitAddress = q.split("=");
			//convert value to string
			var address = new String(splitAddress[1]);
			//replace all occurences of x with a space
			var reg = /\++/g;
			var infoAddress = address.replace(reg," ");
			
			var mapCentre = urlComponents[urlComponents.length-1];
			//grab the coordinates only
			var mapCoordinates = mapCentre.substr(3);
			
			//grab business name
			var business = $(this).parent().find('h2').text()+" - ";
			
			//insert coordinates into array
			stockistInfo[i] = new Array (mapCoordinates, infoAddress, business);
			//alert(stockistInfo[i]+","+i);
			
			//insert new url into links
			var newUrl = "#popup"
			$(this).attr({ 
				href: newUrl
			});
		
		} else {
			//$(this).removeClass('nyroModal');
			$(this).addClass('nyro_error');
			var newUrl = "popup"
				$(this).attr({ 
					href: newUrl
			});			
			
		}
							  
							
	});
	
	
	//on click assign the index button in order to pick up the correct array
	$('.nyroModal').click( function() {
		index = $('.nyroModal').index(this);
		//alert(index)
	});
	
	/*
	$('.nyro_error').click( function(e) {
			e.preventDefault();
			var content = 'This is wrong';
			$.nyroModalManual({
				content: content
			});
			return false;
	});*/
	
	
	$('.nyro_success').click( function(e) {
		active = true;
		//e.preventDefault();							  
		//$.nyroModalManual({
			//gMap(index)
		//});
	 });
	
	
	$('#closeBut').click( function() {
		GUnload();
		active = false;
	});
	
	
});
