function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop //i mean, wtflux?!
		}
	}
	return curleft;
}

function getDistance( pil, eElement )
{
var iReturnValue = 0;

//iReturnValue = eElement.offsetLeft;
//iReturnValue -= (pil.offsetLeft + pil.offsetParent.offsetLeft); //remind me to delete this bullocks
iReturnValue = (findPos(eElement) - findPos(pil));
iReturnValue += ((eElement.width / 2) - (pil.width / 2));

return iReturnValue;
}

function pilGoTo(element) {
    var distance = getDistance($('pilimg'), element);
    new Effect.Move ($('pilimg'),{ x: distance, y: 0, mode: 'relative'}); //MOVE DAMNIT
    return distance;
}


var fwd = function(src, topbilde) {
    distance = -(findPos($(topbilde)) - findPos($('toppbilde')));
    new Effect.Move ($('topbildeantiwrap'),{ x: distance, y: 0, mode: 'relative'});
}

function get_selected_action() {
    return document.location.hash; //mm, hash O.o
}

var prev_hash = document.location.hash;
var expect_change = true;

function expected_change() {
    prev_hash = document.location.hash;
    expect_change = true; //TOtally expected
}

function interval_callback() {
    if(expect_change == true) {
        expect_change = false;
        prev_hash = document.location.hash;
    } else {
        if(prev_hash == document.location.hash) {
            return;
        } else {
            prev_hash = document.location.hash;
            backLoadSite($(document.location.hash.substring(1,document.location.hash.length) + 'meny'), document.location.hash.substring(1,document.location.hash.length)); // (phew)
        }
    }
}

var loadlock = false;
function backLoadSite(element, action) {
    if(loadlock == true)
        return false;
    loadlock = true;
    document.location.hash = action;
    new Ajax.Updater('tekst', '/ajax/get/' + action, { onComplete: function () { var distance = pilGoTo(element);
    fwd('lala', action + 'img');
    initLightbox(); //only used on that... that... site!
    } });
    loadlock = false;
    return true;
}

function ajaxLoadSite(element, action) {
    if(loadlock == true)
        return false;
    loadlock = true;
    document.location.hash = action;
    if($('editknapp'))
        $('editknapp').src = '/images/edit-knapp.jpg';
    new Ajax.Updater('tekst', '/ajax/get/' + action, { onComplete: function() {    
        var distance = pilGoTo(element);
        fwd('lala', action + 'img');
        initLightbox();
    }, evalScripts:true });
    expected_change();
    initLightbox(); //again? someone fix that lightbox
    loadlock = false;
    return false;
}

function onLoad() {
    onLoadAjax();
    setInterval(interval_callback, 500);
    }

function onLoadAjax() {
    action = document.location.hash.split('#');
    if(action == '') {
        action[1] = 'hovedside';
        document.location.hash = action[1];
    }
    var pos = getDistance($('pilimg'), $(action[1] + 'meny'));
    //$('pilimg').style.left = pos + 'px';
    $('pilimg').setStyle({ left: pos + 'px' });
    distance = -(findPos($(action[1] + 'img')) - findPos($('toppbilde')));
    $('topbildeantiwrap').setStyle({ left: distance + 'px' });
    new Ajax.Updater('tekst', '/ajax/get/' + action[1], { onComplete: function() { initLightbox(); }, evalScripts:true});
    
    return action[1]; //wtflux?
}

function editThisPage() {
    action = document.location.hash.split('#');
    if(action == '') {
        action[1] = 'hovedside';
    }
    new Ajax.Updater('tekst', '/ajax/edit/' + action[1], {evalScripts:true});
}
