/******************************************************************************************************************/
// MENU FX FUNCTIONS
/******************************************************************************************************************/

function initCursor(){
	if ($('#menu .sel').size()) {
		seloffset = $('#menu .sel').offset();
		selpos = seloffset.left + ($('#menu .sel').width() / 2) - 5;
		$('#menu .cursor').stop().animate({ opacity: '1', left: selpos }, 300);
	}
}

function menuFx(){
	$('#menu').prepend('<div class="cursor"><!-- pretty cursor --></div>');
	$('#menu .cursor').css('opacity','0');
	
	// initializing cursor position
	initCursor();
	
	$(window).resize(function(){ initCursor() });
	
	// menu hover
	$('#menu A').hover(function(){
		offset = $(this).offset();
		pos = offset.left + ($(this).width() / 2) - 5;
		$('#menu .cursor').stop().animate({ opacity: '1', left: pos }, 300);
	}, function(){
		if ($('#menu .sel').size()) $('#menu .cursor').stop().animate({ left: selpos }, 500);
		else $('#menu .cursor').stop().animate({ opacity: '0' }, 500);
	});
}

/******************************************************************************************************************/
// LAST WORKS FUNCTIONS
/******************************************************************************************************************/

function magnify(i){
	$('#lastworks .works LI:eq('+ i +')').addClass('initbig');
	
	dh = $('#lastworks .works LI:eq('+ i +') .desc').height() + 30;
	
	$('#lastworks .works LI:eq('+ i +') .desc').show();
	$('#lastworks .works LI:eq('+ i +') .desc').animate({ marginTop: '-'+dh, opacity: 0.8 }, 500);
	
	$('#lastworks .works LI:eq('+ i +') .screen').css({
		marginTop: '12px',
		marginLeft: '12px'
	});
	
	$('#lastworks .works LI:eq('+ i +'), #lastworks .works LI:eq('+ i +') .frame').animate({
		width: '424px',
		height: '272px'
	}, 200);
	
	$('#lastworks .works LI:eq('+ i +') .screen, #lastworks .works LI:eq('+ i +') .screen IMG').animate({
		width: '400px',
		height: '240px'
	}, 200);
}

function minify(i){
	$('#lastworks .works LI:eq('+ i +')').removeClass('initbig');
	
	dh = $('#lastworks .works LI:eq('+ i +') .desc').height() + 30;
	
	$('#lastworks .works LI:eq('+ i +') .desc').animate({ marginTop: dh, opacity: 0 }, 500);
	$('#lastworks .works LI:eq('+ i +') .desc').hide();
	
	$('#lastworks .works LI:eq('+ i +') .screen').css({
		marginTop: '7px',
		marginLeft: '7px'
	});
	
	$('#lastworks .works LI:eq('+ i +'), #lastworks .works LI:eq('+ i +') .frame').animate({
		width: '260px',
		height: '167px'
	}, 200);
	
	$('#lastworks .works LI:eq('+ i +') .screen, #lastworks .works LI:eq('+ i +') .screen IMG').animate({
		width: '246px',
		height: '148px'
	}, 200);
}

function slideTo(index){
	minify(current);
	magnify(index + 1);
	
	mleft = '-'+ (w * index ) +'px';
	$('#lastworks .works UL').animate({ marginLeft: mleft }, 500);
	
	current = index + 1;
	sel = index;
	
	$('#lastworks .bullets LI').removeClass('sel');
	$('#lastworks .bullets LI:eq('+ sel +')').addClass('sel');
	
	$('#lastworks .next, #lastworks .prev').css({
		cursor: 'pointer',
		opacity: 1
	});
		
	if (index == 0) {
		$('#lastworks .prev').css({
			cursor: 'auto',
			opacity: 0.4
		});
	}
	
	if (index == (num - 3)) {
		$('#lastworks .next').css({
			cursor: 'auto',
			opacity: 0.4
		});
	}
}

