/**
 * @author nbalsaras
 */

$(document).ready(function() {
	addControls();
	selfLabel('login');
	selfLabel('search');
	selfLabel('findClub');
	addUserControls();
	//addTicker();
	addGalleryCar('galleryCar');
	//subNavSetup();
	setupTabs('events');
	setupTabs('moreNews');
	imgGal();
	descs();
	addAccordion();
});

window.onload =	function() {
}

function addControls(){
	var myTools;
	$('ul.tools').each(function(){
		myTools = this.getElementsByTagName('li')[1];
	})
	if (myTools != null) {
		var toolsFrag = document.createDocumentFragment();
		
		li = document.createElement('li');
		li.innerHTML = '<a href="#" onclick="window.print();">Print</a>';
		toolsFrag.appendChild(li);
		
		li = document.createElement('li');
		li.className = 'rFont'
		li.innerHTML = '<a href="#" onclick="setFontSize(\'0.8em\');"><span>&nbsp;</span>Regular fonts</a>';
		toolsFrag.appendChild(li);
		
		li = document.createElement('li');
		li.className = 'lFont'
		li.innerHTML = '<a href="#" onclick="setFontSize(\'0.9em\');"><span>&nbsp\;</span>Large fonts</a>';
		toolsFrag.appendChild(li);
		
		li = document.createElement('li');
		li.className = 'xlFont'
		li.innerHTML = '<a href="#" onclick="setFontSize(\'1.05em\');"><span>&nbsp\;</span>Extra Large fonts</a>';
		toolsFrag.appendChild(li);
		
		myTools.parentNode.insertBefore(toolsFrag, myTools);
	}
}

function setFontSize(size) {
	document.body.style.fontSize = size;
	$.cookie("font-size", size, { expires: 355 });
}

var userControls;
function addUserControls() {
	userControls = document.getElementById('userControls');
	userControls.toggle = document.getElementById('userControlsToggle');
	userControls.myState = 'hidden';
	userControls.myHeight = userControls.offsetHeight - userControls.toggle.offsetHeight;
	userControls.top = -userControls.myHeight;
	userControls.speed = 10;

	userControls.style.top = userControls.top + 'px';
	userControls.onmouseover = function(){
		clearTimeout(userControls.timeout);
		if (userControls.myState == 'hidden') {
			userControls.myState = 'showing';
			$(userControls).animate({top: "0px"}, 200, function(){userControls.myState = 'shown';});
		}
	}
	userControls.onmouseout = function(){
		userControls.timeout = setTimeout(function(){
		if (userControls.myState == 'shown') {
			userControls.myState = 'hidding';
			$(userControls).animate({top: '-' + userControls.myHeight + 'px'}, 200, function(){userControls.myState = 'hidden';});
		}
		}, 500)
	}
}

