window.onload = init;

//********** INIT **********//
function init() {
	// Menu list over
	startList();
	if(lPict = document.getElementById('lgPict')) {
		lgImgAni();
	}
	
	// get page Id
	var pgID = document.getElementsByTagName('body')[0].getAttribute('id');

	switch(pgID) {
		case 'homePg':
		homeFn();
		break;

		case 'companyPg':
		companyFn();
		break;

		case 'companyPgMark':
		companyFn();
		break;

		case 'aboutusPg':
		aboutusFn();
		break;

		case 'newsPg':
		newsFn();
		break;
		
		case 'galleryPg':
		triggerGallery();
		break;

		case 'videoPg':
		videoFn();
		break;
		
		case 'supportPg':
		supportFn();
		break;

		case 'partnersPg':
		partnersFn();
		break;
		
		case 'contactPg':
		break;
		
		default:
		break;
	}

	setUpMailingList();
}

//********* PAGE FUNCTIONS *********//

///--- HOME ---///
//var quotes = [];
var quoteCount = 0;
var quote;

function homeFn() {
/*	quotes = document.getElementById('quotes').getElementsByTagName('img');
	for(var i=0;i<quotes.length;i++) {
		quotes[i].style.position = 'absolute';
		quotes[i].style.top = '0px';
		quotes[i].style.left = '0px';
		quotes[i].style.opacity = 0;
		if(quotes[i].filters) {
			quotes[i].style.filter = 'alpha(opacity=' + 0 + ')';
		}
	}
*/
		quote = document.getElementById('quotes').getElementsByTagName('img')[0];
		quote.style.position = 'absolute';
		quote.style.top = '0px';
		quote.style.left = '0px';
		quote.style.opacity = 0;
		if(quote.filters) {
			quote.style.filter = 'alpha(opacity=' + 0 + ')';
		}	
	
}


function lgImgAni() {
	var lgPict = document.getElementById('lgPict');

	if(lgPict.filters) {
		lgPict.style.filter = 'alpha(opacity=' + 0 + ')';
	} else {
		lgPict.style['opacity'] = 0;
		lgPict.style['-moz-opacity'] = 0;
		lgPict.style['-webkit-opacity'] = 0;
	}

	var tw = new OpacityTween(lgPict, Tween.regularEaseOut, 0, 100, 1);
	if(document.getElementsByTagName('body')[0].getAttribute('id') == 'homePg') {
		tw.onMotionFinished = function() {
			quoteIn();
		};
	}
	tw.start();
}

function quoteIn() {
		//var obj = quotes[quoteCount];
		var obj = quote;
		var trIn = new Parallel();
		var tw1 = new OpacityTween(obj, Tween.regularEaseIn, 0, 100, 2);
		var tw2 = new Tween(obj.style, 'left', Tween.regularEaseOut, obj.offsetLeft + 50, obj.offsetLeft, 2, 'px');
		
		trIn.addChild(tw1);
		trIn.addChild(tw2);
		trIn.onMotionFinished = function() {
			//setTimeout('quoteOut()', 2500);
		};
		
		trIn.start();
}

function quoteOut() {
		var obj = quotes[quoteCount];
		
		var trOut = new Parallel();
		var tw1 = new OpacityTween(obj, Tween.regularEaseOut, 100, 0, 1);
		var tw2 = new Tween(obj.style, 'left', Tween.regularEaseIn, obj.offsetLeft, obj.offsetLeft - 50, 1, 'px');
		
		trOut.addChild(tw1);
		trOut.addChild(tw2);
		
		trOut.onMotionFinished = function() {
			obj.style.left = obj.offsetLeft + 50;
			(quoteCount >= (quotes.length - 1)) ? quoteCount = 0 : quoteCount += 1;
			quoteIn();
		};
		trOut.start();
}

///--- ABOUT US ---///
function aboutusFn() {
	var scroll;
	if(scroll = document.getElementById('scrollBar')) {
		setUpDrag('thumb', 'aboutSlider');
	}
}

///--- NEWS ---///

function newsFn() {
	setUpDrag('thumb', 'newsSlide');
}

///--- PARTNERS ---///
function partnersFn() {
	setUpDrag('thumb', 'slide');
}

///--- COMPANY ---///
function companyFn() {
	//lgImgAni();
	setUpDrag('thumb', 'staffSlider');
}

///--- GALLERY ---///
var photoGal;
var photoThumbs;

function galleryFn() {
	setPageGalleries();
	defaultGallery();
}
// gallery thumbs
function setPageGalleries() {
	var cont = document.getElementById('gallery-links');
	var links = cont.getElementsByTagName('a');
	for(var i=0; i<links.length; i++) {
		links[i].onclick = function() {return false;};
		addEvents(links[i],'mousedown', function(e) {displayGallery(e);});
	}
}

function defaultGallery() {
	photoGal = new ejGallery();
	photoGal.init('galCont', 'multimedia/gallery/adhesion-2010.xml', {width:'753px', height:'380px', backgroundColor:''});
	getThumbs();
}


