//CALL-22119 Analytics per tabblad (op een cursuspagina)
function track_page(opleiding, tab) {
    pageTracker._trackEvent('Opleidingen', opleiding, tab);
    //alert('opleiding:' + opleiding + '|tab:' + tab);
}


function sizeText(size) {
    var link;
    var url;
    var pos;

    $('head link').each(function(i) {
        url = $(this).attr('href');
        pos = url.indexOf('fontsize');
        if (pos > 0) {
            link = $(this);
            url = url.substring(0, pos + 8) + size + url.substring(pos + 9);

            $(this).attr('href', url);
        }
    });
    document.cookie = "lettergrootte=" + size + ";expires=31/12/2099 00:00:00";
    return false;
}

var education_id = "";

var total_divs = 0;
var className = 'items';
function fadeEngine(x) {
    var y = x;
    if (x == total_divs) y = 1; else y++;
    $("#fade" + x).fadeOut(2000);
    $("#fade" + y).fadeIn(2000);

    setTimeout('fadeEngine(' + y + ')', 5000);
}

function setTrainingAlsOpleidingData(url) {
    $.post(url, function(data) {
        //$('.trainingAlsOpleidingData').fadeOut("fast");
        $('.trainingAlsOpleidingData').html(data);
        //$('.trainingAlsOpleidingData').fadeIn("fast");
        initializeTrainingAlsOpleidingTable();
        setSelection();
    });
}

function setSelection() {
    var avond = $("#hiddenAvond").val();
    var jaar = $("#hiddenJaar").val();
    var locatie = $("#hiddenLocatie").val();
    if (avond != "") {
        if (avond == "1")
            $("a:contains('Avond')").addClass("selectie");
        if (avond == "0")
            $("a:contains('Dag')").addClass("selectie");
    }
    if (jaar != "") {
        $("a:contains('" + jaar + "')").addClass("selectie");
    }
    if (locatie != "") {
        $("a:contains('" + locatie + "')").addClass("selectie");
    }
}

function filterTrainingAlsOpleidingData() {
    var param = "";
    var avond = $("#hiddenAvond").val();
    var jaar = $("#hiddenJaar").val();
    var locatie = $("#hiddenLocatie").val();
    if (avond != "") {
        param += "&avond=" + avond;
    }
    if (jaar != "") {
        param += "&jaar=" + jaar;
    }
    if (locatie != "") {
        param += "&locatie=" + locatie;
    }
    var url = "http://" + location.host + "/?id=JSON_DATABYEDUCATION&education=" + education_id + param;
    setTrainingAlsOpleidingData(url);
    return false;
}

