/*
----------------------------------------------------------------------------
Modified by: Hoang Nguyen 253-924-5305
Date: 10/23/2007

----------------------------------------------------------------------------
*/
//************	 BUILD LOCATION ARRAY
weyLocation = new Array();
function buildLocation (name, sectorID, sectorName, businessID, businessName, address1, address2, city, state, zip, shipAddress1, shipAddress2, shipCity, shipState, shipZip, TimeZone, phone, description, directions, x, y) {
	this.name = name;
	this.sectorID = sectorID;
	this.businessID = businessID;
	this.sectorName = sectorName;
	this.businessName = businessName;
	this.address1 = address1;
	this.address2 = address2;
	this.city = city;
	this.state = state;
	this.zip = zip;
	this.shipAddress1 = shipAddress1;
	this.shipAddress2 = shipAddress2;
	this.shipCity = shipCity;
	this.shipState = shipState;
	this.shipZip = shipZip;
	this.TimeZone = TimeZone;
	this.phone = phone;
	this.description = description;
	this.directions = directions;
	this.x = x;
	this.y = y;
	this.iconID = 0;
}



//************	 BUILD BUSINESS AND SECTOR NAME ARRAYS
businessName = new Array();
function buildBusinessName(id, name, sectorID,  sectorName) {
	this.id = id;
	this.name = name;
	this.sectorID = sectorID
	this.sectorName = sectorName
}

sectorName = new Array();
function buildSectorName(id, name) {
	this.id = id;
	this.name = name;
}



