function el(id) { return document.getElementById(id); }
// templates
var tts = new Array();

/* mmm, blogs over XMLHTTP... =) */

var blog_pn = 0;
var blog_pp = 0;

function blognav(dir) {
	if (blog_pn > 1 && dir < 0) { blog_pn--; }
	else if (dir > 0) { blog_pn++; }
	// branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = processBlogNavChange;
        xmlhttp.open("GET", '/blog/index.phtml?format=xml&pn='+blog_pn+'&pp='+blog_pp, true);
        xmlhttp.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        if (xmlhttp) {
            xmlhttp.onreadystatechange = processBlogNavChange;
	        xmlhttp.open("GET", '/blog/index.phtml?format=xml&pn='+blog_pn+'&pp='+blog_pp, true);
            xmlhttp.send();
        }
    }
    window.status='/blog/index.phtml?format=xml&pn='+blog_pn+'&pp='+blog_pp;
    return false;
}

// handle onreadystatechange event of req object
function processBlogNavChange() {
    // only if req shows "loaded"
    if (xmlhttp.readyState == 4) {
        // only if "OK"
        if (xmlhttp.status == 200) {
            updateBlogEntries();
            document.body.scrollIntoView();
         } else {
            alert("There was a problem retrieving the XML data:\n" +
                xmlhttp.statusText);
         }
    }
}

function updateBlogEntries() { 
    var items = xmlhttp.responseXML.getElementsByTagName("entry");
  
    for (i=0; i < items.length; i++) { 
		el('bentrytitle'+i).innerHTML = getElementTextNS("", "blogtitle", items[i], 0);
		el('bentrydates'+i).innerHTML = getElementTextNS("", "blogdates", items[i], 0);
		el('bentrybody'+i).innerHTML = getElementTextNS("", "blogbody", items[i], 0);
		el('bentry'+i).style.display = 'block';
    }
    for (i=items.length; i < 3; i++) { 
		el('bentry'+i).style.display = 'none';
    }
    el('backlink').style.display = blog_pn > 1 ? 'inline' : 'none';
    el('nextlink').style.display = items.length == blog_pp ? 'inline' : 'none';
    el('nextbacksep').style.display = ( (blog_pn > 1) && (items.length == blog_pp) ) ? 'inline' : 'none';
}





// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method 
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided 
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes 
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else if (result.firstChild) {
            return result.firstChild.nodeValue;    		
        } else { 
        	return '';
        }
    } else {
        return "n/a";
    }
}



var expDays = 30;
var exp=new Date;
exp.setTime(exp.getTime() + (expDays*30*24*60*60*1000));
function swapTemplateTheme() { 
	var te = el('template_select');
	var tev = te.options[te.selectedIndex].value;
	cssremoved=false;
	var se = el('template_css');
	var sh = el('theme_css');
	var th = el('theme_select');
	var thv = th.options[th.selectedIndex].value;
	var sehref = '/templates/' + tev + '/default.css.php';
	var shhref = '/templates/' + tev + '/themes/'+ thv + '.css.php';
	se.href = sehref;
	sh.href = shhref;
	SetCookie('TPTEMPLATE', tev, exp, '/','.thomporter.com');
	SetCookie('TPTEMPLATETHEME', thv, exp, '/','.thomporter.com');
	document.body.className = tts[te.selectedIndex][3];
	return false;
}

function templateSelectChange() { 
	var te = el('template_select');
	var th = el('theme_select');
	for (i=th.options.length; i>-1; i--) { 
		th.remove(i);
	}
	if (tts[te.selectedIndex][2] == false) return;
	
	for (i=0; i < tts[te.selectedIndex][2].length; i++) { 
		var opt = document.createElement('option');
		th.options.add(opt);
		opt.value = tts[te.selectedIndex][2][i][0];
		opt.text =  tts[te.selectedIndex][2][i][1];
	}
}



/*mmmm, cookies. =) */
   function getCookieVal (offset) { 
    var endstr = document.cookie.indexOf (";", offset); 
    if (endstr == -1) 
    endstr = document.cookie.length; 
    return unescape(document.cookie.substring(offset, endstr)); 
    } 
    function GetCookie (name) { 
    var arg = name + "="; 
    var alen = arg.length; 
    var clen = document.cookie.length; 
    var i = 0; 
    while (i < clen) { 
    var j = i + alen; 
    if (document.cookie.substring(i, j) == arg) 
    return getCookieVal (j); 
    i = document.cookie.indexOf(" ", i) + 1; 
    if (i == 0) break; 
    } 
    return null; 
    } 
    function SetCookie (name, value) { 
    var argv = SetCookie.arguments; 
    var argc = SetCookie.arguments.length; 
    var expires = (argc > 2) ? argv[2] : null; 
    var path = (argc > 3) ? argv[3] : null; 
    var domain = (argc > 4) ? argv[4] : null; 
    var secure = (argc > 5) ? argv[5] : false; 
    document.cookie = name + "=" + escape (value) + 
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
    ((path == null) ? "" : ("; path=" + path)) + 
    ((domain == null) ? "" : ("; domain=" + domain)) + 
    ((secure == true) ? "; secure" : ""); 
    } 
    function DeleteCookie (name) { 
    var exp = new Date(); 
    exp.setTime (exp.getTime() - 1); 
    // This cookie is history 
    var cval = GetCookie (name); 
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString(); 
}

