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


function trim(str){
	if(!str || typeof str != 'string')
		return null;
	return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
}

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

var global = this; 
$(document).ready(function(){

	/* SEARCH FORM */
	$("#site_search span.go").click(function(){
    	$("#siteSearchForm").submit();
	});

	/* MENU BUTTONS */
    $("#menu > ul > li > a, #bottom_menu ul li a").each(function(){
    	t = $(this).html();
		if(t){
			$(this).html("<img class=\"buttonImg\" src=\"/domains/project346.nl/generate/button.php?txt=" + t + "\" border=\"0\" />");
	
			//hover
			$("#menu img.buttonImg, #bottom_menu img.buttonImg").parent('a').hover(
				function(){
					if(!$(this).children('.buttonImg').attr('origSrc'))
						$(this).children('.buttonImg').attr('origSrc',$(this).children('.buttonImg:eq(0)').attr('src'));
	
					s = $(this).children('.buttonImg').attr('origSrc');
					$(this).children('.buttonImg').attr('src',s + '&over=1');
				},
				function(){
					s = $(this).children('.buttonImg').attr('origSrc');
					$(this).children('.buttonImg').attr('src',s);
				}
			);
		}
    });

	/* ENABLE SUBMENU */
    $("#menu 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');
		}
    );
	//change some html
 	$("#menu ul ul li:last-child, #menu ul ul li:last-child > a").addClass("last");
 	$("#menu ul ul li:first-child, #menu ul ul li:first-child > a").addClass("first");
	$("#menu ul ul").wrapInner("<div class=\"ulTop clear\"><div class=\"ulBack clear\"></div></div>");


	/* ANIM FLAGS */
	var lang = new Array();
	lang['lang_0'] = 'nl';
	lang['lang_1'] = 'uk';
	lang['lang_2'] = 'fr';
	lang['lang_3'] = 'de';
	$("#header_overlay .lang a").hover(
		function(){
			$(this).css('background-image',"url(/domains/project346.nl/images/flags/" + lang[$(this).attr('class')] + "_a.gif)");
		},
		function(){
			$(this).css('background-image',"url(/domains/project346.nl/images/flags/" + lang[$(this).attr('class')] + "_o.gif)");
		}
	);

	/* TITLES */
    $(".title h1.title").each(function(){
		if($(this).children('a').length){
	    	t = $(this).children('a:first').html();
			if(t)
				$(this).children('a:first').html("<img src=\"/domains/project346.nl/generate/title.php?txt=" + t + "\" alt=\"" + t + "\" border=\"0\" />");
		}else{
	    	t = $(this).html();
			if(t)
				$(this).html("<img src=\"/domains/project346.nl/generate/title.php?txt=" + t + "\" alt=\"" + t + "\" border=\"0\" />");
		}
    });

	//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');
			
				if(c){
					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 + ");");				
				}
			}
		}
	});

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