$(document).ready(function() {
	$( "button, input:submit, a.button").button();
	
	var pageFade = 1000;
	var linkFade = 200;
	var justPushedState = false;
	var linkInterceptClasses = 'aside.sidebar a, .ct-profile-list a';
	
	/**
	 * URL management, dynamic content loading and fades
	 */
	var History = window.History;
	if (History.enabled) {
		var loadContent = function(url, doState) {
			if (!Modernizr.csstransitions) {
				$('#main-container').fadeOut(linkFade, function() {
					$('#main-container').load(url+' .main', function(resp) {
						document.title = $(resp).filter('title').text();
					}).fadeIn(linkFade);
					hook_pageload();
				});
			}
			else {
				$('#main-container').addClass('fadeOut')
					.bind('webkitTransitionEnd oTransitionEnd transitionend', function(e) {
						if ($(this).hasClass('fadeOut')) {
							$(this).load(url+' .main', function(resp) {
								document.title = $(resp).filter('title').text();
								$(this).removeClass('fadeOut').addClass('fadeIn');
								hook_pageload();
							});
						}
						else if ($(this).hasClass('fadeIn')) {
							$(this).removeClass('fadeIn');
						}
						$(this).unbind('webkitTransitionEnd oTransitionEnd transitionend');
					});
			}
			
			if (doState !== undefined) { 
				stateObj = { url: url };
				justPushedState = true;
				History.pushState(stateObj, '', url);
				justPushedState = false;
			}
		};
		
		$(linkInterceptClasses).live('click', function(e) {
			if (!$(this).hasClass('no-intercept')) {
				e.preventDefault();
				loadContent(this.href, true);
			}
		});
		History.Adapter.bind(window, 'statechange', function() {
			var state = History.getState();
			if (state !== null && !justPushedState) {
				loadContent(state.url);
			}
		});
	}
	
	/**
	 * Fade page in
	 */
	$('body').hide().fadeIn(pageFade);
	
	/**
	 * Centre Viewport 
	 */
	homeCentreViewport();
	
	/**
	 * Home page link backgrounds
	 */
	 $('#home-nav a').bind('mouseover', function() {
		var bg = '#'+$(this).attr('data-bg');
		$(bg).fadeIn();
		$(this).css({
			'color': '#EF1F73'
		});
	 })
	 .bind('mouseleave', function() {
		var bg = '#'+$(this).attr('data-bg');
		$(bg).fadeOut();
		$(this).animate({
			'color': '#ffffff'
		}, 300);
	 });
	 
	/**
	 * Custom Drop down
	 */
	$('.accordion .accordion-header').live('mouseover', function() {
		if ($(this).next().hasClass('accordion-content') && !$(this).hasClass('accordion-header-open')) {
//			if (!Modernizr.cssanimations) {
				$(this).addClass('accordion-header-open');
				if ($(this).next().html() != '') {
					$(this).next().slideDown().addClass('accordion-open');
				}
//			} else {
////				$(this).addClass('active').next().addClass('accordion-open slideDown');
//			}
		};
		return false;
	});
	
	/*$('.accordion .accordion-open').live('mouseout', function() {
		if ($(this).children('ul li a.active').length == 0) {
			alert('hello');
			$(this).prev().removeClass('active');
		};
	});*/
	
	// Keeping sub nav elements open
	if ($('.accordion .accordion-content a.active').length > 0) {
		$('.accordion .accordion-content a.active').first()
			.parents('.accordion-content')
			.show()
			.prev()
			.addClass('active accordion-header-open');
	}
	
	// Closing out of context sub menu elements on click
	$('.accordion a, .sub-nav a').click(function() {
		var currentMenuActiveId = $(this).parents('.accordion-content').attr('id');
		
		// Make the sub menu active
		$('.accordion .active, .sub-nav .active').removeClass('active');
		$(this).parent().addClass('active').children('.accordion-header').addClass('active');
		$(this).parent().children('.accordion-content').addClass('active');
		$(this).addClass('active');
		
		// Make other menus inactive
		$('.accordion .accordion-content').each(function() {
			if (currentMenuActiveId != $(this).attr('id')) {
//				if (!Modernizr.cssanimations) {
					$(this).slideUp().removeClass('accordion-open slideDown').prev().removeClass('accordion-header-open');
//				} else {
//					$(this).removeClass('accordion-open slideDown').addClass('accordion-close slideUp').prev().removeClass('active');
//				}
			}
		});
		$(this).parents('.accordion-content').prev().addClass('active');
	});
	
	if (!Modernizr.csstransitions && Modernizr.rgba) {
		$('.sidebar .accordion .accordion-content A').live('mouseout', function() {
			if (!$(this).hasClass('active')) {
				$(this).css({
						'background-color': 'rgba(239, 31, 115, 1)'
					})
					.animate({
						'background-color': 'rgba(239, 31, 115, 0)'
					}, 100)
			}
		});
		
		$('.sidebar .accordion .accordion-content A').live('mouseover', function() {
			if (!$(this).hasClass('active')) {
				$(this).css({
					'background-color': 'rgb(239, 31, 115)'
				});
			}
		});
	}
	
	/**
	 * Listing Hover Cards
	 */
	$('.ct-profile-list .query_block .ct-profile').live('mouseover', function() {
		var selectedProfile = $(this).html();
		var id = $(this).attr('id');
		var idSelected = 'selected-' + id;
		
		if (!Modernizr.csstransitions) {
			$('.ct-profile-list .query_block .ct-profile-over').fadeOut(200);
			$('.ct-profile-list .query_block .ct-profile-over').remove();
		} else {
			$('.ct-profile-list .query_block .ct-profile-over').addClass('fadeOut')
				.bind('webkitTransitionEnd oTransitionEnd transitionend', function(e) {
					$(this).remove();
				});
		}
		if ($('#' + idSelected).length == 0) {
			$(this).after('<div id="' + idSelected + '" class="ct-profile-over selected">' + selectedProfile + '</div>');
			var position = $(this).position();
			$('#' + idSelected).css({
				position: 'absolute',
				top: (-10) + position.top
			});
		}
	});
	$('.ct-profile-list .query_block .ct-profile-over').live('mouseleave', function() {
		if (!Modernizr.csstransitions) {
			$(this).fadeOut(200);
			$(this).remove();
		} else {
			$(this).addClass('fadeOut')
				.bind('webkitTransitionEnd oTransitionEnd transitionend', function(e) {
					$(this).remove();
				});
		}
	});
	
	/**
	 * Map
	 */
	var office = $('.ct-map .map .office a.selected').parent().parent().html();
	$('.ct-map .map-details').fadeOut(300, function() {
		$(this).html(office).fadeIn(800);
	});
	$('.ct-map .map .office a').live('click', function(e) {
		e.preventDefault();
		$('.ct-map .map .office a').removeClass('selected');
		$(this).addClass('selected');
		var office = $(this).parent().parent().html();
		$('.ct-map .map-details').fadeOut(300, function() {
			$(this).html(office).fadeIn(800);
		});
	});
	
	/**
	 * Contact dialog
	 */ 
	$('.popup').live('click', function(e) {
		e.preventDefault();
		$('#dialog').html('<div class="loading">Loading..</div>');
		$("#dialog").load($(this).attr('href'), function() {
			$( "button, input:submit, a.button").button();
		}).dialog({
			position: 'center',
			title: $(this).attr('title'),
			height: 330,
			width: 400,
			modal: true,
			draggable: false,
			resizable: false
		});
	});
	$('.popup-iframe').live('click', function(e) {
		e.preventDefault();
		$('<iframe src="' + $(this).attr('href') + '" style="padding:10px;width:100%;height:300px">Loading..</iframe>').dialog({
			position: 'center',
			title: $(this).attr('title'),
			height: 330,
			width: 400,
			modal: true,
			draggable: false,
			resizable: false
		}).width(380).height(330);
	});
	
	$('#dialog .form.secureddownload FORM, #dialog .form.contact FORM').live('submit', function(e) {
		e.preventDefault();
		var formdata = $(this).serialize();
		$('#dialog').html('<div class="loading">Loading..</div>');
		$.post($(this).attr('action'), formdata, function(returndata) {
			$('#dialog').html(returndata);
			$( "button, input:submit, a.button").button();
		});
	});
	
	/**
	 * xhtml external links
	 */
	$('a[rel=external][href*="http"]').live('click', function(e) {
		window.open(this.href);
		e.preventDefault();
	});
	
	/**
	 * Photo Gallery
	 */
	ini_gallery();
});

$(window).resize(function() {
	homeCentreViewport();
});

function homeCentreViewport() {
	var docHeight = $(document).height();
	var menuHeight = $('#home-nav').height();
	var logoHeight = $('.home-logo').height();
	var bgHeight = $('.bg').height();
	var buffer = docHeight * 0.005;
	
	$('#home-nav').css('top', ((docHeight * 0.5) - menuHeight * 0.5) - buffer);
	$('.home-logo').css('top', ((docHeight * 0.5) - logoHeight * 0.5) - buffer);

	var bgTop = (docHeight * 0.5) - (bgHeight * 0.5);
	$('.bg').css('top', bgTop );
	$('.bg').css('height', docHeight - bgTop - 10);
}

function ini_gallery() {
	if (jQuery().slides) {
		$('#gallery').slides( {
			generateNextPrev: true,
			pagination: false,
			generatePagination: false,
			preload: true
		});
		$("#gallery a").addClass('no-intercept');
		$("#gallery a.next, #gallery a.prev").html('<span class="background"></span><span class="arrow"></span>');
	}
}

function hook_pageload() {
	
	// Initiate other js code on page reload
	ini_gallery();
}
