YUI().use( 'node', 'event', function( Y ) {
	Y.on( 'domready', AddEvents );

	function AddEvents() {
		// page name is set in 'name' property of buttons, so just load to that page
		Y.all( 'div.container' ).each( function( newsItem ) { EnableSocialButtons( newsItem ); } );
	}

	function EnableSocialButtons( newsItem ) {
		if( ! Y.one( '.container_social_share' ) )
			return;

		var title = newsItem.one( '.container_title' ).get( 'text' );

		var text = "";
		pNodes = newsItem.all( 'p' );
		for ( var i = 0; i < pNodes.size(); i++ )
			text += pNodes.item( i ).get( 'innerHTML' );

		// remove tags and text which are not needed for Facebook
		text = text.replace( new RegExp( '<span class="image_description">.*?</span>', 'g' ), '', text ); // remove image descriptions
		text = text.replace( new RegExp( "</?span[^>]*>", "g" ), '', text ); // remove other span's
		text = text.replace( new RegExp( "<a [^>]*PopupImage[^>]*>.*?</a>", "g" ), '', text ); // remove PopupImage links
		text = text.replace( new RegExp( "<a [^>]*>(more|više)\.\.\.</a>", "g" ), '', text ); // remove PopupImage links

		// extract first image, and remove others..
		var image = "";
		if( text.match( '<img src=' ) )
			image = text.match( /<img src="([^"]*)"[^>]*>/ )[1];

		text = text.replace( new RegExp( '<img[^>]*>', 'g' ), '', text );

		// remove newlines and multiple <br> tags...
		text = text.replace( new RegExp( '[\t\n]', 'g' ), '', text );
		text = text.replace( new RegExp( '(<br( /)?>){2,}', 'g' ), '<br />', text );
		text = text.replace( new RegExp( '^<br( /)?>', 'g' ), '', text );

		// get url
		var url = newsItem.one( '.container_title' ).getAttribute( 'name' );

		newsItem.all( '.container_social_share img' ).each( function ( shareIcon ) {
			shareIcon.on( 'mouseover', function( e ) { this.setAttribute( 'src', this.getAttribute( 'src' ).replace( 'disabled', 'send' ) ); } );
			shareIcon.on( 'mouseout', function( e ) { this.setAttribute( 'src', this.getAttribute( 'src' ).replace( 'send', 'disabled' ) ); } );

			if( shareIcon.getAttribute( 'src' ).match( /facebook/ ) )
				shareIcon.on( 'click', function( e ) { window.location = "http://www.facebook.com/sharer/sharer.php?u=" + encodeURIComponent( url ) + "&t=" + encodeURIComponent( title ) + "&v=3"; } );

			if( shareIcon.getAttribute( 'src' ).match( /twitter/ ) )
				shareIcon.on( 'click', function( e ) { window.location = "http://twitter.com/intent/tweet?source=webclient&text=" + encodeURIComponent( '"' + title + '" ' + url ); } );

			shareIcon.on( 'mouseout', function( e ) { this.setAttribute( 'src', this.getAttribute( 'src' ).replace( 'send', 'disabled' ) ); } );
		} );
	}
} );

function PopupImage( url, image_size ) {
	window_width  = parseInt( image_size.split( "x" )[0] );
	window_height = parseInt( image_size.split( "x" )[1] );

	url = "popup_image.php?url=" + encodeURIComponent( url );

	window.open( url, "big_picture_window", "width=" + window_width + ",height=" + window_height + ",directories=false,hotkeys=false,location=false,menubar=false,resizable,status=false,toolbar=false" );
}

