var baseURL = "/";
var xmlHttp;
var currVid;
var newVid;
var start = 0;
var videoListCount = 0;
var maxVideoListItems = 8;
var searchStr = "";
var sortType = "default";
var tradegothic = {src: baseURL + 'swf/tradegothic.swf'};


function updateFonts() {
	sIFR.fitExactly = true;
	sIFR.activate(tradegothic);
	sIFR.replace(tradegothic, {
	selector: 'h1, h2'
	 ,css: [
      '.sIFR-root {color: #826E43; font-weight: bold; background-color: #000000; }'
	],
	ratios: [8,1.41,10,1.33,14,1.31,16,1.26,20,1.27,24,1.26,25,1.24,26,1.25,35,1.24,39,1.23,40,1.24,49,1.23,74,1.22,75,1.21,81,1.22,94,1.21,95,1.22,1.21],
	tuneHeight: -8,
	offsetTop: -3
	});
}

function highlightScrollButton(dir) {
	
	if (dir == "prev" && start > 0) {
		btnPrevImg = document.getElementById('mediaListScrollPrevImg');
		if (btnPrevImg) {
			btnPrevImg.src = baseURL + "images/zy_media_arrow_prev_on.gif";
		}
	}
	else if (dir == "next" && start < videoListCount - maxVideoListItems) {
		btnNextImg = document.getElementById('mediaListScrollNextImg');
		if (btnNextImg) {
			btnNextImg.src = baseURL + "images/zy_media_arrow_next_on.gif";
		}
	}
}

function lowlightScrollButton(dir) {
	//alert(dir);
	if (dir == "prev") {
		btnPrevImg = document.getElementById('mediaListScrollPrevImg');
		if (btnPrevImg) btnPrevImg.src = baseURL + "images/zy_media_arrow_prev_off.gif";
	}
	else if (dir == "next") {
		btnNextImg = document.getElementById('mediaListScrollNextImg');
		if (btnNextImg) btnNextImg.src = baseURL + "images/zy_media_arrow_next_off.gif";
	}
}


function getMediaList() {
	var url = baseURL + 'media/media_actions.php?sort=' + sortType + '&start=' + start + '&video=' + currVid + '&search=' + escape(searchStr);
	//alert(url);
	xmlHttp = initializeXmlHTTP();
	xmlHttp.onreadystatechange = onCompleteGetMediaList;
	xmlHttp.open('GET', url);
	xmlHttp.send(null);
}

function scrollPrevMediaList() {
	if (start > 0) {
		start -= maxVideoListItems;
		if (start < 0) {
			start = 0;	
		}
		getMediaList();
	}
}

function scrollNextMediaList() {
	var tmp = start + maxVideoListItems;
	
	if (tmp >= videoListCount) {
		return;
	}
	start = tmp;
	getMediaList();
	
}

function sortMediaList(pSortType) {
	if (sortType == pSortType) return;
	
	sortType = pSortType;
	getMediaList();
}


function getVideo(id) {
	newVid = id;
	
	xmlHttp = initializeXmlHTTP();
	xmlHttp.onreadystatechange = onCompleteGetVideo;
	xmlHttp.open('GET', baseURL + 'media/media_actions.php?video=' + id);
	xmlHttp.send(null);
}

function doSearch() {
	var inputText = document.getElementById("searchKeyword");
	if (inputText) {
		searchStr = trimString(inputText.value);
	}
	start = 0;
	
	getMediaList();
}

function ehSearchBoxKeyDown(e) {
	var keynum;

	if (window.event) {
		keynum = e.keyCode;
 	}
	else if(e.which) {
		// Netscape/Firefox/Opera
		keynum = e.which;
	}
	
	if (keynum == 13) {
		doSearch();
	}

}


function onCompleteGetMediaList() {
	if(xmlHttp.readyState==4) {
		xml = xmlHttp.responseXML.documentElement;
		elem = document.getElementById('mediaList');
		var content = xml.getElementsByTagName("content")[0];
		
		btnPrev = document.getElementById('mediaListScrollPrevHL');
		btnNext = document.getElementById('mediaListScrollNextHL');
		
		if (content) {
			videoListCount = Number(content.getAttribute('count'));
			elem.innerHTML = content.childNodes[0].nodeValue;
			
			if (btnPrev && btnNext) {
				if (start > 0) {
					btnPrev.style.opacity = 0;
					btnPrev.style.filter="alpha(opacity=0)";
				}
				else {
					btnPrev.style.opacity = 0.4;
					btnPrev.style.filter="alpha(opacity=40)";
					btnPrevImg = document.getElementById('mediaListScrollPrevImg');
					btnPrevImg.src = baseURL + "images/zy_media_arrow_prev_off.gif";
				}
				
				if (start + maxVideoListItems >= videoListCount) {
					btnNext.style.opacity = 0.4;
					btnNext.style.filter="alpha(opacity=40)";
					btnNextImg = document.getElementById('mediaListScrollNextImg');
					if (btnNextImg) btnNextImg.src = baseURL + "images/zy_media_arrow_next_off.gif";
				}
				else {
					btnNext.style.opacity = 0;
					btnNext.style.filter="alpha(opacity=0)";
				}
			}
		}
	}
}


function onCompleteGetVideo() {
	if(xmlHttp.readyState==4) {
		xml = xmlHttp.responseXML.documentElement;
		elem = document.getElementById('mediaLeftCol');
		contentXML = xml.getElementsByTagName("content")[0];
		if (contentXML) {
			id = contentXML.getAttribute('id');
			
			content = contentXML.childNodes[0].nodeValue;
			elem.innerHTML  = content;
			updateFonts();
			
			elem = document.getElementById('media' + currVid);
			if (elem) {
				elem.style.opacity = 0.3;
				elem.style.filter="alpha(opacity=30)";
			}
			currVid = newVid;

			elem = document.getElementById('media' + currVid);
			if (elem) {
				elem.style.opacity = 0;
				elem.style.filter="alpha(opacity=0)";
			}
		}
	}
}

function mediaListHighlight(id, state) {
	elem = document.getElementById('media' + id);
	if (id == currVid) return;
	if (state) {
		elem.style.opacity = 0;
		elem.style.filter="alpha(opacity=0)";
	}
	else {
		elem.style.opacity = 0.3;
		elem.style.filter="alpha(opacity=30)";
	}
}



// Helper functions

function initializeXmlHTTP() {
	var xmlHttp;
	
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				return;
			}
		}
	} // end outer catch
	
	return xmlHttp;
}

function trimString(str) {
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function createPostString(formName) {
	theForm = document.forms[formName];
	var qs = ''
	for (e=0; e < theForm.elements.length; e++) {
		if (theForm.elements[e].name != '') {
			var name = theForm.elements[e].name;
			qs += (qs=='') ? '': '&';
			qs += name+'='+escape(theForm.elements[e].value);
		}
	}
	qs+="\n";
	return qs;
} 

