var NicanRating = {
		
		ShowMessage: function( id, text )
		{
			var TextBox = $('RatingText' + id);
			
			if(TextBox == null)
				return;
			
			TextBox.set('html', text );
			TextBox.set('opacity', 1 );
			
			this.HideMessage.delay( 1000 + text.length * 30, this, TextBox);
			
		},

		HideMessage: function( obj ){
			obj.fade( 0 );
		},
		
		VoteTopic: function( msgid, rating ){
			
			var request = new Request.JSON({
				url: smf_scripturl + '?action=nicanrating&msg=' + msgid + '&rating=' + rating,
				onComplete: function( jsonObj ){
					NicanRating.JSONChangeRatings( jsonObj )
				}
			}).send();
			
		},
		
		JSONChangeRatings : function( jsonObj ){
			
			if( !jsonObj ){
				alert("Internal error.");
				return;	
			}
			
			var RatingElement = $( 'rating' + jsonObj.msg + '.' + jsonObj.ratingtype );
			
			if( RatingElement && jsonObj.newcount ){
				RatingElement.set('html', "x" + jsonObj.newcount );			
			}
			
			this.ShowMessage( jsonObj.msg, jsonObj.string );
			
		}
		
};

function VideoCleanUrl( link )
{	
	if( link.contains( "youtube.com" ) ){
		
		var StrId = link.search(/v=/);
		var VideoId = link.substr( StrId + 2 );
		
		var obj = '<object width="480" height="295"><param name="movie" value="http://www.youtube.com/v/' + VideoId +'&hl=en&fs=1"></param>'
			+ '<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>'
			+ '<embed src="http://www.youtube.com/v/' + VideoId +'&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="295"></embed>'
			+ '</object>';
		
			return Array( obj, 480, 295, "YouTube" );
	
	} else if( link.contains( "wegame.com" ) ){
		
		var StrId = link.search(/watch/);
		var VideoId = link.substr( StrId + 6 ).replace(/\//, "");
		
		var obj = '<object width="480" height="387">' + 
		'<param name="movie" value="http://www.wegame.com/static/flash/player.swf?xmlrequest=http://www.wegame.com//player/video/'+VideoId+'"></param>' + 
		'<param name="flashVars" value="xmlrequest=http://www.wegame.com/player/video/'+VideoId+'&embedPlayer=true"></param><param name="allowFullScreen" value="true"></param>' + 
		'<embed src="http://www.wegame.com/static/flash/player.swf?xmlrequest=http://www.wegame.com/player/video/'+VideoId+'&embedPlayer=true" type="application/x-shockwave-flash" allowfullscreen="true" width="480" height="387">' + 
		'</embed></object>';
		
		return Array( obj, 480, 387, "WeGame" );
	
	} else if( link.contains( "revver.com" ) ){
		
		var StrId = link.search(/video\//);
		var VideoId = link.substr( StrId + 6 );
		
		StrId = VideoId.search(/\//);
		VideoId = VideoId.substr( 0, StrId );			
	
		var obj = '<object width="480" height="392" data="http://flash.revver.com/player/1.0/player.swf?mediaId='+VideoId+'" type="application/x-shockwave-flash">'
			+ '<param name="Movie" value="http://flash.revver.com/player/1.0/player.swf?mediaId='+VideoId+'"></param><param name="FlashVars" value="allowFullScreen=true"></param><param name="AllowFullScreen" value="true"></param>'
			+ '<param name="AllowScriptAccess" value="always"></param><embed type="application/x-shockwave-flash" src="http://flash.revver.com/player/1.0/player.swf?mediaId='+VideoId+'" pluginspage="http://www.macromedia.com/go/getflashplayer" allowScriptAccess="always" flashvars="allowFullScreen=true" allowfullscreen="true" height="392" width="480">'
			'</embed></object>';
		
		return Array( obj, 480, 400, "Revver" );
		
	} else if( link.contains( "video.google.com" ) ){
		
		var StrId = link.search(/docid=/);
		var VideoId = link.substr( StrId + 6 ).replace(/\//, "");
		
		var obj = '<embed id="VideoPlayback" src="http://video.google.com/googleplayer.swf?docid='+VideoId+'&hl=en&fs=true" style="width:400px;height:326px" allowFullScreen="true" allowScriptAccess="always" type="application/x-shockwave-flash"> </embed>';
	
		return Array( obj, 400, 326, "Google videos" );
		
	} else if ( link.contains( "vimeo.com" ) ) {
		
		var StrId = link.search(/\//);
		var VideoId = link.substr( StrId + 1 ).replace(/\//, "");
		
		
		var obj = '<object width="400" height="302"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" />' + 
		'<param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=1334121&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" />' + 
		'<embed src="http://vimeo.com/moogaloop.swf?clip_id=1334121&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="302">' + 
		'</embed></object>';
		
		return Array( obj, 400, 302, "Vimeo" );
		
	}
	
	
	return null;
}

function OpenNicanFrame( frame ){
	
	var elem = $( frame );
	
	if( elem == null )
		return;
	
	var html = elem.getElements('a').toString();
	
	if( html == null || html.length < 5 )
		return;
	
	elem.set('html', '<iframe src ="'+ html +'" width="100%" style="height: 480px"></iframe>' );
	
}

window.addEvent('domready', function() {

	var Videos = $$('div.DisplayVideo');
	
	Videos.each( function(elem){
		
		var link = elem.getElements('a').toString();
		var newhtml = VideoCleanUrl( link );
		
		if( newhtml == null )
			return;
		
		elem.set('html', newhtml[3] + ' - <a href="' + link +'">Link</a> <br>' + newhtml[0] + '<p>');
		elem.set('styles', { 'width': newhtml[1] + 10 + "px" });
		
	} );
	
	var Frames = $$('div.DisplayFrame');
	
	Frames.each( function(elem, x){
		
		var link = elem.getElements('a').toString();
		var oldhtml = elem.get('html');
		var html = '<table width="100%"><tr><td colspan="2" align="center" id="NicanFrame_'+ x +'">'+ oldhtml +'</td></tr>'
		+ '<tr><td align="center"><a href="'+link+'">Open link</a></td>'
		+ '<td align="center"><a href="javascript:void(0);" onclick="OpenNicanFrame(\'NicanFrame_'+ x +'\')">Open frame</a></td></tr></table>';
		
		elem.set('html', html );
		
	} );
	
	var Spoilers = $$('div.PostSpoilers');
	
	Spoilers.each( function(elem, x){
		
		if( elem.getParents('div.PostSpoilers').length >= 5 ){
			elem.set('html', 'Too many spoiler tags!');
			return;
		}
		
		
		var HiddenBox = elem.getFirst().getFirst();
		var HiddenBoxEffect = new Fx.Morph(HiddenBox);
		
		HiddenBoxEffect.addEvent('complete', function(){
			if( HiddenBoxEffect.Hidden == false )
				HiddenBox.setStyle('height', 'auto');
		} )
		
		HiddenBoxEffect.Hidden = true;
		
		var SpoilerHeader = new Element('a', {
			'class': 'quoteheader',
			'html': "Spoiler: Show/Hide",
			'href': "#",
			'styles': {
				'color': '#ffe9d0'
			},
			'events': {
				'click': function(e){
					e.stop();
					
					HiddenBoxEffect.cancel();
					HiddenBoxEffect.start({
						'height': HiddenBoxEffect.Hidden ? [5,HiddenBox.scrollHeight] : [HiddenBox.scrollHeight,5],
						'background-color': HiddenBoxEffect.Hidden ? '#888888' : '#AA4444',
					});	
					HiddenBoxEffect.Hidden = !HiddenBoxEffect.Hidden;
				}
			}
		} );
		
		elem.grab( SpoilerHeader, 'top' );
		
	} );
	
	
	
});