var offsetfrommouse = [15, 25]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var displayduration = 0; //duration in seconds image should remain visible. 0 for always.
var defaultimageheight = 40; // maximum image size.
var defaultimagewidth = 40; // maximum image size.
var prev_width = 200;
var prev_height = 200;

var timeout = 200;

var timer;

function truebody(){
return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function show(imagename, title, width, height){
    var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth - offsetfrommouse[0]
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)
    
    if ((navigator.userAgent.indexOf("Konqueror") == -1 || navigator.userAgent.indexOf("Firefox") != -1 || (navigator.userAgent.indexOf("Opera") == -1 && navigator.appVersion.indexOf("MSIE") != -1)) && (docwidth > 650 && docheight > 500)) {
        (width == 0) ? width = defaultimagewidth : '';
        (height == 0) ? height = defaultimageheight : '';
        
        width += 30;
        height += 55;
        defaultimageheight = height;
        defaultimagewidth = width;
        
        $(document).mousemove(function(e){
			followmouse(e);
		});
        
        newHTML = '<div class="border_preview" style="width:' + width + 'px;height:' + height + 'px"><div id="loader_container"><div id="loader"><div align="center">Laster bilde...</div><div id="loader_bg"><div id="progress"> </div></div></div></div>';
        //newHTML = newHTML + '<h2 class="title_h2">' + ' ' + title + '</h2>'
        newHTML = newHTML + '<div class="preview_temp_load"><img onload="javascript:remove_loading();" src="' + imagename + '" border="0"></div>';
        newHTML = newHTML + '</div>';
        if (navigator.userAgent.indexOf("MSIE") != -1 && navigator.userAgent.indexOf("Opera") == -1) {
            newHTML = newHTML + '<iframe src="about:blank" scrolling="no" frameborder="0" width="' + width + '" height="' + height + '"></iframe>';
        }
        $('#preview_div').empty();
        $('#preview_div').append(newHTML);
        $('#preview_div').show();
    }
}

function followmouse(e){

    var xcoord = offsetfrommouse[0];
    var ycoord = offsetfrommouse[1];
    
    var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth - offsetfrommouse[0]
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)
    
    if (typeof e != "undefined") {
        if (docwidth - e.pageX < defaultimagewidth + 2 * offsetfrommouse[0]) {
            xcoord = e.pageX - xcoord - defaultimagewidth; // Move to the left side of the cursor
        }
        else {
            xcoord += e.pageX;
        }
        if (docheight - e.pageY < defaultimageheight + 2 * offsetfrommouse[1]) {
            ycoord += e.pageY - Math.max(0, (2 * offsetfrommouse[1] + defaultimageheight + e.pageY - docheight - truebody().scrollTop));
        }
        else {
            ycoord += e.pageY;
        }
        
    }
    else 
        if (typeof window.event != "undefined") {
            if (docwidth - event.clientX < defaultimagewidth + 2 * offsetfrommouse[0]) {
                xcoord = event.clientX + truebody().scrollLeft - xcoord - defaultimagewidth; // Move to the left side of the cursor
            }
            else {
                xcoord += truebody().scrollLeft + event.clientX
            }
            if (docheight - event.clientY < (defaultimageheight + 2 * offsetfrommouse[1])) {
                ycoord += event.clientY + truebody().scrollTop - Math.max(0, (2 * offsetfrommouse[1] + defaultimageheight + event.clientY - docheight));
            }
            else {
                ycoord += truebody().scrollTop + event.clientY;
            }
        }
    $('#preview_div').css('left',xcoord + "px");
    $('#preview_div').css('top',ycoord + "px");
    
}

$(document).ready(function(){
    $("img.preview").mouseout(function(){
        $('#preview_div').hide();
        $('#preview_div').css('left', '-500px');
        clearTimeout(timer);
    }).mouseover(function(){		
        imagename = this.src;
        title = this.alt;
        w = prev_width;
        h = prev_height;
        timer = setTimeout("show('" + imagename + "',title,w,h);", timeout);		
    });
    
    
});

