/*var defaultSearchText = "Trefwoord / referentienummer";*/

$(document).ready(function () { init(); });
$(window).load(function() {
	loadBackground();
});

function init(){
	
	// table stripes:
	$("table tbody tr:odd").addClass("odd");

	initSearch($('#search-field'));
	initInterfaceElements();
	if($('.expandable').is('*')){ initExpandable(); }
	if($('.filter').is('*')){ initFilters(); }
	if($('#luxaflex').is('*')){	jQuery.luxaFlex.init(); }
	
	// Sliders:
	if($('#slider').is('*')){ jQuery.slider.init('#slider', '.slideContainer ul', 3);}
	if($('#imageSlider').is('*')){ jQuery.slider.init('#imageSlider', 'ul', 1);}
}

/*	ACHTERGROND INLADEN * * * * * * * * * * * * * * * * * */

function loadBackground() {
	$('#background').fadeIn(500);
	
	$('#background img').bind("contextmenu",function(){
		return false;
	});
	$('#background img').bind("mousedown",function(){
		return false;
	});
}

/*	INIT ZOEKEN * * * * * * * * * * * * * * * * * * * * * */
 
function initSearch(object){
	var obj = $(object);
	obj.attr('value', defaultSearchText);
	obj.focus(function(){
		if (obj.attr('value') === defaultSearchText) {
			obj.attr('value', '');
		}
	});
	obj.focusout(function(){
		if (obj.attr('value') === '') {
			obj.attr('value', defaultSearchText);
		}
	});
}

/*	INIT HOOFDMENU * * * * * * * * * * * * * * * * * * * * */

function initInterfaceElements() {

	$('#back-to-top').click(function(){
		$('html, body').animate({scrollTop: 0}, 500, 'swing');
		return false;
	});
	
	$('.fotoStrip a').click(function(){
		//return false;
	});

	var mainMenuItems = $('ul.main > li');
	var mainMenuItemsSize = mainMenuItems.size()
	
	$.each(mainMenuItems, function(key, value) { 
  		
  		var cols = $(value).find('.col');
  		var colSize = cols.size();
  		var colWidth = cols.width();
  		var foldOut = $(value).children('.foldOut')
  		
  		var foldOutWidth = (colWidth+30)*colSize
  		foldOut.width(foldOutWidth);
  		var foldOutHeight = foldOut.height();
  		
  		var relativeItemPos = $(value).offset().left - $("#mainWrapper").offset().left;
  		var overflow = $("#mainWrapper").width() - (relativeItemPos + foldOutWidth);
  		
  		if (overflow < 0){
  			foldOut.css('left', -100 + overflow);
  		} else {
  			foldOut.css('left', -100);
  		}
  		if (key == mainMenuItemsSize-1) {
  			//foldOut.css('left', ($(value).width() - foldOutWidth)-40);
        foldOut.css('left', ($(value).width() - foldOutWidth)-59);
  		}
  		
  		$(value).hover(function(){
  			foldOut.animate({height: foldOutHeight+30, opacity: 1},400);
  		}, function() {
  			foldOut.height(foldOutHeight);
  			foldOut.css('opacity', 0);
  		});
	});
}

/* 	EXPANDABLE OBJECT * * * * * * * * * * * * * * * * * * */

function initExpandable()
{
	//$('.expandable .content').css('display', 'none');
	$('.expandable .title a').click(function(){
		var obj = $(this).parent().parent();
		if (obj.hasClass('open')){
			obj.children('.content').slideUp();
			obj.removeClass('open');
		} else {
			obj.children('.content').slideDown();
			obj.addClass('open');
		}
		return false;
	});
}

/* 	FILTERS * * * * * * * * * * * * * * * * * * */

function initFilters()
{
	$.each($('.filter ul li'), function(index, obj) {
		var checkBox = $(obj).children(':checkbox')
		checkBox.css('display', 'none');
		$(obj).css('padding-left', '25px')
		if(checkBox.is(':checked')){
			checkBox.attr('checked', true);
			$(this).addClass('checked');
		}
  });
/*
	$('.filter ul li').click(function(){
		var checkBox = $(this).children(':checkbox')
		if(checkBox.is(':checked')){
			checkBox.attr('checked', false);
			$(this).removeClass('checked');
		} else {
			checkBox.attr('checked', true);
			$(this).addClass('checked');
		}
	});
*/
}

/* 	SLIDER OBJECT * * * * * * * * * * * * * * * * * * */

