/*
	Zorg voor vrijheid
	******************
	
	Javascript by pasz.nl. Magic by jQuery.
*/

$(document).ready(function() {
	/*	Global stuff
		************/
	
	// IE
	window.ie = ($.browser.msie);
	window.ie6 = ($.browser.msie && parseInt($.browser.version) <= 6);
	window.ie7 = ($.browser.msie && parseInt($.browser.version) == 7);
	window.ie8 = ($.browser.msie && parseInt($.browser.version) == 8);
	
	// Upgrade script voor IE6
	upgrade.init();
	
	// Click event op body
	$('body').click(function() {
		selects.hideAll();
	})
	
	
	
	var makeEmpty = function() {
		/*	Empty inputs
			************
			Inputs met css class 'makeEmpty' krijgen als value
			wat er in de title attribuut staat als de input leeg is.	*/
		
		function refresh($parent) {
			if ($parent == undefined) $parent = $('body');
			
			$parent.find('.makeEmpty').each(function() {
				$(this).val($(this).attr('title')).addClass('grayedOut')
			}).focus(function() {
				if($(this).val() == $(this).attr('title')) $(this).val('').removeClass('grayedOut')
			}).blur(function(){
				if($(this).val() == '') $(this).val($(this).attr('title')).addClass('grayedOut')
			}).trigger('blur');
		}
		
		return {
			refresh: refresh
		}
	}()
	
	// Doen
	makeEmpty.refresh();
	
	
	
	// Listing op W08
	$('ul.listing li').click(function() {
		window.location.href = $(this).find('h3 a').attr('href');
	})
	
	
	
	/*	Custom selects
		**************/
	var selects = function() {
		if(window.ie6) return false;
		$('select').each(function() {
			var $select = $(this),
				$options = $select.find('option'),
				$cSelect,
				$title,
				$ul;
			
			// Wrap met div.select
			$select.wrap('<div class="select"></div');
			$cSelect = $select.parent();
			
			// Strong en ul toevoegen
			$cSelect.append('<strong /><ul />');
			$title = $cSelect.find('strong');
			$ul = $cSelect.find('ul');
			
			// Ul met 'option'-li's vullen 
			$options.each(function(i) {
				var $li,
					cssclass = '';
				
				// Is deze option selected?
				if ($(this).attr('selected')) {
					cssclass = 'selected';
					
					// Title vullen
					$title.text($(this).text())
				}
				
				// Li toevoegen en vullen
				$li = $('<li class="' + cssclass + '">' + $(this).text() + '</li>').addClass($(this).attr('class'));
				$ul.append($li);
				
				// Nummer vastleggen
				$(this).data('num', i);
				$li.data('num', i);
				
				// Event voor li
				$li.click(function() {
					var $ul = $(this).parent(),
						$option = $ul.parent().find('select option:eq(' + $(this).data('num') + ')');
					
					// IE7 bug workaround
					if (window.ie7) $('.select').css({zIndex:1})
					
					// Hide ul
					hideSelect($cSelect);
					
					// Update echte select
					$option.attr('selected', 'selected');
					$select.trigger('change')
				})
			})
			
			// Title juiste breedte geven
			if (!$(this).hasClass('autoWidth')) $title.width($ul.outerWidth() - ($title.outerWidth() - $title.width()));
			
			// Events voor title
			$title.click(function(event) {
				var v = $(this).parent().find('ul').css('visibility');
				
				event.stopPropagation();
				
				// Verberg eerst alle selects
				selects.hideAll();
				
				if (window.ie7) {
					// IE7 bug workaround
					$('.select').css({zIndex:-1})
					$(this).parent().css({zIndex:1})
				}
				
				if (v == 'visible') {
					hideSelect($cSelect)
				} else {
					showSelect($cSelect)
				}
				
			})
			
			// Events voor verborgen select
			$select.change(function(){
				$title.text($(this).find('option:selected').text())
			})
		})
		
		function hideAll() {
			hideSelect($('.select'))
		}
		
		function hideSelect($elm) {
			$elm.each(function() {
				$(this).find('ul').css('visibility', 'hidden');
			})
		}
		
		function showSelect($elm) {
			$elm.each(function() {
				var $ul = $(this).find('ul'),
					selNum;
				
				$ul.css('visibility', 'visible');
				
				// Li's css class resetten
				$ul.find('li').removeClass('selected');
				selNum = $elm.find('option:selected').data('num');
				$ul.find('li:eq(' + selNum + ')').addClass('selected')
				
				
				if ($ul.height() > 200) $ul.css({height : 200, overflow : 'auto', overflowX : 'hidden'})
			})
		}
		
		// Make public
		return {
			hideAll: hideAll
		}
	}()
	
	
	
	
	// Min price mag nooit hoger zijn dan max price. Zit in W01 en W08.
	var minMaxPrice = function() {
		
		if($('#minPrice').length == 0) return false;
		
		var $min = $('#minPrice'),
			$max = $('#maxPrice');
		
		$min.change(function() {
			var minVal = parseInt($min.val()),
				maxVal = parseInt($max.val());
		
			if (minVal >= maxVal) {
				$max.find('option[value=' + minVal + ']').next().attr('selected', 'selected').trigger('change')
			}
		});
	
		$max.change(function() {
			var minVal = parseInt($min.val()),
				maxVal = parseInt($max.val());
		
			if (maxVal <= minVal) {
				$min.find('option[value=' + maxVal + ']').prev().attr('selected', 'selected').trigger('change')
			}
		});
	}();
	
	
	
	/*	Paginaspecifiek
		***************/
	
	// Page: W01 Home
	var pageHome = function() {
		if ($('body').attr('id') != 'pageHome') return false;
		
		// Toon koop/huur in zoekformulier
		var $radioKoop = $('#homeSearch #inpKoopaanbod'),
			$radioHuur = $('#homeSearch #inpHuuraanbod'),
			$koop = $('#homeSearch #koop'),
			$huur = $('#homeSearch #huur');
		
		// Eerst koop-/huurformulieren verbergen
		$('#homeSearch #koop, #homeSearch #huur').css({
			visibility : 'visible',
			display : 'none'
		})
		
		// Event voor radiobuttons in zoekformulier
		$('#homeSearch input[type=radio]').click(function() {
			checkSearch()
		})
		
		// Function die checkt of het koop- of huurformulier getoond moet worden
		function checkSearch() {
			if ($radioKoop.attr('checked')) {
				$koop.show();
				$huur.hide();
			}

			if ($radioHuur.attr('checked')) {
				$koop.hide();
				$huur.show();
			}
		}
		checkSearch();
		
		// Toggle teksvlak onder zoekblok
		$('#btnToggle').click(function() {
			var $box = $('#homeText');

			if ($box.hasClass('open')) {
				$box.animate({
					left : 192
				}, 250).removeClass('open')
			} else {
				$box.animate({
					left : 376
				}, 250).addClass('open')
			}
		})
		
		// Home nieuws
		function homeNieuws(n) {
			// Hilites in het ingeklapte paneel
			var self = this;
			$('#nieuws li').fadeOut('fast',function(){$(this).css({'display':'none'})});
			n++;
			if(n == $('#nieuws li').length) n = 0;
			$('#nieuws li:eq('+n+')').fadeIn('fast');
			setTimeout(function(){homeNieuws(n)},3000)
		}
		homeNieuws(-1)
		
		// Home tabs
		$('#homeTabs li a').click(function() {
			$('#homeTabs li').removeClass('active');
			$('#homeTabs .content > div').hide();
			$($(this).attr('href')).show();
			$(this).parent().addClass('active');
			return false
		})
		$('#homeTabs li a:eq(0)').trigger('click')
	}();
	
	
	
	// Page: W08 Koopaanbod
	var pageListing = function() {
		if ($('body').attr('id') != 'pageListing') return false;
		
		// Filterblok net zo hoog als resultaten maken
		$('#filter').css('min-height',$('#results ul.listing').height() + 8)
		
		// Zoekcriteria events
		$('#criteria .select .btnDelete').click(function() {
			$(this).parent().parent().remove();
			updateCriteria();
		})
		
		function updateCriteria() {
			// Last child
			$('#criteria .select').removeClass('last-child');
			$('#criteria .select:last-child').addClass('last-child');
		}
		updateCriteria();
	}()
	
	
	
	// Page: W10 t/m W14
	var pageVastgoed = function() {
		if ($('body').attr('id') != 'pageVastgoed') return false;

		// Modals
		$('#btnDoorsturen').modal({width:300}, function($modal) {
			// Callback
			
			// Doe makeEmpty inputs
			makeEmpty.refresh($modal)
			
			// Focus op eerste input
			$modal.find('input.text:eq(0)').focus();
		});
		
		$('#btnRoute').modal({width:300}, function($modal) {
			// Callback
		});
		
		$('#btnContact').modal({width:300}, function($modal) {
			// Doe makeEmpty inputs
			makeEmpty.refresh($modal);
			
			// Focus op eerste input
			$modal.find('input.text:eq(0)').focus();
		});
		
		// Gallery thumbs
		$('ul.thumbs').gallery();
	}()
});
