$(document).ready(function(){
	// Configuration
	var defaultApp = '#app1';
	var effectSpeed = 500;
	var hash = location.hash;
	var sItems = $('.sitems li a');
	var sWrapWidth = 885;
	var sWidth = 126;
	var sMax = sItems.length * sWidth - (sWidth * 7) - sWidth;
	var sMoving = false;
	
	if(typeof(window['isHome']) != "undefined") {
	
		// Check to see if the hash is there and if it can be found
		defaultApp = (hash && $(hash).length != 0) ? hash : defaultApp;
		$(defaultApp).fadeIn(effectSpeed*2);
		
		// Activate slideshow images
		$('.slideshow').cycle({fx: 'fade'});
		
		// Add active class to default link
		$('a[rel="'+defaultApp+'"]').addClass('active');
		
		// Screenshots click event
		$('.fscreens').click(function() {
			iphone = $(this).parent('.title').parent('.lbottom').siblings('.iphone');
			iphone.children().css({display: 'none'});
			iphone.children('.slideshow').css({display: 'block'});
		});
		
		// Videos click event
		$('.fvideo').click(function() {
			iphone = $(this).parent('.title').parent('.lbottom').siblings('.iphone');
			iphone.children().css({display: 'none'});
			iphone.children('.video').css({display: 'block'});
		});
	
	}
	
	// Check to see if there is a cookie for the current selector current value
	if($.cookie('sCurrent')) {
		$('.sitems').css('left',$.cookie('sCurrent')+'px');
	}
	
	// Right arrow events
	$('.sright').click(function() {
		current = parseInt($('.sitems').css('left'));
		if(sItems.length > 7 && Math.abs(current) <= sMax && sMoving == false) {
			sMoving = true;
			left = current - sWidth;
			$('.sitems').animate({left: left}, effectSpeed,function() {
				sMoving = false;
				$.cookie('sCurrent', left)
			});
		}
	});
	
	// Left arrow events
	$('.sleft').click(function() {
		current = parseInt($('.sitems').css('left'));
		if(sItems.length > 7 && Math.abs(current) > 0 && sMoving == false) {
			sMoving = true;
			left = current + sWidth;
			$('.sitems').animate({left: left}, effectSpeed, function() {
				sMoving = false;
				$.cookie('sCurrent', left)
			});
		}
	});
	
	// Selector/Slider click functions
	$('.sitems a').click(function() {
		var appDiv = $($(this).attr('rel'));
		
		// Check to see if the app div exists
		if(appDiv.length != 0) {
			// Add active class
			$('.sitems a').removeClass('active');
			$(this).addClass('active');
			
			// Fade all others
			$('.featured').fadeOut(effectSpeed,function(){
				// Hide the others
				$('.featured .fitem').css('display','none');
				
				// Fade the selected div in
				appDiv.css('display','block')
				$('.featured').fadeIn(effectSpeed);
			});
		}
	});
});
