$(function() {
	
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var lastSize;
	var newWidth;
	
	var liWidth = 210;
	var ulWidth = ( $('ul.apps li').size() + 1) * ( liWidth );
	
	var position = 0;
	var maxLength = $('#slidenav').width();
	var shownNum = Math.floor(maxLength / liWidth);
	
	var resizeTimer = null;
	
	var shownNum;
	
	window.onload = function() {
		$('.images ul').css('width', $('.images ul li').size() * $('.images ul li img').width() );
	}
	
	/******************************************************************************/
	$('#slidenav').each(function() {
		
		var prev = $('a.left', this);
		var next = $('a.right', this);
		var slider = $('ul.apps', this);
		var thumbs = $('ul.apps li').size();
		maxLength = $('#slidenav').width();
		
		$(prev).click(function() {
			if (position < 0) {
				position += liWidth;
				$(slider).animate({
					left: position
				}, 200);
				//alert('position is '+position);
			}
		});
		
		$(next).click(function() {
			var checkVal = ((ulWidth - maxLength - 210) * -1) - position;
			if ( checkVal < 5) {
				position -= liWidth;
				$(slider).animate({
					left: position
				}, 200);
			}
		});
	});
	
	function changeSlider() {
		checkSize();
		maxLength = windowWidth - 50;
		//alert('maxLength is '+maxLength);
		shownNum = Math.ceil(maxLength / liWidth);
		
		if (shownNum < 5) shownNum = 5;
		newWidth = shownNum * liWidth;
		
		var lis = $('ul.apps li').size();
		//var endMarker = Math.abs(position/liWidth) + shownNum;
		var endMarker = Math.abs(position/liWidth) + shownNum;
		
		if(newWidth != lastSize) {
			if (endMarker > lis && position != 0) {
				//$('#slidenav').css('width', maxLength);
				var diff = endMarker - lis;
				position = position + (liWidth * diff);
				$('ul.apps').animate({
					left: position
				}, 200);
			} else {
				//$('#slidenav').css('width', maxLength);
			}
		
		}
		
		lastSize = newWidth;
	}
	
	/******************************************************************************/
	// RESIZE TRIGGER
	$(window).resize(function() {
		var resizeTimer = null;
		clearTimeout(resizeTimer);
		resizeTimer = setTimeout(resizeComplete, 200);
	});
	
	function resizeComplete() {
		changeSlider();
	}
	
	function checkSize() {
		windowWidth = $(window).width();
		windowHeight = $(window).height();
	}
	
	/******************************************************************************/
	// IMAGE GALLERY
	$('#gallery').each(function() {
		
		var trigger = $('.btn a', this);
		var images = $('.images ul', this);
		//var activeImg = $('.images').find('img').css('display', 'block');
		var shownImg;
		
		$(trigger).click(function() {
			var trigID = $(this).attr('id');
			/*
			$(images).fadeOut();
			shownImg = $('#slide_'+trigID).fadeIn();
			*/
			
			$(images).animate({
				'left' : ((trigID - 1) * $('.images ul li img').width() ) * -1
			}, 200);
			
			$(trigger).removeClass('active');
			$(this).addClass('active');
		});
		
		$(images).click(function() {
			//alert('derpinstein');
		});
	});
	
	/******************************************************************************/
	// DETAILS FUNCTIONALITY
	$('#details .center').each(function() {
		
		var trigger = $('.subnav a', this);
				
		$(trigger).click(function() {
			
			var curDivs = new Array( $('div.desc'), $('div.features'), $('div.faqs'), $('div.reviews'), $('div.friend') );
			var div = $(this).attr('id');
			
			for (i=0; i<curDivs.length; i++) {
				$(curDivs[i]).hide();
				if (i == 4) $('.'+div).fadeIn(400);
			}
			
			$(trigger).removeClass('active');
			$(this).addClass('active');
		});
		
	});
	
	/******************************************************************************/
	// INIT
	
	if ( $('.appPage').length > 0 ) {
		$('ul.apps').css('width', ulWidth);
		$('.desc').fadeIn(200);
		changeSlider();
		$('#slide_1').show();
		$('#1').addClass('active');
	}
	
	if ( $('.home').length > 0 ) {
		$("#app_nav img")
		.mouseover(function() { 
			var src = $(this).attr("src").match(/[^\.]+/) + "-on.jpg";
			$(this).attr("src", src);
		})
		.mouseout(function() {
			var src = $(this).attr("src").replace("-on", "");
			$(this).attr("src", src);
		});
	}
});

/******************************************************************************/
// INPUT FIELD TEXT SWAP
function clearText(input) {
	if (input.defaultValue == input.value) input.value = '';
}
function restoreText(input){
	if (input.value == '') input.value = input.defaultValue;
}
