﻿var title="";
var mdX, mdY, py, px;
var popupImgId = 0;
var popupFldId = 0;
var myndFjoldi = 0;

function toggleImage(pxl,it){
    it.style.backgroundPosition="0px "+pxl+"px";
}

function changeFrettaAr(ar){
    var arin=document.getElementById("div_Arin").childNodes;
    for (i=0;i<arin.length;i++){
        if(arin[i].tagName=="DIV"){
            arin[i].style.display="none";
        }
    }
    document.getElementById("div_Ar_"+ar).style.display="block";
}
function hideAdrirVefir(){
    document.getElementById("divVefirAkraness").style.display="none";
    RemoveEvent(document.body,"mouseup",hideAdrirVefir);
}
function adrirVefirClick(){
    document.getElementById("divVefirAkraness").style.display="block";
    AddEvent(document.body,"mouseup",hideAdrirVefir);
}


function AddEvent(tObject, tEvent, tFunc){
	if (document.attachEvent)
	{
		tObject.attachEvent("on"+tEvent, tFunc);
	}
	else {
		tObject.addEventListener(tEvent,tFunc,false);
	}
}
	
function RemoveEvent(tObject, tEvent, tFunc){
	if (document.detachEvent)
	{
		tObject.detachEvent(tEvent, tFunc);
	}
	else
	{
		tObject.removeEventListener(tEvent,tFunc,false);
	}
}

function whatToTo(where){
    if(where!=""){
        document.location.href=where;
    }
}



function showMap(){
    document.getElementById("divCollapse").style.display="none";
    document.getElementById("divFlash").style.display="block";
    document.getElementById("ifrKort").src="/vefsja/index.html";
}












function setActiveStyleSheet(tTitle) {
	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
			a.disabled = true;
			if(a.getAttribute("title") == tTitle){
				a.disabled = false;
			}
		}
	}
	title=tTitle;
	//setButtons(tTitle);
}

function window_load(){
	var cookie = readCookie("style");
	title = cookie ? cookie : getPreferredStyleSheet();
	setActiveStyleSheet(title);
}

function setButtons(tTitle){
    var a=document.getElementById("imgA");
    var aa=document.getElementById("imgAA");
    switch(tTitle){
        case "Size2":
          a.style.backgroundPosition="0px 0px";
          a.style.cursor="pointer";
          aa.style.backgroundPosition="0px 0px";
          aa.style.cursor="pointer";
          break;    
        case "Size3":
          a.style.backgroundPosition="0px 0px";
          a.style.cursor="pointer";
          aa.style.backgroundPosition="0px -28px";
          aa.style.cursor="default";
          break;
        default:
          a.style.backgroundPosition="0px -28px";
          a.style.cursor="default";
          aa.style.backgroundPosition="0px 0px";
          aa.style.cursor="pointer";
    }
}

function fontLarger(){
    if(title=="Size1" || title==""){
        setActiveStyleSheet("Size2");
    }else{
        if(title=="Size2"){
            setActiveStyleSheet("Size3");
        }
    }
}
function fontSmaller(){
    if(title=="Size3"){
        setActiveStyleSheet("Size2");
    }else{
        if(title=="Size2"){
            setActiveStyleSheet("Size1");
        }
    }
}

function window_unload(){
	var title = getActiveStyleSheet();
	createCookie("style", title, 7);
}
function getPreferredStyleSheet() {
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1	&& a.getAttribute("rel").indexOf("alt") == -1 && a.getAttribute("title"))
			return a.getAttribute("title");
	}
	return null;
}
function getActiveStyleSheet() {
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1
			&& a.getAttribute("title")
			&& !a.disabled) return a.getAttribute("title");
		}
	return null;
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function askrift(SID_ID, VEF_ID) {
    gluggi_askrift = window.open('http://nepal.vefurinn.is/fastar/postlistaskraning_1.asp?ASK_SID_ID='+SID_ID+'&VEF_ID='+VEF_ID,'image',"toolbar=0,scrollbars=0,location=0,status=0,menubar=0,width=350,height=300");
}

function getXML(tUrl, tHandler) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = tHandler;
        req.open("GET", tUrl, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = tHandler;
            req.open("GET", tUrl, true);
            req.send();
        }
    }
}

function myndaMappa(id, mId, mSrc, mTxt, mFjoldi) {
    var it = new MakemyndaMappa(id, mId, mSrc, mTxt, mFjoldi)
}


