var video_content = function() 
{
	this.init();
}

function unescapeHTML(string) 
{
	return string.replace(/&lt;/g, '<').replace(/&gt;/g, '>');
}

function get_parameter_from_url(url, name)
{
	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS );
	  var results = regex.exec(url);
	  if( results == null )
	    return "";
	  else
	    return results[1];
}

video_content.prototype = {
				
	init: function() 
	{
		this.layout = $('#layout');
		this.jFade = $('#fade');
		this.popup = $('#popup_big');
		this.close_button = this.popup.find('.close');
		this.video_popup_link = $('.video_popup_link');
				
		this.video_content = $('#video_content');

		if ( !this.video_content.size() )
		{
			this.video_content = $(document.createElement('div')).attr('id', 'video_content');
			$('#popup_big > .content').append(this.video_content);
		}

		var that = this;
		this.video_popup_link.click(function(event)
		{
			var the_link = $(this);
			var new_link = the_link[0].href;
			var links = new_link.split(",");
			var many_videos = (links.length > 2) ? true : false;
			
			event.preventDefault();
			if(/tv.gosloto.ru/.test(new_link) || (/tvgosloto/.test(new_link))){
				
				for(var m = 0; m < links.length; m++){
					
					if(/tv.gosloto.ru/.test(links[m]) || (/tvgosloto/.test(links[m]))){
						window.location.href = links[m];
					}
				}
			}
			else{
				if (typeof(video_player_popup_on_open) !== 'undefined')
					video_player_popup_on_open();
				
				var html = '';
				var object_class = '';
				
				html += '<div id="video_player_the_video_itself_container">';
				
				for(var n = 0; n < links.length; n++){
					var gameType = '';
					if(many_videos){
						if(links[n].split(";")){
							var a = links[n].split(";");
							
							if(a.length > 1){
								links[n] = a[0];
								gameType = a[1];
							}
						}
						
					}
					
					if( /rutube.ru/.test(links[n]) )
					{
						html += '<div class="popup-video"' + ((gameType != '') ? 'id="'+gameType+'"' : '') + 'style="display: none">';
							var	new_class = links[n].match(/\.ru\/([\w]+)$/)[1];
							
							html += '<object id="video_player_the_video_itself" width="640" height="390"><param name="movie" value="http://video.rutube.ru/' + new_class + '"></param><param name="wmode" value="window"></param><param name="allowFullScreen" value="true"></param><embed src="http://video.rutube.ru/' + new_class + '" type="application/x-shockwave-flash" wmode="window" width="640" height="390" allowFullScreen="true" ></embed></object>';
						html += '</div>';
					} 
					else if( /yandex.ru/.test(links[n]) )
					{
						html += '<div class="popup-video"' + ((gameType != '') ? 'id="'+gameType+'"' : '') + 'style="display: none">';
							html += '<object id="video_player_the_video_itself" width="640" height="390" style="margin-top: -50px;"><param name="video" value="' + links[n] + '"/><param name="allowFullScreen" value="true"/><param name="scale" value="noscale"/><embed src="' + links[n] + '" type="application/x-shockwave-flash" width="640" height="390" allowFullScreen="true" scale="noscale"/></object>';
						html += '</div>';
					} 
					else if( /youtube.com/.test(links[n]) )
					{
						html += '<div class="popup-video" style="display: none;"' + ((gameType != '') ? 'id="'+gameType+'"' : '') + '>';
							html += '<iframe width="640" height="390" align="left" src="http://www.youtube.com/embed/' + get_parameter_from_url(links[n], 'v') + '" frameborder="0" allowfullscreen></iframe>';
						html += '</div>';
					}
						/*else
						{
						var video_src = links[n].attr('video_src');
							if (!video_src)
							{
//			//					alert("Неподдерживаемая ссылка на всплывающее видео: " + new_link);
								return;
							}
							
							html += video_src;
						}*/
				}
				
				html += "</div>";
				
				if (typeof(video_player_get_extra_markup) !== 'undefined')
				html += video_player_get_extra_markup(the_link);
				
				that.video_content.html(html);
				
				if(links.length > 0){
					$('#video_type').find('.currentSwitcher').removeClass('currentSwitcher').end().find('li:first-child span').show().addClass('currentSwitcher');
					$('#video_player_the_video_itself_container div:first-child').show().addClass('currentVideo');
				}
				
				// Переключение по видосам
				$('#video_type li span').click(function(){
					var videoSwitcher = $(this);
					
					if(videoSwitcher.hasClass('currentSwitcher'))
						return;
					
					$('#video_type').find('.currentSwitcher').removeClass('currentSwitcher');
					videoSwitcher.addClass('currentSwitcher');
					
					var toVideo = videoSwitcher.attr('id').split("-")[1];
					
					videoSwitcher.addClass('currentSwitcher');
					
					$('#video_player_the_video_itself_container').find('.currentVideo').removeClass('currentVideo').hide().end().find('#' + toVideo).show().addClass('currentVideo');
				});
				
				that.popup.addClass('video_popup').removeClass('not_display');
				
				that.jFade.removeClass('not_display');
				if( $.browser.msie && $.browser.version == 6 )
					that.jFade[0].style.height = that.layout[0].offsetHeight + 'px';
				
				that.video_content.removeClass('not_display');
				
				var top_value = document.documentElement.scrollTop || document.body.scrollTop;
				
				that.popup.css
				({
					marginLeft: - that.popup[0].offsetWidth/2,
					top: top_value + 50
				});
				
				return false;
			}
			
		});
		
		this.close_button.click(function()
		{
			if (typeof(video_player_popup_on_close) !== 'undefined')
				video_player_popup_on_close();
			that.popup.addClass('not_display').removeClass('video_popup').removeAttr('style');
			that.jFade.addClass('not_display').removeAttr('style');
			that.video_content.addClass('not_display');
			that.video_content.find('iframe').attr('src', '');// очищаем атрибут src, чтобы остановить видео
		});
	}
};

$(function()
{
	window.gosloto_video_content = new video_content();
});
