//wallpaper.js
function resizeWallpaper() {

	if($('wallpaper') && (typeof(FLASH_LAYOUT) == 'undefined' || !FLASH_LAYOUT)) {
		var img = $('wallpaper').down('img');
		$('wallpaper').style.visibility = 'hidden';
		var imgW = 1920;
		var imgH = 1080;
		var viewport = document.viewport.getDimensions();
		
		if($('footer') && $('header')) {
			var offset = $('footer').getHeight() + $('header').getHeight();
		} else {
			var offset = 0;
		}
		
   	if(img.width != viewport.width) {
   		var factor = viewport.width / imgW;			
			img.height = imgH * factor;
			img.width = imgW * factor;
   	}
   	
   	if(img.height < (viewport.height - offset)) {
			var factor = (viewport.height - offset) / imgH;			
			img.height = imgH * factor;
			img.width = imgW * factor;			
		}
		
		//reposition vertically if more than 90% wallpaper is cropped
		
		if((viewport.height - offset)  < (img.height)) {
			//weighted to not crop top
			img.style.marginTop = ((img.height - (viewport.height - offset))/-2.75) + 'px';
		} else {
			img.style.marginTop = '0';
		}
		if(viewport.width < (img.width)) {
			img.style.marginLeft = ((img.width - viewport.width)/-2) + 'px';
		} else {
			img.style.marginLeft = '0';
		}
		$('wallpaper').style.visibility = 'visible';
	}
	
}
if(typeof(FLASH_LAYOUT) == 'undefined' || !FLASH_LAYOUT) {
	Event.observe(document.onresize ? document : window, "resize", resizeWallpaper); 
	document.loaded ? resizeWallpaper() : document.observe("dom:loaded", resizeWallpaper);
}

//captions.js
function imageTitles() {
 $$('img.banner.caption').each(function(img) {
			
 			var caption = new Element('div', 
 				{
 					'class': 'caption-container',
 					'style': 'position: absolute; width: ' + img.getWidth() + 'px; top: ' + img.getHeight() + 'px'
 				}
 			);
 			caption.insert('<p class="caption-content">' + img.title + '</p>');
 			
 			 		
			img.up('a').insert({after: caption});
			
			var container = img.up('div');
			container.setStyle({overflow: 'hidden'});
		
			img.up('a').observe('mouseover', function(event) {
					var caption = this.next('.caption-container');
					//caption.effect = new Effect.Move(caption, {duration: 0.3, y:(this.down('img').getHeight() - caption.getHeight()), mode:'absolute',  transition: Effect.Transitions.sinoidal});
					caption.effect = new Effect.Move(caption, {duration: 0.3, y:(this.down('img').getHeight() - caption.getHeight()), mode:'absolute'});
					//caption.style.top = (this.down('img').getHeight() - caption.getHeight()) + 'px';
			});
			container.observe('mouseout', function(event) {
					
					
					var pos = this.viewportOffset();
					var mouse = [event.pointerX(), event.pointerY()];				
					if(mouse[0] <= pos[0] || mouse[1] <= pos[1] || mouse[0] >= pos[0] + this.down('img').getWidth() || mouse[1] >= pos[1] + this.down('img').getHeight()) {
						var caption = this.down('.caption-container');
						//caption.effect = new Effect.Move(caption, {duration: 0.3, y:this.getHeight(), mode:'absolute', transition: Effect.Transitions.sinoidal});	
						caption.effect = new Effect.Move(caption, {duration: 0.3, y:this.getHeight(), mode:'absolute'});	
						//caption.style.top = this.getHeight() + 'px';
					}
					
					
					
			});
			
 		}
 );
}

document.loaded ? imageTitles() : document.observe("dom:loaded", imageTitles);

/**
 * Menu controller class
 *
 */

