var main, default_start_col, container, scroll, cols, curr_col,
	prev_link, next_link, scroll_interval,
	header_image0, header_image1, header_image2,
	header_image3, header_image4;

var hash=location.hash;

var key = {
	l: 37, //Left Key
	r: 39, //Right Key
	b: 40, //Top Key
	t: 38, //Bottom Key
	ok: 13 //Return Key
};

jQuery(function($) {

	duration 	 		= 1200;
	default_start_col	= '#main .cols[id^=slice]:first';
	main				= $('#main');
	container 	 		= main.find('.container');
	scroll 		 		= main.find('.scroll');
	prev_link	 		= $('#prev_arrow');
	next_link	 		= $('#next_arrow');

	main.before('<div id="header_image0"></div>');
	main.before('<div id="header_image1"></div>');
	main.before('<div id="header_image2"></div>');
	main.before('<div id="header_image3"></div>');
	main.before('<div id="header_image4"></div>');

	header_image0 		= $('#header_image0');
	header_image1  		= $('#header_image1');
	header_image2  		= $('#header_image2');
	header_image3  		= $('#header_image3');
	header_image4  		= $('#header_image4');

	getStartCol();

	prev_link.click(function(){
		getScrollTo(-1);
		scrollContentTo();
		return false;
	});

	next_link.click(function(){
		getScrollTo(1);
		scrollContentTo();
		return false;
	});

	getScrollTo(null);
	scrollContentTo();
	$('body').scrollTo(0, 0, {axis: 'y'});

	if (document.addEventListener) {
		document.addEventListener("keydown", getKeyCode, true);
	} else if (document.attachEvent) {
		document.attachEvent ("onkeydown", getKeyCode);
	} else {
		document.keydown = getKeyCode;
	}

	$('#dropdown .wrapper').hover(
		 function() {
		  		$(this).parent().addClass('active');
		 },
		 function() {
		 	$(this).parent().removeClass('active');
		 }
	 );
});


function getKeyCode(e){

	switch (e.keyCode) {

		case key.l:
		 	if (getScrollTo(-1)) {
				prev_link.addClass('focus');
				scrollContentTo();
			}
			break;
		case key.r:
		 	if (getScrollTo(1)) {
				next_link.addClass('focus');
				scrollContentTo();

			}
			break;
	}
	return false;
}

function getStartCol(){

	var start_col = (hash != '') ? hash : default_start_col;

	curr_col = $(start_col).attr('id');

	if (curr_col == undefined || curr_col == ''){
		curr_col = $(default_start_col).attr('id');
	}
}

function scrollContentTo(){


	scroll_interval = window.setInterval("scrollBackground()", 2);

	container
		.scrollTo($('#'+curr_col),
				  duration, {
				  axis: 'x',
				  easing:'easeout',
				  onAfter: function(){
				  	window.clearInterval(scroll_interval);
				  	getScrollTo(null);
					prev_link.removeClass('focus');
					next_link.removeClass('focus');
				  }
		});
}

function scrollBackground(){

	var position = scroll.position();
	var header_image0_pos = ((position.left*1)/14) - 50;
	var header_image1_pos = ((position.left*1)/12) + 400;
	var header_image2_pos = ((position.left*1)/6) + 200;
	var header_image3_pos = ((position.left*1)/6)  - 10;
	var header_image4_pos = ((position.left*1)/5)  + 610;

	header_image0.css('background-position',header_image0_pos+'px 0px');
	header_image1.css('background-position',header_image1_pos+'px 0px');
	header_image2.css('background-position',header_image2_pos+'px 0px');
	header_image3.css('background-position',header_image3_pos+'px 0px');
	header_image4.css('background-position',header_image4_pos+'px 0px');
}

function getScrollTo(dir){

	var prevAll = $('#'+curr_col).prevAll('.cols[id^=slice]');
	var nextAll = $('#'+curr_col).nextAll('.cols[id^=slice]');
	var prev_hash = '';
	var next_hash = '';

	switch(prevAll.length){
		case 1:  prev_hash = prevAll.eq(0).attr('id'); break;
		case 2:  prev_hash = prevAll.eq(1).attr('id'); break;
		case 3:  prev_hash = prevAll.eq(2).attr('id'); break;
		default: prev_hash = prevAll.eq(3).attr('id'); break;
	}

	if (prevAll.length > 0 && prev_link.is(':hidden')){
		if ($.browser.msie){
			prev_link.show();
		} else{
			prev_link.fadeIn(duration/2);
		}
		prev_link.attr('href','#'+prev_hash);
	}
	else if (prevAll.length < 1 && !prev_link.is(':hidden')){
		if ($.browser.msie){
			prev_link.hide();
		} else{
			prev_link.fadeOut(duration/2);
		}
		prev_link.attr('href','#');
	}

	switch(nextAll.length){
		case 4:  next_hash = nextAll.eq(0).attr('id'); break;
		case 5:  next_hash = nextAll.eq(1).attr('id'); break;
		case 6:  next_hash = nextAll.eq(2).attr('id'); break;
		default: next_hash = nextAll.eq(3).attr('id'); break;
	}

	if (nextAll.length > 3 && next_link.is(':hidden')){
		if ($.browser.msie){
			next_link.show();
		} else{
			next_link.fadeIn(duration/2);
		}
		next_link.attr('href','#'+next_hash);
	}
	else if (nextAll.length < 4 && !next_link.is(':hidden')){
		if ($.browser.msie){
			next_link.hide();
		} else{
			next_link.fadeOut(duration/2);
		}
		next_link.attr('href','#');
	}

	if (curr_col == next_hash ||
		curr_col == prev_hash) {
		return false;
	}
	else {
		if (dir > 0 && nextAll.length > 3) {
			curr_col = next_hash;
		}
		else if (dir < 0 && prevAll.length > 0) {
			curr_col = prev_hash;
		}
		return true;
	}
}