//jas - this file contains code for the Store Locator page (department 103)

// Configure store data
var Stores = new Array();

	Stores["Boston University"] = new Store("boston_university","Boston University","1035 Commonwealth Avenue","Boston, MA 02215-1001","617-782-5121","Mon-Sat: 10 AM - 9 PM<br>Sunday: 10 AM - 8 PM",42.352011,-71.122417,"Store_Boston","Boston_University.jpg");
	Stores["Back Bay"] = new Store("back_bay","Back Bay","480 Boylston Street","Boston, MA 02116-3700","617-267-3900","Mon-Sat: 9 AM - 9:30 PM<br>Sunday: 10 AM - 8 PM",42.350945,-71.074205,"Store_Boston","Back_Bay.jpg");
	Stores["Downtown Crossing"] = new Store("downtown_crossing","Downtown Crossing","11 Bromfield Street","Boston, MA 02108-5107","617-423-2015","Mon-Fri: 9 AM - 8 PM <br>Saturday: 10 AM - 8 PM<br>Sunday: 11 AM - 7 PM",42.356474,-71.059739,"Store_Boston","Downtown_Crossing.jpg");
	Stores["Harvard Square"] = new Store("harvard_square","Harvard Square","44 Brattle Street","Cambridge, MA 02138-3741","617-492-6000","Mon-Sat: 10 AM - 9 PM<br>Sunday: 10 AM - 8 PM",42.374004,-71.121519,"Store_Boston","Harvard_Square.jpg");
	Stores["Porter Square"] = new Store("porter_square", "Porter Square", "1815 Massachusetts Avenue", "Cambridge, MA 02140-1430", "617-661-1666", "Mon-Sat: 10 AM - 9 PM<br>Sunday: 10 AM - 8 PM", 42.387152, -71.119105, "Store_Boston", "Porter_Square.jpg");
	Stores["Chestnut Hill"] = new Store("chestnut_hill", "Chestnut Hill", "37 Boylston Street", "Chestnut Hill, MA 02467-1719", "617-566-0220", "Mon-Sat: 10 AM - 9 PM<br>Sunday: 10 AM - 8 PM", 42.320225, -71.175974, "Store_Boston", "Chestnut_Hill.jpg");
	Stores["Legacy Place"] = new Store("legacy_place","Legacy Place","950 Providence Highway","Dedham, MA 02026","781-381-5500","Mon-Sat: 10 AM - 9 PM<br>Sunday: 10 AM - 8 PM",42.232871,-71.179712,"Store_Boston","Legacy_Place.jpg");
	Stores["Wellesley"] = new Store("wellesley","Wellesley","475 Washington Street","Wellesley, MA 02482","781-431-0010","Mon-Fri: 9:30 AM - 8 PM<br>Sat: 10 AM - 8 PM<br>Sun: 10 AM - 7 PM",42.301156, -71.28788,"Store_Boston","Wellesley.jpg");
	
	Stores["19th Street"] = new Store("19th_street","19th Street","1111 19th Street NW<br>(Between L and M)","Washington, DC 20036-3603","202-467-4100","Mon-Thurs: 10 AM - 8 PM<br>Friday: 9 AM - 9 PM<br>Saturday: 10 AM - 8 PM<br>Sunday: 11 AM - 6 PM",38.904050,-77.043466,"Store_Washington","19th_street.jpg");
	
	Stores["Gallery Place"] = new Store("gallery_place","Gallery Place","715 7th St NW","Washington, DC 20001-3715","202-638-3115","Mon-Thurs: 10 AM - 9 PM<br>Fri-Sat: 10 AM - 9:30 PM<br>Sunday: 10 AM - 8 PM",38.898632,-77.021906,"Store_Washington","gallery_Place.jpg");
	
		Stores["Silver Spring"] = new Store("silver_spring","Silver Spring","8510 Fenton St.","Silver Spring, MD 20910","301-585-1730","<br>Mon - Sat: 10 AM - 9 PM<br>Sunday: 11 AM - 7 PM",38.99604858770304,-77.02484607696533,"Store_Washington","silver_Place.jpg");
	
	Stores["Bethesda"] = new Store("bethesda","Bethesda","7121 Arlington Road","Bethesda, MD 20814","301-718-1007","Mon-Fri: 10 AM - 9 PM<br>Saturday 9 AM - 9 PM <br>Sunday: 10 AM - 7 PM",38.981080,-77.098403,"Store_Bethesda","Bethesda.jpg");
	
	Stores["Georgetown"] = new Store("georgetown","Georgetown","3338 M Street NW","Washington, DC 20007","202-944-9600<br>Store Manager: Christina Vega","Mon-Thurs: 10 AM - 8 PM<br>Fri-Sat: 10 AM - 8:30 PM<br>Sunday: 11 AM - 7 PM",38.904833, -77.067131,"Store_Washington","Georgetown.jpg");
	
	Stores["Atlantic Station"] = new Store("atlantic_station","Atlantic Station","261 19th St NW Suite 1170","Atlanta, GA 30363-1132","404-541-1100","<b>Location Closing on 1/1/2012</b><br>Mon-Thurs: 10 AM - 9 PM<br>Fri-Sat: 10 AM - 10 PM<br>Sunday: 12 PM - 7 PM",33.793441,-84.396545,"Store_Atlanta","Atlantic_Station.jpg");
	
	Stores["Harbor East"] = new Store("harbor_east","Harbor East","809 Aliceanna Street","Baltimore, MD 21202-4385","410-837-4420","Mon-Sat: 10 AM - 9 PM<br>Sunday: 11 AM - 7 PM",39.283197,-76.601124,"Store_Baltimore","Harbor_East.jpg");
	
	Stores["5th Avenue"] = new Store("5th_avenue","5th Avenue","390 5th Avenue (at 36th)","New York, NY 10018-8104","212-695-0171","Mon-Sat: 9 AM - 9 PM<br>Sunday 9 AM - 8 PM",40.749750,-73.983575,"Store_NewYork","5th_avenue.jpg");
	
	Stores["Rockefeller Center"] = new Store("Rockefeller_Center","Rockefeller Center","64 West 48th Street<br>Between 5th and 6th Ave","New York, NY 10036","212-730-2009","Mon-Sat: 9 AM - 9 PM<br>Sunday: 10 AM - 8 PM",40.758209, -73.980928,"Store_NewYork","Rockefellar_center.jpg");
		
	Stores["Center City"] = new Store("center_city","Center City","1608 Walnut Street","Philadelphia, PA 19103-5403","215-985-5860","Mon-Thurs: 9 AM - 9 PM<br>Fri-Sat: 9 AM - 9:30 PM<br>Sunday: 10 AM - 8 PM",39.949777,-75.167659,"Store_Philadelphia","Center_City.jpg");
	Stores["Suburban Square"] = new Store("suburban_square","Suburban Square","6 East Montgomery Ave","Ardmore, PA 19003-2405","610-649-7497","Mon-Tues: 10 AM - 8 PM<br>Wed: 10 AM - 9PM<br>Thurs: 10 AM - 8 PM<br>Friday: 10 AM - 9 PM<br>Sat-Sun: 10 AM - 7 PM",40.009645,-75.289682,"Store_Philadelphia","Suburban_Square.jpg");
	Stores["Wayne"] = new Store("wayne","Wayne","309 East Lancaster Ave","Wayne, PA 19087","610-688-0400","<br>Mon-Fri: 10 AM - 9 PM<br>Saturday: 10 AM - 8 PM<br>Sunday: 11 AM - 7 PM",40.043634, -75.380094,"Store_Philadelphia","Wayne.jpg");
	
	Stores["Thayer Street"] = new Store("thayer_street","Thayer Street","271 Thayer Street","Providence, RI 02906-1524","401-521-6555","Mon-Sat: 10 AM - 9 PM<br>Sunday: 11 AM - 7 PM",41.829171,-71.400778,"Store_ThayerStreet","Thayer_Street.jpg");

