/*
$Revision: 1.31 $
*/
$(document).ready(function() {

    /*******
    ** vp - Panel toggle  mijn dossier page - (c) - 0.0.1 **
    *******/
    $('input.sameAddress').each(function(i) {
        if ($(this).is(':checked')) {
            $('div.sameAddress').filter(function(index) {
                return index == i
            }).hide();
        }
        else {
            $('div.sameAddress').filter(function(index) {
                return index == i
            }).show();
        }

        $(this).click(function(e) {
            if ($(this).is(':checked')) {
                $('div.sameAddress').filter(function(index) {
                    return index == i
                }).hide();
            }
            else {
                $('div.sameAddress').filter(function(index) {
                    return index == i
                }).show();
            }
        });
    });

    /*******
    ** Panel toggle wel/niet werkzaam op praktijkadres **
    *******/
    $('input.hasWorkAddress').each(function(i) {
        if ($(this).is(':checked')) {
            $('div.hasWorkAddress').filter(function(index) {
                return index == i
            }).show();
        }
        else {
            $('div.hasWorkAddress').filter(function(index) {
                return index == i
            }).hide();
        }

        $(this).click(function(e) {
            if ($(this).is(':checked')) {
                $('div.hasWorkAddress').filter(function(index) {
                    return index == i
                }).show();
            }
            else {
                $('div.hasWorkAddress').filter(function(index) {
                    return index == i
                }).hide();
            }
        });
    });

    /*******
    ** vp - Panel toggle  mijn dossier page - (c) - 0.0.1 **
    *******/
    $('.panel').each(function() {
        $(this).find('h2').find('a').click(function(e) {
            $(this).blur();
            if ($(this).parent().parent().hasClass("openPanel")) {
                $(this).parent().parent().removeClass("openPanel")
            }
            else {
                //alert('ja');
                $(this).parent().parent().addClass("openPanel")
            }
            e.preventDefault();
        });
    });


    /*******
    ** vp - forward dropdown homepage box - (c) - 0.0.1 **
    *******/
    $('#formDropdown1').submit(function(e) {
        $('#dropdown1 option:selected').each(function() {
            if ($(this).val() != '') {
                document.location.href = $(this).val();
                //alert($(this).val());
                return true;
            }


        });

        e.preventDefault();

    });


    /*******
    ** vp - print functionality - (c) - 0.0.1 **
    *******/
    $('.contentNav').find('li').each(function() {
        if ($(this).hasClass('print')) {
            $(this).click(function() {
                window.print();
                return false;
            });
            $(this).toggle();
        }
    });


    /*******
    ** vp - add target = _blank - (c) - 0.0.1 **
    *******/
    $('.extern').attr('target', '_blank');
    $('.pdffile').attr('target', '_blank');

    $('.pdffile').attr('onClick', '_gaq.push([\'_trackPageview\', \'' + $('.pdffile').attr('href') + '\']);');

    //pick one out of five stylesheets for the font size
    //and save in a cookie	//createCookie("fontsize","",-1); //delete cookie - only for testing, do not remove comment!
    var c = readCookie("fontsize");
    if (c) {
        setFont(c);
        makeControls(c);
    }
    else {
        createCookie("fontsize", 3, "");
        makeControls(3);
    }

    /*******
    ** vp - go to link functionality - (c) - 0.0.1 **
    *******/
    $('#gobutton').click(function(e) {
        return go(this.previousSibling.previousSibling.value);
    });


    //Set print popup
    //setPrintPopup("btnPrint", "/secure/print.aspx");

    addTracker();
});

function setPrintPopup(ID, page) {
    //var but = $(":asp('" + ID + "')");
    var but = $("*[id$='" + ID + "']")
    if (but.length) {
        but.click(function() {
            var win = window.open(page, "printpopup");
            if (win) {
                return false;
            }
            else {
                return true;
            }
        });
    }
}

function go(strValue) {
    window.location.href = strValue;
    return false;
}


function makeControls(c) {
    //draw the "Tekstgrootte" header and the plus and minus buttons
    var divTag = document.getElementById("textsize");
    var ulTag = document.createElement('ul');
    var liTag = document.createElement('li');
    var liTag1 = document.createElement('li');
    var liTag2 = document.createElement('li');
    var aTag1 = document.createElement('a');
    var aTag2 = document.createElement('a');
    var liText = document.createTextNode('Tekstgrootte');
    var aText1 = document.createTextNode('-');
    var aText2 = document.createTextNode('+');

    liTag.appendChild(liText);
    aTag1.appendChild(aText1);
    aTag2.appendChild(aText2);

    liTag1.appendChild(aTag1);
    liTag2.appendChild(aTag2);
    ulTag.appendChild(liTag);
    ulTag.appendChild(liTag1);
    ulTag.appendChild(liTag2);
    divTag.appendChild(ulTag);

    aTag1.setAttribute("id", "smaller");
    aTag2.setAttribute("id", "larger");
    aTag1.setAttribute("title", "smaller");
    aTag2.setAttribute("title", "larger");

    if (c > 1) aTag1.setAttribute("href", "#textsmaller");
    else aTag1.className = "inactive";

    if (c < 5) aTag2.setAttribute("href", "#textlarger");
    else aTag2.className = "inactive";

    addListener(aTag1, -1);
    addListener(aTag2, 1);
}

//the actual resizing (changing of stylesheet)

function resize(value) {
    //calculate the new size
    var c = readCookie("fontsize");
    if (c) {
        //get the number from the cookie and make it an int
        var oldSize = readCookie("fontsize") - 0;
    }
    else {
        //if something went wrong (e.g. IE6, in multipleIEs), at least the current font size is 3
        oldSize = 3;
    }
    //newsize is (oldsize + 1) or (oldsize + -1)
    var newSize = oldSize + value;
    if (0 < newSize && newSize < 6) {
        setFont(newSize);
        //test if we should deactivate a button (to prevent sizes bigger than 5 or smaller than 0) or reactivate one
        if (oldSize == 4 && newSize == 5) removeLink("larger");
        if (oldSize == 2 && newSize == 1) removeLink("smaller");
        if (oldSize == 5 && newSize == 4) addLink("larger", 1);
        if (oldSize == 1 && newSize == 2) addLink("smaller", -1);
    }
}

function setFont(fontsize) {
    //get the link tag by id and change the href, update the cookie
    var l = document.getElementById("fontsize");
    l.href = "\/static\/css\/tekstgrootte" + fontsize + ".css";
    createCookie("fontsize", fontsize, "");
}

//functions for the links

function removeLink(id) {
    //deactivate a button to prevent sizes bigger than 5 or smaller than 0
    var el = document.getElementById(id);
    var nr = 1;
    if (id == "smaller") nr = -1;
    el.removeAttribute("href");
    el.className = "inactive";
}

function addLink(id, nr) {
    //activate a button if we need it again
    var el = document.getElementById(id);
    el.setAttribute("href", "#text" + id);
    el.className = "";
}

function addListener(tag, param) {
    //add eventListener with the resize function and the correct param
    if (tag.addEventListener) {
        tag.addEventListener('click', function() { resize(param); }, false);
    }
    else if (tag.attachEvent) {
        tag.attachEvent('onclick', function() { resize(param); });
    }
}

//cookie functions

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 var 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 eraseCookie(name) {
    createCookie(name, "", -1);
}