var accordion = new Array();
function addAccordion() {
	$('div.accordion').each(function(){
		thisIndex = accordion.length;
		accordion[thisIndex] = this;
		accordion[thisIndex].toggle = new Array();
		accordion[thisIndex].body = new Array();

		var toggleID;
		accordion[thisIndex].open = 'false';
		
		$(this).children('.acToggle, .acBody').each(function(){
			if(this.className.match('acToggle')){
				toggleID = accordion[thisIndex].toggle.length;
				accordion[thisIndex].toggle[toggleID] = this;
				accordion[thisIndex].toggle[toggleID].myID = thisIndex;
				accordion[thisIndex].toggle[toggleID].style.position = 'relative';
				accordion[thisIndex].toggle[toggleID].toggleID = toggleID;
				
				accordion[thisIndex].toggle[toggleID].icon = document.createElement('img');
				accordion[thisIndex].toggle[toggleID].icon.src = '/_fuse/801/_/bwRight.gif';
				accordion[thisIndex].toggle[toggleID].icon.alt = '';
				accordion[thisIndex].toggle[toggleID].icon.style.position = 'absolute';
				accordion[thisIndex].toggle[toggleID].icon.style.top = '10px';
				accordion[thisIndex].toggle[toggleID].icon.style.right = '10px';
				accordion[thisIndex].toggle[toggleID].icon.style.height = '8px';
				accordion[thisIndex].toggle[toggleID].icon.style.width = '4px';
				accordion[thisIndex].toggle[toggleID].icon.style.margin = '0px';
				accordion[thisIndex].toggle[toggleID].appendChild(accordion[thisIndex].toggle[toggleID].icon);
			}
			else {
				accordion[thisIndex].body[toggleID] = this;
				//accordion[thisIndex].toggle[toggleID].targetHeight = accordion[thisIndex].body[toggleID].offsetHeight;
			}
		})
		
		for (j = 0; j < accordion[thisIndex].toggle.length; j++) {
			accordion[thisIndex].toggle[j].state = '0';
			accordion[thisIndex].toggle[j].animation = 'false';
			//accordion[thisIndex].toggle[j].speed = 5;
			//accordion[thisIndex].toggle[j].height = 0;

			accordion[thisIndex].toggle[j].style.cursor = 'pointer';
			$(accordion[thisIndex].body[j]).hide();
			if(j == accordion[thisIndex].toggle.length - 1){
				accordion[thisIndex].toggle[j].style.marginBottom = '0px';
			}
			
			accordion[thisIndex].toggle[j].onclick = function(){
				if (this.state == '0') {
					accordionShow(this);
					this.state = '1';
				}
				else if (this.state == '1') {
					accordionHide(this);
					accordion[this.myID].open = 'false';
					this.state = '0';
				}
				return false;
			}
		}
		if (accordion[thisIndex].className.match('open')){
			accordionShow(accordion[thisIndex].toggle[0]);
			accordion[thisIndex].toggle[0].state = '1';
		}
	})
}

function accordionShow(t){
	if (accordion[t.myID].open != 'false') {
		accordion[t.myID].toggle[accordion[t.myID].open].state = '0';
		accordionHide(accordion[t.myID].toggle[accordion[t.myID].open]);
	}	

	t.animation = 'true';
	t.icon.src = '/_fuse/801/_/bwDown.gif';
	t.icon.style.width = '8px';
	t.icon.style.height = '4px';
	t.icon.style.top = '12px';
	t.icon.style.right = '8px';
	$(accordion[t.myID].body[t.toggleID]).slideDown(150, function(){
			accordion[t.myID].open = t.toggleID;
			t.animation = 'false';
	});
}

function accordionHide(t){
	t.animation = 'true';
	t.icon.src = '/_fuse/801/_/bwRight.gif';
	t.icon.style.top = '10px';
	t.icon.style.right = '10px';
	t.icon.style.height = '8px';
	t.icon.style.width = '4px';
	$(accordion[t.myID].body[t.toggleID]).slideUp(150, function(){
		t.animation = 'false';
	});
}

var newsTicker;
function addTicker(){
	if(document.getElementById('newsTicker') == null) return false;
	newsTicker = document.getElementById('newsTicker').getElementsByTagName('*')[0];
	newsTicker.innerHTML = newsTicker.innerHTML.replace(/ (?!href=)(?!\>)/g, '&nbsp;')
	newsTicker.width = newsTicker.offsetWidth;
	newsTicker.left = 280;
	newsTicker.speed = 5/100; // pixels per second / 100  (to get into pixels per millisecond)
	newsTicker.time = (newsTicker.width+280)/newsTicker.speed; 
	//newsTicker.animation = animate.length;
	
	newsGo(newsTicker.time-(280/newsTicker.speed));
	newsTicker.animation = 'true';
	/*animate[animate.length] = function(){
		if(newsTicker.left > -newsTicker.width){
			newsTicker.left = newsTicker.left - 1;
		}
		else {
			newsTicker.left = 280;
		}
		newsTicker.style.left = newsTicker.left + 'px';
	}*/
	
	newsTicker.onmouseover = function() {
		$(newsTicker).stop();
		newsTicker.animation = 'false';
		/*if(newsTicker.animation != 'false'){
			animate.splice(newsTicker.animation, 1);
			animate[newsTicker.animation] = null;
			newsTicker.animation = 'false';
		}*/
	}
	newsTicker.onmouseout = function() {
		if (newsTicker.animation == 'false') {
			newsGo((newsTicker.width + newsTicker.offsetLeft) / newsTicker.speed);
			newsTicker.animation = 'true';
		}
		/*newsTicker.animation = animate.length;
		animate[animate.length] = function(){
			if(newsTicker.left > -newsTicker.width){
				newsTicker.left = newsTicker.left - 1;
			}
			else {
				newsTicker.left = 280;
			}
			newsTicker.style.left = newsTicker.left + 'px';
		}*/
	}
}

