
var currentDropdownNum = null;

var pagePaddingBottom;
var pagePaddingBottomX2;

var submenuTopPadding = 5;
var submenuBottomPadding = 5;

var projectScroll = 0;
var maxProjectScroll = -1;

var projectArray;
var projectGallery = null;

var isBelowIE8 = false;

var initLayout = function() {
	
	if(!inEditor)
	{
		$('td.bodytext').wrapInner('<div class="contentWrapper" id="contentWrapper"></div>');
		
		$('#contentWrapper').appendTo('body');
		$('#contentWrapper').prepend('<div class="topLeftLogo"><a href="page.htm?cpi_id=21"></a></div>');
		
		$('#menuPaddingWrapper').prependTo('body');
		$('#aaa').hide();
	}
	else
	{
		$('td.bodytext').wrapInner('<div class="contentWrapper" id="contentWrapper"></div>');
		
		$('#contentWrapper').appendTo('body');
		$('#contentWrapper').prepend('<div class="topLeftLogo"><a href="page.htm?cpi_id=21"></a></div>');
		
		$('#menuPaddingWrapper').appendTo('body');
		$('#aaa').hide();
	}
	
	if(typeof initComplete == 'function')
	{
		initComplete();
	}
	
	$('.pageTopDropdownMenu').animate(
	{opacity: 0},
	{duration: 0,
	easing: 'linear'});
	
	var i = 1;
	
	$('.pageTopDivMenu li').each(function() {
		
		if($('.pageTopDropdownMenu#dropdown'+i+' li').length > 0)
		{
			var localI = i;
			
			$(this).mouseenter(function() { showDropdown(localI); } );
		}
		else
		{
			$(this).mouseenter(function() { hideDropdowns(); } );
		}
		
		i++	
	});
	
	$('.menuWrapper').mouseleave(closeDropdown);

	//$(window).scroll(function(){positionMenu(true);});
	
	positionMenu();
}

var positionMenu = function(noAuto) {
	
	var windowHeight = $(window).height();
	var contentHeight = $('#contentWrapper').height() - 3;
	var menuHeight = 60;
	//var menuHeight = 260;
	
	var newPageTop = 50;
	
	if((contentHeight+100) < windowHeight) {
		newPageTop = (windowHeight - (contentHeight+100)) / 2;
	}
	
	var newTop = $(window).scrollTop() + windowHeight - menuHeight;
	
	if(newTop > (newPageTop + contentHeight - menuHeight)) {
		newTop = newPageTop + contentHeight - menuHeight;
	}
	
	var newLeft = ($(window).width() - 980) / 2;
	$('#menuPaddingWrapper, #contentWrapper').css('left', newLeft + "px");
	
	$('#menuPaddingWrapper').css('top', newTop + "px");
	
	$('#contentWrapper').css('top', newPageTop + "px");
	
	$('.popupGalleryWrapper').css('paddingTop', (newPageTop + 100) + "px");
	
	requestAnimFrame(positionMenu);
}

var showDropdown = function(num) {
	
	if(currentDropdownNum != num)
	{
	
		var newDelay = 0;
		
		if(currentDropdownNum!=null)
		{
			newDelay = 200;
			hideDropdowns();
		}
		
		currentDropdownNum = num;
	
		var newHeight = ($('.pageTopDropdownMenu#dropdown'+num+' li').length * $('.pageTopDropdownMenu#dropdown'+num+' li').height()) + submenuTopPadding + submenuBottomPadding;
		
		//alert(newHeight);
		setTimeout(function() {
			if(currentDropdownNum == num)
			{
				$('.pageTopDropdownMenu#dropdown'+num).stop().animate(
				{opacity: 1},
				{duration: 300,
				easing: 'swing'});
				
			}
		}, newDelay);
		
		newmenuWrapperHeight =  newHeight + 67;
		
		$('.submenuWrapper').stop().animate(
		{height: newHeight,
		marginTop: -newHeight},
		{duration: 300,
		easing: 'swing'});
	}

}

var hideDropdowns = function() {
	currentDropdownNum = null;
	
	$('.pageTopDropdownMenu').stop().animate(
	{opacity: 0},
	{duration: 200,
	easing: 'swing'});
	
	$('.submenuWrapper').stop().animate(
	{height: 0,
	marginTop: 0},
	{duration: 300,
	easing: 'swing'});
}

var closeDropdown = function() {
	$('.pageTopDropdownMenu').stop().animate(
	{opacity: 0},
	{duration: 300,
	easing: 'swing'});
	
	$('.submenuWrapper').stop().animate(
	{height: 0,
	marginTop: 0},
	{duration: 300,
	easing: 'swing'});
	
	/*
	$('#menuPaddingWrapper').stop().animate(
	{paddingTop: 200},
	{duration: 300,
	easing: 'swing'});
	*/
	
	currentDropdownNum = null;
}

window.requestAnimFrame = (function(){
	return  window.requestAnimationFrame       || 
			window.webkitRequestAnimationFrame || 
			window.mozRequestAnimationFrame    || 
			window.oRequestAnimationFrame      || 
			window.msRequestAnimationFrame     || 
			function(/* function */ callback, /* DOMElement */ element){
				window.setTimeout(callback, 1000 / 60);
			};
})();


