$(document).ready(function() {
	// Defines current section and its ID
	page_cur = $('#interface .act');
	page_cur_id = $(page_cur).attr("id");
	
	page_first = $('#interface .container:first');
	page_last = $('#interface .container:last');
	
	if (!page_cur_id) {
		page_cur = $('#interface .container:first');
		page_cur_id = $(page_cur).attr("id");
		page_cur.addClass('act');
	}
	
	closeIFace();
	
	
	// CE listing
	if (window.location.hash) {
		actCE = $('#ce-'+window.location.hash.substr(1).replace(/\D+/, ''));
		actNavCE = $('#nav-ce-'+window.location.hash.substr(1).replace(/\D+/, ''));
	} else {
		actCE = $('.ce:first');
		actNavCE = $('.nav-ce li:first');
	}
	$('.ce, #footer').hide();
	$('#footer').fadeIn(500).addClass('act');
	actCE.fadeIn(500).addClass('act');
	$('.ce *').click(function() {
		scrollCE($(this),'.ce');
	});
	
	// TODO with ahchors
	actNavCE.addClass('act');
	$('.nav-ce a').click(function() {
		var cur = $('.ce:act');
		var next = $(this).attr("href").replace(/\D+/, '');
		
		scrollCE(cur,'.ce', $('#ce-'+next));
		
		return false;
	});
	
	$('.news-latest-item').hide();
	$('.news-latest-item:first').fadeIn(500).addClass('act');
	$('.news-latest-item').click(function() {
		scrollCE($(this),'.news-latest-item');
	});
	//autoScrollNews();
	
	
	// Interface buttons actions
	$('#interface .btn').click(function(){
		var cur = $('#interface .act');
		
		if ($(this).hasClass('btn-all')) {
			var next = $('#interface .nav-icons');
		}
		
		if ($(this).hasClass('btn-next')) {
			var next = cur.next('#interface .container');
		}
		
		if ($(this).hasClass('btn-prev')) {
			var next = cur.prev('#interface .container');
		}
		
		
		if (!$(this).hasClass('btn-open')) {
			scrollIFace(cur,next);
		}
		
		return false;
	});
	
	$('#interface .btn-open').click(function(){
		var cur = $('#interface .act');
		openIFace(cur);
	});
	
	// Close IFace on click
	$('#interface').click(closeIFace);
	
	
	$('.nav-icons .span-2 a, .navBrands .navBrands-item a').hover(
		function(){
			$('#interface .content').stop(1,1);
			var m = ($(this).width() - $('img:last', this).width())/2;
			var ce = '#'+$('img:last',this).attr('class');	
			
			$('img:first',this).stop(1,1);
			$('img:first',this).css({'marginLeft':m}).fadeIn(500);
			$('img:last',this).animate({'opacity':'0'},100);
			
			//$('#interface .act').removeClass('act');
		
			
			setTimeout(function(){
			//	$('#interface .content:visible').fadeOut(100);
			//	$(ce).addClass('act').fadeIn(300);
			},100);
		},
		function(){
			//$('#interface .act').removeClass('act').fadeOut(100);
			setTimeout(function(){
			//	$('#interface .cur.act').fadeIn(500);
			},500);
			//$('img:first',this).fadeOut(200);
			$('img:first',this).stop(1,1);
			$('img:first',this).fadeOut(300);
			$('img:last',this).animate({'opacity':'1'},300);
		}
	);
	
	
	
	setTimeout(function(){
		// valignPageCenter('#content');
		valignIFaceCenter('#interface .valign-center');
	
		//placeFooter();
	}, 50);
});

$(window).load(function(){
	
});

function valignPageCenter(selector) {
	var windowHeight = document.body.clientHeight;
	var totalHeight = $('#content').height() + $('#header').height() + $('#footer').height();
	var top = (windowHeight - totalHeight ) / 2;
	
	$(selector).each(function(){
		if (windowHeight > totalHeight) {
			$(selector).animate({'marginTop':top},100);
		}
	});
}

function valignIFaceCenter(selector) {
	$(selector).each(function(){
		var top = ($(this).parent().height() - $(this).height() ) / 2;
		
		$(selector).css('margin-top', top);
	});
}