function newsGo(time){
	$(newsTicker).animate({left: '-' + newsTicker.width + 'px'}, time, 'linear', function(){
		newsTicker.style.left = '280px'
		newsGo(newsTicker.time);
	})
}

var slForm = new Array();
function selfLabel(formName){
	thisIndex = slForm.length;
	slForm[thisIndex] = document.getElementById(formName);
	if(slForm[thisIndex] == null) return false;
	slForm[thisIndex].input = new Array();
	inputs = slForm[thisIndex].getElementsByTagName('input');
	for(i = 0; i < inputs.length; i++){
		if(inputs[i].className.match('text') || inputs[i].className.match('password')){
			inputIndex = slForm[thisIndex].input.length;
			slForm[thisIndex].input[inputIndex] = inputs[i];
			slForm[thisIndex].input[inputIndex].label = inputs[i].parentNode.getElementsByTagName('label')[0];
			
			inputs[i].parentNode.style.position = 'relative';
						
			slForm[thisIndex].input[inputIndex].label.style.position = 'absolute';
			slForm[thisIndex].input[inputIndex].label.style.display = 'block';
			slForm[thisIndex].input[inputIndex].label.style.cursor = 'text';
			slForm[thisIndex].input[inputIndex].label.style.top = '1px';
			slForm[thisIndex].input[inputIndex].label.style.left = '8px';
			
			slForm[thisIndex].input[inputIndex].onfocus = function(){
				this.label.style.display = 'none';
			}
			slForm[thisIndex].input[inputIndex].onblur = function(){
				if(this.value == ''){
					this.label.style.display = 'block';
				}
			}
		}
	}
	
}

