var step = 0;
var imageCount = 0;
var images = new Array();

function addImage(img) {
    images[imageCount] = new Array();
    images[imageCount]['img'] = img;

    if(!imageCount) {
        setTimeout("cacheImage(0, '" + img + "');", 2000);
    }
    ++imageCount;
}

function cacheImage(num, img) {
    images[num]['loaded'] = true;
    new Image().src = img;
}

function slideIt() {
    var myImg    = document.getElementById('fancy');
    var cur_num  = (step%imageCount);
    var img      = images[cur_num]['img'];
    if(img) {
        crossfade(myImg, img, '2', 'Velkommen');
    }
    step++;

    var next_num = (step+1)%imageCount;
    var img_next = images[next_num]['img'];
    if(!images[next_num]['loaded']) {
        cacheImage(next_num, img_next);
    }
}

var a=setInterval('slideIt()', 8500)

function open_popup(el, margin) {
    margin = margin > 0 ? 40 : -240;

    var pos = findPos(el);
    var popup = document.getElementById('supsearch');
    var shadow = document.getElementById('supsearch_shadow');

    var top = pos[1] + margin;
    var left = 737;
    popup.style.position = "absolute";
    popup.style.left = left + "px";
    popup.style.top = top + "px";
    popup.style.display = "block";

    shadow.style.position = "absolute";
    shadow.style.left = (left+6) + "px";
    shadow.style.top = (top+6)   + "px";
    shadow.style.display = "block";
}

function close_popup(el) {
    document.getElementById('supsearch').style.display = "none";
    document.getElementById('supsearch_shadow').style.display = "none";
}

function getMousePos(ev){
    var ev = ev || window.event;
    var mousePos = mouseCoords(ev);
}

function mouseCoords(ev){
    if(ev.pageX || ev.pageY){
        return {x:ev.pageX, y:ev.pageY};
    }
    return {
        x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
        y:ev.clientY + document.body.scrollTop  - document.body.clientTop
    };
}

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
        }
    }
    return [curleft,curtop];
}

function setBigImg(uri, me) {
    if ($(me).css('opacity') != '0.6')
    {
        var bigimg = $("#bigimg");
        var bigimg_img = bigimg.find('img');

        clearImgs();

        bigimg.css('display', 'block');
        bigimg_img.attr('src', uri + "&w=350");

        $(me).css('opacity', '0.6');
        $("#zoomcont").hide();
    }

    return false;
}

function clearImgs()
{
    $(".smallimg").css('opacity', '1');
    $("#bigimg").attr('src', '').css('display', 'none');
    stopZoom();

    return false;
}

function scaleDiv()
{
    var bigimg = $("#bigimg img");
    var div = bigimg.parent();

    div.width(bigimg.width());

    if (bigimg.width() == '350')
        $("#zoomer").show();
    else
        $("#zoomer").hide();

    return false;
}

function zoomImg()
{
    var zoomimg = $("#zoomimg");
    var zoomimg_img = zoomimg.find("img");

    var bigimg = $("#bigimg");
    var bigimg_img = bigimg.find("img");

    var pos = findPos(bigimg_img[0]);

    zoomimg.css('position', 'absolute').css('top', pos[1]).css('left', pos[0]).css('z-index', '9999999');

    var new_src = bigimg_img.attr('src').split("&");
    delete new_src[3];
    new_src = new_src.join("&");

    zoomimg_img.attr('src', new_src);
    zoomimg.show();

    bigimg.css('visibility', 'hidden');

    return false;
}

function stopZoom()
{
    $("#zoomimg").hide();
    $("#bigimg").css('visibility', 'visible');

    return false;
}