//************	 FILTER ON RIGHT LOCATION CLICK
function filter(who , from) {
	var currGroup = -1;
	var currLoc = -1;
	var filterGroup = ''
	var filterLoc = ''
	var currFrom = ''
	var cityState = ''
	var load = ''
	
	if (who == 'all') {
		rightLocations = '<samp>';
		for (i in weyLocation) {
			//	set up local variables
			switch( view ){
				case 'corp': 
					filterGroup = weyLocation[i].sectorName;
					filterLoc = weyLocation[i].businessName
					currFrom = weyLocation[i].businessID;
					load = 'loadLeftMenu('+ currFrom +',\'menu\');'
				break;
				 
				default : 
					filterGroup = weyLocation[i].businessName;
					filterLoc = weyLocation[i].name;
					cityState = weyLocation[i].city +', '+ weyLocation[i].state +'<br>';
					currFrom = i;
					load = 'loadDescription('+ currFrom +',\'description\');'
			}
			if ( currGroup != filterGroup ) {
				rightLocations += '<b>'+ filterGroup +':</b><br>';
				currGroup = filterGroup;
			}

			if ( currLoc != filterLoc ||  view != 'corp' ) {
				if (currFrom == from) {
					//  mark navigation 'at' state by checking 'from' arg
					rightLocations += '&raquo;&nbsp;'+ filterLoc +'<br>' + cityState;
				}
				else {
					
					rightLocations += '<a href="#" onclick="filter(\'all\' , \''+ currFrom +'\'); ' + load + ' return false;">'+ filterLoc +'</a><br>' + cityState;
				}
				currLoc = filterLoc;
			}
		}
	}
	
	else {
		allFilters();
		if ( view == 'corp' ) {
			rightLocations = '<b>'+ sectorName[who].name +':</b><br><samp>'; 
		}
		else { 
			rightLocations = '<b>'+ businessName[who].name +':</b><br><samp>'; 
		}
		
		//turn all locations OFF by default -Jim Liang 9/9/05
		//to fix issues with single city having multiple weyLocations
		
		/*comment out by Hoang Nguyen
		//not display the locaitons on the map if the sector is not selected*/
		
		for (i in weyLocation) {
		//turn city icon off
				//bang = new Flip( "location"+weyLocation[i].iconID+"Img", "/maps/images/iconMapEmpty.gif" );
				bang = new Flip( "location"+weyLocation[i].iconID+"Img", "/maps/images/iconMapEmpty.gif" );  
				bang.on();
		}
		
		for (i in weyLocation) {
			var currIconID = '';
			//	set up pointer variables
			switch( view ){
				case 'corp': 
					filterGroup = weyLocation[i].sectorID;
					filterLoc = weyLocation[i].businessName;
					currFrom = weyLocation[i].businessID;
					load = 'loadLeftMenu(\''+ currFrom +'\' , \'menu\');';
				break;
				
				default : 
					filterGroup = weyLocation[i].businessID;
					filterLoc = weyLocation[i].name;
					currFrom = i;
					load = 'loadDescription(\''+ currFrom +'\' , \'description\');';
					cityState = weyLocation[i].city +', '+ weyLocation[i].state +'<br>';
			}
			
			if (filterGroup == who) {
				// mark navigation 'at' state by checking 'from' arg
				if ( currLoc != filterLoc  ||  view != 'corp' ) {
					if ( currFrom == from ){
						rightLocations += '&raquo;&nbsp;'+ filterLoc +'<br>' + cityState;
					}
					else{ 
						rightLocations += '<a href="#" onclick="filter(\''+ filterGroup +'\' , \''+ currFrom +'\');' + load + ' return false;">'+ filterLoc +'</a><br>' + cityState;
					}
					currLoc = filterLoc;
				}
				currIconID = weyLocation[i].iconID 
				bang = new Flip("location"+ weyLocation[i].iconID +"Img", "/maps/images/iconMap.gif" );
				bang.on();
			}
			//else if( currIconID != weyLocation[i].iconID ){
				//turn city icon off
			//	bang = new Flip( "location"+weyLocation[i].iconID+"Img", "/maps/images/iconMapEmpty.gif" ); 
			//	bang.on();
			//}
		}
		bang = new Flip( 'rightSideBar' + who +"Img" , "/maps/images/iconSidebarFilled.gif" );
		bang.on();
	}
	
	if ( mapOver.size.y > mapImage.size.y-20 ) rightChannel.moveTo(300, mapOver.position.y + mapOver.size.y + 10);
	
	(isNS4) ? rightLocations = '<table width="130" cellpadding="0" cellspacing="0" border="0"><tr><td width="130">'+ rightLocations + '</samp>' +'</td></tr></table>': rightLocations += '</samp>' ;
	rightChannel.write(rightLocations);
	rightChannel.show();

	changeHelp('To view location details, select a business from the lower right-hand menu.  Or, narrow your search by choosing a city from the map above or a group in the upper-right menu.');

	//rightChannel.getSize();
	help.getSize();	

	if (help.size.y < rightChannel.size.y && !isNS4) help.clipTo(0,580,rightChannel.size.y+5,0);
	leftChannel.write('');
	leftChannel.hide();
	resizeMaparea();
}


//************	DISPLAY T3 LEVEL DETAILED LOCATION INFORMATION IN leftChannelDiv
	
var corpView = '' //set global variable when in corporate view mode, adds back arrow to details and rewrites right channel correctly in loadCity()