function initializeTrainingAlsOpleidingTable() {
    $('.toggleSelectionBox').click(function() {
        $(this).next("div.selectionBox").toggle("slow");
        return false;
    });

    $(".dagavond").click(function() {
        var avond = $(this).html();
        if (avond == "Alles") {
            $("#hiddenAvond").val("");
        }
        else if (avond == "Dag") {
            $("#hiddenAvond").val("0");
        }
        else {
            $("#hiddenAvond").val("1");
        }
    });

    $(".jaar").click(function() {
        var jaar = $(this).html();
        if (jaar == "Alles") {
            $("#hiddenJaar").val("");
        }
        else {
            $("#hiddenJaar").val(jaar);
        }
    });

    $(".locatie").click(function() {
        var locatie = $(this).html();
        if (locatie == "Alles") {
            $("#hiddenLocatie").val("");
        }
        else {
            $("#hiddenLocatie").val(locatie);
        }
    });

    $('.selection').click(function() {
        $(this).closest('.selectionBoxContent').children('a').removeClass('selectie');
        $(this).addClass("selectie");
        $(this).closest('.selectionBoxContent').toggle("slow");
        window.setTimeout(filterTrainingAlsOpleidingData, 100);
        return false;
    });
	
	$(".locations").each(function(){
		var originalText = $(this).html();
		//replace 's-Hertogenbosch so that it gets sorted correct
		var text = $(this).html();
		text = text.replace("'s-Hertogenbosch","S-Hertogenbosch");
		var locations = text.split(',');
		var locations2 = new Array();
		$(this).html('');
		for(i=0; i<locations.length;i++){
			locations2[i] = jQuery.trim(locations[i]);
		}
		locations = locations2.sort();
		var result = ""
		for(i = 0; i<locations.length;i++){
			result += locations[i];
			if(i != locations.length - 1){
				result += ", ";
			}
		}
		
		//re-replace s-Hertogenbosch so that it gets showed correct
		result = result.replace("S-Hertogenbosch","<nobr>'s-Hertogenbosch</nobr>");
		$(this).html(result);

	});

    $(".meerData").each(function() {
        var result = "";
		var originalText = $(this).html();
        var split = $(this).html().split(',');
        if (split.length > 3) {
            /*var yearblock = split[split.length - 1];
            var yearsplit = jQuery.trim(yearblock).split(' ');
            var year = yearsplit[2];
            var i = 0;
            for (i = 0; i < 3; i++) {
                result += jQuery.trim(split[i]) + ", ";
            }
            result = result.substr(0, result.length - 2);
            result += "<span class='courseData hidden'>, </span> <span class='courseData'>" + year + "</span>";

            result += "<span class='courseData hidden'>";
            for (i = 3; i < split.length; i++) {
                result += "" + jQuery.trim(split[i]) + ", ";
            }
            result = result.substr(0, result.length - 2);
            result += "</span><br />";
            result += "<a href='#' class='showAllCourseData courseData'>Toon overige data</a>";
            result += "<a href='#' class='showAllCourseData courseData hidden'>Verberg data</a>";
            $(this).html(result);*/
			
			var yearblock = split[split.length - 1];
            var yearsplit = jQuery.trim(yearblock).split(' ');
            var year = yearsplit[2];
			split[split.length - 1] = yearsplit[0] + ' ' + yearsplit[1];
			
			
			var shortVersion = "";
			for(x = 0; x < 3;x++){
			    //differentiate between the last number (appended by the year) and normal dates
			    if(x==2){
			        shortVersion += "<div class='singleDateLast'>" + jQuery.trim(split[x])+ " " + year;
			        shortVersion += "</div>";
			    }
			    else{
			        shortVersion += "<div class='singleDate'>" + jQuery.trim(split[x]);
			        shortVersion += ",";
			        shortVersion += "</div>";
			    }
			}
			
			var longVersion = ""
			for(x = 0; x < split.length;x++){
    			//differentiate between the last number (appended by the year) and normal dates
			    if(x==split.length-1){
			        longVersion += "<div class='singleDateLast'>" + jQuery.trim(split[x])+ " " + year;
			        longVersion += "</div>";
			    }
			    else{
			        longVersion += "<div class='singleDate'>" + jQuery.trim(split[x]);
			        longVersion += ",";
			        longVersion += "</div>";
			    }			
			}
			
			
			$(this).html('');
			datesLong = $('<div class="datesLong"></div>').appendTo(this);
			datesLong.html(longVersion);
			datesShort = $('<div class="datesShort"></div>').appendTo(this);
			datesShort.html(shortVersion);
			
			hideDates = $("<a href='#' class='clearboth'>Verberg data</a>").appendTo(datesLong);
			showDates = $("<a href='#' class='clearboth'>Toon overige data</a>").appendTo(datesShort);
			setHandlersForDates(datesShort,datesLong,hideDates,showDates);
        }

    });
	
	function setHandlersForDates(datesShort, datesLong, hideDates, showDates){
			datesLong.hide();
			hideDates.click(function(){
				datesLong.hide();
				datesShort.show();
				return false;
			});
			showDates.click(function(){
				datesShort.hide()
				datesLong.show();
				return false;
			});		
	}
    
    $(".showAllCourseData").click(function() {
        $(this).closest(".meerData").children(".courseData").toggle();
        return false;
    });


    $(".inschrijfLink").click(function() {
        var locatie = $("#hiddenLocatie").val();
        if (locatie != null && locatie != -1 && locatie != '') {
            $(this).attr("href", $(this).attr("href") + "&locatie=" + locatie);
        }

    });
}

