var ContentHeightIndex = 0;
var TimeToSlide = 250.0;
var TimeToSlideContent = 450.0;
var TimeToFade = 500.0;
var MinContentHeight = window.innerHeight - 380;

var openAccordion = new Array();
var loadedPage;
var pageToBeLoaded;

var inProcess = false;
	  
var galleryHeight = 0;
var galleryTagWidths = new Array();

var scrollX;
var scrollY;

var baseUrl;

if (window.location.href.indexOf("#") > 0) {
	baseUrl = window.location.href.substring(0, window.location.href.indexOf("#"));
} else {
	baseUrl = window.location.href;
}
 
	  
var pages = new Array("home", "papers", "projects", "bio");

var contentHeights = new Array();

contentHeights["home"] = 800;
contentHeights["papers"] = 330;
contentHeights["projects"] = 1520;
contentHeights["bio"] = 950;

var accordionHeights = [250, 72 * galleries.length + 15, 60, 170];

var content;
	  
openAccordion["Accordion1Content"] = false;
openAccordion["Accordion2Content"] = false;
openAccordion["Accordion3Content"] = false;
openAccordion["Accordion4Content"] = false;

var initilized = false;

function loadIEPage() {
	var row = document.getElementById("content-row");
	row.removeChild(document.getElementById("side-menu"));
	row.firstChild.colSpan = "2";
	document.getElementById("menu-container").removeChild(document.getElementById("menu"));
//	document.getElementById("AccordionContainer").style.display = "none";
	var content = document.getElementById("content");
	content.innerHTML = "<div id='ie-page' class='page' style='display:block'><table><tr><td colspan='2'>i am very sorry to inform you that this website's content will remain unavailable through <a href='http://www.microsoft.com/windows/internet-explorer/default.aspx' target='_blank'>microsoft internet explorer</a> until the png transperancy problem is resolved. Please use some of the other popular browsers to access my home page:</td></tr><tr><td colspan='2'><ul><li><a href='http://www.google.com/chrome' target='_blank'>google chrome</a></li><li><a href='http://www.mozilla.com/en/' target='_blank'>mozilla firefox</a></li><li><a href='http://www.apple.com/safari/download/' target='_blank'>apple safari</a></li></ul></td></tr></div>";
}

function runAccordion(index) {
    var nID = "Accordion" + index + "Content";
    var oID = '';
    if(openAccordion[nID] == true) {
        oID = nID;
        openAccordion[nID] = false;
        nID = '';
		removeUrlAccordion(index);
    } else {
        openAccordion[nID] = true;
		addUrlAccordion(index);
    }

    setTimeout("animate(" + new Date().getTime() + "," + TimeToSlide + ",'"
        + oID + "','" + nID + "','" + accordionHeights[(index - 1)] +"')", 33);

}

function toggleSound() {
	playSound('toggle-sound');
	setTimeout("stopSound('toggle-sound')",500);
	var key = document.getElementById("speaker-on");
	if (key == null) {
		key = document.getElementById("speaker-off");
		key.id = "speaker-on";
		key.title = "Sound On";
	} else {
		key.id = "speaker-off";
		key.title = "Sound Off";
	}
}

function animate(lastTick, timeLeft, closingId, openingId, height) {
    var curTick = new Date().getTime();
    var elapsedTicks = curTick - lastTick;

    var opening = (openingId == '') ? null : document.getElementById(openingId);
    var closing = (closingId == '') ? null : document.getElementById(closingId);

    if(timeLeft <= elapsedTicks)
    {
        if(opening != null)
            opening.style.height = height + 'px';

        if(closing != null)
        {
            closing.style.display = 'none';
            closing.style.height = '0px';
        }
        return;
    }

    timeLeft -= elapsedTicks;
    var newClosedHeight = Math.round((timeLeft/TimeToSlide) * height);

    if(opening != null)
    {
        if(opening.style.display != 'block')
            opening.style.display = 'block';
        opening.style.height = (height - newClosedHeight) + 'px';
    }

    if(closing != null)
        closing.style.height = newClosedHeight + 'px';

    setTimeout("animate(" + curTick + "," + timeLeft + ",'"
        + closingId + "','" + openingId + "', '" + height + "')", 33);
}

