/**
 * Copyright (c) 2000-2005 Liferay, LLC. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

window.onscroll= moveNav;

var iconList;
var maxZoom = 24;
var iconHeight = 50;
var navTitleWidth = 100;
var navIconTitle;
var navWidth;
var navArrowPos = 0;
var navTimer;


function aquaClosePortlet(layoutId, portletId, columnId) {
	if (closePortlet(layoutId, portletId, columnId, true)) {
		trashPortletFill();
		playThemeSound("soundTrash");
	}
}

function aquaMaximizePortlet(layoutId, portletId, columnId) {
	if (minimizePortlet(layoutId, portletId, columnId)) {
		playThemeSound("soundMaximize");
	}
}

function aquaMinimizePortlet(layoutId, portletId, columnId) {
	if (minimizePortlet(layoutId, portletId, columnId)) {
		playThemeSound("soundMinimize");
	}
}

function buildIconList () {
	var i;
	var navPadding = 15;

	iconList = document.getElementById("layout-nav-tabs-box").getElementsByTagName("img");
	document.getElementById("layout-nav-container").style.height = iconHeight + maxZoom;
	navWidth = (iconList.length * iconHeight) + navPadding;
	document.getElementById("layout-nav-decoration").style.width = navWidth + "px";

	for (i=0; i<iconList.length; i++) {
		iconList[i].border = 0;
		iconList[i].style.height = iconHeight + "px";
		iconList[i].style.width = iconHeight + "px";

		if (i == 0) {
			iconList[i].next = iconList[i + 1];
		}
		else if ((i + 1) >= iconList.length) {
			iconList[i].previous = iconList[i - 1];
		}
		else {
			iconList[i].next = iconList[i + 1];
			iconList[i].previous = iconList[i - 1];
		}
	}
}

function cancelResetMenu() {
	clearTimeout(navTimer);
}

function curDate() {
	var mydate=new Date()
	var year=mydate.getYear()
	if (year < 1000) year+=1900
	var day=mydate.getDay()
	var month=mydate.getMonth()
	var daym=mydate.getDate()
	if (daym<10) daym="0"+daym
	var dayarray=new Array("Sun","Mon","Tues","Wed","Thur","Fri","Sat")
	var montharray=new Array("Jan","Feb","Mar","Apr","May","June","Jul","Aug","Sept","Oct","Nov","Dec")
	//var dispDate = dayarray[day]+", "+montharray[month]+" "+daym+", "+year;
	var dispDate = dayarray[day];
	return dispDate;
}

function curTime() {
	var digital = new Date();
	var hours = digital.getHours();
	var minutes = digital.getMinutes();
	var amOrPm = "AM";
	if (hours > 11) amOrPm = "PM";
	if (hours > 12) hours = hours - 12;
	if (hours == 0) hours = 12;
	if (minutes <= 9) minutes = "0" + minutes;
	var dispTime = curDate() + "&nbsp;&nbsp;" + hours + ":" + minutes + " " + amOrPm;
	document.getElementById("layout-bar-clock").innerHTML = dispTime;
}


function displayMenu(elem1) {
	if (document.getElementById) {
		var item;

		if (item = document.getElementById(elem1)) {
			if (item.style.display == 'block') {
				hidePopUp(elem1)
			}
			else {
				showPopUp(elem1);
			}
		}
	}
}

function initMenu()
{
	buildIconList();
	resizeClient();
	curTime();
	setInterval("curTime()", 60000);
	trashBin = document.getElementById("layout-icon-trash");
}

function moveNav() {
	var oMenu = document.getElementById("layout-nav-container");

	if (is_safari) {
		oMenu.style.top = (self.pageYOffset + self.innerHeight - iconHeight - maxZoom) + "px";
	}
	else {
		oMenu.style.top = (document.body.scrollTop + document.body.clientHeight - iconHeight - maxZoom) + "px";
	}

	oMenu.style.width = document.body.offsetWidth - 20 + "px";
	document.getElementById("layout-nav-tabs-box").style.width = document.body.offsetWidth - 20 + "px";

	if (is_mozilla) {
		setTimeout("moveNav()", 500);
	}

	navIconTitle = document.getElementById("layout-nav-icon-title");
 
	// Move select arrow
	var iconSelected = document.getElementById("layout-icon-selected");
	if (iconSelected != null) {
		var navArrow = document.getElementById("layout-nav-arrow");
		nwOffset = Coordinates.northwestOffset(iconSelected, true);
		navArrowPos = nwOffset.x + iconSelected.offsetHeight/2 - 4;
		navArrow.style.left = navArrowPos + "px";
		navArrow.style.display = "block";
	}


}

function resetMenu() {
	var i;
	document.getElementById("layout-nav-icon-title").innerHTML = "";

	navTimer = setTimeout("shrinkMenu()", 1);
}

function resizeClient() {
	moveNav();
	changeBackground("jpg");
}

function resizeIcon(obj, event) {
	var midPoint = obj.offsetHeight/2;
	var tempWidth = obj.offsetHeight;
	var tempNavWidth = navWidth;
	var newHeight = 0;
	var nextHeight = 0;
	var prevHeight = 0;
	var midDiff = 0;
	var heightDiff = 0;
	var nwOffset = Coordinates.northwestOffset(obj, true);

	getMousePos(event);
	var tempX = mousePos.x - nwOffset.x;

	if (tempX < 0){tempX = 0;}

	// Previous icon size
	if (obj.previous != null) {
		heightDiff = maxZoom - (tempX * maxZoom)/tempWidth;
		prevHeight = iconHeight + heightDiff;
		tempNavWidth += heightDiff;
		obj.previous.style.height = prevHeight + "px";
		obj.previous.style.width = prevHeight + "px";
	}

	// Next icon size
	if (obj.next != null) {
		heightDiff = (tempX * maxZoom)/tempWidth;
		nextHeight = iconHeight + heightDiff;
		tempNavWidth += heightDiff;
		obj.next.style.height = nextHeight + "px";
		obj.next.style.width = nextHeight + "px";
	}

	// Middle (active) icon size
	newHeight = iconHeight + maxZoom;
	tempNavWidth += maxZoom;
	obj.style.height = newHeight + "px";
	obj.style.width = newHeight + "px";

	nwOffset = Coordinates.northwestOffset(obj, true);
	navIconTitle.style.left = nwOffset.x - navTitleWidth/2 + "px";
	navIconTitle.style.top =  5 - maxZoom + "px";
	navIconTitle.style.width = newHeight + navTitleWidth + "px";
	navIconTitle.innerHTML = obj.alt;
	document.getElementById("layout-nav-decoration").style.width = tempNavWidth;

	// Move arrow
	var iconSelected = document.getElementById("layout-icon-selected");
	if (iconSelected != null) {
		var navArrow = document.getElementById("layout-nav-arrow");
		nwOffset = Coordinates.northwestOffset(iconSelected, true);
		navArrow.style.left = nwOffset.x + iconSelected.offsetHeight/2 - 4 + "px";
		navArrow.style.display = "block";
	}
}  

function shrinkMenu () {
	var i;
	var modified = false;
	var curHeight;
	var navObj = document.getElementById("layout-nav-decoration");
	var curWidth = parseInt(navObj.style.width);
	var arrowObj = document.getElementById("layout-nav-arrow");
	var arrowPos = parseInt(arrowObj.style.left);
	var iconContainer = document.getElementById("layout-nav-icon-container");

	for (i=0; i<iconList.length; i++) {
		curHeight = parseInt(iconList[i].style.height);
		if (curHeight > iconHeight) {
			iconList[i].style.height = curHeight - 1;
			iconList[i].style.width = curHeight - 1;
			modified = true;
		}
	}

/*
	if (curWidth > navWidth) {
		navObj.style.width = curWidth - 2;
		modified = true;
	}
*/
	navObj.style.width = iconContainer.offsetWidth + "px";

	if (!is_mozilla) {
		if (arrowPos > navArrowPos) {
			arrowObj.style.left = arrowPos - 1;
			modified = true;
		}
		else if (arrowPos < navArrowPos) {
			arrowObj.style.left = arrowPos + 1;
			modified = true;
		}
	}

	if (modified) {
		navTimer = setTimeout("shrinkMenu()", 1);
	}
	
}

