/**
 * Date: 2008-01-12
 * @author Hans Jakob Gehring, http://www.codewhisper.com
 */

$.scrollTo.defaults.axis = 'x';
	
var scroller_index = 0;
var scroller_width = 0;

scroller_prior_click = function() {
	scrolling(scroller_index - 1);
}

scroller_next_click = function() {
	scrolling(scroller_index + 1);
}

scroller_up_click = function() {
	scroller_text('-', 300);
}

scroller_down_click = function() {
	scroller_text('+', 300);
}

scroller_text = function(dir, duration) {
	$('#scroller_text div').stop();
	if (duration > 0) {
		$('#scroller_text div').scrollTo(dir + '=50px', {axis: 'y', duration: duration});
	}
	else {
		$('#scroller_text div').scrollTo(dir + '=50px', {axis: 'y'});
	}
}

scroller_text_mouse = function(event, delta) {
    if (delta > 0) {
    	scroller_text('-', 0);
    }
    else {
    	scroller_text('+', 0);
    }

    return false;
}

scroller_controls = function() {
	var left = $('#image_scroll').offset().left;
	
	if (left == 0) {
		$("#scroller .prior").addClass('hidden_scroller');	
	}
	else {
		$("#scroller .prior").removeClass('hidden_scroller');
	}

	if (scroller_width + left > $('#image_container').width()) {
		$("#scroller .next").removeClass('hidden_scroller');
	}
	else {
		$("#scroller .next").addClass('hidden_scroller');
	}
	
	return left;
}

scroller_after = function() {
	var left = scroller_controls();
	
	if (left == -40 && $('#content').is('.contact')) {
		$('#image_container').stop();
		$('#image_container').scrollTo('40px');
	}
}

scrolling = function(index) {
	var children = $('#image_scroll').children();

	if (index >= 0 && children.length > index) {
		scroller_index = index;
		
		$('#image_container').stop();
		$('#image_container').scrollTo('li:eq(' + scroller_index + ')', 800, {onAfter:scroller_after, margin: true});
	}
}

image_scroll_position = function() {
	$.session('scroller_index', String(scroller_index));
	$.history.add(String(scroller_index));
}

text_height = function() {
	$('#scroller_text, #scroller_text div').height(70 + $(document).height() - 600);
	
	if ($('#scroller_text p').height() > $('#scroller_text').height()) {
		$('#scroller_text a.up, #scroller_text a.down').removeClass('hidden_scroller');
		
		$('#scroller_text div').bind('mousewheel', scroller_text_mouse);
	}
	else {
		$('#scroller_text a.up, #scroller_text a.down').addClass('hidden_scroller');
		
		$('#scroller_text div').bind('mousewheel', scroller_text_mouse);
	}
	
	if ($.browser.msie && $.browser.version == '6.0') {
		if ($(window).height() > 600) {
			$('#wrapper').height($(window).height());
		}
		else {
			$('#wrapper').height(600);
		}
	}
}

$(document).ready(function() {
	$('#image_container, #scroller_text, #scroller_text div').addClass('overflow_hidden');

	scroller_width = $('#image_scroll').width();
	if ($('#content').is('.contact')) {
		if ($.browser.msie && $.browser.version == '6.0') {
			scroller_width += 100;
		}
	}

	$('#image_container').scrollTo(0);
	$.scrollTo(0);
	
	$("#scroller .prior").click(scroller_prior_click);
	$("#scroller .next").click(scroller_next_click);
	
	$("#scroller_text .up").click(scroller_up_click);
	$("#scroller_text .down").click(scroller_down_click);
	
	$('#image_scroll a').click(image_scroll_position);
	
	scroller_controls();

	if ((window.location.pathname.indexOf('/') == window.location.pathname.length -1) || window.location.pathname.indexOf('/index.html') >= 0) {
		scroller_index = 0;
		
		if ($.history.getCurrent()) {
			scroller_index = $.history.getCurrent();
		}
		
		if 	($.session && $.session('scroller_index')  != undefined) {
			scroller_index = Number($.session('scroller_index'));
		}
		
		scrolling(scroller_index);
	}
	
	text_height();	
	
	$('#logo').click(function(){
		$.session('scroller_index', '0');
		$.history.add('0');
		
		window.location = '/';		
	});
});

$(window).resize(function() {	
	scroller_controls();
	
	text_height();
});