/*Cufon.set('fontFamily', ' helvetica').replace('h1:not(.nocufon)')('h2:not(.nocufon)')('h3:not(.nocufon)')('h4:not(.nocufon)')('#cases li span')('#navMain a')('.cufon');

Cufon.replace('#navSub a', { hover: { color: '#FFF'} });
Cufon.replace('blockquote a', { hover: { color: '#E70033'} });
Cufon.replace('#contentWrapper ul.cufon li', { hover: { color: '#E70033'} });
Cufon.replace('.pdfbrowser .extend .datePicker li', { hover: false })
Cufon.set('fontFamily', ' helveticaItalic').replace('.intro');
*/

Cufon.set('fontFamily', ' helvetica');
Cufon.replace('#navSub a', { hover: { color: '#FFF'} })('#navMain a')('.childlist')('blockquote a');

Cufon.set('fontFamily', ' helveticaItalic').replace('.intro');
/*
.filter(function(){
    if ($(this).parents('.no-cufon').length == 0) return true;
}));
*/
var peopleState = 1;
var peopleSearch = 0;
var searchTimer = null;
var peopleArray = Array();
function startPeopleSearch() {
    if (searchTimer) {
        clearTimeout(searchTimer);
    }
    searchTimer = setTimeout("doPeopleSearch()", 100);
}
function doPeopleSearch() {
    searchVal = $("#people_searchfield").val();
    searchVal = searchVal.toLowerCase();
    varAfdeling = $(".people_afdeling:first").val().toLowerCase();
    varVestiging = $(".people_vestiging:first").val().toLowerCase();
    if (searchVal != "" || varVestiging != "" || varAfdeling !="") {
        for (i = 0; i < peopleArray.length; i++) {
            if (
               (searchVal == "" ||
               peopleArray[i]["name"].indexOf(searchVal) >= 0 ||
               peopleArray[i]["jobfunction"].indexOf(searchVal) >= 0
               ) 
               &&
               (varAfdeling == "" || peopleArray[i]["department"].indexOf(varAfdeling) >= 0)
               &&
               (varVestiging == "" || peopleArray[i]["officelocation"].indexOf(varVestiging) >= 0)
             
               ) {
                $('#' + peopleArray[i]["id"]).show();
            } else {
            $('#' + peopleArray[i]["id"] ).hide()/*.hide();*/

            }
        }
    } else {
        $(".ccblock").show();
        
    }

}
$(document).ready(function () {
    Cufon.set('fontFamily', ' helvetica');
    Cufon.replace(
  $('h1, h2').filter(function () {
      if ($(this).parents('.no-cufon').length == 0) return true;
  })

);

    Cufon.now();
    Shadowbox.init();

    $('#standardcontent table tr:first').css('font-weight', 'bold');
    $('#standardcontent table tr:first td').css('border-bottom', '1px solid #36424a');
    $('#standardcontent table tr:last td').css('border-bottom', '1px solid #36424a');

    if ($('.equalize').is('*')) equalize('.equalize li');



    if ($('.pdfbrowser').is('*')) setPdfBrowser();

    if ($('.vestiging').text() != "") {
        /*google maps init.*/
        initialize();
    } else if ($('.vestigingoverzicht').text() != "") {


        $.each($('#vestigingenOverzicht .vestigingItem'), function () {
            $(this).css("display", "none");
            vestArr.push({ 'id': $(this).attr('id'),
                'postcode': $(this).find('.vestiging_postcode').text(),
                'name': $(this).find('.vestiging_name').text(),
                'latitude': $(this).find('.latitude').text(),
                'longitude': $(this).find('.longitude').text(),
                'link': $(this).find('a').attr('href'),
                'street': $(this).find('.vestiging_bezoekAdres').text()
            });
        });
        initialize();
    }


    if ($('.microCarousel').is('*')) {
        Microcarousel({ element: '.microCarousel', step: 1, control: false, autoplay: 8000 });
    }


    $('.close').click(function () {
        $(this).parent().fadeOut();
    });

    //setInnerShadows();

    if ($('#block_toplist') != null && $('#block_topcontact') != null) {
        var height1 = $('#block_toplist').height();
        var height2 = $('#block_topcontact').height();

        if (height1 > height2) {
            $('#block_topcontact').css({ height: height1 });
        } else {
            $('#block_toplist').css({ height: height2 });
        }
    }

    $("#contactSelect").hide();

    if ($('#people_searchfield').is('*') != null) {
        $("#people_searchfield").keyup(function () {
            startPeopleSearch();
        });
        $(".people_afdeling").change(function () {
            startPeopleSearch();
        });

        $(".people_vestiging").change(function () {
            startPeopleSearch();
        });
        ///build people Array()
        $(".ccblock").each(function () {
            name = $(this).find(".people_detail .name").text().toLowerCase();
            jobfunction = $(this).find(".people_detail .jobfunction").text().toLowerCase();
            officelocation = $(this).find(".people_detail .officelocation").text().toLowerCase();
            department = $(this).find(".people_detail .department").text().toLowerCase();
            pId = this.id;
            peopleArray[peopleArray.length] = { "name": name, "jobfunction": jobfunction, "id": pId, "officelocation": officelocation, "department": department };
            if (peopleArray.length < 33) {
                $(this).show();
            }
        });

        if ($('#peopleSearch') != null) {
            /* -- People page*/
            $('.state0').hide();
            $('.state2').hide();
            $('.state3').hide();
        }

        $.each($("#peopleList li"), function (e) {
            $(this).find(".state0").hide();

            $(this).mouseover(function () {

                if ($(this).find(".state3").css("display") == "none") {
                    $(this).find(".state0").show();
                }

            }).mouseout(function () {
                $(this).find(".state0").hide();
            });

            $(this).click(function (e) {
                $('.state1').show();
                if ($(this).find(".state3").css("display") == "none") {
                    $('.people_tooltip').hide();
                    $('.state3').hide();
                    $(this).find('.state1').hide();
                    $(this).find('.state3').show();
                } else {

                    $('.state1').show();
                    $(this).find('.state3').hide();
                }
            });
        });
    }

    var bt_id = "";
    $(".back_tel").click(function (e) {
        var Pos = $(this).position();
        var pTop = Pos.top - 200;
        // alert(pTop);
        var pLeft = Pos.left;
        bt_id = $(this).find("a").attr("id");

        /*if (pTop > 960) pTop = 960;*/
        if (pLeft > 618) pLeft = 618;

        $("#contactSelect").fadeOut(300, function () {
            $("#contactSelect").css({ top: Pos.top, left: pLeft, marginTop: -100 });
            $("#contactSelect").fadeIn(300);
            $("#contactSelect").find('iframe').remove();
            $("#contactSelect").append('<iframe id="contact_frame" class="iframe" allowtransparency frameborder="0" src="/umbraco_mod/CallMeBack.aspx?id=' + bt_id + '"></iframe>');

        });
        if ($(this).find("a") != null) $(this).find("a").click(function (e) { e.preventDefault() });
    });

    /* -- Lavalamp jquery style menu -- */
    $('#navMain ul').addClass("lavaLamp")
    var lavaTimeOut;
    if ($(".lavaLamp").html() != null) {

        /*$(function () { $(".lavaLamp").lavaLamp({ fx: "backout", speed: 700 }) });*/
        $(".lavaLamp").append("<li class='back'></li>");

        if ($('.lavaLamp a.active').is('*') == false) {
            $('.back').animate({ opacity: 0 }, 1)
        } else {
            $('.back').css({ "margin-left": Math.round($('.lavaLamp a.active').parent().position().left), "width": Math.round($('.lavaLamp a.active').parent().width()) });
            $('.back').stop().animate({ queue: false, opacity: 1 }, 600, "easeOutCubic");
        }

        $(".lavaLamp li a").addClass("jsLavaLamp");
        $(".lavaLamp li").mouseover(function (e) {

            clearTimeout(lavaTimeOut);

            $('.back').stop().animate({ queue: false, "margin-left": Math.round($(this).position().left), "width": Math.round($(this).width()), opacity: 1 }, 600, "easeOutCubic");
        }).mouseout(function (e) {

            if ($('.lavaLamp a.active').is('*') == true) {
                lavaTimeOut = setTimeout(lavaActive, 1000);
            } else {
                $('.back').stop().animate({ queue: false, opacity: 0 }, 300);
            }
        });
    }


    /* -- MagmaLamp jquery style menu*/
    var magma = $('#navSub>ul>li');

    $.each(magma, function (e) {
        //console.log($(this).find('a'));
        if ($(this).find('a').hasClass("active") == false) {
            $(this).append("<span style='position:absolute;height:0px;background-color:#36424A;display:block;z-index:11;margin-top:-30px;width:" + $(this).width() + "px'></span>");

            $(this).find('a').addClass("noBg");


            var magmaO;
            $(this).find('a').mouseover(function () {

                /* magmaO = setTimeout(*/$(this).parent().css("color", '#ffffff');
                $(this).parent().find('span').stop().animate({ queue: false, 'height': '30px' }, 300, 'easeOutBack');

            }).mouseout(function () {

                /* magmaO = setTimeout(*/$(this).parent().css("color", '#36424A');
                $(this).parent().find('span').stop().animate({ queue: false, 'height': '0px' }, 300, 'easeInQuad');

            });
        }

    })

    if ($('.quotes').is('*')) clickQuotes();

});                                                                                 /*end document ready*/


