var imgloaded = false,
	movie,
	startAt = 0,
	videoCategory = Array(0,70,116,999,999),
	hoverConfig = {    
	     over: slideDown,
	     timeout: 1000,  
	     out: slideUp
	};

$("document").ready(function(){
	$("#arrow").show();
	$("#nav div.navitem").bind('mouseenter',function(){
		$(this).each(function(){
			var navitemoffset = $(this).position();
			var arrowposition = parseInt(navitemoffset.left)+(parseInt($(this).width())/2)+26;
			$("#arrow").stop().animate({
				left: arrowposition
			},200);
		});
	}).each(function(i){
		$(this).click(function(){
			navto(i);
		});
	});
	$("#nav").bind('mouseleave',function(){
			var navitemoffset = $("#nav div.navitem.active").position();
			var arrowposition = parseInt(navitemoffset.left)+(parseInt($("#nav div.navitem.active").width())/2)+26;
			$("#arrow").stop().animate({
				left: arrowposition
			},200);
	});
	
	
	$.loadImages(['images/backgrounds/page-bg-01.jpg', 'images/backgrounds/page-bg-02.jpg', 'images/backgrounds/page-bg-03.jpg', 'images/backgrounds/page-bg-04.jpg', 'images/backgrounds/page-bg-05.jpg', 'images/backgrounds/page-bg-06.jpg'], function(){
		imgloaded = true;
	});
	var containerheight = parseInt($(window).height())-197;
	$("#fader").css('height',containerheight+'px');
	initVideo();
});

function navto(itemno) {
	$('.formError').remove();
	
	
	
	$("#nav div.navitem").removeClass('active');
	$("#nav div.navitem").eq(itemno).addClass('active');
	$("#content").html('&nbsp;');
	$("#content").load('templates/content-'+itemno+'.html',function(){
		
	});
	if(imgloaded){
	$("#fader").css("opacity",0.4);
	$("body").css('background-image', 'url(../images/backgrounds/page-bg-0'+itemno+'.jpg)');
	$("#fader").animate({
		opacity:0
	},400);
	videoJumpTo(videoCategory[itemno]);
	}
}

function initVideo() {
	$('#video').show();
	$('#video').flash({
		swf:'../video/videoplayer.swf',
		id:'videoplayer',
		width: '512',
		height:'288'
	});
	movie = document.getElementById('videoplayer');
}

function videoBuffered() {
	movie.playVideoAt(startAt);
}

function videoJumpTo(sec) {
	if(sec<999) {
		if($("#content").hasClass('novideo'))
		{
			startAt = sec;
			initVideo();
		}
		else
		{
			$('#video').show();
			movie.playVideoAt(sec);
		}
		$("#content").removeClass('novideo');
	} else {
		$('#video').fadeOut(200, function(){$(this).find('object').remove();});
		$("#content").addClass('novideo');
	}
	movie.pauseVideo();
}

function slideDown()
	{
		var l = $(this).attr('linkedID');
		$('#'+l).slideDown();
		setTimeout('fadeItemsIn()', 300);
	}
	function slideUp()
	{
		
	}
	function bullOver()
	{
		var l = $(this).attr('linkedID');
		if(!$('#'+l).hasClass('opened')){
		$('.opened').removeClass('opened').fadeOut(function()
			{
				$(this).children().each(function(){
					$(this).css('opacity', '0')});
			});
		}
		$('#'+l).addClass('opened');
		$(this).attr('originZindex', $(this).css('z-index'));
		$(this).css('z-index', '20');
	}
	function bullOut()
	{
		$(this).css('z-index', $(this).attr('originZindex'));
	}
	function fadeItemsIn()
	{
		var count = 0;
		$('.opened').children().each(function()
		{
			$(this).delay(count * 50).animate({opacity: '1'}, 'fast');
			count++;
		})
	}
	function fadeItemsOut()
	{
		var count = 0;
		alert('fadeOut');
		$('.opened').children().each(function()
		{
			$(this).animate({opacity: '0'}, 'fast');
			count++;
		})
	}


/*

	CASES AND DESCRIPTION

*/
	
	function caseClickHandler()
	{
		currPage = $(this).attr('ref');
		$('.caseBullItem').unbind('click');
		$('.currentCase').removeClass('currentCase');
		$(this).addClass('currentCase');
			var c = $('#caseWrapper').load('/templates/content-3-'+currPage+'.html', function(){
				$('.caseBullItem').click(caseClickHandler);
			});
	}

	function loadNextCase()
	{
		currCasePage++;
		if(currCasePage <= currCaseMaxPage)
		{
			$('.contentlist').slideUp(function(){
				$('.contentlist').load('/templates/content-3-'+currPage+'-'+currCasePage+'.html', function(){
					$('.contentlist').slideDown();
				});
			});
			(currCasePage == currCaseMaxPage) ? $('#nextCase').fadeOut() : $('#nextCase').fadeIn();
			(currCasePage == 1) ? $('#prevCase').fadeOut() : $('#prevCase').fadeIn();
		}
		paginateCase();
	}
	
	function loadPrevCase()
	{
		currCasePage--;
		if(currCasePage > 0)
		{
			$('.contentlist').slideUp(function(){
				$('.contentlist').load('/templates/content-3-'+currPage+'-'+currCasePage+'.html', function(){
					$('.contentlist').slideDown();
				});
			});
			(currCasePage == 1) ? $('#prevCase').fadeOut() : $('#prevCase').fadeIn();
			(currCasePage == currCaseMaxPage) ? $('#nextCase').fadeOut() : $('#nextCase').fadeIn();
		}
		paginateCase();
	}
	function loadCaseByRef()
	{
		currCasePage = $(this).attr('ref');
		$('.contentlist').slideUp(function(){
			$('.contentlist').load('/templates/content-3-'+currPage+'-'+currCasePage+'.html', function(){
				$('.contentlist').slideDown();
			});
			(currCasePage == 1) ? $('#prevCase').fadeOut() : $('#prevCase').fadeIn();
			(currCasePage == currCaseMaxPage) ? $('#nextCase').fadeOut() : $('#nextCase').fadeIn();
		});
		paginateCase();
	}
	function paginateCase()
	{
		var str = '';
		for(var i = 1; i<=currCaseMaxPage; i++){
			var style = (i == currCasePage) ? 'font-weight: bold; text-decoration: underline; color: #8B2E2A' : 'font-weight: normal; text-decoration: none;';
			str += '<a href="javascript:void(0)" ref="'+i+'" rel="paginator" style="'+style+'">'+i+'</a>';
			if(i!=currCaseMaxPage) str += '&nbsp;-&nbsp;';
		}
		
		$('#casesPagination').html(str);
		$('a[rel=\"paginator\"]').unbind('click').click(loadCaseByRef);
	}

/* Contact */

	function sendContactform(form, status)
	{
		if(status)
		{
			var data = $(form).serialize();
			var url = $(form).attr('action') + '.php';
			$.ajax({
				url: url,
				type: 'POST',
				data: data,
				dataType: 'text',
				success: function(data)
				{
					$('#ContactForm').slideUp(function(){
						$('#ContactFormSent').text(data).slideDown();
					});
				}
			});
		}
		return false;
	}

