var paused = false;
var galleryDuration = 5000;

function addHover() {
	try {
		$$('#content a').morph('#content .linkBehavior').addEvents({
			'mouseenter': function() {
				this.morph('#content .linkBehavior:hover');
			},
			'mouseleave': function() {
				this.morph('#content .linkBehavior');
			}
		})
		$$('#navigation a').morph('#navigation .linkBehavior').addEvents({
			'mouseenter': function() {
				this.morph('#navigation .linkBehavior:hover');
			},
			'mouseleave': function() {
				this.morph('#navigation .linkBehavior');
			}
		})
		$$('#myGallery a').morph('#myGallery .linkBehavior').addEvents({
			'mouseenter': function() {
				this.morph('#myGallery .linkBehavior:hover');
			},
			'mouseleave': function() {
				this.morph('#myGallery .linkBehavior');
			}
		})
		$$('#ganesh a').addEvents({
			'mouseenter': function() {
				this.morph('#ganesh .linkBehavior:hover');
			},
			'mouseleave': function() {
				this.morph('#ganesh .linkBehavior');
			}
		})
	} catch(e) {}
}

function setupNavLevel0() {
	try {
		var navLi = $$('#navigation > ul > li');
		var debug = "";
		var prevLi, currLi;
		var i;

		//check for prevLi
		for( i = 0; i < navLi.length; i++ ) {
			var li = navLi[i];
			if(li.getElements('ul').length > 0) {
				li.subMenu = li.getElements('ul')[0];
			}
			li.link = li.getElements('a')[0];
			if(li.match('.active')) {
				prevLi = li;
				if(li.subMenu != undefined) {
					li.subMenu.setStyle('display', 'block');
					li.subMenu.fade('show');
				}
			} else {
				if(li.subMenu != undefined) li.subMenu.fade('hide');
			}
		}
				
		for( i = 0; i < navLi.length; i++ ) {
			var li = navLi[i];
			li.link.addEvent('click', function(event) {
					event = new Event(event).stop();
					var par = this.getParent();
					if(prevLi == par) return;
					currLi = par;
					
					if(currLi.subMenu != undefined) currLi.subMenu.setStyle('display', 'block');
					currLi.addClass('active');
					
					$('content').get('tween', {property: 'opacity', duration: 'short'}).start(0);
					if(prevLi == undefined || (prevLi != undefined && prevLi.subMenu == undefined)) {
						//no other li is active, or some li is active but has no submenu
						//then, if current li has submenu then fade it in and go to new page
						//otherwise just go to new page
						if(prevLi != undefined) prevLi.removeClass('active');
						if(currLi.subMenu != undefined) {
							currLi.subMenu.get('tween', {property: 'opacity', duration: 'short'}).start(1).chain(
								function() {
									fadeOutContent(currLi.link.href);
								}
							);
						} else {
							fadeOutContent(currLi.link.href);
						}
					} else {
						//some other li is active and it has a sub menu
						//so fade out that submenu
						//if current li has submenu then fade it in and go to new page
						//otherwise just go to new page
						prevLi.subMenu.get('tween', {property: 'opacity', duration: 'short'}).start(0).chain(
							function() {
								prevLi.removeClass('active');
								if(currLi.subMenu != undefined) {
									currLi.subMenu.get('tween', {property: 'opacity', duration: 'short'}).start(1).chain(
										function() {
											fadeOutContent(currLi.link.href);
										}
									);
								} else {
									fadeOutContent(currLi.link.href);
								}
							}
						);
					}
				}
			);	
		}
		//alert (debug);
	} catch(e) {}
}

