// common js stuff...
$(document).ready(function(){

	//background
	$('#container').wrap('<div id="scroller"></div>');
	$('<div id="background"><img src="/domains/kuukswonen.nl/images/kw-bg4.jpg" border="0" alt="" /></div>').insertBefore("#scroller");
	
	//hide blank br
	$(".article .content .module_contents").each(function(){
		if($(this).prev('br').length == 1)
			$(this).prev('br').css('display','none');
	});
	
	//clear last line
	$("#middle .article:last").css('border','0');

	//rebuild store listing
	$(".store_module .listing div.article").not(':hidden').slice(0,3).wrapAll('<div class="row clear"></div>');
	$(".store_module .listing div.article").not(':hidden').slice(3,6).wrapAll('<div class="row clear"></div>');
	$(".store_module .listing div.article").not(':hidden').slice(6,9).wrapAll('<div class="row clear"></div>');

	//rebuild cart
	$(".shop_cart td.price,.shop_cart td span.price").html('');
	$(".shop_cart .input").remove();

	//change all popups...
	var img = new Array();
	$(".article a").each(function(){
		s = "dow.open(this.href,";
		i = 0;
		if($(this).attr('onclick'))
			i = $(this).attr('onclick').toString().indexOf(s);
	   
		if(i){
			w = 640;
			h = 480;
		   
			u = $(this).attr('href');
			var id = 'popwinlink_' + uid();
			e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
		   
			$(this).attr('id',id);
			if(e == 'png' || e == 'gif' || e == 'jpg'){
				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 = $("a#" + id).attr('onclick');
				c = $("a#popwinlink_0").attr('onclick');
			   
				h = c.substring(c.indexOf('height=') + 7);
				h = h.substring(0,h.indexOf(','));
			   
				w = c.substring(c.indexOf('width=') + 6);
				w = w.substring(0,w.indexOf(','));
			   
				$("a#" + id).attr('onclick',"").addClass('popWindowLink').attr('href',"javascript:popWindow('" + u + "'," + w + "," + h + ");");
			}
		}
	});

});

//make uniqID
var uid = (
	function(){
		var id=0;
		return function(){
			return id++ ;
		};
	}
)();

//emulate popup function...
function popWindow(u,w,h){
	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>");
	else
		popEl = $("<div class=\"popWindowDiv\" style=\"display:block;\"><iframe src=\""+ u +"\" width=\"" + w + "\" height=\"" + h + "\" border=\"0\" frameborder=\"0\"></iframe></div>");
	popEl.modal();
	$("#modalContainer").css('height',h).css('width',w).css('margin-left',(w / 2) * -1);
}
