﻿function MP3Search(query) {
	query = Trim(query);
    if (query.length > 0) {
        document.location = "/music/MP3/search.aspx?q=" + escape(query);
    }
}
function MP3SearchAdv(artist, title, album) {
	artist = Trim(artist);
	title = Trim(title);
	album = Trim(album);
    if ((artist.length > 0) || (title.length > 0) || (album.length > 0)) {
        var qs = new String();
        if (artist.length > 0) {
			if (qs.length > 0) { qs += "&"; }
            qs += "artist=" + escape(artist)
        }
        if (title.length > 0) {
			if (qs.length > 0) { qs += "&"; }
            qs += "title=" + escape(title)
        }
        if (album.length > 0) {
			if (qs.length > 0) { qs += "&"; }
            qs += "album=" + escape(album)
        }
        document.location = "/music/MP3/search.aspx?" + qs;
    }
}


function MP3SearchFromTab(id) {
    if (id.length > 0) {
        var otxt = GetElementById(id);
        if (otxt != null) {
            var sValue = new String();
            sValue = otxt.value;
            MP3Search(sValue);
        }
    }
}

function CaptureMP3SearchTabKey(e, txt) {
    if (e == null) { e = window.event; }
    
    var keyCode = new Number();
    if (e != null) {
        keyCode = e.keyCode ? e.keyCode : e.charCode ? e.charCode : e.which;
    }
    if (keyCode == 13) {
        // Perform Search
        MP3SearchFromTab(txt.id);
        
        // Cancel keystroke
        if (e != null) {
	        if (e.cancelBubble) { e.cancelBubble = true; }
	        if (e.returnValue) { e.returnValue = false; }
            if (e.preventDefault) { e.preventDefault(); }
            if (e.keyCode) { e.keyCode = 0; }
        }
        return false;
    }
}

function MP3SearchResultsGotoPage(page) {
    var nPage = new Number(1);
    if ((page != null) && (isNaN(page) == false)) {
        nPage = Math.max(nPage, page);
    }
    var sQueryString = new String();
    var sLocation = new String();
    sLocation = document.location.toString();
    if (sLocation.indexOf("?") >= 0) { sLocation = sLocation.substr(0, sLocation.indexOf("?")); }
    sQueryString = GetQueryString();
    sQueryString = RebuildQueryString(sQueryString, "page", nPage);
    document.location = sLocation + "?" + sQueryString;
}

function MP3EditPlaylist(playlistId) {
	document.location = "MyPlaylists.aspx?playlistId=" + playListId;
}





function MP3BeginAnimateAAThumbnail(evt, thumbnailImg, fullSizeSource, transition) {
	if (document.images) {
		if ((evt == "undefined") || (evt == null)) { evt = window.event; }
		
		var id = new String();
		id = "AlbumArtViewer_" + fullSizeSource.toString();
		
		var divContainer = GetElementById(id);
		if (divContainer == null) {
			
			divContainer = document.createElement("div");
			divContainer.id = id;
			divContainer.style.filter = "progid:DXImageTransform.Microsoft.GradientWipe(duration=1.0;gradientSize=1.0;wipeStyle=0;motion='forward')";
			divContainer.style.backgroundColor = "#E0E0E0";
			divContainer.style.width = "300px";
			divContainer.style.height = "300px";
			
			document.body.appendChild(divContainer);
	
			var imgFullSize = new Image();
			imgFullSize.onmouseover = function() { MP3HideAAContainerSuspendTimeout(divContainer.id); }
			imgFullSize.onmousemove = function() { MP3HideAAContainerSuspendTimeout(divContainer.id); }
			imgFullSize.onmouseout = function() { MP3HideAAContainerSetTimeout(divContainer.id); }
			imgFullSize.onload = function() { MP3ResizeAAContainer(imgFullSize, divContainer); }
			imgFullSize.onerror = function() { imgFullSize.src = thumbnailImg.src; }
			imgFullSize.src = fullSizeSource;
			divContainer.appendChild(imgFullSize);
		}

		MP3HideAAContainers(id);
		
		divContainer.style.position = "absolute";
		if (evt != null) {
			divContainer.style.top = (GetTop(thumbnailImg) + evt.offsetY + 8) + "px";
			divContainer.style.left = (GetLeft(thumbnailImg) + evt.offsetX + 8) + "px";
		} else {
			divContainer.style.top = GetTop(thumbnailImg) + "px";
			divContainer.style.left = (GetLeft(thumbnailImg) + thumbnailImg.offsetWidth) + "px";
		}
		
		if ((transition == true) || (divContainer.style.display == "none")) {
			if (divContainer.filters != undefined) {
				if (divContainer.filters[0].status != 2) {
					divContainer.style.display = "none";
					divContainer.filters[0].Apply();
					divContainer.style.display = "";
					divContainer.filters[0].Play();
				}
			} else {
				divContainer.style.display = "";
			}
		} else {
			divContainer.style.display = "";
		}
		
		
		MP3HideAAContainerSetTimeout(divContainer.id);
		
	}
}

