window.addEvent('domready', function() {
	
	if ($('search-drop')) {
		nav_search = { link : $('search-link'), drop : $('search-drop'), box : $('search-box-over'), form : $('search-form') }
		initSearchBox( nav_search );
	}
	if ($('hero-search-drop')) {
		hero_search = { drop : $('hero-search-drop'), box : $('hero-search-box-over'), form : $('hero-search-form') }
		initSearchBox( hero_search );
	}
	if ($('search-results')) {
		initPulsate();
	}
	if ($('gallery')) {
		initLightboxes();
	}
	if ($('tell-a-friend')) {
		initTellFriend();
	}
	initSifr();
});

/* Search
----------------------------------------------------------------------------------- */
var dropTimer;
var boxTimer;

function initSearchBox( obj ) {
	/* top nav item */
	if (obj.link) {
		obj.link.addEvent('click', function(e) {
			var e = new Event(e);
			e.stop()
			toggleSearchDrop(obj.form);
		});
		obj.link.addEvent('mouseout', function(e) {
			//clearDropTimer();
			//dropTimer = setTimeout('toggleSearchDrop(\''+obj.form.id+'\')', 2500)
		});
	}
	/* inner search gesture */
	obj.drop.addEvent('mouseover', function(e) 	{ toggleSearchBox( obj.box ); });
	/* drop box */
	obj.box.addEvent('mouseover', function(e) 	{ clearBoxTimer(); });
	obj.box.addEvent('mouseout',  function(e) 	{ startBoxTimer( obj.box, obj.form ); });
}

function toggleSearchDrop( el ) {
	clearDropTimer();
	if (typeof(el) == 'string') { el = $(el); }
	var state = el.getStyle('display') == 'block' ? 'none' : 'block';
	el.setStyle('display', state);
}

function toggleSearchBox( el_box, el_form ) {
	if (typeof(el_box) == 'string') { el_box = $(el_box); }
	var state = el_box.getStyle('display') == 'block' ? 'none' : 'block';
	el_box.setStyle('display', state);
	
	if (state == 'none') {
		if (typeof(el_form) == 'string') { el_form = $(el_form); }
		if (el_form.id.indexOf('hero') < 0) {
			el_form.setStyle('display', 'none');
		}
	} else {
		clearDropTimer();
	}
}

function startBoxTimer( el_box, el_form ) {
	clearBoxTimer();
	boxTimer = setTimeout('toggleSearchBox(\''+el_box.id+'\',\''+el_form.id+'\')', 1500)
}

function clearBoxTimer() 	{ clearTimeout(boxTimer); 	}
function clearDropTimer() { clearTimeout(dropTimer); 	}

/* Pulsate
----------------------------------------------------------------------------------- */
function initPulsate() {
	
	var scroll = new Fx.Scroll(window, {
		wait: false, duration: 1000,
		transition: Fx.Transitions.Quint.easeInOut
 	});
	
	var arrange_els = $ES('a.arrange-viewing','search-results');
	arrange_els.each(function(el){
	   el.addEvent('click', function(e) {
			var e = new Event(e);
			e.stop()
			scroll.toElement('arrange-viewing')
			scroll.toElement('arrange-viewing').chain(function() { new PulsateFx('arrange-viewing', { toColor: '#f5ccdd', toAlpha: .2 }); });
		});
	});
	
	var buy_els = $ES('a.buy','search-results');
	buy_els.each(function(el){
	   el.addEvent('click', function(e) {
			var e = new Event(e);
			e.stop()
			scroll.toElement('buy').chain(function() { new PulsateFx('buy', { toColor: '#f5ccdd', toAlpha: .2 }); });
		});
	});
}
/* Gallery
----------------------------------------------------------------------------------- */
function initLightboxes() {
	var link_els = $ES('a.lightbox','gallery');
	
	link_els.each(function(el) {
		tmp = new Lightbox(el);
	});
	
	/*link_els.each(function(el){
	   el.addEvent('click', function(e) {
			event = new Event(e).stop();
			showLightbox();
		});
	});
	*/
}
/* Tell a friend checkbox
----------------------------------------------------------------------------------- */
function initTellFriend() {
	
	var chk = 			$('tell-check');
	var chk_wrap = 	$('tell-check-wrap')
	var submit = 		$('tell-submit');
	var form = 			$('contact_form');
	
	submit.addEvent('click', function(e) {
		var e = new Event(e);
		e.stop()
		if (chk.checked) {
			form.submit();
		} else {
			chk_wrap.addClass('error');
		}
	});
	
	chk.addEvent('click', function(e) {
		chk_wrap.removeClass('error')
	});
	
}
/* SIFR
----------------------------------------------------------------------------------- */
function initSifr() {
	if(typeof sIFR == "function"){
		sIFR.replaceElement(named({
			sSelector:"h2", 
			sFlashSrc:"/flash/trajan.swf", 
			sColor:"#eb2e7f", 
			sBgColor:"#FFFFFF", 
			sWmode: 'transparent',
			nPaddingTop: 0, 
			nPaddingBottom: 0, 
			sFlashVars:"textalign=left&offsetTop=0"}));
	};
}
/* Popup
----------------------------------------------------------------------------------- */
function popup (url) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(url, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=476,height=464,left = 560,top = 325');");
}