var MiniChrome = function(){
	// The URL for the Disney logo
	this.disneyHomeUrl = "http://www.disney.ro/";
	// The menu items of the Mini Chrome are defined here. Each menu item is represented by an array in below list.
	// * First value of the array is the URL;
	// * second is the text for the link;
	// * third is a boolean which should be set to true if the menu item is to be emphasized (default is false, a normal menu item).
	this.menuItems = [
		["http://www.disney.ro/disney-filme/", "Filme"],
		["http://www.disney.ro/disney-channel/", "TV"],
		["http://www.disney.ro/disney-reviste/", "Carti si reviste"],
		["http://www.disney.ro/redirect/?redirectURL=http://www.disneylandparis.ro/", "Parcuri de distractii"],
		["http://www.disney.ro/magazin/zonadisney-jump.htm", "Magazin"],
		[]// Don't touch this. It is a safeguard against the dreaded "last comma of death in IE"
	];
	
	var secure = this.disneyHomeUrl.match(/^https/) ? "s" : "";
	this.disneyLogoUrl = "http" + secure + "://" + secure + "navbar.disneyinternational.com/minichrome2-disney-logo.png";
	
	this.version = "2.1";
	
	this.UseFlash = false;
	this.Width = undefined;
	this.BgColor = undefined;
	
	this.height = 35;
	this.width = 750;// Minimum width of the Mini Chrome
	this.bgColor = "#000";
	this.rolloverColor = "#515151";
	this.textColor = "#FFF";
	this.BORDER_WIDTH = 1;
	this.RIGHT_PADDING = 10;

	this._calculateWidth = function(){
		if (typeof window.barwidth != "undefined") {
			this.Width = parseInt(window.barwidth,10);
		}
		if (!isNaN(this.Width) && this.Width >= this.width) {
			this.width = this.Width;
		}
		this.width = parseInt(this.width,10);
		this.width -= (this.BORDER_WIDTH * 2);// Compensate box model for border-width
	};
	this._calculateColors = function(){
		if (typeof window.bgcolour != "undefined") {
			this.BgColor = bgcolour;
		}
		if (typeof window.rollovercolour != "undefined") {
			this.rolloverColor = rollovercolour;
		}
		if (typeof window.textcolour != "undefined") {
			this.textColor = textcolour;
		}
		if (typeof this.BgColor != "undefined") {
			this.bgColor = String(this.BgColor);
		}
		if (this.bgColor && this.bgColor.match(/^([0-9a-f]{3}|[0-9a-f]{6})$/i)) {
			this.bgColor = "#" + this.bgColor;
		}
		if (this.rolloverColor && this.rolloverColor.match(/^([0-9a-f]{3}|[0-9a-f]{6})$/i)) {
			this.rolloverColor = "#" + this.rolloverColor;
		}
		if (this.textColor && this.textColor.match(/^([0-9a-f]{3}|[0-9a-f]{6})$/i)) {
			this.textColor = "#" + this.textColor;
		}
	};
	this._createEnvironment = function(){
		this._iframe = document.createElement("iframe");
		this._iframe.id = "miniChrome2";
		this._iframe.style.border = "0";
		this._iframe.style.display = "block";
		this._iframe.setAttribute("frameBorder", "0");
		this._iframe.setAttribute("scrolling", "no");
		this._iframe.style.height = this.height + "px";
		this._iframe.style.margin = "0 auto";
		this._iframe.style.width = this.width + (this.BORDER_WIDTH * 2) + "px";
	};
	this._generateCSS = function(){
		this._css = "* {border:0; margin:0; padding:0}\
body {background:#000}\
div.mini-chrome-2 {\
	background: " + this.bgColor + ";\
	border: 1px solid " + this.rolloverColor + ";\
	font: bold 9px/1.2 sans-serif;\
	height: 100%;\
	margin: 0 auto;\
	padding-right: 10px;\
	text-transform: uppercase;\
	width: " + (this.width - this.RIGHT_PADDING) + "px;\
}\
div.mini-chrome-2 * {\
	border: 0;\
	margin: 0;\
	padding: 0;\
}\
div.mini-chrome-2 a {\
	background: none;\
	color: " + this.textColor + ";\
	display: block;\
	text-decoration: none;\
}\
div.mini-chrome-2 a img {\
	border: none;\
}\
div.mini-chrome-2 a.disney-logo {\
	display: inline;\
	float: left;\
	margin: 0 9px 0 10px;\
}\
div.mini-chrome-2 table.chrome-menu-items {\
	border: 0;\
	border-collapse: collapse;\
	border-spacing: 0;\
	font-size: 9px;\
	height: " + (this.height - (this.BORDER_WIDTH * 2) - (this.BORDER_WIDTH * 2)) + "px;\
	margin: 1px auto;\
	overflow: auto;\
}\
	div.mini-chrome-2.reduced table.chrome-menu-items {\
		font-size: 8px;\
		font-weight: normal;\
	}\
div.mini-chrome-2 table.chrome-menu-items td {\
	padding-left: 1px;\
	vertical-align: center;\
}\
div.mini-chrome-2 table.chrome-menu-items td span.divider {\
	border-left: 1px dotted " + this.rolloverColor + ";\
	float: left;\
	display: inline;\
	height: 1em;\
	margin-left: -1px;\
	position: absolute;\
	top: 13px;\
	width: 0;\
}\
div.mini-chrome-2 table.chrome-menu-items a {\
	background: none;\
	display: block;\
	height: " + (this.height - (this.BORDER_WIDTH * 2) - (this.BORDER_WIDTH * 2)) + "px;\
	padding: 0 8px;\
	text-align: center;\
}\
div.mini-chrome-2 table.chrome-menu-items a:hover {\
	background-color: " + this.rolloverColor + ";\
}\
div.mini-chrome-2 table.chrome-menu-items a span {\
	display: table-cell;\
	height: " + (this.height - (this.BORDER_WIDTH * 2) - (this.BORDER_WIDTH * 2)) + "px;\
	vertical-align: middle;\
}\
div.mini-chrome-2 table.chrome-menu-items a em {\
	display: block;\
	font-size: 12px;\
	font-style: normal;\
	line-height: 1;\
}\
	div.mini-chrome-2.reduced table.chrome-menu-items a em {\
		font-size: 9px;\
		font-weight: normal;\
	}";
		// Adjust for legacy MSIE
		if (navigator.userAgent.match(/MSIE 6.0|MSIE 7.0|MSIE 8.0/)) {
			this._css += "div.mini-chrome-2 table.chrome-menu-items td span.divider {margin-right:-1px}";
			if (navigator.userAgent.match(/MSIE 6.0/)) {
				this._css += "div.mini-chrome-2 a.disney-logo {cursor: pointer; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.disneyLogoUrl +"');margin-right:0}";
				this._css += "div.mini-chrome-2 a.disney-logo img {visibility: hidden}";
			}
			this._css += "div.mini-chrome-2 table.chrome-menu-items td a span {position: relative; top: expression((" + (this.height - 1) + " - offsetHeight) / 2 + 'px')}";
		}
	};
	this._generateJS = function(){
		this._js = "function ensureDisplayableMenuItems() {\
var wrapperElement = document.getElementById('wrapper');\
if (wrapperElement.clientHeight > 34) {/*we're out of horizontal space, let's reduce the font size*/\
	wrapperElement.className += \" reduced\";\
}\
}";
	};
	this._generateHTML = function(){
		this._html = '<div id="wrapper" class="disney-mini-chrome mini-chrome-2">\
<a href="' + encodeURI(this.disneyHomeUrl) + '" class="disney-logo" target="_parent"><img src="' + this.disneyLogoUrl + '" height="33" width="70" alt="Disney" title="Disney" /><\/a>\
<table cellspacing="0" class="chrome-menu-items"><tbody><tr>';
		for (var i = 0, ii = this.menuItems.length - 1; i < ii; i++) {
			this._html += '<td>' + (i == 0 ? '' : '<span class="divider"><\/span>')
				+ '<a href="' + encodeURI(this.menuItems[i][0]) + '" target="_parent" title="' + this.menuItems[i][1].replace(/<br\s*\/?>/g, " ") + '">'
				+ (this.menuItems[i][2] ? '<em>' : '')
				+ '<span>' + this.menuItems[i][1] + '<\/span>'
				+ (this.menuItems[i][2] ? '<\/em>' : '')
				+ '<\/a><\/td>';
		}
		this._html += '<\/tr><\/tbody><\/table><\/div>';
	};
	
	//
	// main
	//
	this.write = function(targetElementId){
		// Calculate the width of the whole Mini Chrome
		this._calculateWidth();
		
		// Calculate colors
		this._calculateColors();
		
		// Create separated environment in an iframe to avoid unwanted CSS inheritances from legacy pages
		this._createEnvironment();
		
		// Generate CSS and attach it to the head
		this._generateCSS();
		
		// Generate JS for reducing the font size if there isn't enough horizontal space
		this._generateJS();
		
		// Generate HTML code
		this._generateHTML();
		
		// Complete
		var targetElement = document.getElementById(targetElementId);
		if (!targetElement) {
			throw "MiniChrome cannot find target element with id of '" + targetElementId + "'.";
		}
		targetElement.innerHTML = "";
		targetElement.appendChild( this._iframe );
		var doc = this._iframe.contentWindow.document;
		doc.open();
		doc.write("<!DOCTYPE html>");
		doc.write("<html>");
		doc.write("<head>");
		doc.write("<meta charset=iso-8859-1>");
		doc.write("<title>MiniChrome2<\/title>");
		doc.write("<style>");
		doc.write(this._css);
		doc.write("<\/style>");
		doc.write("<\/head>");
		doc.write("<body onload=\"ensureDisplayableMenuItems()\">");
		doc.write(this._html);
		doc.write("<script>" + this._js + "<\/script>");
		doc.write("<\/body>");
		doc.write("<\/html>");
		doc.close();
	};
}

// Immediately detect if Mini Chrome should be invoked using the old/standard approach:
var chrome2ContainerId = "chrome2ContainerId20110616";
document.write('<div id="' + chrome2ContainerId + '"><\/div>');
var chromeContainer = document.getElementById(chrome2ContainerId);

window[chrome2ContainerId + "DetectorInterval"] = setInterval(function(){
	chromeContainer.counter = (chromeContainer.counter) ? chromeContainer.counter + 1 : 1;
	if ((window.chrome && window.chrome.disneyHomeUrl) || chromeContainer.counter == 3) {
		// giving up
		clearInterval(window[chrome2ContainerId + "DetectorInterval"]);
		if (!window.chrome || !window.chrome.disneyHomeUrl) {
			// Launch the MiniChrome the old/standard way
			var chrome = new MiniChrome();
			chrome.write(chrome2ContainerId);
		} else if (!window.barwidth) {
			// cleaning up for USA
			if (chromeContainer && chromeContainer.parentNode) {
				chromeContainer.parentNode.removeChild(chromeContainer);
			}
		}
	}
}, 1);