function animateResize(lastTick, timeLeft, targetSize) {
	var curTick = new Date().getTime();
    var elapsedTicks = curTick - lastTick;
	
	if(timeLeft <= elapsedTicks){
		content.style.height = targetSize + "px";
		return;
	}
	timeLeft -= elapsedTicks;
	var oldHeight = content.style.height.substring(0,content.style.height.length - 2);	
	var newHeight = oldHeight - Math.round((timeLeft/TimeToSlideContent) * (oldHeight - targetSize));
	content.style.height = newHeight + "px";
	//document.getElementById("console").innerHTML += timeLeft + " newHeight: " + (newHeight) + " oldHeight: " + (oldHeight) + "<br/>";
	setTimeout("animateResize(" + curTick + ", " + timeLeft + ", " + targetSize + ")", 33);
}

function resize(){
	var h = contentHeights[pageToBeLoaded];
	if (h < MinContentHeight)
		h = MinContentHeight;
	var p = document.getElementById("home-page");
	var offset = p.style.height.substring(0, p.style.height.length - 2);
	if (content.offsetHeight == offset)
		return;
	if (offset != 0)
		h = offset;
//	document.getElementById("console").innerHTML = h + "<br/>";
	centerRobot();
  	setTimeout("animateResize(" + new Date().getTime() + "," + TimeToSlide + "," + h + ")", 15);
}

function fadeOut(buttonOver) {
    for (var i in pages) {
        if (pages[i] != pageToBeLoaded && pages[i] != buttonOver) {
            document.getElementById(pages[i] + "button").style.opacity = "0.2";
        }
    }
	playSound("sounds/menu-over.wav");
}
function fadeIn() {
    for (var i in pages) {
        document.getElementById(pages[i] + "button").style.opacity = "1";
    }
}
function fadeInSide(buttonOn) {
    buttonOn.style.opacity = "1";
}
function fadeOutSide(buttonOff) {
    if (!openAccordion[buttonOff.id.substring(0,10) + "Content"]){
        buttonOff.style.opacity = "0.2";
    }
}
	  
function highlightPic(pic) {
    var p = document.getElementById(pic);
    p.style.opacity = "0.75";
		  
}
	  
function unhighlightPic(pic) {
    var p = document.getElementById(pic);
    p.style.opacity = "1";
}
	  
function writeGalleryThumbnails() {
    var thumbnailsContainer = document.getElementById("Accordion2Content");
    var htmlContent = "";
    galleryHeight = 72 * galleries.length + 15;
	
    for (var i in galleries) {
		htmlContent += "<div id=\"" + galleries[i] + "-div\" class=\"photo\"></div>"
    }
	
    thumbnailsContainer.innerHTML = htmlContent;
	
	for (var i in galleries){
		var img = new Image();
		img.name = galleries[i];
		img.onload = setImgWidth;
        img.src = baseUrl + "img/photo/thumbnails/" + galleries[i] + "-tag.png";
	}
}

function setImgWidth() {
	document.getElementById(this.name + "-div").innerHTML = "<span id=\"" + this.name + "-span\" style=\"width: " + this.width + 
		"px;background:url(img/photo/thumbnails/" + this.name + "-tag.png) no-repeat;\"></span><img id=\"" + this.name + "\" src=\"" + 
		baseUrl + "img/photo/thumbnails/" + this.name + 
		".jpg\" alt=\"image\" onmouseover=\"highlightPic('" + 
		this.name + "')\" onmouseout=\"unhighlightPic('" + this.name + "')\" onmouseup=\"openGallery('" + this.name + "')\"/>";
}
	  
