var popDialog = function(u,w,h){
	var result = window.showModalDialog(u, form, "dialogWidth:"+w+"px; dialogHeight:"+h+"px; center:yes");
}

//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
							});
						});
					},
			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); 
}

var replaceArrows = function(o){
	o.each(function(){
		if(!$(this).find('.googlemaps_module').length){
			h = $(this).html();
			if(h.match(/(&#187;)/) || h.match(/(&gt;)/) || h.match(/(&#171;)/) || h.match(/(&lt;)/)){
				h = h.replace(/(&#187;)/g,'<div class="better_arrow"><img src="/images/spacer.gif" border="0" alt="" /></div>');
				h = h.replace(/(&gt;)/g,'<div class="better_arrow"><img src="/images/spacer.gif" border="0" alt="" /></div>');
				h = h.replace(/(&#171;)/g,'<div class="better_arrow_L"><img src="/images/spacer.gif" border="0" alt="" /></div>');
				h = h.replace(/(&lt;)/g,'<div class="better_arrow_L"><img src="/images/spacer.gif" border="0" alt="" /></div>');
				$(this).html(h);
			}
		}
	});
}


$(document).ready(function(){
	
	//cosmatic stuff for menu
	$("#menu a:eq(0),#bottommenu a:eq(0),#bottommenu_right a:eq(0)").addClass('first');
	
	//change red style...
	$("*[style='color: red;'],*[style='color: Red;']").removeAttr('style').addClass('red');

	//make better arrows
	replaceArrows($('.content'));
	
	//change the stupid HR behavour of IE...
	if($.browser.msie){
		$("#content hr").wrap("<div class=\"hr\"></div>");
		$("#content hr").css('display','none');
	}

	/* ENABLE SUBMENU */
	$("#menu ul li").hover(
		 function(){
			 if($(this).children('ul:eq(0)').is(':hidden')){
				 $(this).children('ul:eq(0)').slideDown('normal');
			 }
		 },
		 function(){
			 if($(this).children('ul:eq(0)').is(':visible')){
				 $(this).children('ul:eq(0)').slideUp('normal');
			 }
		 }
	);
	$("#menu ul > ul > a:first").css('border-top','0');
	
	//add item clicks
	$(".era_module div.listing .item").click(function(){
		self.location.href = $(this).find('.image').find('a').attr('href');
	}).hover(
		function(){$(this).addClass('over');},
		function(){$(this).removeClass('over');}
	);
	

	//add sold/rented sign..
	$(".era_module div.listing .item div.image").append('<div class="overlay"></div>');
	
	//search	
	if($(".era_search_form").length){
		if($(".era_search_form .till select").val() == '0'){
			$(".era_search_form .till select")[0].selectedIndex = $(".era_search_form .till select option").length - 1;
		}
		$(".era_search_form .from select").change(function(){
			i = $(".era_search_form .from select option").index($(".era_search_form .from select option:selected")) + 1;
			if($(".era_search_form .till select")[0].selectedIndex < i){
				$(".era_search_form .till select")[0].selectedIndex = i;
			}
			$(".era_search_form .till select option").show();
			$(".era_search_form .till select option:lt(" + i + ")").hide();
		});
	}


	//huizen router banner
	source_date = new Date();
	var t = '';
	if(source_date < new Date(2010,03,27)){
		t = "ZATERDAG|27 MAART";
	}else if(source_date < new Date(2010,6,5)){
		t = "ZATERDAG|5 JUNI";
	}else if(source_date < new Date(2010,10,9)){
		t = "ZATERDAG|9 OKTOBER";
	}else if(source_date < new Date(2010,12,11)){
		t = "ZATERDAG|11 DECEMBER";
	}
	$("img[src$='logo_open_huizen_route.png']").attr('src','domains/hopman.nu/generate/open_huizen_route.php?text=' + t);

	//photo album scroll events
	var scroll_view_amount = 1;
	$(".era_module div.details div.thumbs div.right").click(function(){
		x = $(".era_module div.details div.thumbs div.scrollContent a.item:eq(0)").width() * scroll_view_amount;
		$(".era_module div.details div.thumbs div.scrollContent").animate({'left':(x * -1) + 'px'}, "slow","swing",function(){
			$(".era_module div.details div.thumbs div.scrollContent a.item:lt(" + scroll_view_amount + ")").appendTo(".era_module div.details div.thumbs div.scrollContent");
			$(".era_module div.details div.thumbs div.scrollContent").css('left','0px');
			pushPreview(1);
		});
	});
	$(".era_module div.details div.thumbs div.left").click(function(){
		x = $(".era_module div.details div.thumbs div.scrollContent a.item:eq(0)").width() * scroll_view_amount;
		i = $(".era_module div.details div.thumbs div.scrollContent a.item").length - (scroll_view_amount + 1);
		$(".era_module div.details div.thumbs div.scrollContent a.item:gt(" + i + ")").prependTo(".era_module div.details div.thumbs div.scrollContent");
		$(".era_module div.details div.thumbs div.scrollContent").css('left',(x * -1) + 'px');
		$(".era_module div.details div.thumbs div.scrollContent").animate({'left':'0px'}, "slow","swing",function(){
			pushPreview(1);
		});
	});
	$(".era_module div.details div.thumbs div.scrollContent a.item").click(function(e){
		e.preventDefault();
		indx = $(".era_module div.details div.thumbs div.scrollContent a.item").index($(this));
		if(indx == 0){
			$(".era_module div.details div.thumbs div.left").trigger('click');
		}else if(indx == 1){
			pushPreview(1);
		}else if(indx == 2){
			$(".era_module div.details div.thumbs div.right").trigger('click');
		}
		return false;
	});
	$(".era_module div.details div.photo_specs_ui div.image a,.era_module div.details.view div.photo_thumbs .thumb,.era_module div.details.view .main_photo").click(function(e){
		e.preventDefault();

		u = $(this).attr('href');
		e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
		
		if(e == 'png' || e == 'gif' || e == 'jpg'){
			img = new Image();
			img.onload = function(){
				img.onload = null;
				w = img.width;
				h = img.height;
				popWindow(u,img.width,img.height);
			}
			img.src = u;
		}
		
		return false;
	});
	$(".era_module .func.beoordeel a,.era_module .func.bezichtiging a").click(function(e){
		e.preventDefault();
		popWindow($(this).attr('href'),710,420);
		return false;
	});
	
	var pushPreview = function(indx){
		url = $(".era_module div.details div.thumbs div.scrollContent a.item:eq(" + indx + ")").attr('href');
		$(".era_module div.details div.photo_specs_ui div.image a").attr('href',url.replace('preview.','')).find('img').attr('src',url);
	}
	$(".era_module .func.kaart a").click(function(e){
		e.preventDefault();
		popWindow($(this).attr('href'),400,400);
		return false;
	});
	$(".era_module .func.videobrochure a").click(function(e){
		e.preventDefault();
		//u = '/domains/hopman.nu/modules/era/video.php?objectID=' + $(this).attr('href').match(/objectID=([0-9]+)/)[1];
		lp = (screen.width) ? (screen.width-570)/2 : 0;
		tp = (screen.height) ? (screen.height-580)/2 : 0;
		var popup = window.open($(this).attr('href'),"popup","scrollbars=no,width=570,height=580,menubar=no,status=yes,toolbar=no,copyhistory=no,top="+tp+",left="+lp+",location=no,directories=no,resizable=no");
		if (popup.focus) { popup.focus(); }
		return false;
	});

	//change all popups...
	var img = new Array();
	$(".article a").each(function(){
		i = 0;

		//change all onclick popups
		if($(this).attr('onclick')){
			i = $(this).attr('onclick').toString().indexOf("dow.open(this.href,");

			if(i){
				w = 640;
				h = 480;
				
				u = $(this).attr('href');
				e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
				
				if(e == 'png' || e == 'gif' || e == 'jpg'){
					var id = 'popwinlink_' + uid();
					$(this).attr('id',id);

					img[id] = new Image();
					img[id].onload = function(){
						img[id].onload = null;
						w = img[id].width;
						h = img[id].height;
	
						$("a#" + id).attr('onclick',"").addClass('popWindowLink').attr('href',"javascript:popWindow('" + u + "'," + w + "," + h + ");");
					}
					img[id].src = u;
				}else{
					c = $(this).attr('onclick').toString();

					h = parseInt(c.match(/height=([0-9]+)/)[1]) + 25;
					w = parseInt(c.match(/width=([0-9]+)/)[1]) + 25;
	
					$(this).attr('onclick',"").addClass('popWindowLink').attr('href',"javascript:popWindow('" + u + "'," + w + "," + h + ");");				
				}
			}

		////change all movie files
		//}else if($(this).attr('href') && $(this).attr('href').match(/.(flv|FLV)$/)){
		//	//get filename...
		//	u = $(this).attr('href').match(/(\/db\/.*.(flv|FLV)$)/)[1];
		//	$(this).addClass('popWindowLink').attr('href',"javascript:popWindow('/domains/mulderobdam.com/video.php?file=" + u + "',320,260);");		
		}
	});

	//hack maps...
	$(".era_maps_module iframe").attr('width','100%');
});
