//#EXPORT navFirst navPrev navNext navLast clickThumb clickMiniThumb selectTopic1 selectTopic2 selectColor
//#EXPORT removeFromFavorites selectFavorite setFavorites addToFavorites removeFromFavorites th si sc lists selectPageSet
//#EXPORT sitepicks includes pset i sci si t1 t2 pi ex color cpi inc theme favorites currTheme site_id

var lists;
var position = 1;
var count;
var currTheme;
var currColor;
var sc = new Object();
var th = new Object();
var cp = new Object();
var si = new Object();
var favoriteCount = 0;
var includeAll = false;

// Site Sources indexed by id
sc['stock'] = {n:'Stock Templates', autoinclude:true};
sc['custom'] = {n:'My Templates', autoinclude:false};

if (0 && document.getElementsByTagName )
{
	var head = document.getElementsByTagName('HEAD')[0]
	var scriptElement;
	var i = scripts.length;
	while ( i-- ) {
		scriptElement = document.createElement('script');
		scriptElement.type = 'text/javascript';
		scriptElement.src = scripts[i] + '?rand=' + (new Date().getTime());
		head.appendChild(scriptElement);
	}
}

function getLists(source, title1, title2, color)
{
	var t1list = new Object();
	t1list['all groups'] = 'all groups';
	var t2list = new Object();
	t2list['all topics'] = 'all topics';
	var sitepicks = new Array();
	var sitepickix = 0;
	var clist = new Object();
	clist['all colors'] = 'all colors';

	for (var key in th) {
		var theme = th[key];
		var colorFound = 0;
		if (color != 'all colors') {
			for (var cix in si[theme.si].color) {
				if (color.match(si[theme.si].color[cix]))
					colorFound = cix;
			}
		}
		if ((source == 'all sources' || theme.sci == source) &&
			(title1 == 'all groups' || theme.t1 == title1) &&
			(title2 == 'all topics' || theme.t2 == title2) &&
			(color  == 'all colors' || colorFound))
		{
			var sitepick = new Object();
			sitepick.theme = theme;
			sitepick.color = (color == 'all colors') ? si[theme.si].color[theme.pi] : color;
			theme.cpi = colorFound ?  colorFound : theme.pi;
			if (sc[theme.sci].autoinclude) 
				theme.inc = (typeof(sc[theme.sci].include) != 'undefined' && 
							 typeof(sc[theme.sci].include[theme.si + '-' + theme.i]) != 'undefined' &&
							 sc[theme.sci].include[theme.si + '-' + theme.i].match(/no/)) ? false : true;
			else 
				theme.inc = (typeof(sc[theme.sci].include) != 'undefined' && 
							 typeof(sc[theme.sci].include[theme.si + '-' + theme.i]) != 'undefined' &&
							 sc[theme.sci].include[theme.si + '-' + theme.i].match(/yes/)) ? true : false;
			theme.priority = (typeof(sc[theme.sci].include) != 'undefined' && 
						 typeof(sc[theme.sci].include[theme.si + '-' + theme.i]) != 'undefined' &&
						  sc[theme.sci].include[theme.si + '-' + theme.i].match(/([0-9]+)/)) ?
							RegExp.$1 :	5;
			if (includeAll || theme.inc) {
				sitepicks[sitepickix++] = sitepick;
				for (var cix in si[theme.si].color) {
					var tcolor = si[theme.si].color[cix];
					if (tcolor.match(/(.*?) and (.*)/)) {
						clist[RegExp.$1] = RegExp.$1;
						clist[RegExp.$2] = RegExp.$2;
					} else
						clist[tcolor] = clist[tcolor];
				}
				t1list[theme.t1] = theme.t1;
				t2list[theme.t2] = theme.t2;
			};
		}
	}
	var lists = new Object();
	lists.t1list = t1list;
	lists.t2list = t2list;
	lists.clist = clist;
	lists.sitepicks = sitepicks;
	sitepicks.sort(sitepickSort);
	return lists;
}
function sitepickSort (a, b)
{
	if (a.theme.priority < b.theme.priority) return -1;
	if (a.theme.priority > b.theme.priority) return 1;
	return 0;
}
function getDropDown(id, style, js, list, selected)
{
	var html ='';
	var sortedlist = new Array();
	var i = 0;
	var all;
	for (var ix in list) {
		if (ix.match(/^all /))
			all = ix;
		else 
			sortedlist[i++] = ix;
	}
	sortedlist.sort();
	html += '<select id="' + id + '" style="' + style + '" onchange="' + js +'">';
	html += '<option value="' + all + '"' + ((all == selected) ? ' selected' : '') + '>' + all +'</option>';
	for (var i = 0; i < sortedlist.length; ++i) {
		var ix = sortedlist[i];
		html += '<option value="' + ix + '"' + ((ix == selected) ? ' selected' : '') + '>' + ix +'</option>';
	}
	html += '</select>';
	return html;
}
function createThumbs_original()
{
	sitepicks = lists.sitepicks
	var thumbContainer = document.getElementById('thumbContainer');
	var width = 192 + miniThumbPadding;
	var height = 146 + miniThumbPadding;
	count = parseInt(getWidth(thumbContainer) / width) * parseInt(getHeight(thumbContainer) / height);
	var html = '';
	for (ix = position - 1; ix < (position + count - 1) && ix < sitepicks.length; ++ix) {
		var theme = sitepicks[ix].theme;
		var onclick = "clickMiniThumb(lists.sitepicks[" + ix + "].theme);";
		html += '<span style="width: ' + width + 'px; height: ' + height +'px;">'; 
		html += '<span style="width: ' + width + 'px; height: ' + height +'px;position: relative;">'; 
		html += getAlphaPng("mini_thumb_shadow.png", 198, 152, "absolute");
		html += '<span title="Click to View a Larger Image" class=cpanel_newsite_thumb ' +
			    'style="position: absolute;width: 192px; height:146px; overflow: hidden;' +
			    'margin-right: ' + miniThumbPadding + 'px; margin-bottom: ' + miniThumbPadding + 'px;">';
		html += '<img style="margin-left: -1px"border="0" src="' + sc[theme.sci].hostPrefix + 'mini_thumb_' + theme.si + '_' + theme.i + '_' + theme.cpi + '.' + theme.ex + '" onclick="' + onclick + '" />' ;
		html += '</span>';
		html += '</span>';
		html += '</span>';
	}
	thumbContainer.innerHTML = html;
	document.getElementById('ledgendContainer').innerHTML = position + ' of ' + sitepicks.length;
}
function createThumbs()
{
	sitepicks = lists.sitepicks
	var thumbContainer = document.getElementById('thumbContainer');
	var width = 192 + miniThumbPadding;
	var height = 146 + miniThumbPadding;
	var cols = parseInt(getWidth(thumbContainer) / width);
	var rows = parseInt(getHeight(thumbContainer) / height);
	count = rows * cols;
	var maxWidth = getWidth(thumbContainer) - width;
	var pxTop = 0;
	var pxLeft = 0;
	var html = '';
	for (ix = position - 1; ix < (position + count - 1) && ix < sitepicks.length; ++ix) {
		var theme = sitepicks[ix].theme;
		var onclick = "clickMiniThumb(lists.sitepicks[" + ix + "].theme);";
			html += getAlphaPng("mini_thumb_shadow.png", 198, 152, "absolute;left: " + pxLeft +"px; top: " + pxTop + "px;");
		html += '<span title="Click to View a Larger Image" class=cpanel_newsite_thumb ' +
			    'style="position: absolute;left: ' + pxLeft +'px; top: ' + pxTop + 'px;width: 192px; height:146px; overflow: hidden;' +
			    'margin-right: ' + miniThumbPadding + 'px; margin-bottom: ' + miniThumbPadding + 'px;">';
		html += '<img style="margin-left: -1px"border="0" src="' + sc[theme.sci].hostPrefix + 'mini_thumb_' + theme.si + '_' + theme.i + '_' + theme.cpi + '.' + theme.ex + '" onclick="' + onclick + '" />' ;
		html += '</span>';
		pxLeft += width;
		if (pxLeft > maxWidth) {
			pxLeft = 0;
			pxTop += height;
		}
	}
	thumbContainer.innerHTML = '<div style="position: relative">' + html + '</div>';
	document.getElementById('ledgendContainer').innerHTML = position + ' of ' + sitepicks.length;
}
function navFirst() {position = 1;createThumbs();}
function navPrev() {position -= count; position = position < 1 ? 1 : position;createThumbs();}
function navNext() {position += count; position = position > lists.sitepicks.length ? lists.sitepicks.length : position; createThumbs();}
function navLast() {position = parseInt((lists.sitepicks.length - 1)/ count) * count + 1;position < 1 ? 1 : position;createThumbs();}
function createPages(theme, currNavigation)
{
	var site = si[theme.si];
	var html ='';
	html += '<select id="pageSel" style="font-size: 11px;" onchange="selectPageSet(' + theme.si + ',this);return false;">';
	var first = true;
	for (var ix in site.pset) {
		html += '<option value="' + ix + '"' + 
				((currNavigation ? (ix == currNavigation) : first) ? ' selected' : '') + '>' +
				ix +'</option>';
		first = false;
	}
	document.getElementById('pageSelContainer').innerHTML = html;
	selectPageSet(theme.si, document.getElementById('pageSel'));
}
function selectPageSet(siteID, dropDown)
{
	var psetSelected = dropDown.value;
	var site = si[siteID];
	var pset = site.pset[dropDown.value];
	var page = new Object();
	for (var ix in site.pset) {
		var pset = site.pset[ix];
		for (var jx in pset) {
			pset[jx].match(/(.*?):(.*)/);
			if ((typeof(page[RegExp.$1]) == 'undefined') || (ix == psetSelected))
				page[RegExp.$1] = '<input style="margin: -1px" id=page_' + jx + ' type="checkbox" ' + (ix == psetSelected ? 'checked' : '') + ' />' + RegExp.$2 + '<br>';
		}
	}
	var html = '';
	for (var ix in page)
		html += page[ix];
	document.getElementById('pageListContainer').innerHTML = html;
}
function clickThumb()
{
	if (typeof(selectThumbEvent) != 'undefined')
		selectThumbEvent();
}
function clickMiniThumb(theme, selColor)
{
	currTheme = theme;
	if (typeof(selColor) == 'undefined') {
		selColor = theme.cpi;
		currColor = selColor;
	}
	currColor = selColor;
	if (typeof(selectMiniThumbEvent) != 'undefined')
			selectMiniThumbEvent(theme);
	var html = '';
	html += '<span style="left: 0px;width: 384px; height: 290px;position: relative;">'; 
	html += getAlphaPng("thumb_shadow.png", 390, 296, "absolute;left: 0px; top: 0px");
	html += '<span title="Click to Select Design" class=cpanel_newsite_thumb style="left: 0px;top: 0px;position: absolute;width: 384px; height:290px; overflow: hidden;">';
	html += '<img class=cpanel_newsite_screenshot style="margin-left: -2px;width: 384px; height:290px;" border="0" ' + 
			'src="' + sc[theme.sci].hostPrefix + 'thumb_' + theme.si + '_' + theme.i + '_' + selColor + '.' + theme.ex +
			'" onclick="clickThumb()" />';
	html += '</span>';
	html += '</span>';
	document.getElementById('screenshotContainer').innerHTML =  html;
	
	if(document.getElementById('colorContainer')) {
		var html = '';
		for (var color in si[theme.si].color) {
			html += '&nbsp;&nbsp;'
			if (color != selColor)
				html += '<a onclick="clickMiniThumb(currTheme, ' + "'" + color + "'" + 
						');return false;" href="">' + si[theme.si].color[color] + '</a>';
			else
				html += si[theme.si].color[color];
			html += '&nbsp;&nbsp;';
		}
		document.getElementById('colorContainer').innerHTML = html;
	}
	if(document.getElementById('nameContainer')) {
		document.getElementById('nameContainer').innerHTML = si[theme.si].n;
	}
}
function selectTopic1()
{
	lists = getLists('all sources',
		document.getElementById('topic').value,'all topics',
		document.getElementById('color').value);
	    var p =document.getElementById('topicPickContainer');
		p.innerHTML = 
			getDropDown('subtopic', 'font-size: 11px;width: ' + getWidth(p) +'px;', "selectTopic2()", lists.t2list, 'all colors');
		position = 1;
		createThumbs();
}
function selectTopic2()
{
	lists = getLists('all sources', 
		document.getElementById('topic').value,
		document.getElementById('subtopic').value,
		document.getElementById('color').value);
	position = 1;
	createThumbs();
}
function selectColor()
{
	selectTopic2();
}
function selectFavorite(i)
{
	var currDiv = document.getElementById('favorite_' + i);
	clickMiniThumb(currDiv.zen_theme, currDiv.zen_color);
}
function addToFavorites()
{
	if (favoriteCount < maxFavorites) {
		var favoriteDiv = document.getElementById('favorite_' + (++favoriteCount));
		favoriteDiv.zen_theme = currTheme;
		favoriteDiv.zen_color = currColor;
		if (favoriteDiv)
			favoriteDiv.style.display = 'block';
		document.getElementById('favorite_remove').style.display = 'block';
	}
}
function removeFromFavorites()
{
	if (favoriteCount == 0)
		return;
	var shiftDown = false;
	var prevDiv;
	for (var i = 1; i <= favoriteCount; ++i) {
		var currDiv = document.getElementById('favorite_' + i);
		if (shiftDown) {
			prevDiv.zen_theme = currDiv.zen_theme;
			prevDiv.zen_color = currDiv.zen_color;
		}
		if (currDiv.zen_theme.si == currTheme.si && currDiv.zen_theme.i == currTheme.i && currDiv.zen_color == currColor)
			shiftDown = true;
		prevDiv = currDiv;
	}
	currDiv.style.display = 'none';
	--favoriteCount;
	if (favoriteCount == 0)
		document.getElementById('favorite_remove').style.display = 'none';
}
function setFavorites(str) {
	var favorites = str.split(";");
	var bias = 0;
	for (var i = 1; i < maxFavorites; ++i) {
		if (favorites[i - 1]) {
			var element = favorites[i - 1].split('-');
			var theme = th[element[0] + '-' + element[1]];
			var color = element[2];
			if (typeof(theme) != 'undefined' && typeof(si[theme.si].color[color]) != 'undefined') {
				var currDiv = document.getElementById('favorite_' + (i - bias));
				currDiv.zen_theme = theme;
				currDiv.zen_color = color;
				currDiv.style.display = 'block';
				document.getElementById('favorite_remove').style.display = 'block';
				++favoriteCount;
			} else
				++bias;
		}
	}
}
function getFavorites()
{
	var str = '';
	var semiColon = '';
	for (var i = 1; i <= favoriteCount; ++i) {
		var currDiv = document.getElementById('favorite_' + i);
		if (currDiv.style.display == 'block') {
			str += semiColon;
			str += currDiv.zen_theme.si + '-';
			str += currDiv.zen_theme.i + '-';
			str += currDiv.zen_color;
			semiColon = ';';
		}
	}
	return str;
}
