j(document).ready(function () {
	if(j.app.name == 'site')
	{
		var slideBusy = false;
		
		j.app.loadScript('/site/site-js/controller/form.js', true);
		j.app.loadCss('/site/site-css/form.css');
		
		var currentUrl = document.location.pathname;
		
		j('#top-menu a, .frame .left .cms-menu a').live('click', function (e) {
			if(slideBusy)
			{
				return false;
			}
			slideBusy = true;
			
			// Continue as normal for cmd clicks etc
			if( e.which == 2 || e.metaKey )
			{ 
				slideBusy = false;
				return true; 
			}
			
			// Get previous url and next url's.
			var prevUrl = document.location.pathname;
			var nextUrl = j(this).attr('href').replace('http://'+document.location.hostname, '');
			
			// Make sure we don't go to the same page as we currently are in.
			if(prevUrl == nextUrl)
			{
				slideBusy = false;
				e.preventDefault();
				return false;
			}
			
			try
			{
				window.history.pushState(null, j(this).html(), nextUrl);
			} catch (e) {
				window.location.href = nextUrl;
			}
			
			j('head title').html(j(this).html());
			
			var inTopMenu = (j(this).parents('#top-menu').length > 0);
			
			if((j(this).parent().prevAll('li:has(a[href="'+prevUrl+'"])').length > 0 && inTopMenu) || j('#top-menu li.active:first').nextAll('li:has(a[href="'+nextUrl+'"])').length > 0)
			{
				direction = 1;
			}
			else if((j(this).parent().nextAll('li:has(a[href="'+prevUrl+'"])').length > 0 && inTopMenu) || j('#top-menu li.active:first').prevAll('li:has(a[href="'+nextUrl+'"])').length > 0)
			{
				direction = -1;
			}
			else
			{
				direction = 0;
			}
			
			j(window).trigger('statechange', [{'url': nextUrl, 'direction': direction}]);
			
			// Set current button as active
			j(this)
				.parent()
					.parent()
						.find('li')
							.removeClass('active')
							.end()
						.end()
						.addClass('active');
			
			e.preventDefault();
			return false;
		});
		
		j('#arrow-left').bind('click', function () {
			currentUrl = document.location.pathname;
			if(j('li > a[href="'+currentUrl+'"]:first').parent().prev().length > 0)
			{
				j('li > a[href="'+currentUrl+'"]:first').parent().prev().find('> a').trigger('click');
			} else {
				j('li > a[href="'+currentUrl+'"]:first').parent().parent().find('> li:last > a').trigger('click');
			}
		});
		
		j('#arrow-right').bind('click', function () {
			currentUrl = document.location.pathname;
			if(j('li > a[href="'+currentUrl+'"]:first').parent().next().length > 0)
			{
				j('li > a[href="'+currentUrl+'"]:first').parent().next().find('> a').trigger('click');
			} else {
				j('li > a[href="'+currentUrl+'"]:first').parent().parent().find('> li:first > a').trigger('click');
			}
		});
		
		setInterval(function() {
			if(currentUrl != document.location.pathname)
			{
				if(slideBusy)
				{
					return false;
				}
				slideBusy = true;
				var prevElement = j('li > a[href="'+currentUrl+'"]:first').parent();
				var nextElement = j('li > a[href="'+document.location.pathname+'"]:first').parent();
				prevElement.removeClass('active');
				nextElement.addClass('active');
				
				if(prevElement.nextAll('li:has(a[href="'+document.location.pathname+'"])').length > 0)
				{
					direction = 1;
				}
				else if(prevElement.prevAll('li:has(a[href="'+document.location.pathname+'"])').length > 0)
				{
					direction = -1;
				}
				else
				{
					direction = 0;
				}
				
				j(window).trigger('statechange', [{'url': document.location.pathname, 'direction': direction}]);
			}
		}, 500);
		
		j(window).bind('statechange', function (e, state) {
			var html = getPageHtml(state.url);
			currentUrl = document.location.pathname;
			if(state.direction == -1)
			{
				var prev = j('.frame.prev');
				prev
					.html(html)
					.find('.cms-header')
						.cmsHeader()
					.end()
					.find('.cms-form-field-date')
						.datepicker({dateFormat: j.app.lang('settings/jsDateFormat'), firstDay: 1, showOn:'focus'})
					.end()
					.find('.cms-form-element')
						.cmsForm()
					.end()
					.find('.lightbox, .external-lightbox')
						.attr('rel', 'Gallery')
						.fancybox({'transitionIn' : 'elastic', 'transitionOut' : 'elastic'});
				
				prev
					.parent()
						.animate({'margin-left': 0}, 1300, 'easeInOutQuart', function () {
							reloadPrevCurrentNext('prev');
							slideBusy = false;
						});
			}
			else if(state.direction == 0)
			{
				j('.frame.current').html(html);
				j('.frame.current')
					.find('.cms-header')
						.cmsHeader()
					.end()
					.find('.cms-form-field-date')
						.datepicker({dateFormat: j.app.lang('settings/jsDateFormat'), firstDay: 1, showOn:'focus'})
					.end()
					.find('.cms-form-element')
						.cmsForm()
					.end()
					.find('.lightbox, .external-lightbox')
						.attr('rel', 'Gallery')
						.fancybox({'transitionIn' : 'elastic', 'transitionOut' : 'elastic'});
				slideBusy = false;
			} else {
				var next = j('.frame.next');
				next
					.html(html)
					.find('.cms-header')
						.cmsHeader()
					.end()
					.find('.cms-form-field-date')
						.datepicker({dateFormat: j.app.lang('settings/jsDateFormat'), firstDay: 1, showOn:'focus'})
					.end()
					.find('.cms-form-element')
						.cmsForm()
					.end()
					.find('.lightbox, .external-lightbox')
						.attr('rel', 'Gallery')
						.fancybox({'transitionIn' : 'elastic', 'transitionOut' : 'elastic'});
				next	
					.parent()
						.animate({'margin-left': -1732}, 1300, 'easeInOutQuart', function () {
							reloadPrevCurrentNext('next');
							slideBusy = false;
						});
			}
		});
		
		var getPageHtml = function (url) {
			var page = j(j.app.loadHtml(url));
			return page.find('.frame.current').html();
		};
		
		var reloadPrevCurrentNext = function (newCurrent) {
			var other = (newCurrent == 'prev' ? 'next' : 'prev');
			j('.frame.current, .frame.'+other).remove();
			j('.frame.'+newCurrent)
				.removeClass(newCurrent)
				.addClass('current')
				.parent()
					.prepend('<div class="frame prev"></div>')
					.append('<div class="frame next"></div>')
					.css('margin-left', -866);
		}
		
		// ipad support
		var touchable = j('body').touchme({
			isDetectHorizontalMovement: true,
			preventDefaultEvents: false,
			wipeRight: function(target) {
				j('#arrow-left').trigger('click');
			},
			wipeLeft: function(target) {
				j('#arrow-right').trigger('click');
			}
		});
	}
});
