var scrollLimit = 805;		// size of container
var scrollDelta = 0;		// how much was scrolled
var scrollIncrement = 161;	// i don't really need to explain this, do I?
var scrollSpeed = 400; 		// ms it takes to scroll one time
var scrollGallery;

var modelsBoxOpen = false;
var bonusBoxOpen = false;

function initEvents() {
	window.addEvent('resize', function(e) {
		if (modelsBoxOpen) resizeBox('models-box');
		if (bonusBoxOpen) resizeBox('bonus-box');
	});

	if ($('scroll-gallery')) {
		scrollGallery = new Fx.Scroll('scroll-gallery', {
			wait: false,
			duration: scrollSpeed,
			transition: Fx.Transitions.Quad.easeInOut,
			wheelStops: false
		});
		scrollGallery.set(0,0); 	// reset the scrolled object to starting position
	}
	$('scroll-left').addEvent('click', function(event) {
		event = new Event(event).stop();
		scroll('left');
	});
	$('scroll-right').addEvent('click', function(event) {
		event = new Event(event).stop();
		scroll('right');
	});

	$('freeaccess-button').addEvent('click', function(event) {
		tourFadeOut('fadeout1');
		window.setTimeout( 'show(\'pop\')', '800');
	});
	$('clicktojoin-button').addEvent('click', function(event) {
		tourFadeOut('fadeout1');
		window.setTimeout( 'show(\'pop\')', '800');
	});
	
	
	$('models-button').addEvent('click', function(event) {
		tourFadeOut('fadeout2');
		resizeBox('models-box');
		window.setTimeout( 'show(\'models-box\')', '800');
		modelsBoxOpen = true;
	});
	
	$('bonus-button').addEvent('click', function(event) {
		tourFadeOut('fadeout2');
		resizeBox('bonus-box');
		window.setTimeout( 'show(\'bonus-box\')', '800');
		bonusBoxOpen = true;
	});
	
	$('vod-button').addEvent('click', function(event) {
		hide('content2');
		hide('content3');
		show('content1');
		$('vod-button').className = 'roll1 on1';
		$('webcams-button').className = 'roll2';
		$('sexfinder-button').className = 'roll3';
	});
	
	$('webcams-button').addEvent('click', function(event) {
		hide('content1');
		hide('content3');
		show('content2');
		$('vod-button').className = 'roll1';
		$('webcams-button').className = 'roll2 on2';
		$('sexfinder-button').className = 'roll3';
	});
	
	$('sexfinder-button').addEvent('click', function(event) {
		hide('content1');
		hide('content2');
		show('content3');
		$('vod-button').className = 'roll1';
		$('webcams-button').className = 'roll2';
		$('sexfinder-button').className = 'roll3 on3';
	});
	
	/* add onclick event to all sexfind and livecam links */
	$('livecam').getElements('a').each( function(el,i) {
		el.addEvent('click', function(event) {
			closeBonusOpenJoin();
		});
	});
	$('sexfind').getElements('a').each( function(el,i) {
		el.addEvent('click', function(event) {
			closeBonusOpenJoin();
		});
	});
}

function checkArrows() {
	var left = $('scroll-left');
	var right = $('scroll-right');
	if (scrollDelta <= 0) {
		left.className = 'left off';
		left.setStyle('cursor', 'default');
	}
	else if (scrollDelta >= scrollLimit) {
		right.className = 'right off';
		right.setStyle('cursor', 'default');
	}
	else {
		left.className = 'left';
		left.setStyle('cursor', 'pointer');
		right.className = 'right';
		right.setStyle('cursor', 'pointer');
	}
}

function scroll(dir) {
	if (dir=='left' && scrollDelta > 0)
		scrollDelta -= scrollIncrement;
	else if (dir=='right' && scrollDelta < scrollLimit)
		scrollDelta += scrollIncrement;
	
	scrollGallery.start(scrollDelta,0);
	window.setTimeout(checkArrows, scrollSpeed);
}

function tourFadeOut(element) {
	$(element).getElements('div').each( function(el,i) {
		el.setStyle('display', 'block');
		var fade2black1 = new Fx.Tween(el);
		fade2black1.start('opacity', '0', '0.9');
	});
}
function tourFadeIn(element) {
	$(element).getElements('div').each( function(el,i) {
		el.setStyle('display', 'none');
		el.setStyle('opacity', '0');
	});
}

function resizeBox(el) {
	// only height needs resizing
	$(el).setStyle('height', (window.getSize().y-120)+'px');
}

function show(el) {
	$(el).setStyle('display', 'block');
}
function hide(el) {
	$(el).setStyle('display', 'none');
}

function back2tour(el) {
	tourFadeIn('fadeout1');
	tourFadeIn('fadeout2');
	hide(el);
	if (el=='bonus-box') 	bonusBoxOpen = false;
	if (el=='modles-box')	modelsBoxOpen = false;
}

function closeBonusOpenJoin() {
	back2tour('bonus-box');
	tourFadeOut('fadeout1');
	window.setTimeout( 'show(\'pop\')', '800');
}