﻿var movedThing = null;
var previousMoved = null;
var area = null;
var xO = 0;
var yO = 0;
var openBookContent=null;
var lastOpenBook = null;

function moveThing(e){
    if (!e) { e = window.event } 
    var mX=e.clientX;
    var mY=e.clientY;
    var minX = 0;
    var minY = 0;
    var maxX = 2000;
    var maxY = 2000;
    if (area){
        minX=area.offsetLeft+5;
        minY=area.offsetTop+5;
        maxX = minX+area.clientWidth-5;
        maxY = minY+area.clientHeight-5;
    }
    if(movedThing){
        maxX=maxX-movedThing.clientWidth-5;
        maxY=maxY-movedThing.clientHeight-5;
        var cX = (mX-xO);
        var cY = (mY-yO);
        if (cX<minX)cX=minX;
        if (cY<minY)cY=minY;
        if (cX>maxX)cX=maxX;
        if (cY>maxY)cY=maxY;
        movedThing.style.left=cX + "px";
        movedThing.style.top=cY + "px";
    }
    return false;
}

function pop()
{
    if(movedThing)
    {
        movedThing.style.zIndex=10;
        if(previousMoved)
        {
            previousMoved.style.zIndex=0;
        }
        previousMoved = movedThing;
    }
}

function pickupThing(e){
    if(!e)e = window.event;//I think this is some IE crap.
    movedThing = e.target;
    if(!movedThing) movedThing = e.srcElement;//I think this is some IE crap.
    area=document.getElementById("main");
    if (movedThing){
        var cls = movedThing.className
        if (cls.indexOf("drag")>=0){
            var note=document.getElementById("chipInfo");
            if(note)document.body.removeChild(note);
            movedThing.style.postion="absolute";
            pop();
           
            xO=e.clientX-parseInt(movedThing.offsetLeft);//grab the offset into the item
            yO=e.clientY-parseInt(movedThing.offsetTop);//for y as well
            document.onmouseup=dropThing;
            document.onmousemove=moveThing;
        }
        else{
            movedThing = null;
        }
    }
    return false;
}

function dropThing(){
    movedThing = null;
    area = null;
    document.onmouseup=null;
    document.onmousemove = null;
    return false;
}

function checkPosition(e)
{
    if(e)
    {
        area=document.getElementById("main");
        if(area)
        {  
            if((e.offsetTop+e.height)>(area.offsetTop+area.clientHeight))e.style.top=area.offsetTop+area.clientHeight-e.height-5 +"px";
            if((e.offsetLeft+e.width)>(area.offsetLeft+area.clientWidth))e.style.left=area.offsetLeft+area.clientWidth-e.width-5 +"px";
        }
    }
}

function imgsize(e)
{
    if(e)
    {
       if (e.width==200){
        e.width=500;
       }
       else{
        e.width=200;
       }
       checkPosition(e);
    }
}

function init()
{
    var inf = document.getElementById("chipInfo");
    if(inf)
    {
        inf.style.backgroundImage="url(images/drag.gif)";
        
    }
    aboutYou();
}

function getBrowserDetails()
{
    var r=["nothing","nothing"];
    if(navigator.userAgent.match(/Firefox/))
    {
        r[0]="Firefox";
    }
    else if(navigator.userAgent.match(/MSIE/))
    {
        r[0]="IE";
    }
    else if(navigator.userAgent.match(/Safari/))
    {
        r[0]="Safari";
    }
    
    if(navigator.userAgent.match(/Windows/))
    {
        r[1]="Windows";
    }
    else if(navigator.userAgent.match(/Mac/))
    {
        r[1]="Mac OS";
    }
    else if(navigator.userAgent.match(/Linux/))
    {
        r[1]="Linux";
    }
        else if(navigator.userAgent.match(/Unix/))
    {
        r[1]="Unix";
    }
    return r;
}

function getWhereFrom()
{
    var f="nowhere!";
    if (document.referrer)
    {
        var s=document.referrer.split("/");
        if(s[2]=="www.5000wpm.co.uk")f="here!";
        else f=s[2];
    } 
    return f;
}

function aboutYou()
{
    var ayFrom = document.getElementById("whereFrom");
    var ayBrowser = document.getElementById("browser");
    var ayOn=document.getElementById("on");
    if (ayFrom && ayBrowser)
    {
        ayFrom.innerHTML=getWhereFrom();
        var b=getBrowserDetails();
        ayBrowser.innerHTML=b[0];
        ayOn.innerHTML=b[1] +" ";
    }
}

document.onmousedown=pickupThing;
        