var isIE = false;var moviegallery;var moviegalleryXML;var sortCol = 0;function loadData() {	try {		loadMovieGallery("moviegallery.xml");	} catch(e) {		var msg = (typeof e == "string") ? e: ((e.message) ?e.message : "Unknown Error");		alert("Unable to get XML data: \n" + msg);	}}function loadMovieGallery(url) {    // branch for native XMLHttpRequest object	if (window.XMLHttpRequest) {        		moviegallery = new XMLHttpRequest();		moviegallery.onreadystatechange = processGalleryReqChange;		moviegallery.open("GET", url, true);		moviegallery.send(null);	// branch for IE/Windows ActiveX version	} else if (window.ActiveXObject) {		isIE = true;		//alert ("isIE");		moviegallery = new ActiveXObject("Microsoft.XMLHTTP");		if (moviegallery) {			moviegallery.onreadystatechange = processGalleryReqChange;			moviegallery.open("GET", url, true);			moviegallery.send();		}	}}/* when playlists xml is loaded, this is called */function processGalleryReqChange() {	//alert("state "+playlists.readyState);	if(moviegallery.readyState == 4 ) {		moviegalleryXML = moviegallery.responseXML;		writeGalleryInfo();		clearPlaylists();		buildPlaylists();		writeMediaForPlaylist(0); 	}}function writeGalleryInfo() {	var		galleryinfo;		//alert(getElementTextNS("", "galleryname", moviegalleryXML, 0));	document.getElementById("title").innerHTML = "<h1>"+getElementTextNS("", "galleryname", moviegalleryXML, 0)+"</h1>";}function clearPlaylists() {	var select = document.getElementById("playlistspopup");	while(select.length > 0) {	  select.remove(0);	}}function buildPlaylists() {	var items;	var select = document.getElementById("playlistspopup");	items = moviegalleryXML.getElementsByTagName("playlist");		//alert(items+ " , "+select);		for(var i=0; i<items.length; i++) {		appendToSelect(select, i, document.createTextNode(getElementTextNS("", "name", items[i], 0)));	}	document.getElementById("info").innerHTML = "";}function mediaItemFromID(mediaid) {	var mediaitem, medialist, i;		try {		mediaitem = moviegalleryXML.getElementById(mediaid);	} catch(e) {		var msg = (typeof e == "string") ? e: ((e.message) ?e.message : "Unknown Error");	//	alert("Unable to get XML data: \n" + msg);	}			// workaround for getElementById failing	if (mediaitem == null) {		try {			medialist = moviegalleryXML.getElementsByTagName("media");		} catch(e) {			return null		} 		for (i=0; i<medialist.length; i++) {			var aMediaID = getElementTextNS("", "mediaID", medialist[i], 0) 			if (aMediaID==mediaid) { 				mediaitem = medialist[i];			}	 	}	}	if (mediaitem != null)		return mediaitem;		return null;}function changePlaylist(evt) {     // equalize W3C/IE event models to get event object    evt = (evt) ? evt : ((window.event) ? window.event : null);    if (evt) {        // equalize W3C/IE models to get event target reference        var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);        if (elem) {        	//MM_goToURL('parent.thumbframe',elem.options[elem.selectedIndex].value);			//alert("vi bytte spellista " + elem.options[elem.selectedIndex].value);			writeMediaForPlaylist(elem.options[elem.selectedIndex].value);        }    }}function changeMovie(mediaid) {	var content, infocontent, mediaitem, filename, div, infostr, i, medialist;			mediaitem = mediaItemFromID(mediaid);	filename = getElementTextNS("", "location", mediaitem, 0);		if (isIE) {		content = "<embed height=\"376\" pluginspage=\"http://www.apple.com/quicktime/download/\" src=\""+filename+"\" type=\"video/quicktime\" kioskmode=\"true\" scale=\"aspect\" width=\"480\" controller=\"true\" autoplay=\"true\" bgcolor=\"#FFFFFF\" />\n";	} else {		content = "<object classid=\"clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\" codebase=\"http://www.apple.com/qtactivex/qtplugin.cab\" height=\"376\" width=\"480\">\n";		content += "<param name=\"src\" value=\""+filename+"\" />\n";		content += "<param name=\"autoplay\" value=\"true\" />\n";		content += "<param name=\"scale\" value=\"aspect\" />\n";		content += "<param name=\"controller\" value=\"true\" />\n";		content += "<param name=\"bgcolor\" value=\"#FFFFFF\" />\n";		content += "<param name=\"kioskmode\" value=\"true\" />\n";		content += "<embed height=\"376\" pluginspage=\"http://www.apple.com/quicktime/download/\" src=\""+filename+"\" type=\"video/quicktime\" kioskmode=\"true\" scale=\"aspect\" width=\"480\" controller=\"true\" autoplay=\"true\" bgcolor=\"#FFFFFF\" />\n";		content += "</object>\n";	}		div = document.getElementById("media");	div.innerHTML = content;		infocontent = "";		if ((infostr = getElementTextNS("", "name", mediaitem, 0)) != null) {		infocontent += "<h3>"+ infostr +"</h3>\n";	}	if ((infostr = getElementTextNS("", "copyright", mediaitem, 0)) != null) {		infocontent += "<h3>"+ infostr +"</h3>\n";	}	if ((infostr = getElementTextNS("", "shareDurationStr", mediaitem, 0)) != null) {		infocontent += "<h3>"+ infostr +"</h3>\n";	}	if ((infostr = getElementTextNS("", "shareSizeStr", mediaitem, 0)) != null) {		infocontent += "<h3>"+ infostr +"</h3>\n";	}	if ((infostr = getElementTextNS("", "info", mediaitem, 0)) != null) {		infocontent += "<h3>"+ infostr +"</h3>\n";	}	infocontent +="<br />\n";		//alert("info "+infocontent);	div = document.getElementById("info");	div.innerHTML = infocontent;}function CompareAlphaIgnore(a, b) {	strA = a[sortCol].firstChild.nodeValue.toLowerCase();	strB = b[sortCol].firstChild.nodeValue.toLowerCase();	//alert("strA:"+strA+", strB:"+strB);	if (strA < strB) { return -1; }	else {		if (strA > strB) { return 1; }		else { return 0; }	}}function CompareNumeric(a, b) {	numA = a[sortCol].firstChild.nodeValue;	numB = b[sortCol].firstChild.nodeValue;	//alert("numA:"+numA+", numB:"+numB);	if (isNaN(numA)) { return 0;}	else {		if (isNaN(numB)) { return 0; }		else { return numA - numB; }	}}function CompareDate(a, b) {	// this one works with date formats conforming to Javascript specifications, e.g. m/d/yyyy	strA = a[sortCol].firstChild.nodeValue.split("-");	strB = b[sortCol].firstChild.nodeValue.split("-");	//alert("datA:"+datA+", datB:"+datB);	datA = new Date(strA[0], strA[1], strA[2]);	datB = new Date(strB[0], strB[1], strB[2]);	if (datA < datB) { return -1; }	else {		if (datA > datB) { return 1; }		else { return 0; }	}} function mediaSort(mediaarray, sortkey) {	 //alert(sortkey);	 switch (sortkey) {			case "mediaID":				sortCol = 0;				mediaarray.sort(CompareNumeric);				break;			case "name":				sortCol = 1;				mediaarray.sort(CompareAlphaIgnore);				break;			case "duration":				sortCol = 6;				mediaarray.sort(CompareNumeric);				break;			case "size":				sortCol = 10;				mediaarray.sort(CompareNumeric);				break;			case "dateCreated":				sortCol = 12;				mediaarray.sort(CompareDate);				break;			case "dateAdded":				sortCol = 11;				mediaarray.sort(CompareDate);				break;			case "dateShared":				sortCol = 14;				mediaarray.sort(CompareDate);				break;			case "rating":				sortCol = 18;				mediaarray.sort(CompareNumeric);				break;			default:				// default sort on mediaID				sortCol = 0;				mediaarray.sort(CompareNumeric);	} }function writeMediaForPlaylist(index) {	var items, content, div, playlist, playlistname, playlistitems, i, a, mediaid, mediaitem, sortKey, sortAscending;			// get the selected playlist	try {      	playlist = moviegalleryXML.getElementsByTagName("playlist").item(index);    } catch (e) {      	alert(e);      	return false;    }	// get sort info	sortKey = getElementTextNS("", "sortKey", playlist, 0);	sortAscending = getElementTextNS("", "sortAscending", playlist, 0);		// get this playlists items, ie the mediaid's of it's media	playlistitems = playlist.getElementsByTagName("items");		var mediaArray = new Array;		// iterate over this playlists media and put it into an array	for(var i=0; i<playlistitems.length; i++) {		var mediaElems;		mediaid = getElementTextNS("", "mediaid", playlistitems[i], 0);		mediaitem = mediaItemFromID(mediaid);		if (mediaitem != null) {			mediaElems = mediaitem.getElementsByTagName("*");			if (mediaElems != null) {				mediaArray.push(mediaElems);			}		}	}		//alert(mediaArray);		// sort the array with media according to it's sort key	mediaSort(mediaArray, sortKey);		//alert("sortAscending: "+sortAscending);		// ascending or descending	if (sortAscending != 1) {		mediaArray.reverse();	}		// start writing content from the sorted array	content = "<table id=\"t1\" border=\"0\" align=\"left\" cellpadding=\"0\" cellspacing=\"0\">\n";	content += "<tr>\n";	for(var a=0; a<mediaArray.length; a++) {		var mediaid = mediaArray[a][0].firstChild.nodeValue;		//alert(mediaid);		content += "<td width=\"120\" align=\"center\">\n";		content += "<a href=\"javascript:;\" onclick=\"changeMovie("+mediaid+")\">\n";		content += "<img src=\"thumbnails/"+mediaid+".jpg\" alt=\"\" width=\"110\" height=\"110\" />\n";		content += "</a>\n";		content += "</td>\n";	}	content += "</tr>\n";	content += "</table>\n";		// adjust the scroll layers maxX according to the number of elements in this playlist	dw_scrollObj.adjustMaxX('wn', (mediaArray.length * 120) - 480);	// scroll back to the beginning	dw_scrollObj.scrollTo('wn',0,0);	div = document.getElementById("lyr1");	div.innerHTML = content;			// set playlist name and count	content = "<h5>"+getElementTextNS("","name", playlist, 0)+"</h5>\n";	div = document.getElementById("headerleft");	div.innerHTML = content;	content = "<h5>"+mediaArray.length+" Movies</h5>\n";	div = document.getElementById("headerright");	div.innerHTML = content;}function tenLatest() {	var	medialist, mediaid, mediaitem, numAdded, content, div;			numAdded = 0;		medialist = moviegalleryXML.getElementsByTagName("media");		var mediaArray = new Array;	for(var i=0; i<medialist.length; i++) {		var mediaElems;		mediaid = getElementTextNS("", "mediaID", medialist[i], 0);		mediaitem = mediaItemFromID(mediaid);		if (mediaitem != null) {			mediaElems = mediaitem.getElementsByTagName("*");			if (mediaElems != null && numAdded < 10) {				mediaArray.push(mediaElems);				numAdded++;			}		}	}	//alert(mediaArray);		// sort the array with media according to it's sort key	mediaSort(mediaArray, "dateShared");	mediaArray.reverse();		// start writing content from the sorted array	content = "<table id=\"t1\" border=\"0\" align=\"left\" cellpadding=\"0\" cellspacing=\"0\">\n";	content += "<tr>\n";	for(var a=0; a<mediaArray.length; a++) {		var mediaid = mediaArray[a][0].firstChild.nodeValue;		//alert(mediaid);		content += "<td width=\"120\" align=\"center\">\n";		content += "<a href=\"javascript:;\" onclick=\"changeMovie("+mediaid+")\">\n";		content += "<img src=\"thumbnails/"+mediaid+".jpg\" alt=\"\" width=\"110\" height=\"110\" />\n";		content += "</a>\n";		content += "</td>\n";	}	content += "</tr>\n";	content += "</table>\n";	//	initScrollLayer();	// scroll back to the beginning	dw_scrollObj.scrollTo('wn',0,0);	div = document.getElementById("lyr1");	div.innerHTML = content;			// set playlist name and count	content = "<h5>10 Latest</h5>\n";	div = document.getElementById("headerleft");	div.innerHTML = content;	content = "<h5>"+mediaArray.length+" Movies</h5>\n";	div = document.getElementById("headerright");	div.innerHTML = content;}// general purpose stufffunction getElementTextNS(prefix, local, parentElem, index) {	var result = "";	if (prefix && isIE) {		result = parentElem.getElementsByTagName(prefix + ":" + local)[index];	} else {		result = parentElem.getElementsByTagName(local)[index];	}	if(result) {	   if(result.childNodes.length > 1) {	      return result.childNodes[1].nodeValue;		} else {		  if (result. firstChild != null) {		  	return result.firstChild.nodeValue;		  } else {		  	return null;		  }		}	} else {		return null;	}}// general purpose, reusable appendfunction appendToSelect(select, value, content) {	var opt;	opt = document.createElement("option");	opt.value = value;	opt.appendChild(content);	select.appendChild(opt);}