var steps = Array(1, 1.3, 1.5, 1.7, 1.85, 1.9);
var offsetArray = Array(2, 1.6, 1.3, 1.2, 1.1, 1);

var animating = false;

var pages = Array();
var width = 494;
var height = 657;

var defaultWidth = 300;
var defaultHeight = 375;

var zoomLevel = 1;
var tmpZoom = 1;

var widths = Array(350, 494, 642)
var fontSizes = Array(160, 200, 280);
var heights = Array(494, 657, 854)

var defaultWidth = 150;
var defaultHeight = 175;		

var activeId = 0;
var activePos = 0;

var browserWidth = 0;
var browserHeight = 0;
var widthOffset = 0;
var pageOffsetRatio = 1.5;
var pageMargin = 20;
var direction = 0;
var pageCount = 0; 

var repeat = 0;
var interval;

function defaultOffset() {
	widthOffset = (browserWidth / 2) - (widths[zoomLevel] /2);
	heightOffset = (browserHeight / 2) - (heights[zoomLevel] /2) + 30;
};

function innitPages() {
	var i = 0;
	$('.page').each(function() {
		if ($(this).hasClass('active')) {
			activeId = i;
			activePos = i;
		}
		id = $(this).attr('id');
		
		pages[id] = i;
		i++;
	});
	pageCount = i;
	sortPages();
}

function zoomIn() {
	if (zoomLevel < widths.length-1) {
		zoomLevel = (zoomLevel+1);
		rotate();
	}
}

function zoomReset() {
	zoomLevel = tmpZoom;
	rotate();
}

function zoomMin() {
	tmpZoom = zoomLevel;
	zoomLevel = 0;
	rotate();
	
}

function zoomOut() {
	if (zoomLevel > 0) {
		zoomLevel = (zoomLevel-1);
		rotate();
	}	
}

function sortPages() {
	for (var id in pages) {
		if (pages[id] > Math.ceil(pageCount / 2)) {
			pages[id] = pages[id]-pageCount;
		}
		else if (pages[id] < -2) {
			pages[id] = pages[id]+pageCount;
		}
	}
}

function rotate() {
	defaultOffset();
	for (var id in pages) {
		offset = 0 - pages[id];
		absoluteOffset = Math.abs(offset);
		trans = offsetArray[absoluteOffset] - 1;
		if (offset > 0) {
			right = true;
			newWidth = widths[zoomLevel] / steps[absoluteOffset];
			newHeight = heights[zoomLevel] / steps[absoluteOffset];			
			newWidthOffset = widthOffset - (widths[zoomLevel]*steps[absoluteOffset]) + widths[zoomLevel];
			newHeightOffset = heightOffset + ((heights[zoomLevel]-newHeight)/2);
			
		}
		else if (offset == 0) {
			newWidthOffset = widthOffset;
			newWidth = widths[zoomLevel];
			newHeightOffset = heightOffset;
			newHeight = heights[zoomLevel];	
		}
		else {
			right = false;
			newWidth = widths[zoomLevel] / steps[absoluteOffset];
			newHeight = heights[zoomLevel] / steps[absoluteOffset];			
			newWidthOffset = widthOffset + (widths[zoomLevel]*steps[absoluteOffset+1]) - (widths[zoomLevel]);
			newHeightOffset = heightOffset + ((heights[zoomLevel]-newHeight)/2);
		}
		
		var newWidth = parseInt(widths[zoomLevel]/steps[absoluteOffset]);
		
		if (offset == 2 && direction == 1) {
			$('#' + id).css({'zIndex': 5-absoluteOffset});
		}
		else if (offset == -3 && direction == -1 ) {
			$('#' + id).css({'zIndex': 7-absoluteOffset});
		}		
		//$('#' + id).find('.canvas').fadeTo(500, trans);
		
		if (absoluteOffset != 0) {
			$('#' + id).find('.content-block').animate({'opacity': 0});
			$('#' + id).find('.menu ul').animate({'opacity': 0}, {'queue': false, 'duration': 500});
			$('#' + id).css({'cursor': 'pointer'}, {'queue': false, 'duration': 500});
		}			
		var stepSize = (2-steps[absoluteOffset])*100*(zoomLevel+1*1.2) + '%';
		var stepSizePx = ((2-steps[absoluteOffset])*100*(zoomLevel*1.1) * 42) / 100;
		
		var stepSize = parseInt(fontSizes[zoomLevel]/steps[absoluteOffset]) + '%';
		
		$('#' + id).find('.logo').animate({'width': stepSizePx}, {'queue': false, 'duration': 500});
		$('#' + id).animate({
			'left': newWidthOffset + 'px', 
			'width': newWidth, 
			'height': newHeight, 
			'top': newHeightOffset + 'px',
			'font-size': stepSize
		}, {'queue': false, 'duration': 500, 'easing': 'swing', 'complete': function() {
			id = $(this).attr('id');
			offset = 0 - pages[id];
			absoluteOffset = Math.abs(offset);
			$('#' + id).css({'zIndex': 10-absoluteOffset});
			
			if (absoluteOffset == 0) {
				$('#' + id).find('.content-block').animate({'opacity': 1}, {'queue': false, 'duration': 500});
				
				$('#' + id).find('.menu ul').animate({'opacity': 1}, {'queue': false, 'duration': 500});
				$('#' + id).css({'cursor': 'default'});
				
			}

		}, 'easing': 'swing'});

	}
}

