/**
 * @author Andrew Murphy <andrew@fryewiles.com>
 * @version 0.0.1a 20070809
 */




var fwLocations = {
	'id': {
		'tabs': '#directions-tabs li a',
		'description' : 'directions-widget-description',
		'fromAddress' : 'fromAddress',
		'toAddress'   : 'toAddress',
		'title'       : 'directions-widget-title',
		'schedule'    : 'schedule-'
	},
	'selected': null,

	'info': {
		'canyon-crest'     : {
			'title'       : 'Canyon Crest Studio',
			'description' : '5053 LaMart Dr., Suite 202<br>Riverside, 92507,<br>(951) 784-3110',
			'address'     : '5053 LaMart Dr., Suite 202 Riverside, CA 92507',
			'image'       : '/images/locations/canyon_crest_floor.jpg'
		},
		'tibbetts'         : {
			'title'       : 'Tibbetts Studio',
			'description' : '3742 Tibbetts St., Suite 202<br>Riverside, CA 92506,<br>(951) 276-YOGA (9642)',
			'address'     : '3742 Tibbetts St. Suite 202, Riverside, CA 92506',
			'image'       : '/images/locations/tibbetts_floor.jpg'
		},
		'rancho-cucamonga' : {
			'title'       : 'Rancho Cucamonga Studio',
			'description' : '3742 Tibbetts St., Suite 202<br>Riverside, 92506,<br>(951) 276-YOGA (9642)',
			'address'     : '9271 Arrow Route Rancho Cucamonga, CA 91730',
			'image'       : '/images/locations/rancho_cucamongo_floor.jpg'	
		},
		'research' : {
			'title'       : 'Research Institute',
			'description' : '4159 Mission Inn Ave<br>Riverside, CA 92501<br>(951) 276-YOGA (9642)',
			'address'     : '4159 Mission Inn Ave Riverside, CA 92506',
			'image'       : '/images/locations/rancho_cucamongo_floor.jpg'	
		},
		'seattle' : {
			'title'       : 'Seattle Studio',
			'description' : '206-850-2292<br>eastlakeyoga.com',
			'address'     : 'Seattle, WA 98101',
			'image'       : '/images/locations/seattle-studio.jpg'	
		}
	},

	'behaviour': {
		'#directions-tabs li a': function(element) {
			element.onclick = function(event) {
				fwLocations.select(element);
				return false;
			}
		}
	},

	'init': function(){
		fwDirections.init();

		var tabs = $$(fwLocations.id.tabs);
		var ctab = null;
		var temp = null;

		if(tabs){
			for(key in tabs){
				if(tabs.hasOwnProperty(key)){
					ctab = tabs[key];
					if(
						ctab.className                                &&
						ctab.className.indexOf('selected') >= 0
					){
						fwLocations.select(ctab);
					}else{
						temp = $(fwLocations.id.schedule+ctab.rel);
						if(temp){
							temp.style.display=  'none';
						}
					}
				}
			}
		}
	},

	'unselect': function(element){
		if(
			element != fwLocations.selected ||
			element == null                   ||
			typeof element == 'undefined'
		){
			return false;
		}
		var temp = element.parentNode;
		temp.className = element.className.replace(/selected/i, '');
		fwLocations.selected = null;
		$(fwLocations.id.description).innerHTML = '';
		
		temp = $(fwLocations.id.schedule+element.rel);
		if(temp){
			temp.style.display=  'none';
		}

		return true;
	},

	'select': function(element){
		if(
			element == fwLocations.selected ||
			element == null                 ||
			typeof element == 'undefined'
		){
			return false;
		}
		fwLocations.unselect(fwLocations.selected);

		var temp = element.parentNode;

		temp.className += ' selected';
		fwLocations.selected = element;

		fwLocations.display(element.rel);
		
		fwLocations.selected = element;

		return true;
	},

	'display': function(rel){
		if(!rel){
			return false;
		}
		if(fwLocations.info.hasOwnProperty(rel)){
			var info   = fwLocations.info[rel];
			var temp = $(fwLocations.id.description);

			if(temp){
				temp.innerHTML = '<h4>'+info.title+'</h4>\n'+info.description;
				temp.style.backgroundImage = 'url('+info.image+')';
			}

			temp = $(fwLocations.id.title);
			if(temp){
				temp.innerHTML = info.title;
			}

			temp = $(fwLocations.id.toAddress);
			if(temp){
				temp.value = info.address;
			}
			
			temp = $(fwLocations.id.schedule+rel);
			if(temp){
				temp.style.display=  'block';
			}

			fwDirections.reset_directions();
			fwDirections.center_map(info.address);
		}
		return true;
	}

}






toggle = function(expand_id) {
	var expand_content = $(expand_id);
	var next_class = 'hide-scrollers';
	
	if ( expand_content ) {
		var expand_link = $(expand_id + '-link');
		
		// Class modification HAIL ME!
		next_class = expand_link.innerHTML.toLowerCase().indexOf('close') != -1
						? 'hide-scrollers'
						: '';
		
		//Scriptaculous expand
		new Effect.toggle(
			expand_content, 
			'slide', 
			{
				duration: 0.5,
				afterFinish: function() {
					expand_content.className = next_class;
				}
			}
		);
		
		// Link title modification
		if ( expand_link.innerHTML.toLowerCase().indexOf('close') != -1 ) {
			expand_link.innerHTML = 'click to expand';
			expand_content.className = 'hide-scrollers';
		}
		else {
			expand_link.innerHTML = 'click to close';
		}
	}
}


Behaviour.register(fwLocations.behaviour);
Behaviour.addLoadEvent(fwLocations.init);

