jQuery.fn.loading = function() {
	this.each(function() {
		var w = $(this).width();
		var offset = $(this).offset();
		var div = $('<img src="/gfx/loading.gif" style="position:absolute;top:'+offset.top+'px;left:'+(offset.left+w-16)+'px;" />');
		$(this).prepend(div);
		});
	return this;
};

jQuery.popup = function(content) {
	var div = $('<div></div>')
		.css('position','absolute')
		.css('top',$(window).scrollTop()+10)
		.css('left','50%')
		.css('padding','1em')
		.css('width','400px')
		.css('marginLeft','-200px')
		.css('background','white')
		.html(content);
	$('body').append(div).shade();
	return this;
};

jQuery.fn.dynaform = function() {
	$(this)
		.find('FORM')
		.submit(function(event) {
			event.preventDefault();
	
			$(this)
				.loading();
	
			$.post(this.action,$(this).serialize(),function(data) {
				wk_perform(data);
				replaceheader();
				});
			})
		;
	this
		.find('.tweet').click(function(event) {
			var uri = 'http://twitter.com/home/?status='+encodeURIComponent(this.form.body.value+' '+window.location);
			window.open(uri);
			})
		;
	this
		.find('A[class!=n]').click(function(event) {
			if (this.target=="_blank") return;

			event.stopPropagation();
			event.preventDefault();

			$(this).closest('div').loading();
			$.get(this.href,wk_perform);
			})
		;
	this
		.find('.quote').each(function() {
			$(this).closest('.item').hover(function() {
					$(this).find('.quote').fadeIn('fast');
				}, function() {
					$(this).find('.quote').fadeOut('fast');
				});
			});
	return this;
};

jQuery.fn.embedtube = function() {
	this.find('.embedtube').each(function() {
		swfobject.embedSWF($(this).text(),this.id,$(this).width(),$(this).height(),'9.0.0','/gfx/expressInstall.swf',{},{ wmode:"transparent" });
		});
	return this;
};

jQuery.fn.shade = function() {
	this.each(function() {
		var o = $(this).offset();
		var m = 4;
		
		$('<div class="shade" />')
			.css('position','absolute')
			.css('width',$('body').width()+'px')
			.css('height',$('body').height()+'px')
			.css('top','0px')
			.css('left','0px')
			.css('opacity',0.6)
			.css('background','black')
			.insertBefore(this)
			;
		});
	return this;
};

wk_show = function(id,qry) {
	$('#'+id).css('top',$(window).scrollTop()+10).fadeIn('slow').shade();
	$('#'+id).find('.late').each(function() {
		$.get('include/'+this.id+'.php'+qry,null,function(data) {
			wk_perform(data);
			replaceheader();
			});
		});
	return false;
};

wk_perform = function(data) {
	for (a in data) {
		if (a == 'popup') {
			for (b in data[a])
				$.popup(data[a][b]);
		}	else
		if (a == 'navigatetweetwin') {
			for (b in data[a]) {
				tweetwin.location=data[a][b];
			}
		}	else
		if (a == 'navigate') {
			for (b in data[a])
				window.open(data[a][b]);
		}	else
		if (a == 'iframe') {
			for (b in data[a]) {
				$('<iframe />').attr('src',data[a][b]).prependTo('BODY');
			}
		}	else
		if (a == 'show') {
			for (b in data[a])
				setTimeout('wk_show("'+data[a][b][0]+'","'+data[a][b][1]+'")', 500);
		}	else
		if (a == 'appear') {
			for (b in data[a]) {
				$('#'+b).children('img').remove();
				var div = $(data[a][b]);
				$('#'+b).append(div);
				$('#'+b).find('.error').delay(5000).fadeOut('fast');
			}
		}	else
		if (typeof data[a] == 'object') {
			$('#'+a).html('');
			for (b in data[a]) {
				var id = a+'_'+b;
				$('#'+a).append('<div class="item '+(b%2==0 ? ' odd' : '')+'" id="'+id+'">'+data[a][b]+'</div>');
			}
		}	else
			$('#'+a).html(data[a]);
		$('#'+a).dynaform().embedtube();
	}
};

jQuery.fn.dynamic = function() {
	this.find('.adyn').dynaform();
	this.find('.dyn').each(function() {
		$(this).loading();
		$.get('include/'+this.id+'.php',null,function(data) {
			wk_perform(data);
			replaceheader();
			});
		});
	return this;
};

$(document).ready(function() {
	$(this).embedtube().dynamic();
	navigatie();	
	});
	
function navigatie(){
		$("#menu li").hover(
		function(){
			var iconName = $(this).find("img").attr("src");
			var origen = iconName.replace("active","off");
			var origen = origen.split("_off")[0];
			
			$(this).find("img").attr({src: "" + origen + "_over.png"});
			
		},
		function(){
			var iconName = $(this).find("img").attr("src");
			var klasse = $(this).find("img").attr("class");
			var origen = iconName.split("_over")[0];
			if(klasse == 'active'){
			$(this).find("img").attr({src: "" + origen + "_active.png"});
			} else
			$(this).find("img").attr({src: "" + origen + "_off.png"});
			
		});
}

function HideContent(d) {
	if(d.length < 1) { return; }
	document.getElementById(d).style.display = "none";
	$('.shade').remove();
}

function ShowContent(d) {
	if(d.length < 1) { return; }
	document.getElementById(d).style.display = "block";
}

//var tweetwin=null;

function dotweet(form) {
	if (form.twitter.checked && form.naam.value!='' && form.body.value!='' && form.plaats.value!='') {
		var txt = 'Stem op mijn vraag zodat ik prachtige prijzen kan winnen!\n\nhttp://www.wkzomerhelden.nl/stel-een-vraag-aan-frits-of-barbara/'+form.ts.value+'-vraag';
		window.open('http://twitter.com/?status='+encodeURIComponent(txt));
	}
}

