//-----------------------------------------------------------------------------
// Copyright (c)2005-2007, Summerleaze Computer Services
// $Revision: 1.1 $
//-----------------------------------------------------------------------------

var errors = 0;
var debug  = 0;

var classname = "";
var submitting = false;

/*
tickerPreload("images/ticker/timeline.jpg");
tickerPreload("images/ticker/timeline_over.jpg");
tickerPreload("images/ticker/academy.jpg");
tickerPreload("images/ticker/academy_over.jpg");
tickerPreload("images/ticker/gloves.jpg");
tickerPreload("images/ticker/gloves_over.jpg");
tickerPreload("images/ticker/keepers.jpg");
tickerPreload("images/ticker/keepers_over.jpg");
tickerPreload("images/ticker/download.jpg");
tickerPreload("images/ticker/download_over.jpg");
tickerPreload("images/ticker/contact.jpg");
tickerPreload("images/ticker/contact_over.jpg");
tickerPreload("images/ticker/buy.jpg");
tickerPreload("images/ticker/buy_over.jpg");
tickerPreload("images/ticker/glovecuts.jpg");
tickerPreload("images/ticker/glovecuts_over.jpg");
tickerPreload("images/ticker/palm.jpg");
tickerPreload("images/ticker/palm_over.jpg");
*/

$(document).ready(function() {
	$('.hidden').hide();

	$('.tickerbox').hover(function() {
		var img = $(this).find('img');
		tickerMouseOver = true;
		img.attr('src', img.attr('src').replace(/(\w+).jpg/, "$1_over.jpg"));
	}, function() {
		var img = $(this).find('img');
		tickerMouseOver = false;
		img.attr('src', img.attr('src').replace(/(\w+)_over.jpg/, "$1.jpg"));
	});

	initTicker('#ticker-main');

	$('#home-slide2').cycle({
    fx:      'scrollLeft',
    speed:    300,
    timeout:  5000
	});

	setInterval(dispatch, 50);
});

//-----------------------------------------------------------------------------
// Only want 1 timer running, so use a 'dispatcher'

var ticks = 0;
function dispatch() {
	updateTicker('#ticker-main');
	if(++ticks % 100 == 0) {
		// Every 5 seconds
		ticks = 0;
	}
}

//-----------------------------------------------------------------------------
// Mouseover Functions...

/*
function _over(el) {
	el.src = el.src.replace(/(\w+).jpg/, "$1_over.jpg");
}

function _out(el) {
	el.src = el.src.replace(/(\w+)_over.jpg/, "$1.jpg");
}
*/

function _over(el, img) {
	var obj = el;

	if (!img) img = "$1_over.jpg";
	if(typeof(el) == "string") {
		obj = document.getElementById(el);
	}
	obj.src = obj.src.replace(/(\w+).jpg/, img);
}

function _out(el, img) {
	var obj = el;
	var regex = /(\w+).jpg/;

	if (!img) {
		img = "$1.jpg";
		regex = /(\w+)_over.jpg/;
	}
	if(typeof(el) == "string") {
		obj = document.getElementById(el);
	}
	obj.src = obj.src.replace(regex, img);
}

function scrollRight(el) {
	var content = el.parentNode.getElementsByTagName('div')[0];
}

//-----------------------------------------------------------------------------
// Image Loop

var imgs =
[
	["PRO MEGA PRIDE",             "gloves.php?id=pro_mega_pride", "images/news/pro_mega_pride.jpg"],
	["HELTON - FC PORTO & BRAZIL", "keepers.php?id=helton",        "images/news/helton2.jpg"],
	["HO SOCCER PATENTS",          "gloves.php?id=elite_pro_rf",   "images/news/patents.jpg"],
	["MAX JOHNSON, NEWCASTLE",     "keepers.php?id=johnson",       "images/news/johnson.jpg"]
];

