var myAccordion;
var carousel = {};

window.addEvent('domready', function() {
    drawGlyph();
	bindLightbox();
	myAccordion = new Accordion($$('.tn'), $$('.sn'), {show: -1, alwaysHide: true});
	showNav();
	tmp();
	bindCarousel();
});

function tmp() {
	if($('ln_blog') != null) {
		$('ln_blog').addEvent('click', function(){
			alert('Our blog will be coming soon - as soon as we decide on a name!');
		});
	}
}

function drawGlyph() {
	$('pg').setStyle('background-image', 'url(/images/pg/' + $random(1,9) + '.gif)');
	$('pg').setStyle('top', 390 + $random(0,150));
}

function bindLightbox() {
	$$('a > img').each(function(item, index) {
		var a = item.getParent('a');
		if (a != null) {
			var href = item.getParent('a').getProperty('href');
			if (href.substr(href.length - 4) == '.jpg') {
				a.slimbox();
			}
		}
	});
}

var navs = ['/holidays/', '/yurts/', '/courses/', '/gallery/'];

function showNav() {
	navs.each(function(item, index) {
		if (window.location.href.indexOf(navs[index]) > 0 ) {
			myAccordion.display(index);
		}
	});
}


function hidePanels() {
	carousel.pnls.each(function(item, index) {
		item.setStyle('display', 'none');
		carousel.btns[index].setStyle('background', '#666');
	});
}

function showPanel(index) {
	hidePanels();
	carousel.btns[index].setStyle('background', '#999');
	carousel.pnls[index].setStyle('display', 'inline-block');
}

function bindCarousel() {
	if ($('slider') != null) {
		carousel.w = 497;
		carousel.c = 0;
		carousel.btns = new Array();
		carousel.pnls = $$('#slider-list li');
	
		carousel.pnls.each(function(item, index) {
			btn = new Element('a', {
				'href': 'javascript:;',
				'events': {
					'click': function() {
						idx = index;
						carousel.c = idx;
						showPanel(idx);
					}
				}
			  });
			carousel.btns.push(btn);
			btn.inject($('slider-buttons'));
		});
		
		hidePanels();
		showPanel(0);
		
		window.setInterval(function() {
			hidePanels();
			if (carousel.c < carousel.btns.length - 1) {
				carousel.c += 1;
			}
			else {
				carousel.c = 0;
			}
			showPanel(carousel.c);
		}, 7000);                    
	}
}
                                   
function EmailSubscriberAdd(email) {
	var myRequest = new Request({
			method: 'get', 
			url: '/service/EmailSubscriberAdd.aspx'
		}).send('Email=' + email);
}
