/* © Patersons 2000-2009. All Rights Reserved.*/
var flybackdellay = 1500;
var flyduration = 200;
var flying = false;
var timeout = false;
$(document).ready(
	function() {
		$()
		
		$('#menu').addClass('active'); //this makes the style "work" this means the css can be used rather than pure js
		$('#menu .root  .ifly').hide().siblings('.button_bar').click(flyout);
		setup_menu();
		
		//news & events
		$('#right_column_news').click(news_events_up);
		$('#right_column_events').click(news_events_up);
		
		$(window).resize(resize);	
	}
);

function news_events_up(){
	$('#right_column_events_content').animate({height:"157px"},1000);
	$('#right_column_news_content').animate({height:"0"},1000, '', function(){
		$('#right_column_news_content').css('display', 'none');
		$('#events_menu_arrow').removeClass('up').addClass('down');
	});
	$('#right_column_news').unbind('click').click(function(){news_events_down();});
	$('#right_column_events').unbind('click').click(function(){news_events_down();});
}

function news_events_down(){
	$('#right_column_news_content').animate({height:"157px"},1000);
	$('#right_column_events_content').animate({height:"0"},1000, '', function(){
		$('#right_column_events_content').css('display', 'none');
		$('#events_menu_arrow').removeClass('down').addClass('up');
	});
	$('#right_column_news').unbind('click').click(function(){news_events_up();});
	$('#right_column_events').unbind('click').click(function(){news_events_up();});
}






function setup_menu() {
	set_height_100($('#root .right_shadow'));
	
	$('#menu .root .ifly').hide()
	.siblings('.button_bar').each(function() {
		//$(this).css({'position' : 'relative','top' : 'false','left' : 'false'}); 
		off  =$(this).offset();
		x = off.left;
		y = off.top;
		//$(this).css({'position' : 'absolute','top' : y,'left' : x}); //the position is in here so it does not have to effect the page at all.
		$('#menu .root .ifly').css({top : y + $(this).parent('li.root').height(),left : x+ $(this).parent('li.root').width() +5})
	});
	$('#menu .root .ifly a.current, #menu a.current').children('.button_bar').removeClass('light_blue').addClass('blue').end().parent('li').parent('ul.ifly').parent('.root').children('.button_bar').removeClass('light_blue').addClass('blue');
	//.parent('.root').children('.button_bar').removeClass('light_blue').addClass('blue');
}
function resize() {
	
	setup_menu();
	$('#menu .root .ifly').parent('.root').each(function() {
		id = $(this).attr('id');
		ul = $('.ifly.'+id);
		button = $('.button_bar.'+id);
		off  =$(this).offset();
		x = off.left;
		y = off.top;
		button.css({'top' : y,'left' : x}); //the position is in here so it does not have to effect the page at all.
		ul.css({'position' : 'absolute', 'top' : y + $(this).height(),'left' : x+ $(this).width()+2});
	});
}
var flyclose = false;
function hoverstart() {
	clearTimeout(flyclose);
}
function hoverstop() {
	flyclose = setTimeout(flyin,flybackdellay);
}
function flyout() {
	f = function(){
		var open = $($this).addClass($($this).parent('li').attr('id')).siblings('ul.ifly');
		off  =$($this).offset();
		x = off.left;
		y = off.top;
		clone = $($this).clone(true);
		open_1 = open.clone(true);
		if ($($this).hasClass('blue')) {
			$($this).removeClass('blue').addClass('green').addClass('tmp_blue');
		}
		else{
			$($this).removeClass('light_blue').addClass('green');
		}
		$('body').append(clone);
		$('body').append(open_1);
		clone.removeClass('light_blue').addClass('green');
		clone.css({'position' : 'absolute','top' : y,'left' : x}); //the position is in here so it does not have to effect the page at all.
		open.hide();
		$('#VideoPlayer .holder').hide();
		clone.animate({'width' : ($($this).parent('li').width() * 2) + 2},flyduration,function(){
			clone.removeClass('green').addClass('blue');
			
			open_1.css({'position' : 'absolute', 'top' : y + open.parent('.root').height(),'left' : x+ open.parent('.root').width()+2}).hover(hoverstart,hoverstop);
			var h = open.height();
			open_1.css({'height' : 1, 'width' : open.parent('.root').width()}).animate({'height' : h},flyduration,function(){
				flying = false;	
			});
			open_1.children('li').height(31); //this is to fix a issue with IE
		}).hover(hoverstart,hoverstop);
	}
	if (flying == true) {
		thistm = this;
		setTimeout(function() {
			$this = thistm;
			flying = true;
			if (flyin()) {			clearTimeout(timeout);			timeout = setTimeout(f,flyduration);		} else { f(); } 
		},flyduration*2);
	}
	else {
		$this = this;
		flying = true;
		if (flyin()) {setTimeout(f,flyduration);} else {f();}
	}
}

