//dom ready
window.addEvent('domready', function(){
	//variables
	var mooSifrTags = $$('h1,h2,h3,h4,h5,h6,span');
	var topMenuLinks = $$('#TopMenu a');
	var pTags = $$('#Top .col.left p,.panel p,#Top .col.left_big p');
	var logo = $$('#Logo a');
	var searchSubmit = $$('#Search input.Submit');
	
	//javascript detection
	$('Container').removeClass('noJavaScript');
	
	//call mooSIFR (example usage: mooSIFR_swfname)
	if(mooSifrTags.length > 0){//only triggered if 'h tags' are found on page
		mooSifrTags.each(function(el,i){//loop through each h tag
			var font = '';//declare variable to store font in
			if(el.className.contains('mooSIFR')){//only grab h tags with mooSIFR class
				el.className.split(' ').each(function(myClass,i){//incase h tag has more than 1 class
					if(myClass.contains('mooSIFR')){//grab the class we need
						font = myClass.split('_')[1];//only take the font part and store it
					};
				});
				var initSIFR = new sIFR({//create a swf to replace text
					elements: el,
					swfPath: 'Portals/InspiringScotland/flash/',
					font: font
				});
			};
		});
	};
	
	//top menu
	topMenuLinks.each(function(el,i){
		//create effect
		var myEffect = new Fx.Morph(el,{duration:250,link:'chain',transition:Fx.Transitions.Sine.easeOut});
		//add event listener
		el.addEvents({
			'mouseenter': function(){
				myEffect.start({
					'background-color': ['#DBE0E2','#514D4D'],
					'color': ['#4D5257','#fff'],
					'padding': ['38px 9px 9px','43px 9px 9px']
				});
			},
			'mouseleave': function(){
				myEffect.start({
					'background-color': ['#514D4D','#DBE0E2'],
					'color': ['#fff','#4D5257'],
					'padding': ['43px 9px 9px','38px 9px 9px']
				});
			}
		});
	});
	
	//paragraphs
	pTags.each(function(el,i){
		if(pTags.length > 1 && i == 0){
			el.setStyles({
				'font-weight': 'bold',
				'font-size': '140%'
			});
		};
		if(i == pTags.length-1){
			el.setStyle('margin-bottom',0);
		};
	});
	
	//top menu
	logo.each(function(el,i){
		//create effect
		var myEffect = new Fx.Morph(el,{duration:250,link:'cancel',transition:Fx.Transitions.Sine.easeOut});
		//add event listener
		el.addEvents({
			'mouseenter': function(){
				myEffect.start({
					'opacity': [1,0.5]
				});
			},
			'mouseleave': function(){
				myEffect.start({
					'opacity': [0.5,1]
				});
			}
		});
	});
	
	//search subit
	searchSubmit.each(function(el,i){
		//add event listener
		el.addEvents({
			'mouseenter': function(){
				el.setStyle('background-position','left bottom');
			},
			'mouseleave': function(){
				el.setStyle('background-position','left top');
			}
		});
	});
	
	/*
	setup Link List redirects
	------------------------------------*/
	$$('#venturesLinkList a').each(function(el,i){
		el.href = el.href+'-blank';
		el.addClass('mb')	
	});
	
	
	//call multiBox
	var initMultiBox = new multiBox({
		mbClass: '.mb',//class you need to add links that you want to trigger multiBox with (remember and update CSS files)
		container: $(document.body),//where to inject multiBox
		descClassName: 'multiBoxDesc',//the class name of the description divs
		path: 'Portals/InspiringScotland/flash/',//path to mp3 and flv players
		useOverlay: false,//use a semi-transparent background. default: false;
		maxSize: {w:700, h:500},//max dimensions (width,height) - set to null to disable resizing
		addDownload: false,//do you want the files to be downloadable?
		pathToDownloadScript: 'Portals/InspiringScotland/scripts/ForceDownload.asp',//if above is true, specify path to download script (classicASP and ASP.NET versions included)
		addRollover: false,//add rollover fade to each multibox link
		addOverlayIcon: false,//adds overlay icons to images within multibox links
		addChain: false,//cycle through all images fading them out then in
		recalcTop: true,//subtract the height of controls panel from top position
		addTips: false//adds MooTools built in 'Tips' class to each element (see: http://mootools.net/docs/Plugins/Tips)
	});
	
	//call mooCollapsibles
	var initCollapsibles = new mooCollapsibles({
		cllpsContainers: $$('.collapsible'),//passes content/contents to be collapsed
		cllpsHeaders: $$('.collapsible .header').reverse(),//the headers inside containers
		cllpsBodies: $$('.collapsible .body').reverse(),//the bodies inside containers
		buildToggler: false,//'expand all | collapse all' buttons will be generated if true
		expandDefault: false,//have the collapsibles open or closed by deafult
		accordionEffect: true,//will simulate MooTools Accordion
		showAlerts: false//display alerts to warn users cookies are disabled etc
	});
	
	//staff flash
	if($('peopleFlash') || $('directorsFlash')){
		if($('peopleFlash')){
			var staffFlash = new Swiff('Portals/InspiringScotland/flash/staff.swf', {
				id: 'Inspiring_Scotland_Staff',
				width: 762,
				height: 160,
				container: 'peopleFlash'
			});
			jiggyTheNav('peopleFlash');
		}else if($('directorsFlash')){
			var directorsFlash = new Swiff('Portals/InspiringScotland/flash/directors.swf', {
				id: 'Inspiring_Scotland_Directors',
				width: 762,
				height: 160,
				container: 'directorsFlash'
			});
			jiggyTheNav('directorsFlash');
		};

		function setupTab(el,i){
			el.setStyles({
				opacity: .75,
				padding: 18,
				float: 'left'
			});
			el.i = i;
			el.addEvents({
				'mouseenter': function(){
					this.hovering = true;
					this.set('tween',{duration:400,transition:Fx.Transitions.linear}).tween('opacity',1);
				},
				'mouseleave': function(){
					this.hovering = false;
					if(!this.active){
						this.set('tween',{duration:400,transition:Fx.Transitions.linear}).tween('opacity',.75);
					};
				},
				'click': function(){
					active = this.i;
				}
			});
		};
	};
	if($('tabs')){
		$$('#tabs li').each(function(el,i){setupTab(el,i)});
		myTabs = new SlidingTabs('tabs','panes');
		window.addEvent('resize',myTabs.recalcWidths.bind(myTabs));
	};
	
	//mooScroll
	var initScroller = new mooScroll({
		container: 'mooScroll',//container from which html is grabbed (will act as window pane)
		windowHeight: 237,//height of the window pane you see the content through (set as false to auto calculate)
		speed: 70,//scroll speed
		direction: 'up',//direction (only 'top' and 'bottom' allowed)
		stopOnHover: true//scrolling stops when you hover over news item
	});
	if($('mooScroll')){$('mooScroll').setStyles({ opacity: '1' })}
	
	//insert diagonal text
	/*var diagFlash = new Swiff('Portals/InspiringScotland/flash/diag-text.swf', {
		id: 'Inspiring_Scotland_Diagonal_Text',
		width: 119,
		height: 93,
		container: 'diag-text',
		vars: function(){
			setText: 'Read my storyyyy...',
			setFontSize: 30,
			setFontColor: '0x000000',
			setTextRot: -15,
			setOffSetTop: 0
		}
	});*/
	
	/*		Overlay Image Captions		*/
	if($$('#Container img').length>0){ checkImageOverlay.delay(500) }
});


