// Check for #
if(location.href.indexOf('#!') > 0) {
	var sharp = location.href.indexOf('#!');
	location.href = location.href.substr(sharp+2);
}
//

var FooterAnimEnabled = true;
var FooterTimeout;
var MovingMenu = false;
var floatMenuPosLeft = new Array();
var floatMenuPosTop = new Array();
var ContentHeight = 330;
var ContentDefaultHeight = 330;
var isCollapsed = false;

function addCss(cssCode, id) {
var styleElement = document.createElement("style");
  styleElement.type = "text/css";
  styleElement.id = (id != null) ? id : '';
  if (styleElement.styleSheet) {
    styleElement.styleSheet.cssText = cssCode;
  } else {
    styleElement.appendChild(document.createTextNode(cssCode));
  }
  document.getElementsByTagName("head")[0].appendChild(styleElement);
}

// pre-init
addCss(
	' #ContentPanel h1 {' +
		'border-radius: 8px !important;' +
		'-moz-border-radius: 8px !important;' +
		'-webkit-border-radius: 8px !important;' +
	'}' +

	' #ContentPanel {' +
		'height: 57px !important;' +
	'}'
, 'jsCssFix');

function init() {
	realignDisplay();
	contentCollapse();
	$('#jsCssFix').remove();
	
	$('a.mainmenu').each(function(index) {
		floatMenuPosLeft[index] = $(this).position().left;
		floatMenuPosTop[index] = $(this).position().top;
	});
	
	if(!StartHidden) {
		contentExpand();
		moveMenuToOrigin();
	}
	else {
		MovingMenu = true;
		contentCollapse();
	}
	
	$('body').css('background-color', $('#MainContent').css('background-color'));
};

/**
 * Function: loadCss
 * 
 * Load a Cascading Stylesheet file.
 * 
 * Parameters:
 * 
 *   url - File location. The location is relative to document's 
 *         directory.
 *   id  - (Optional) ID attribute for the script element.
 * 
 * [1005]
 */
loadCss = function(url, id) {
	var elm = document.createElement("link");
	if(id != null)
	elm.id = id;
	elm.type = "text/css";
	elm.rel = "stylesheet";
	elm.href = url;
	document.getElementsByTagName("head")[0].appendChild(elm);
};


loadCss(WEBROOT+'/styles/js.css');

$(window).resize(function() {
	realignDisplay();
});

function contentCollapse(direct) {
	if(direct == null)
		direct = false;
		
	$(function() {
		isCollapsed = true;
		$('#ContentExpandBtn').addClass('tree-btn-down');
		$('#ContentExpandBtn').removeClass('tree-btn-up');
		$('#ContentPanel h1').css('border-radius', '8px 8px 8px 8px');
		$('#ContentPanel h1').css('-moz-border-radius', '8px 8px 8px 8px');
		$('#ContentPanel h1').css('-webkit-border-radius', '8px 8px 8px 8px');
		$('#ContentPanel').animate({
			height: (
			$('#ContentPanel h1').outerHeight() -
			$('#ContentPanel').css('padding-top').replace('px', '') -
			$('#ContentPanel').css('padding-bottom').replace('px', '')
			) + 'px'
		}, (direct ? 0 : 'normal'));
		
		moveMenuToFloat();
	});
}

function contentExpand(direct) {
	if(direct == null)
		direct = false;
		
	$(function() {
		isCollapsed = false;
		$('#ContentExpandBtn').removeClass('tree-btn-down');
		$('#ContentExpandBtn').addClass('tree-btn-up');
		$('#ContentPanel h1').css('border-radius', '8px 8px 0 0');
		$('#ContentPanel h1').css('-moz-border-radius', '8px 8px 0 0');
		$('#ContentPanel h1').css('-webkit-border-radius', '8px 8px 0 0');
		$('#ContentPanel').animate({
			height: ContentHeight+'px'
		}, (direct ? 0 : 'normal'));
		
		moveMenuToOrigin();
	});
}