function loadDescription(who, which) {
	var leftContent = '<img src="/images/pixel.gif" width="285" height="1" alt=""><br>'
	//make a back button when viewing in corpView.
	var backButton = ''
	
	if ( view == 'corp' ){
		if ( which == 'descriptionCorp' || which == 'directionsCorp' ){
			backButton = '<br><b>&nbsp;&laquo;&nbsp;<a href="#" onclick="loadLeftMenu( '+ weyLocation[who].businessID +' , \'menu\');return false;">Back</a></b><br>';
			corpView = 'Corp';
		}
		if ( which == 'descriptionCity' || which == 'directionsCity' ){
			backButton = '<br><b>&nbsp;&laquo;&nbsp;<a href="#" onclick="loadCity( '+ who +' );return false;">Back</a></b><br>';
			corpView = 'City';
		}
	}
	 
	// addresses, phone, and time zone info rows are all conditional only if they have content.
	leftContent += '<span class="left"><b>'+ weyLocation[who].name +'</b><br><i>Mailing Address:</i><br>'+ weyLocation[who].address1 +'<br>';
	if (weyLocation[who].address2!='') leftContent += weyLocation[who].address2 +'<br>';
	leftContent += weyLocation[who].city +' '+ weyLocation[who].state +', '+ weyLocation[who].zip +'<br><br>';
	if (weyLocation[who].shipAddress1 != '') {
		leftContent += '<i>Shipping Address:</i><br>'+ weyLocation[who].shipAddress1 +'<br>';
		if (weyLocation[who].shipAddress2 != '') leftContent += weyLocation[who].shipAddress2 +'<br>';
		leftContent += weyLocation[who].shipCity +' '+ weyLocation[who].shipState +', '+ weyLocation[who].shipZip;
	}
	if (weyLocation[who].TimeZone != '') leftContent += '<br><br><i>Time Zone:</i><br>'+ weyLocation[who].TimeZone;
	if (weyLocation[who].phone != '') leftContent += '<br><br><i>Phone:</i><br>'+ weyLocation[who].phone;
	
	//leftContent += '<br><br>';
	
	// details and driving details conditional only if they have content ( assumes that if a loc doesn't have any description info, it doesn't have any driving directions, won't write either tab.) 'corpView' variable ensures that back button gets written on subsequent calls )
	if ( which == ('description'+corpView) ) {
		var directionTab = ''
		/*comment out by HN
		if (weyLocation[who].directions != ''){ directionTab = '|&nbsp;&nbsp;<a href="javascript:loadDescription('+ who +',\'directions'+corpView+'\')">Directions</a>' }

		if (weyLocation[who].description != ''){ leftContent += 'Details&nbsp;&nbsp;'+ directionTab +'<br><br>';
			leftContent += weyLocation[who].description;
		}
		*/
		if (weyLocation[who].description != ''){ leftContent += ''+ directionTab +'<br><br>';
			leftContent += weyLocation[who].description;
		}
	}
	/*comment by Hoang Nguyen
	if ( which == ('directions'+corpView) ) {
		if (weyLocation[who].directions != ''){ leftContent += '<a href="javascript:loadDescription('+ who +',\'description'+corpView+'\')">Details</a>&nbsp;&nbsp;|&nbsp;&nbsp;Directions<br><br>';
			leftContent += '<b>Driving Directions:</b><br>'+ weyLocation[who].directions;
		}
	}
	*/
	leftContent = backButton+leftContent+'</span>';
	
	changeHelp('To view a different address, select a business from the lower right-hand menu. You may also choose a city on the map above or a sector group from the upper right-hand menu.');
	leftChannel.write(leftContent);
	leftChannel.moveTo(help.position.x, help.position.y + help.size.y );
	leftChannel.getSize();
	//leftChannel.clipTo(0,285,leftChannel.size.y,0);
	leftChannel.show();
	
	blinkCity(weyLocation[who].iconID);
	
	resizeMaparea();
	return;
}



//************	 'loadLeftMenu' is only called in corporate view-  'who' is a business group id
function loadLeftMenu(who , which){
	var leftContent = '<img src="/images/pixel.gif" width="285" height="1" alt=""><br>'
	leftContent += '<b>&nbsp;'+ businessName[who].sectorName +'</b><br><b>&nbsp;&nbsp;&nbsp;'+ businessName[who].name +'&nbsp;Locations</b><br>'

	for (i in weyLocation) {
		if ( who == weyLocation[i].businessID ){
			leftContent += '<span class="left"><a href="#" onclick="loadDescription('+ i +' , \'descriptionCorp\'); return false;"><b>'+ weyLocation[i].name +'</b></a><br>'+ weyLocation[i].address1 +'<br>';
			leftContent += weyLocation[i].city +' '+ weyLocation[i].state +', '+ weyLocation[i].zip +'<br>';
			if (weyLocation[i].phone != '') leftContent +=  weyLocation[i].phone+'<br>';
			leftContent += '(<a href="#" onclick="loadDescription('+ i +' , \'descriptionCorp\'); return false;">view details</a>)<br></span>';
		}
	}
	
	changeHelp('To view a different address, select a business from the lower right-hand menu. You may also choose a city on the map above or a sector group from the upper right-hand menu.');
	leftChannel.getSize();
	leftChannel.write(leftContent);
	leftChannel.moveTo(help.position.x, help.position.y + help.size.y );
	leftChannel.getSize();
	//leftChannel.clipTo(0,285,leftChannel.size.y,0);
	leftChannel.show();
	resizeMaparea();	
	return;
}



