﻿
var g_strMarqueeID = Number(1);
var g_strFilterID = "";
var g_strSort = "123asc";
var g_iPage = Number(1);
var g_iMaxPage = Number(0);
var g_iItemsPerPage = Number(9);
var g_iFilterMenuTimerID = Number(0);

function InitBrowser(strFilterID, strSort, iPage, iItemsPerPage) {
	g_strMarqueeID = DocumentHashValue("marquee",g_strMarqueeID);
	g_strFilterID = DocumentHashValue("filter", strFilterID);
	g_strSort = DocumentHashValue("sort", ((strSort=="")?"123asc":strSort));
	g_iPage = DocumentHashValue("page", ((Number(iPage)<1)?1:Number(iPage)));
	g_iItemsPerPage = ( Number(iItemsPerPage) < 3 ) ? 3 : Number(iItemsPerPage);
	
	SetBrowserFilter(g_strFilterID);
}

function UpdateBrowser() {

	// Update the page navigation
	UpdatePageNav();

	// The template for populating each item
	var strTemplate = document.getElementById('application-template').innerHTML;
	
	// How many items have we filled in
	var iApp = Number(0);
	var iAppSkip = Number((g_iPage-1)*g_iItemsPerPage);
	
	// What index are we at in our search
	var iPosition = Number(0);
	
	while ( iApp < (g_iItemsPerPage+iAppSkip) && iPosition < g_iAppCount ) {
		var iIndex = Number(0);
		switch (g_strSort) {
			case "ABCdesc":
				var iIndex = g_aSortAppsAlpha[(g_iAppCount-iPosition)-1];
				break;
			case "ABCasc":
				var iIndex = g_aSortAppsAlpha[iPosition];
				break;
			case "123desc":
				var iIndex = g_aSortAppsPopular[(g_iAppCount-iPosition)-1];
				break;
			case "123asc":
			default:
				var iIndex = g_aSortAppsPopular[iPosition];
				break;
		}
		
		// Did we find a match
		if ( g_aApps[iIndex][11] == g_strFilterID || g_strFilterID == "" ) {
			if ( iApp >= iAppSkip ) {
				document.getElementById('application-'+(iApp+1-iAppSkip)).innerHTML = PopulateApplicationTemplate(strTemplate,iIndex);
			}
			iApp++;
		}
		
		iPosition ++;
	}
	
	// Fill in the remaining items with blanks
	while ( iApp < (g_iItemsPerPage+iAppSkip) ) {
		document.getElementById('application-'+(iApp+1-iAppSkip)).innerHTML = "";
		iApp++;
	}
}

