var myTempAdvertiserLocationsContainer;
var myTempLocationObject;

CJP.LocationCollection = Class.create();
CJP.LocationCollection.prototype = {

	initialize: function(locationsXML){
		this.locationXML = null;
		this.locations = new Array();
		this.categories = new Array();
		this.overlappingLocationsAreMatched = false;
		
		this.setLocationsXML(locationsXML);
		
		this.loaded = false;
	},
	
	resetLocationCollection: function(){
		this.locations = new Array();
		this.categories = new Array();
	},
	
	setLocationsXML: function(xml){
		this.resetLocationCollection();
		this.locationXML = xml;
		this.convertXMLToLocationCollection(xml);
	},
	
	convertXMLToLocationCollection: function(xml){		
		for(var i = 0; i < xml.getElementsByTagName('category').length; i++){
			this.categories[this.categories.length] = this.createCategoryLocationsFromXML(xml.getElementsByTagName('category')[i]);}
	},
	

	createCategoryLocationsFromXML: function(categoryXML){
		var id = categoryXML.getAttribute("id");
		var parentCategoryID = categoryXML.getAttribute("parentCategoryID");
		var name = categoryXML.getAttribute("name");
		var color = categoryXML.getAttribute("color");		
		var colorFolder = categoryXML.getAttribute("colorFolder");
		
		var newCategory = new CJP.Category(id, parentCategoryID, name, color, colorFolder);
		
		for(var i = 0; i < categoryXML.childNodes.length; i++){
			if(categoryXML.childNodes[i].nodeType != 1 || categoryXML.childNodes[i].nodeName != 'advertiser'){continue;}
			var newAdvertiser = this.createAdvertiserLocationsFromXML(categoryXML.childNodes[i], newCategory);
			newCategory.addAdvertiser(newAdvertiser);}
				
		for(var i = 0; i < categoryXML.getElementsByTagName('subCategory').length; i++){
			var newSubCategory = this.createCategoryLocationsFromXML(categoryXML.getElementsByTagName('subCategory')[i]);
			newCategory.addSubCategory(newSubCategory);}
		
		return newCategory;
	},
	
	createAdvertiserLocationsFromXML: function(advertiserXML, category){
		var id = advertiserXML.getAttribute("businessID");
		var name = advertiserXML.getAttribute("name");
		var collapseLocations = (advertiserXML.getAttribute("collapseLocationInWebsite") == 'True');		
		
		var newAdvertiser = new CJP.Advertiser(id, name, collapseLocations);
				
		for(var i = 0; i < advertiserXML.getElementsByTagName('location').length; i++){
			var newLocation = this.createLocationFromXML(advertiserXML.getElementsByTagName('location')[i], category, newAdvertiser);
			newAdvertiser.addLocation(newLocation);
			this.locations[this.locations.length] = newLocation;}
		
		return newAdvertiser;
	},
	
	createLocationFromXML: function(locationXML, category, advertiser){	
		var id = locationXML.getAttribute("id");
		var name = locationXML.getAttribute("name");		
		var omc = locationXML.getAttribute("onMapCode");
		var oms = locationXML.getAttribute("onMapShape");
		var ps = locationXML.getAttribute("plotShape");
		var pd = locationXML.getAttribute("plottingDimensions");
		var ac = locationXML.getAttribute("arrowCoordinates");		
		var addr1 = locationXML.getAttribute("addressLine1");
		var addr2 = locationXML.getAttribute("addressLine2");
		var city = locationXML.getAttribute("city");
		var state = locationXML.getAttribute("state");
		var zip = locationXML.getAttribute("zip");
		var phone = locationXML.getAttribute("phone");
		var website = locationXML.getAttribute("website");
		var includeInMIP = (locationXML.getAttribute("includeInMoreInfoPages").toLowerCase() == 'true');
		
		var newLocation = new CJP.Location(id, name, omc, oms, ps, pd, ac, addr1, addr2, city, state, zip, phone, website, includeInMIP, category, advertiser);		
		return newLocation;
	},
	
	getCategoryLocations: function(categoryID){
		var locationArray = new Array();
		for(var i = 0; i < this.locations.length; i++){
			if(this.locations[i].category.id == categoryID){locationArray[locationArray.length] = this.locations[i];}
		}
		return locationArray;
	},
	
	getAdvertiserLocations: function(advertiserID){
		var locationArray = new Array();
		for(var i = 0; i < this.locations.length; i++){
			if(this.locations[i].advertiser.id == advertiserID){locationArray[locationArray.length] = this.locations[i];}
		}
		return locationArray;		
	},
	
	getLocationByID: function(locationID){
		for(var i = 0; i < this.locations.length; i++){
			if(this.locations[i].id == locationID){return this.locations[i];}
		}
	},
	
	matchOverlappingLocations: function(){
		if(this.overlappingLocationsAreMatched){return;}
		var x, y, radius, currentX, currentY;
		var base, adjacent, hypotenuse;
		
		for(var i = 0; i < this.locations.length; i++){
			if(this.locations[i].plotShape != 'circle'){continue;}
			this.locations[i].overlappingLocations = new Array();
			x = parseInt(this.locations[i].plottingDimensions.substring(0, this.locations[i].plottingDimensions.indexOf(',')));
			y = parseInt(this.locations[i].plottingDimensions.substring(this.locations[i].plottingDimensions.indexOf(',')+1, this.locations[i].plottingDimensions.lastIndexOf(',')));
			radius = parseInt(this.locations[i].plottingDimensions.substring(this.locations[i].plottingDimensions.lastIndexOf(',')+1));
			
			for(var j = this.locations.length - 1; j > i; j--){
				if(this.locations[i] == this.locations[j] || this.locations[j].plotShape != 'circle'){continue;}
				currentX = parseInt(this.locations[j].plottingDimensions.substring(0, this.locations[j].plottingDimensions.indexOf(',')));
				currentY = parseInt(this.locations[j].plottingDimensions.substring(this.locations[j].plottingDimensions.indexOf(',')+1, this.locations[j].plottingDimensions.lastIndexOf(',')));
				
				if(x == currentX && y == currentY){
					this.locations[i].overlappingLocations[this.locations[i].overlappingLocations.length] = this.locations[j];
				}else{
					var base = (x > currentX) ? x - currentX : currentX - x;
					var adjacent = (y > currentY) ? y - currentY : currentY - y;
					
					if(base < radius || adjacent < radius){
						var hypotenuse = Math.ceil(Math.sqrt(Math.pow(base, 2) + Math.pow(adjacent, 2)));					
						if(hypotenuse <= radius){
							this.locations[i].overlappingLocations[this.locations[i].overlappingLocations.length] = this.locations[j];
							this.locations[j].overlappingLocations[this.locations[j].overlappingLocations.length] = this.locations[i];
						}
					}
				}
			}		
		}
		
		this.overlappingLocationsAreMatched = true;
	},
	
	deselectAll: function(){
		for(var i = 0; i < this.locations.length; i++){
			this.locations[i].deselect();
		}
	},
	
	hideLocations: function(){
		for(var i = 0; i < this.categories.length; i++){
			this.categories[i].CategoryContainer.style.display = 'none';
		}
	},
	
	showLocations: function(){
		for(var i = 0; i < this.categories.length; i++){
			this.categories[i].CategoryContainer.style.display = 'block';
		}
	}
};




