// *************************************************
// STANDARDS
// *********************************************** //

//make uniqID
var uid = (
	function(){
		var id=0;
		return function(){
			return id++ ;
		};
	}
)();

//emulate popup function...
var popWindow = function(u,w,h,popEl){
	if(!popEl){
		e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
		if(e == 'png' || e == 'gif' || e == 'jpg')
			popEl = $("<div class=\"popWindowDiv\" style=\"display:block;\"><img src=\""+ u +"\" width=\"" + w + "\" height=\"" + h + "\" border=\"0\" /></div>",{overlay:20});
		else
			popEl = $("<div class=\"popWindowDiv\" style=\"display:block;\"><iframe src=\""+ u +"\"  width=\"" + w + "\" height=\"" + h + "\" border=\"0\" frameborder=\"0\"></iframe></div>",{overlay:20});
	}
	popEl.modal({
			onOpen: function (dialog) {
						dialog.data.addClass('alert');
						dialog.container.css('height','auto');
						dialog.overlay.fadeIn('normal', function () {
							dialog.container.show('normal', function () {
								dialog.data.slideDown('normal'); // See Other Notes below regarding
														   // data display property and
														   // iframe details
														   
								_typeface_js.renderDocument();
							});
						});
					},
			onClose: function (dialog) {
						dialog.data.slideUp('normal', function () {
						  dialog.container.hide('normal', function () {
							dialog.overlay.fadeOut('normal', function () {
							  $.modal.close(); // must call this to have SimpleModal
											   // re-insert the data correctly and
											   // clean up the dialog elements
							});
						  });
						});
					}
	});
	$("#modalContainer").css('width',w).css('margin-left',(w / 2) * -1).css('height',h).css('margin-top',(h / 2) * -1); 
}

//make same height
var make_same_height = function(selector){
	var max_height = 0;
	$(selector).each(function(){
		if($(this).height() > max_height){
			max_height = $(this).height();
		}
	});
	$(selector).height(max_height);	
}

function jumpScroll(){
	if($("#aanbod_slideshow").length){
		$("#aanbod_slideshow").fadeTo(8000,1,function(){
			scrollRight(1);
			jumpScroll();
		});
	}
}

function scrollRight(scroll_view_amount) {
	x = $("#aanbod_slideshow div.funda_last_added_module .object:eq(0)").width() * scroll_view_amount;
	$("#aanbod_slideshow div.funda_last_added_module").animate({'left':(x * -1) + 'px'}, "slow","swing",function(){
		$("#aanbod_slideshow div.funda_last_added_module .object:lt(" + scroll_view_amount + ")").appendTo("#aanbod_slideshow div.funda_last_added_module");
		$("#aanbod_slideshow div.funda_last_added_module").css('left','0px');
		//jumpScroll();
	});
}





// *************************************************
// DOCUMENT READY
// *********************************************** //