jQuery.slider = {
	init: function (setSliderDiv, setSlidable, setVisibleItems) {
		try {
			var S = jQuery.slider;
			S.initInterface(setSliderDiv, setSlidable, setVisibleItems);
		} catch (e) { }
	},
	initInterface: function (setSliderDiv, setSlidable, SetVisibleItems) {
		var visibleItems = SetVisibleItems;
		var sliderDiv = setSliderDiv;
		var slidablePart = setSlidable;
		var element = $(sliderDiv);
		var length = $(sliderDiv + ' ul li').size();
		var itemWidth = $(sliderDiv + ' ul li').outerWidth();
		var position = 0;
		var slideTimer = {};
		var slide = true;
		
		changeButtonStates();
		setTimer();
		
		element.hover(function(){
			slide = false;
		}, function (){
			slide = true;
		});
		
		element.children('.slideButton').click(function(){
			if ($(this).hasClass('prev') && position > 0) {
				position --;
				animate();
			}
			if ($(this).hasClass('next') && position < length-visibleItems) {
				position ++;
				animate();
			}
			return false;
		});
		function animate() {
			var newMargin = (position * itemWidth)*-1;
			$.clearTimer(slideTimer);
			element.find(slidablePart).stop().animate({marginLeft: newMargin},600);
			changeButtonStates();
			setTimer();
		}
		function changeButtonStates() {
			if (position == 0) {
				element.children('.slideButton.prev').css('opacity', 0.4);
			} else {
				element.children('.slideButton.prev').css('opacity', 1);
			}
			
			if (position == length -visibleItems) {
				element.children('.slideButton.next').css('opacity', 0.4);
			} else {
				element.children('.slideButton.next').css('opacity', 1);
			}
		}
		function setTimer() {
			slideTimer = $.timer(10000,function(){
				if(slide == true)
				{
					position ++;
					if (position > length-visibleItems) {
						position = 0;
					}
					animate();
				}
			});
		}
	}
}

/* 	LUXAFLEX OBJECT * * * * * * * * * * * * * * * * * */

jQuery.luxaFlex = {
	init: function () {
		try {
			var LF = jQuery.luxaFlex;
			LF.initInterface();
			LF.initAnimations();
			LF.changeContent();
		} catch (e) { }
	},
	initInterface: function () {
        var luxaFlex = jQuery.luxaFlex;
        var element = $('#luxaflexMenu');
        var linkElement = $('#categories li');
        $('#luxaflex').append('<div id="luxaflexShades"><div class="loader"></div></div>');
		$('#luxaflex').css({'overflow': 'hidden'});
		
		if(!$('#luxaflexContent').hasClass('standard')){
			$('#luxaflexMenu').delay(300).animate({ width : 40 }, 200, 'swing');
			$('#luxaflexJobs').animate({ left: 41 }, 400, 'swing');
    }
	},
	initAnimations: function () {
		var parentElement = $('#luxaflexMenu');
		var linkParent = $('#categories li');
		var linkElement = $('#categories li a');
		var shadows = $('#luxaflexShades');
		var slideLock = true;
		
		var hooverIn = function(){
			if(!$('#luxaflexContent').hasClass('standard')){
				$(this).stop();
				$('#luxaflexJobs').stop();
				$(this).animate({ width: 296 }, 400, 'swing');
				$('#luxaflexJobs').animate({ left: 18 }, 400, 'swing');
			}
		};
		var hooverOut = function(){
			if(!$('#luxaflexContent').hasClass('standard')){
				$(this).stop();
				$('#luxaflexJobs').stop();
				$(this).delay(300).animate({ width: 40 }, 200, 'swing');
				$('#luxaflexJobs').animate({ left: 41 }, 400, 'swing'); 
			}
		};
		var linkHooverIn = function(){
			if(!$('#luxaflexContent').hasClass('standard')){
				$(linkElement).stop();
				$('#luxaflexMenu').css({ 'overflow':'visible' });
				$(this).animate({'font-size': '16px'}, 100, 'swing');
			}
		};
		var linkHooverOut = function(){
			$(linkElement).css({'font-size': '14px'});
			
		};
		var loadNewData = function(){
			$(shadows).fadeIn(200);
			slideLock = false;
			$('#categories li').removeClass('current');
			$('#categories li.' + arguments[2]).addClass('current');
      ajax_loadContent(arguments[0],arguments[1]);
			$(shadows).fadeOut(200);
		};
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
			$('#categories li a').click(function(e){
        strCategoryXML = $(this).parent('li').attr('class');
		    strCategoryXML = strCategoryXML.replace(' current', '');
        strContentPath = strLuxaFlexContentRoot + strCategoryXML + ".xml";
				if($('#luxaflexMenu').css('width') === '296px'){
					$('#luxaflexMenu').stop();
				  loadNewData('ajaxContent',strContentPath,strCategoryXML);
					$('#luxaflexMenu').delay(300).animate({ width: 40 }, 200, 'swing');
				} else {
					$('#luxaflexMenu').animate({ width: 296 }, 400, 'swing');
				}

				return false;
			});
		} else {
			$('#categories li a').click(function(e){
        strCategoryXML = $(this).parent('li').attr('class');
		    strCategoryXML = strCategoryXML.replace(' current', '');
        strContentPath = strLuxaFlexContentRoot + strCategoryXML + ".xml?__toolbar=0&ajax=1";
				loadNewData('ajaxContent',strContentPath,strCategoryXML);
				return false;
			});
		}
		$(parentElement).hover(hooverIn, hooverOut);
		$(linkElement).hover(linkHooverIn, linkHooverOut);
		
	}
}


