/*
    Global JS behaviours
*/
document.write ('<style type="text/css">#divLogin{display:none;}</style>');

window.addEvent('domready', function(){
	document.getElementById('divLogin').style.display = 'block';	 
	// Login panel
	if ($('loginToggle')) {
		
	    $('divLoginPanel').set('display','none');
		var loginSlyder = new Fx.Slide('divLoginPanel',{duration:650,transition: Fx.Transitions.Pow.easeOut}).hide();
		if ($('divLoginPanel').hasClass('open')) {
			loginSlyder.show();
		}
		$('loginToggle').addEvent('click', function(e) {
			e = new Event(e); loginSlyder.toggle(); e.stop();
			$('divForgotForm').set('opacity',0);
		});
		$('divLoginPanel').set('display','block');
	}
	// Hide all nav
	$$('.ulSubNav').each(function(el) {
		el.set('display','none');
	});
	// Forgotten password panel
	if ($('divForgotForm')) {
		if (!$('divForgotForm').hasClass('open')) $('divForgotForm').set('opacity',0);
		
		$('aForgottenPassword').addEvent('click', function(e) {
			e = new Event(e); $('divForgotForm').fade(1); e.stop();
		});
		$('closeForgottenPassword').addEvent('click', function(e) {
			e = new Event(e); $('divForgotForm').fade(0); e.stop();
		});
	}
	
	if ($('divNavMain')) {
		// Support tab
		var supportSlide = new Fx.Slide('ulSubSupport',{duration:300,transition: Fx.Transitions.Pow.easeOut}).hide();
		$('liNavSupport').addEvent('mouseover',function(){
			supportSlide.cancel();
			supportSlide.slideIn();
		})							   
		$('liNavSupport').addEvent('mouseout',function(){
			supportSlide.cancel();
			supportSlide.slideOut();
		})	
		// Contact tab
		var contactSlide = new Fx.Slide('ulSubContact',{duration:300,transition: Fx.Transitions.Pow.easeOut}).hide();
		$('liNavContact').addEvent('mouseover',function(){
			contactSlide.cancel();
			contactSlide.slideIn();
		})							   
		$('liNavContact').addEvent('mouseout',function(){
			contactSlide.cancel();
			contactSlide.slideOut();
		})	
		// What we do tab
		var whatSlide = new Fx.Slide('ulSubWhat',{duration:300,transition: Fx.Transitions.Pow.easeOut}).hide();
		$('liNavWhat').addEvent('mouseover',function(){
			whatSlide.cancel();
			whatSlide.slideIn();
		})
		$('liNavWhat').addEvent('mouseout',function(){
			whatSlide.cancel();
			whatSlide.slideOut();
		})
		// About us tab
		var aboutSlide = new Fx.Slide('ulSubAbout',{duration:300,transition: Fx.Transitions.Pow.easeOut}).hide();
		$('liNavAbout').addEvent('mouseover',function(){
			aboutSlide.cancel();
			aboutSlide.slideIn();
		})
		$('liNavAbout').addEvent('mouseout',function(){
			aboutSlide.cancel();
			aboutSlide.slideOut();
		})
		// Info centre tab
		var infoSlide = new Fx.Slide('ulSubInfo',{duration:300,transition: Fx.Transitions.Pow.easeOut}).hide();
		$('liNavInfo').addEvent('mouseover',function(){
			infoSlide.cancel();
			infoSlide.slideIn();
		})
		$('liNavInfo').addEvent('mouseout',function(){
			infoSlide.cancel();
			infoSlide.slideOut();
		})
		// Show all
		$$('.ulSubNav').each(function(el) {
			el.set('display','block');
		});
	}
	
});




function kz_swapImage (imgId, imgSrc) {
	// imgId can be img object reference or id string
	if (imgId.indexOf && document.getElementById){
		imgId = document.getElementById(imgId);
	}
	imgId.src = imgSrc;
}
function kz_preloadImages () {
	var args = kz_preloadImages.arguments;
	if (!document.pi) document.pi = new Array();
	var j = document.pi.length;
	for (i=0; i < args.length; i++) {
		document.pi[j] = new Image();
		document.pi[j].src = args[i];
		j++;
	}
}

function kz_clearField (fieldId, defaultVal) {
	if (document.getElementById) {
		var ref = document.getElementById(fieldId)
		if (ref.value == defaultVal) ref.value = '';
	}
}

function confirmDelete (x) {
    var answer = confirm ('Are you sure you want to delete ' + x + '?');
	if (!answer) return false;
	return true;
}
function confirmDeleteUser (x) {
    var answer = confirm ('Are you sure you want to delete ' + x + '?');
	if (!answer) return false;
	return true;
}
function confirmApproveUser (x) {
    var answer = confirm ('Are you sure you want to approve ' + x + '?');
	if (!answer) return false;
	return true;
}
function confirmMakeAdmin (x) {
	var answer = confirm ('Are you sure you want to make ' + x + ' an administrator?');
	if (!answer) return false;
	return true;
}
function confirmRemoveAdmin (x) {
	var answer = confirm ('Are you sure you want to remove ' + x + ' as an administrator (will not delete user)?');
	if (!answer) return false;
	return true;
}