// Store constructor
function Store(Id,Name,Address,CSZ,Phone,Hours,Lat,Long,ContentDiv,Image){
	this.Id = Id;
	this.Name = Name;
	this.Address = Address;
	this.CSZ = CSZ;
	this.Phone = Phone;
	this.Hours = Hours;
	this.Lat = Lat;
	this.Long = Long;
	this.ContentDiv = ContentDiv;
	this.Image = Image;
}

// Show a Google Map for a store
function ShowStoreMap(StoreName){
	
	// If the store exists...
	if(StoreExists(StoreName)){
			
		// Draw the google map
	    DrawGMap(Stores[Store].Lat, Stores[Store].Long, "<span class=\"GmapInfoWindowText\"><strong>" + Stores[Store].Name + " Store</strong><br>" + Stores[Store].Address + "<br>" + Stores[Store].CSZ + "</span>");
	    $("#tableStoreDetails").show();
	}
}

// Draw a Google Map
function DrawGMap(Lat, Long, InfoWindowHTML){

	// Configure map div name
	var MapDiv = "GoogleMap";
	
	// Configure offset so info window isn't hidden by controls
	var Offset = new Array();
	Offset["Lat"] = 0.002;
	Offset["Long"] = 0.000;
	
	// If the browser is compatible with Google Maps...
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById(MapDiv));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());
		map.setCenter(new GLatLng(Lat + Offset["Lat"], Long + Offset["Long"]), 15);
		var point = new GLatLng(Lat, Long);
		map.addOverlay(new GMarker(point));
		map.openInfoWindowHtml(point,InfoWindowHTML);
	}
}