function PopulateApplicationTemplate(strTemplate, iAppIndex) {
	
	// Search and replace the template with the actual values
	var strNewTemplate = strTemplate
	
	// Title
	strNewTemplate = strNewTemplate.replace(/#TITLE#/g, g_aApps[iAppIndex][1]);
	
	// Description
	strNewTemplate = strNewTemplate.replace(/#DESCRIPTION#/g, g_aApps[iAppIndex][2]);
	
	// Thumbnail
	strNewTemplate = strNewTemplate.replace(/#THUMBNAIL#/g, g_aApps[iAppIndex][3]);

	// Url
	strNewTemplate = strNewTemplate.replace(/#URL#/g, "javascript:GoToApp('"+g_aApps[iAppIndex][4]+"','"+g_aApps[iAppIndex][0]+"');" );

	// Views
	strNewTemplate = strNewTemplate.replace(/#VIEWS#/g, g_aApps[iAppIndex][10]);

	// Icons
	strNewTemplate = strNewTemplate.replace(/#ICON-VIDEO#/g, ( (g_aApps[iAppIndex][5] == "1") ? "div-visible-inline" : "div-hidden" ) );
	strNewTemplate = strNewTemplate.replace(/#ICON-DIAGRAM#/g, ( (g_aApps[iAppIndex][6] == "1") ? "div-visible-inline" : "div-hidden" ) );
	strNewTemplate = strNewTemplate.replace(/#ICON-BOM#/g, ( (g_aApps[iAppIndex][7] == "1") ? "div-visible-inline" : "div-hidden" ) );
	strNewTemplate = strNewTemplate.replace(/#ICON-REFERENCE-DESIGN#/g, ( (g_aApps[iAppIndex][8] == "1") ? "div-visible-inline" : "div-hidden" ) );
	strNewTemplate = strNewTemplate.replace(/#ICON-DOWNLOAD#/g, ( (g_aApps[iAppIndex][9] == "1") ? "div-visible-inline" : "div-hidden" ) );

	return strNewTemplate;
}

function UpdatePageNav() {
	// Calculate the number of pages needed
	iAppCount = g_iAppCount
	if ( g_strFilterID != "" ) {
		for (var iFilter=0; iFilter<g_iFilterCount; iFilter++ ) {
			if ( g_aFilters[iFilter][0] == g_strFilterID ) {
				iAppCount = Number(g_aFilters[iFilter][2]);
			}
		}
	}
	g_iMaxPage = Math.ceil(iAppCount/g_iItemsPerPage);
	if ( g_iPage > g_iMaxPage ) {
		g_iPage = g_iMaxPage;
	}
	
	// Rewrite the source for the page navigation
	var strSource = String("");
	
	if ( g_iMaxPage > 1 ) {
		
		strSource += "Page: &nbsp;&nbsp;&nbsp;&nbsp; ";
		
		// Add the previous link if we're not on the first page
		if ( g_iPage > 1 ) {
			strSource += "<a href=\"javascript:SetBrowserPage('" + (Number(g_iPage)-1) + "');\" onclick=\"this.blur();\">&laquo; Previous</a> | ";
		} else {
			strSource += "&laquo; Previous | ";
		}
		
		// Add the numbers
		for (var iPage=1; iPage<=g_iMaxPage; iPage++) {
			if ( iPage > 1 ) {
				strSource += " | ";
			}
			if ( iPage == g_iPage ) {
				strSource += "<span>" + iPage + "</span>"
			} else {
				strSource += "<a href=\"javascript:SetBrowserPage('" + iPage + "');\" onclick=\"this.blur();\">" + iPage + "</a>";
			}
		}
		
		// Add the next link if we're not on the last page
		if ( g_iPage < g_iMaxPage ) {
			strSource += " | <a href=\"javascript:SetBrowserPage('" + (Number(g_iPage)+1) + "');\" onclick=\"this.blur();\">Next &raquo;</a>";
		} else {
			strSource += " | Next &raquo;";
		}
	}
	
	// Update the document	
	document.getElementById('application-browser-bottom-pages').innerHTML = strSource;
}

// Set the filter and redisplay the applications
function SetBrowserFilter(strFilterID) {
	// Remember the new filter id
	g_strFilterID = strFilterID;	

	// Hide the filter menu
	FilterMenuHide()
	
	// Update the filter tab
	var strFilterName = String("All Applications");
	var iAppCount = g_iAppCount;
	
	if ( g_strFilterID != "" ) {
		for (var iFilter=0; iFilter<g_iFilterCount; iFilter++ ) {
			if ( g_aFilters[iFilter][0] == g_strFilterID ) {
				strFilterName = String(g_aFilters[iFilter][1]);
				iAppCount = Number(g_aFilters[iFilter][2]);
			}
		}
	}
	strSource = String(strFilterName + " <em>(" + iAppCount + ")</em>");
	document.getElementById('filter-button').innerHTML = strSource;
	
	// Update the filter in the hash
	UpdateHash();
	
	// Update the browser
	UpdateBrowser();
}

// Change the page and redisplay the applications
function SetBrowserPage(iPage) {
	g_iPage = Number(iPage);
	UpdateHash();
	UpdateBrowser();
}

function SetBrowserSort(strSort) {
	g_strSort = strSort;
	g_iPage = 1;
	
	// Update the sort UI
	document.getElementById('application-browser-sort-123asc').style.display = ( ( strSort == "123asc" ) ? 'inline' : 'none' );
	document.getElementById('application-browser-sort-123desc').style.display = ( ( strSort == "123desc" ) ? 'inline' : 'none' );
	document.getElementById('application-browser-sort-123none').style.display = ( ( strSort == "ABCasc" || strSort == "ABCdesc" ) ? 'inline' : 'none' );

	document.getElementById('application-browser-sort-ABCasc').style.display = ( ( strSort == "ABCasc" ) ? 'inline' : 'none' );
	document.getElementById('application-browser-sort-ABCdesc').style.display = ( ( strSort == "ABCdesc" ) ? 'inline' : 'none' );
	document.getElementById('application-browser-sort-ABCnone').style.display = ( ( strSort == "123asc" || strSort == "123desc" ) ? 'inline' : 'none' );

	UpdateHash();
	UpdateBrowser();
}

function FilterMenuOver( ) {
	// Show the menu
	document.getElementById('application-browser-filter-menu').style.display = 'block';
	// Clear the current timer, if any
	clearTimeout(g_iFilterMenuTimerID);
	g_iFilterMenuTimerID = 0;
}

function FilterMenuOut( ) {
	// Clear the current timer, if any
	clearTimeout(g_iFilterMenuTimerID);
	// Set a new timer to hide the menu
	g_iFilterMenuTimerID = setTimeout('FilterMenuHide();', 500);
}

function FilterMenuHide( ) {
	document.getElementById('application-browser-filter-menu').style.display = 'none';
}

function GoToApp(strUrl, strID) {
	strUrl += "?id=" + strID + "&filter=" + g_strFilterID + "&sort=" + g_strSort + "&page=" + g_iPage;
	document.location = strUrl;
}