function scrollCE (cur,cls,next){
	if(!next) {
		var next = $(cur).nextAll(cls+':first');
	}
	
	var next_id = $(next).attr('id');
	
	if(!next_id) {
		var next = $(cur).prevAll(cls+':last');
		var next_id = $(next).attr('id');
	}
	
	if (!next_id) {
		return false;
	}
	
	$(cls).stop(1,1);
	$(cur).fadeOut(250).removeClass('act');
	
	setTimeout(function(){
		$(next).addClass('act').fadeIn(250);
		$('#footer').fadeIn(250);
		$('.nav-ce li').removeClass('act');
		$('.nav-ce #nav-'+next_id).addClass('act');
		
	}, 260);
	
	setTimeout(function(){
		//valignPageCenter('#content');
		//placeFooter();
	}, 300);
}

function autoScrollCE () {
	setTimeout(function(){
		scrollCE($('.ce:visible'),'.ce');
		autoScrollCE();
	},10000);
}

function autoScrollNews () {
	setTimeout(function(){
		scrollCE($('.news-latest-container .act'),'.news-latest-item');
		autoScrollNews();
	},7000);
}


function closeIFace (){
	$('#interface .container:visible').fadeOut(200);
	setTimeout(function(){
		$('#interface').animate({
			"top": "-100%"
		}, 300);
	}, 200);
	
	
	setTimeout(function(){
		$('#interface').animateToClass(page_cur_id, 500);
		$('#interface .act').removeClass('act');
		$(page_cur).addClass('act');
	}, 400);
	
	setTimeout(function(){
		$('#interface .btn-open').slideDown(200);
	}, 500);
}

function openIFace (cur) {		
	$('#interface').stop(1,1).animate({
		"top": "0"
	}, 400);
	
	$('#interface').animateToClass(cur.attr('id'), 250);
	
	if (cur.attr('id') == 'i-all') {
		$('#interface .btn-all:visible').hide();
	} else {
		$('#interface .btn-all').show();
	}
	
	if (cur.attr('id') == page_first.attr('id')) {
		$('#interface .btn-prev').hide();
	} else {
		$('#interface .btn-prev').show();
	}
		
	if (cur.attr('id') == page_last.attr('id')) {;
		$('#interface .btn-next').hide();
	} else {
		$('#interface .btn-next').show();
	}
	
	$('#interface .btn-open').slideUp(100);
	
		
	setTimeout(function(){
		$(cur).fadeIn(500);
		
	}, 400);
		
	setTimeout(function(){
		valignIFaceCenter('#interface .valign-center');
	},450);
}


function placeFooter () {
	var windowHeight = document.body.clientHeight;
	var totalHeight = $('#content').height() + $('#header').height() + $('#footer').height() + 200;
	
	if (totalHeight > windowHeight) {
		$('#footer.absolute').hide().removeClass('absolute').addClass('relative').fadeIn(200);
	} else {
		$('#footer.relative').hide().removeClass('relative').addClass('absolute').fadeIn(200);
	}
}


function scrollIFace (cur,next) {
	$('#interface').stop(1,1);
	next_id = $(next).attr('id');
	
	if (next_id != 'i-all') {
		$('#interface .btn-all:hidden').fadeIn(100);
	} else {
		$('#interface .btn-all:visible').fadeOut(100);
	}
	
	if (page_last.attr('id') == next_id) {
		$('#interface .btn-next').fadeOut(100);
	} else {
		$('#interface .btn-next:hidden').fadeIn(100);
	}
	
	if (page_first.attr('id') == next_id) {
		$('#interface .btn-prev').fadeOut(100);
	} else {
		$('#interface .btn-prev:hidden').fadeIn(100);
	}
	
	if (next_id) {
		setTimeout(function(){
			$('#interface').stop(1,1).animateToClass(next_id, 250);
		}, 150);
		
		$(cur).fadeOut(150).removeClass('act');
		
		setTimeout(function(){
			$(cur).hide().removeClass('act');
			$(next).fadeIn(250).toggleClass('act');
			$('#interface').stop(1,1).animateToClass(next_id, 250);
		}, 300);
		
		setTimeout(function(){
			valignIFaceCenter(next_id);
		}, 350);
		
	}
}