function handleLastWorks(){
	$('#lastworks .works UL').prepend('<li class="stop-left"><!-- stop left --></li>');
	$('#lastworks .works UL').append('<li class="stop-right"><!-- stop right --></li>');
	
	$('#lastworks .desc').css({ opacity: 0});
	$('#lastworks .desc').hide();
	
	$('#lastworks .bullets LI:eq(3)').addClass('sel');
	
	num = $('#lastworks .works LI').size();
	w = 310;
	
	current = 4;
	sel = 3;
	magnify(current);
	
	mleft = '-'+ (w * (current - 1) ) +'px';
	$('#lastworks .works UL').animate({ marginLeft: mleft }, 500);
		
		
	$('#lastworks .next').click(function(){
		if (current < (num - 2)) {
			
			$('#lastworks .prev').css({
				cursor: 'pointer',
				opacity: 1
			});
			
			minify(current);
			
			mleft = '-'+ (w * current ) +'px';
			$('#lastworks .works UL').animate({ marginLeft: mleft }, 500);
			
			current ++;
			sel ++;
			
			magnify(current);
			
			$('#lastworks .bullets LI').removeClass('sel');
			$('#lastworks .bullets LI:eq('+ sel +')').addClass('sel');
			
			if (current == (num - 2)) {
				$('#lastworks .next').css({
					cursor: 'auto',
					opacity: 0.4
				});
			}
		}
	});
	
	$('#lastworks .prev').click(function(){
		if (current > 1) {
			
			$('#lastworks .next').css({
				cursor: 'pointer',
				opacity: 1
			});
			
			minify(current);
			
			current --;
			sel --;
			
			mleft = '-'+ (w * (current - 1)) +'px';
			$('#lastworks .works UL').animate({ marginLeft: mleft }, 500);

			magnify(current);
			
			$('#lastworks .bullets LI').removeClass('sel');
			$('#lastworks .bullets LI:eq('+ sel +')').addClass('sel');
			
			if (current == 1) {
				$('#lastworks .prev').css({
					cursor: 'auto',
					opacity: 0.4
				});
			}
		}
	});
	
	$('#lastworks .bullets LI').click(function(){
		if (!$(this).hasClass('sel')) {
			j = $('#lastworks .bullets LI').index(this);
			slideTo(j);
		}
	});
	
	$('#lastworks .works .screen').click(function(){
		if (!$(this).parent('LI').hasClass('initbig')) {
			j = $('#lastworks .works .screen').index(this);
			slideTo(j);
			return false;
		}
	});
}

/******************************************************************************************************************/
// SERVICES TAB FUNCTION
/******************************************************************************************************************/

function handleServices(){
	$('#services .tab').hide();
	$('#services .tab:eq(0)').show();
	$('#services .nav LI:eq(0)').addClass('sel');
	
	$('#services .nav A').hover(function(){
		if (!$(this).parent('LI').hasClass('sel')) {
			$('#services .nav LI').removeClass('sel');
			$(this).parent('LI').addClass('sel');
			
			myid = '#' + $(this).attr('class');
			
			$('#services .tab').hide();
			$(myid).fadeIn(100);
			//$(myid).show();
		}
	}, function(){
		// nothing to do :)
	});
}

/******************************************************************************************************************/
// CONTACT FORM FUNCTION
/******************************************************************************************************************/

function handleForm(){
	$('.fieldwrap INPUT').focus(function(){
		$(this).css('background-position','0px -30px');
	});
	
	$('.fieldwrap INPUT').blur(function(){
		$(this).css('background-position','0px 0px');
	});
	
	$('.fieldwrap TEXTAREA').focus(function(){
		$(this).css('background-position','0px -300px');
	});
	
	$('.fieldwrap TEXTAREA').blur(function(){
		$(this).css('background-position','0px 0px');
	});
}


/******************************************************************************************************************/
// FOOTER FUNCTIONS
/******************************************************************************************************************/

function showLogo(i){
	logos = $('.welove A').size();
	
	$('.welove A').hide();
	$('.welove A:eq('+ i +')').fadeIn(500);
	
	i++;
	if (i >= logos) i = 0;
	
	window.setTimeout('showLogo('+ i +')', 4000);
}

function handleWeLove(){
	logos = $('.welove A').size();
	
	if (logos > 1) {
		showLogo(0);
	}
	
}

/******************************************************************************************************************/
// TAGS FUNCTION
/******************************************************************************************************************/

function handleTags(){
	$('.tags A').wrapInner('<span></span>');
}

/******************************************************************************************************************/
// On document load...
/******************************************************************************************************************/

$(function(){
	// start menu fx
	menuFx();
	
	// contact form fx
	handleForm();
	
	// footer function
	handleWeLove();
	
	// tags handle
	handleTags();
});