//************	 DISPLAY MENU IN RIGHT CHANNEL FOR MAP ICON CLICK
	
function loadCity(who , from) {
	var busID = weyLocation[who].businessID;
	var busName =  weyLocation[who].businessName;
	var leftContent = ''
	var rightContent = ''	
	var countID = 0;
	var countLoc = 0;
	var currSector = -1;
	var currBusiness = -1;
	var header = '';
	var whoX = Thing.all['location'+who].position.x;
	var whoY = Thing.all['location'+who].position.y;
	
	leftChannel.write('');
	leftChannel.hide();
	resizeMaparea();
	allFilters();

	for(i in weyLocation) {
		//turn city icons off
		bang = new Flip( "location"+weyLocation[i].iconID+"Img", "/maps/images/iconMapEmpty.gif" );
		bang.on();
		// if locations are within 8 pixels of each other write list in right channel
		if( Math.abs( weyLocation[i].x - whoX ) < 8 && Math.abs( weyLocation[i].y - whoY ) < 8   ){
			header = '<p><b>Locations in and near '+ weyLocation[i].city + ', '+ weyLocation[i].state +'</b></p>'
			if ( view == 'corp' ){
				currSectorNameBlock = '<b>'+ weyLocation[i].sectorName +'</b><br><b>&nbsp;'+ weyLocation[i].businessName +'&nbsp;Locations</b><br>';
				var rightSideBarID = weyLocation[i].sectorID
			}
			else { 
				currSectorNameBlock = '<b>&nbsp;'+ weyLocation[i].businessName +'&nbsp;Locations</b><br>'; 
				var rightSideBarID = weyLocation[i].businessID
			}
			
			if ( currSector != weyLocation[i].sectorName ) {
				rightContent += '<p><b>'+ weyLocation[i].sectorName +':</b><br>';
				currSector = weyLocation[i].sectorName;
			}
			if ( currBusiness != weyLocation[i].businessName ) {
				rightContent += '<b>'+ weyLocation[i].businessName +'</b><br>';
				currBusiness = weyLocation[i].businessName;
			}
			
			if ( i == from ){	
				rightContent += '<samp>&raquo;&nbsp;'+ weyLocation[i].name +'<br>'+ weyLocation[i].city +', '+ weyLocation[i].state +'<br></samp>';	;
			}
			else{
				rightContent += '<samp><a href="#" onclick="loadCity('+ who +' , '+ i +');loadDescription('+ i +',\'description\'); return false;">'+ weyLocation[i].name +'</a><br>'+ weyLocation[i].city +', '+ weyLocation[i].state +'<br></samp>';	
				
			}
			// turn on icons in upper right
			bang = new Flip( "rightSideBar"+rightSideBarID+"Img", "/maps/images/iconSidebarFilled.gif" );
			bang.on();
			// turn on included city icons	
			city = new Flip( "location"+ weyLocation[i].iconID +"Img",  "/maps/images/iconMap.gif");
			city.on();
		}
	}
	
	changeHelp('To view location details, select a Location from the lower right-hand menu.');
	(isNS4) ? rightContent = '<table width="130" cellpadding="0" cellspacing="0" border="0"><tr><td width="130">'+ header+rightContent + '</td></tr></table>': rightContent = header+rightContent ;
	rightChannel.write(rightContent);
	if ( mapOver.size.y > mapImage.size.y-20 ) rightChannel.moveTo(300, mapOver.position.y + mapOver.size.y + 10);
	resizeMaparea();
	blinkCity(who);
	linkStreamCity(eval('location'+who+'.linkStream'));
	return;
}