var cuts =
[
	["FLAT", "#EB7D00", "images/cuts/flat.jpg", "Traditional cut with a flat palm construction, offering a larger contact surface."],
	["NEGATIVE", "#007DC6", "images/cuts/negative.jpg", "This is a cut with internal seams, that provide a tighter fit and better contact with the ball. Also gussets are in latex, offering a larger grip area"],
	["SPECIAL NEGATIVE", "#007DC6", "images/cuts/negative_special.jpg", "This EXCLUSIVE cut is used in <a href=\"#\">ENIGMA gen 4</a>. Construction is same as negative cut but thumb has seperate piece design that assists the natural hand motion."],
	["R-NEGATIVE", "#EB7D00", "images/cuts/r_negative.jpg", "This EXCLUSIVE is similar to Negative Cut with internal seams, but also latex is rolled onto the tip of the fingers, offering extra traction around them."],
	["ROLLFINGER", "white", "images/cuts/rollfinger.jpg", "In this roll finger cut latex palm envelopes the fingers. This ensures constant ball contact and guarantees stability with the ball."],
];

var i = 0;
var j = 0;
var k = 0;
var l = 0;
var timer;
var ticker;
var loopDelay = 5000;

function cutsLoop(id) {
	var obj = document.getElementById(id);
	var img = obj.getElementsByTagName('img')[0];
	var h1  = obj.getElementsByTagName('h1')[0];
	var p   = obj.getElementsByTagName('p')[0];

	h1.innerHTML   = cuts[l][0];
	h1.style.color = cuts[l][1];
	img.src        = cuts[l][2];
	p.innerHTML    = cuts[l][3];

	if(++l >= cuts.length) l = 0;
	setTimeout("cutsLoop('"+id+"')", loopDelay);
}

function imageLoop(id) {
	var obj = document.getElementById(id);
	var img = obj.getElementsByTagName('img')[0];

	obj.lastChild.nodeValue = imgs[k][0];
	obj.href = imgs[k][1];
	img.src  = imgs[k][2];

	if(++k >= imgs.length) k = 0;
	setTimeout("imageLoop('"+id+"')", loopDelay);
}

function newsLoop(id) {
	var obj = document.getElementById(id);
	var img = obj.getElementsByTagName('img')[0];

	obj.lastChild.nodeValue = news[i][0];
	obj.href = news[i][1];
	obj.target = news[i][2];
	img.src  = news[i][3];

	// Update the news story...
	j = 0;
	ticker = setInterval("addCharacter('"+news[i][4]+"','"+id+"','stop-press',"+news[i][5]+")", 50);

	if(++i >= news.length) i = 0;
}

function addCharacter(story, main, id, delay) {
	var div = document.getElementById(id).getElementsByTagName('div')[0];

	div.innerHTML = story.substr(0,j) + '_';
	if(++j > story.length) {
		div.innerHTML = story;
		clearInterval(ticker);
		timer = setTimeout("newsLoop('"+main+"')", delay);
	}
}


//-----------------------------------------------------------------------------


//window.onload = initMenu;
function setBack(id, img) {
	var obj = document.getElementById(id);
	var oPic = new Image;

	oPic.src = img;
	alert(obj.clientHeight + " -=- " + oPic.height);

	obj.style.background = "url('" + img + "')";
	if(obj.height < oPic.height) {
		obj.style.height = opic.height;
	}
}

// Function included in code because including a JS src file seems to crash IE6...
function imgWindow(img,txt) {
	// Load a full size copy of the image so we can work out the correct size to use...
	var oPic = new Image;
	oPic.src = img.src;

	w = open("",'image','width=600,height=600,toolbar=no,scrollbars=no,resizable=yes');
	w.document.write("<HTML><HEAD><TITLE>:: ZOOM :: " + txt + "</TITLE></HEAD>");
	w.document.write("<SCRIPT type=\"text/javascript\">");
	w.document.write("function checksize(w,h) { ");
	w.document.write("	if (document.images[0].complete) {  ");
	w.document.write("		window.resizeTo(document.images[0].width+w, document.images[0].height+h); ");
	w.document.write("		window.focus();");
	w.document.write("	} ");
	w.document.write("	else { ");
	w.document.write("		setTimeout('checksize()',100);");
	w.document.write("	}");
	w.document.write("}");
	w.document.write("</SCRIPT>");
	w.document.write("<BODY onload='checksize(50,50)' leftMargin=0 topMargin=0 marginwidth=0 marginheight=0>");
	w.document.write("<IMG src='" + img.src + "' width=" + oPic.width/2 + " border=0>");
//	w.document.write("<IMG src='" + img.src + "' border=0>");
	w.document.write("</BODY></HTML>");
	w.document.close();
}

