var md_ele     = null;   // Store HTML element object that called the MD_SetElement function.
var md_id      = 0;      // Store ID of a suite which is used to get information for an associated merchant or tenant
var md_type    = '';     // Store type of item to get information for, value can be merchant or tenant.
var md_timeout = 0;      // Used to retard the display update time so that someone can click an items link before it is replaced with a different one.

var dirCatsNav_last = null;


/**
 * Set which element to update.
 *
 * @param    object    ele       HTML element object that called this function.
 * @param    string    iType     Type of item to get information for, value can be merchant or tenant.
 * @param    int       iId       ID of a suite which is used to get information for an associated merchant or tenant.
 * @return   none
 **/
function MD_SetElement(ele, iType, iId){
	if(md_timeout){
		clearTimeout(md_timeout);
		md_timeout = 0;
	}
	md_ele     = ele;
	md_id      = iId;
	md_type    = iType;
	md_timeout = setTimeout(function(){MD_Update();}, 500);
}


/**
 * Update element which was set in MD_SetElement() above.
 *
 * @param    none
 * @return   none
 **/
function MD_Update(){
	// if an element, an ID and a type exist.
	if(md_ele && md_id && md_type){
		// Get some merchant or tenant data.
		$.post('http://www.newmarketsquare.com/ajax/' + md_type + '.php?id=' + md_id, function(response_text){
			var v = eval("(" + response_text + ")");
			if(v){
				// Fade out main container.
				$('#mapDetailsBlock').fadeOut('fast', function(){
					// Set image if one exists.
					if(v.image && v.image.mapthumb){
						$('#mapDetailsA').html('<img src="' + v.image.mapthumb + '" alt="' + (v.data.name ? v.data.name : '') + '" title="' + (v.data.name ? v.data.name : '') + '" />');
					}else{
						$('#mapDetailsA').html('');
					}
					// Any data?
					if(v.data){
						if(v.data.name){
							md_ele.alt   = v.data.name;
							md_ele.title = v.data.name;
							$('#mapDetailsStoreName').html(v.data.name);
						}else{
							md_ele.alt   = '';
							md_ele.title = '';
							$('#mapDetailsStoreName').html('');
						}
						if(v.data.description){
							$('#mapDetailsStoreDescription').html(v.data.description);
						}else{
							$('#mapDetailsStoreDescription').html('');
						}
						if(v.data.phone){
							$('#mapDetailsStorePhoneHeading').html('Phone');
							$('#mapDetailsStorePhone').html(v.data.phone);
						}else{
							$('#mapDetailsStorePhoneHeading').html('');
							$('#mapDetailsStorePhone').html('');
						}
						if(v.data.web_address){
							$('#mapDetailsStoreURLHeading').html('Web');
							$('#mapDetailsStoreWebAddress').html('<a href="http://' + v.data.web_address + '" title="' + (v.data.name ? v.data.name : '') + '" target="_blank">' + v.data.web_address + '</a>');
						}else{
							$('#mapDetailsStoreURLHeading').html('');
							$('#mapDetailsStoreWebAddress').html('');
						}
						if(v.data.hours){
							$('#mapDetailsStoreHoursHeading').html('Hours');
							$('#mapDetailsStoreHours').html(v.data.hours.replace(/\n/, '<br/>'));
						}else{
							$('#mapDetailsStoreHoursHeading').html('');
							$('#mapDetailsStoreHours').html('');
						}
					}else{
						// Clear everything. We should never really get here.
						$('#mapDetailsStoreName').html('&nbsp;');
						$('#mapDetailsStoreDescription').html('&nbsp;');
						$('#mapDetailsStorePhone').html('&nbsp;');
						$('#mapDetailsStoreWebAddress').html('&nbsp;');
						$('#mapDetailsStoreHours').html('Not available');
					}
					$('#mapDetailsBlock').fadeIn('slow');
				});
			}
		});
	}
}


function List_Update(eid, cid){
	$.post('http://www.newmarketsquare.com/ajax/list.php?cid=' + cid, function(response_text){
		$('#dir').fadeOut('fast', function(){
			$('#dir').html(response_text);
			$('#dir').fadeIn('slow', function(){
				if(document.getElementById(eid)){
					if(dirCatsNav_last){
						dirCatsNav_last.className = 'dirCatsNav_normal';
					}
					document.getElementById(eid).className = 'dirCatsNav_highlighted';
					dirCatsNav_last = document.getElementById(eid);
				}
			});
		});
	});
}