//************	 BUILD LINKSTREAM WHEN CLICKING AND MOUSING ON CITY

linkStreamTrigger = '';
function linkStreamCity(who) {
	mapClickStreamOver.moveTo(mapClickStream.position.x+mapClickStream.size.x,top-100);
	mapClickStreamOver.write('<span class="list">&nbsp;&gt;&nbsp;'+ who.toUpperCase() +'</span>' );
	mapClickStreamOver.show();
	linkStreamTrigger = who;
}


function mapInfoOver(who) {
	if (tier==2) {
		if (isNS4) who.mapOverInfo = '<table border=0 cellpadding=0 cellspacing=0 width=130><tr><td>' + who.mapOverInfo +'</td></tr></table>';
		mapOver.write('<p>'+ who.mapOverInfo +'</p>');
		mapOver.show();
	}
	linkStreamCity(who.linkStream);
}

function mapInfoOut() {
	if (tier==2) mapOver.hide();
	mapClickStreamOver.hide();
}


//************	 TURN ALL FILTER AND CITY ICONS ON OR OFF 
function allCities(what) {
	if (what == 'on') imageSrc =  "/maps/images/iconMap.gif"
	else { imageSrc = "/maps/images/iconMapEmpty.gif"}
	for ( i in weyLocation ){
		bang = new Flip( "location"+weyLocation[i].iconID+"Img", imageSrc ); bang.on();
	}
}

function allFilters(what) { 
	if (what == 'on') imageSrc = "/maps/images/iconSidebarFilled.gif"
	else{imageSrc = "/maps/images/iconSidebarEmpty.gif" }
	var rightIcons = businessName
	if ( view == 'corp' ) rightIcons = sectorName
	for ( i in rightIcons ){
		bang = new Flip( "rightSideBar"+ i +"Img", imageSrc ); bang.on(); 
	}
}

//************	 RESET TO PAGE LOAD STATUS 
function resetFilters() {
	allFilters('on'); //turn them all on
	allCities('on'); //turn them all on

	linkStreamTrigger = '';
	mapInfoOut();
	filter('all');
	
	leftChannel.moveTo(help.position.x, help.position.y+help.size.y);
	leftChannel.write( stateInfo );
	leftChannel.getSize();
	//leftChannel.clipTo(0,285,leftChannel.size.y,0);
	leftChannel.show()
	
	resizeMaparea();
}



//************	 RESIZE MAP AREA

function resizeMaparea() {
	leftSize = '';
	mapImage.getSize();
	help.getSize();
	leftSize = mapImage.size.y + help.size.y + 15;
	if (leftChannel.isVisible()) {
		leftChannel.getSize();
		leftSize += leftChannel.size.y;

	}
	
	rightSize = '';
	mapOver.getSize();
	(mapOver.size.y > mapImage.size.y) ? rightSize = mapOver.size.y : rightSize = mapImage.size.y+20;
	if (rightChannel.isVisible()) {
		rightChannel.getSize(); 
		rightSize += rightChannel.size.y+300;  //to expand the container area vertically
	}
	if (leftSize >= rightSize && !isNS6) {
		mapContainer.clipTo(0,720,leftSize,0)  //modified by HN increasing size from 440 to 720
	} else if( !isNS6 ){
		mapContainer.clipTo(0,720,rightSize,0); //modified by HN increasing size from 440 to 720

	}
}




//************	 UTILITY FUNCTIONS

function blinkCity(who) {
	imageSrc = '/maps/images/iconMap.gif';
	imageSrcEmpty = '/maps/images/iconMapEmpty.gif';
	bang = new Flip( "location"+ who +"Img", imageSrc );
	bangOff = new Flip( "location"+ who +"Img", imageSrcEmpty );
	bangOff.on();
	setTimeout('bang.on()',500);
	setTimeout('bangOff.on()',1000);
	setTimeout('bang.on()',1500);
	setTimeout('bangOff.on()',2000);
	setTimeout('bang.on()',2500);
}