function setupNavLevel1() {
	try {
		var menu = $$('.nav1 > .hassubmenu');
		var linkUrl = new String();
		var prev, curr;
		var i;
		
		//handle links with .hassubmenu
		//in these cases, the previous link has to slide-close and .active has to be removed 
		//then the current link has to slide-open and .active has to be added
		for(i = 0; i < menu.length; i++) {
			menu[i].subMenu = menu[i].getElements('.nav2')[0];
			menu[i].slide = new Fx.Slide(menu[i].subMenu);
			menu[i].link = menu[i].getElements('a')[0];
			if(menu[i].hasClass('active')) {
				menu[i].subMenu.setStyle('display', 'block');
				prev = menu[i];
			} else {
				if(!menu[i].hasClass('linkscroller')) {
					menu[i].slide.hide();
				}
			}
			menu[i].link.addEvent('click', function(event) {
				event = new Event(event).stop();
				var par = this.getParent();
				if(prev == par) return;
				linkUrl = this.href;
				curr = par;
				par.addClass('active');
				par.subMenu.setStyle('display', 'block');
				if(prev != undefined) {
					prev.removeClass('active');
					prev.slide.toggle().chain(
						function() {
							curr.slide.toggle().chain(
								function() {
									if(!par.hasClass('linkscroller')) {
										fadeOutContent(linkUrl);
									}
								}
							);
						}
					);
				} else {
					if(!par.hasClass('linkscroller')) { 
						par.slide.toggle().chain(
							function(){
								fadeOutContent(linkUrl);
							}
						);
					}
				}
				prev = curr;
			});
		}
		
		//now handle links without .hassubmenus
		menu = $$('.nav1 > li');
		for(i = 0; i < menu.length; i++) {
			//add only to li tags that are not child of ul.linkscroller and do not have .hassubmenu class
			if(!menu[i].hasClass('hassubmenu') && !menu[i].hasClass('linkscroller') ) {
				menu[i].link = menu[i].getElements('a')[0];
				menu[i].link.addEvent('click', function(event) {
					var par = this.getParent();
					par.addClass('active');
					event = new Event(event).stop();
					linkUrl = this.href;
					if(prev != undefined) {
						prev.removeClass('active');
						prev.slide.toggle().chain(
							function() {
								fadeOutContent(linkUrl);
							}
						);
					} else {
						fadeOutContent(linkUrl);
					}
				});
			}
		}


	} catch(e) {}
}

function setupNavLevel2() {
	try {
		var navlinks = $$('.nav2 a');
		for(var i = 0; i < navlinks.length; i++) {
			navlinks[i].addEvent('click', function(event) {
					event = new Event(event).stop();
					if(!this.getParent().hasClass("linkscroller")) {
						fadeOutContent(this.href);
					}
				}
			);
		}
	} catch(e) {}
}

function setupLinkScroller() {
	try{
		var scroll = new Fx.Scroll(window, {
			wait: false,
			duration: 1000,
			offset: {'x': -350, 'y': 0},
			transition: Fx.Transitions.Quad.easeInOut
		});
		
		var menu = $$('#navigation .linkscroller a');
		var i, divId, linkItem, prev = -1, first = -1;
					
		for(i = 0; i < menu.length; i++) {
			var a = menu[i].href;
			menu[i].div = a.substr(a.lastIndexOf('#') + 1);

			if(document.location.hash == '#' + menu[i].div) {
				menu[i].getParent().addClass('active');
				prev = menu[i];
			}

			linkItem = menu[i];
			if($(menu[i].div) != undefined) {
				first = first == -1 ? i : first;
				$(menu[i].div).index = i;
				$(menu[i].div).addEvent('mouseenter', function(e) {
					e = new Event(e).stop();
					if(prev == menu[this.index]) return;
					menu[this.index].getParent().addClass('active');
					prev.getParent().removeClass('active');
					prev = menu[this.index];
				});

				menu[i].addEvent('click', function(event) {
					event = new Event(event).stop();
					divId = this.div;
					if(this == prev) {	
						scroll.toElement(divId).chain(function(){document.location.hash = '#' + divId;}); 
						return;
					}
					scroll.toElement(divId).chain(function(){document.location.hash = '#' + divId;});
	
					if(prev != -1) {
						prev.getParent().removeClass('active');
					}
					prev = this;
					this.getParent().addClass('active');
				});
			}
		}
		
		if(document.location.hash == "") {
			menu[first].getParent().addClass('active');
			prev = menu[first];
		}
				
	} catch(err) {}
}

function setupReadMore() {
	try {
		var moredataDiv = $('moredata');
		if(moredataDiv != undefined) {
			var scroll = new Fx.Scroll(window, {
				wait: false,
				duration: 1000,
				offset: {'x': -350, 'y': 0},
				transition: Fx.Transitions.Quad.easeInOut
			});
			moredataDiv.setStyle('display', 'none');
			moredataDiv.fade('hide');
			
			var readMoreButton = new Element('span', {
				'html': 'Read More &rarr;',
				'styles': {
					'cursor': 'pointer',
					'color': '#666'
				},
				'events': {
					'click': function() {
						this.get('tween', {property:'opacity', duration: 'short'}).start(0);
						$('moredata').setStyle('display', 'block');
						$('moredata').get('tween', {property: 'opacity', duration: 'short'}).start(1).chain(
							function() {
								scroll.toElement('moredata');
							}
						);
					},
					'mouseover': function() {
						this.morph('#content .linkBehavior:hover');
					},
					'mouseout': function() {
						this.morph({color: '#666'});
					}
				}
			});

			readMoreButton.inject(moredataDiv, 'before');
		}
	} catch(err) {}
}

