/* Javascript for RealTown Blogs -- Custom Templates */


var arrHostName = location.hostname.split('.');

if(arrHostName[(arrHostName.length - 2)] == 'realtown') {
    var strBasePath = '/';
} else {
    var arrPath = location.pathname.split('/');
    var arrCleanPath = new Array();
    for(var x = 0; x < arrPath.length; x++) {
        if(arrPath[x].match('.php') || arrPath[x] == 'admin') {
            break;
        }
        arrCleanPath.push(arrPath[x]);
    }
    var strBasePath = arrCleanPath.join('/') + '/';
}

var intLastScrollPositionTop = 0;
var intLastScrollPositionLeft = 0;
var intCurrentScrollPositionTop = 0;
var intCurrentScrollPositionLeft = 0;
var intCurrentWindowWidth = 0;
var intLastWindowWidth = 0;
var tmrLogoPosition;
var intPoweredByWidth = 100;


Event.observe(window, 'load', function() {
    if($('rtPoweredBy')) {
        tmrLogoPosition = setInterval("updatePoweredByLogoPosition()", 10);
        intPoweredByWidth = $('rtPoweredBy').getWidth();
    }
});

function updatePoweredByLogoPosition() {
    if($('rtPoweredBy') && $('rtPoweredBy').style.display != 'none') {
        intCurrentScrollPositionLeft = (document.all) ? document.body.scrollLeft: window.pageXOffset;
        intCurrentScrollPositionTop = (document.all) ? document.body.scrollTop: window.pageYOffset; 
        intCurrentWindowWidth = (document.all) ? document.body.clientWidth: window.outerwidth;
        
        if(intLastScrollPositionTop != intCurrentScrollPositionTop) {
            intLastScrollPositionTop = intCurrentScrollPositionTop;
            $('rtPoweredBy').style.top = intLastScrollPositionTop + 5;
        }

        if(intLastScrollPositionLeft != intCurrentScrollPositionLeft || intLastWindowWidth != intCurrentWindowWidth) {
            intLastScrollPositionLeft = intCurrentScrollPositionLeft;
            intLastWindowWidth = intCurrentWindowWidth;
            $('rtPoweredBy').style.left = intLastScrollPositionLeft - 5 + intCurrentWindowWidth - intPoweredByWidth;
        }
    }
}

function starItem(strItemID, enumSource, strIdPrefix, intListID) {
    if(strItemID && enumSource && $(strIdPrefix + strItemID)) {
        var starItemHandler = function(t) {
            var response = t.responseText;
            if(response == 'fail') {
                alert("You are not allowed to star this item.");
                return;
            }
            if(response == 1) {
                $(strIdPrefix + strItemID).src = 'img/buttons/star_on.png';
            } else {
                $(strIdPrefix + strItemID).src = 'img/buttons/star_off.png';
            }
        }
        
        var starItemError = function(t) {
            alert('Error ' + t.status + ' -- ' + t.statusText);
        }
        
        var postData  = 'task=star';
            postData += '&strItemID=' + encodeURIComponent(strItemID);
            postData += '&enumSource=' + encodeURIComponent(enumSource);
        if(intListID) {
            postData += '&intListID=' + encodeURIComponent(intListID);
        }
        new Ajax.Request(strBasePath + 'ajax.php', {method:'post', postBody:postData, onSuccess:starItemHandler, onFailure:starItemError});
    }
}