/*
    $Revision: 1.3 $
*/
$(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();
			}
		});
	});
	
	/*******
	** 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');


	//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");
});

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);
}
/*******
** vp **	
*******/

$(document).ready(function() {

    $('.extern').attr('target', '_blank');
    $('.extern').attr('title', 'opent in een nieuw venster');
    createPopupLinks();
    changeDocTAG();
});

window.onload = function() {
    setPrintPopup('DigiDForm_DigiD00S04Step_btnPrint', "/secure/print2.aspx");
    setPrintPopup('DigiDForm_DigiD00S00Step_btnPrint', "/secure/print.aspx");
    setPrintPopup('DigiDForm_DigiD00S12Step_btnPrint', "/secure/print3.aspx");
    setFooter();

}

function createPopupLinks() {
    $(".jsPopup").attr("title", "opent in nieuw venster");
    $(".jsPopup").attr("alt", "opent in nieuw venster");
    $(".jsPopup").css("text-decoration", "none");
    $(".jsPopup").css("cursor", "pointer");
    
    $(".jsPopup").click(function() {
        $(".jsPopup").attr("href", $(".jsPopup").prev("a").attr("href"));
        $(".jsPopup").attr("target", "_blank");
    });
}

function setPrintPopup(ID, page) {
    if (document.getElementById(ID)) {
        var but = document.getElementById(ID);
        but.onclick = function() {
            var win = window.open(page, "printpopup");
            if (win) {
                return false;
            }
            else {
                return true;
            }
        }
    }
}

function setFooter() {
    if (document.getElementById('rightBar')) {
        var rightBarHeight = document.getElementById('rightBar').clientHeight;

        var entries = document.getElementsByTagName('div');
        for (var i = 0; i < entries.length; i++) {
            if (entries[i].className == 'format' && entries[i].id == '') {
                var contentHeight = entries[i].clientHeight;
                if (rightBarHeight > contentHeight) {
                    entries[i].style.height = (rightBarHeight + 35) + 'px';
                }
            }
        }
    }
}


function GetDonorFormOnestatPath() {
    if (document.getElementById('DigiDForm_OnestatUrl')) {
        return document.getElementById('DigiDForm_OnestatUrl').value
    }
    else {
        return '';
    }
}

function GetDonorFormOnestatTitle() {
    if (document.getElementById('DigiDForm_OnestatTitle')) {
        return document.getElementById('DigiDForm_OnestatTitle').value
    }
    else {
        return '';
    }
}

function changeDocTAG() {
    var d = document;
    var sid = "394020";
    var CONTENTSECTION = "";
    var osp_ACTION = "";
    var osp_TRANSACTION = "";
    var osp_AMOUNT = "";
    var osp_PRODUCTCODE = "";
    var osp_PRODUCTGROUP = "";
    var osp_ADCAMPAIGN = "";
    var osp_CUSTOMER = "";
    var osp_CUST0 = "";
    var osp_CUST1 = "";
    var osp_CUST2 = "";
    var osp_CUST3 = "";
    var osp_CUST10 = "";
    var osp_CUST11 = "";
    var osp_URL = GetDonorFormOnestatPath() != '' ? GetDonorFormOnestatPath() : d.URL;
    var osp_Title = GetDonorFormOnestatTitle() != '' ? GetDonorFormOnestatTitle() : d.title;
    var t = new Date();
    var p = "http" + (d.URL.indexOf('https:') == 0 ? 's' : '') + "://stat.onestat.com/stat.aspx?tagver=2&sid=" + sid;
    p += "&url=" + escape(osp_URL);
    p += "&ti=" + escape(osp_Title);
    p += "&section=" + escape(CONTENTSECTION);
    p += "&cma=" + escape(osp_ACTION);
    p += "&cmt=" + escape(osp_TRANSACTION);
    p += "&cmm=" + escape(osp_AMOUNT);
    p += "&cmp=" + escape(osp_PRODUCTCODE);
    p += "&cmg=" + escape(osp_PRODUCTGROUP);
    p += "&cmad=" + escape(osp_ADCAMPAIGN);
    p += "&cmc=" + escape(osp_CUSTOMER);
    p += "&cu0=" + escape(osp_CUST0);
    p += "&cu1=" + escape(osp_CUST1);
    p += "&cu2=" + escape(osp_CUST2);
    p += "&cu3=" + escape(osp_CUST3);
    p += "&cu10=" + escape(osp_CUST10);
    p += "&cu11=" + escape(osp_CUST11);
    p += "&rf=" + escape(parent == self ? document.referrer : top.document.referrer);
    p += "&tz=" + escape(t.getTimezoneOffset());
    p += "&ch=" + escape(t.getHours());
    p += "&js=1";
    p += "&ul=" + escape(navigator.appName == "Netscape" ? navigator.language : navigator.userLanguage);
    if (osp_URL != d.URL) p += "&ol=" + escape(d.URL);
    if (typeof (screen) == "object") {
        p += "&sr=" + screen.width + "x" + screen.height; p += "&cd=" + screen.colorDepth;
        p += "&jo=" + (navigator.javaEnabled() ? "Yes" : "No");
    }
    if (document.getElementById('ONESTAT_TAG')) {
        document.getElementById('ONESTAT_TAG').src = p;
    }
}