function MP3ResizeAAContainer(fullsizeImg, container) {
	container.style.width = fullsizeImg.offsetWidth;
	container.style.height = fullsizeImg.offsetHeight;
}

var g_MP3AAContainerCloseTimeouts = null;
function MP3EnsureAAContainerCloseTimeouts() {
	if (g_MP3AAContainerCloseTimeouts == null) {
		g_MP3AAContainerCloseTimeouts = new Array();
	}
	return g_MP3AAContainerCloseTimeouts;
}
function MP3ContainerCloseTimeoutIndex(containerId) {
	var timeouts = new Array();
	timeouts = MP3EnsureAAContainerCloseTimeouts();
	for (var i=0;i<timeouts.length;i++) {
		var t = timeouts[i];
		if ((t != null) && (t.ContainerId == containerId)) {
			return i
		}
	}
	return -1;
}


function MP3HideAAContainerSetTimeout(containerId) {
	var timeouts = new Array();
	timeouts = MP3EnsureAAContainerCloseTimeouts();
	var index = MP3ContainerCloseTimeoutIndex(containerId);
	if (index >= 0) {
		if ((timeouts[index] != null)&&(timeouts[index].Timeout != null)&&(timeouts[index].Timeout > 0)) {
			window.clearTimeout(timeouts[index].Timeout);
			timeouts[index].Timeout = null;
		}
	} else {
		index = timeouts.length;
	}
	timeouts[index] = new MP3HideAAContainerCloseTimeout(containerId, window.setTimeout(function() { MP3HideAAContainer(containerId); }, 5000));
}


function MP3HideAAContainerSuspendTimeout(containerId) {
	var timeouts = new Array();
	timeouts = MP3EnsureAAContainerCloseTimeouts();
	var index = MP3ContainerCloseTimeoutIndex(containerId);
	if (index >= 0) {
		if ((timeouts[index] != null)&&(timeouts[index].Timeout != null)&&(timeouts[index].Timeout > 0)) {
			window.clearTimeout(timeouts[index].Timeout);
			timeouts[index].Timeout = null;
		}
	}
}

function MP3HideAAContainer(containerId) {
	var timeouts = new Array();
	timeouts = MP3EnsureAAContainerCloseTimeouts();
	var index = MP3ContainerCloseTimeoutIndex(containerId);
	if (index >= 0) {
		var container = GetElementById(containerId);
		if ((container != null) && (container.style.display != "none")) {
			if (container.filters != undefined) {
				container.filters[0].Apply();
				container.style.display = "none";
				container.filters[0].Play();
			} else {
				container.style.display = "none";
			}
		}
		timeouts[index] = null;
		if (timeouts.splice != undefined) {
			timeouts.splice(index, 1);
		}
	}
}

function MP3HideAAContainers(containerId) {
	var timeouts = new Array();
	timeouts = MP3EnsureAAContainerCloseTimeouts();
	
	for (var i=timeouts.length-1;i>=0;i--) {
		var t = timeouts[i];
		if ((t != null) && (t.ContainerId != containerId)) {
			MP3HideAAContainer(t.ContainerId);
		}
	}
}

function MP3HideAAContainerCloseTimeout(containerId, timeout) {
	this.ContainerId = containerId;
	this.Timeout = timeout;
	return this;
}

function MP3RotateArtistPhotoArray(interval) {
    if ((g_mp3ArtistPhotoArray != undefined) && (g_mp3ArtistPhotoArray != null)) {
        window.setInterval("MP3ArtistPhotoArraySetIndexImage()", interval);
    }
}

function MP3ArtistPhotoArraySetIndexImage() {
    if ((g_mp3ArtistPhotoArray != undefined) && (g_mp3ArtistPhotoArray != null)) {
        var imgPhotoArray = GetElementById("ArtistPhotoArray");
        if (imgPhotoArray != null) {
            g_mp3ArtistPhotoArrayIndex += 1;
            if (g_mp3ArtistPhotoArrayIndex >= g_mp3ArtistPhotoArray.length) { g_mp3ArtistPhotoArrayIndex = 0; }
            imgPhotoArray.src = g_mp3ArtistPhotoArray[g_mp3ArtistPhotoArrayIndex];
        }
    }
}