$(document).ready(function(){
	$(".btn-slide").click(function(){
	  $("#panel").slideToggle("fast");
	  $(this).toggleClass("active");
	});
});

//image should be a jQuery style image
function widthForNewHeight(thisHeight,theImage) {
	var ratio = thisHeight / theImage.height();
	return Math.round(theImage.width() * ratio);
}

function heightForNewWidth(thisWidth,theImage) {
	var ratio = thisWidth / theImage.width();
	return Math.round(theImage.height() * ratio);
}

function initAppsPage() {	
	
	$('.platformNav li a img').each(function(item) {
		var clickImgSrc = $(this).attr('src');
		var actualSrcStart = clickImgSrc.indexOf('img') + 4;
		var actualSrc = '';
		if(clickImgSrc.indexOf('&',actualSrcStart) > -1) {
			actualSrc = clickImgSrc.substr(actualSrcStart,clickImgSrc.indexOf('&',actualSrcStart) - actualSrcStart);
		} else {
			actualSrc = clickImgSrc.substr(actualSrcStart);
		}
		
		$.cacheImage($(this).attr('src')); //Cache thumbnail
		$.cacheImage(actualSrc); //Cache big image
	});
	
	$('.details li').hide();
	$('.details').find('li:eq(0)').show();
	
	var wrapper = $('.platformNav li:first-child').find('.viewport');
	var initImg = $('.platformNav li:first-child').find('.screenWrap img').first();
	var initImgRefl = $('.platformNav li:first-child').find('.screenWrap img').last();
	var span = $('.platformNav li:first-child').find('span');

	var theHeight = 140;
	if ($('#device').attr('title') == 'blackberry' || $('#device').attr('title') == 'ipad') {
		theHeight = 109;
	}
	var newWidth = widthForNewHeight(theHeight,initImg);
	initImg.animate({ height:theHeight, left: '0', top: '0', width: newWidth}, 300);
	var newHeight = heightForNewWidth(newWidth,initImgRefl);
	initImgRefl.animate({ height:newHeight, left: '0', top: '0', width: newWidth}, 300);
	
	$('.platformNav li a').bind('click', function(event) {
		event.preventDefault();
		var parent = $(this).parents('div:eq(0)').attr('id');
		var click_id = $(this).attr('id');
		var click_uid = click_id.lastIndexOf('_');
		var click_num = $(this).attr('rel');
		var appId = $('.platformNav li:first-child').attr('id');
		var app = appId.split('_');		
		var device = $('#device').attr('title');
		
		var clickImgSrc = $(this).find('img').first().attr('src');
		var actualSrcStart = clickImgSrc.indexOf('img') + 4;
		var actualSrc = '';
		if(clickImgSrc.indexOf('&',actualSrcStart) > -1) {
			actualSrc = clickImgSrc.substr(actualSrcStart,clickImgSrc.indexOf('&',actualSrcStart) - actualSrcStart);
		} else {
			actualSrc = clickImgSrc.substr(actualSrcStart);
		}
		
		var screenImg = actualSrc;
		
		$('#device').load('../app_screen.php?src=' + actualSrc + '&app=' + app[0] + '&platform=' + device, function() {
			$('#device').find('img').fadeOut(0);
			$('#device').find('img').fadeIn(300);
			var screenshotimage = $('#device').find('img');
			if(screenshotimage.width() > screenshotimage.height()) {
				if($('#device').attr('class') != $('#device').attr('title') + '_landscape') {
					$('#device').attr('class',$('#device').attr('title') + '_landscape');
				}
			} else {
				if($('#device').attr('class') != $('#device').attr('title')) {
					$('#device').attr('class',$('#device').attr('title'));
				}
			}
		});
	
		$('.platformNav').children('li').each(function() {
			var item_id = $(this).attr('id');
			var item_uid = item_id.lastIndexOf('_');
			var item_num = $(this).find('a:eq(0)').attr('rel');
			var wrapper = $(this).find('.viewport');
			var appImg =  $(this).find('.screenWrap img').first();
			var appImgRefl =  $(this).find('.screenWrap img').last();
			var span = $(this).find('span');
			var imgStr_num = parseInt(click_num);;

			if (item_num == click_num) {
				$(this).addClass('active');
				var theHeight = 140;
				if ($('#device').attr('title') == 'blackberry' || $('#device').attr('title') == 'ipad') {
					theHeight = 109;
				}
				var newWidth = widthForNewHeight(theHeight,appImg);
				appImg.animate({ height:theHeight, left: '0', top: '0', width: newWidth}, 300);
				var newHeight = heightForNewWidth(newWidth,appImgRefl);
				appImgRefl.animate({ height:newHeight, left: '0', top: '0', width: newWidth}, 300);
				$(parent + ' .details').find('li').hide();
				$(parent + ' .details').find('li:eq(' + (parseInt(click_num)-1) + ')').fadeIn(300);
				
			} else {
				$(this).removeClass('active');
				var theHeight = 116;
				if ($('#device').attr('title') == 'blackberry' || $('#device').attr('title') == 'ipad') {
					theHeight = 90;
				}
				var newWidth = widthForNewHeight(theHeight,appImg);
				appImg.animate({ height:theHeight, left: '0', top: '10', width: newWidth}, 300);
				var newHeight = heightForNewWidth(newWidth,appImgRefl);
				appImgRefl.animate({ height:newHeight, left: '0', top: '10', width: newWidth}, 300);
			}
			$('platformNav li').attr('disabled', 'true');
		});
	});
}