function focusOn(id) {
	var offset = 0 - pages[id];
	var steps = Math.abs(offset);
	
	$('.menu ul li a').each(function() {
		$(this).removeClass('active');
	});
	
	
	if (steps > 0) {
		repeat = steps;

		if (offset < 0) {
			stepOffset = -1;
			direction = -1;
		}
		else {
			stepOffset = 1;
			direction = 1;
		}					
		doStep(stepOffset);
		if (repeat > 0) {

			window.clearInterval(interval);
			interval = window.setInterval("doStep(" +  stepOffset + ")", 450);
		}
	}
	else {
		
		doStep(0);
	}
	$('.menu ul li a').each(function() {
		if ($(this).attr('rel') == id) {
			$(this).addClass('active');
		}
	});		
	return false;
}
function doStep(stepOffset) {
	for (var id in pages) {
		pages[id] = pages[id]+stepOffset 
	}
	sortPages();
	rotate();
	repeat = repeat - 1;
	if (repeat <= 0) {
		window.clearInterval(interval);
		animating = false;
	}
}

function switchItem(element, parent_id, page_id, img, title, content, position) {
	if (img) {
		$('#page' + parent_id + ' .photo').attr('src', img);
	}	
	$('#page' + parent_id + ' .ajax-block').fadeOut(200);
	
	$('#page' + parent_id + ' .header-text').fadeOut(200, function() {
		if (title) {
			$('#page' + parent_id + ' .header-text h3').html(title);
			$('#page' + parent_id + ' .header-text span').html(content);
			$('#page' + parent_id + ' .header-text').fadeIn(200);
		}
	});

		
	
	$('#page' + parent_id + ' .ajax-block').load('/ajax/subpage/' + parent_id + '/' + page_id, function(response, status, xhr) {
		if (status == "error") {
			var msg = "Sorry but there was an error: ";
			alert(msg + xhr.status + " " + xhr.statusText);
		}
		else {
			$('#page' + parent_id + ' .ajax-block').fadeIn();
		}

	}); 
}

function switchSubItem(element, parent_id, page_id, img, title, content, position) {
	if (img) {
		$('#page' + parent_id + ' .photo').attr('src', img);
	}	
	$('#page' + parent_id + ' .ajax-block').fadeOut(200);
	
	$('#page' + parent_id + ' .header-text').fadeOut(200, function() {
		if (title) {
			$('#page' + parent_id + ' .header-text h3').html(title);
			$('#page' + parent_id + ' .header-text span').html(content);
			$('#page' + parent_id + ' .header-text').fadeIn(200);
		}
	});

		
	$('#page' + parent_id + ' .ajax-block').load('/ajax/subsubpage/' + parent_id + '/' + page_id, function(response, status, xhr) {
		if (status == "error") {
			var msg = "Sorry but there was an error: ";
			alert(msg + xhr.status + " " + xhr.statusText);
		}
		else {
			$('#page' + parent_id + ' .ajax-block').fadeIn();
		}

	}); 
}
var unload = false;
function nextPage(element) {
	unload = true;
	$('#' + id).animate(
		{
			'left': browserWidth+800 + 'px' 
		}, 
		{'queue': false, 'duration': 500, 'easing': 'swing', 'complete': function() {
			document.location.href=element.href;
		}}
	);
	return false;
}

$(window).resize(function() {
	if (!unload) {
		browserWidth = $(window).width();
		browserHeight = $(window).height();
		rotate();
	}
});

$(window).load(function() {
	browserWidth = $(window).width();
	browserHeight = $(window).height();
	innitPages();
	defaultOffset();
	newWidthOffset = widthOffset;
	newHeightOffset = heightOffset;
	
	$('.page').css({
		'top': newHeightOffset + 'px'
	});	
	
	focusOn('page0');
	
    $(".menu a").each(function() {
        $(this).click(function(e) {
          e.stopPropagation();
        });
      });
	
	
});		
