jQuery.fn.exists = function(){return jQuery(this).length>0;}

$(document).ready(function(){
	init();
	//menu();
	jmenu();
	jscrollpane();
	scrollto();
	maps();
});

$(window).load(function(){
	//$('.fade').fadeTo('slow', 1, function(){});
	window.setTimeout(function(){$('.fade').fadeTo('slow', 1, function(){});}, 500);
});

function init()
{
	// set client-container width
	
	if($('#client-container').exists() && $('#scrollnavigation').exists() && $('.client').exists())
	{
		var navigationLength = parseInt($('#scrollnavigation li').size());
		var clientWidth = parseInt($('.client').css('width').replace('px', ''));
		var clientMargin = parseInt($('.client').css('margin-right').replace('px', ''));
	
		$('#client-container').css('width', (navigationLength * (clientWidth + clientMargin)) + 'px');
	}
	
	$('#scrollnavigation li:eq(0) a').css('color', '#ed1c24');
}

function jmenu()
{
	var container = '#navigation';
	var trigger = '#menu';
	var subject = '#submenu';
	var menuHeight = 82;
	
	$(trigger).toggle(
		function(){
			$(subject).animate({height: menuHeight + 'px'}, 200);
		},
		function(){
			$(subject).animate({height: '0px'}, 200);
		}
	);
	
	/*$(container).mouseover(function() {
		$(subject).animate({height: menuHeight + 'px'}, 200);
	});
	
	$(container).mouseout(function() {
		$(subject).animate({height: '0px'}, 200);;
	});*/
}

function jscrollpane()
{
	if($('.scrollpane').exists())
	{
		$('.scrollpane').jScrollPane({
			showArrows: true,
			verticalDragMinHeight: 24,
			verticalDragMaxHeight: 24,
			horizontalDragMinWidth: 24,
			horizontalDragMaxWidth: 24
		});
	}
}

function jscrollnavigation()
{
	$('#scrollnavigation').localScroll({
		target: '#client-pane',
		axis: 'xy',
		duration: 200,
		offset: -110
	});
}

function scrollto()
{
	$('#scrollnavigation li').click(function(event)
	{		
		var duration = 200;
		var index = parseInt($('li').index(this));
		var navigationLength = parseInt($('#scrollnavigation li').size());
		var totalWidth = parseInt($('#client-container').css('width').replace('px', ''));
		var clientWidth = parseInt($('.client').css('width').replace('px', ''));
		var clientMargin = parseInt($('.client').css('margin-right').replace('px', ''));
		var trackWidth = parseInt($('#client-pane .jspTrack').css('width').replace('px', '') - 24);
		var clientCount =  parseInt($('#client-pane').css('width').replace('px', '') / (clientWidth + clientMargin));
		
		if(index == 0)
		{
			var position = 0;
		}
		else if((index + 1) >= navigationLength)
		{
			var position = ((index - 1) * -(clientWidth + clientMargin));
		}
		else
		{
			var position = ((index - 1) * -(clientWidth + clientMargin)) - (clientWidth / 2);
		}
		
		var dragPosition = parseInt((-position / (totalWidth - (clientCount * (clientWidth + clientMargin)))) * trackWidth);
		
		$('#client-pane .jspPane').animate({
			left: position
		}, duration, function(){
			
		});
		
		$('#client-pane .jspDrag').animate({
			left: dragPosition
		}, duration, function(){
			
		});
		
		$('#scrollnavigation li a').css('color', '#000000');
		$('#scrollnavigation li:eq(' + index + ') a').css('color', '#ed1c24');
	});
}

function maps()
{
	var latlng = new google.maps.LatLng(52.210782, 5.192394);
	var options = {zoom:14, center:latlng, mapTypeId:google.maps.MapTypeId.ROADMAP};
	var map = new google.maps.Map(document.getElementById('contact-map'), options);
	marker = new google.maps.Marker({position:latlng, map:map});
}

function menu()
{
	//tween.start();
	//tween.stop();
	//tween.resume();
	//tween.rewind();
	//tween.fforward();
	
	var state = true;
	var subject = document.getElementById('submenu');
	var trigger = document.getElementById('menu');
	var property = 'height';
	var from = 0;
	var to = 60;
	var time = .2;
	var suffix = 'px';
	
	var expand = new Tween(subject.style, property, Tween.regularEaseIn, from, to, time, suffix);
	var collapse = new Tween(subject.style, property, Tween.regularEaseIn, to, from, time, suffix);
	
	function toggle()
	{
		if(state)
		{
			expand.start();
		}
		else
		{
			collapse.start();
		}
		
		state = !state;
	}
	
	trigger.onclick = toggle;
}

function deleteItem(url)
{
	var accepted = confirm('Are you sure you want to delete this item?');
	
	if(accepted)
	{
		location.href = url;
	}
	else
	{
		//
	}
}

/*function scrollto(position)
{
	var navigationLength = $('#scrollnavigation li').size();
	var totalWidth = $('#client-container').css('width').replace('px', '');
	var clientWidth = 220;
	var clientMargin = 20;
	var clientCount = 2;
	var trackWidth = 436;
	var dragPosition = parseInt((-position / (totalWidth - (clientCount * (clientWidth + clientMargin)))) * trackWidth);
	
	$('#client-pane .jspPane').animate({
		left: position
	}, 200, function(){
		
	});
	
	$('#client-pane .jspDrag').animate({
		left: dragPosition
	}, 200, function(){
		
	});
}*/