function realignDisplay(direct) {
	if(direct == null)
		direct = false;
		
	$(function() {
		if(	$(window).height() <
			$('#ContentPanel').offset().top + ContentDefaultHeight + 140
		) {
			ContentHeight = ContentDefaultHeight;
			
			if(!isCollapsed) {
				$('#ContentPanel').css('height', (
					ContentHeight
				)+'px');
			}
			
			$('#BottomNav').css('position', 'static');
			$('#Footer').slideDown((direct ? 0 : 'normal'));
			FooterAnimEnabled = false;
		}
		else {
			ContentHeight = 
				$(window).height() - 
				$('#Playground').offset().top - 
				140;
			
			if(!isCollapsed) {
				$('#ContentPanel').css('height', (
					ContentHeight
				)+'px');
			}
			
			
			$('#BottomNav').css('position', 'fixed');
			$('#Footer').slideUp((direct ? 0 : 'normal'));
			FooterAnimEnabled = true;
		}
	});
	/*
	$(function() {
		if(
			$('#Playground').outerHeight() <
			$(window).height() - 
			$('#TopNav').outerHeight(true) - 
			$('#Copyright').outerHeight(true) -
			$('#MainContent').css('padding-top').replace('px', '') -
			$('#MainContent').css('padding-bottom').replace('px', '') 
		) {
			$('#Playground').animate({marginTop: (
				($(window).height() - 
				$('#TopNav').outerHeight(true) - 
				$('#Copyright').outerHeight(true) -
				$('#MainContent').css('padding-top').replace('px', '') -
				$('#MainContent').css('padding-bottom').replace('px', '') ) / 2 -
				
				$('#Playground').css('height').replace('px', '') / 2 - 
				10
			) + 'px'}, (direct ? 0 : 'normal'));
			$('#BottomNav').css('position', 'fixed');
			$('#Footer').slideUp((direct ? 0 : 'normal'));
			FooterAnimEnabled = true;
		}
		else {
			$('#Playground').animate({marginTop: 0}, (direct ? 0 : 'normal'));
			$('#BottomNav').css('position', 'static');
			$('#Footer').slideDown((direct ? 0 : 'normal'));
			FooterAnimEnabled = false;
		}
	});*/
}

function setLang(lang) {
	$.cookie('language', lang);
	if(location.href.indexOf('lang=') > 0)
		location.href = location.href.replace('lang=', 'langOld=');
	else
		location.reload();
}

$(function() {
	$('a[rel=ajax]').live('click', function() {
		var url = $(this).attr('href');
		
		// Special case for back to home navigation link
		if($(this).hasClass('BackToHome')) {
			if(CurrentPage == 'home') {
				contentCollapse();
				return false;
			}
		}
		//
		
		var currentUrl = location.href;
		var sharp = location.href.indexOf('#');
		if(sharp > 0) {
			currentUrl = currentUrl.substring(0, sharp);
		}
		location.href = currentUrl + '#!' + url;
		
		var qmark = url.indexOf('?');
		if(qmark > 0)
			url += '&ajax=true';
		else
			url += '?ajax=true';
			
		if($(this).hasClass('backward'))
			var backward = true;
		else
			var backward = false;
		
		$(this).blur();
		
		// Show loader icon
		$('#Loader').show();
		
		$.get(url, function(data) {
			CurrentPage = data.page;
			// Hide loader icon
			$('#Loader').hide();
			
			realignDisplay();
			$('#ContentContainer').css('overflow', 'hidden');
			$('#AjaxContent').attr('id', 'AjaxContentOld');
			$('#AjaxContentOld').css('position', 'absolute');
			$('#AjaxContentOld').css('width', '100%');
			$('#AjaxContentOld').css('top', (0-$('#ContentContainer').scrollTop())+'px');
			$('#ContentContainer').scrollTop(0);
			
			if(!backward) { // Forward animation
				$('#AjaxContentOld').animate({left: (
					0 - 
					$('#AjaxContentOld').outerWidth()
				) + 'px', opacity: 0
				}, 'normal', 'swing', function() {
					$(this).remove();
				});
			
				$('#ContentContainer').append(
					'<div id="AjaxContent" style="opacity: 0; ' +
					'position: absolute; left: 100%; width: 100%;">' +
					data.content +
					'</div>'
				);
				$('#AjaxContent').animate({left: 0, opacity: '1'}, 'normal', 'swing', function() {
					$('#ContentContainer').css('overflow', 'auto');
				});
			}
			
			else { // Backward animation
				$('#AjaxContentOld').animate({left: '100%', opacity: 0
				}, 'normal', 'swing', function() {
					$(this).remove();
				});
			
				$('#ContentContainer').append(
					'<div id="AjaxContent" style="opacity: 0; ' +
					'position: absolute; left: -100%; width: 100%;">' +
					data.content +
					'</div>'
				);
				$('#AjaxContent').animate({left: 0, opacity: '1'}, 'normal', 'swing', function() {
					$('#ContentContainer').css('overflow', 'hidden');
				});
			}
			
			if(data.title != null && data.title != 'Home') {
				$('#CurrentLocation').hide();
				if(data.titleNav == null)
					$('#CurrentLocation').html(' &bull; '+data.title);
				else
					$('#CurrentLocation').html(' &bull; '+data.titleNav);
				$('#CurrentLocation').fadeIn();
				$('head title').html(WebTitle+" - "+data.title);
			}
			else {
				$('#CurrentLocation').fadeOut();
				$('head title').html(WebTitle);
			}
			
			if(data.startHidden != null && data.startHidden == false) {
				contentExpand();
			}
		});
		
		return false;
	});
	
	$('#BottomNav').hover(function() {
		if(!FooterAnimEnabled)
			return;
		clearTimeout(FooterTimeout);
		$('#Footer').slideDown();
	});
	
	$('#BottomNav').mouseout(function() {
		if(!FooterAnimEnabled)
			return;
		clearTimeout(FooterTimeout);
		FooterTimeout = setTimeout("$('#Footer').slideUp();", 1000);
	});
	
	$('#ContentExpandBtn').click(function() {
		if($(this).hasClass('tree-btn-down')) {
			$(this).removeClass('tree-btn-down');
			$(this).addClass('tree-btn-up');
			contentExpand();
		}
		else {
			$(this).addClass('tree-btn-down');
			$(this).removeClass('tree-btn-up');
			contentCollapse();
		}
		$(this).blur();
		return false;
	});
	
	$('a.external-link').click(function() {
		$(this).blur();
		spreadEverything(null, $(this).attr('href'));
		$('#Loader').show();
		return false;
	});

});