function toggleAdd ()
{
	obj = document.getElementById("layout-bottom-container");

	if (obj.style.display == "block") {
		obj.style.display = "none";
		obj = document.getElementById("link-add-portlets").href = "#";
	}
	else {
		obj.style.display = "block";
		obj = document.getElementById("link-add-portlets").href = "#add-portlets-section";
	}
}

function toggleSearch () {
	searchField = document.getElementById("layout-search-field");
	if (searchField.style.display == "block") {
		searchField.style.display = "none";	
		document.getElementById("layout-search-icon").className = "layout-search-icon-off";
	}
	else {
		oIcon = document.getElementById("layout-search-icon");
		nwIcon = Coordinates.northwestOffset(oIcon, true);
		oSearchBar.style.left = (nwIcon.x - 265) + "px";
		oSearchBar.style.top = (nwIcon.y + 22) + "px";

		searchField.style.display = "block";	
		oIcon.className = "layout-search-icon-on";
	}
}

function trashPortlet (oPortlet, layoutId, columnId) {
	trashBin.northwest = Coordinates.northwestOffset( trashBin, true );
	trashBin.southeast = Coordinates.southeastOffset( trashBin, true );

	if (mousePos.inside(trashBin.northwest, trashBin.southeast)) {
		if (closePortlet(layoutId, oPortlet.portletId, columnId, true)) {
			trashPortletFill();
			playThemeSound("soundTrash");
			return true;
		}
		else {
			return false;
		}
	}
	else {
		playThemeSound("soundDrop");
	}
}

function playThemeSound(soundName) {
	var sound = eval("document."+soundName);
	if (!is_ie) return;

	try {
		sound.DoPlay();
	}
	catch (e) {
		sound.Play();
	}
}