function setupNavigation() {
	try {
		var menu = $$('#navigation a');
		var i;
		
		var docUrl = new String(document.location);
		if(document.location.hash != "") {
			docUrl = docUrl.substring(0, docUrl.lastIndexOf('#'));
		}
		
		for(i = 0; i < menu.length; i++) {
			if(menu[i].href == docUrl) {
				menu[i].getParent().addClass('active');
			}
		}
		
	} catch(e) {}
	
	
	setupNavLevel0();
	setupNavLevel1();
	setupNavLevel2();

	setupLinkScroller();
}

function fadeInContent() {
	try {
		$$('html').addClass('switchpage');
		$('content').fade('hide');
		$('content').get('tween', {property: 'opacity', duration: 'short'}).start(1).chain(
			function() {
				$$('html').removeClass('switchpage');
			}
		);
	} catch(e) {}
}

function fadeOutContent(newUrl) {
	try {
		$$('html').addClass('switchpage');
		$('content').get('tween', {property: 'opacity', duration: 'short'}).start(0).chain(
			function() {
				document.location = newUrl;
			}
		);
	} catch(e) {}
}

function initGallery() {
	try {
		var gallery = $('myGallery');
		var timerID;
		paused = false;
		var prevImg, currIndex, currImg;
		
		var advance = function() {
			if(!gallery) return;
			//if(paused) return;
			prevImg		 = gallery.images[gallery.curr++];
			gallery.curr 	%= gallery.images.length;
			currIndex 	 = gallery.curr;
			currImg		 = gallery.images[gallery.curr];
			update();			
		}
		
		var pauseFn = function() { 
			clearInterval(timerID);
			timerID = undefined;
			$('toggle').set('class', 'play');
			$('toggle').innerHTML = "Play";
		}
		
		var playFn = function() {
			if(!timerID) {
				timerID = setInterval(advance, galleryDuration);
			}
			$('toggle').set('class', 'pause');
			$('toggle').innerHTML = "Pause";
		}
		
		var toggleFn = function() {
			if(timerID) pauseFn();
			else playFn();
		}
		
		var nextFn = function() {
			if(!gallery) return;
			pauseFn();
			prevImg		 = gallery.images[gallery.curr++];
			gallery.curr 	%= gallery.images.length;
			currIndex 	 = gallery.curr;
			currImg		 = gallery.images[gallery.curr];
			update();
		}
		
		var prevFn = function() {
			if(!gallery) return;
			pauseFn();
			prevImg		 = gallery.images[gallery.curr--];
			if(gallery.curr < 0) {
				gallery.curr = gallery.images.length - 1;
			} else {
				gallery.curr 	%= gallery.images.length;
			}
			currIndex 	 = gallery.curr;
			currImg		 = gallery.images[gallery.curr];
			update();
		}
		
		var update = function() {
			prevImg.get('tween',  {property: 'opacity', duration: 'long'}).start(0);
			currImg.get('tween', {property: 'opacity', duration:'long'}).start(1);
		}
			
		var prevBu = new Element('a', {
				'href': '#',
			    'class': 'prev',
			    'html': 'Previous',
			    'events': {
			        'click': function (e) { e = new Event(e).stop();prevFn(); }
			    }
			}
		);
		
		var nextBu = new Element('a', {
				'href': '#',
			    'class': 'next',
			    'html': 'Next',
			    'events': {
			        'click': 
			        function (e) { e = new Event(e).stop(); nextFn(); }
			    }
			}
		);
		
		var toggleBu = new Element('a', {
				'href': '#',
				'id': 'toggle',
				'class': 'pause',
			    'html': 'Pause',
			    'events': {
			        'click': function (e) { e = new Event(e).stop(); toggleFn(); }
			    }
			}
		);
	
		gallery.images	= gallery.getElements('div');
		$each(gallery.images, function(item, index) {
			if(index != 0) item.setStyle('opacity', 0);
			item.setStyle('z-index', index + 1);
			var imgUrl = item.getElements('img').get('src');
			item.setStyle('background-image', 'url(' + imgUrl + ')');
		});
	
		gallery.adopt(prevBu);
		gallery.adopt(toggleBu);
		gallery.adopt(nextBu);
		
		gallery.curr = 0;
		playFn();
	} catch(e) {}
}


window.addEvent('domready', initGallery);
window.addEvent('domready', setupReadMore);
window.addEvent('domready', setupNavigation);
window.addEvent('domready', addHover);
window.addEvent('domready', fadeInContent);