jQuery.timer = function(time,func,callback){
	var a = {timer:setTimeout(func,time),callback:null}
	if(typeof(callback) == 'function'){a.callback = callback;}
	return a;
};

jQuery.clearTimer = function(a){
	clearTimeout(a.timer);
	if(typeof(a.callback) == 'function'){a.callback();};
	return this;
};



/**
 * jQuery gMap
 *
 * @url		http://gmap.nurtext.de/
 * @author	Cedric Kastner <cedric@nur-text.de>
 * @version	1.1.0
 */
/**
 * jQuery gMap
 *
 * @url		http://gmap.nurtext.de/
 * @author	Cedric Kastner <cedric@nur-text.de>
 * @version	1.1.0
 */
(function($)
{
	// Main plugin function
	$.fn.gMap = function(options)
	{
		// Check if the browser is compatible
		if (!window.GBrowserIsCompatible || !GBrowserIsCompatible()) return this;
		
		// Build main options before element iteration
		var opts = $.extend({}, $.fn.gMap.defaults, options);
    	
		// Iterate through each element
		return this.each(function()
		{
			// Create map and set initial options
			$gmap = new GMap2(this);
			
			// Create new object to geocode addresses
			$geocoder = new GClientGeocoder();
			
			// Check for address to center on
			if (opts.address)
			{ 
				// Get coordinates for given address and center the map
				$geocoder.getLatLng(opts.address, function(gpoint){ $gmap.setCenter(gpoint, opts.zoom); });
				
			}
			else
			{
				// Check for coordinates to center on
				if (opts.latitude && opts.longitude)
				{
					// Center map to coordinates given by option
					$gmap.setCenter(new GLatLng(opts.latitude, opts.longitude), opts.zoom);
					
				}
				else
				{
					// Check for a marker to center on (if no coordinates given)
					if ($.isArray(opts.markers) && opts.markers.length > 0)
					{
						// Check if the marker has an address
						if (opts.markers[0].address)
						{
							// Get the coordinates for given marker address and center
							$geocoder.getLatLng(opts.markers[0].address, function(gpoint){ $gmap.setCenter(gpoint, opts.zoom); });
							
						}
						else
						{
							// Center the map to coordinates given by marker
							$gmap.setCenter(new GLatLng(opts.markers[0].latitude, opts.markers[0].longitude), opts.zoom);
							
						}
						
						
					}
					else
					{
						// Revert back to world view
						$gmap.setCenter(new GLatLng(34.885931, 9.84375), opts.zoom);
						
					}
					
				}
				
			}
						
			// Set the preferred map type
			$gmap.setMapType(opts.maptype);
			
			// Check for map controls
			if (opts.controls.length == 0)
			{
				// Default map controls
				$gmap.setUIToDefault();
				
			}
			else
			{
				// Add custom map controls
				for (var i = 0; i < opts.controls.length; i++)
				{
					// Eval is evil
					eval('$gmap.addControl(new ' + opts.controls[i] + '());');
					
				}
				
			}
						
			// Check if scrollwheel should be enabled
			if (opts.scrollwheel == true && opts.controls.length != 0) { $gmap.enableScrollWheelZoom(); }
									
			// Loop through marker array
			for (var j = 0; j < opts.markers.length; j++)
			{
				// Get the options from current marker
				marker = opts.markers[j];
								
				// Create new icon
				gicon = new GIcon();
				
				// Set icon properties from global options
				gicon.image = opts.icon.image;
				gicon.shadow = opts.icon.shadow;
				gicon.iconSize = ($.isArray(opts.icon.iconsize)) ? new GSize(opts.icon.iconsize[0], opts.icon.iconsize[1]) : opts.icon.iconsize;
				gicon.shadowSize = ($.isArray(opts.icon.shadowsize)) ? new GSize(opts.icon.shadowsize[0], opts.icon.shadowsize[1]) : opts.icon.shadowsize;
				gicon.iconAnchor = ($.isArray(opts.icon.iconanchor)) ? new GPoint(opts.icon.iconanchor[0], opts.icon.iconanchor[1]) : opts.icon.iconanchor;
				gicon.infoWindowAnchor = ($.isArray(opts.icon.infowindowanchor)) ? new GPoint(opts.icon.infowindowanchor[0], opts.icon.infowindowanchor[1]) : opts.icon.infowindowanchor;
				
				if (marker.icon)
				{
					// Overwrite global options
					gicon.image = marker.icon.image;
					gicon.shadow = marker.icon.shadow;
					gicon.iconSize = ($.isArray(marker.icon.iconsize)) ? new GSize(marker.icon.iconsize[0], marker.icon.iconsize[1]) : marker.icon.iconsize;
					gicon.shadowSize = ($.isArray(marker.icon.shadowsize)) ? new GSize(marker.icon.shadowsize[0], marker.icon.shadowsize[1]) : marker.icon.shadowsize;
					gicon.iconAnchor = ($.isArray(marker.icon.iconanchor)) ? new GPoint(marker.icon.iconanchor[0], marker.icon.iconanchor[1]) : marker.icon.iconanchor;
					gicon.infoWindowAnchor = ($.isArray(marker.icon.infowindowanchor)) ? new GPoint(marker.icon.infowindowanchor[0], marker.icon.infowindowanchor[1]) : marker.icon.infowindowanchor;
					
				}
				
				// Check if address is available
				if (marker.address)
				{
					// Check for reference to the marker's address
					if (marker.html == '_address') { marker.html = marker.address; }
					
					// Get the point for given address
					$geocoder.getLatLng(marker.address, function(gicon, marker)
					{
						// Since we're in a loop, we need a closure when dealing with event handlers, return functions, etc.
						// See <http://www.mennovanslooten.nl/blog/post/62> for more information about closures
						return function(gpoint)
						{
							// Create marker
							gmarker = new GMarker(gpoint, gicon);
							
							// Set HTML and check if info window should be opened
							if (marker.html) { gmarker.bindInfoWindowHtml(opts.html_prepend + marker.html + opts.html_append); }
							if (marker.html && marker.popup) { gmarker.openInfoWindowHtml(opts.html_prepend + marker.html + opts.html_append); }
							
							// Add marker to map
							if (gmarker) { $gmap.addOverlay(gmarker); }
						}
						
					}(gicon, marker));
					
				}
				else
				{
					// Check for reference to the marker's latitude/longitude
					if (marker.html == '_latlng') { marker.html = marker.latitude + ', ' + marker.longitude; }
					
					// Create marker
					gmarker = new GMarker(new GPoint(marker.longitude, marker.latitude), gicon);
					
					// Set HTML and check if info window should be opened
					if (marker.html) { gmarker.bindInfoWindowHtml(opts.html_prepend + marker.html + opts.html_append); }
					if (marker.html && marker.popup) { gmarker.openInfoWindowHtml(opts.html_prepend + marker.html + opts.html_append); }
						
					// Add marker to map
					if (gmarker) { $gmap.addOverlay(gmarker); }
					
				}
				
			}
			
		});
		
	}
		
	// Default settings
	$.fn.gMap.defaults =
	{
		address:				'',
		latitude:				0,
		longitude:				0,
		zoom:					1,
		markers:				[],
		controls:				[],
		scrollwheel:			true,
		html_prepend:			'<div class="gmap_marker">',
		html_append:			'</div>',
		icon:
		{
			image:				"http://www.google.com/mapfiles/marker.png",
			shadow:				"http://www.google.com/mapfiles/shadow50.png",
			iconsize:			[20, 34],
			shadowsize:			[37, 34],
			iconanchor:			[9, 34],
			infowindowanchor:	[9, 2]
			
		}
		
	}
	
})(jQuery);