function loadPage(page) {
    if ((loadedPage != null && page == loadedPage) || inProcess) {
        return;
    }
	inProcess = true;
	pageToBeLoaded = page;
    document.getElementById(pageToBeLoaded + "button").style.borderColor = "black";
    if (loadedPage != null) {
        document.getElementById(loadedPage + "button").style.borderColor = "white";
		fade(loadedPage + "-page");
		setTimeout("document.getElementById(loadedPage + \"-page\").style.display = \"none\";", 512.0);
		setTimeout("resize(pageToBeLoaded);",513.0);
		setTimeout("document.getElementById(pageToBeLoaded + \"-page\").style.display = \"block\";", 765.0);
		setTimeout("fade(pageToBeLoaded + '-page');", 770.0);
		setTimeout("loadedPage = pageToBeLoaded;", 772);
		fadeOut(pageToBeLoaded);
		changeUrlPage(page);
	} else {
		loadedPage = pageToBeLoaded;
		document.getElementById(pageToBeLoaded + "-page").style.display = "block";
		fade(pageToBeLoaded + "-page");
		fadeIn();
		if(window.location.href.indexOf("#") < 0) {
			changeUrlPage(page);
		}
	}
	setTimeout("inProcess = false;", 1000.0);
}
	  
function openGallery(name) {
    var script = "var flashvars = {XMLFile: \"gallery_" + name + ".xml\"};\nvar params = {wmode: \"transparent\", bgcolor: \"#ffffff\"};\nswfobject.embedSWF(\"swf/PhotoGallery.swf\", \"myAlternativeContent\", window.innerWidth - 40, \"900\", \"9.0.0\",false, flashvars, params);"
    var galleryScr = document.getElementById("gallery-screen");
    var scriptObj = document.createElement("script");
    var closeScrUp = document.getElementById("gallery-close-screen-up");
    var closeScrDown = document.getElementById("gallery-close-screen-down");
    var closeScrLeft = document.getElementById("gallery-close-screen-left");
    var closeScrRight = document.getElementById("gallery-close-screen-right");
    closeScrUp.style.display = "block";
    closeScrDown.style.display = "block";
    closeScrRight.style.display = "block";
    closeScrLeft.style.display = "block";
    closeScrDown.style.height = (document.body.clientHeight - 760) + "px"; 
    closeScrLeft.style.width = ((document.body.clientWidth - 460)/2) + "px";
    closeScrRight.style.width = ((document.body.clientWidth - 470)/2) + "px";
    closeScrRight.style.left = ((document.body.clientWidth + 470)/2) + "px";
    scriptObj.setAttribute("type", "text/javascript");
    scriptObj.innerHTML = script;
    galleryScr.appendChild(scriptObj);
    galleryScr.style.display = "block";
	//setTimeout("document.getElementById(\"gallery-close\").style.display = \"block\"", 3000.0);
	scrollX = window.pageXOffset;
	scrollY = window.pageYOffset;
	window.scrollTo(0,0);
}

function closeGallery() {
	fade("gallery-screen");
	//document.getElementById("gallery-close").style.display = "none";
	document.getElementById("gallery-close-screen-up").style.display = "none";
	document.getElementById("gallery-close-screen-down").style.display = "none";
	document.getElementById("gallery-close-screen-left").style.display = "none";
	document.getElementById("gallery-close-screen-right").style.display = "none";
	setTimeout("document.getElementById(\"gallery-screen\").style.display = \"none\";", 510.0);
	setTimeout("fade(\"gallery-screen\");", 520.0);
//	setTimeout("window.scrollTo(scrollX, scrollY);", 530.0);
}
	  