$(document).ready(function() {


    $('.newwindowlink').attr("target", "_blank");
    $(".newwindowlink a").attr("target", "_blank");

    $('.submitbtn').hover(function() {
        $(this).addClass("hover");
    }, function() {
        $(this).removeClass("hover");
    });
    
   var dateSwitch =0
   $(".showAllDates").click(function() {
    if(dateSwitch ==0){
    dateSwitch =1;
     $("tr.hidden").css("display","block");
     }else{
      dateSwitch =0;
     $("tr.hidden").css("display","none");
     }
       
        $(".showAllDates").toggle();
        return false;
    });

    $("." + className).attr("id", function(arr) {
        total_divs++;
        return "fade" + (arr + 1);
    });
    fadeEngine(0);

    /* Tooltip engine */
    $('.tooltip-show').hover(function() {
        $('.tooltip').hide();

        var offset = $("#" + this.id).offset();

        $("#tt-" + this.id).css({
            'left': offset.left - 250,
            'top': offset.top - 90,
            'position': 'absolute',
            'height': '100px',
            'width': '300px'
        });

        $("#tt-" + this.id + " div").addClass("content");
        $(".tooltip-close").fadeIn("fast");
        $("#tt-" + this.id).fadeIn();

    },
	    function() {
	        $('div.tooltip').hide();
	    }
	);
    /*end*/

    /* Leslocatie hover tooltip engine */
    $('.les-tooltip-show').hover(function() {
        $('.lestooltip').hide();

        var offset = $("#" + this.id).offset();

        $("#lestt-" + this.id).css({
            'left': "-2px",
            'top': 0,
            'position': 'absolute',
            'height': 'auto',
            'width': '200px'
        });

        $("#lestt-" + this.id + " div").addClass("content");
        $(".les-tooltip-close").fadeIn("fast");
        $("#lestt-" + this.id).fadeIn();


    },
	    function() {
	        /*  $('div.lestooltip').hide();*/
	    }

	);
    $('.lestooltip').mouseleave(function() {
        $('div.lestooltip').hide();
    });
    /*end*/
    $(".watermarksearch").focus(function() {
        $(this).filter(function() {
            return $(this).val() == "" || $(this).val() == "titel, onderwerp, etc."
        }).removeClass("watermarkOn").val("");
    });
    $(".watermarksearch").blur(function() {
        $(this).filter(function() {
            return $(this).val() == ""
        }).addClass("watermarkOn").val("titel, onderwerp, etc.");
    });

    $(".watermarknieuwsbrief").focus(function() {
        $(this).filter(function() {
            return $(this).val() == "" || $(this).val() == "Uw email adres"
        }).removeClass("watermarkOn").val("");
    });
    $(".watermarknieuwsbrief").blur(function() {
        $(this).filter(function() {
            return $(this).val() == ""
        }).addClass("watermarkOn").val("Uw email adres");
    });

    $(".watermarkstraatnaam").focus(function() {
        $(this).filter(function() {
            return $(this).val() == "" || $(this).val() == "Straatnaam"
        }).removeClass("watermarkOn").val("");
    });
    $(".watermarkstraatnaam").blur(function() {
        $(this).filter(function() {
            return $(this).val() == ""
        }).addClass("watermarkOn").val("Straatnaam");
    });

    $(".watermarkpostcode").focus(function() {
        $(this).filter(function() {
            return $(this).val() == "" || $(this).val() == "Postcode"
        }).removeClass("watermarkOn").val("");
    });
    $(".watermarkpostcode").blur(function() {
        $(this).filter(function() {
            return $(this).val() == ""
        }).addClass("watermarkOn").val("Postcode");
    });

    $(".watermarkhuisnr").focus(function() {
        $(this).filter(function() {
            return $(this).val() == "" || $(this).val() == "Huisnr"
        }).removeClass("watermarkOn").val("");
    });
    $(".watermarkhuisnr").blur(function() {
        $(this).filter(function() {
            return $(this).val() == ""
        }).addClass("watermarkOn").val("Huisnr");
    });

    $(".watermarkwoonplaats").focus(function() {
        $(this).filter(function() {
            return $(this).val() == "" || $(this).val() == "Woonplaats"
        }).removeClass("watermarkOn").val("");
    });
    $(".watermarkwoonplaats").blur(function() {
        $(this).filter(function() {
            return $(this).val() == ""
        }).addClass("watermarkOn").val("Woonplaats");
    });


    if ($('#tabcontent').length > 0) {
        $('#tabcontent').tabs({ fxFade: false, fxSpeed: 'fast' });
        $('.tabstitle').show();
    }

    if ($('#locatieaccordion').length > 0) {
        $("#locatieaccordion").accordion({
            active: false,
            header: '.accordionheader',
            alwaysOpen: false
        });
    }
    /* Tooltip engine */
    $('.tooltip-show').click(function() {
        $('.moreinformation').fadeOut("fast");
        var offset = $("#" + this.id).offset();
        $("#tt-" + this.id + " div").addClass("content");
        $(".tooltip-close").fadeIn("fast");
        $("#tt-" + this.id).fadeIn();
        return false;
    });
    $('.tooltip-close').click(function() {
        $(".moreinformation").fadeOut("fast");
        return false;
    });
    /*end*/


    function doSomething() {
        $("#meeropties").css('width', '1px');
        var countedWidth = $(document).width();
        var countedHeight = $(document).height();
        var sidebarWidth = 218;

        if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
            var ieversion = new Number(RegExp.$1)
            if (ieversion >= 6) {
                sidebarWidth = 238;
            }
            if (ieversion >= 7) {
                sidebarWidth = 218;
            }
        }
        countedWidth = countedWidth - sidebarWidth;
        $("#meeropties").css('width', countedWidth);
        $("#jsddm ul ul").css('width', countedWidth);
        $(".foldout").css('height', countedHeight);
        $(".foldoutBack").css('height', countedHeight);
    };

    var resizeTimer = null;
    $(window).bind('resize', function() {
        if (resizeTimer) clearTimeout(resizeTimer);
        resizeTimer = setTimeout(doSomething, 100);
    });

    var countedHeight = $(document).height();
    countedHeight = countedHeight;
    $('#hoofdnavigatie .foldout').css('height', countedHeight);
    $('#hoofdnavigatie .foldoutBack').css('height', countedHeight);

    var config = {
        sensitivity: 4, // number = sensitivity threshold (must be 1 or higher)    
        interval: 100, // number = milliseconds for onMouseOver polling interval    
        over: makeTall, // function = onMouseOver callback (REQUIRED)    
        timeout: 500, // number = milliseconds delay before onMouseOut    
        out: makeShort // function = onMouseOut callback (REQUIRED)    
    };



    $('#hoofdnavigatie .head').hoverIntent(config);

    function makeTall() {
        //$(".foldoutBack").css('display', 'block');
        $(this).find(".foldout").css('display', 'block');
        //$(this).find(".foldout").fadeIn(400);
        $('#hoofdnavigatie li a', this).removeClass("active");
        $('a', this).addClass("active");
        $("#meeropties").fadeOut("fast");
        if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
            var ieversion = new Number(RegExp.$1)
            if (ieversion >= 6) {
                $("select").hide();
            }
        }
    };
    function makeShort() {
        $(this).find(".foldout").css('display', 'none');
        $('a', this).removeClass("active");

    };
    $('#hoofdnavigatie').mouseout(function() {
        //$(".foldoutBack").css('display', 'none');

        if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
            var ieversion = new Number(RegExp.$1)
            if (ieversion >= 6) {
                $("select").show();
            }
        }
    });

    function removefoldoutBack() {
        //$(".foldoutBack").css('display', 'none');
    };



    var meeroptiesStatus = 0;
    $('.meeropties').click(function() {
        var countedWidth = $(document).width();

        var sidebarWidth = 218;

        if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
            var ieversion = new Number(RegExp.$1)
            if (ieversion >= 6) {
                sidebarWidth = 239;
            }
            if (ieversion == 7) {
                sidebarWidth = 218;
            }
        }
        countedWidth = countedWidth - sidebarWidth;

        if (meeroptiesStatus == 0) {
            $("#meeropties").fadeIn("slow");
            $("#meeropties").css('width', countedWidth);
            $('.meeropties').html("minder zoekopties");
            $('.meeropties').addClass("active");
            meeroptiesStatus = 1;
        } else {
            $("#meeropties").fadeOut("fast");
            $('.meeropties').html("meer zoekopties");
            $('.meeropties').removeClass("active");
            meeroptiesStatus = 0;
        }
        return false;
    });

    var timeout = 500;
    var closetimer = 0;
    var ddmenuitem = 0;
    var subnavStatus = 1;
    var defaultStatus;

    function jsddm_open() {
        jsddm_canceltimer();
        jsddm_close();
        var countedWidth = $(document).width();

        if (subnavStatus == 0) {
            ddmenuitem = $(this).find('ul').slideDown("medium");
            subnavStatus = 1;
        } else {
            ddmenuitem = $(this).find('ul').css('display', 'block');

        }
        var sidebarWidth = 240;
        if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
            var ieversion = new Number(RegExp.$1)
            if (ieversion >= 6) {
                sidebarWidth = 261;
            }
            if (ieversion >= 7) {
                sidebarWidth = 240;
            }
            if (ieversion >= 8) {
                sidebarWidth = 260;
            }
        }
        countedWidth = countedWidth - sidebarWidth;
        $(this).find('ul').css('width', countedWidth);
        $('#jsddm').find('a').removeClass("current");
        $(this).find('a:first').addClass("current");
        //$(".echo").append("tuut#");

    }



    function jsddm_close() {
        if (ddmenuitem) {
            ddmenuitem.css('display', 'none');
            //ddmenuitem.slideUp("medium");
        }
        $('#jsddm').find('a').removeClass("current");
    }
    function removeStatus() {

        window.clearTimeout(defaultStatus);
        defaultStatus = 0;
    }

    function jsddm_timer() {
        closetimer = window.setTimeout(jsddm_close, timeout);
        defaultStatus = window.setTimeout(removeStatus, timeout);
    }

    function jsddm_canceltimer() {
        if (closetimer) {
            window.clearTimeout(closetimer);
            closetimer = null;
        }
    }

    $(document).ready(function() {
        $('#jsddm > li').bind('mouseover', jsddm_open);
        $('#jsddm > li').bind('mouseout', jsddm_timer);
    });

    document.onclick = jsddm_close;


});





