var mapLocations = [];
mapLocations[0] = {label:"Dordrecht, The Netherlands", xPos:172, yPos:131};
mapLocations[1] = {label:"Ghent, Belgium", xPos:159, yPos:143};
mapLocations[2] = {label:"Bietigheim - Bissingen, Germany", xPos:189, yPos:157};
mapLocations[4] = {label:"Zug, Switzerland", xPos:185, yPos:173};
mapLocations[5] = {label:"São Paulo, Brazil", xPos:524, yPos:215};
mapLocations[6] = {label:"Monte Carlo, Principality of Monaco", xPos:183, yPos:210};
mapLocations[7] = {label:"Hong Kong, China", xPos:610, yPos:169};
mapLocations[8] = {label:"Shanghai, China", xPos:622, yPos:130};
mapLocations[9] = {label:"Budapest, Hungary", xPos:268, yPos:167};
mapLocations[10] = {label:"Singapore, Republic of Singapore", xPos:581, yPos:269};

var worldwideLocations = {
    init: function(){
        if($("#wordwide_map").length == 0) return;
        $("#wordwide_map").html("<ul id='locations'></ul>");
        worldwideLocations.positionLocations();
    },
    positionLocations: function(){
        for(var location in mapLocations){
            var dot = $("<li class='dot'></li>").appendTo("#locations");
            
            $(dot).css({'top' : mapLocations[location].yPos+'px', 'left' : mapLocations[location].xPos+'px'});
            $(dot).html(mapLocations[location].label);

            $(dot).hover(
                function () {
                    $(".qtip-tip").css({'left' : 13});
                },
                function () {} // out
            );

            $(dot).qtip({
               content: mapLocations[location].label, // Set the tooltip content to the current corner
               position: {
                  corner: {
                     tooltip: 'bottomLeft', // Use the corner...
                     target: 'bottomLeft' // ...and opposite corner
                  },
                  adjust : {
                      x: -6,
                      y: -16
                  }
               },
               show: {
                  when: {
                    event: 'mouseover'},
                    effect: { type: 'fade', length: 75},
                    delay: 0
               },
               hide: {
                  when: { event: 'mouseout' }
               },
               style: {
                  color: '#ffffff',
                  fontSize: '12px',
                  fontWeight: 'normal',
                  border: {
                     width: 0,
                     radius: 0
                  },
                  padding: '3px 10px 3px 10px',
                  textAlign: 'center',
                  tip: { // Now an object instead of a string
                         corner: 'bottomLeft', // We declare our corner within the object using the corner sub-option
                         color: '#4a7f87',
                         size: {
                            x: 7, // Be careful that the x and y values refer to coordinates on screen, not height or width.
                            y : 7 // Depending on which corner your tooltip is at, x and y could mean either height or width!
                         }
                  },
                  background: '#4a7f87'
               }
            });
        }
        
    }
};

var site = {
	init: function() {
		//button hovers
		$(".blackbox a.button").hover(function () {
			var underlinePos = "";
			if ($("a.button").height() == 14) { //ie
				underlinePos = "20px"
			}
			if ($("a.button").height() == 13) { //ff
				underlinePos = "20px"
			}
			if ($("a.button").height() == 12) { //safari
				underlinePos = "18px"
			}

			$(this).stop().animate({backgroundPosition:"0px " + underlinePos}, 200, function(){
				$(this).animate({backgroundPosition:"7px " + underlinePos,paddingRight:"0px"}, 200 );
			} );
			
		},
			function () {
			$(this).stop().animate({backgroundPosition:"0px 0px",paddingRight:"8px"}, 200 );
		})

		$(" a[rel='blank']").each(function() {
			$(this).attr({
				//target: "_blank",
				href: "javascript:window.open('" + $(this).attr("href") + "');void(0);",
				title: "Link opens in new window/tab"
			});
		});

	}
}

/* Fill formfields with temp data */
var formFiller = {
	fill: function(fieldId, fieldValue){
		if($(fieldId).length == 0) return false;

		if($(fieldId).val() == ""){
			$(fieldId).val(fieldValue);
		}

		$(fieldId).focus(function () {
			if($(fieldId).val() == fieldValue){
                $(fieldId).val("");
                //console.log('focus');
			}
		});

		$(fieldId).blur(function () {
			if($(fieldId).val() == ""){
				$(fieldId).val(fieldValue);
                //console.log('blur');
			}
		});
		return fieldValue;
	}
}

