function formatPhone(phone) {
	return '(' + phone.substr(0, 3) + ') ' +  phone.substr(3, 3) + '-' +  phone.substr(6, 4);
}

function getStoreInfo(URL) {
	jQuery.post(URL, {'DoAjax': 'yes'}, function (data) { 
		jQuery(".store").html(data);
	});
}

function addHighlight(strUnit) {
	var strCat = null;
	jQuery('ul.category').stop();
	jQuery('a.active, li.active').removeClass('active');
	if (strUnit !== undefined) {
		if (strUnit.substring(0, 3) === 'cat') {
			strCat = strUnit;
			strUnit = null;
		} else {
			strCat = jQuery('li.' + strUnit).parents('li').attr('class');
			jQuery('.' + strUnit + ' > a').addClass('active');
		}
		jQuery('li.' + strCat).addClass('active');
		jQuery('#categories li.' + strCat + ' a').addClass('active');
		if (jQuery('.category .' + strCat + ' > ul').length > 0) {
			jQuery('ul.category').scrollTo(jQuery('.category .' + strCat + ' > ul').parents('li'), 1200);
		}
	}
	
	var URL = null;
	if (jQuery('.category a.active').length > 0) {
		URL = jQuery('.category a.active').attr('href');
	} else if (strUnit !== null) {
		URL = '/vacant.php?unit=' + strUnit;
	}
	if (URL !== null) {
		getStoreInfo(URL);
	} else {
		getStoreInfo(document.location.href);
	}
}

function buildStoreList() {
	jQuery('ul.category').html('');
	jQuery.getJSON('/getStores.php', function (data) {
		jQuery.each(data.categories, function (i, cat) {
			var strList = '<li class="cat-' + cat.id + '">' + cat.name + '<ul class="stores">';
			jQuery.each(cat.stores, function (i, store) {
				strList += '<li class="unit-' + store.unit_id + ' "><a href="/' + cat.url + '/' + store.name + '/" title="' + store.name + '">' + store.name + '</a></li>';
			});
			strList += '</ul></li>';
			jQuery('ul.category').append(strList);
		});
	});	
}

jQuery(document).ready(function () {   						   
	jQuery('#header-animation').flash({ 
		src: '/swf/header.swf', 
		width: 780, 
		height: 92, 
		flashvars: {autoPlay: true, autoRewind: false}
	}, {version: '8'});			   
						   
	jQuery.scrollTo.defaults.axis = 'y';
	   
	var strUnit = jQuery('.category a.active').parents('li').attr('class');
	if (strUnit !== undefined) { 
		addHighlight(strUnit);
	} else { 
		addHighlight(jQuery('#categories a.active').parents('li').attr('class')); 
		strUnit = ''; 
	}

	var flashvars = {
		unit: strUnit 
	};

	var params = {
		menu: "false",
		allowscriptaccess: "sameDomain", 
		wmode: 'transparent',
		quality: 'high',
		loop: false
	};
	swfobject.embedSWF('/swf/map.swf', "map", "536", "330", "8.0.0", "/swf/expressInstall.swf", flashvars, params, {id: "mapmovie"});

	var mapmovie = function (id) {
		if (jQuery.browser.msie) { 
			return document.getElementById(id);
		} else { 
			return document[id]; 
		}
	};
	
	jQuery('ul#categories li a').click(function (event) {
		event.preventDefault();
		addHighlight(jQuery(this).parents('li').attr('class'));
		mapmovie('mapmovie').showMeOnMap('home');
	});
	
	jQuery('ul.stores li a').click(function (event) {
		event.preventDefault();
		addHighlight(jQuery(this).parents('li').attr('class'));
		mapmovie('mapmovie').showMeOnMap(jQuery(this).parents('li').attr('class'));
	});
	
	jQuery('#nav ul > li').hover(function () {
		jQuery('ul:first', this).fadeIn('fast');
	},
	function () {
		jQuery('ul:first', this).fadeOut('fast');
	});
	jQuery('#nav ul li li').hover(function () {
		jQuery('ul:first', this).each(function () {
			jQuery(this).css('top', jQuery(this).parent().position().top);
			jQuery(this).css('left', jQuery(this).parent().position().left + jQuery(this).parent().width());
			jQuery(this).show();
		});
	}, function () {
		jQuery('ul:first', this).hide();
	});
});