var justDragged = false
var inter;

$(function(){
	var isiPad = navigator.userAgent.match(/iPad/i) != null;
	if(!(isiPad)){
		$( ".draggable:not(.flash)" ).draggable({stack:'.draggable', stop: function(event, ui) { 
				justDragged = true;
				inter = setInterval(resetDragged,100);
			}
	 	});
	 	
	 	$(".flash").mouseover(function(){
	 		$( ".draggable" ).draggable( "option", "disabled", true );
	 	});
	 	
	 	$(".flash").mouseout(function(){
	 		$( ".draggable" ).draggable( "option", "disabled", false );
	 	});
	 	
 	}else{
 		$(".content-item-big").each(function(event){
			$("span",this).css("visibility","visible");
			$("div.item-play",this).css("visibility","visible");
		});
 		$(".content-item-small").each(function(event){
			$("span",this).css("visibility","visible");
			$("div.item-play",this).css("visibility","visible");
		});
		$("#footer").hide();
 	}
	
	$.getJSON("http://twitter.com/statuses/user_timeline.json?screen_name=Mediawerkplaats&count=3&callback=?",
		function(data){
			$("#jstweet").html();
		$.each(data, function(i,item){
			ct = item.text;
			ct = ct.replace(/http:\/\/\S+/g,  '<a href="$&" target="_blank">$&</a>');
		    ct = ct.replace(/\s(@)(\w+)/g,    ' @<a href="http://twitter.com/$2" target="_blank">$2</a>');
		    ct = ct.replace(/\s(#)(\w+)/g,    ' #<a href="http://search.twitter.com/search?q=%23$2" target="_blank">$2</a>');
			$("#jstweet").append('<h3>Mediawerkplaats:</h3><p>'+ct +"</p>");
			});
	});
	
	if($("#content").height() > $("#rightbar").height()){
		$("#rightbar").height($("#content").height());
	}
	else if($("#rightbar").height() > $("#content").height()){
		$("#content").height($("#rightbar").height());
	}
	$(".popup").fancybox({
		'width'				: 960,
		'height'			: 425,
		'autoScale'			: false,
		'autoDimensions'	: false,
		'padding'			: 0,
		'margin'			: 0,
		'type'				: 'iframe'
	});
	

	$(".popup div").click(function(event){
		if(justDragged){
			event.stopPropagation();
			return false;
		}
	});
	
	$(".content-item-big").mouseover(function(event){
		$("span",this).css("visibility","visible");
		$("div.item-play",this).css("visibility","visible");
	});
	
	$(".content-item-big").mouseout(function(event){
		$("span",this).css("visibility","hidden");
		$("div.item-play",this).css("visibility","hidden");
	});
	
	$(".content-item-small").mouseover(function(event){
		$("span",this).css("visibility","visible");
		$("div.item-play",this).css("visibility","visible");
	});
	
	$(".content-item-small").mouseout(function(event){
		$("span",this).css("visibility","hidden");
		$("div.item-play",this).css("visibility","hidden");
	});
	
	var options = { 
        success:       showResponse  // post-submit callback 
    }; 
    	$('#nieuwsbrief_form').ajaxForm(options); 
    
	
});


function showResponse(responseText, statusText, xhr, $form)  { 
	if(responseText == "ERROR"){
		$("#m28d4eemail").addClass("error");
	}else{
		$("#responder").html("<br /><p>Bedankt voor je inschrijving!</p>");
	}
}

function resetDragged(){
	clearInterval(inter);
	justDragged = false;
}


function showCat(cat){

	if(cat == "all"){
		$(".content-item-small").show();
		$(".content-item-big").show();
	}else{
		$(".content-item-small").hide();
		$(".content-item-big").hide();
		$("."+cat).show();
	}
	
}