function MakemyndaMappa(id, mId, mSrc, mTxt, mFjoldi) {
    var myself = this;
    this.mappaId = id;
    this.myndId = 0;
    this.myndFjoldi = 0
    this.myndSrc = "";
    this.myndTxt = "";
    this.pos = 1;
    var itm = 0;
    while (document.getElementById("mndMappa_" + itm)) { itm = itm + 1; }
    if (mId) { this.myndId = mId };
    if (mSrc) { this.myndSrc = mSrc };
    if (mTxt) { this.myndTxt = mTxt };
    if (mFjoldi) { this.myndFjoldi = mFjoldi };
    this.AddEvent = function(tObject, tEvent, tFunc) { if (document.attachEvent) { tObject.attachEvent("on" + tEvent, tFunc) } else { tObject.addEventListener(tEvent, tFunc, false) } }
    this.RemoveEvent = function(tObject, tEvent, tFunc) { if (document.detachEvent) { tObject.detachEvent("on" + tEvent, tFunc); } else { tObject.removeEventListener(tEvent, tFunc, false); } }

    this.showLarge = function() {
        setPopupImage(myself.myndId, myself.mappaId, "t", myself.myndFjoldi);
    }
    this.showNext = function() {
        myself.requestXML("r");
    }
    this.showPrev = function() {
        myself.requestXML("l");
    }

    this.requestXML = function(pn) {
        div6.style.display = "block";
        var tUrl = "/getMynd.aspx?MyndId=" + this.myndId + "&FloId=" + this.mappaId + "&Pos=" + pn + "&rnd=" + Math.random();
        if (window.XMLHttpRequest) {
            req = new XMLHttpRequest();
            req.onreadystatechange = this.responseXML;
            req.open("GET", tUrl, true);
            req.send(null);
        } else if (window.ActiveXObject) {
            req = new ActiveXObject("Microsoft.XMLHTTP");
            if (req) {
                req.onreadystatechange = responseXML;
                req.open("GET", tUrl, true);
                req.send();
            }
        }
    }

    this.responseXML = function() {
        if (req.readyState == 4) {
            if (req.status == 200) {
                response = req.responseXML.documentElement;
                var items = response.getElementsByTagName("*");
                if (items.length > 7) {
                    myself.myndId = items[0].firstChild.data;
                    myself.myndSrc = items[3].firstChild.data;
                    try { myself.myndTxt = items[4].firstChild.data; } catch (e) { myself.myndTxt = ""; }
                    var lr = items[8].firstChild.data;
                    myself.pos = myself.pos + parseInt(lr);
                    if (myself.pos == 0) { myself.pos = myself.myndFjoldi }
                    if (myself.pos > myself.myndFjoldi) { myself.pos = 1 }
                    divPos.innerHTML = myself.pos + " af " + myself.myndFjoldi;
                    div5.style.backgroundImage = "url(/images/128/" + myself.myndSrc + ")";
                    div10.innerHTML = myself.myndTxt;
                }
            }
            div6.style.display = "none";
        }
    }

    var div0 = retObj("DIV", "", "mr_outer", "");
    var div1 = retObj("DIV", "", "mr_h1", "");
    var div2 = retObj("DIV", "", "mr_h2", "");
    var div3 = retObj("DIV", "", "mr_h3", "");
    var imgShow = retObj("IMG", "", "mr_gler"); imgShow.src = "/img/rammi/gler.gif"; myself.AddEvent(imgShow, "click", myself.showLarge);
    var imgRight = retObj("IMG", "", "mr_or_r"); imgRight.src = "/img/rammi/or_right.gif"; myself.AddEvent(imgRight, "click", myself.showNext);
    var imgLeft = retObj("IMG", "", "mr_or_l"); imgLeft.src = "/img/rammi/or_left.gif"; myself.AddEvent(imgLeft, "click", myself.showPrev);
    var divPos = retObj("DIV", "", "mr_Pos", "")
    var div4 = retObj("DIV", "", "mr_m1", "");
    var div5 = retObj("DIV", "", "mr_m2", "");
    var div6 = retObj("DIV", "", "mr_m3", "");
    var div7 = retObj("DIV", "", "mr_b1", "");
    var div8 = retObj("DIV", "", "mr_b2", "");
    var div9 = retObj("DIV", "", "mr_b3", "");
    var div10 = retObj("DIV", "", "mr_b4", "");
    div0.appendChild(div1);
    div1.appendChild(div2);
    div2.appendChild(div3);
    div3.appendChild(imgShow);
    div3.appendChild(imgRight);
    div3.appendChild(imgLeft);
    div3.appendChild(divPos);
    div0.appendChild(div4);
    div4.appendChild(div5);
    div5.appendChild(div6);
    div0.appendChild(div7);
    div7.appendChild(div8);
    div8.appendChild(div9);
    div9.appendChild(div10);
    div5.style.backgroundImage = "url(/images/128/" + mSrc + ")"; myself.AddEvent(div5, "click", myself.showLarge)
    div10.innerHTML = mTxt;
    divPos.innerHTML = "1 af " + mFjoldi
    document.write('<span id="mndMappa_' + itm + '"></span>');
    document.getElementById("mndMappa_" + itm).appendChild(div0);
}

function retObj(tType, tId, tClass, tStyle) {
    var it = document.createElement(tType);
    if (tId) { it.setAttribute("id", tId) }
    if (tClass) { it.className = tClass }
    if (tStyle) { it.style.cssText = tStyle }
    return it;
}

