﻿/* general */
function getEl(elName) {
	return document.getElementById(elName);
}

function ReduceCharCount(input, output, count) {
	if (input.value.length > count)
		input.value = input.value.substring(0, count);

	getEl(output).innerHTML = count-input.value.length + " characters left";
}

/* search */

function FilterSearch(startDate) {
	getEl(dateFromBox).value = startDate;
	getEl(dateToBox).value = todaysDate;
	getEl(searchButtonId).click();
}

/* view announcement */

function SwapTabs(newTab) {
	var isComments = newTab == "comments";
	getEl("tab-comments").className = isComments ? "curr" : "";
	getEl("tab-gifts").className = isComments ? "" : "curr";
	getEl("announce-comments").style.display = isComments ? "block" : "none";
	getEl("announce-gifts").style.display = isComments ? "none" : "block";
}

function DisplayPopup(tabName, newURL) {
	getEl("modal-popup-outer").style.display = "block";
	getEl("modal-popup").style.display = "block";
	getEl("announce-gifts-inner").style.display = "none";

	var sky = getEl("skyscraper-ad");
	if (sky) sky.style.display = "none";

	getEl("modal-iframe").src = newURL.replace("?head=true", "");

	getEl("popup-comments").className = "";
	getEl("popup-gifts").className = "";
	getEl("popup-photos").className = "";
	getEl("popup-videos").className = "";
	getEl("popup-audio").className = "";

	getEl("popup-" + tabName).className = "curr";
}

function ClosePopup() {
	getEl("modal-popup-outer").style.display = "none";
	getEl("modal-popup").style.display = "none";
	getEl("announce-gifts-inner").style.display = "block";
	getEl("modal-iframe").src = "/EmptyIFrame.htm";

	var sky = getEl("skyscraper-ad");
	if (sky) sky.style.display = "block";
}

function DisplayPhoto(imageURL) {
	getEl("photo-popup-outer").style.display = "block";
	getEl("photo-popup").style.display = "block";
	getEl("announce-gifts-inner").style.display = "none";
	getEl("photo-popup-display").src = imageURL;
}

function ClosePhotoPopup() {
	getEl("photo-popup-outer").style.display = "none";
	getEl("photo-popup").style.display = "none";
	getEl("announce-gifts-inner").style.display = "block";
	getEl("photo-popup-display").src = "/Images/pixel.gif";
}

function DisplayVideo(videoURL) {
	getEl("video-popup-outer").style.display = "block";
	getEl("video-popup").style.display = "block";
	getEl("announce-gifts-inner").style.display = "none";
	getEl("video-iframe").src = videoURL;
}

function CloseVideoPopup() {
	getEl("video-popup-outer").style.display = "none";
	getEl("video-popup").style.display = "none";
	getEl("announce-gifts-inner").style.display = "block";
	getEl("video-iframe").src = "/EmptyIFrame.htm";
}

function OpenPhotoStory(storyUrl) {
	var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
	var left = (screen.width / 2) - 400;
	var top = (screen.height / 2) - 355;
	if (is_chrome)
		window.open(storyUrl, "photostory", "width=800, height=674, top=" + (top-15) + ", left=" + left);
	else
		window.open(storyUrl, "photostory", "width=800, height=652, top=" + top + ", left=" + left);
}

/* audio player */

function SwitchAudio(divID, newFile) {
	var audioList = getEl("audio-player-list");
	var nodeLength = audioList.childNodes ? audioList.childNodes.length : audioList.children.length;
	for (var i = 0; i < nodeLength; i++) {
		var itm = audioList.childNodes ? audioList.childNodes[i] : audioList.children[i];
		if (itm.nodeType == 1)
			itm.className = itm.id == "ag"+divID ? "curr" : "";
	}
	getEl("audio-player-outer").innerHTML = '<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" id="audio-player" autostart="1" showcontrols="1" src="/Audio/Gifts/'+newFile+'">';
}

/* add photo and video */

function DisplayProgressBar(guid) {
	getEl("progress-bar-outer").style.display = "block";
	getEl("progress-bar").style.display = "block";
	getEl("progress-bar-iframe").src = "/UploadProgress.aspx?guid=" + guid;
}

/* browse category */

function SetPhotoScrollerWidth(newWidth) {
	getEl("photo-scroller-inner").style.width = newWidth;
}