function displayGallery(url) {
	//var targ = getTarget(e);
	//var url = targ.parentNode.getAttribute('href');
	photoGal = null;
	photoGal = new ejGallery();
	photoGal.init('galCont', url, {width:'753px', height:'380px', backgroundColor:''});
	getThumbs();
	
	return false;
}

function getThumbs() {
	photoThumbs = new ejGalleryThumbs();
	photoThumbs.onImgLoaded = function() {
		if(photoThumbs.count == 6) {
			document.getElementById('scrollBar').style.display = 'block';
			setUpDrag('thumb','thumbSlide', true);
		}
	};
	photoThumbs.init('thumbSlide', photoGal, {width: '90px', height: '70px'}, true);
}

// Get menu from title, attach it to the back of the url as a var with the name of the gallery
// and go to the page
function gotoGallery(e) {
	var gal = e.getAttribute('title');
	window.location = 'gallery.php?' + escape(gal);
}

// Get the var that comes with the link and trigger that gallery
function triggerGallery() {
	var gal = window.location.search;
	if (gal.substring(0, 1) == '?') {
    	gal = gal.substring(1);
  	}
  	
  	var xmlplace = 'multimedia/gallery/';
  	
  	gal = xmlplace + gal + '.xml';
	displayGallery(gal);
}


///--- VIDEO ---///
function videoFn() {
	setUpDrag('thumb', 'thumbSlide');
}

///--- COMPANY ---///
function supportFn() {
	var drag;
	if(drag = document.getElementById('scrollBar')) {
		setUpDrag('thumb', 'supportSlider');
	}
}

//********** UTILITIES *********//
function setUpDrag(thmb, sldr, bool) {
	var thumb = document.getElementById(thmb);
	var slider = document.getElementById(sldr);
	Drag.init(thumb,slider,bool);
}

function getTarget(e) {
	var targ;
	if (!e) var e = window.event;
	if (e.target) {
		targ = e.target;
	} else if (e.srcElement) {
		targ = e.srcElement;
	}
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	
	return targ;
}

function addEvents(obj,evt,fn) {
		if (obj.addEventListener) {
			obj.addEventListener(evt,fn,false);
		} else if (obj.attachEvent) {
			obj.attachEvent('on'+evt,fn);
		}
}


// ========== DONATE BUTTON FUNCTION ========== //

function getDonatePopUp(url) {
var req = new AJAX('donate.html', 'responseText', null);
	req.uponReceived = function() {
		var htm = this.xmldata;
		donateShow(htm);
	};
	req.getData();
}

function donateShow(htm) {
	var cont = document.getElementById('content');
	var dCont = document.createElement('div');
	dCont.setAttribute('id','donateDivPar');
	
	dCont.innerHTML = htm;
	cont.appendChild(dCont);
	
	var bck;
	if(bck= document.getElementById('donateBack')) {
		bck.onclick = function() {
			donateRemove();
			return false;
		};
	}
	
	unsetDonateBtn();
	
	var tw = new OpacityTween(dCont, Tween.regularEaseOut, 0, 100, 1);
	tw.start();
}

function donateRemove() {
	var cont = document.getElementById('donateDivPar');	

	var tw = new OpacityTween(cont, Tween.regularEaseOut, 100, 0, 0.5);
	tw.onMotionFinished = function() {
		cont.parentNode.removeChild(cont);
		setUpDonateBtn();
	};
	tw.start();
}

// Menu set up for EI
function startList() {
	if (document.all&&document.getElementById) {
		var navRoot = document.getElementById("nav");
		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", "");
				}
			}
		}
	}
}

// Set up Mailing List button //
function setUpMailingList() {
	var mlb;
	if(mlb = document.getElementById('mailing-list')) {
		mlb.onclick = function() {
				window.open(this.href,null,'width=700,height=720,toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1');
				return false;
		};
	}

	var mlb2;
	if(mlb2 = document.getElementById('subscribe')) {
		mlb2.onclick = function() {
				window.open(this.href,null,'width=700,height=720,toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1');
				return false;
		};
	}

	var mlb3;
	if(mlb2 = document.getElementById('subscribe-menu')) {
		mlb2.onclick = function() {
				window.open(this.href,null,'width=700,height=720,toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1');
				return false;
		};
	}
	
	var ejg = document.getElementById('copyright');
	ejg = ejg.getElementsByTagName('a')[0];
	ejg.onclick = function() {
		window.open(this.href,'Ellier');
		return false;
	};
	
	setUpDonateBtn();
}

function setUpDonateBtn() {
	var donBtn;
	if(donBtn = document.getElementById('donate-btn')) {
		donBtn.onclick = function() {
				getDonatePopUp(this.href);
				return false;
		};
	}
	
	var donBtn2;
	if(donBtn2 = document.getElementById('donate-tdy')) {
		donBtn2.onclick = function() {
				getDonatePopUp(this.href);
				return false;
		};
	}
}

function unsetDonateBtn() {
	var donBtn;
	if(donBtn = document.getElementById('donate-btn')) {
		donBtn.onclick = function() {
				return false;
		};
	}

	var donBtn2;
	if(donBtn2 = document.getElementById('donate-tdy')) {
		donBtn2.onclick = function() {
				return false;
		};
	}
}