var galCar = new Array();
function addGalleryCar(gcID) {
	thisIndex = galCar.length;
	galCar[thisIndex] = document.getElementById(gcID);
	if(galCar[thisIndex] == null) return false;
	galCar[thisIndex].wrapper = galCar[thisIndex].getElementsByTagName('div')[0];
	galCar[thisIndex].galLength = galCar[thisIndex].getElementsByTagName('img').length;
	galCar[thisIndex].pos = 0;
	galCar[thisIndex].left = 0;
	galCar[thisIndex].animate = 'false';
	
	galCar[thisIndex].parentNode.style.position = 'relative';
	
	galCar[thisIndex].backButton = document.createElement('span');
	galCar[thisIndex].backButton.innerHTML = 'back';
	galCar[thisIndex].backButton.className = 'back';
	galCar[thisIndex].backButton.style.display = 'none';
	galCar[thisIndex].backButton.item = thisIndex;
	galCar[thisIndex].parentNode.appendChild(galCar[thisIndex].backButton);
	
	galCar[thisIndex].nextButton = document.createElement('span');
	galCar[thisIndex].nextButton.innerHTML = 'next';
	galCar[thisIndex].nextButton.className = 'next';
	galCar[thisIndex].nextButton.item = thisIndex;
	// Hide the next arrow if we only have 3 or less items
	if(galCar[thisIndex].galLength <= 3) { 
		galCar[thisIndex].nextButton.style.display = 'none';	
	}
	galCar[thisIndex].parentNode.appendChild(galCar[thisIndex].nextButton);
	
	galCar[thisIndex].width = (82*galCar[thisIndex].galLength);
	galCar[thisIndex].wrapper.style.width = galCar[thisIndex].width + 10 + 'px';
	
	galCar[thisIndex].nextButton.onclick = function(){
		i = this.item;
		galCar[i].pos = galCar[i].pos + 82;
		//alert('pos='+galCar[i].pos+' >= '+(galCar[i].width - 82*3));
		if(galCar[i].pos >= (galCar[i].width - 82*3) ) {
			galCar[i].pos = galCar[i].width - 82*3;
			galCar[i].nextButton.style.display = 'none';	
			galCar[i].backButton.style.display = 'block';	
		}
		else {
			galCar[i].backButton.style.display = 'block';	
		}
		
		if (galCar[i].animate == 'false') {
			galCar[i].animate = 'true';
			$(galCar[i].wrapper).animate({left: '-' + galCar[i].pos + 'px'}, 100, 'linear', function(){
				galCar[i].animate = 'false'
			})
			/*animate[animate.length] = function(){
				galCar[i].left += 20;
				if (galCar[i].pos < galCar[i].left) {
					galCar[i].left = galCar[i].pos;
					animate[galCar[i].animate] = null;
					galCar[i].animate = 'false'
				}
				galCar[i].wrapper.style.left = -galCar[i].left + 'px';
			}*/
		}
		else if(galCar[i].animate == 'true'){
			$(galCar[i].wrapper).stop();
			$(galCar[i].wrapper).animate({left: '-' + galCar[i].pos + 'px'}, 100, 'linear', function(){
				galCar[i].animate = 'false'
			})
		}
	}
	
	galCar[thisIndex].backButton.onclick = function(){
		i = this.item;
		galCar[i].pos = galCar[i].pos - 82;
		if(galCar[i].pos <= 0) {
			galCar[i].pos = 0;
			galCar[i].backButton.style.display = 'none';
			galCar[i].nextButton.style.display = 'block';
		}
		else {
			galCar[i].nextButton.style.display = 'block';
		}
		
		if (galCar[i].animate == 'false') {
			galCar[i].animate = 'true';
			$(galCar[i].wrapper).animate({left: '-' + galCar[i].pos + 'px'}, 100, 'linear', function(){
				galCar[i].animate = 'false'
			})
			/*animate[animate.length] = function(){
				galCar[i].left -= 20;
				if (galCar[i].pos > galCar[i].left) {
					galCar[i].left = galCar[i].pos;
					animate[galCar[i].animate] = null;
					galCar[i].animate = 'false'
				}
				galCar[i].wrapper.style.left = -galCar[i].left + 'px';
			}*/
		}
		else if(galCar[i].animate == 'true'){
			$(galCar[i].wrapper).stop();
			$(galCar[i].wrapper).animate({left: '-' + galCar[i].pos + 'px'}, 100, 'linear', function(){
				galCar[i].animate = 'false'
			})
		}
	}
}

/*var subNavDrops = new Array();
function subNavSetup() {
	var lists = document.getElementsByTagName('ul');
	for (i = 0; i < lists.length; i++){
		if(lists[i].className.match('subNav') || lists[i].className.match('nav')){
			var listItems = lists[i].getElementsByTagName('li');
			for (j = 0; j < listItems.length; j++){
				var drops = listItems[j].getElementsByTagName('ul')
				if(drops.length > 0){
					k = subNavDrops.length;
					subNavDrops[k] = listItems[j];
					subNavDrops[k].drop = drops[0];
					subNavDrops[k].drop.style.overflow = 'hidden';
					subNavDrops[k].targetHeight = subNavDrops[k].drop.offsetHeight -5;
					subNavDrops[k].height = 0;
					subNavDrops[k].drop.style.height = '0px';
					subNavDrops[k].animate = 'false';
					
					subNavDrops[k].onmouseover = function(){
						var t = this;
						if(this.animate != 'false'){
							animate[this.animate] = null;
						}
						this.animate = animate.length;
						this.drop.style.visibility = 'visible';
						animate[animate.length] = function(){
							t.height += 50;
							if(t.height >= t.targetHeight){
								t.height = t.targetHeight;
								animate[t.animate] = null;
								t.animate = 'false';
							}
							t.drop.style.height = t.height + 'px';
						}
					}
					subNavDrops[k].onmouseout = function(){
						t = this;
						if(this.animate != 'false'){
							animate[this.animate] = null;
						}
						this.animate = animate.length;
						animate[animate.length] = function(){
							t.height -= 200;
							if(t.height <= 0){
								t.height = 0;
								t.drop.style.visibility = 'hidden';
								animate[t.animate] = null;
								t.animate = 'false';
							}
							t.drop.style.height = t.height + 'px';
						}
					}
				}
			}
			i = lists.length;
		}
	}
	delete listItems;
	delete lists;
}*/

