/**********************************************************
BILDVISNING
**********************************************************/
var currentImage = 0;
var currentList = 0;
function showImage(src, id, listId) {
    currentImage = id - 1;
    currentList = listId;
    
    var bg = document.getElementById('overlay');
    var imgBox = document.getElementById('imageContainer');
    var oImg = document.getElementById('imgObj');
    var bodyHeight;
    var xPos = winWidth() / 2;
    var yPos = winHeight() / 2;

    bg.onclick = '';

    bodyHeight = getBodyHeight();
    if (winHeight() > getBodyHeight()) {
        bodyHeight = winHeight();
    }

    setOpacity(bg, 0);
    setOpacity(oImg, 0);

    bg.style.width = winWidth() + 'px';
    bg.style.height = bodyHeight + 'px';
    bg.style.display = 'inline';
    $(bg).animate({ opacity: 0.7 }, 100, function() { });

    imgBox.style.width = '0';
    imgBox.style.height = '0';
    imgBox.style.display = 'inline';
    imgBox.style.left = xPos + 'px';
    imgBox.style.top = yPos + 'px';


    var pos = scrollY()+50;

    oImg.onload = function () {
        $(imgBox).animate({ width: oImg.width, height: oImg.height + 20, left: xPos - (oImg.width / 2), top: pos }, 150, function () { });
        setTimeout(fixImageSize, 250);
    };
    oImg.src = src;
}

function switchImage(src, id) {
    var imgBox = document.getElementById('imageContainer');
    var oImg = document.getElementById('imgObj');

    var xPos = winWidth() / 2;
    var pos = scrollY() + 50;

    oImg.src = src;
    oImg.onload = function () {
        $(imgBox).animate({ width: oImg.width, height: oImg.height + 20, left: xPos - (oImg.width / 2), top: pos }, 150, function () { });
        setTimeout(fixImageSize, 250);
    }
}

function fixImageSize() {
    var bg = document.getElementById('overlay');
    var imgBox = document.getElementById('imageContainer');
    var oImg = document.getElementById('imgObj');

    $(imgBox).animate({ width: oImg.width, height: oImg.height + 20 }, 150, function () { });
    $(oImg).animate({ opacity: 1 }, 150, function () { });

    var delay = function () { $(oImg).animate({ opacity: 1 }, 150, function () { }); }
    setTimeout(delay, 150);

    enableNav(); 
    bg.onclick = closeImage;
}

function enableNav() {
    document.getElementById('overlay').onclick = closeImage;

    document.getElementById('nextImage').onclick = nextImage;
    document.getElementById('prevImage').onclick = prevImage;

    document.getElementById('nextImage').style.color = '#000000';
    document.getElementById('prevImage').style.color = '#000000';
}
function disableNav() {
    document.getElementById('overlay').onclick = '';

    document.getElementById('nextImage').onclick = '';
    document.getElementById('prevImage').onclick = '';

    document.getElementById('nextImage').style.color = '#cccccc';
    document.getElementById('prevImage').style.color = '#cccccc';
}
function prevImage() {
    disableNav();
    var listCount = 0;
    if (catArray.length > 0) {
        listCount = catArray[currentList-1][0].length - 1;
    } else {
        listCount = imgArray.length - 1;
    }

    currentImage--;
    //alert(currentImage + ' / ' + listCount);
    if (currentImage < 0) {
        currentImage = listCount;
    }

    var image
    if (catArray.length > 0) {
        image = catArray[currentList - 1][0][currentImage];
    } else {
        image = imgArray[currentImage];
    }
    switchImage(image.split(',')[0], image.split(',')[1]);
}
function nextImage() {
    disableNav();

    var listCount = 0;
    if (catArray.length>0) {
        listCount = catArray[currentList - 1][0].length - 1;
    } else {
        listCount = imgArray.length - 1;
    }

    currentImage++;
    //alert(currentImage + ' / ' + listCount);
    if (currentImage > listCount) {
        currentImage = 0;
    }
    //alert(currentImage);
    var image;
    if (catArray.length > 0) {
        image = catArray[currentList - 1][0][currentImage];
    } else {
        image = imgArray[currentImage];
    }
    switchImage(image.split(',')[0], image.split(',')[1]);
}
function closeImage() {
    currentImage = 0;
    var bg = document.getElementById('overlay');
    var img = document.getElementById('imageContainer');
    var oImg = document.getElementById('imgObj');
    
    bg.style.display = 'none';
    img.style.display = 'none';
    bg.onclick = '';
}