function spreadEverything(duration, link) {
	if(duration == null)
		duration = 1000;
	
	MovingMenu = false;
	
		
	$(function() {
		var xMax = $(window).width() + 500;
		var yMax = $(window).height() + 500;
		var xMin = -500;
		var yMin = -500;
		var posTop = new Array();
		var posLeft = new Array();
		
		var count = $('a.mainmenu').length;
		
		$('a.mainmenu').each(function(index) {
			$(this).stop();
			posTop[index] = $(this).offset().top;
			posLeft[index] = $(this).offset().left;
			
		});
		
		
		$('a.mainmenu').each(function(index) {
			$(this).css('position', 'fixed');
			$(this).css('top', posTop[index] + 'px');
			$(this).css('left', posLeft[index] + 'px');
			
			var orient = Math.floor(Math.random() * 4) + 1;
			switch(orient%2) {
				case 0:
					var left = (orient == 2) ? xMin : xMax;
					var top = (Math.random() * (yMax - yMin) + yMin);
					break;
				case 1:
					var top = (orient == 1) ? yMin : yMax;
					var left = (Math.random() * (xMax - xMin) + xMin);
					break;
			}
			$(this).animate({
				left: left + 'px',
				top: top + 'px'
			}, duration);
			$(this).fadeOut(duration);
			
			if(index == count-1) 
				$('#ContentPanel').fadeOut(duration);
		});
		
		$('#CurrentLocation').fadeOut(duration);
		
		$('body').append(
			'<div id="SpreadBackToHome" style="display: none;"><a href="'+CurrentPage+'">Reload Page</a></div>'
		);
		$('#SpreadBackToHome').css('position', 'absolute');
		$('#SpreadBackToHome').css('width', '100%');
		$('#SpreadBackToHome').css('background', 'transparent');
		$('#SpreadBackToHome').css('text-align', 'center');
		$('#SpreadBackToHome').css('top', ($(window).height()/2)+'px');
		$('#SpreadBackToHome').css('z-index', 2000);
		$('#SpreadBackToHome').fadeIn('slow');
	});
	
	if(link != null)
		setTimeout(function() { location.href=link; }, duration+100);
}

function floatingMenu(elm, left, top) {
	if(!MovingMenu)
		return;
		
	var duration = 1500 + Math.random() * 1000;
		
	$(function() {
		if($(elm).hasClass('onhover'))
			return;
		$(elm).animate({
			left: (left + (Math.random() * 20) * (Math.round(Math.random()) * 2 - 1)) + 'px',
			top: (top + (Math.random() * 20) * (Math.round(Math.random()) * 2 - 1)) + 'px',
		}, duration, 'swing', function() {
			floatingMenu(elm, left, top, duration);
		});
	});
}

function moveMenuToFloat() {
	$(function() {
		$('a.mainmenu').each(function(index) {
			$(this).animate({
				top: floatMenuPosTop[index] + 'px',
				left: floatMenuPosLeft[index] + 'px'
			}, 'normal', 'swing', function() {
				MovingMenu = true;
				floatingMenu($(this), floatMenuPosLeft[index], floatMenuPosTop[index]);
			});
		});
	});
}

function moveMenuToOrigin() {
	MovingMenu = false;
	
	$(function() {
		var top = 0;
		$('a.mainmenu').each(function() {
			$(this).stop(true);
			var left = 
				780 - 
				$(this).outerWidth() / 2;
			$(this).animate({
				top: top + 'px',
				left: left + 'px'
			}, 'normal', 'swing');
			top += $(this).outerHeight() + 12;
		});
	});
}

/// SNAP TO MOUSE //
$(function() {
	$('a.mainmenu').mousemove(function(e) {
		if(!MovingMenu)
			return;
		var left = 
			e.pageX -
			$('#Playground').offset().left - 
			$(this).outerWidth() / 2;
		var top = 
			e.pageY -
			$('#Playground').offset().top - 
			$(this).outerHeight() / 2 
		$(this).stop();
		$(this).addClass('onhover');
		$(this).animate({
			top: top + 'px',
			left: left + 'px'
		}, 'slow', 'linear', function() {
		});
	});
	
	$('a.mainmenu').mouseout(function(e) {
		$(this).removeClass('onhover');
		floatingMenu($(this), $(this).position().left, $(this).position().top);
	});
});

$(function() {
	$('#PortfolioCatalog .PortfolioItem').live('hover', function() {
		$('#PortfolioInfo').html($(this).html());
	});
});

