$(function () {
	// Declare class names
	var profileMenu = $('.menu-profile');
	var servicesMenu = $('.menu-services');
	var blogMenu = $('.menu-blog');
	var contactMenu = $('.menu-contact');
	var menu = $('.menu');
	var content = $('#content');
	var container = $('#container');
	
	// Initialization
	changeLink();
	profileMenu.css('top', '104px');
	servicesMenu.css('top', '104px');
	blogMenu.css('top', '104px');
	contactMenu.css('top', '104px');
	
	// Menu slide functions
	profileMenu.hover(
		function(){profileMenu.stop().animate({top:'13px'}, 1000, 'easeOutExpo');},
		function(){profileMenu.stop().animate({top:'104px'}, 1500, 'easeOutElastic');}
	);
	servicesMenu.hover(
		function(){servicesMenu.stop().animate({top:'13px'}, 1000, 'easeOutExpo');},
		function(){servicesMenu.stop().animate({top:'104px'}, 1500, 'easeOutElastic');}
	);
	blogMenu.hover(
		function(){blogMenu.stop().animate({top:'13px'}, 1000, 'easeOutExpo');},
		function(){blogMenu.stop().animate({top:'104px'}, 1500, 'easeOutElastic');}
	);
	contactMenu.hover(
		function(){contactMenu.stop().animate({top:'13px'}, 1000, 'easeOutExpo');},
		function(){contactMenu.stop().animate({top:'104px'}, 1500, 'easeOutElastic');}
	);
	
	// Ajax router for first page load
	var hash = window.location.hash;
	if(hash != '')
		loadContentUnanimated(hash);

	
	// Link handler
	$('a').click(function() {
		var alamat = $(this).attr('href');
		if(alamat.substr(0,4) == 'http' || alamat.substr(0,6) == 'mailto' || $(this).attr('rel') == 'fancybox')
		{}
		else
		{
			loadContent(alamat);
		}
		return true;
	});
	
	$('#livechat').click(function() {
		window.open('/livesupport/livezilla.php?code=SERVERPAGE','Xituz Live Support','width=600,height=550,left=0,top=0,resizable=no,menubar=no,location=yes,status=yes,scrollbars=no');
		return false;
	});
});

var active = 0;
// Ajax function for page navigation
function loadContent(url) {
	var container = $('#container');
	var content = $('#content');
	var wrapper = $('#content-wrap');
	var loading = $('#loading');
	// Remove hash header
	var linkd = url.substr(2);
	// Handles empty anchor (anchor shouldnt be empty)
	if(linkd == '')
		linkd = 'home';
	
	// Animation

	if(active == 0)
	{
		if(linkd == 'home')
		{
			var yCenter = $(window).height()/2 - ((210 + 374) / 2);
			if(yCenter < 0) yCenter = 0;
			container.animate({top: yCenter + 'px'}, 2000, 'easeOutExpo');
		}
		else
		{
			container.animate({top: '0px'}, 2000, 'easeOutExpo');
		}
		active = 1;
		loading.fadeIn(300);
		content.css('height', wrapper.height()+140+ 'px');
		wrapper.fadeOut(300, function() {
			content.animate({height: '350px'}, 300, 'easeOutExpo',function() {
				wrapper.load(linkd, function () {
					content.animate({height: wrapper.height()+140+ 'px'}, 500, 'easeOutExpo',function() {
						wrapper.fadeIn(500,function() {postLoad();});
						loading.fadeOut(300);
						active = 0;
						content.css('height', 'auto');
					});
				});
			});
		});
	}

}

// Not animated
function loadContentUnanimated(url) {
	var content = $('#content');
	var wrapper = $('#content-wrap');
	var container = $('#container');


	// Remove hash header
	var linkz = url.substr(2);
	// Handles empty anchor (anchor shouldnt be empty)
	if(linkz == '')
		linkz = 'home';

	container.hide();
	wrapper.load(linkz, function() {container.fadeIn(300, function() {postLoad();});});
}

//Link changer for ajax funtion
function changeLink() {
	$('#title span').html('');
	$('a').each(function() {
		var link = $(this).attr('href');
		// If link is not anchor, convert it to anchor
		if(link != null)
		{
			if(link.charAt(0) != '#' && link.substr(0,4) != 'http' && link.substr(0,6) != 'mailto' && $(this).attr('rel') != 'fancybox')
			{
				// Remove prefix links, dont know why there are prefix with $this->url
				link = '#/' + link.substr(1);
				$(this).attr('href', link);
			}
		}
	});	
}

