// fix names
for (var hostname in clubs) {
	clubs[hostname].name = clubs[hostname].name.replace(/ (Golf| |and|Country)*Club *$/,'');
}

// aliases
for (var hostname in clubs)
	for (var i in clubs[hostname].aliases)
		clubs[clubs[hostname].aliases[i]] = clubs[hostname];

// Replace the personalized club information on the page : name,logo,pro
addEvent(window,'load',dynamicClubPlacement);

function getClub(club) {
	// get the club_name from the cookie || query string || hostname || die
	var club = '';
	if (club=='') club=getQueryField('club');
	if (club=='') {
		var hostname = /^http:\/\/(.+).clubstore.org/.exec(document.location);
		if (hostname!=undefined && hostname.length>1 && hostname[1]!='www')
			club=hostname[1],s='hostname';
	}
	//if (club=='') club = getCookie('club'),s='cookie';
	return clubs[club];
}

function dynamicClubPlacement() {
	// image placeholders - logo, pro, club
	var club_logo_img = document.getElementById('club_logo');
	var club_pro_img = document.getElementById('pro_image');
	var club_img = document.getElementById('club_image');
	var pro_or_club_img = document.getElementById('pro_or_club_image');

//jda
//	var club_logo_full_img = document.getElementById('club_logo_full_img)');

	// display logo and club image
	if (club_logo_img!=undefined) club_logo_img.src='images/clubs/template_logo.jpg';
	if (club_img!=undefined) club_img.src='images/clubs/template_club.jpg';
	if (pro_or_club_img!=undefined) pro_or_club_img.src='images/clubs/template_club.jpg';

//jda
//	if (club_logo_full_img!=undefined) club_logo_full_img.src='images/clubs/template_logo.jpg';

	// get the club
	var club = getClub();
	if (club==undefined) {
		if (club_pro_img!=undefined) club_pro_img.style.display='none';
		return;
	}

	// replace the club name, and pro name
	setElementText('club_hostname',club.hostname);
	if (club.name!=undefined && club.name.length>0) setElementText('club_name','the ' + club.name + ' ClubStore');
	if (club.pro!=undefined && club.pro.length>0) setElementText('pro_name',club.pro);

	// replace club name for span.club_name and span.hostname
	var spans = document.getElementsByTagName('span');
	for (var i=0;i<spans.length;i++) {
		var e = spans.item(i);
		if (/club_name/.test(e.className)) e.innerHTML = club.name;
		if (/hostname/.test(e.className)) e.innerHTML = club.hostname;
	}

	// replace {hostname} with club.hostname for all a href attributes
	var as = document.getElementsByTagName('a');
	for (var i=0;i<as.length;i++) {
		var e = as.item(i);
		var href = e.getAttribute('href') || '';
		e.setAttribute('href',href.replace('{hostname}',club.hostname).replace('%7bhostname%7d',club.hostname));
	}

	// replace the club logo
	if (club_logo_img!=undefined) club_logo_img.src='/images/clubs/' + club.hostname + '_logo.jpg';

	// replace the pro image
	if (club_pro_img!=undefined) {
		if (club.pro!=undefined && club.pro.length>0)
			club_pro_img.src='/images/clubs/' + club.hostname + '_pro.jpg';
		else if(/landing.html/.test(document.location) && club.clubImage)
			club_pro_img.src='/images/clubs/' + club.hostname + '_club.jpg';
		else club_pro_img.style.display='none';
	}
	
	// replace the club image
	if (club_img!=undefined && club.clubImage) club_img.src='/images/clubs/' + club.hostname + '_club.jpg';

	// pro or club image
	if (pro_or_club_img!=undefined) {
		if (club.pro.length>0) pro_or_club_img.src='/images/clubs/' + club.hostname + '_pro.jpg';
		else if (club.clubImage) pro_or_club_img.src='/images/clubs/' + club.hostname + '_club.jpg';
	}

	// replace the welcome message
	var wmt = document.getElementById('welcome_message_text');
	if (wmt!=undefined && club.welcome_message!=undefined && club.welcome_message.length>0) 
		wmt.innerHTML = club.welcome_message;
		setElementText('welcome_message',club.welcome_message);

//jda
//	if (club.logofull!=undefined && club.logofull.length>0) club_logo_full_img.src='/images/clubs/' + club.hostname + '_logo_full.jpg';
//		else  club_logo_full_img.src='/images/clubs/' + club.hostname + '_logo.jpg';

}

function setElementText(id,text) {
	var e = document.getElementById(id);
	if (e!=undefined) e.innerHTML = text;
}

function init_index() {
	var hostname = /^http:\/\/(.+).clubstore.org/.exec(document.location);
	var club = getClub();
	if (club!=undefined) document.location.href = 'landing.html';
}
