
CreatoorSN.namespace('videos');
CreatoorSN.videos = {
	
	width: 425,
	height: 350,
	playerId: null,
	player: null,
	playlistArray: null,
	isUnix: false,
	instanceWidth: null,
	instanceHeight: null,
	
	
	embedYoutubeVideo : function (media_content_url, media_content_type, target, width, height) {
		
		width = width ? parseInt(width) : CreatoorSN.videos.width;
		height = height ? parseInt(height) : CreatoorSN.videos.height;
		var videoUrl = media_content_url.replace(/\?/g, '%3F').replace(/=/g, '%3D').replace(/&/g, '%26');
		var date = new Date();
        var playerId = 'player' + String (date.getTime());
		var flashvars = {
			skin: '/jwplayer/skins/regular.swf',
			file: videoUrl,
			autostart: 'true'
		};
		var params = {
			allowscriptaccess: 'always'
		};
		var attributes = {
			id: playerId + 'object',
            name: playerId + 'object'		
		};		
		var playerObject = document.createElement('object');
		playerObject.setAttribute('width', width);
		playerObject.setAttribute('height', height);
		var paramObject = document.createElement('param');
		paramObject.setAttribute('name', 'movie');
		paramObject.setAttribute('value', media_content_url);
		var embedObject = document.createElement('embed');
		embedObject.setAttribute('autoplay', 'true');
		embedObject.setAttribute('quality', 'high');
		embedObject.setAttribute('src', media_content_url + '&autoplay=1');
		embedObject.setAttribute('type', media_content_type);
		embedObject.setAttribute('width', width);
		embedObject.setAttribute('height', height);
		var playerDiv = new creaDomObject({tag: 'div', id: playerId});		
		
		if (!target) {
            return playerDiv;
        }
		
		var close = new creaDomObject({
			tag: 'a',
			href: location.href.search('#stay') > 0 ? location.href : location.href + '#stay',
			html: 'x' 
		});
		
		var closePlayer = new creaDomObject({
			tag: 'div',
			className: 'window-header',
			style: {textAlign: 'right'},
			elements: [close]
			
		});
		
		var playerContainer = new creaDomObject({tag: 'div', elements: [closePlayer, playerDiv]});
		close.domElement.onclick = function() {
            playerContainer.remove();
			parent.style.width = '130px';
            parent.appendChild(target);
        }
		
		var parent = target.parentNode;
		parent.style.width = 'auto';
		parent.removeChild(target);
		playerContainer.addTo(parent);
		_$(playerId).appendChild(embedObject);		
		return false;
	},
	
	showLibraryPlayer : function (playerTargetId, playerId, videoUrl, imageUrl, width, height) {
		
		width = width ? parseInt(width) : CreatoorSN.videos.width;
		height = height ? parseInt(height) : CreatoorSN.videos.height;
		
		var flashvars = {
			file: videoUrl			
		};
		if (imageUrl) {
			flashvars.image = imageUrl;
		}
		
		var params = {
			allowscriptaccess: 'always',
            allowfullscreen: 'true'
		};
		
		var attributes = {
			id: playerId,
			name: playerId
		};
		
		swfobject.embedSWF('/jwplayer/player.swf', playerTargetId, width, height, '9.0.0', false, flashvars, params, attributes);
	},
	
	showPlayerAndPlaylist : function (playerTargetId, playerId, videoArray, width, height) {
		
		
		CreatoorSN.videos.isUnix = navigator.platform.toLowerCase().indexOf('linux') >= 0 ? true : false;
		CreatoorSN.videos.playerId = playerId;		
		CreatoorSN.videos.player = null;
		CreatoorSN.videos.playlistArray = videoArray;
		CreatoorSN.videos.instanceWidth = width = width ? parseInt(width) : CreatoorSN.videos.width;
        CreatoorSN.videos.instanceHeight = height = height ? parseInt(height) : CreatoorSN.videos.height;
		var video = videoArray[0];
		var flashvars = {			
			file:'/members_data/' + video.userDirectory + '/videos/' + video.fileName,
			image:'/members_data/' + video.userDirectory + '/videos/' + video.thumbnail,
			skin: '/jwplayer/skins/regular.swf',
			type: 'video'
		};
		if (CreatoorSN.videos.playlistArray.length > 1) {
			flashvars.playlist = 'right';
			flashvars.playlistsize = 300;
			flashvars.playerready = 'CreatoorSN.videos.playlistPlayerReady';
		}
		if (video.duration) {
			flashvars.duration = video.duration;
		}
		var params = {
			allowscriptaccess: 'always',
            allowfullscreen: 'true'
		};
		var attributes = {
            id: playerId,
            name: playerId
        };
		swfobject.embedSWF('/jwplayer/player.swf', playerTargetId, width, height, '9', false, flashvars, params, attributes);
	},	
	
	playlistPlayerReady : function (obj)  {
		CreatoorSN.videos.player = _$(CreatoorSN.videos.playerId);
		var videoArray = CreatoorSN.videos.playlistArray;
		if (videoArray.length > 1) {
			var playlist = new Array();
			for (var i = 0; i < videoArray.length; i++) {
				var item = {
					file:'/members_data/' + videoArray[i].userDirectory + '/videos/' + videoArray[i].fileName,
                    image:'/members_data/' + videoArray[i].userDirectory + '/videos/' + videoArray[i].thumbnail,
					title: videoArray[i].videoTitle ? videoArray[i].videoTitle : CreatoorSN.common.translate('No title'),
					description: videoArray[i].videoDescription ? videoArray[i].videoDescription : CreatoorSN.common.translate('No description') 
				};
				if (videoArray[i].duration) {
					item.duration = videoArray[i].duration; 
				}
				playlist.push(item);
			}
			CreatoorSN.videos.player.sendEvent('LOAD', playlist);
		}
		
	},
	
	playVideo : function (targetId, playerId, videoData, width, height) {
		var target = _$(targetId);
		if (target && playerId && videoData) {
			var tmpHTML = target.innerHTML;
			var closePlayer = new creaDomObject({
				tag: 'img',
				src: '/images/delete.gif',
				style: {cursor: 'pointer'},
				wrapper: {tag: 'div', params: {style: {textAlign: 'right'}}}
			});
			
			var playerContainer = new creaDomObject({
				tag: 'div',
				id: 'container' + playerId
			});
			closePlayer.domElement.onclick = function () {
                //removeElement(this.nextSibling);
				target.innerHTML = tmpHTML;   
            }
			target.innerHTML = '';
			closePlayer.addTo(target);
			playerContainer.addTo(target);
			var flashvars = {            
	            file:'/members_data/' + videoData.userDirectory + '/videos/' + videoData.fileName,
	            image:'/members_data/' + videoData.userDirectory + '/videos/' + videoData.thumbnail,
	            skin: '/jwplayer/skins/regular.swf',
	            type: 'video',
				autostart: true
	        };
			if (videoData.duration) {
				flashvars.duration = videoData.duration;
			}
			var params = {
	            allowscriptaccess: 'always',
	            allowfullscreen: 'true'
	        };
            var attributes = {
	            id: playerId,
	            name: playerId
	        };
			width = width ? width : CreatoorSN.videos.width;
			height = height ? height : CreatoorSN.videos.height;
			swfobject.embedSWF('/jwplayer/player.swf', 'container' + playerId, width, height, '9', false, flashvars, params, attributes);
		}
	},

    popPlayer: function(video, image, playerId) {
        playerId = playerId ? playerId : 'videoPlayer';
        var playerContainer = new creaDomObject({
            tag : 'div',
            id : 'playerContainer',
            style : {
                width : CreatoorSN.videos.width + 'px',
                height : CreatoorSN.videos.height + 'px',
                border : '#808080 1px solid',
                textAlign : 'center'
            },
            html : '<img style="margin-top: 100px" src="/images/ajax-loader.gif" alt="loading" />'
        });
		var closeButton = new creaDomInput( {
			type : 'button',
			value : CreatoorSN.common.translate('Close'),
			wrapper : {
				tag : 'div',
				params : {
					style : {
						marginTop : '10px'
					}
				}
			}
		});
		var videoWindow = new creaDomWindow( {
			width : parseInt(CreatoorSN.videos.width + 10),
			draggable : false,
			modal : true,
			section : 'attachments',
			name : 'playerWindow',
			content : {
				elements : [ playerContainer, closeButton ]
			}

		});
		videoWindow.open();
		closeButton.domElement.onclick = function() {
			videoWindow.close();
		};
		CreatoorSN.videos.showLibraryPlayer('playerContainer', playerId, video, image);
    }

}

