initCufon();
$(document).ready(function(){

	$("a[rel*='external']").click(function(){
    	this.target="_blank";
	});
	
	if($('#images_container').length>0){
		$('#images_container').before('<div id="nav">').cycle({
			fx:     'fade', 
			pager: '#nav',
			before: loadText
		});	
	}
	initTweets('Beursvloer_IJSS');
	
	initCkeditors();
	
	initFaqAccordion();
	
	adminCopyBlockTexts();
	adminBlockTextsListener();
});


function adminBlockTextsListener(){
	if($('#BlockAdminEditForm').length>0){
		$("#BlockTitle, #BlockContentRule1, #BlockContentRule2, #BlockButtontext, #BlockLinkTo").keyup(function(){
			adminCopyBlockTexts();
		});
	}
	
	if($('#BlockAdminEdithomepageForm').length>0){
		$("#BlockContentRule1, #BlockContentRule2, #BlockButtontext, #BlockLinkTo").keyup(function(){
			adminCopyBlockTexts();
		});
	}
}

function adminCopyBlockTexts(){
	if($('#BlockAdminEditForm').length>0){
		$("#block_preview h3").html($('#BlockTitle').val());
		$("#block_preview .block_text .rule1").html($('#BlockContentRule1').val());
		$("#block_preview .block_text .rule2").html($('#BlockContentRule2').val());
		$("#block_preview .button_colored").html($('#BlockButtontext').val());
		$("#block_preview .button_colored").attr('href',$('#BlockLinkTo').val());
		initCufon();
	}
	
	if($('#BlockAdminEdithomepageForm').length>0){
		$("#block_preview .block_text .rule1wide").html($('#BlockContentRule1').val());
		$("#block_preview .block_text .rule2wide").html($('#BlockContentRule2').val());
		$("#block_preview .button_colored").html($('#BlockButtontext').val());
		$("#block_preview .button_colored").attr('href',$('#BlockLinkTo').val());
	}	
}


function initFaqAccordion(){
	if($('.accordion').length>0){
		$('.accordion .text').css('display','none');
		$('.accordion .title').click(function(){
			$(".accordion.shown .text").slideUp(500, function(){
				$(this).parent().removeClass('shown');
			});
			if (!$(this).next().is(':visible')) {
				$(this).next().slideDown(500, function(){
					$(this).parent().addClass('shown');
				});
			}
		});
	}
}

function initCufon(){
	Cufon.replace('.block_colored h3');
	Cufon.replace('#introtext h1');
	Cufon.replace('.footer_column h4');
	Cufon.replace('#date_time_location span');
	
	Cufon.replace('#twitter_head_text .firstline');
	Cufon.replace('#twitter_head_text .secondline');

	Cufon.replace('#partners');
	
	Cufon.replace('#column2 h1');
}

function initCkeditors(){
	if($('textarea[id="PageContent"]').length>0){
		$('textarea[id="PageContent"]').ckeditor(function(){ /* callback */},{ width: '445' });
	}
	
	if($('textarea[id="NewsitemContent"]').length>0){
		$('textarea[id="NewsitemContent"]').ckeditor(function(){ /* callback */},{ width: '445' });
	}
	
	if($('textarea[id="DealContent"]').length>0){
		$('textarea[id="DealContent"]').ckeditor(function(){ /* callback */},{ width: '445' });
	}
	
	if($('textarea[id="FaqitemAnswer"]').length>0){
		$('textarea[id="FaqitemAnswer"]').ckeditor(function(){ /* callback */},{ width: '445' });
	}
	
	if($('textarea[id="ProjectgroupmemberExtratext"]').length>0){
		$('textarea[id="ProjectgroupmemberExtratext"]').ckeditor(function(){ /* callback */},{ width: '445' });
	}	
}

function initTweets(username){
	if($('#tweets').length>0){
		$("#tweets").tweet({
		    username: username,
		    count: 3,
		    loading_text: "Laatste tweets aan het laden...",
		    template: "{text} {time}"
		}).bind("loaded",function(){$(this).find("a").attr("target","_blank");});
	}
	
	if($('#twitterpage').length>0){
		$("#twitterpage").tweet({
		    username: username,
		    count: 20,
		    loading_text: "Tweets aan het laden...",
		    template: "{text} {time}"
		}).bind("loaded",function(){$(this).find("a").attr("target","_blank");});
	}
	
}

function loadText() {
	$('#infoblock #info').hide().html(this.title).fadeIn('slow');
}