// Function included in code because including a JS src file seems to crash IE6...
function imgPopup(img,txt) {
	w=open("",'image','width=600,height=600,toolbar=no,scrollbars=no,resizable=yes');
	w.document.write("<HTML><HEAD><TITLE>"+txt+"</TITLE></HEAD>");
	w.document.write("<SCRIPT type=\"text/javascript\">");
	w.document.write("function checksize(w,h) { ");
	w.document.write("	if (document.images[0].complete) {  ");
	w.document.write("		window.resizeTo(document.images[0].width+w, document.images[0].height+h); ");
	w.document.write("		window.focus();");
	w.document.write("	} ");
	w.document.write("	else { ");
	w.document.write("		setTimeout('checksize()',100);");
	w.document.write("	}");
	w.document.write("}");
	w.document.write("</SCRIPT>");
	w.document.write("<BODY onload='checksize(12,30)' leftMargin=0 topMargin=0 marginwidth=0 marginheight=0>");
	w.document.write("<BODY>");
	w.document.write("<IMG src='"+img+"' border=0>");
	w.document.write("</BODY></HTML>");
	w.document.close();
}

function checksize(w,h) {
	if (document.images[0].complete) {
		window.resizeTo(document.images[0].width+w, document.images[0].height+h);
		window.focus();
	}
	else {
		setTimeout('checksize()',100);
	}
}

function openWindow(address,name) {
	window.open(address, name, "menubar=1,resizable=1");
}


/*
function imgWindow(txt,img) {
	w = open("",'image','width=600,height=600,toolbar=no,scrollbars=no,resizable=yes');
	w.document.write("<HTML><HEAD><TITLE>ZOOM :: " + txt + "</TITLE></HEAD>");
	w.document.write("<SCRIPT type=\"text/javascript\" src=\"functions.js\"></SCRIPT>");
	w.document.write("<BODY onload='checksize()' leftMargin=0 topMargin=0 marginwidth=0 marginheight=0>");
	w.document.write("<IMG src='"+img+"' width=\"75%\" border=0>");
	w.document.write("</BODY></HTML>");
	w.document.close();
}

function checksize() {
	if (document.images[0].complete) {
		window.resizeTo(document.images[0].width+50,document.images[0].height+50);
		window.focus();
	}
	else {
		setTimeout('checksize()',100);
	}
}
*/

function showpage(n) {
	var page = document.getElementById("page"+n);

	// Hide all the pages...
	var aPages = document.getElementsByTagName("div");
	for(var i=0; i<aPages.length; i++) {
		if (aPages[i].className == "page") {
			aPages[i].style.display = "none";
		}
	}
	page.style.display = "block";

/*
	// Add an arrow background to the selected button
	var j = 1;
	var aLinks = document.getElementsByTagName("a");
	for(var i=0; i<aLinks.length; i++) {
		//alert(i + ": " + aLinks[i].className + " - " + n);
		if (aLinks[i].className == "pagebutton") {
			alert(aLinks[i].className + " - " + aLinks[i].style.background);
			if(j==n) {
				aLinks[i].style.background = "url(images/arrow.gif) 100% 50% no-repeat";
			}
			else {
				aLinks[i].style.background = "";
			}
			++j;
		}
	}
*/
}

var current_player = 0;

function next_player() {
	++current_player;
	playermenu();
}

function prev_player() {
	--current_player;
	playermenu();
}

function playermenu() {
	var aPlayers = document.getElementsByTagName("li");
	var nPlayers = aPlayers.length - 1;

	if(current_player<0) current_player = 0;
	if(current_player>aPlayers.length-3) current_player = nPlayers-2;

	for(var i=0; i<aPlayers.length; i++) {
		aPlayers[i].style.display = "none";
		if(i>=current_player && i<current_player+3) {
			aPlayers[i].style.display = "block";
		}
	}
}