function init() {
	centerFooter();
	if (navigator.appName.indexOf("Netscape") < 0) {
		loadIEPage();
		return;
	}
	content = document.getElementById("content"); 
	content.style.height = "400px";
	centerAdminForm();
	centerLoginForm();
	centerRobot();
	
	var page = "home";
	var params = readUrl();
	
	if (params[0]) {
		page = params[0];
	}
	for (var i in pages) {
		var p = document.getElementById(pages[i] + "-page");
		p.style.opacity = "0";
		p.style.filter = "alpha(0)";
	}
    writeGalleryThumbnails();
	pageToBeLoaded = page;
	resize(pageToBeLoaded);
	loadPage(pageToBeLoaded);
	ajaxpage("php/load-news.php", "news");
	ajaxpage("php/load-song.php", "last-song");
	ajaxpage("php/load-project.php", "current-project");
	if (params[1] && params[1].indexOf(",") > 0) {
		var oa = params[1].split(",");
		var waitTime = 0;
		for (var i in oa) {
			waitTime += accordionHeights[i] + 20;
			setTimeout(runAccordion(oa[i]), waitTime);
		}
	} else if (params[1]) {
		runAccordion(params[1]);
	}
}
	  
function fade(eid) { 
    var element = document.getElementById(eid);
    if(element == null)
        return;
   
    if(element.FadeState == null){
        if(element.style.opacity == null
            || element.style.opacity == ''
            || element.style.opacity == '1'){
            element.FadeState = 2;
        }
        else {
            element.FadeState = -2;
        }
    }
    
    if(element.FadeState == 1 || element.FadeState == -1){
        element.FadeState = element.FadeState == 1 ? -1 : 1;
        element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
    } else {
        element.FadeState = element.FadeState == 2 ? -1 : 1;
        element.FadeTimeLeft = TimeToFade;
        setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
    }
}

function animateFade(lastTick, eid) {  
    var curTick = new Date().getTime();
    var elapsedTicks = curTick - lastTick;
  
    var element = document.getElementById(eid);
 
    if(element.FadeTimeLeft <= elapsedTicks){
        element.style.opacity = element.FadeState == 1 ? '1' : '0';
        element.style.filter = 'alpha(opacity = '
        + (element.FadeState == 1 ? '100' : '0') + ')';
        element.FadeState = element.FadeState == 1 ? 2 : -2;
        return;
    }
 
    element.FadeTimeLeft -= elapsedTicks;
    var newOpVal = element.FadeTimeLeft/TimeToFade;
    if(element.FadeState == 1)
        newOpVal = 1 - newOpVal;

    element.style.opacity = newOpVal;
    element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
  
    setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}

function changeUrlPage(page){
	var url = window.location.href;
	if (url.indexOf("#") > 0) {
		url = url.replace('page=home', 'page=' + page);
		url = url.replace('page=papers', 'page=' + page);
		url = url.replace('page=projects', 'page=' + page);
		url = url.replace('page=bio', 'page=' + page);
	} else {
		url += "#/?page=" + page;
	}
	window.location.href = url;
}

function readUrl() {
	var url = window.location.href;
	if (url.indexOf("#") < 0) {
		return ["home"];
	}
	var params = new Array();
	params = url.split("/?");
	if (params.length == 3) {
		//window.location.href = url.substring(0, url.indexOf("accordion=") + 10);
	}
	if (params.length == 2) {
		return [params[1].substring(params[1].indexOf("=") + 1)];
	} 
	return [params[1].substring(params[1].indexOf("=") + 1), params[2].substring(params[2].indexOf("=") + 1)];
}

function addUrlAccordion(accordionIndex){
	var url = window.location.href;
	if (url.indexOf("/?accordion=") > 0) {
		var accordions = new Array();
		var oldAccordion = url.substring(url.indexOf("accordion=") + 10);
		accordions = oldAccordion.split(",");
		for (var i in accordions) {
			if (accordions[i] == accordionIndex) {
				return;
			} 
		}
		accordions.push(accordionIndex);
		accordions = accordions.sort();
		url = url.replace(url.substring(url.indexOf("accordion=")), "accordion=" + accordions.join(","));
	} else {
		url += "/?accordion=" + accordionIndex;
	}
	window.location.href = url;
}