/*
Function: $get
This function provides access to the “get” variable scope + the element anchor

Version: 1.3

Arguments:
key - string; optional; the parameter key to search for in the url’s query string (can also be “#” for the element anchor)
url - url; optional; the url to check for “key” in, location.href is default

Example:
>$get(”foo”,”http://example.com/?foo=bar”); //returns “bar”
>$get(”foo”); //returns the value of the “foo” variable if it’s present in the current url(location.href)
>$get(”#”,”http://example.com/#moo”); //returns “moo”
>$get(”#”); //returns the element anchor if any, but from the current url (location.href)
>$get(,”http://example.com/?foo=bar&bar=foo”); //returns {foo:’bar’,bar:’foo’}
>$get(,”http://example.com/?foo=bar&bar=foo#moo”); //returns {foo:’bar’,bar:’foo’,hash:’moo’}
>$get(); //returns same as above, but from the current url (location.href)
>$get(”?”); //returns the query string (without ? and element anchor) from the current url (location.href)

Returns:
Returns the value of the variable form the provided key, or an object with the current GET variables plus the element anchor (if any)
Returns “” if the variable is not present in the given query string

Credits:
Regex from [url=http://www.netlobo.com/url_query_string_javascript.html]http://www.netlobo.com/url_query_string_javascript.html[/url]
Function by Jens Anders Bakke, webfreak.no
*/
function $get(key,url){
if(arguments.length < 2) url =location.href;
if(arguments.length > 0 && key != ""){
if(key == "#"){
var regex = new RegExp("[#]([^$]*)");
} else if(key == "?"){
var regex = new RegExp("[?]([^#$]*)");
} else {
var regex = new RegExp("[?&]"+key+"=([^&#]*)");
}
var results = regex.exec(url);
return (results == null )? "" : results[1];
} else {
url = url.split("?");
var results = {};
if(url.length > 1){
url = url[1].split("#");
if(url.length > 1) results["hash"] = url[1];
url[0].split("&").each(function(item,index){
item = item.split("=");
results[item[0]] = item[1];
});
}
return results;
}
}

window.addEvent('domready', function() {
									 
	new SmoothScroll({ duration: 600, transition: Fx.Transitions.Quad.easeInOut }, window);


/*
   Fix IE7 clear + float bug
*/

	$$('.newRow').each(function(el) {
		var ie_div = new  Element('div' , {'style':'height:0px;'}).injectBefore(el);
	});
	

/*
    Open external links (beginning with 'http://') in a new window
*/

	if ($(document.body).getProperty('class') != 'sectionSearch') {
		$(document.body).getElements('a[href^=http://]').addEvent('click', function(event){
				event.stop();
				window.open (this.href);
		});
	}
/*
    Open specific links in a new window
*/

	$$('.newWindow').each(function(el) {
		el.addEvent('click', function(e) {
			e.stop();
			window.open (el.href);
		});
	});

/*
    Shrink and expand images
*/
	// for every shrinking image
	$$('.shrinkIt').each(function(el) {
		var startW = el.get('width');
		var startH = el.get('height');
		var shrinkW = el.get('width')/2;
		var shrinkH = el.get('height')/2;
		var isOpen = false;
		// Add instructions
       var js_instructions = new  Element('div').injectAfter(el);
	   js_instructions.set({
			//The 'styles' property passes the object to Element:setStyles.
			'styles': {
				'font-size': '11px',
				'line-height': '13px',
				'color': '#888'
			}});
	   js_instructions.set('text', 'Click to enlarge. ');
	   
		if (startW != null && startH != null) {
			var expander = new Fx.Morph(el,{duration:500,transition: Fx.Transitions.Back.easeOut}).set({width:shrinkW,height:shrinkH});
			//click event
			el.addEvent('click', function(e) {
				e = new Event(e); 
				expander.cancel ();
				if (isOpen) {
					expander.start({width:shrinkW,height:shrinkH});
					js_instructions.set('text', 'Click to enlarge. ');
				}else{
					expander.start({width:startW,height:startH});
					js_instructions.set('text', 'Click to close. ');
				}
				isOpen = !isOpen;
				e.stop();
			});
		}
	});
	
/*
	Expanding content
*/
	// hide all
	if ($$('.expanding').length > 0) {
		$$('.expanding .content').each(function(el) {
			el.set('display','none');
		});
		// Set up drop-downs
		$$('.expanding .trigger').each(function(el) {
			//create a slider
			var slyder = new Fx.Slide(el.get('rel'),{duration:450,transition: Fx.Transitions.Pow.easeOut}).hide();
			// add events
			el.addEvent('click', function(e) {
				e = new Event(e); slyder.toggle(); e.stop();
			});
		});
		// show all
		$$('.expanding .content').each(function(el) {
			el.set('display','block');
		});
	}
	
});




