

function getCurAppNo() {
	var currentAppNo = $('#appTitle').html();
	currentAppNo = currentAppNo.split('<!--')[1] + '';
	currentAppNo = currentAppNo.replace('-->','');
	return currentAppNo;
}





function setCurrentAppStoreLink(icoId) {

	if (icoId == 'ico1') {
		$('#appBadge').attr('href','http://itunes.apple.com/app/defcow-one/id388858136?mt=8');	
	} else if (icoId == 'ico2') {
		$('#appBadge').attr('href','http://itunes.apple.com/us/app/jhoops/id377138563?mt=8');	
	} else if (icoId == 'ico3') {
		$('#appBadge').attr('href','http://itunes.apple.com/us/app/t-minus-counting/id348569225?mt=8');	
	} else if (icoId == 'ico4') {
		$('#appBadge').attr('href','http://itunes.apple.com/us/app/halloween-screams-dreams/id470825549?mt=8');	
	} else if (icoId == 'ico5') {
		$('#appBadge').attr('href','http://itunes.apple.com/us/app/origami-airplane-folds-in-3d/id440455939?mt=8&ls=1');	
	} else if (icoId == 'ico6') {
		$('#appBadge').attr('href','http://itunes.apple.com/us/app/origami-fish-folds-in-3d./id447826940?ls=1&mt=8');	
	} else if (icoId == 'ico7') {
		$('#appBadge').attr('href','http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=336730591&mt=8&s=143519');	
	} else if (icoId == 'ico8') {
		$('#appBadge').attr('href','http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=322720643&mt=8&s=143441');	
	} else if (icoId == 'ico9') {
		$('#appBadge').attr('href','http://itunes.apple.com/us/app/jackalope-in-the-art-not-being/id442170788?mt=8&ls=1');	
	}

}







function setCurrentApp(ico, speed) {
	
	
	ico.css('z-index', 100);
	ico.animate({
		left:'388px',
		top:'100px',
		width:'114px',
		height:'114px',
		opacity:1.0
		},
		speed, 
		function() {
			
			
			$('#appTitle').fadeIn(500);
			
			setCurrentAppStoreLink(ico.attr('id'));
			
			
			localStorage.currentAppNo = getCurAppNo();
		}
		
	);
	
	

}

function animateLoading(cnt, total, rev) {
	cnt += 1;
	if (cnt == total) {
		animateLoading(-1, total, !rev);
	} else {
	
		var divNo = cnt;
		if (rev) {
			divNo = total - 1 - cnt;
		}
		
		$('#loadDiv' + divNo).fadeIn(50,
			function() {
				$(this).fadeOut(300);
				if ($(this).attr('name') == 'loading' || cnt != total - 1) {
					animateLoading(cnt, total, rev);
				}
			}
		);
	}

}

function showLoading() {


	$('.loading').attr('name','loading');
	var loadingList = $('.loading').toArray();
	
	for (var i=0; i < loadingList.length; i++) {
		var left = i * 70 + 90;
		$('#loadDiv' + i).css('left', left + 'px');
	}
	
	animateLoading(0, loadingList.length, false);
}

function hideLoading() {

	$('.loading').attr('name','');
}



function resetIcons(icoId, appName, speed) {
	$('#appTitle').fadeOut(speed);
	for (var i=1; i<10; i++) {
		var id = 'ico' + i;
		if (icoId != id) {
			$('#' + id).css('z-index',0);
			$('#' + id).animate({
				left:(60 + i * 74) + 'px',
				top:'30px',
				width:'50px',
				height:'50px'
				}, 
				speed,
				function () {
					$('#appTitle').html(appName);
				}
			);
		
		}
		
	}
}

function loadCenterDiv(moveLeftDist, ico, fadeTime) {

	if (moveLeftDist == 0) {
	
		
		$('#centerDiv').fadeOut(fadeTime,
			function() {
				loadDiv(moveLeftDist, ico);
			}
		);
	} else {
		
		$('#centerDiv').animate({
			left:moveLeftDist + 'px'
			},
			500,
			function() {
				loadDiv(moveLeftDist, ico);
			}
		);
		
	}
}

function loadDiv(moveLeftDist, ico) {
	
	showLoading();
	$('#centerDiv').css('left',-moveLeftDist + 'px');
	var forceReload = '';
	forceReload = '?time=' + new Date().getTime();
	var cApp = getCurAppNo();

	if (cApp != 'undefined') {
		var divUrl = 'divs/a' + cApp + 's' + $('#centerDiv').attr('name') + '.html' + forceReload;

		$.ajax({
			url: divUrl,
			dataType: 'html',
			success: 
				function(html) {
					$('#centerDiv').html(html);
					if (moveLeftDist == 0) {
						setCurrentApp(ico, 500);
					}
					$('.loadedImg').load(
						function() {
							hideLoading();
							if (moveLeftDist == 0) {
								
								$('#centerDiv').fadeIn(500);
							} else {
								$('#centerDiv').animate({
									left:'0px'
									},
									500
								);
							}
						}
					);
				
				}
			}
		);


	}
}



$(document).ready(
	function() {		
	

		var curAppNo = '5';
		
		if (localStorage.currentAppNo) {
			curAppNo = localStorage.currentAppNo;
		}
		
		if (curAppNo == 'undefined') {
			curAppNo = '5';
		}
		
		var curAppId = '#ico' + curAppNo;
		
		//setCurrentApp($(curAppId), 0);
		
		$('#centerDiv').attr('name', '1');

		
		resetIcons('', $(curAppId).attr('name'), 0);
		

		
		loadCenterDiv(0, $(curAppId), 0);
		
		$('.ico').fadeIn(500);
		
		$('.ico').hover(
			function() {
				$(this).removeClass('ensmallen');
				$(this).addClass('enbiggen');
			},
			function() {
				$(this).removeClass('enbiggen');
				$(this).addClass('ensmallen');
			}
		);
		
		
		$('.ico').click(
			function() {
			
				if ($('.loading').attr('name') != 'loading') {
					$('#centerDiv').attr('name', '1');
	
					resetIcons($(this).attr('id'), $(this).attr('name'), 200);
	
					loadCenterDiv(0, $(this), 200);
				
				}
			}
		);
		
		$('.arrow').hover(
			function() {
				$(this).removeClass('transTrans');
				$(this).addClass('opaqueClass');
			},
			function() {
				$(this).removeClass('opaqueClass');
				$(this).addClass('transTrans');
			}
		
		);
		
		$('.arrow').click(
			function() {
				
				var moveLeftDist = screen.width + 1000;
				if ($(this).attr('id') == 'arrowLeft') {
					$('#centerDiv').attr('name', $('#prevPage').attr('value'));
					loadCenterDiv(moveLeftDist, 0, 0);
				} else {
					$('#centerDiv').attr('name', $('#nextPage').attr('value'));
					loadCenterDiv(-moveLeftDist, 0, 0);
				}
				
				
				
			}
		
		)
	
	}
);