function clickQuotes() {

    $.each($('#contentWrapper .quotes li'), function () {
        $(this).css({ 'cursor': 'pointer' });
        $(this).mouseover(function(){$(this).css('border-bottom','1px solid #E70033');
       }).mouseout(function(){$(this).css('border-bottom','1px solid #CDD0D2');
    })

    $(this).click(function () {
        var link = $(this).find('a').attr('href');
        //window.location = ;

        Shadowbox.open({
            content: link,
            player: "iframe",
            height: 650,
            width: 800
        });
             return false;
        })
    });

};

function lavaActive(){
    var xPos = $('.lavaLamp .active').position().left;
    $('.back').stop().animate({ queue: false, "margin-left": xPos, "width": $('.lavaLamp .active').parent().width() }, 600, "easeOutQuad");
}

function setInnerShadows() {
/*
    $('.innershadow').each(function () {
        $(this).css("float", "none");
        $(this).wrap("<div class='imgHolder'></div>");

        $(this).before('<img class="th_overlay" src="/images/framework/img_thumb_overlay.png" style="height:' + ($(this).parent().height() - 1) + 'px; width:' + ($(this).parent().width() - 1) + 'px; position:absolute;" />');
        //$(this).append("</div>");
    });*/

}

function equalize(element) {
  
    var element = element || ".items li";
    max = 150;
    $.each($(element), function () {
       
        var H = $(this).height();
        if (H > max) {
            max = H;
        }
    })
    $(element).css({height:max});
}

