
/**
 *	fires when the dom is ready
 *
 */
$(document).ready(function() {
	cufonReplace();
    slider();
	blockLink();
	logo();
	imgPreLoader();
	fetchTweets();
	externalLinks();
	gravityForm();	
	autoLightBox();
});

/**
 * wordpress theme url
 *
 */
function wpUrl() {
	return $('link[href$="reset.css"]').attr('href').replace('/assets/css/reset.css','');
}

/**
 *	replace targeted fonts using cufon
 *
 */
function cufonReplace() {
	Cufon.replace(
		'h1, h2, h3, #access > ul > li > a, #top_bar .text, #top_nav, #content .sub .meta, #submit, #copyright, .comment-top, .comment-reply-link, .gform_wrapper .gfield_label, .overview .eventdate, .overview h2 a, .content .date ', { 
		hover: true
	});
	Cufon.replace('.slide_title', { 
		textShadow: '#1e1e1e 0px 1px'
	});
}

function slider() {
	
	// slide events
	$.featureList(
        $("#tabs li h3"),
        $("#output li"), {
            start_item	:	0,
			transition_interval	: 10000
        }
    ); 	
	
	// youtube popup
	$(".youtubelink").click(function() {
		var youtubeid 	= $(this).attr('rel');
		if ( typeof youtubeid != 'undefined' ) {
			var href = 'http://www.youtube.com/watch?v='+youtubeid;				
			$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				//'overlayOpacity': '0.7', 
				//'overlayColor'	: '#111111',
				'title'			: this.title,
				'width'			: 680,
				'height'		: 495,
				'href'			: href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
					'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			}); 
		}		
		return false;
	});
	
	// vimeo popup
	$(".vimeolink").click(function() {
		var vimeoid 	= $(this).attr('rel');
		if ( typeof vimeoid != 'undefined' ) {		
			var href = 'http://vimeo.com/moogaloop.swf?clip_id='+vimeoid;			
			$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'			: 680,
				'height'		: 495,
				'href'			: href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
					'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			}); 
		}		
		return false;
	});
	
}

/**
 *	makes a div clickable
 *
 */
function blockLink() {
	var elements = '#sidebar a.readmore, #front_col_1 .readmore, .blocklink';	
	$(elements).each(function(){
		var thelink = $(this).attr('href');		
		var theDiv 	= $(this).parent();
		//if not, open in same window
		if ((thelink != '')&&(thelink != '#')) {
			theDiv.css({'cursor':'hand','cursor':'pointer'})			
			.click(function(){
				window.location.href = thelink;			
			});
		}
	});
	
};

/**
 *	logo
 *
 */
function logo() {
	// logo
	$("#logo img").hover(function(){
		$(this).stop().fadeTo(300, 0.8);
	},function(){
		$(this).stop().fadeTo(300, 1.0); 
	});	
}

/**
 *	Image preloader
 *
 */
function imgPreLoader() { 
	$(document).load(wpUrl()+"/assets/images/menu_subnav_bg.png");
}

/**
 *	Add Tweets to the frontpage
 *
 */
function fetchTweets() {

	var usernames = new Array('genderplatform', 'esteenhoven');	
	var tweetquery = 'genderjustice OR genderequality OR womensrights OR emancipatie OR vrouwenrechten OR mannenemancipatie OR UNSCR1325 OR Arabwomenpower'; //1325 OR 

	$("#front_col_3, .tweets_block").tweet({
        count: 3,
        fetch: 20,       
		//query: tweetquery,
        retweets: false,
        username: usernames,
		loading_text: "searching twitter...",
		template: "{join}{text}{time}"
	})
	.bind("empty", function() { 
		$(this).append("No matching tweets found"); 
	});
};

/**
 * External Link; New-Window Links in a Standards-Compliant W3C
 */
function externalLinks() { 
	
	var anchors = $("[rel*='external']");	
	
	for (var i=0; i<anchors.length; i++) {  
		var anchor = anchors[i];  
		
		anchor.target = "_blank"; 
		$(anchor).addClass("external");
		$(anchor).attr('title', $(anchor).attr('title') + ' Deze link wordt geopend in een nieuw venster');		
	}
}

/**
 *	Gravity Form - Cufon Refresh on Ajax validation
 */
function gravityForm() {
	
	if( !$('.ginput_container').length )
		return;
	
	// hook for graviy form ajax events
	$(document).bind('gform_page_loaded', function(e, id, current){
		// cufon label refresh
		Cufon.refresh();		

	});	
}

/**
 *	Lightbox
 *
 */
function autoLightBox() {
	// caption	
	$('#inner_content .item img').each( function() {	
		var title 		= $(this).attr('title');
		var link 		= $(this).parents('a');		
		if( title && link ) {
			var href 		= link.attr('href');
			if ( href ) {
				if (href.match(/\.(jpg|png|gif)/) ) {
					link.fancybox({
						'opacity'			: true,
						'href'				: href,
						'overlayColor'		: '#000',
						'hideOnContentClick': true,
						'overlayOpacity'	: 0.7,
						'centerOnScroll'	: true				
					});	
				}
			}
		}
	});	
}