/**********************************************************
EVENTS
**********************************************************/
onresize = function() {
    var bg = document.getElementById('overlay');
    var imgBox = document.getElementById('imageContainer');

    imgBox.style.left = winWidth() / 2 - 350 + 'px';
    imgBox.style.top = winHeight() / 2 - 250 + 'px';
}

/******************************************************************************
OTHER
******************************************************************************/

function setPageHeight() {
    if (document.getElementById('col1')) {
        document.getElementById('col1').style.height = document.getElementById('main').offsetHeight + 'px';
    }
    if (document.getElementById('col3')) {
        document.getElementById('col3').style.height = document.getElementById('main').offsetHeight + 'px';
    }
}

function setFooterPosition() {

}

function hideValue(control, value) {
    if (control.value == value) {
        control.value = '';
    }
}
function resetValue(control, value) {
    if (control.value.length == 0) {
        control.value = value;
    }
}

function trim(value) {

    return LTrim(RTrim(value));

}

onresize = function() {
    if (document.getElementById('overlay')) {
        document.getElementById('overlay').style.width = winWidth() + 'px';
        document.getElementById('overlay').style.height = winHeight() + 'px';
    }
}

/******************************************************************************
HELPER FUNCTIONS
******************************************************************************/
function getBodyHeight() {
    var bodyHeight;
    if (document.body.offsetHeight) {
        bodyHeight = document.getElementById('wrap').offsetHeight;
    } else {
        bodyHeight = document.getElementById('wrap').clientHeight;
    }
    return bodyHeight;
}
function scrollY() {
    var y = 0;
    if (document.documentElement.scrollTop) {
        y = document.documentElement.scrollTop;
    } else if (document.body.scrollTop) {
        y = document.body.scrollTop;
    }

    return y
}
var Browser = {
    Version: function() {
        var version = 999;
        // we assume a sane browser    
        if (navigator.appVersion.indexOf("MSIE") != -1)
        // bah, IE again, lets downgrade version number
            version = parseFloat(navigator.appVersion.split("MSIE")[1]);
        return version;
    }
}
function winWidth() {
    if (parseInt(navigator.appVersion) > 3) {
        if (navigator.appName == "Netscape") {
            winW = window.innerWidth;
        }
        if (navigator.appName.indexOf("Microsoft") != -1) {
            winW = document.body.offsetWidth;
        }
    }
    return winW
}
function winHeight() {
    if (parseInt(navigator.appVersion) > 3) {
        if (navigator.appName == "Netscape") {
            winH = window.innerHeight;
        }
        if (navigator.appName.indexOf("Microsoft") != -1) {
            winH = document.body.offsetHeight;
        }
    }
    return winH
}
function setOpacity(testObj, value) {
    testObj.style.opacity = value / 10;
    testObj.style.filter = 'alpha(opacity=' + value * 10 + ')';
}
function setText(obj, text) {
    if (document.all) {
        obj.innerText = text;
    } else {
        obj.textContent = text;
    }
}


/******************************************************************************
IMAGE TOGGLER
******************************************************************************/

var currentImg = 0;

function imgToggler() {
    var toggler = document.getElementById('toggler');
    $(toggler).animate
    (
        {
            opacity: 0
        },
        500,
        function() {
            toggler.style.background = 'url(' + imageList[currentImg][0] + ') no-repeat';
            $(toggler).animate({ opacity: 1 }, 500, function() { });
        }
    );

    currentImg++;
    if (currentImg == imageList.length)
        currentImg = 0;
}