/* ======================================================================

Functions

=======================================================================*/
function checkImageOverlay(){
	arr = $$('#Container img')
	/*		check image has an alt tag and is a decent size		*/
	arr.each(function(el,i){
		if(el.get('longdesc')&&!el.get('alt')){ el.set('alt', el.get('longdesc')) }
		if(el.get('alt')!='null' && el.get('alt')!='' && el.get('alt'))
		{ if(el.getSize().x>150 && el.getSize().y>150){ addImageCaption.delay(500, el); el.captionText = el.get('alt'); el.erase('alt'); } }
	});
}
function addImageCaption(){
	this.addClass('wrapped')
	this.outer = new Element('div', {'class': 'imgCapOuter'}).wraps($(this));
	this.outer.img = this
	this.outer.setStyles({
		position: 'relative',
		overflow: 'hidden',
		width: this.getSize().x,
		height: this.getSize().y
	});
	this.cap = new Element('p', {'class': 'imgCap'}).injectInside($(this.outer));
	this.cap.fade('hide')
	this.cap.setStyles({
		position: 'absolute',
		width: this.getSize().x,
		top: this.cap.getSize().y*-1,
		left: 0,
		background: '#D2D7D8',
		margin: '0',
		color: '#303030',
		padding: '10px'
	});
	this.cap.setStyle('border-bottom', '2px solid #fafafa')
	this.cap.setStyle('font-weight', 'bold')
	this.cap.set('text', this.captionText)
	this.outer.offset = this.cap.getSize().y*-1
	this.fect = new Fx.Morph(this.cap,{duration:250,link:'cancel',transition:Fx.Transitions.Sine.easeOut});
	showCap.delay(500, this)
	closeCap.delay(3500, this)
	this.over = false
	this.outer.addEvents({
		'mouseenter': function(){ showCap.delay(200, this.img); this.img.over = true },
		'mouseleave': function(){ closeCap.delay(200, this.img); this.img.over = false }
	});

}
function closeCap(){
	if(this.over!=true){
		this.cap.fade(.25)
		this.fect.start({ 'top': this.outer.offset+'px' });
	}
}
function showCap(){
	this.fect.start({ 'top': '0px' });
	this.cap.fade(.75)
}