function changeHelp(myhelp) {
	helpContent = '';
	if ( ver.indexOf("mac") != -1 ) {
		helpContent = '<span class="list">'+ myhelp +'</span>';
	} else {
	
		helpContent = '<table border="0" cellpadding="0" cellspacing="0" width="560">'
			+ '<tr valign="top"><td width="10" height="10" nowrap></td><td width="1" nowrap></td><td width="6" nowrap></td><td width="3" nowrap></td><td width="245" nowrap></td><td width="3" nowrap></td><td width="6" nowrap></td><td width="1" nowrap></td><td width="10" nowrap></td></tr>'
			+'<tr valign="top">'
			+ '<td height="1" nowrap></td>'
			+ '<td bgcolor="#4d9609"><img src="../images/pixel.gif" width="1" height="1" alt=""></td>'
			+ '<td bgcolor="#4d9609"><img src="../images/pixel.gif" width="6" height="1" alt=""></td>'
			+ '<td></td>'
			+ '<td></td>'
			+ '<td></td>'
			+ '<td bgcolor="#4d9609"><img src="../images/pixel.gif" width="6" height="1" alt=""></td>'
			+ '<td bgcolor="#4d9609"><img src="../images/pixel.gif" width="1" height="1" alt=""></td>'
			+ '<td></td></tr>'
			+ '<tr valign="top">'
			+ '<td></td>'
			+ '<td height="10" bgcolor="#4d9609" nowrap><img src="../images/pixel.gif" width="1" height="1" alt=""></td>'
			+ '<td></td>'
			+ '<td></td>'
			+ '<td></td>'
			+ '<td></td>'
			+ '<td></td>'
			+ '<td bgcolor="#4d9609"><img src="../images/pixel.gif" width="1" height="1" alt=""></td>'
			+ '<td></td></tr>'
			+ '<tr valign="top">'
			+ '<td></td>'
			+ '<td bgcolor="#4d9609"><img src="../images/pixel.gif" width="1" height="1" alt=""></td>'
			+ '<td></td>'
			+ '<td></td>'
			+ '<td class="list">'+ myhelp +'</td>'
			+ '<td></td>'
			+ '<td></td>'
			+ '<td bgcolor="#4d9609"><img src="../images/pixel.gif" width="1" height="1" alt=""></td>'
			+ '<td></td></tr>'
			+ '<tr valign="top">'
			+ '<td></td>'
			+ '<td height="10" bgcolor="#4d9609" nowrap><img src="../images/pixel.gif" width="1" height="1" alt=""></td>'
			+ '<td></td>'
			+ '<td></td>'
			+ '<td></td>'
			+ '<td></td>'
			+ '<td></td>'
			+ '<td bgcolor="#4d9609"><img src="../images/pixel.gif" width="1" height="1" alt=""></td>'
			+ '<td></td></tr>'
			+ '<tr valign="top">'
			+ '<td></td>'
			+ '<td height="1" bgcolor="#4d9609" nowrap><img src="../images/pixel.gif" width="1" height="1" alt=""></td>'
			+ '<td bgcolor="#4d9609" nowrap><img src="../images/pixel.gif" width="6" height="1" alt=""></td>'
			+ '<td></td>'
			+ '<td nowrap></td>'
			+ '<td></td>'
			+ '<td bgcolor="#4d9609" nowrap><img src="../images/pixel.gif" width="6" height="1" alt=""></td>'
			+ '<td bgcolor="#4d9609" nowrap><img src="../images/pixel.gif" width="1" height="1" alt=""></td>'
			+ '<td></td></tr>'
			+ '<tr valign="top"><td width="10" height="10" nowrap></td><td width="1" nowrap></td><td width="6" nowrap></td><td width="3" nowrap></td><td width="245" nowrap></td><td width="3" nowrap></td><td width="6" nowrap></td><td width="1" nowrap></td><td width="10" nowrap></td></tr>'
			+ '</table>';
			

	}
	help.write(helpContent);
	help.getSize();
	help.clipTo(0,770,help.size.y,0);  //modified by HN increase from 285 to 770
}