function readObj(obj) {
    var str = "";
    for (i in obj) {
        str += i + " : " + obj[i] + "  |  ";
    }
}

function resizeContactFrame(height) {
    height += 20;
    $('#contact_frame').css('min-height',height);
}


function setPdfBrowser(){
   
    var pdf     = '.pdfbrowser';
    var extend  = '.extend';
    var base    = '.base';
    var date    = '.datePicker';
    var archive = '.archive';
    var anext    = '.nextArrow';
    var aprev    = '.prevArrow';
    var archiveBtn = '.archiveBtn';
   /* alert($(pdf + '>li').siblings().size());*/
    $.each($(pdf + '>li'), function () {

        /*running through archives, setting up sliders and classes*/
        if ($(this).find('.extend').is('*')) {
            var n = 0;
            var arr = [];

            $(this).find('.extend').hide();
            $.each($(this).find('.datePicker ul li'), function () {
                $(this).addClass('y_' + n);
                $(this).hide();
                n++;
            });
            n = 0;
            $.each($(this).find('.extend ul.archive'), function () {
                $(this).addClass('y_' + n)
                $(this).hide();
                n++;
            });
            if (n != 1) {
                $(this).attr('rel', '1,' + n);
            } else {

                $(this).attr('rel', '0,' + n);
            }
        };
    });


    // Fold out and in on Archive Click.
    $(archiveBtn).click(function () {

        if ($(this).parent().find('.extend').css('display') == "none") {
            $(this).find('ul').css("position", "absolute").hide();
            $(this).find('.datePicker li').hide();
            $(this).addClass('switchArrow');

            nextPDF($(this).parent().attr('rel').split(',')[0], $(this).parent().find('.extend'));

        } else {
            $(this).parent().find('.extend>.archive').fadeOut(200);
            $(this).parent().find('.datePicker').fadeOut(301, function () {
                $(this).parent().slideUp(300, function () {
                    $(this).parent().find(archiveBtn).removeClass('switchArrow');
                });
            });
        };
        return false;
    });

        /*opbouw navigatie elementen*/
    $('.datePicker .prevArrow').click(function () {
        arr = $(this).parent().parent().parent().attr('rel').split(',');
        cur = Number(arr[0]);
        max = Number(arr[1]);

        $(this).show();
        $(this).parent().find('.nextArrow').show();

        if (cur > arr.length - 1) { $(this).fadeOut() ; cur = max - 1 } else { cur++ };



        $(this).parent().parent().parent().attr('rel', cur + "," + max);

        nextPDF(cur, $(this).parent().parent());
        return false;
    });
    $('.datePicker').each(function () {
        if ($(this).find('li').size() <= 0 || $(this).parent().parent().find(archive+" li").size() <= 1) {
            $(this).parent().parent().find(archiveBtn).hide();
        }
    });
    $('.datePicker .nextArrow').click(function () {
        arr = $(this).parent().parent().parent().attr('rel').split(',');
        cur = Number(arr[0]);
        max = Number(arr[1]);

        $(this).show();
        $(this).parent().find('.prevArrow').show();

        if (cur - 2 < 0) { $(this).fadeOut(); cur = 0 } else { cur-- };


        $(this).parent().parent().parent().attr('rel', cur + "," + max);

        nextPDF(cur, $(this).parent().parent());
        return false;
        });
    };