/******************************************************************************
TEXT POPUP
******************************************************************************/
function infoWindow(id, src, headline) {
    var overlay = document.getElementById('overlay');
    var container = document.getElementById('textContainer');
    var content = document.getElementById('popup_container');
    var xPos = winWidth() / 2;
    var yPos = (winHeight() / 2) + scrollY();
    
    var bodyHeight = getBodyHeight();
    if (winHeight() > getBodyHeight()) {
        bodyHeight = winHeight();
    }
    
    setOpacity(overlay, 0);
    overlay.onclick = closeTextPopup;
    overlay.style.display = 'inline';
    container.style.width = 0 + 'px';
    container.style.height = 0 + 'px';
    overlay.style.width = winWidth() + 'px';
    overlay.style.height = bodyHeight + 'px';

    container.style.top = yPos + 'px';
    container.style.left = xPos + 'px';
    
    var delay;
    $(overlay).animate({ opacity:0.7 }, 0, function() { });
    delay = function() { container.style.display = 'inline'; $(container).animate({ width: 550, height: 500, left: xPos - 275, top: yPos - 220 }, 100, function() { }); };
    setTimeout(delay, 0);
    delay = function() { content.style.display = ''; };
    setTimeout(delay, 200);

    var headlineElm = document.getElementById('popup_headline');
    var descriptionElm = document.getElementById('popup_description');
    var txtContent = document.getElementById('info_description' + id);
    var imgContent = document.getElementById('info_img' + id);
    
    if (src != null && src != '' && src != 'undefined') {
        descriptionElm.innerHTML = imgContent.innerHTML;
    }

    if (document.all) {
        headlineElm.innerText = headline;
        descriptionElm.innerHTML += txtContent.innerHTML;
    } else {
        headlineElm.textContent = headline;
        descriptionElm.innerHTML += txtContent.innerHTML;
    }

}
function closeTextPopup() {
    var overlay = document.getElementById('overlay');
    var container = document.getElementById('textContainer');
    var content = document.getElementById('popup_container');

    overlay.style.display = 'none';
    container.style.width = 0;
    container.style.height = 0;
    container.style.display = 'none';
    content.style.display = 'none';

    var headlineElm = document.getElementById('popup_headline');
    var descriptionElm = document.getElementById('popup_description');
    if (document.all) {
        headlineElm.innerHTML = '';
        descriptionElm.innerHTML = '';
    } else {
        headlineElm.innerHTML = '';
        descriptionElm.innerHTML = '';
    }
}

/******************************************************************************
TREE VIEW
******************************************************************************/
function subMenuExpander(header, list, count, src) {

    size = (count * 30)
    if (list.style.height == '0px' || list.style.height == '') {
        header.getElementsByTagName('img')[0].src = src + 'Images/arrow_down.gif';

        $(list).animate
        (
            {
                height: size
            },
            200,
            function() { }
        );
    } else {
        header.getElementsByTagName('img')[0].src = src + 'Images/arrow_up.gif';
        $(list).animate
        (
            {
                height: '0'
            },
            200,
            function() { }
        );
    }
}

function listViewItem_OnClick(name, type, mark) {
    var elms = new Array();
    var mark = document.getElementById(mark);

    if (navigator.appName.indexOf("Microsoft") != -1) {
        elms = getElementsByName_iefix(type, name);
    } else {
        elms = document.getElementsByName(name);
    }

    var i = 0;
    for (i; i < elms.length; i++) {
        var row = elms[i];
        if (row.style.display == 'none') {
            row.style.display = '';
            setOpacity(row, 0);
            $(row).animate
            (
                {
                    opacity: 1
                },
                1000,
                function() { }
            );
            if (mark) {
                setText(mark,'- ');
            }
            
        } else {
            row.style.display = 'none';
            if (mark) {
                setText(mark, '+ ');
                
            }
        }
    }
}
function getElementsByName_iefix(tag, name) {

    var elem = document.getElementsByTagName(tag);
    var arr = new Array();
    for (i = 0, iarr = 0; i < elem.length; i++) {
        att = elem[i].getAttribute("name");
        if (att == name) {
            arr[iarr] = elem[i];
            iarr++;
        }
    }
    return arr;
}

var isNS = (navigator.appName == "Netscape") ? 1 : 0;
if (navigator.appName == "Netscape") document.captureEvents(Event.MOUSEDOWN || Event.MOUSEUP);
function mischandler() {
    return false;
}
function mousehandler(e) {
    var myevent = (isNS) ? e : event;
    var eventbutton = (isNS) ? myevent.which : myevent.button;
    if ((eventbutton == 2) || (eventbutton == 3)) return false;
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail")
		    return false
		 }

 		 return true					
	}

function ValidateForm(){
    var emailID = document.getElementById('news_email');
    var retVal = true;

    if ((emailID.value == null) || (emailID.value == "")) {
        alert("Please Enter your Email")

        retVal = false;
    } else if (echeck(emailID.value) == false) {
        emailID.value = ""
        emailID.focus()

        retVal = false;
    }

    return retVal
}

function writeEmail(alias, domain) {

    if (document.getElementById('mailContent').innerHTML.indexOf('Email:') > 0) {
        //alert('replace');
        var string1 = alias;
        var string2 = "@";
        var string3 = domain;
        var string4 = string1 + string2 + string3;

        //newHTML = newHTML.replace("Email:", "Email: <a href=" + "mail" + "to:" + string1 + string2 + string3 + ">" + string4 + "</a>");
        document.getElementById('mailContent').innerHTML = document.getElementById('mailContent').innerHTML.replace("Email:", "Email: <a href=" + "mail" + "to:" + string1 + string2 + string3 + ">" + string4 + "</a>");
    }
}
