//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('fast', function () {
							dialog.container.show('fast', function () {
								dialog.data.slideDown('fast'); // See Other Notes below regarding
														   // data display property and
														   // iframe details
							});
						});
					},
			onClose: function (dialog) {
						dialog.data.slideUp('fast', function () {
						  dialog.container.hide('fast', function () {
							dialog.overlay.fadeOut('fast', 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 foundOnloadAlerts = new Array();
var alert = function(str){
	foundOnloadAlerts[foundOnloadAlerts.length] = str;
}

$(document).ready(function(){
	//my own alert...
	alert = function(str){
		popWindow('',300,100,$('<div id="alert_popup clear"><span class="string">' + str + '</span></div>'));
	}
	
	
	var img = new Array();
	
	if($(".auction_module").length){
		$(".auction_module .listing div.item").hover(
			function(){$(this).addClass('hover');},
			function(){$(this).removeClass('hover');}
		).click(function(){
			self.location.href = $(this).find('a').attr('href');
		});
		
		$(".auction_module .details .image a").click(function(e){
			e.preventDefault();

			var id = 'popwinlink_' + uid();
			var u = $(this).attr('href');
			img[id] = new Image();
			img[id].onload = function(){
				img[id].onload = null;
				w = img[id].width;
				h = img[id].height;

				popWindow(u,w,h);
			}
			img[id].src = u;
			
			return false;
		});
	}
	
	//change all popups...
	$(".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);");		
		}
	});

	//show all found alerts
	if(foundOnloadAlerts.length > 0){
		$.each(foundOnloadAlerts,function(){
			alert(this);
		});
	}
	
	//change the stupid HR behavour of IE...
	if($.browser.msie){
		$("#content hr").wrap("<div class=\"hr\"></div>");
		$("#content hr").css('display','none');
	}
});
