/**
 * Adriaan van Erk
 */


/**
 * Fancy selectbox
 * @param {Object} element
 */
 

$j(document).ready(function(){  
							
	// Slider	
	initCycles();

});


function initCycles() {
	$j('.home_action_block').cycle({
	    fx:      'fade',
	    timeout:  5000,
		random:  0
	});	
	
	$j('.fotoSlideShow').cycle({
	    fx:      'fade',
	    timeout:  5000,
		random:  1
	});	
		
	$j('#page-header').cycle({
	    fx:      'fade',
	    timeout:  7000,
		random:  1
	});	
}
 
var Select = Class.create({

	element: null,

	initialize: function(e) {
		e.observe('click',this.toggle.bind(this));
		e.down('li.first-child a').observe('click',this.toggle.bind(this));
		this.element = e;
	},

	toggle: function(event) {
		if((a = event.findElement('a')) && a.hasClassName('disabled')) {
			event.stop();
		}
		// populate hidden input
		this.element.toggleClassName('active');
	}

})

var map;

document.observe('dom:loaded', function() {
	// replace image headers
	/*$$('h2.image-header').each(function(e) {
		e.addClassName('updated');
		e.setStyle({backgroundImage: "url('"+BaseURL+"/img/headers/"+Divisie+'/' 
		+ (e.hasClassName('dark') ? 'dark/' : '')
		+ (e.hasClassName('home') ? 'home/' : '')
		+ (e.hasClassName('bigwhite') ? 'bigwhite/' : '')
		+ e.innerHTML+".gif')" , height: 26});
	});*/

	// fancy selectbox handler
	$$('ul.select').each(function(e) { new Select(e); });

	if($('photo') && $('thumbs')) {
		var thumbs = $('thumbs').select('img');
		thumbs.invoke('observe', 'click', function() {
			if (!this.up('li').hasClassName('empty')) {
				$('photo').setStyle({
					backgroundImage: 'url(' + this.alt + ')'
				}).setAttribute('href', this.getAttribute('longdesc'));
			}
		});

		// thumbnail settings:
		var sliderIndex = 0;
		var imageWidth = 99;
		var topRow = 0;

		var found = false;
		thumbs.each(function(e) {
			if(!found) {
				topRow++;
				if(e.up('li').hasClassName('break'))
					found = true;
			}
		});

		//$('thumbs').setStyle({width: (imageWidth*topRow)-topRow+'px'})

		$('photo').setStyle({
			backgroundImage: 'url('+$('photo').down('img').src+')'
		}).down('img').hide();
		
		//$j('#photo').css('background-image', 'url('+$('photo').down('img').src+')');
		
		$('zoom-image').setStyle({display: 'inline', opacity: 0.75});
		//. observe('mouseover','setOpacity',1).observe('mouseout','setOpacity',0.75);

		var navbuttons = $('thumb-slide').select('a.nav');
		navbuttons.invoke('setOpacity',0.75);

		navbuttons.invoke('observe','click', function(event) {
			event.stop();
			if(thumbs.length > 6) {

				if(this.hasClassName('next')) {
					//alert(sliderIndex+'-1 >= -'+(topRow+4));
					if(sliderIndex-1 >= -topRow+4)
						sliderIndex--;
				} else {
					if(sliderIndex+1 <= 0)
						sliderIndex++;
				}

				//alert(sliderIndex);

				if(sliderIndex <= 0) {
					new Effect.Morph('thumbs', {
						style: { marginLeft: ((sliderIndex*imageWidth))+'px' },
						duration: 0.5
					});
				}
			}
		});
	}

	
	$$('div.slideshow').each(function(slideshow) {
		var nav    = slideshow.select('a.nav');
		var slide  = slideshow.down('ul');
		var thumbs = slide.select('li');
		
	    thumbs.each(function(li) {
			li.down('img').observe('click',function() { window.location.href = this.next('a').href });
		});
		var imageWidth = 130; //slide.down('li').getWidth();
		var sliderIndex = 0;
		nav.invoke('observe', 'click', function(event) {
	
			//event.stop();

			if(thumbs.length > 3) {
				this.up('div').select('a.nav').invoke('setOpacity',1)
						
				if(this.hasClassName('next')) {
					if (sliderIndex - 1 >= -thumbs.length+3) {
						sliderIndex--;
					}
					this.setOpacity((sliderIndex == -thumbs.length+3) ? 0.5 : 1);
				} else {
					if(sliderIndex+1 <= 0) {
						sliderIndex++;
					}
					this.setOpacity((sliderIndex == 0) ? 0.5 : 1);
				}
				
				
				
				if(sliderIndex <= 0) {
						
					$j(slideshow).find('ul').animate({marginLeft:((sliderIndex*imageWidth))});
				
					//new Effect.Morph(slideshow.down('ul'), {style: { marginLeft: ((sliderIndex*imageWidth))+'px' },	duration: 0.5});
				}
				
			
			}
			
			return false;
			 
		});
		
	});
	
	$$('div.slideshowLong').each(function(slideshowLong) {
		var nav    = slideshowLong.select('a.nav');
		var slide  = slideshowLong.down('ul');
		var thumbs = slide.select('li');
		
	    thumbs.each(function(li) {
			li.down('img').observe('click',function() { window.location.href = this.next('a').href });
		});
		var imageWidth = 130; //slide.down('li').getWidth();
		var sliderIndex = 0;
		nav.invoke('observe', 'click', function(event) {
			event.stop();
			if(thumbs.length > 4) {
				this.up('div').select('a.nav').invoke('setOpacity',1)
				
				if(this.hasClassName('next')) {
					if (sliderIndex - 1 >= -thumbs.length+4) {
						sliderIndex--;
					}
					this.setOpacity((sliderIndex == -thumbs.length+4) ? 0.5 : 1);
				} else {
					if(sliderIndex+1 <= 0) {
						sliderIndex++;
					}
					this.setOpacity((sliderIndex == 0) ? 0.5 : 1);
				}
				
				if(sliderIndex <= 0) {
					new Effect.Morph(slideshowLong.down('ul'), {
						style: { marginLeft: ((sliderIndex*imageWidth))+'px' },
						duration: 0.5
					});
				}
			}
		});
	});

//		if((slideshow = $('home-slideshow_'+sliderName))) {
//			var nav = slideshow.select('a.nav');
//			var slide = slideshow.down('ul');
//			var thumbs = slide.select('li');
//			
//			var id = sliderName;
//			
//			thumbs.each(function(li) {
//				li.down('img').observe('click',function() { window.location.href = this.next('a').href });
//			});
//			var imageWidth = slide.down('li').getWidth();
//			var sliderIndex = 0;
//			nav.invoke('observe', 'click', function(event) {
//				event.stop();
//				if(thumbs.length > 3) {
//					this.up('div').select('a.nav').invoke('setOpacity',1)
//					
//					if(this.hasClassName('next')) {
//						if (sliderIndex - 1 >= -thumbs.length+3) {
//							sliderIndex--;
//						}
//						this.setOpacity((sliderIndex == -thumbs.length+3) ? 0.5 : 1);
//					} else {
//						if(sliderIndex+1 <= 0) {
//							sliderIndex++;
//						}
//						this.setOpacity((sliderIndex == 0) ? 0.5 : 1);
//					}
//					
//					if(sliderIndex <= 0) {
//						new Effect.Morph($('home-slideshow_'+sliderName).down('ul'), {
//							style: { marginLeft: ((sliderIndex*imageWidth))+'px' },
//							duration: 0.5
//						});
//					}
//				}
//			});
//		}
	

	// searchform events:
	if($('search-form')) {

		// spinner handler:
		var toggleSpinner = function() {
			$('search-results').replace('<p id="search-results" class="spinner">Bezig met laden</p>');
		}

		var url = ''+window.location;
		url = BaseURL+'/ajax'+url.gsub(BaseURL,'');
		
		// update results method:
		var update_results = function(transport) {
			try {
				map.clearOverlays();
			} catch(e) { }
			$('search-results').replace(transport.responseText);
			
			$('search-results').select('li').invoke('observe','mouseover',function() { this.addClassName('hover') });
			$('search-results').select('li').invoke('observe','mouseout', function() { this.removeClassName('hover') });
			$('search-results').select('li').invoke('observe','click', 	  function(event) {
				window.location.href = this.down('li.title a').href;
			});
			
			var numFound = new Template('Toon #{item} <strong>(#{num})</strong>');
			
			var woningen = parseFloat($('num-woningen').innerHTML);
			var projecten = parseFloat($('num-projecten').innerHTML);

			$('show-woningen').update(numFound.evaluate({num: (woningen == 0) ? '0' : woningen, item: (woningen == 1) ? 'woning':'woningen'}));
			$('show-projecten').update(numFound.evaluate({num: (projecten == 0) ? '0' : projecten, item: (projecten == 1) ? 'project':'projecten'}));
			if($('no-post')) {
				$('show-woningen').insert({top: new Element('img', { src: BaseURL+'/img/searchform-button-arrow.gif' })}).down('strong').remove();
				$('show-projecten').insert({top: new Element('img', { src: BaseURL+'/img/searchform-button-arrow.gif' })}).down('strong').remove();
			}
			
			if(projecten == 0) {
				$('show-kaart').up('span.button').addClassName('hidden');
			} else {
				$('show-kaart').up('span.button').removeClassName('hidden');
			}
			
			if($('search-results').select('li.map')) {
				try { map.clearOverlays(); } catch(e) { }
				map = new Map($('search-results').down('.map'));
				new Effect.ScrollTo('search-results', {offset: -24});
			}
			
			return true;
		}

		// observe inputs in form for changes
		var changeObserver = function(event) {
			// don't fire if it's the select-all input (will fire a 2nd request)
			if(event && event.target.up('li') && event.target.up('li').hasClassName('select-all')) {
				return;
			}
			
			var query = $('search-form').serialize();
			$('search-form').select('span.button').invoke('removeClassName','selected');

			new Ajax.Request(url, {
				method: 'post',
				postBody: query+'&countonly=yes',
				onSuccess: update_results.wrap(function(proceed, transport) {
					proceed(transport);
				})
			});
		}
		$('search-form').select('input').invoke('observe', 'click', changeObserver);
		$('search-form').select('select').invoke('observe', 'change', changeObserver);

		// 'Select all' observer
		$('search-form').select('li.select-all input').invoke('observe', 'click', function() {
			var checkedvalue = this.checked;
			
			this.up('ul').select('input[type=checkbox]').each(function(e) {
				if(e.name != 'select-all-cities')
					e.checked = checkedvalue;
			});
			// force a call to the change observer:
			changeObserver();
		});

		// observe the submit buttons
		$('search-form').select('span.button button').each(function(button) {
			// Button style:
			button
			
			.observe('click', function(event) {
				this.blur();
				event.stop();
				
				$('search-form').select('span.button').invoke('removeClassName','selected');
				this.up('span.button').addClassName('selected');
			
				$('search-action').value = this.className;

				var query = $('search-form').serialize();

				var success = update_results;
				if(this.className == 'kaart') {
					success = update_results.wrap(function(proceed, transport) {
						if(proceed(transport)) {
							if (map) {
								GUnload();
								map.clearOverlays();
							}
							map = new Map($('search-results').down('.map'));
							new Effect.ScrollTo('search-results', {offset: -24});
						}
					});
				}

				toggleSpinner();

				new Ajax.Request(url, {
					method: 'post',
					postBody: query,
					onSuccess: success.wrap(function(proceed, transport) {
						try {
						map.clearOverlays();
						} catch(e) { }
						if(proceed(transport)) {
							new Effect.ScrollTo('search-results', {offset: -24});
						}
					})
				});
			})
			// observe the buttons:
			.up('span.button')
			.observe('mouseover', function(event) { this.addClassName('active'); })
			.observe('mouseout', function(event) { this.removeClassName('active'); })
			
		});

		// price from / to
		if($('price-from')) {
			$('price-from').observe('change', function(event) {
				if($F('price-to') != 0 && $F(this) > $F('price-to')) {
					alert('De minimum bedrag kan niet hoger zijn dan maximum bedrag.');
					this.selectedIndex = $('price-to').selectedIndex-1;
				}
			});
		
			$('price-to').observe('change', function(event) {
				if($F('price-from') != 0 && $F(this) < $F('price-from')) {
					alert('De maximum bedrag kan niet lager zijn dan de minimum bedrag.');
					this.selectedIndex = $('price-from').selectedIndex+1;
				}
			});
		}

	}

	// tabs
	$$('ul.tabs li a').invoke('observe','click', function(event) {
		if(!this.hasClassName('link')) {
			event.stop();
			this.up('ul').select('li').invoke('removeClassName','active');
			this.up('li').addClassName('active');
			$$('div.tabbody').invoke('addClassName','hidden');
			$(this.up('li').id.gsub('-tab', '')).removeClassName('hidden');
		}
	});

});

function toDate(){
  var today = new Date();
  var day=today.getDate();
  var month = today.getMonth() + 1;
  var year = today.getFullYear().toString();
	
  switch (month){
    case 1 : month = 'januarie';  break;
    case 2 : month = 'februarie'; break;
    case 3 : month = 'maart';     break;
    case 4 : month = 'april';     break;
    case 5 : month = 'mei';       break;
    case 6 : month = 'juni';      break;
    case 7 : month = 'juli';      break;
    case 8 : month = 'augustus';  break;
    case 9 : month = 'september'; break;
    case 10: month = 'oktober';   break;
    case 11: month = 'november';  break;
    case 12: month = 'december';  break;
  }

  if ( (day/10)<1)
    day=("0" + today.getDate()).toString();

  return day + month + year;
}

function openSupPrijslijst()
{
	$j('#supprijslijst').show();
	$j('#opensupprijslijst').hide();
}

