/*
Javascript for Yarra Point Mirvac. Need help? Sam Richardson (sam@richardson.co.nz)
*/

/*
	Page Initialise
*/

$(document).ready(function(event) {
	
		$('#page .footer').sctFooterBanner();
		// if( $('html').attr('id') != 'js' )
		// {
		// 	$('.scrolling_area').jScrollPane({
		// 		scrollbarWidth: 5
		// 	});
		// }
		
		// if( $('.jScrollPaneDrag').length > 0 )
		// {
		// 	$('.jScrollPaneDrag').each(function(){
		// 		$(this).append('<div class="dragUpArrow"></div>');
		// 		$(this).append('<div class="dragDownArrow"></div>');
		// 	});
		// }
		//Replace jScroll with resizing content:
		if ( $('.section_aside').height() >= 441 ) {
			var shortfall = $('.section_aside').height() - 441;
			increaseHeight(shortfall);
		}else if ( $('.section_article').height() >= 441 ) {
			var shortfall = $('.section_article').height() - 441;
			increaseHeight(shortfall);
		}
	
});

function increaseHeight(shortfall) {
	$('#page').height($('#page').height() + shortfall);
	$('.content').height($('.content').height() + shortfall);
	$('#page #modal_overlay').height(744 + (shortfall - 44));
}

/*
	Singletons
*/
if(typeof yp == 'undefined') {
	yp = {};
}

yp.constants = function() {
	var module = {};
	return module;
}();

/*
	Monads
*/
(function($) {
	
	// Dynamic dropdown menu
	$.fn.sctDropdownMenu = function(settings) {
		var		config = {
						startOpen: false,
						speed: 250
					},
					state = "closed",
					that = this;
		
		// setup 
		if (settings && config) $.extend(config, settings);
		
		function menuOpen($menu, $button, openHeight, callback) {
			var buttonOffset = $button.offset();
			
			$button.removeClass('home_closed').addClass('home_open');
			
			// show and position the menu
			$menu.css({
				display:'block',
				position: 'absolute',
				top: buttonOffset.top + $button.height() + 2,
				left: buttonOffset.left + 1,
				overflow: 'visible'
			});
			
			// record menu height
			if(!openHeight) openHeight = $menu.height();
			
			// animate the menu open
			$menu.css({height: 0}).animate({height: openHeight}, config.speed, function() {
				callback(openHeight);
			});
			
			// FadeIn Overlay
			$('#modal_overlay').fadeTo(500,0.5);
			
			return 'open';
		};
		
		function menuClose($menu, $button) {
			$button.removeClass('home_open').addClass('home_closed');
			$menu.css({overflow: 'hidden'}).animate({height: 0}, config.speed);
			$('#modal_overlay').fadeOut(500);
			return "closed";
		};
		
		this.each(function() {
			var		children = $(this).children(),
						openHeight,
						$button = children.eq(0),
						$menu = children.eq(1);

			// move menu to the top level of the dom
			$menu.css({zIndex:10}).detach();
			$('body').prepend($menu);

			// special behaviour if the menu is set to "start open"
			if(config.startOpen) {
				
				state = menuOpen($menu, $button, openHeight, function(returned_height) {
					openHeight = returned_height;
				});
				
				$('body').bind({
					'mousemove.dropdownmenu': function(event) {
						state = menuClose($menu, $button);
						$(this).unbind('.dropdownmenu');
					}
				});
			}

			//button binds (open/close the menu)
			$button.bind({
							click: function(event) {
								event.preventDefault();
								if( state === "closed" ) {
									state = menuOpen($menu, $button, openHeight, function(returned_height) {
										openHeight = returned_height;
									});
								} else if (state === "open" ) {
									state = menuClose($menu, $button);
								}
							}
						});
			
			// $button.mouseover(function(event){
			// 		event.preventDefault();
			// 		if( state === "closed" )
			// 		{
			// 			state = menuOpen($menu, $button, openHeight, function(returned_height) {
			// 				openHeight = returned_height;
			// 			});
			// 		}
			// 	});

			
			$('#modal_overlay').click(function(){
				state = menuClose($menu, $button);
			});
			
			// menu bindings (display sub menus)
			$menu.find('li').hoverIntent({
				over: function (event) {
					var		$this = $(this),
								position = $(this).position();
					
					if( $this.find('li').length > 0 ) {
						$this.addClass('open');
						$this.find('ul').css({
							position: 'absolute',
							left: position.left + $this.width(),
							top: position.top,
							zIndex : 10
						}).find('li').eq(0).addClass('first_item');
					}
				},
				out: function(event) {
					var $this = $(this);
					if( $this.find('li').length > 0 ) {
						$this.removeClass('open');
					}
				}
			});
			
		});

		return this;

	};
	
	// Dynamic dropdown menu
	$.fn.sctFooterBanner = function(settings) {
		var		that = this,
					config = {
						bindClickTo: ['contact_us', 'quick_links'],
						bannerSelector: '#banner',
						currentItem: 'general',
						speed: 500
					};
		
		// setup 
		if (settings && config) $.extend(config, settings);
		
		var $bannerSelector = $(config.bannerSelector);
		
		function getClass(name) {
			return '.'+name;
		};
		
		function changeTo(new_class) {
			$bannerSelector.find( getClass(config.currentItem) ).animate({opacity: 0}, config.speed, function() {
				$(this).css({display:'none'});
				$bannerSelector.find( getClass(new_class) ).css({display:'block', opacity:0}).animate({opacity: 1}, config.speed);
			});
			return new_class;
		};
		
		this.each(function() {
			
			// turn off other content types and enable the default
			$bannerSelector.find('.content_type').not(getClass(config.currentItem)).css({display:'none'});
			
			$(this).find('a').bind({
				click: function(event) {
					var thisClass = $(this).attr('class');
					if( $.inArray(thisClass, config.bindClickTo) >= 0) {
						event.preventDefault();
						config.currentItem = changeTo(thisClass);
					}
				}
			})
			
		});

		return this;

	};

})(jQuery);