var tabSet = new Array();

function setupTabs(tabBlock) {
	id = tabSet.length;
	tabSet[id] 				= document.getElementById(tabBlock);
	if(tabSet[id] == null) return false;
	tabSet[id].nav 			= new Array();
	tabSet[id].block 		= new Array();	
		
	divs 					= tabSet[id].getElementsByTagName('div');
	for(i = 0; i < divs.length; i++){
		if(divs[i].className.match('tabBlock')){
			tabSet[id].block[tabSet[id].block.length] = divs[i];
		}
	}
	delete divs;
	
	lists = tabSet[id].getElementsByTagName('ul');
	for(i = 0; i < lists.length; i++){
		if(lists[i].className.match('nav')){
			tabSet[id].nav = lists[i].getElementsByTagName('a');
			for(j = 0; j < tabSet[id].nav.length; j++){
				if (tabSet[id].nav[j].parentNode.className.match('selected')) {
					//tabSet[id].block[j].style.display = 'block';
				}
				else {
					tabSet[id].block[j].style.display = 'none';
				}
				tabSet[id].nav[j].myID = id;
				tabSet[id].nav[j].j = j;
				tabSet[id].nav[j].onclick = function() {
					for(k = 0; k < tabSet[this.myID].nav.length; k++){
						if (k != this.j) {
							tabSet[this.myID].nav[k].parentNode.className = '';
							tabSet[this.myID].block[k].style.display = 'none';
						}
						else {
							tabSet[this.myID].nav[k].parentNode.className = 'selected';
							tabSet[this.myID].block[k].style.display = 'block';
						}
					}
					return false;
				}
			}
			i = lists.length;
		}
	}
	delete lists;
}

function imgGal(){
	$('div.imageGallery:gt(0)').each(function(){
		this.style.display = 'none';
	});
	$('div.imageGallery h3').each(function(){
		this.style.display = 'none';
	});
	
	$('#galleryNav a').click(function(){
		var thisIndex = $('#galleryNav li').index($(this).parent());
		
		$('#galleryNav li').each(function(){
			if(this.className.match('selected')) this.className = this.className.slice(0, this.className.indexOf("selected"));
		})
		$(this).parent().each(function(){
			this.className = this.className + " selected";
		})
		
		$('div.imageGallery, div.videoGallery').each(function(){
			this.style.display = 'none';
		});
		$('div.imageGallery:eq('+thisIndex+'), div.videoGallery:eq('+thisIndex+')').each(function(){
			this.style.display = 'block';
		})
		return false;
	});
}

function descs(){
	$('#showAssoc, #showClub').each(function(){
		myItem = $(this);
		myItem.children('span').each(function(){this.style.display = ''});
		myItem.children('span').hide();
		myItem.mouseover(function(){
			$(this).children('span').show();
		})
		myItem.mouseout(function(){
			$(this).children('span').hide();
		})
	})
}

/* Animation framework
var animate = new Array();
function animateAll(){
	for(var i = 0; i < animate.length; i++){
		if(animate[i] != null) animate[i]();
	}
}
var timer = setInterval(animateAll,20);*/

function log(msg){
	try{
		console.log(msg);
	}
	catch(err){
		//do nothing
	}
}