function ScrollLeft() {
	if (getEl("photo-scroller-inner").offsetWidth > getEl("photo-scroller").offsetWidth) {
		var leftOff = getEl("photo-scroller-inner").style.marginLeft;
		if (leftOff != "0px" && leftOff != "") {
			var delay = 0;
			for (var i = -600; i <= 0; i += 20) {
				eval("window.setTimeout(\"document.getElementById('photo-scroller-inner').style.marginLeft='" + i + "px'\", " + delay + ")");
				delay += 20;
			}
		}
	}
}

function ScrollRight() {
	if (getEl("photo-scroller-inner").offsetWidth > getEl("photo-scroller").offsetWidth) {
		if (getEl("photo-scroller-inner").style.marginLeft != "-600px") {
			var delay = 0;
			for (var i = 0; i >= -600; i -= 20) {
				eval("window.setTimeout(\"document.getElementById('photo-scroller-inner').style.marginLeft='" + i + "px'\", " + delay + ")");
				delay += 20;
			}
		}
	}
}

/* my home */

function ViewMessage(messageID) {
	location.href = mfaUrl + "/myhome/messages/" + messageID
}

/* drop down menu code */

var timeOn = null;
var numMenus = 15;
var menus = new Array(numMenus);

function displayMenu(menuNo, sourceEl) {
    //hide anything else currently open
    hideAllMenus();

    //get the location of the item we are displaying
    var offset = getOffset(sourceEl)
    var x = offset.left;
    var y = offset.top + sourceEl.offsetHeight;

    //get the menu to display, only proceed if it exists
    var menuEl = document.getElementById("menu" + menuNo);
    if (menuEl) {
        moveEl(menuEl, x, y, sourceEl.offsetWidth);
    }
}

function moveEl(el, x, y, w) {
    el.style.left = x + "px";
    el.style.top = y + "px";
    //el.style.width = w + "px";
    el.style.visibility = "visible";
}

function hideAllMenus() {
    //loop through our menu array and hide everything
    for (var i = 0; i < menus.length; i++) {
        var el = document.getElementById(menus[i]);
        if (el) el.style.visibility = "hidden";
    }
}

function menuOut() {
    timeOn = setTimeout("hideAllMenus()", 1000);
}

function menuOver() {
    clearTimeout(timeOn);
}

function getOffset(el) {
    var _x = 0;
    var _y = 0;

    while (el && !isNaN(el.offsetLeft) && !isNaN(el.offsetTop)) {
        _x += el.offsetLeft;
        _y += el.offsetTop;
        el = el.offsetParent;
    }

    return { top: _y, left: _x };
}

menus[0] = "menu1";

/* photo story */

// elements array,
// current position in array,
// fade (bool),
// fade duration (ms),
// delay before next
function PerformTransition(els, pos, fade, dur, del) {
	//get elements to adjust
	var el1 = document.getElementById(els[pos]);
	var newPos = els.length > pos + 1 ? pos + 1 : 0;
	var el2 = document.getElementById(els[newPos]);

	//perform the fade if required
	if (fade) {
		//hide and display the new element
		SetOpacity(el2.id, 0);
		el2.style.display = "block";

		//perform the fade
		CrossFadeElements(el1, el2, dur);
	}
	else {
		//toggle visibility immediately
		el1.style.display = "none";
		el2.style.display = "block";
	}

	//create a string for the array definition
	var elsStr = "";
	for (var i = 0; i < els.length; i++)
		elsStr += "'" + els[i] + "',";
	elsStr = elsStr.substr(0, elsStr.length - 1);

	//set up the timeout for next time
	setTimeout("PerformTransition(new Array(" + elsStr + "), " + newPos + ", " + fade + ", " + dur + ", " + del + ")", del);
}

// element ref to fade out,
// element ref to fade in,
// fade duration (ms)
function CrossFadeElements(el1, el2, dur) {
	var timeOut = Math.round(dur / 10);

	//loop through, setting the timeouts
	for (var i = 1; i < 11; i++) {
		//have to eval before passing to setTimeout, feels messy
		eval("var timeOutFunc = function() { SetOpacity('" + el1.id + "', " + (10 - i) + "); SetOpacity('" + el2.id + "', " + i + "); } ");
		setTimeout(timeOutFunc, i * timeOut);
	}
}

// element to set
// opacity value
function SetOpacity(el, amt) {
	var elm = document.getElementById(el);
	//el.style.KhtmlOpacity = amt/10;
	elm.style.filter = "alpha(opacity=" + (amt * 10) + ")";
	elm.style.opacity = amt / 10;
}