function flyin() {
	$('#VideoPlayer .holder').show();
	op = $('body > ul.ifly:visible');
	pare = $('body > .button_bar');
	if (op.length > 0) {
		if ($('#menu .root .button_bar.green').hasClass('tmp_blue')) {
			$($this).removeClass('green').addClass('blue').removeClass('tmp_blue').show();
		}
		else{
			$('#menu .root .button_bar.green').removeClass('green').addClass('light_blue').removeClass('blue').show();
		}
	//	$('#menu .root .button_bar.green').removeClass('green').addClass('light_blue');
		op.animate({'height' : 1},flyduration/2,function(){
			op.hide();
			pare.animate({'width' : (pare.width()/2)-5},flyduration/2,function() {op.remove(); pare.remove();	});
		});
		$('#menu .root .ifly a.current, #menu a.current').children('.button_bar').removeClass('light_blue').addClass('blue').end().parent('li').parent('ul.ifly').parent('.root').children('.button_bar').removeClass('light_blue').addClass('blue');
		return true;
	}
	else {
		return false;
	}
}
function slidy_setup (options) {
	var slidy =options['slide'];
	$(options['left']).click(function () {
		var $parent = $(slidy);
		var $it = $parent.children('li:last');
		var $me = $it.clone(true);
		$me.prependTo($parent);
		$it.remove();
	});
	$(options['right']).click(function() {
		var $parent = $(slidy);
		var $it = $parent.children('li:first');
		var $me = $it.clone(true);
		$me.appendTo($parent);
		$it.remove();
	});
}


function gallary_build(){
	window.scrollTo(0, 1);
	html = '<div class="image_gallary" >';
	i = 0;
	html += '<div class="blue bar"><img src="'+site_root+'/images/corp/gallery/close.png" class="close"></div>';
	html += '<img class="display" style="border-bottom: 1px solid grey; width: 800px; height: 600px" src="'+site_root+"/"+gallery_images[0]+'">';
	html += '<p>'+gallery_text[0]+'</p>';
	html += '<div class="boxes"><img src="'+site_root+'/images/corp/gallery/arrow_L.png" style="float: left;" class="left" />';
	
	html +='<ul class="slidy" style="float: left;overflow: hidden; width: 550px;height: 52px">';
	for (a in gallery_images) {
		if (a == 0) {
			html += '<li class="nbox active '+(++i)+'">'+i+'</li>';
		}
		else {
			html += '<li class="nbox '+(++i)+'">'+i+'</li>';
			img= new Image(955,500);
			img.src = site_root+"/"+gallery_images[a];
		}
	}
	html += '</ul><img src="'+site_root+'/images/corp/gallery/arrow_R.png" class="right" style="float: left; "/></div>';
	html += '<div class="blue bar bottom" style="display: block;clear: both;position: relative;"></div>';
	html += '</div>';
	gray = '<div class="grayout" />';
	$('body').append(gray);
	$('body').append(html);
	$('.grayout').css({"display" : "block","position" : "absolute","top" :"0px",left : "0px",width : "100%",height: "100%","opacity" : "0.5","background-color" : "RGB(125,125,125)"});
	set_height_100($('.grayout'))
	$('.image_gallary').css({"display" : "block","position" : "absolute",top :"25px",left : ($('#root').offset().left+140)+"px",width : "800px",height: "735px"});
	$('.bar.blue.bottom').css({"bottom": "-10px","left" :"0px"});
	$(window).resize(function() {
		$('.grayout').css({height: "100%"});
		set_height_100($('.grayout'))
	});
	$('.image_gallary .nbox').click(function() {
		$('.image_gallary .display').attr("src",site_root+"/"+gallery_images[($(this).text())-1]);
		$('.image_gallary .nbox').removeClass("active");
		$(this).addClass("active");
		$('.image_gallary p').html(gallery_text[($(this).text())-1]);
	});
	$('.image_gallary .close').click(function() {
		$('.grayout,.image_gallary').remove();	
	});
	$('.boxes').css({"position" : "relative","bottom" : "-5px","left" : "100px","display" : "block"});
	slidy_setup({left : '.left', right: '.right',slide : 'ul.slidy'});
	$('.image_gallary .nbox.active').click();
	//support for inline editing
	if (typeof(edit_setup) != 'undefined' ) {
		edit_setup();
	}
}

/*
 * Captcha code allows regeneration of captcha
*/
function captcha_new(id_base,link) {
	d = utc();
	$('#'+id_base+'_img').attr('src',link+d);
	$('#'+id_base+'_fi').attr('value',d);
	return false;
}
function utc() {
	date = new Date();
	return Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), 	date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), date.getUTCMilliseconds());
}
/*
 * Added this function to get around strange artifacts when firebug is installed or other things that moddify the ui (ie dev toolbar etc) without triggering the window/body height to reported by the browser as changed
 * NOTE: this is dirty if nice CSS way can be found this should be removed
 * NOTE: the timeout is dirty but is needed else artifacts are calused during page load with IE devtool bar if better why can be found it should be removed
*/
function set_height_100(el) {setTimeout(function(){bh = $(document).height(); if (bh > el.height()) {el.height(bh);}},1);}