function jiggyTheNav(ids){
	var tlist = $('tabs').get('html');
	var txt = '<ul id="addedNav">'+tlist+'</ul>';
	$$('.leftmenu .active').each(function(el,i){
		if(i == 0){
			el.set('html',el.get('html')+txt);
			reworkList.delay(500);
		};
	});
};
function reworkList(){
	$$('#addedNav a').each(function(el,i){
		el.num = i;
		if(i == 0){
			el.addClass('selected');
		};
		el.addEvents({
			'click': function(e){
				e.stop();
				setActive(this.num);
			}
		});
	});
};
function setActive(id){
	active = id;
	$$('#addedNav a').each(function(el,i){
		if(i==active){
			el.addClass('selected');
		}else{
			el.removeClass('selected');
		};
	});
	myTabs.changeTo(active+1);
};
function setupTab(el,i){
	el.setStyles({
		opacity:.75,
		padding:18,
		float:'left'
	});
	el.i = i;
	el.addEvents({
		'mouseenter': function(){
		this.hovering = true;
			this.set('tween',{duration:400,transition:Fx.Transitions.linear}).tween('opacity',1);
		},
		'mouseleave': function(){
		this.hovering = false;
			if(!this.active){
				this.set('tween',{duration:400,transition:Fx.Transitions.linear}).tween('opacity',.75);
			};
		},
		'click': function(){
			active = this.i;
		}
	});
};
function checkActive(){
	$$('#tabs li').each(function(el,i){ 
		if(active == i){ 
			el.active = true; 
			el.set('tween',{duration:400,transition:Fx.Transitions.linear}).tween('opacity',1);
		}else{
			el.active = false;
			if(!el.hovering){
				el.set('tween',{duration:400,transition:Fx.Transitions.linear}).tween('opacity',.75);
			};
		};
	});
	myTabs.changeTo(active+1);
	return active;
};


/* ======================================================================

utils

=======================================================================*/
function trace(msg){ console.log(msg) }