function haccordion() {
	var ac1 = $('.ha-1');
	var ac2 = $('.ha-2');
	var ac3 = $('.ha-3');
	var ac4 = $('.ha-4');
	var ac5 = $('.ha-5');
	var twidth = 60;
	var imgwidth = 340;
	var divwidth = 510;
	var time = 1000;
	var easing = 'easeOutCubic';
	
	ac1.css('left', '0');
	ac2.css('left', divwidth - (twidth*4) + 'px');
	ac3.css('left', divwidth - (twidth*3) + 'px');
	ac4.css('left', divwidth - (twidth*2) + 'px');
	ac5.css('left', divwidth - (twidth*1) + 'px');
	
	ac1.hover(
			function() {
				ac2.stop().animate({left: divwidth - (twidth*4) + 'px'}, time, easing);
				ac3.stop().animate({left: divwidth - (twidth*3) + 'px'}, time, easing);
				ac4.stop().animate({left: divwidth - (twidth*2) + 'px'}, time, easing);
				ac5.stop().animate({left: divwidth - (twidth*1) + 'px'}, time, easing);
			},		
			function() {}
	);
	
	$('.ha-2').hover(
			function() {
				ac1.stop().animate({left: '0px'}, time, easing);
				ac2.stop().animate({left: twidth + 'px'}, time, easing);
				ac3.stop().animate({left: divwidth - (twidth*3) + 'px'}, time, easing);
				ac4.stop().animate({left: divwidth - (twidth*2) + 'px'}, time, easing);
				ac5.stop().animate({left: divwidth - (twidth*1) + 'px'}, time, easing);
			},		
			function() {}
	);
	
	ac3.hover(
			function() {
				ac1.stop().animate({left: '0px'}, time, easing);
				ac2.stop().animate({left: twidth + 'px'}, time, easing);
				ac3.stop().animate({left: (twidth*2) + 'px'}, time, easing);
				ac4.stop().animate({left: divwidth - (twidth*2) + 'px'}, time, easing);
				ac5.stop().animate({left: divwidth - (twidth*1) + 'px'}, time, easing);
			},		
			function() {}
	);
	
	ac4.hover(
			function() {
				ac1.stop().animate({left: '0px'}, time, easing);
				ac2.stop().animate({left: twidth + 'px'}, time, easing);
				ac3.stop().animate({left: (twidth*2) + 'px'}, time, easing);
				ac4.stop().animate({left: (twidth*3) + 'px'}, time, easing);
				ac5.stop().animate({left: divwidth - (twidth*1) + 'px'}, time, easing);
			},		
			function() {}
	);
	
	ac5.hover(
			function() {
				ac1.stop().animate({left: '0px'}, time, easing);
				ac2.stop().animate({left: twidth + 'px'}, time, easing);
				ac3.stop().animate({left: (twidth*2) + 'px'}, time, easing);
				ac4.stop().animate({left: (twidth*3) + 'px'}, time, easing);
				ac5.stop().animate({left: (twidth*4) + 'px'}, time, easing);
			},		
			function() {}
	);
	
}

// Post load function calls
function postLoad() {
	var content = $('#content');
	var wrapper = $('#content-wrap');
	

	// Change link
	changeLink();
	
	// Link handler
	$('a').click(function() {
		var alamat = $(this).attr('href');
		if(alamat.substr(0,4) == 'http' || alamat.substr(0,6) == 'mailto' || $(this).attr('rel') == 'fancybox')
		{
	
		}
		else
		{
			loadContent(alamat);
		}
	});
	
	// Contact form 
	$('#contactform').submit(function() {
		var errors = $('.errors');
	   	errors.slideUp();
		wrapper.load('contact', {name:$('#name').val(), company:$('#company').val(), email:$('#email').val(), contact:$('#contact').val(), msg:$('#msg').val()}, function() {
			var errors = $('.errors');
			errors.each(function() {
				$(this).hide();
			    $(this).slideDown();
			});
			var emailsent = $('.emailsent');
	        if (emailsent.is(':visible')) {
	        	emailsent.hide();
	        	emailsent.slideDown();
	        }
			postLoad();
		});
		return false;
	});
	
	// Blog form 
	$('#formblog').submit(function() {
		var errors = $('.errors');
	   	errors.slideUp();
	   	var linkz = window.location.hash.substr(2);
		wrapper.load(linkz, {author_name:$('#author_name').val(), author_email:$('#author_email').val(), author_url:$('#author_url').val(), formcontent:$('#formcontent').val()}, function() {
			var errors = $('.errors');
			errors.each(function() {
				$(this).hide();
			    $(this).slideDown();
			});
			var emailsent = $('.emailsent');
	        if (emailsent.is(':visible')) {
	        	emailsent.hide();
	        	emailsent.slideDown();
	        }
			postLoad();
		});
		return false;
	});
	
	
}
