var base_url = "http://dev.amediacreative.com/otm/";

window.addEvent('domready',function(){

	profileTips();
	/** Hide the gallery for now. 
	var mid = $('mid');
	if(mid){
		mid.setStyles({
			'opacity':'0',
			'visibility':'hidden'
			});
	}
	var descriptionText = $E('div.text');**/
	/**
	if($('mid').hasChild('div.mainText')){
		$E('div.mainText').setStyle('display','none');
	}	
	if(descriptionText){
		descriptionText.setStyle('overflow','hidden');
	}**/
	
	/** If this is not the main page, show the main menu link **/
	var blinkElements = [];
	
	if(document.baseURI != base_url){
		var returnLink = $('returnlink');
		returnLink.removeProperty('id');
		returnLink.addClass('returnlink');
		
		/** Include the bottom back link. **/
		blinkElements.include(returnLink);
	}
	
	/** If the URL contains a section paramater , set the scroll to the bottom of the window, reveal the swish. **/
	var pageArguments = getArgs();
	
	if(pageArguments.section){
		var swoosh;
		//var switchPage;
		
		if ($('nonFooter').hasChild($E('.page')) ){
			
			var switchPage = $E('.page').getProperty('id');
		
			switch(switchPage){
				case 'page_captivate': swoosh = 'swoosh2'; break;
				case 'page_motivate': swoosh = 'swoosh1'; break;
				case 'page_celebrate': swoosh = 'swoosh5'; break;
				case 'page_fascinate': swoosh = 'swoosh3'; break;
				case 'page_synergy': swoosh = 'swoosh4'; break;
				case 'default': break;
				default: break; 
			}
			
			document.documentElement.scrollTop = document.documentElement.scrollHeight - document.documentElement.clientHeight;
			
			$(swoosh).setStyle('height',1700);
			
		}
	}else{
		window.addEvent('load',function(){
			var swoosh;
			//var switchPage;
			
			if ($('nonFooter').hasChild($E('.page')) ){
				
				var switchPage = $E('.page').getProperty('id');
			
				switch(switchPage){
					case 'page_captivate': swoosh = 'swoosh2'; break;
					case 'page_motivate': swoosh = 'swoosh1'; break;
					case 'page_celebrate': swoosh = 'swoosh5'; break;
					case 'page_fascinate': swoosh = 'swoosh3'; break;
					case 'page_synergy': swoosh = 'swoosh4'; break;
					case 'default': break;
					default: break; 
				}
				
				var thisGrowScroller = new growScroller(swoosh,'height',{
						duration:4000			
				});
					
				thisGrowScroller.start(0,1700);
			}
		
		});
	}
	
	/** Start the gallery. **/
	startGallery();
	
	
	
	/** Include the top synergy link. **/
	var synergyDiv = $E('div','synergy_menu');
	var synergyLink = synergyDiv.getElement('a');
	blinkElements.include(synergyLink);
	
	
	
	
	blinkElements.each(function(element,index){
		(function(){
		var thisRotator = menuFX.periodical(700, element);
		//thisRotator;
		}).delay(index*100);
	});
	
	
});


/*
 * This function parses ampersand-separated name=value argument pairs from
 * the query string of the URL. It stores the name=value pairs in
 * properties of an object and returns that object. Use it like this:
 *
 * var args = getArgs( );  // Parse args from URL
 * var q = args.q || "";  // Use argument, if defined, or a default value
 * var n = args.n ? parseInt(args.n) : 10;
 * 
 * This function is courtesy O'Reilly Media. It's free to use in our applications as long as we don't
 * resell it for instructional purposes.
 *
 */

function getArgs( ) {
    var args = new Object( );
    var query = location.search.substring(1);     // Get query string
    var pairs = query.split("&");                 // Break at ampersand
    for(var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');          // Look for "name=value"
        if (pos == -1) continue;                  // If not found, skip
        var argname = pairs[i].substring(0,pos);  // Extract the name
        var value = pairs[i].substring(pos+1);    // Extract the value
        value = decodeURIComponent(value);        // Decode it, if needed
        args[argname] = value;                    // Store as a property
    }
    return args;                                  // Return the object
}

function startGallery() {
	var myGallery = new gallery($('otmGallery'), {
		timed: true,
		showArrows: true,
		showCarousel: false,
		embedLinks: false,
		delay: 9000,
		defaultTransition:'fadeslideleft'
	});
}

