/*!
 #####################################################################
 # 
 # Columbus Media International
 # JavaScript setup routines
 #
 # Hand-crafted by Phenotype (phenotype.net)
 #
 #####################################################################
 */
	
	/* =Assign setup routines
	------------------------------------------------------------------- */
	
		// Called when DOM is ready
		$(document).ready( domSetup );
		
		// Called when entire page is loaded
		$(window).load( pageSetup );
		
		// Called when DOM is unloaded
		$(window).unload( domUnload );
	
	/* =Global variables
	------------------------------------------------------------------- */
	
	
	/* =Declare setup routines
	------------------------------------------------------------------- */
	
	/**
	 * domSetup()
	 *
	 * All JavaScript requiring initialisation on DOM LOAD should be called
	 * from this routine.
	 */
	function domSetup() {

		/* =IE6 PNG Fix
		------------------------------------------------------------------- */
		
			if ( $.browser.msie && $.browser.version == '6.0' ) {
				
				// Supplement modernizr applied classes
				$('html').addClass( 'ie6' );
				
				// Fix PNGs
				DD_belatedPNG.fix( '#header h1 a, a.cycle, #initiatives #primary-content article.initiative .preamble p.action, #partners #primary-content article .media ul#partners-listings li h2 a, #cboxClose');
			}
		
		/* =Enhancements
		------------------------------------------------------------------- */
		
			/**
			 * Home
			 */
		
			// Flash animation embedding
			if ( $('#home')[0] ) {
				
				// Set site root path
				//var siteRoot			= '/columbus/';
				var siteRoot			= '/';
				
				// Set express install location
				var expressInstaller 	= null;
				
				// Set Flash embedding parameters
				var replacedContentID 	= 'alternative-content';	
				var swf 				= siteRoot + 'assets/templates/columbus/flash/hello.swf';	
				var width 				= '600';
				var height 				= '400';
				var flashVersion 		= '9.0.0';
				
				var flashvars = {
				};
				var params = {
					wmode 				: 'transparent',
					scale				: 'showall',
					allowScriptAccess	: 'always',
					allowFullScreen		: 'false',
					bgcolor				: '#ffffff'
				};
				var attributes = {
					id					: 'flash-content',
					name				: 'flash-content'
				};
				
				// Replace content
				swfobject.embedSWF( swf, replacedContentID, width, height, flashVersion, expressInstaller, flashvars, params, attributes );
				
			}
			
			/**
			 * About
			 */
		
			// About tile centring
			$('#about #about-listings li h2, #about #about-listings li p').wrapInner('<span />').each( function() {
				// Make reference
				var $this = $(this);
				var $text = $this.find('span');
				// Vertically centre text
				$text.css( {
					'margin-top':	'-' + ( $text.height() / 2 ) + 'px'
				} );
			} );
			
			// Animate About tiles
			$('#about #about-listings li').bind( 'mouseenter', function() { 
				// Make reference
				var $this = $(this);
				// Animate tile contents
				$this.find('h2').animate( {
					'top'	:	'-200px'
				}, 200 );
				$this.find('p').animate( {
					'top'	:	'0px'
				}, 200 );
			} );
				
			$('#about #about-listings li').bind( 'mouseleave', function() { 
				// Make reference
				var $this = $(this);
				// Animate tile contents
				$this.find('h2').animate( {
					'top'	:	'0px'
				}, 200 );
				$this.find('p').animate( {
					'top'	:	'200px'
				}, 200 );
			} );
			
			/**
			 * Initiative
			 */
			
			/* Initiative content .body toggle */
			
			// Hide .body
			$('#initiatives #primary-content article.initiative .body').hide();

			// Inject toggler
			$('#initiatives #primary-content article.initiative .preamble').append('<p class="action">Read more</p>');
			
			// Bind toggler event
			$('#initiatives #primary-content article.initiative .preamble p.action').bind('click', function() {
				// Make reference
				var $this = $(this);
				// Check state
				if ( $this.hasClass('toggled') ) {
					// Remove class and change text
					$this.removeClass('toggled').text('Read more');
				} else {
					// Add class and change text
					$this.addClass('toggled').text('Read less');
				}
				// Toggle related .body
				$this.parents('article').find('.body').slideToggle();
			} );
			
			/* Initiative images slideshow for intiatives with more than one image */
			$('#initiatives article.initiative #initiative-images.scrollable').each( function() {
				// Make reference
				var $this = $(this);
				
				// Count images
				if ( $this.find('img').length > 1 ) {
					// Append cycle buttons
					$this.append('<a class="cycle previous">Previous</a> <a class="cycle next">Next</a>')
					// Init slideshow
					.scrollable( {
						items		:	'.scroll-container ul',
						circular	: 	true,
						mousewheel	:	false,
						speed		:	600,
						easing		:	'easeInSine',
						next		:	'.scrollable .next',
						prev		:	'.scrollable .previous'
					} )
					.autoscroll( {
						steps		:	1,
						interval	:	6000,
						autoplay	:	true,
						autopause	:	true
					} );
					
				}
				
			} );

			/**
			 * People
			 */
			
			$('#people #people-listings ul a').colorbox( { 
				width		:	'640px',
				height		:	'360px',
				top			:	'180px',
				scrolling	:	false,
				onComplete	:	function() {
					// Allow overflowing content
					$('#colorbox, #cboxLoadedContent').css({
						'overflow'	:	'visible'
					});
					// Modify external links
					$('#cboxLoadedContent a[rel="external"]').attr( 'target', 'blank' );
				}
			} );
				
			/**
			 * Partners
			 */
			
			// Colorbox
			$('#partners .region #partners-listings ul a').colorbox( { 
				width		:	'600px',
				height		:	'400px',
				top			:	'110px',
				scrolling	:	false,
				onComplete	:	function() {
					// Allow overflowing content
					$('#colorbox, #cboxLoadedContent').css({
						'overflow'	:	'visible'
					});
					// Modify external links
					$('#cboxLoadedContent a[rel="external"]').attr( 'target', 'blank' );
				}
			} );
			
			// Tooltips
			$('#partners .region #partners-listings h2 > a').tooltip( { 
				/* delay		: 	30, */
				relative	:	true,
				effect		:	'slide',
				position	:	'top left',
				direction	: 	'up',
				bounce		: 	true,
				offset		:	[5, 5]
			} ).dynamic( {
				'up'	: {
					direction	:	'down'
				}
			} );
		
		/* =Navigation
		------------------------------------------------------------------- */
			
			/*
			Setup external links (target attribute not allowed by XHTML 1.1)
			(see http://www.sitepoint.com/article/standards-compliant-world/)
			*/
			$('a[rel="external"]').attr( 'target', 'blank' );

			
		/* =Forms
		------------------------------------------------------------------- */
		
			/**
			 * Form element enhancements
			 */
			
			// Replace submit buttons
			$('input[type="submit"]').each( function() {
				// Make reference
				var $this = $(this);
				// Replace with span
				$this.replaceWith( '<span class="' + $this.attr('type') +'" id="' + $this.attr('id') +'" title="'+ $this.attr('value') +'">'+ $this.attr('value') +'</span>' );
				// Bind click event to submit form
				$('span#'+$this.attr('id') ).bind( 'click', function() {
					$(this).parents('form').submit();
				});
			
			});
			
			/**
			 * Only required for IE6 compatibility
			
			// Add classes to represent types
			$('input, button').each( function() {
				// Make reference
				var $this = $(this);
				// Add class	
				$this.addClass( $this.attr('type') );
			}); */
			
			/**
			 * Partners region/country selector
			 */
			if ( $('.selector')[0] ) {
				
				// Store properties of selector list in array
				var selector = Array(
					{
						'text'	:	' ',
						'value'	:	-1
					}
				);
				
				$('.selector ul li').each( function(){
					// Make reference
					var $this = $(this);
					// Determine value
					var value = $this.find('a').attr( 'href' );
					value = ( value != undefined ) ? value : 0;
					// Add to array
					selector.push({
						'text'	:	$this.text(),
						'value'	:	value
					} );
				} );
				
				// Build select field to replace list
				var output = '<select>';
				
				for ( var i in selector ) {
					
					if ( selector[i].value == 0 ) {
						output += '<option value="' + selector[i].value + '" disabled="disabled">' + selector[i].text + '</option>';
					} else {
						output += '<option value="' + selector[i].value + '">' + selector[i].text + '</option>';
					}
					
				}
				
				output += '</select>';
				
				// Replace content
				$('.selector ul').replaceWith( output );
				
				/* 
				
				// (Now) redundant code to select option from list that corresponds to current page
				 
				// Set selected list item
				var selected = window.location.pathname;
				
				if ( selected != '' ) {
					
					// Select region/office from list
					$('.selector option[value="'+selected+'"]').attr('selected', 'selected');	
					
				} else {
					// Select first in list
					$('.selector option:first-child').attr('selected', 'selected');
				}
				
				*/

				// Bind to change event
				$('.selector select').bind('change', function(){
					// Get region/country
					var selected = $('.selector select option:selected').attr('value');
					
					// Check selected option has an action associated with it
					if ( selected != 0 && selected != -1 ) {
					
						// Determine action to take
						if ( $('#region-selector')[0] ) {
							// Load new region page
							window.location = selected;
						} else {
							// Launch partner popup
							$.colorbox( { 
								href		:	selected,
								width		:	'600px',
								height		:	'400px',
								top			:	'110px',
								scrolling	:	false,
								onComplete	:	function() {
									// Allow overflowing content
									$('#colorbox, #cboxLoadedContent').css({
										'overflow'	:	'visible'
									});
									// Modify external links
									$('#cboxLoadedContent a[rel="external"]').attr( 'target', 'blank' );
								}
							} );
						}
						
					}

				});
				
			}
	
	}
	
	/**
	 * pageSetup()
	 *
	 * All JavaScript requiring initialisation on PAGE LOAD should be called
	 * from this routine (all images and elements should be loaded and ready to
	 * manipulate by this point)
	 */
	function pageSetup() {

	}
	
	/**
	 * domUnload()
	 *
	 * Called when page/DOM is unloaded
	 */
	function domUnload() {

	}