var initProjects = function() {
	
	$('body').css({'-webkit-user-select': 'none', '-khtml-user-select': 'none', '-moz-user-select': 'none', '-o-user-select': 'none', 'user-select': 'none'});
	
	isBelowIE8 = checkBelowIE8();
	
	projectArray = new Array();
	
	$.ajax({
		type: "GET",
		url: encodeURI("storage/xml/projects.xml"),
		dataType: "xml",
		success: function(xml) {
			$(xml).find('xml').each(function(){
											 
				var currentProjectSection;
				var i = 0;
											 
				$(this).find('project').each(function(){
													  
					if(i % 9 == 0)
					{
						var hiddenStyle = '';
						
						if(isBelowIE8 && i != 0)
						{
							if(maxProjectScroll != 0)
							{
								hiddenStyle = ' style="display: none;"';
							}
						}
						
						currentProjectSection = $('<div class="projectsSection"'+hiddenStyle+'></div>').appendTo('#projectsMover');
						maxProjectScroll++;
					}
					
					var rightClass = '';
													  
					if((i+1) % 3 == 0)
					{
						rightClass = ' right';
					}
					
					var newProject = new projectObj($(this).attr('title'), $(this).attr('desc'), $(this).attr('cost'), $(this).attr('galleryPath') );
					
					projectArray.push( newProject );
					
					var localI = i;
					
					var newProjectElement = $('<div class="projectThumb'+rightClass+'"><div class="projectThumbImage" style="background-image: url('+$(this).attr('thumb')+');"></div><div class="projectThumbCaption">'+$(this).attr('title')+'</div></div>').appendTo(currentProjectSection).click(function() { launchProject(localI); });
					
					i++;
						
				});
				
				$('#projectsArrowRight').click(nextProjectSection);
				$('#projectsArrowLeft').click(lastProjectSection);
				
				$('.enlargeOverlay').appendTo('body');
				$('.enlargeOverlayBG').appendTo('body');
				
				$('#galleryCloseBtn').click(closeOverlay);
				
				resetProjectArrows();
			});
		}
	});
	
}

var launchProject = function(num)
{
	var newProject = projectArray[num];
	
	//$('.midSection h2').html(newProject.title);
	//$('#galleryDesc').html(newProject.desc);
	//$('#projectCost').html(newProject.cost);
	
	$('.midSection h2, #galleryDesc, #projectCost').empty();
	
	//popupGalleryFonts();
	
	openOverlay(function(){
		if(projectGallery != null)
		{
			projectGallery.destroy();
		}
		
		projectGallery = new Gallery({wrapper: "popupGalleryContainer", xmlPath: newProject.galleryPath, slideInterval: 0, extRightBtn: '#galleryArrowRight', extLeftBtn: '#galleryArrowLeft', externalData: {caption: "#galleryCaption", title: ".midSection h2"}, firstCaptionOnly: true, firstTitleOnly: true, preAnimationCallback: popupGalleryFonts});
	});
}

var projectObj = function(title, desc, cost, galleryPath) {
	this.title = title;
	this.desc = desc;
	this.cost = cost;
	this.galleryPath = galleryPath;
}

var nextProjectSection = function() {
	
	projectScroll++;
	if(projectScroll > maxProjectScroll)
	{
		projectScroll = maxProjectScroll;
	}
	
	getProjectSection();
	
}

var lastProjectSection = function() {
	
	projectScroll--;
	if(projectScroll < 0)
	{
		projectScroll = 0;
	}
	
	getProjectSection();
	
}

var getProjectSection = function() {
	
	if(!isBelowIE8)
	{
		var newLeft = -(685 * projectScroll);
		
		$('#projectsMover').animate(
		{marginLeft: newLeft},
		{duration: 300,
		easing: 'easeOutExpo'});
	}
	else
	{
		var i = 0;
		$('.projectsSection').each(function() {
			
			if(i == projectScroll)
			{
				$(this).show();
			}
			else
			{
				$(this).hide();
			}
			   
			i++;
			
		});
	}
	
	resetProjectArrows();
}

var resetProjectArrows = function() {
	if(projectScroll == 0)
	{
		$('#projectsArrowLeft').animate(
		{opacity: 0},
		{duration: 200,
		easing: 'linear'});
	}
	else
	{
		$('#projectsArrowLeft').animate(
		{opacity: 1},
		{duration: 200,
		easing: 'linear'});
	}
	
	if(projectScroll >= maxProjectScroll)
	{
		$('#projectsArrowRight').animate(
		{opacity: 0},
		{duration: 200,
		easing: 'linear'});
	}
	else
	{
		$('#projectsArrowRight').animate(
		{opacity: 1},
		{duration: 200,
		easing: 'linear'});
	}
}

var openOverlay = function(callbackFunction)
{
	$('.enlargeOverlay').height($(document).height());
	$('.enlargeOverlayBG').height($(document).height());
	
	$('.enlargeOverlay').stop().fadeIn(100);
	$('.enlargeOverlayBG').stop().fadeTo(100, 0.75);
	
	if(isFunction(callbackFunction))
	{
		setTimeout(callbackFunction, 150);
	}
}

var closeOverlay = function(callbackFunction)
{
	
	$('.enlargeOverlay').stop().fadeOut(250);
	$('.enlargeOverlayBG').stop().fadeOut(250);
	
	setTimeout(function() {
		if(projectGallery != null)
		{
			projectGallery.destroy();
		}
	}, 300);
	
	if(isFunction(callbackFunction))
	{
		setTimeout(callbackFunction, 300);
	}
}

function isFunction(possibleFunction) {
	return (typeof(possibleFunction) == typeof(Function));
}

function getInternetExplorerVersion() {
	var rv = -1;
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
			rv = parseFloat(RegExp.$1);
	}
	return rv;
}

function checkBelowIE8() {
	var ver = getInternetExplorerVersion();
	if (ver > -1) {
		if (ver >= 8.0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return false;
	}
}