CJP.Category = Class.create();
CJP.Category.prototype = {
	initialize: function(i, pcid, n, c, cf){
		this.id = i;
		this.parentCategoryID = pcid;
		this.name = unescape(n);
		this.color = c;
		this.colorFolder = cf;
		this.advertisers = new Array();
		this.subCategories = new Array();
		this.type = 'category';
		
		this.CategoryContainer = null;
		this.SmallBarImageContainer = null;
		this.LocationContainer = null;
		this.LocationFloatingContainer = null;
	},	
	addAdvertiser: function(advertiser){this.advertisers[this.advertisers.length] = advertiser;},
	addSubCategory: function(subCategory){this.subCategories[this.subCategories.length] = subCategory;},
	
	categoryIsExpanded: function(){return (this.LocationContainer.style.display == 'block');},	
	collapse: function(){
		this.LocationContainer.style.display = 'none';	
		turnImageOff(this.SmallBarImageContainer.getElementsByTagName('IMG')[0]);},
		
	expand: function(){
		this.LocationContainer.style.display = 'block';	
		turnImageOn(this.SmallBarImageContainer.getElementsByTagName('IMG')[0]);}
};


CJP.Advertiser = Class.create();
CJP.Advertiser.prototype = {
	initialize: function(i, n, c){		
		this.isIE7 = (/MSIE 7/.test(navigator.userAgent));
		this.id = i;
		this.name = unescape(n);
		this.collapseLocationInWebsite = c;		
		this.locations = new Array();
		this.type = 'advertiser';
		
		this.ieTimerFixer = null;
		
		this.ShowHideContainer = null;
		this.AdvertiserLocationsController = null;
		this.CollapsedFirstLine = null;
		this.AdvertiserLocationsContainer = null;
	},	
	
	addLocation: function(location){this.locations[this.locations.length] = location;},	
	duplicate: function(){return (new CJP.Advertiser(this.id, this.name, this.collapseLocationInWebsite));},
	
	locationsAreExpanded: function(){return (this.ShowHideContainer != null && this.ShowHideContainer.style.display == 'block');},
	
	collapse: function(){
		if(this.ShowHideContainer != null){
			this.ShowHideContainer.style.display = 'none';
			this.AdvertiserLocationsContainer.className = 'collapsedBusinessLocationContainer';
			this.AdvertiserLocationsController.src = 'images/multiLocationArrowUp.gif';
			this.AdvertiserLocationsContainer.style.height = '22px';}
	},
	
	expand: function(){
		if(this.ShowHideContainer != null){
			this.AdvertiserLocationsContainer.className = 'expandedBusinessLocationContainer';
			this.AdvertiserLocationsController.src = 'images/multiLocationArrowDown.gif';
			this.ShowHideContainer.style.display = 'block';
			
			var containerHeight = parseInt(this.CollapsedFirstLine.offsetHeight) + parseInt(this.ShowHideContainer.offsetHeight) + 7 + 'px';
			this.AdvertiserLocationsContainer.style.height = containerHeight;
			
			if(this.isIE7){
				myTempAdvertiserLocationsContainer = this;				
				window.setTimeout(new function(){
											   var newHeight = parseInt(myTempAdvertiserLocationsContainer.CollapsedFirstLine.offsetHeight) + parseInt(myTempAdvertiserLocationsContainer.ShowHideContainer.offsetHeight) + 7 + 'px';
												myTempAdvertiserLocationsContainer.AdvertiserLocationsContainer.style.height = newHeight;}, 200);
			}			
		}	
	}
};


