$(document).ready(function() {

	$("#ticket").click(function(){

		if($("#ticket").attr('mode')=='ticket'){
			
			$("#ticket").css('background-image','url(http://www.rotown.nl/images/ticket_tipje.png)');
			var html="";
			$("#ticket_placeholder").html(html);
			$("#info_placeholder").show();
			$("#ticket").attr('mode','info');
			if($('#aanwezigList').length>0){
				$('#aanwezigList').uncolumnlists();
				$('#aanwezigList').makeacolumnlists({cols: 4, colWidth: 0, equalHeight: 'ul', startN: 1});
			}
			return;
		}
		if($("#ticket").attr('mode')=='info'){
			var paylogicid=$("#ticket").attr("paylogicid");
			var date=$("#date").val();
			var html="";
			//if(date>20120131){
			//	html="<iframe frameborder = '0' height = '525px' width = '450px' src = 'https://frontoffice.paylogic.nl/?event_id="+paylogicid+"&point_of_sale_id=78' scrolling = YES marginheight = 0 marginwidth = 0 name = 'paymentMenu'></iframe>";
			//}else{
				html="<iframe frameborder = '0' height = '525px' width = '450px' src = 'https://v1.paylogic.nl/frontoffice/?command=paymentMenu&merchantModuleID="+paylogicid+"' scrolling = NO marginheight = 0 marginwidth = 0 name = 'paymentMenu'></iframe>";
			//}

			$("#ticket_placeholder").html(html);
			$("#ticket").css('background-image','url(http://www.rotown.nl/images/info_tipje.png)');
			$("#info_placeholder").hide();
			$("#ticket").attr('mode','ticket');
			if($('#aanwezigList').length>0)
				$('#aanwezigList').makeacolumnlists({cols: 4, colWidth: 0, equalHeight: 'ul', startN: 1});
			return;
		}
	});
		
	$("#shout_body").focus(function(){
		$("#captacharow").show();
	}
	);
	
	$('.youtube_tumb_link').click(
		function(event){
			event.preventDefault(); 
			id=$(this).attr('id');
			$("#playvideo").html(play(id));  
		}
	);
	
	if($('#aanwezigList').length>0)
		$('#aanwezigList').makeacolumnlists({cols: 4, colWidth: 0, equalHeight: 'ul', startN: 1});

	if($('#aanwezigForm').length>0){
		$("#aanwezigForm").validate({
			errorPlacement: function(error, element) {
			error.appendTo($('#aanwezigForm'));
		}});

		$("#forwardForm").validate({
				wrapper: "p"
		});
	
		$('#aanwezigForm').ajaxForm({beforeSubmit: showAanwezigRequest,success: showAanwezigResponse});
		$('#forwardForm').ajaxForm({beforeSubmit: showForwardRequest,success: showForwardResponse});
		$('#commentForm').ajaxForm({beforeSubmit: showCommentRequest,success: showCommentResponse,dataType:'json'});

		$("#aanwezigdialog").dialog({ autoOpen: false });
		$("#flickrdialog").dialog({ autoOpen: false });
		$("#forwarddialog").dialog({ autoOpen: false, width:500, modal:true, zindex:3999 });
		
		$("#agendadialog").dialog({ autoOpen: false});
	}

	$(".ListViewItem").each(
		function(index){
			var id=$(this).attr('id').substr(5);
			var height=$(this).height();
			if($("#image_"+id).height()<height){
		//		$("#image_"+id).height(height);
			}
		}
	)
	
	$(".band_image").load(function(){
		
	});
});

function showAanwezigRequest(formData, jqForm, options){
	if($("#aanwezigForm").valid()){
		$("#aanwezigdialog").dialog('close'); 
	}	
}

function showAanwezigResponse(responseText, statusText){
	if(responseText>0){
		var naam=$('#aanwezigNaam').val();
		$('<li>' + naam + '</li>').appendTo('ul#aanwezigList');
		$('#aanwezigList').uncolumnlists();
		$('#aanwezigList').makeacolumnlists({cols: 4, colWidth: 0, equalHeight: 'li',startN:12});
		$('#aanwezigen').html('aanwezig zijn <span class="aantal">('+responseText+')</span>');
	}
}

function showForwardRequest(formData, jqForm, options){
	if($("#forwardForm").valid()){
		$("#forwarddialog").dialog('close'); 
	}	
}

function showForwardResponse(responseText, statusText){
	if(responseText>0){

	}
}

function showCommentRequest(formData, jqForm, options){

}

function showCommentResponse(shout, statusText){

	if(shout['error']==1){
		$("#captcha_error").html('De code komt niet overeen.');
		$("#captcha_error").css('color','#f00');
	}else{
		$("#captacharow").hide();	
		$("#captcha_error").html('Vul de code in.');
		$("#captcha_error").css('color','#000');

		$("#shouts").append('<div id="shout_'+shout.id+'" class="shout"><div class="header">posted by <b>'+shout.author+'</b> - '+shout.date+' <div style="float:right;"><a href="#" onClick="removeShout('+shout.id+')">remove</a></div> </div><div>'+shout.body+'</div></div>');
		$("#shout_body").val('');
		$("#shout_author").val('');
	}
}

function openDialogFlickr(){
	$("#flickrdialog").dialog('open');
}


function openDialogAanwezig(){
	$("#aanwezigdialog").dialog('open');
}

function openDialogForward(){
	$("#forwarddialog").dialog('open');
//	alert($("#forwarddialog").css('left'));

}

function openDialogAgenda(){
	$("#agendadialog").dialog('open');
}

function comment(){
	$("#commentForm").submit();
}

function play(id){ 
	var so = new SWFObject("http://www.youtube.com/v/"+id+"&enablejsapi=1&playerapiid=ytplayer", "mymovie", "206", "200", "8", "#ffffff");
	so.addParam("menu", "false");
	so.write("playvideo");
}; 

function removeShout(id){
	$.getJSON("/include/Beheer/removeShout.php?id="+id,
	function(data){
		$("#shout_"+data).remove();
	});
}
