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


//config sIFR
var sari = {
	src: '/domains/johwin.nl_2/fonts/Sari_Bold.swf'
	,ratios: [8, 1.42, 9, 1.33, 12, 1.35, 15, 1.3, 19, 1.28, 27, 1.27, 28, 1.25, 32, 1.26, 33, 1.24, 34, 1.25, 48, 1.24, 52, 1.23, 53, 1.24, 73, 1.23, 76, 1.22, 77, 1.23, 81, 1.22, 82, 1.23, 1.22]
};
sIFR.useStyleCheck = true;
sIFR.activate(sari);


$(document).ready(function(){
	
	//fix columns
	if($("#middle").height() < $("#content").height())
		$("#middle").height($("#content").height());
		
	//catalogus
	$(".catalog_module .item:odd").css("margin-right",'0px').css("float",'right');
	$(".catalog_module .title").addClass('clear');

	//clear search val?
	$("#site_search input:eq(0)").focus(function(){
		if($(this).val() == 'search')
			$(this).val('');
	});

	//top and left submenu's
	$("#menu ul ul li").hover(
		 function(){
			 if($(this).children('ul:eq(0)').is(':hidden'))
				 $(this).children('ul:eq(0)').css('display','block');
		 },
		 function(){
			 if($(this).children('ul:eq(0)').is(':visible'))
				 $(this).children('ul:eq(0)').css('display','none');
		 }
	);
	$("#menu ul li").mouseover(
		 function(){
			if($(this).children('ul:eq(0)').is(':hidden')){
				$('ul.visibleSubmenu').addClass('removeSubmenu').css('display','none');
				$(this).children('ul:eq(0)').addClass('visibleSubmenu').css('display','block');
			}
		 }
	);

	//set clicked button down
	$("#menu a.buttonID_" + $("#container").attr('class').split('mainButtonID_')[1]).addClass('buttonDown');

	
	//change all popups...
	var img = new Array();
	$(".article a").each(function(){
		i = '';
		s = "dow.open(this.href,";
		if($(this).attr('onclick'))
			i = $(this).attr('onclick').toString().indexOf(s);

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

				$link = $(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)
						$link.attr('onclick',"").addClass('popWindowLink').attr('href',"javascript:popWindow('" + u + "'," + w + "," + h + ");");
					}
					img[id].src = u;
				}
			}
		}
	});
	
	//change the stupid HR behavour of IE...
	if($.browser.msie)
		$("#content hr").css('display','none').wrap("<div class=\"hr\"></div>");


	/**
	* sIFR replacements
	*/
	
	/* == Converts color to HEX == */
	var hex = function(N) {
		if(N==null) return "00";
		N = parseInt(N);
		if(N==0 || isNaN(N)) return "00";
		N = Math.max(0, N);
		N = Math.min(N, 255);
		N = Math.round(N);
		return "0123456789ABCDEF".charAt((N - N%16) / 16) + "0123456789ABCDEF".charAt(N%16);
	};
	/* == Converts colors to HEX == */
	var hexed = function(color) {
		if(!color) { return false; }
		if(color.search('rgb') > -1) {
			color = color.substr(4,color.length-5).split(', ');
			color = hex(color[0]) + hex(color[1]) + hex(color[2]);
		}
		color = color.replace('#','');
		if(color.length < 6) {
			color = color.substr(0, 1) + color.substr(0, 1) + color.substr(1, 1) + color.substr(1, 1) + color.substr(2, 1) + color.substr(2, 1);
		}
		return '#' + color;
	};
	
	//do replacements
	$('.article h1.title').each(function(){
		$title = $(this);
		sIFR.replace(sari, {
								elements: $title
								,wmode: 'transparent'
								,css: [
								  '.sIFR-root { font-size: ' + (parseInt($(this).css('fontSize'))) + '; color: ' + hexed($(this).css('color')) + '; }'
								  ,'a { text-decoration: none; color: ' + hexed($(this).css('color')) + '; }'
								  ,'a:hover { color: ' + hexed($(this).css('color:hover')) + '; }'
								]
							});
		//sIFR.debug.ratios({ src: '/domains/johwin.nl_2/fonts/Sari_Bold.swf', selector: $(this) });
	});
	
	//patch title margin
	//$("div.title").css('padding-bottom','0');

	//** End sIFR **//


	//change colom hights, do it at last so we have all hights...
	var f = 0;
	$("#middle .article").each(function(){
		if($(this).hasClass('colom')){
			f++;
		}else if($(this).hasClass('whole')){
			f = 0;
		}
		if(f == 2){
			$(this).addClass('right_colom');
			if($(this).height() < $(this).prev().prev().height())
				$(this).height($(this).prev().prev().height());
			else
				$(this).prev().prev().height($(this).height()/* + 3*/); //3px is FF patch!
			f = 0;
		}
	});
});


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