function menuFX() {

	var fadeElement = this.getElement('span[class=peg]');
	var fx = new Fx.Style(fadeElement, 'opacity', {
		duration: 500,
		transition: Fx.Transitions.Quart.easeInOut
	});

	var elementOpacity = fadeElement.getStyle('opacity');

	if(elementOpacity == 1 || elementOpacity == "visible") {
		fx.start(1,0);
		//fadeElement.setStyle('opacity',0);
	}
	else {
		fx.start(0,1);
		//fadeElement.setStyle('opacity',1);
	}
}



profileTips = function() {  
	theseTips =	new Tips($$('.profileElement'),{
		 maxTitleChars: 50,
		 maxOpacity: .9,
		 initialize:function(){
			this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
		},
		onShow: function(toolTip) {
				this.fx.start(1);
		},
		onHide: function(toolTip) {
				this.fx.start(0);
		}
	});
};

/** OTM Resize/Scroll Class 

	This class will automatically scroll such that 
	the bottom of the div that is growing vertically
	will always be in the middle of the browser window.
	
	Doesn't take any other options than the normal FX.Style,
	but one of the manipulated style Elements has to be height. **/

var growScroller = Fx.Style.extend({
	scroller : Class.empty(),
	initialize: function(el, property, options){
	/** I'm pretty sure overwriting this function is unnecessary now, but 
		we might need to put something in here later. **/
		this.parent(el,property,options);
		/** This gets the real offset of the element when it starts, before it grows. **/
		//this.coordinates = this.element.getPosition();
		this.startHeight = this.element.getPosition().y;
		/** Get the window scroller. **/
		this.windowScroller = new Fx.Scroll(window,{wait:true,duration:100,transition:Fx.Transitions.linear});
		this.halt = false;
	},
	step: function(){
		this.parent();
		//this.element.setStyle(this.property, this.css.getValue(this.now, this.options.unit, this.property));
		/** In case someone resizes a window, this is here. **/
		if(window.pageYOffset >= 1200 && !this.halt) {
			//targetY = 1200;
			this.halt = true;
			//this.windowScroller.scrollTo(0,targetY);
			
			/** Fade in the mid div.. 
			
			var mid = $('mid');
			//startGallery();
			//mid.setStyle('display','block');
			mid.setStyle('visibility','visible');
			
			midFade = new Fx.Style(mid,'opacity',{duration:700});
			midFade.start(0,1);**/
			
			
			
			var descriptionText = $E('div.text');
			
			/**
			if($('mid').hasChild('div.mainText')){
				$E('div.mainText').setStyle('display','none');
			}	**/
			
			if(descriptionText){
				descriptionText.setStyle('overflow','auto');
			}
			
			/** If the window is smaller (height wise) than the gallery, just scroll to the gallery. **/
			
			if(window.innerHeight <= 500){
				var targetToScroller = $('main').getElement('h2[class=png]');
				this.windowScroller.toElement(targetToScroller);
			}
			//delete midFade;
		}
		if(!this.halt){
			var height = window.getHeight();
			height = height/2;
			
			/** Now move the window position to the height of the element + the original position - 1/2 of the window height. **/
			var elementDimensions = this.element.getSize();
			var elementSize = elementDimensions.size.y;
			var targetY = elementSize + this.startHeight - (height);
			this.windowScroller.scrollTo(0,targetY);
		}
		
	}
})

/* SCROLL DOWN */
// JavaScript Document

//Requires: mooTools 1.1 or later.

//Declare autoScroller.
var autoScroller;
var initialized = false;

//This function initiates the luxeScroller object when the page is fully loaded.
//It also sets "initialized" to true.
function initScrollFx(){
	autoScroller = new Fx.Scroll(window);
	initialized = true;
}

//This function scrolls the window to the target element, which can nearly anything.
// ..div tags, id, etc.

function scrollAuto(targetElement){
	
	//Make sure the scroller is initialized so the browser doesn't complain.
	//If not initialized, do nothing and return. (Innocuous)
	if(initialized){
		//Scroll to the target element.
		autoScroller.toElement(targetElement);
	}
	else return;
}


/* SHADOWBOX */

function shadowBoxInit() {
	var options = {
		resizeLgImages:		false,
		handleUnsupported:  'remove',
		type:				'html',
		keysClose:			['c',27]
	}
	Shadowbox.init(options);
}