

var GettyMenuClass = Class.create();
GettyMenuClass.prototype = {
		
	initialize: function() {
		$$('li.navHeader').each( function(ele) {
			ele.observe('mouseover', this.navHeaderMouseover );
			ele.observe('mouseout', this.navHeaderMouseout );
		}.bind(this) );

		$$('.gntlx').each( function(ele) {
			ele.observe('mouseover', this.gntlxMouseover );
			ele.observe('mouseout', this.gntlxMouseout );
		}.bind(this) );

		$$('.searchkeyForm').each( function(ele) {
			ele.observe('focus', this.searchKeyFocus );
		}.bind(this) );
		
		$('closeButton').observe('click', this.searchKeyHide );
		
	},

	getChildren: function( obj ) {
		if( obj.children ) {
			return obj.children;
		}
		
		var children = [];
		
		for( var i=0; i<obj.childNodes.length; i++ ) {
			if( obj.childNodes[i].nodeType == 1) {
				children[ children.length ] =  obj.childNodes[i];
			}
		}
		
		return children;
	},
	
	navHeaderMouseover: function() {
		var children = GettyMenuClass.prototype.getChildren( this );
		
		if( children.length >= 1 ) {
			var className = children[0].className;
			if(className.substr(className.length-3) != '_on' ) {
				children[0].className += '_over';
			}
		}
		if( children.length >= 2 ) {
			children[1].style.display = 'block';
		}
	},

	navHeaderMouseout: function() {
		var children = GettyMenuClass.prototype.getChildren( this );
		
		if( children.length >= 1 ) {
			var className = children[0].className;
			if(className.substr(className.length-5) == '_over' ) {
				children[0].className = className.substr(0, className.length-5);
			}
		}
		if( children.length >= 2 && children[1].style.display == 'block' ) {
			children[1].style.display = 'none';
		}
	},

	gntlxMouseover: function() {
		var _menuHeader=false;
		var _menuBody=false;
		
		var children = GettyMenuClass.prototype.getChildren( this );
		
		for( var i=0; i<children.length; i++) {
			var child = children[i];
			if(Element.hasClassName(child, 'smbOC') ) {
				child.style.display = 'block';
				_menuBody	= child;
			}
			else if(Element.hasClassName(child, 'smhOC')) {
				child.style.display = 'block';
				_menuHeader = child;
			}
		};
		
		GettyMenu._ensurePosition(_menuHeader, _menuBody);
	},

	gntlxMouseout: function() {
		var children = GettyMenuClass.prototype.getChildren( this );
		
		for( var i=0; i<children.length; i++) {
			var child = children[i];
			if(Element.hasClassName(child, 'smhOC') || Element.hasClassName(child, 'smbOC') ) {
				child.style.display = 'none';
			}
		};
	},


	_ensurePosition: function (_menuHeader, _menuBody) {
	    var headerWidth = _menuHeader.getElementsByTagName("TD")[4].offsetWidth;
	    var bodyWidth = _menuBody.getElementsByTagName("TD")[4].offsetWidth;
	    if ((bodyWidth - headerWidth) < 10) {
	        bodyWidth = headerWidth + 10;
	        GettyMenu._setBodyWidth(_menuBody, bodyWidth);
	    }
	    if (bodyWidth > headerWidth) {
	        var bodyLeftMargin = GettyMenu._calculateBodyLeftMargin(_menuBody,headerWidth);
	        _menuBody.style.marginLeft = bodyLeftMargin + "px";
	    }
	    //this._positioned = true;
	},
	
	
	_calculateBodyLeftMargin: function (_menuBody, widthOfHeader) {
	    var contentWidth = _menuBody.getElementsByTagName("TD")[4].offsetWidth;
	    var rightCellWidth = _menuBody.getElementsByTagName("TD")[3].offsetWidth;
	    return 0 - (contentWidth + rightCellWidth - widthOfHeader);
	},
	
	
	_setBodyWidth: function (_menuBody, width) {
	    var contentCell = _menuBody.getElementsByTagName("TD")[4];
	    contentCell.style.width = width + "px";
	    contentCell.getElementsByTagName("DIV")[0].style.width = width + "px";
	},
	
	
	searchKeyFocus: function() {
		GettyMenu._addShadow($('flyout'));
		$('flyout').style.display = 'block';
	},
	
	
	searchKeyHide: function() {
		$('flyout').style.display = 'none';
	},

	
	_addShadow: function ( element ) {
	    if (element.shadow) {
	        return;
	    }
	    var content = document.getElementById("flyoutContent");
	    var lb = document.createElement("div");
	    lb.className = "lb";
	    element.appendChild(lb);
	    rb = document.createElement("div");
	    rb.className = "rb";
	    lb.appendChild(rb);
	    element.replaceChild(lb, content);
	    rb.appendChild(content);
	    var bcl = document.createElement("div");
	    bcl.className = "bcl";
	    element.appendChild(bcl);
	    var bb = document.createElement("div");
	    bb.className = "bb";
	    element.appendChild(bb);
	    var bcr = document.createElement("div");
	    bcr.className = "bcr";
	    element.appendChild(bcr);
	    element.shadow = true;
	}
};


var GettyMenu = false;
Event.observe(window, 'load', function() {
	GettyMenu = new GettyMenuClass();
});

function toggleCreativeEditorial(type) {
	if(type == 'creative') {
		$('cbxEditorial').checked = false;
		$('creativeGroup').style.display = 'block';
        $('editorialGroup').style.display = 'none';
	} else if (type == 'editorial') {
		$('cbxCreative').checked = false;
		$('editorialGroup').style.display = 'block';
        $('creativeGroup').style.display = 'none';
	}
}

if($('cbxEditorial').checked == true) {
	$('creativeGroup').style.display = 'none';
	$('editorialGroup').style.display = 'block';
}
