$(document).ready(function() {

	var wW = $(window).width();
	var wH = $(window).height();
	var bH = $('.inner').outerHeight(true);
	var iW = 0;
	var cH = 0;
	var oC = '';
	if (wW > 1440) { iW = 1440; oC = "wide";  }
	else { iW = 1000; oC = "narrow"; }
	
	var _left = (wW - iW)/2;
	$('#outter').addClass(oC);
	$('#carousel-outter').height(wH - 170);
	$('#carousel-inner').height(wH - 170 - $('#header').outerHeight(true));
	$('.inner').css('left', _left);
	
	centerNav(iW);
	
	$('input[type="text"], textarea').focus(function(){ if( this.value == this.defaultValue ) { this.value = ""; }}).blur(function() { if( !this.value.length ) { this.value = this.defaultValue; } });
	
	$('#menu ul li').hoverIntent({ 
		over: function() { 
			$(this).find('.off').animate({ opacity: 0 }, 200, 'easeInOutQuart'); 
			$(this).find('.on').animate({ opacity: 1 }, 200, 'easeInOutQuart'); 
		}, 
		timeout: 200, 
		out: function() { 
			$(this).find('.on').animate({ opacity: 0 }, 200, 'easeInOutQuart'); 
			$(this).find('.off').animate({ opacity: 1 }, 200, 'easeInOutQuart'); 
		} 
	});
	
	$('#carousel').cycle({
        fx:	'scrollLeft',
		easing: 'easeInOutSine',
        speed: 750,
        timeout: 10000,
        pager: '#nav',
        activePagerClass: 'active',
        pagerAnchorBuilder: function(idx, slide) { return '#nav li:eq(' + (idx) + ')'; },
		after: function(currSlideElement, nextSlideElement, options, forwardFlag) {
			var this_id = currSlideElement.id.replace('slide-', '');
			var next_id = nextSlideElement.id.replace('slide-', '');

			if (next_id == 1) $('#helper').animate({ opacity: 0 }, 500);
			if (next_id != 1) $('#helper').animate({ opacity: 1 }, 500);

			$('#text-' + this_id).animate({ opacity: 0}, 500, 'easeInSine', function() {
				$('#text-' + this_id).removeClass('active');
				$('#text-' + next_id).animate({ opacity: 1 }, 500, 'easeOutSine', function() {
					$('#text-' + next_id).addClass('active');
				});
			});
		}
	});
	
	$('.project-head').click(function() {
		var id = $(this).parent('div').attr('id').replace('project-', '');
		if ($('#project-' + id).hasClass('active-project')) return false;
		if ($('.project-inner').is(':animated')) return false;
		closeProject();
		throwProject(id);
	});
	
	var url = document.location.href;
	var url_split = url.split('#');
	var project_id = parseInt(url_split[1]);
	if (project_id && project_id > 0) throwProject(project_id);
	else throwProject(6);
	
	$('#cForm').submit(function() {
		$.ajax({
			type: 'POST',
			data: $('#cForm').serialize(),
			url: 'ajax.php?action=contact',
			success: function(rData) {
				if (rData == 1) {
					$('#cForm').animate({ opacity: 0 }, 500, function() {
						$('#cForm').html('<div id="cf-success">Jūsų užklausa sėkmingai išsiųsta!<br /><br />Mes, kaip galėdami greičiau, su Jumis susisieksime.</div>').animate({ opacity: 1 }, 500);
					});
				}
			}
		});
		
		return false;
	});
	
});

function throwContacts() {
	if ($('#contact-wrapper').is(':visible')) { $.scrollTo('100%', { duration: 500, easing: 'easeInSine' }); }
	else {
		$('#footer-outter').animate({ height: '+=400px' }, 500, 'easeInSine', function() {
			$.scrollTo('100%', { 
				duration: 500,
				easing: 'easeInSine',
				onAfter: function() { $('#contact-wrapper').fadeIn(500, 'easeOutSine'); }
			});
		});
	}
	
	return false;
}

function throwProject(id) {
	var project = $('#project-' + id)
	project.addClass('active-project');
	project.find('.project-load').fadeIn(300, function() {
		$.ajax({
			type: "GET",
			url: "ajax.php?action=getProject&project_id=" + id,
			success: function(data) {
				project.find('.project-inner').html(data);
				project.find('.project-inner ul').jcarousel({
					scroll: 1,
					animation: 1000,
					easing: 'easeInOutQuart',
					wrap: 'last'
				});
				project.find('.project-inner').animate({ height: 490 }, 500, function() {
					project.find('.project-load').fadeOut(300);
				});
			}
		});
	});
}

function closeProject() {
	var project = $('.active-project'); 
	project.find('.project-inner').animate({ height: 0 }, 500, function() {
		project.removeClass('active-project');
		$(this).html();
	});
}

function centerNav(innerWidth) {
	var navWidth = 122;
	var navPos = (innerWidth - navWidth)/2;
	$('#nav').css('left', navPos);
}