function showplayer(el) {
	var aPlayers = document.getElementsByTagName("li");

	// use first <li> if none is specified
	if(el == undefined) el = aPlayers[0];

	for(var i=0; i<aPlayers.length; i++) {
		// reset all the players photo's to B/W
		aPlayers[i].firstChild.src = aPlayers[i].firstChild.src.replace(/(\w+)_over.jpg/, "$1.jpg");
		if(el == aPlayers[i]) {
			// Build list of all the player pages
			var aPages = document.getElementById("player-pages").childNodes;

			// Hide them all
			for(var j=0; j<aPages.length; j++) {
				if(aPages[j].nodeName == "DIV") aPages[j].style.display = "none";
			}

			// Display the selected player's page
			var k = 0;
			for(var j=0; j<aPages.length; j++) {
				if(aPages[j].nodeName == "DIV") {
					if(k == i) {
						aPages[j].style.display = "block";
					}
					++k;
				}
			}
		}
	}
	// make selected player's photo colour
	el.firstChild.src = el.firstChild.src.replace(/(\w+).jpg/, "$1_over.jpg");
}



function setimg(o, i) {
	//alert(i);
	o.src = i;
}

function nav(v) {
	var view = document.getElementById('view');

	view.value = v;
	document.form.submit();
}

function doHover(el) {
	el.className = "nav_hover";
}

function noHover(el) {
	el.className = "";
}

function submitForm() {
	submitting = true;
	document.form.submit();
}

function backTo(page) {
	if (!submitting) {
		window.location = page.length > 0 ? page : "diary.php";
	}
	submitting = false;
}

function gotoPage(page) {
	window.location = page;
}

function showForm(form, params) {
	window.location = form + params;
}

function changeClass(el, sClass) {
	el.className = sClass;
}

function initMenu() {
	if (document.all && document.getElementById) {
		var navRoot = document.getElementById('dmenu');
		if (navRoot) {
			for(i=0; i<navRoot.childNodes.length; i++) {
				var node = navRoot.childNodes[i];
				if (node.nodeName == 'LI') {
					node.onmouseover = function() { this.className += ' over'; }
					node.onmouseout  = function() { this.className = this.className.replace(' over', ''); }
				}
			}
		}
	}
}

function menuOver(el) {
	for (i=0; i<el.childNodes.length; i++) {
		node = el.childNodes[i];
		if (node.nodeName=="LI") {
			node.className = "over";
		}
	}
}

function menuOut(el) {
	for (i=0; i<el.childNodes.length; i++) {
		node = el.childNodes[i];
		if (node.nodeName=="LI") {
			node.className = "";
		}
	}
}

function toggleMenu(el) {
	if (document.getElementById) {
		var mStyle = document.getElementById(el).style;
		if (mStyle.display == "block") {
			mStyle.display = "none";
		}
		else {
			mStyle.display = "block";
		}
		return false;
	}
	else {
		return true;
	}
}

function confirmChk(elem) {
	var chkbox = 0;

	// figure out which boxes are ticked
	for (var i=0; i<document.form.length; i++) {
		var el = document.form.elements[i];

		if (el.type == 'checkbox' && el.checked == true) {
			chkbox++;
		}
	}

	// Set value of a hidden input element - gives us a POST variable to trigger the operation
	switch(elem.name) {
		case 'delete':
			var msg = "Are you sure you want to delete the " +
				(chkbox == 1 ? "" : chkbox + " ") + "selected " +
				(chkbox == 1 ? "entry" : "entries");
			if (chkbox > 0 && confirm(msg)) {
				document.getElementById('operation').value = 'delete';
				submitForm();
			}
			break;
		case 'edit':
			document.getElementById('operation').value = 'edit';
			submitForm();
			break;
	}
}

function confirmDelete() {
	var chkbox = 0;
	var valid = true;

	for (var i=0; i<document.form.length; i++) {
		var el = document.form.elements[i];

		if (el.type == 'checkbox' && el.checked == true) {
			chkbox++;
		}
	}

	if (chkbox > 0 && confirm("Are you sure you want to delete the selected entries")) {
		// Set value of a hidden input element - gives us a POST variable to trigger the deletion(s)
		document.getElementById('delete').value = 'delete';
		submitForm();
	}
}