var homeHovers = {
	hoverHolder:  "",
	pHeights: [],

	init: function(hoverholder) {
		homeHovers.hoverHolder = hoverholder;
		$(homeHovers.hoverHolder + " li div p").each(function(i, val) {
			// array build of original dimensions
		   homeHovers.pHeights.push($(this).height() + parseInt($(this).css("padding-top").replace("px","")) + parseInt($(this).css("padding-bottom").replace("px","")));
		});

		// close em up & color the titles
		$(homeHovers.hoverHolder + " li div").css({'height':'0px'});
		$(homeHovers.hoverHolder + " li h1 a").css({'color':'#FFF'});
		$(homeHovers.hoverHolder + " li").css({'cursor':'pointer'});

		// click event area
		$(homeHovers.hoverHolder + " li").live('click', function(evt){
             window.location.href = $(this).find("h1 a").attr("href");
             return false;
		});

		animate = 0;

		$(homeHovers.hoverHolder + " li").bind('mouseenter', function (e) {
			 animate = 1;
			 posItem =  GetSiblingNumber($(this)) ;
			 originalHeight = homeHovers.pHeights[posItem];

			 $(this).find("div").stop().animate({
                height: originalHeight + "px"
			 },  200).end().find("h1 a").stop().animate({
                color: "#4a7f87"
			  }, 200 )
		  })
		.bind('mouseleave',function (e) {
			 $(this).find("div").stop().animate({
                height: "0px"
			  }, 200 ).end().find("h1 a").stop().animate({
                color: "#FFFFFF"
			  }, 200 )
		  })
	}
}

var submenuHovers = {
	submenu:  "",
	init: function(submenu) {
		submenuHovers.submenu = submenu;

		$(submenuHovers.submenu + " li:not(.active)").hover(
		  function () {
			$(this).stop().animate({marginLeft: "12px"}, 200 )
		  },
		  function () {
			$(this).stop().animate({marginLeft: "0px"}, 200 )
		  }
    );
	}
}

var contactLocations = {
    locationHash: "",
    
	init: function() {
        contactLocations.locationHash = window.location.hash.split("#")[1];

		//looking up the heighest and setting the wrapper height
		var wrapperHeight = 0;
		$("div.locations").each(function(){
			if ( $(this).height() > wrapperHeight )
				wrapperHeight =  $(this).height();
		});

        $(".contact .wrapper").height($("div.locations:eq(0)").height() + 100);

		//position the locations and show first
		$("div.locations ul").css({
			position: "absolute",
			top: 0
		});

        $("div.locations:gt(0)").hide(1);

		// add the tabs listholder
		$(".contact .wrapper").prepend("<ul id='tabs-locations'></ul>");

        // add some styling classes to rows
        $("table.locations tr.header").prev().addClass('last');
        $("table.locations").each(function(){
            $("tr:last", this).addClass('last');
        })

		// remove headers and build tabs
		$("h2.locations").each(function(i){
            // deze class wordt gebruikt om tabs te activeren via een hash in de url, zodat een link naar een tab kan verwijzen/activeren
            var tabClass = $(this).html().toLowerCase().replace(" ", "_");

			if (i == 0) {
				$("#tabs-locations").append("<li class='"+tabClass+" active'><a href='#'>"+$(this).html()+"</a></li>");
			} else {
				$("#tabs-locations").append("<li class='"+tabClass+"'><a href='#'>"+$(this).html()+"</a></li>");
			}
			$(this).remove();
		});

		// tab click binds
		$("#tabs-locations li a").bind("click", function(e){
			prevPos = GetSiblingNumber($(this).parent().parent().find(".active"));
			
			if ( $(this).parent().not(".active").size() == 1 ) {
				$(this).parent().parent().find(".active").removeClass("active");
				$(this).parent().addClass("active");
				newPos = GetSiblingNumber($(this).parent());

				// switch locations
				$("div.locations:eq("+prevPos+")").fadeOut("fast", function(){
                    $(".contact .wrapper").height($("div.locations:eq("+newPos+")").height() + 100);
					$("div.locations:eq("+newPos+")").fadeIn("fast");
				});
			}
			return false;
		});

        // kijkt of er in de link een hash is meegegeven en trigger vervolgens de klik
        if(contactLocations.locationHash != undefined){
            $("#tabs-locations li."+contactLocations.locationHash+" a").trigger('click');
        }

        // tijdelijk uitschakelen van tab
        $("li.head_offices a").hide();
	}
};

var mediaCentre = {
	init: function() {
		/*$(function() {
			$(".datepicker").datepicker({
				dateFormat: 'yy-mm-dd',
				duration:''
			});
		});*/
		
		var newheight = 0;
		$(".content-2").each(function(){
			if ( $(this).height() > newheight )
				newheight =  $(this).height();
		});
		$(".content-2").height(newheight+0);

		//h1 height fix
		setTimeout("sifrHeight('h1')",200);
	}
}

function sifrHeight(element) {
	if ($(element).height() < parseInt($(element + " object").attr("height"))) {
		$(element).height( parseInt($(element + " object").attr("height")) );
	}
}

function GetSiblingNumber(oElement) {
	return $(oElement).parent().children(oElement.nodeName).index(oElement);
}

$(document).ready(function() {
    worldwideLocations.init();
    formFiller.fill("#search-terms", "Search");

    $(".responsibility .article").css('cursor', 'pointer');
    $(".responsibility .article").click(function(){
        window.location.href = $("a", this).attr('href');
        return false;
    });

/* LET OP TEMP STUFF */
    $("#footerlinks li:eq(1)").hide(); // schakelt de terms & conditions tijdelijk uit
/* END TEMP STUFF */

	//global initializers
	site.init();

	//home
	homeHovers.init("#slider-home ul");

	//activities
	homeHovers.init("#slider-activities ul");

	//submenus
	submenuHovers.init("#navigation-submenu ul");

	//contact
	contactLocations.init();

    // news
	mediaCentre.init();
});

