/* Author: 

*/

MOUSEDOWN = false;

SITE = {
	common : {
		init     : function() {
		/*@cc_on
			/*@if (@_jscript_version < 7)
			//DD_belatedPNG.fix('#global-navigation, #global-navigation a, #section-title');
			/*@end
		@*/
		
		
/* 			$('#global-navigation a').blend(150); */
		
			document.body.onmousedown = function() { 
				MOUSEDOWN = true;
			}
			document.body.onmouseup = function() {
				MOUSEDOWN = false;
			}

			var carousel = $('.carousel').scrollable({
					mousewheel: true,
					onSeek: function(event, index) {
						$('.carousel-slider').trigger('carousel.move', [index]);
					}
				}).data('scrollable');
				
/*
			var slider = $('.carousel-slider');
			var handle = $('span a', slider);
			var data = {
				
				s : handle.parent().width(),
				i : this.s / carousel.getSize()
			};
*/
			if ($('.carousel-slider').length) {
				$('.carousel-slider')
					.bind('carousel.move', function(event, index) {
						
						if (typeof $(this).data('carousel.locked') != 'undefined' && $(this).data('carousel.locked') != index) return;
						$(this).removeData('carousel.locked');
						
						var handle = $('span a', $(this));
						var s = handle.parent().width();
						var i = s / carousel.getSize();
						
						
						var p = handle.position();
						
						if (Math.floor(p.left / i) != index) {
							
							if (index < (carousel.getSize() - 1)) {
								handle.animate({
									left: i * index + 'px'
								}, 100);
							}
							else {
								handle.animate({
									left: s - handle.width() + 'px' // Crude handling of rounding calculations for slider handle width.
												 					// If slider is in last slot, force it to far right end of the scale.
								}, 100);
							}
						}
					})
					.find('span a')
						.css({
							width: Math.floor(100 / carousel.getSize()) + '%'
						})
						.draggable({
							axis: 'x',
							containment: 'parent',
							drag: function(event, ui) {
			
								var s = $(this).parent().width();
								var i = s / carousel.getSize();
								
								var pos = Math.floor(ui.position.left / i);
								if (carousel.getIndex() != pos) {
									carousel.seekTo(pos);
								}
							},
							stop: function(event, ui) {
								var s = $(this).parent().width();
								var i = s / carousel.getSize();
								
								var pos = Math.floor(ui.position.left / i);
								$('.carousel-slider').data('carousel.locked', pos);
							}
						})
						.show();
			}
			


		},
		finalize : function() {}
	},
	landing : {
		init:		function() {
			
			$('.slideshow')
				.find('.content-viewer')
					.elastic({
						selector:	'.content-item'
					})
					.end()
				.find('.content-tabs')
					.tabs('.content-viewer .content-item', {
						effect: 'fade',
						fadeOutSpeed: 'slow',
						rotate: true
					})
					.slideshow({
						autoplay:	true,
						interval: '6000'
					});
		}
	},
	who : {
		landing:		function() {
			
			$('.carousel').scrollable({
					mousewheel: true
				});
		}
	},
	what : {
		landing: function() {
			
			$('.carousel').scrollable({
					mousewheel: true
				});
		}
	},
	
	work:	{
	
		init:	function() {
		
			/**
			 *	Set up soft-fade behaviour on navigation elements.
			 */	
			$('#local-navigation a').blend(150);
			//$('#folio-navigation a').blend(150);
			$('.folio-image-tabs a').blend(150);
			
			
			/**
			 *	Add cross-fade behaviour, between thumbnail and blue title overlay.
			 *	Applies to each Folio item 'block', on landing and index pages.
			 */	
			$('.folio-index-item')
				.hover(function() {
						$('.folio-index-overlay', $(this)).fadeIn('fast');
					}, 
					function() {
						$('.folio-index-overlay', $(this)).fadeOut('fast');
					})
				.autoclick({selector:	'a'});
				
			
			/**
			 *	Add cross-fade behaviour to Related Work items (on Folio pages only).
			 */	
			$('.folio-related li')
				.hover(function() {
						$('.folio-index-overlay', $(this)).fadeIn('fast');
					}, 
					function() {
						$('.folio-index-overlay', $(this)).fadeOut('fast');
					})
				.autoclick({selector:	'a'});
				
				
				
			/**
			 *	Set up slide show behaviour for folio display images.
			 */
			
			// Calculate required height of container
			// and hide (initially) all slideshow contents
			// (to prevent distracting flash of unstyled content [FOUC]).
			$('.folio-images')
				.elastic({selector:	'figure'})
				.children('figure')
					.hide();
			// If Folio contains multiple images (i.e.: a slideshow)
			// then set up slideshow ...
			if ($('.folio-image-tabs').length) {
				$('.folio-image-tabs')
					.tabs('.folio-images figure', {
		
						effect: 		'fade',
						fadeOutSpeed: 	'slow',
						rotate: 		true
					
					}).slideshow({
						autoplay:	true,
						interval:	3000
					});
			}
			// ... otherwise, display single image.
			else {
				$('.folio-images figure').show();
			}
				
				
				
			
/*
init_scroller: function() {
	if($('#hp-features').size()>0) {
		$('#hp-features .scrollbar').show();
		var intWindowWidth=$(window).width();
		var intLastColOffset=25;
		var intScrollbarWidth=parseInt($('#hp-features .scrollbar').width());
		
		if(!$("div.col:last div.feat-widget").size()) $("div.col:last").remove();
		
		$("div.col:last").addClass("last");
		var intColsWidth=($('#hp-features .col').size()*parseInt($('#hp-features .col').outerWidth(true)))-intLastColOffset;
		$('#hp-features .cols').css('width',intColsWidth+'px');
		
		var intScrollerWidth=parseInt((intWindowWidth/intColsWidth)*intScrollbarWidth);
		var intPerOne=((intColsWidth-intWindowWidth)/(intScrollbarWidth-intScrollerWidth)).toFixed(2);
		$('#hp-features .scrollbar span')
			.css('width',intScrollerWidth+'px')
			.click(function(){
				return false;
			})
			.draggable({
				axis: 'x',
				containment: 'parent',
				drag: function(event,ui) {
					var mLeft=(intPerOne*ui.position.left);
					if (mLeft>intColsWidth) {
						mLeft=intColsWidth;
					}
					$('#hp-features .cols').animate({marginLeft:'-'+mLeft+'px'},{duration:5,queue:false});
				}
			})
			.children('a').css('width',(intScrollerWidth-9)+'px');
			
		if((parseInt($('#hp-features .scrollbar span').css('left'))+intScrollerWidth)>intScrollbarWidth) {
			$('#hp-features .scrollbar span').css('left',(intScrollbarWidth-intScrollerWidth)+'px');
		}
		if((intColsWidth+parseInt($('#hp-features .cols').css('margin-left'))) < intWindowWidth) {
			$('#hp-features .cols').css('margin-left','-'+(intColsWidth-intWindowWidth)+'px');
		}
	}
}
*/

			

		},
		landing: function() {
		
			/**
			 *	Set up soft-fade behaviour on folio index (category) panels.
			 */	
/* 			$('.folio-index').blend(150); */
			
			
			/**
			 *	Set up cross-fade behaviour for folio index images.
			$('.folio-index-items')
				.each(function(index, el) {
					$(this).cycle({
						fx: 'fade',
						timeout: 2000,
						random:	1,
						delay:	Math.floor(Math.random() * 4000)
					});
				});
			 */
		
		},
		web:	function() {
			
/*
			$('.folio-images')
				.elastic({selector:	'figure'})
			$('.folio-image-tabs')
				.tabs('.folio-images figure', {
	
					effect: 		'fade',
					fadeOutSpeed: 	'slow',
					rotate: 		true
				
				}).slideshow({
					autoplay:	true,
					interval:	3000
				});
*/
			
		},
		identity:	function() {
			
/*
			$('.folio-images')
				.elastic({selector:	'figure'})
			$('.folio-image-tabs')
				.tabs('.folio-images figure', {
	
					effect: 		'fade',
					fadeOutSpeed: 	'slow',
					rotate: 		true
				
				}).slideshow({
					autoplay:	true,
					interval:	3000
				});
*/
			
		},
		multimedia:	function() {
			
/*
			$('.folio-images')
				.elastic({selector:	'figure'})
			$('.folio-image-tabs')
				.tabs('.folio-images figure', {
	
					effect: 		'fade',
					fadeOutSpeed: 	'slow',
					rotate: 		true
				
				}).slideshow({
					autoplay:	true,
					interval:	3000
				});
*/
			
		},
		print:	function() {
			
/*
			$('.folio-images')
				.elastic({selector:	'figure'})
			$('.folio-image-tabs')
				.tabs('.folio-images figure', {
	
					effect: 		'fade',
					fadeOutSpeed: 	'slow',
					rotate: 		true
				
				}).slideshow({
					autoplay:	true,
					interval:	3000
				});
*/
			
		}
		
	},
	
	contact : {
		init     : function() {
		
		}, // end init
		
		contact  : function() {
		
			$('#contact-form')
				.validator({
//					grouped: 'true',
					effect:	'standard',
					errorInputEvent: 'null',
					InputEvent: 'null'
				})
				.submit(function(e) {
				
					if (!e.isDefaultPrevented()) {
						
						$(this).ajaxSubmit({
								
							success:	function(response, status, xhr, form) {
											$('#contact-request').replaceWith($('#contact-response', $(response)));
										}
						});
						e.preventDefault();
					}
					return false;
				});
				
			var api = $('#contact-form').data('validator');
			api.getInputs().bind('blur', function() {
				api.checkValidity($(this));
			});
	
			$('#contact-form button[type="reset"]')
				.bind('click', function(e) {

					$('li', $(this).parents('form')).each(function(index) {
							var ve = $(this).data('Validator.Effect');
							if (ve) {
								ve.clear();
							}
						});
				});

		}, // end contact
		
		response : function() {}
	}
};


UTIL = {
	
	namespace:	SITE,

	fire : function(func,funcname, args) {
	
		funcname = (funcname === undefined) ? 'init' : funcname;
		if (func !== '' && UTIL.namespace[func] && typeof UTIL.namespace[func][funcname] == 'function'){
		 	UTIL.namespace[func][funcname](args);
		} 
	}, 
	
	loadEvents : function() {
	
		var bodyId = document.body.id;
		
		// hit up common first.
		UTIL.fire('common');
		
		// do all the classes too.
		$.each(document.body.className.split(/\s+/),function(i,classnm){
			UTIL.fire(classnm);
			UTIL.fire(classnm,bodyId);
		});
		
		UTIL.fire('common','finalize');
	} 
 
};

// For now, assume that this script ONLY contains UX/UI code.
// Assume that the ENVIRONMENT has been set up, that is, all
// libraries, plugins, shims, etc., have been loaded as required.
// i.e.: DON'T USE yepnope within this script.

// Which means ... the way this script is 'kicked off' may change!

// kick it all off here 
$(document).ready(UTIL.loadEvents);