function removeUrlAccordion(accordionIndex){
	var url = window.location.href;
	if (url.indexOf("/?accordion") > 0) {
		var accordions = new Array();
		var oldAccordion = url.substring(url.indexOf("accordion=") + 10);
		accordions = oldAccordion.split(",");
		for (var i in accordions) {
			if (accordions[i] == accordionIndex) {
				accordions.splice(i, 1);
			}
		}
		if (accordions.length > 0) {
			url = url.replace(oldAccordion, accordions.join(","));
		} else {
			url = url.replace("/?accordion=" + oldAccordion, "");
		}
	}
	window.location.href = url;
}

function fixLowerCase(text) {
	return text.replace(/([0-9?!;:"@#$%^&*()\[\].,-]+)/g, "<span class='text-misc'>$1</span>");
}

function fixTweet() {
	var tweet = document.getElementById("tweet");
	var tweetLink = tweet.getElementsByTagName("li").item(0);
	var code = tweetLink.innerHTML;
	var text = code.substring(0, code.indexOf("<a"));
	var hyperLink = tweetLink.getElementsByTagName("a").item(0);
	var hyperLinkCode = hyperLink.innerHTML;
	hyperLink.innerHTML = fixLowerCase(hyperLinkCode);
	tweetLink.innerHTML = fixLowerCase(text);
	hyperLink.className = "time";
	hyperLink.target = "_blank";
	tweetLink.appendChild(hyperLink);
}

function centerAdminForm() {
	var title = document.getElementById("admin-title");
	var container = document.getElementById("admin-container");
	var background = document.getElementById("admin-background");
	var closeButton = document.getElementById("admin-close");
	var logoutButton = document.getElementById("admin-logout");
	var left;
	
	title.style.display = "block";
	container.style.display = "block";
	background.style.display = "block";
	closeButton.style.display = "block";
	logoutButton.style.display = "block";
	
	left = (window.innerWidth - title.offsetWidth) / 2;
	
	title.style.position = "fixed";
	title.style.left =  (left - ((container.offsetWidth + title.offsetWidth)/ 2) + 11) + "px";
	title.style.top = "140px";

	background.style.width = document.body.offsetWidth + "px";
	left = (window.innerWidth - container.offsetWidth) / 2;
	
	container.style.left = left + "px"
	left += container.offsetWidth - 25;
	
	closeButton.style.left = (left + 24) + 'px';
	closeButton.style.top = '18px';
	
	logoutButton.style.left = (left - 18) + 'px';;
	logoutButton.style.top = '36px';
	
	title.style.display = "none";
	container.style.display = "none";
	background.style.display = "none";
	closeButton.style.display = "none";
	logoutButton.style.display = "none";
	
	closeButton.style.zIndex = "5";
	title.style.zIndex = "4";
	container.style.zIndex = "4";
	background.style.zIndex = "4";
	logoutButton.style.zIndex = "5";
}

function centerLoginForm() {
	var container = document.getElementById('login-container');
	var closeButton = document.getElementById('login-close');
	var title = document.getElementById("login-title");
		
	container.style.display = "block";
	var padding = (container.offsetWidth - document.getElementById("login-form").offsetWidth - 4) / 2;
	closeButton.style.display = "block";
	
	var left = (window.innerWidth - container.offsetWidth) / 2;
	var top = 350;
	
	title.style.top = "403px";
	title.style.zIndex = "5";
	title.style.left = (left - 15) + "px";
	
	container.style.left = left + "px";
	left += (container.offsetWidth);
	closeButton.style.left = left + "px";
	
	container.style.top = top + "px";
	top -= 10;
	closeButton.style.top = top + "px";
	
	container.style.display = "none";
	closeButton.style.display = "none";
	
	container.style.zIndex = "4";
	closeButton.style.zIndex = "5";
}

function centerFooter() {
	var footer = document.getElementById("footer");
	var footerLinks = document.getElementById("footer-links");
	var width = (footer.offsetWidth - footerLinks.offsetWidth) / 2;
	footerLinks.style.marginLeft = width + "px";
}

function centerRobot() {
	var robot = document.getElementById("robot");
	var formula = document.getElementById("formula");
	robot.style.display = "block";
	robot.style.top = (window.innerHeight - robot.offsetHeight - 1) + "px";
	robot.style.left = (((window.innerWidth - 1022) / 2) + 30) + "px";
	formula.style.top = (robot.offsetTop - 32) + "px";
	formula.style.left = (robot.offsetLeft - 61) + "px";
}

function toggleFormula() {
	var formula = document.getElementById("formula");
	if (formula.style.display == "block") {
		formula.style.display = "none";
	} else {
		formula.style.display = "block";
	}
}

function closeAdminForm() {
	document.getElementById("admin-title").style.display = "none";
	document.getElementById("admin-container").style.display = "none";
	document.getElementById("admin-background").style.display = "none";
	document.getElementById("admin-close").style.display = "none";
	document.getElementById("admin-logout").style.display = "none";
	document.getElementsByTagName("html")[0].style.overflowY = "scroll";
}

function openAdminForm(sessionid) {
	closeLoginForm();
	setCookie("sessionid_savkov", sessionid, 1);
	document.getElementById("admin-title").style.display = "block";
	document.getElementById("admin-container").style.display = "block";
	document.getElementById("admin-background").style.display = "block";
	document.getElementById("admin-background").style.height = (document.body.offsetHeight + 30) + "px";
	document.getElementById("admin-close").style.display = "block";
	document.getElementById("admin-logout").style.display = "block";
	window.scrollTo(0,document.getElementById("admin-title").offsetTop - 10);
	document.getElementsByTagName("html")[0].style.overflowY = "hidden";
}

function checkLoginSession() {
	var session = getCookie("sessionid_savkov");
	if (session == "") {
		openLoginForm();
		return;
	}
	var script = document.createElement("script");
	script.setAttribute("id", "authenticate-script");
	script.setAttribute("language", "jscript");
	script.setAttribute("type", "text/javascript");
	script.setAttribute("charset", "utf-8");
	var head = document.getElementsByTagName("head").item(0);
	head.appendChild(script);
	ajaxpage("php/authenticate-session.php?session=" + session, "authenticate-script");
	setTimeout('head.removeChild(document.getElementById("authenticate-script"));', 3000);
}

function openLoginForm() {
	document.getElementsByTagName("html")[0].style.overflowY = "hidden";
	document.getElementById("login-container").style.display = "block";
	document.getElementById("login-close").style.display = "block";
	document.getElementById("admin-background").style.display = "block";
	document.getElementById("admin-background").style.height = document.body.offsetHeight + "px";
	document.getElementById("admin-background").style.width = window.innerWidth + "px";
	document.getElementById("login-user").select();
	document.getElementById("login-title").style.display = "block";
}

function closeLoginForm() {
	document.getElementById("admin-background").style.display = "none";
	document.getElementById("login-container").style.display = "none";
	document.getElementById("login-form").style.display = "block";
	document.getElementById("login-wait").style.display = "none";
	document.getElementById("login-close").style.display = "none";
	document.getElementById("login-title").style.display = "none";
	document.getElementsByTagName("html")[0].style.overflowY = "scroll";
}

function resetLoginForm() {
	document.getElementById("login-wait").style.display = "none";
	document.getElementById("login-form").style.display = "block";
	document.getElementById("login-user").value = "";
	document.getElementById("login-pass").value = "";
}

function login() {
	var username = document.getElementById("login-user").value;
	var password = document.getElementById("login-pass").value;
	var session = Math.round(Math.random(11) * 1000000);
	document.getElementById("login-form").style.display = "none";
	document.getElementById("login-wait").style.display = "block";
	var script = document.createElement("script");
	script.setAttribute("id", "login-script");
	script.setAttribute("language", "jscript");
	script.setAttribute("type", "text/javascript");
	script.setAttribute("charset", "utf-8");
	var head = document.getElementsByTagName("head").item(0);
	head.appendChild(script);
	ajaxpage("php/login.php?user=" + username + "&pass=" + escape(password) + "&session=" + session, "login-script");
	document.getElementById("login-user").value = "";
	document.getElementById("login-pass").value = "";
	setTimeout('head.removeChild(document.getElementById("login-script"));', 3000);
}

function logout() {
	var script = document.createElement("script");
	var session = getCookie("sessionid_savkov");
	script.setAttribute("id", "logout-script");
	script.setAttribute("language", "jscript");
	script.setAttribute("type", "text/javascript");
	script.setAttribute("charset", "utf-8");
	var head = document.getElementsByTagName("head").item(0);
	head.appendChild(script);
	ajaxpage("php/login.php?session=" + session, "logout-script");
	closeAdminForm();
	deleteCookie("sessionid_savkov");
	setTimeout('head.removeChild(document.getElementById("logout-script"));', 3000);
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toUTCString());
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
  		if (c_start!=-1) {
    		c_start=c_start + c_name.length+1;
    		c_end=document.cookie.indexOf(";",c_start);
    		if (c_end==-1)
				c_end=document.cookie.length;
    	return unescape(document.cookie.substring(c_start,c_end));
    	}
  	}
	return "";
}

function deleteCookie(c_name, path, domain) {
	setCookie(c_name, "", 0);
}

function wellformedYoutubeUrl(url) {
	
	var parameters = url.substring(url.indexOf("?") + 1).split("&");
	var params = "?";
	var video;
	for (var i = 0; i < parameters.length; i++) {
		if (parameters[i].match("^v=.+") != null) {
			video = parameters[i].substring(parameters[i].indexOf("=") + 1);
		} else if (parameters[i].match("[^=]+=.+")){
			if (params == "?") {
				params += parameters[i];
			} else {
				params = params + "&" + parameters[i];
			}
		}
	}
	
	return "http://www.youtube.com/v/" + video + params;
}

function uploadData(boxName) {
	var url;
	if (boxName == 'project') {
		url = "php/upload-" + boxName + ".php?name=" + document.getElementById(boxName + "-input-name").value 
			+ "&url=" + escape(document.getElementById(boxName + "-input-url").value);
		document.getElementById(boxName + "-input-name").value = "";
		document.getElementById(boxName + "-input-url").value = "";
	} else if (boxName == 'video') {
		url = "php/upload-" + boxName + ".php?name=" + document.getElementById(boxName + "-input-name").value 
			+ "&url=" + escape(wellformedYoutubeUrl(document.getElementById(boxName + "-input-url").value));
		document.getElementById(boxName + "-input-name").value = "";
		document.getElementById(boxName + "-input-url").value = "";

	} else if (boxName == 'news') {
		url = "php/upload-" + boxName + ".php?news=" + escape(document.getElementById(boxName + "-input-text").value.replace(/'/g,"`"));
		document.getElementById(boxName + "-input-text").value = "";
	} else {
		return;
	}
	
	document.getElementById(boxName + "-input-wait").style.display = "block";
	document.getElementById(boxName + "-input").style.display = "none";
	document.getElementById(boxName + "-input-button").style.display = "none";
	
	var script = document.createElement("script");
	script.setAttribute("id", "upload-script");
	script.setAttribute("language", "jscript");
	script.setAttribute("type", "text/javascript");
	script.setAttribute("charset", "utf-8");
	var head = document.getElementsByTagName("head").item(0);
	
	if (document.getElementById("upload-script"))
		head.removeChild(document.getElementById("upload-script"));
		
	head.appendChild(script);
	
	ajaxpage(url, "upload-script");
	
	//setTimeout('head.removeChild(document.getElementById("upload-script"));', 3000);
}

function uploadStatus(boxName, status) {
	document.getElementById(boxName + "-input-wait").style.display = "none";
	document.getElementById(boxName + "-input-" + status).style.display = "block";
	setTimeout("document.getElementById('" + boxName + "-input-" + status + "').style.display = 'none';", 1500);
	setTimeout("document.getElementById('" + boxName + "-input').style.display = 'block';", 1550);	
	setTimeout("document.getElementById('" + boxName + "-input-button').style.display = 'block';", 1550);	
}

function onEnter(e){
	if(event.keyCode=='13'){
		login();
	}
}