function confirmAcknowledge() {
	var chkbox = 0;
	var valid = true;

	for (var i=0; i<document.Form.length; i++) {
		var el = document.form.elements[i];

		if (el.type == 'checkbox' && el.checked == true) {
			chkbox++;
		}
	}

	if (chkbox > 0 && confirm("Are you sure you want to acknowledge the selected entries")) {
		// Set value of a hidden input element - gives us a POST variable to trigger the Acknowledgment(s)
		document.getElementById('acknowledge').value = 'acknowledge';
		submitForm();
	}
}

function sendEmail(el) {
	var email = prompt("Please enter the email address of the recipient(s)\n(seperate entries with a comma)");
	var isValid = false;

	if (email != null) {
		if (email.indexOf(',') != -1) {
			isValid = validateEmailList(email);
		}
		else {
			isValid = validateEmail(email);
		}
		if (isValid) {
			el.href += "?email=" + email;
		}
	}
}

function showTitle(elem) {
	var pattern = /^(\w+)_(\d+)$/

	if (pattern.test(elem.id)) {
		var name    = RegExp.$1;
		var journey = RegExp.$2;
	}
	else {
		alert("No Pattern Match!");
	}

	var limit = parseInt(document.getElementById('limit_'+elem.id).value);

	switch(name) {
	case 'total':
		var s = 'Passengers';
		break;
	case 'wheelchairs':
		var s = 'Passengers with Wheelchairs';
		break;
	case 'bicycles':
		var s = 'Passengers with Bicycles';
		break;
	}

	elem.title = isNaN(limit) ? '' : s + ' (Limit ' + limit + ')';
}

// Displays all the form's elements...
function showDOM() {
	for (var i=0; i<document.party.length; i++) {
		var el = document.party.elements[i];

		document.getElementById('debug').innerHTML += "el[" + i + "] => " + el.type + ", " + el.id + "<br />";
	}
}

// toggle the 'disabled' state for each argument
function toggleElement() {
	var state = (arguments[0].value.length == 0);
	for(i=1; i<arguments.length; i++) {
		document.getElementById(arguments[i]).disabled = state;
	}
}

function disableElement() {
	for(i=0; i<arguments.length; i++) {
		document.getElementById(arguments[i]).disabled = true;
	}
}

function enableElement() {
	for(i=0; i<arguments.length; i++) {
		document.getElementById(arguments[i]).disabled = false;
	}
}

//**************************************************************************************************
// Toggle the state of a panel
//**************************************************************************************************
function panelToggle(elem) {
	if (elem.className == "collapsed") {
		elem.className = "expanded";
	}
	else {
		elem.className = "collapsed";
	}
	// Expand / Collapse the list
	lists = document.getElementsByTagName('ul');
	if (lists.length > 0) {
		for (var i = 0; i < lists.length; i++) {
			el = lists[i];
			if (el.id == elem.name) {
				if (el.className == "collapsed") {
					el.className = "expanded";
				}
				else {
					el.className = "collapsed";
				}
			}
		}
	}
	divs = document.getElementsByTagName('div');
	if (divs.length > 0) {
		for (var i = 0; i < divs.length; i++) {
			el = divs[i];
			if (el.id == elem.name) {
				if (el.className == "collapsed") {
					el.className = "expanded";
				}
				else {
					el.className = "collapsed";
				}
			}
		}
	}
}


//**************************************************************************************************
// Useful Debugging Functions
//**************************************************************************************************

function DebugMsg(txt) {
	if(debug) $('#debug')[0].innerHTML += txt+"<br />";
}

function showValue(el) {
	el.title = el.name + ' = ' + el.value;
}

function showClass(el) {
	el.title = el.id + ' = ' + el.className;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr;

  for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document;

  if(d.images){
  	if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
    for(i=0; i<a.length; i++)
    	if (a[i].indexOf("#")!=0){
    		d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];
    	}
   }
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments;

  document.MM_sr = new Array;

  for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null) {
   	document.MM_sr[j++]=x;
   	if(!x.oSrc) x.oSrc=x.src;
   	x.src=a[i+2];
   }
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