function nextPDF(cur, loc) {
    cur = cur || 0;
    loc = loc || $('.extend:first');
   
    /*dimension set to correct slideDown*/
    $(loc).find('.datePicker').show().css({ "visibility": "hidden"});
    $(loc).find('ul.y_' + cur).show().css({ "visibility": "hidden", "position": "relative" });

    $(loc).slideDown(400, function () {
        /*Setting the right data in place*/
        $(loc).find('.datePicker').css({ "visibility": "visible" });
        $(this).parent().find('.archive').show();
        //$(this).find('ul').css("position", "absolute").hide();

        $(this).find('>ul').hide();
        $(this).find('.datePicker li').hide();

        $(this).find('.y_' + cur).css({ "position": "absolute", "display": "block" }).fadeIn();

        $(this).find('ul.y_' + cur).css({"position":"relative",  "visibility": "visible"}).hide().fadeIn();
       

        $(this).find('.datepicker .years').fadeIn();

        $(this).fadeIn(300);

    });
};


/* Minified - Microcarousel
   Example: Microcarousel('.carouselDemo',1,true);
   @f = element for carousel
   @a = page to jump to or start with
   @e = generate controls (use once) 
function Microcarousel(f,a,e){var l,i,k,b,j;var d="Vorige";var g="Volgende";$.each($(f),function(){l=$(this);if(l.data("currentstep")==undefined){i=l.height();k=l.width();l.data("currentstep",0);l.data("totalsteps",l.children().size());l.find("li").css({height:i,width:k,position:"absolute",right:-k,top:5});if(e){$(this).after('<div class="controls"><a href="#1" class="prev">'+d+'</a><a href="#2" class="next">'+g+"</a></div>");var c=$(this).nextAll(".controls:first");c.data("id",$(this));c.find(".prev").click(function(){Microcarousel($(this).parent().prevAll(f+":first"),"prev")});c.find(".next").click(function(){Microcarousel($(this).parent().prevAll(f+":first"),"next")})}}});if(a!=undefined){l=$(f);j=l.data("currentstep");b=l.data("totalsteps");i=l.height();k=l.width();if(a=="next"){j++;if(j>b){j=1}if(j<0){j=b}}else{if(a=="prev"){j--;if(j>b){j=1}if(j<0){j=b}}else{j=a;if(j>b){j=b}if(j<1){j=1}}}l.data("currentstep",j);l.find("li").fadeOut(500,function(){$(this).css("right",-k)});l.find("li:nth-child("+j+")").stop().hide().css("right",20).delay(500).fadeIn(500,function(){})}};
*/