CJP.Location = Class.create();
CJP.Location.prototype = {
	initialize: function(id, name, omc, oms, ps, pd, ac, addr1, addr2, city, state, zip, phone, website, includeInMIP, category, advertiser){
		this.id = id;
		this.name = unescape(name);
		this.onMapCode = omc;
		this.onMapShape = oms;
		this.plotShape = ps;
		this.plottingDimensions = pd;
		this.arrowCoordinates = ac;
		this.addressLine1 = unescape(addr1);
		this.addressLine2 = unescape(addr2);
		this.city = unescape(city);
		this.state = state;
		this.zip = unescape(zip);
		this.phone = unescape(phone);
		this.website = unescape(website);
		this.includeInMIP = includeInMIP;
		this.category = category;
		this.advertiser = advertiser;			
		this.type = 'location';
		
		this.LocationContainer = null;
		this.OnMapShapeContainer = null;
		this.OnMapCodeContainer = null;
		this.NameContainer = null;
		this.Separator = null;
		
		this.areaMaps = new Array();		
		this.overlappingLocations = new Array();
	},
	
	duplicate: function(){return (new CJP.Location(this.id, this.name, this.onMapCode, this.onMapShape, this.plotShape, this.plottingDimensions, this.arrowCoordinates, this.addressLine1, this.addressLine2, this.city, this.state, this.zip, this.phone, this.website, this.includeInMIP, this.category, this.advertiser));},
	
	deselect: function(){
		if(this.NameContainer.firstChild == null){return;}
		this.NameContainer.firstChild.className = this.category.color;},	
	select: function(){
		if(this.NameContainer.firstChild == null){return;}
		this.NameContainer.firstChild.className = this.category.color+'InverseNoRollover';}
};