var SlideMenu = Class.create({
  initialize: function(containerPrefix, itemPrefix, menuIds, parentIds) {
	
	//initialize menu array
	this._menuIds = menuIds;
	this._parentIds = parentIds;
	
    //initilize variables here
    this._menus = new Object();
    
    //full tree structure of nodes
    this._nodes = new Object();

    //array that functions as a stack holding open menu references
    this._stack = [];
    //domId of current menu
    this._activeId = null;
    this.containerPrefix = containerPrefix;
    this.itemPrefix = itemPrefix;
  },
  
	add: function(parentId, nodeId) {		
  		if(parentId != null && this._menus[parentId] == undefined) {  		
  			this._menus[parentId] = new SlideMenu_Container(parentId);
  		}
  		this._nodes[nodeId] = new SlideMenuNode(parentId, nodeId);
  		//this._menus[parentId].push(nodeId);
	},
	
	
	showMenu: function(menuId, persist) {
		persist = persist == undefined ? false : true;		
		if($(this.containerPrefix + menuId)) {
			//build heirarchy stack for new menu id
			var tmpStack = [];
			var tmpId = menuId;
			if(this._nodes[menuId] != undefined && menuId != this._activeId) {
				//tmpId = this._nodes[tmpId].parentId;
				tmpId = menuId;				
				while(tmpId != null) {
					if(this._nodes[tmpId] != undefined) {						
						tmpStack.unshift(tmpId);
						tmpId = this._nodes[tmpId].parentId;					
					} else {
						tmpId = null;	
					}	
				}				
				while(this._stack.length > 0 && !this._stack.compare(tmpStack.slice(0,this._stack.length))) {
				
					//pop stack
					obj = this._stack.pop();
					if($(this.containerPrefix + obj)) {
							//de-activate menu item
							this._nodes[obj].blur();	
							$(this.containerPrefix + obj).style.display = 'None';
							if($(this.containerPrefix + obj).effect != undefined) {
								$(this.containerPrefix + obj).effect.cancel();								
							}						
							$(this.containerPrefix + obj).style.left = '0px';
					}
				}
				this._stack = [];
				//open menus in tmpStack	
						
				while(tmpStack.length > 0) {					
					obj = tmpStack.pop();
					if($(this.containerPrefix + obj)) {		
							//activate menu item
							this._nodes[obj].hover(persist);				
							if(!($(this.containerPrefix + obj).visible()) && !$(this.containerPrefix + obj).down('.menu-empty-content')) {								
								//hide from screen
								var child = $(this.containerPrefix + obj).down('.menu-content');
								
								$(this.containerPrefix + obj).style.display = 'block';								
								$(this.containerPrefix + obj).style.zIndex = 99 - tmpStack.length;
								
								var w = child.getWidth();
								if($(this.containerPrefix + obj).effect != undefined) {
									$(this.containerPrefix + obj).effect.cancel();
								}
							
								child.style.left = (w*-1) + 'px';
																
								//$(this.containerPrefix + obj).effect = new Effect.Move(child, {duration: 0.3, x:w,  transition: Effect.Transitions.sinoidal});
								//child.style.left = w + 'px';
								$(this.containerPrefix + obj).effect = new Effect.Move(child, {duration: 0.3, x:w});
							}
					}
					this._stack.unshift(obj);
				}				
			}
			//this._stack.push(menuId);
			//$(this.containerPrefix + menuId).style.display = 'Block';	
			this._activeId = menuId;
		}	else {
			
			
		}
			
				
	}	
});


//slidemenu.js

/**
 * Menu container model class, holds a group of SlideMenu_Item Objects
 * Scrolling is managed here also
 *
 */ 
var SlideMenu_Container = Class.create({
  initialize: function(domId) {
    //initilize variables here
    this.domId = domId;
    //item that opens this menu
   // this.parentId = parentId;
    this._items = new Object();

  },
  getParentMenuId: function() {
  		
  		return null;
	},
  //pushes a new menu item onto the end of the menu
  push: function(itemId) {
  		//this._items[itemId] = new SlideMenu_Item(itemId, this.domId);
	}
  
});

/**
 * Menu node model class
 *
 */ 
var SlideMenuNode = Class.create({
  initialize: function(parentId,nodeId) {
    //initilize variables here
    this.domId = nodeId;
    this.parentId = parentId;
  },
  hover: function(persist) {
  	if(persist) {
  		$('mainMenu_Item_' + this.domId).down('a').addClassName('active');  	
  	}
  	
  	//$('mainMenu_Item_' + this.domId).down('a').style.color = '#009ADE';  	
  	
  },
  blur: function() {
  	//$('mainMenu_Item_' + this.domId).down('a').style.color = '#FFFFFF';
  }  
});

 Array.prototype.compare = function(testArr) {
    if (this.length != testArr.length) return false;
    for (var i = 0; i < testArr.length; i++) {
        if (this[i].compare) { 
            if (!this[i].compare(testArr[i])) return false;
        }
        if (this[i] !== testArr[i]) return false;
    }
    return true;
}

//deeplinkhelper.js

//call this to switch from flash to html and change theme
function deeplinkToHtml(th) {
 	if(typeof(THEME) != 'undefined' && THEME == 'flash') {
 		
 		
 		var dl = parseDeeplink();
 		if(dl != '') {
	 		var redirect  = th + '/' + dl;
	 		if(WEBROOT != undefined) {
	 			redirect = WEBROOT + redirect;	
	 		} 	
	 	} else {
	 		//no deep link / no flash
	 		redirect = window.location.toString();
	 		if(WEBROOT != undefined) {
	 			redirect = redirect.replace(WEBROOT, WEBROOT + th + '/')
	 		}
	 	}
	 	
 		location.href = redirect;	
 	}  	
}

function parseDeeplink() {
	htmlLink = "";
	var loc = window.location.toString();
	if(loc.indexOf('#') != -1) {
 		loc = loc.substr(loc.indexOf('#') + 2);
 		var controller = loc.substr(0,loc.indexOf('/'));
 		controller = controller.replace('-','_');
 		htmlLink = controller + loc.substr(controller.length); 		
 	}
 	
 	return htmlLink;
}

function gotoDeeplink(deeplink) {
	if(swfobject.hasFlashPlayerVersion('9.0.47')) {
		//need to switch here for chrome and safari
		
		location.href = deeplink;
		if(!$('flashcontent')) {			
			window.location.reload();
		}
		return false;		
	}
	return true;	
}