$(document).ready(function(){

	//KILL EMPTY ARTICLES
	$(".article,.title").each(function(){
		if(!$.trim($(this).text()) && !$(this).find('.module_contents').length && !$(this).find('img').length){
			$(this).remove();
		}
	});

	//SCROLL HUIZEN AANBOD
	var scroll_view_amount = 6;
	$("#aanbod_slideshow div.forward").click(function(){
		scrollRight(scroll_view_amount);
	});
	$("#aanbod_slideshow div.backward").click(function(){
		x = $("#aanbod_slideshow div.funda_last_added_module .object:eq(0)").width() * scroll_view_amount;
		i = $("#aanbod_slideshow div.funda_last_added_module .object").length - (scroll_view_amount + 1);
		$("#aanbod_slideshow div.funda_last_added_module .object:gt(" + i + ")").prependTo("#aanbod_slideshow div.funda_last_added_module");
		$("#aanbod_slideshow div.funda_last_added_module").css('left',(x * -1) + 'px');
		$("#aanbod_slideshow div.funda_last_added_module").animate({'left':'0px'}, "slow","swing",function(){
			//jumpScroll();
		});
	});
	$("#aanbod_slideshow .object").click(function(){
		self.location.href = $(this).find('.read_more').attr('href');
	});
	
	//SPECS OPEN/CLOSE
	if($('.funda_aanbod_module .details .properties ul').length){
		$('.funda_aanbod_module .details .properties > ul > li > label').click(function(e){
			e.preventDefault();
			i = $(this).next('ul:eq(0)');
			if(!i.hasClass('open')){
				i.slideDown('normal',function(){$(this).addClass('open');});
			}else{
				i.slideUp('normal',function(){$(this).removeClass('open');});
			}
			return false;
		});
	}
	if ($('.funda_aanbod_module .listing').length) {
		//sort menu submitter
		$('.funda_aanbod_module .sort_menu form select').change(function(){
			$(this).parents('form').trigger('submit');
		});
		//thumb clicker
		$(".funda_aanbod_module .listing .object div.thumb").css('cursor','pointer').click(function(){
			self.location.href = $(this).parents('.object').find('a.read_more:eq(0)').attr('href');
		});
	}

	//SLIDE MENU
	$("#menu > ul > li").hover(
		function(){
			$(this).children('a').addClass('hovering');
			i = $(this).children('ul:eq(0)');
			if(i.is(":hidden"))
				i.show();
		},
		function(){
			$(this).children('a').removeClass('hovering');
			i = $(this).children('ul:eq(0)');
			if(i.is(":visible"))
				i.hide();
		}
    );
	

	//CALL ME
	$(".call_to_action a.bel_mij_terug").removeAttr('target').click(function(e){
		e.preventDefault();
		$("#callme script").remove();
		popWindow('',350,$("#callme").innerHeight() + 40,$("#callme"));
		return false;
	});

	//advanced search?
	if($(".funda_aanbod_module .funda_advancedsearch_module").length){
		//info tabs
		$('.funda_aanbod_module div.funda_advancedsearch_module ul.detail_tabs li').click(function(){
			v = $(this).attr('class');
			$('.funda_aanbod_module div.funda_advancedsearch_module ul.detail_tabs li').removeClass('high');
			$(this).addClass('high');
			$('.funda_aanbod_module div.funda_advancedsearch_module .details_block').hide();
			$('.funda_aanbod_module div.funda_advancedsearch_module .details_block.' + v).show();
		});
		setTimeout(function(){$('.funda_aanbod_module div.funda_advancedsearch_module ul.detail_tabs li:first').trigger('click');},3000);
	}

	
	//details
	if($(".funda_aanbod_module .details").length){
		
		//info tabs
		$('.funda_aanbod_module div.details ul.detail_tabs li').click(function(){
			v = $(this).attr('class');
			if(v != 'brochure' && v != '360_link'){
				$('.funda_aanbod_module div.details ul.detail_tabs li').removeClass('high');
				$(this).addClass('high');
				$('.funda_aanbod_module div.details .details_block').hide();
				$('.funda_aanbod_module div.details .details_block.' + v).show();
			}else{
				window.open($(this).attr('href'));
			}
		});

		setTimeout(function(){$('.funda_aanbod_module div.details ul.detail_tabs li:first').trigger('click');},3000);
				
		//navigation
		$(".funda_aanbod_module a.preview").addClass('preview_0').click(function(e){
			e.preventDefault();
			if(e.target.className != 'back' && e.target.className != 'forward'){
				i = parseInt($(this).attr('class').match(/([0-9]+)/)[1]);
				$(".funda_aanbod_module .photos a.thumb:eq(" + i + ")").trigger('click');
			}
			return false;
		});
		
		var preloadImg = new Array();
		$(".funda_aanbod_module .photos a.thumb").click(function(e){
			e.preventDefault();
			
			var curIndx = $(".funda_aanbod_module .photos a.thumb").index($(this));
			
			u = $(this).attr('href');
			d = new Date();
			var imgKey = d.getTime();
			preloadImg[imgKey] = new Image();
			preloadImg[imgKey].onload = function(){
				
				preloadImg[imgKey].onload = null;
				w = preloadImg[imgKey].width;
				h = preloadImg[imgKey].height;
				u = preloadImg[imgKey].src;
		
				popEl = $("<div class=\"popWindowDiv\" style=\"display:block;background-position:center;background-image:url(" + u + ")\"><span class=\"back\">&lt;</span><span class=\"forward\">&gt;</span></div>").width(w).height(h);

				popEl.find('span').click(function(e){
					if($(this).hasClass('back')){
						curIndx--;
					}else{
						curIndx++;
					}
					if(curIndx >= $(".funda_aanbod_module .photos a.thumb").length){
						curIndx = 0;
					}else if(curIndx < 0){
						curIndx = $(".funda_aanbod_module .photos a.thumb").length - 1;
					}
					
					u = $(".funda_aanbod_module .photos a.thumb:eq(" + curIndx + ")").attr('href');					
					var primg = new Image();
					primg.onload = function(){
						primg.onload = null;
						w = primg.width;
						h = primg.height;
						u = primg.src;
						$('#modalContainer .popWindowDiv').css('background-image','url(' + u + ')').css('width',w).css('height',h);
						$("#modalContainer").css('width',w).css('margin-left',(w / 2) * -1).css('height',h).css('margin-top',(h / 2) * -1);
					}
					primg.src = u;
				});

				popEl.modal({
					onOpen: function (dialog) {
								//dialog.data.addClass('alert');
								dialog.data.show();
								dialog.container.css('height','auto');
								dialog.overlay.fadeIn('', function () {
									dialog.container.show('normal', function () {
										//dialog.data.slideDown('normal'); // See Other Notes below regarding
																   // data display property and
																   // iframe details
									});
								});
							},
					onClose: function (dialog) {
								//dialog.data.slideUp('normal', function () {
								  dialog.container.hide('normal', function () {
									dialog.overlay.fadeOut('normal', function () {
									  $.modal.close(); // must call this to have SimpleModal
													   // re-insert the data correctly and
													   // clean up the dialog elements
									});
								  });
								//});
							}
				});
				$("#modalContainer").css('width',w).css('margin-left',(w / 2) * -1).css('height',h).css('margin-top',(h / 2) * -1);
			}
			preloadImg[imgKey].src = u;
			return false;
		});
		
		
		$(".funda_aanbod_module .maps a.thumb").click(function(e){
			e.preventDefault();
			
			var curIndx = $(".funda_aanbod_module .maps a.thumb").index($(this));
			
			u = $(this).attr('href');
			d = new Date();
			var imgKey = d.getTime();
			preloadImg[imgKey] = new Image();
			preloadImg[imgKey].onload = function(){
				
				preloadImg[imgKey].onload = null;
				w = preloadImg[imgKey].width;
				h = preloadImg[imgKey].height;
				u = preloadImg[imgKey].src;
		
				w = 800;
				h = 600;
		
				popEl = $("<div class=\"popWindowDiv\" style=\"display:block;background-position:center;background-repeat:no-repeat;background-image:url(" + u + ")\"><span class=\"back\">&lt;</span><span class=\"forward\">&gt;</span></div>").width(w).height(h);

				popEl.find('span').click(function(e){
					if($(this).hasClass('back')){
						curIndx--;
					}else{
						curIndx++;
					}
					if(curIndx >= $(".funda_aanbod_module .maps a.thumb").length){
						curIndx = 0;
					}else if(curIndx < 0){
						curIndx = $(".funda_aanbod_module .maps a.thumb").length - 1;
					}
					$(this).parent().css('background-image','url(' + $(".funda_aanbod_module .maps a.thumb:eq(" + curIndx + ")").attr('href') + ')');
				});

				popEl.modal({
					onOpen: function (dialog) {
								//dialog.data.addClass('alert');
								dialog.data.show();
								dialog.container.css('height','auto');
								dialog.overlay.fadeIn('', function () {
									dialog.container.show('normal', function () {
										//dialog.data.slideDown('normal'); // See Other Notes below regarding
																   // data display property and
																   // iframe details
									});
								});
							},
					onClose: function (dialog) {
								//dialog.data.slideUp('normal', function () {
								  dialog.container.hide('normal', function () {
									dialog.overlay.fadeOut('normal', function () {
									  $.modal.close(); // must call this to have SimpleModal
													   // re-insert the data correctly and
													   // clean up the dialog elements
									});
								  });
								//});
							}
				});
				$("#modalContainer").css('width',w).css('margin-left',(w / 2) * -1).css('height',h).css('margin-top',(h / 2) * -1);
			}
			preloadImg[imgKey].src = u;
			return false;
		});
	}
	

	//add twitter feed...
	//$('.twitter_feed').append('<div id="twitter_div"><div id="twitter_update_list">&nbsp;</div><a style="display: block; text-align: right;" id="twitter-link" href="http://twitter.com/broekmanog">volg ons op Twitter</a></div><script type="text/javascript" src="http://twitter.com/statuses/user_timeline/broekmanog.json?callback=twitterCallback2&count=0"></scr'+'ipt></div>');

	//jumpScroll();
	
	make_same_height('.article.half');


	//_typeface_js.renderDocument();

});