function popupDoMove(e) {
    if (!e) { e = event };
    var it = document.getElementById("popup");
    var bx = e.screenX;
    var by = e.screenY;
    it.style.left = (px + bx - mdX) + "px";
    it.style.top = (py + by - mdY) + "px";

}
function popupStopMove(e) {
    funRemoveEvent(document, "mousemove", popupDoMove);
    funRemoveEvent(document, "mouseup", popupStopMove);
}
function popupMove(e) {
    if (!e) { e = event };
    var it = document.getElementById("popup");
    px = it.offsetLeft;
    py = it.offsetTop;
    mdX = e.screenX;
    mdY = e.screenY;
    funAddEvent(document, "mousemove", popupDoMove);
    funAddEvent(document, "mouseup", popupStopMove);

}
function funAddEvent(tObject, tEvent, tFunc) {
    if (document.attachEvent) {
        tObject.attachEvent("on" + tEvent, tFunc)
    } else {
        tObject.addEventListener(tEvent, tFunc, false)
    }
}
function funRemoveEvent(tObject, tEvent, tFunc) {
    if (document.detachEvent) {
        tObject.detachEvent("on" + tEvent, tFunc)
    } else {
        tObject.removeEventListener(tEvent, tFunc, false)
    }
}

function setPopupImage(imgId, fldId, dir) {
    popupFldId = fldId;
    document.getElementById("popupMdl4").style.backgroundImage = "url(/img/null.gif)";
    if (document.getElementById("popup").style.visibility == "hidden") {
        funAddEvent(document, "selectstart", retFalse);
        setPopupSize(320, 240);
        document.getElementById("popup").style.visibility = "visible";
    }

    requestImage(imgId, fldId, dir)
}
function retFalse() {
    return false;
}

function closePopup() {
    document.getElementById("popup").style.visibility = "hidden";
    funRemoveEvent(document, "selectstart", retFalse);
    popupImgId = 0;
    popupFldId = 0;
}
function doNextImage() {
    requestImage(popupImgId, popupFldId, "r");
}
function doPrevImage() {
    requestImage(popupImgId, popupFldId, "l");
}

function requestImage(imgId, fldId, pn) {
    var tUrl = "/getMynd.aspx?MyndId=" + imgId + "&FloId=" + fldId + "&Pos=" + pn + "&size=l&rnd=" + Math.random();
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = this.responseImage;
        req.open("GET", tUrl, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = responseImage;
            req.open("GET", tUrl, true);
            req.send();
        }
    }
}

function setPopupSize(x, y) {
    document.getElementById("popup").style.width = parseInt(x + 28) + "px";
    document.getElementById("imgPopup").style.width = x + "px";
    document.getElementById("imgPopup").style.height = y + "px";
    document.getElementById("popupMdl2").style.width = parseInt(x + 20) + "px";
    if (popupImgId == 0) {
        setPopupPosition();
    }
}
function setPopupPosition() {
    var myself = document.getElementById("popup");
    myself.style.left = (document.body.clientWidth - myself.clientWidth) / 2 + "px";
    myself.style.top = (document.body.clientHeight - myself.clientHeight) / 3 + "px";
}
function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft;
            obj = obj.offsetParent;
        } 
    }
    else if (obj.x) {
        curleft += obj.x;
    }
    return curleft + document.body.offsetLeft;
};

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop;
            obj = obj.offsetParent;
        } 
    }
    else if (obj.y) {
        curtop += obj.y;
    }
    return curtop + document.body.offsetTop;
};

function responseImage() {
    if (req.readyState == 4) {
        if (req.status == 200) {
            try {
                response = req.responseXML.documentElement;
                var items = response.getElementsByTagName("*");
                if (items.length > 7) {
                    document.getElementById("popupMdl4").style.backgroundImage = "url(/images/" + items[3].firstChild.data + ")";
                    setPopupSize(parseInt(items[1].firstChild.data), parseInt(items[2].firstChild.data));
                    document.getElementById("popupHeading").innerHTML = items[9].firstChild.data;
                    try { document.getElementById("popupTxt").innerHTML = items[4].firstChild.data; } catch (e) { document.getElementById("popupTxt").innerHTML = ""; }
                    popupImgId = parseInt(items[0].firstChild.data)

                    //      alert(items[3].firstChild.data);
                    //       myself.myndId=items[0].firstChild.data;
                    //       myself.myndSrc=items[3].firstChild.data;
                    //       try{myself.myndTxt=items[4].firstChild.data;}catch(e){myself.myndTxt="";}
                    var lr = items[8].firstChild.data;
                    var pos = pos + parseInt(lr);
                    if (pos == 0) { pos = myndFjoldi }
                    if (pos > myndFjoldi) { pos = 1 }

                    //alert(items[3].firstChild.data);
                    // lr = 0 í fyrsta
                    // +1 áfram
                    // -1 afturábak
                    //items[3].firstChild.data = mynd_xxxx.jpg
                    
                    document.getElementById("popupPos").innerHTML = pos + " af " + myndFjoldi;
                    //       div5.style.backgroundImage="url(/images/128/"+myself.myndSrc+")";
                    //       div10.innerHTML=myself.myndTxt;
                }
            } catch (e) {
                alert("Því miður hefur komið upp villa\nog því ekki hægt að birta myndina.\n\nVinsamlegast reynið aftur síðar");
                closePopup()
            }
        }
        //div6.style.display="none";
    }
}