function Microcarousel(arg){
	var t, h, w, m, c;
	var element = arg.element;
	var step 	= arg.step;
	var control = arg.control;
	var autoplay= arg.autoplay;
	
	//text labels for controls
	var prev = "Vorige"   //previous
	var next = "Volgende" //next
	
	//checks for all carousels with the given name
	$.each($(element),function(){
		t = $(this);
		
		//if the carousel isn't rendered yet, it will be calculated here.
		if(t.data('currentstep') == undefined){
			//sets variables
			h = t.height();
			w = t.width();
			
			//sets data within carousel
			t.data('currentstep',0);
			t.data('totalsteps',t.children().size());
			
			// initializes items within carousel
			t.children().css({'height':h,'width':w,'position':'absolute','right':-w,'top':5});
			
			//render controls
			if(control){
				$(this).after('<div class="controls"><a href="#1" class="prev">'+prev+'</a><a href="#2" class="next">'+next+'</a></div>');
				var cc = $(this).nextAll('.controls:first')
				cc.data('id',$(this));
				cc.find('.prev').click(function(){ Microcarousel($(this).parent().prevAll(element+':first'),'prev') })
				cc.find('.next').click(function(){ Microcarousel($(this).parent().prevAll(element+':first'),'next') })
			}
		}
	});
	
	//Checks if a step is defined and shows item
	if(step != undefined){
		
		//set up autoplay
		if(autoplay){
				$(this).data('autoplay',true);
				var to = setTimeout(function() { 
					if($(this).data('autoplay') == false){clearTimout(to)};
					Microcarousel({element:t,step:'next',autoplay:autoplay});
				},autoplay); 
			}
		
		//setting variables
		t = $(element);
		c = t.data('currentstep');
		m = t.data('totalsteps');
		
		// checking if Currentstep is not to big or too small, calculates wrap-around.
		if(step == "next"){			c++; if(c > m)c=1; if(c < 0)c=m;	
		}else if(step == "prev"){	c--; if(c > m)c=1; if(c < 0)c=m;	
		}else{						c = step; if(c > m)c=m; if(c < 1)c=1}
		
		//setting currentstep
		t.data('currentstep', c);
		
		//positioning items
		t.children().fadeOut(300,function(){$(this).css('right',-t.width())});
		t.find(' >li:nth-child('+c+')').stop().hide().css('right',0).delay(300).fadeIn(700,function(){$(this).css({opacity:''})});
	}
}