// Show store info
function ShowStore(StoreName){

	// If the store exists...
	if(StoreExists(StoreName)){

		// Hide iframe div
		document.getElementById("about_main_image").style.display = "none";

		// Show store info div
		document.getElementById("about_store_div").style.display = "block";

		// Show store image
		if (Stores[StoreName].Image != "") {
		    document.getElementById("store_image").innerHTML = "<img src=\"" + $("#hiddenStoreBannerImagePath").val() + Stores[StoreName].Image + "\" alt=\"" + Stores[StoreName].Name + "\" />";
		}
		else {
		    document.getElementById("store_image").innerHTML = "";
		}

		// Show store name
		document.getElementById("Store_Name").innerHTML = Stores[StoreName].Name + " Store";
	
		// Show address
		document.getElementById("Store_Address").innerHTML = Stores[StoreName].Address + "<br>" + Stores[StoreName].CSZ + "<br>" + "Phone: " + Stores[StoreName].Phone;
		
		// Show store hours
		document.getElementById("Store_Hours").innerHTML = Stores[StoreName].Hours;
		
		// Show directions link
		document.getElementById("Store_Directions").innerHTML = "<a href=\"http://maps.google.com/maps?f=q&hl=en&geocode=&time=&date=&ttype=&q=" + encodeURIComponent(Stores[StoreName].Address) + "," + encodeURIComponent(Stores[StoreName].CSZ) + "&sll=37.09024,-95.712891&sspn=45.601981,101.074219&ie=UTF8&z=16&iwloc=addr&om=1&f=d\" target=\"_new\" style=\"display:block;\"><img src=\"assets/images/buttonGetDirections.jpg\" alt=\"Get Directions\" border=\"0\"/></a>";
		
		
		// Show store content div
		ShowStoreContent(StoreName);
		
		// Show map
		ShowStoreMap(StoreName);

		//getStoreImage(Stores[StoreName].Id);

	} else {
	
		// Show an error
		alert("ERROR: Sorry, the store \"" + StoreName + "\" was not found.");	

	}
}

// Show store info by id
function ShowStoreById(StoreId){

	// Show store
    ShowStore(GetStoreNameById(StoreId));
    $("#tableStoreDetails").show();
}

// Show store content div
function ShowStoreContent(StoreName){

	// For each of the stores...
	for(Store in Stores){
		// Hide the div
		document.getElementById(Stores[Store].ContentDiv).style.display = "none";			
	}	
	// Show the desired div
	document.getElementById(Stores[StoreName].ContentDiv).style.display = "block";
}

// Determine whether a store exists
function StoreExists(StoreName){

	// For each of the stores...
	for(Store in Stores){
		
		// If the store name matches...
		if(Stores[Store].Name == StoreName){
		
			return true;
		}
	}
	return false;
}

// Determine whether a store exists
function GetStoreNameById(StoreId){

	// For each of the stores...
	for(Store in Stores){
		
		// If the store name matches...
		if(Stores[Store].Id == StoreId){
		
			return Stores[Store].Name;
		}
	}
	return false;
}

// Get selected store (from header)
function GetSelectedStore(key) {

    //if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return false;
    else
        return qs[1].replace("%20", " ");
}  
	// Get the cookie value
    //var cookie_usercity = GetCookieByName('usercity');	
	
	// If the cookie isn't null...
//	if (cookie_usercity != null) 
//	{

//		// Split the city/store
//		var usercity_split = cookie_usercity.split("%2C");
//		
//		// Get the store
//		var usercity = usercity_split[1];
//		
//		// If the city is isn't null...
//		if (usercity.length>1)
//		{
//			return usercity;
//		}
//	}

//	return false;
//}

//get cookie information (to see if shopper has already selected a store with the MY CITY box)
//function GetCookieByName(name) {
//	var nameEQ = name + "=";
//	var ca = document.cookie.split(';');
//	for(var i=0;i < ca.length;i++) {
//		var c = ca[i];
//		while (c.charAt(0)==' ') c = c.substring(1,c.length);
//		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
//	}
//	return null;
//}

//shows default store
function ShowDefaultStore() {	
	// If a store is selected...
	if(GetSelectedStore('store_info')){
	
		// Show the store	
		// IMPORTANT: Must use setTimeout, otherwise IE breaks.
	    //setTimeout("ShowStoreById(GetSelectedStore('store_info'))", 1);
	    ShowStoreById(GetSelectedStore('store_info'));
	} else {	
		// Show default content
	    $("#about_main_image").show();
	}
}

// image name should be the same as the store name used to get the store 
// when a link is selected. e.g. boston_univeristy
function getStoreImage(imageName) {   
    
    // this hidden will contain the the resolved image path    
    var imagePath = $("#hiddenStoreImagePath").val();
    imagePath = imagePath + "/"+ imageName + ".jpg";
    // clear out any existing children
    $("#imageStoreBanner").children().remove();
    $("#imageStoreBanner").image(imagePath);
}

// simple jquery plugin esque to retreive the image asynchronosly
$.fn.image = function(src, f) {
    return this.each(function() {
        $("<img />").appendTo(this).each(function() {
            this.src = src;
            this.onload = f;
        });

    });
}









