﻿jQuery(document).ready(function() 
{	
	var VideoStep = 1;

	if ((".broadcast-add").length)
	{
		var url = document.location.href;
		url = url.split('?');
		
		$(".broadcast-add").click(function()
		{
			var c_id = 0;
			
			if ($(this).attr("chid") != null)
				c_id = $(this).attr("chid");
				
			var link = url[0] + '?service=video&action=translation_add&savevideo=1';
			var content;
											
			$.get(link, function(data){
				content= data;
				dialogSetContentAndShow(content,  'dialog_broadcast_add', 'Создание трансляции');
				ShowFolder(0);
				VideoStep = 1;				
			});
		});
	}
	if ((".video_chat-add").length)
	{
		var url = document.location.href;
		url = url.split('?');
		
		$(".video_chat-add").click(function()
		{
			var c_id = 0;
			
			if ($(this).attr("chid") != null)
				c_id = $(this).attr("chid");
				
			var link = url[0] + '?service=video&action=video_chat_add';
			var content;
											
			$.get(link, function(data){
				content= data;
				dialogSetContentAndShow(content,  'dialog_video_chat_add', 'Создание видео-конференции');
				ShowFolder(0);
			});
		});
	}
		
	if ((".video-add").length)
	{
		var url = document.location.href;
		url = url.split('?');
		
		$(".video-add").click(function()
		{
			var c_id = 0;
			
			if ($(this).attr("chid") != null)
				c_id = $(this).attr("chid");
				
			var link = url[0] + '?service=uploader&ftype=2&c_id=' + c_id;
			var content;
											
			$.get(link, function(data){
				content= data;
				dialogSetContentAndShow(content,  'dialog_edit_page', 'Добавление видео');
				ShowFolder(0);
				VideoStep = 1;				
			});
		});
	}
	
	$(".VideoEdit").live("click", function()
	{
		var url = document.location.href;
		url = url.split('?');
		
		var link = url[0] + '?service=video&action=video_edit&video_id=' + $(this).attr('videoid');
		
		var content;
										
		$.get(link, function(data){
			content= data + '<div style="claer:both">&nbsp;</div>';
			dialogSetContentAndShow(content,  'video_edit_page', 'Редактирование видео');
		});		
	});
	
	
	
	$(".btnBroadcastPreviewChoose").live("click", function()
	{
		ShowChoosePictureDialog("Выбор превью картики", function(id) {
		var url = document.location.href;
		url = url.split('?');

		$('#preview_id').attr('value', id);
			
		CloseChoosePictureDialog();
		});
	})
	$(".btnBroadcastStartChoose").live("click", function()
	{
		ShowChoosePictureDialog("Выбор стартовой картинки", function(id) {
		var url = document.location.href;
		url = url.split('?');

		$('#start_id').attr('value', id);
			
		CloseChoosePictureDialog();
		});
	})
	
	$(".btnBrowsePreview").live("click", function()
	{
		var fileid = $(this).attr('fileid');
		
		ShowChoosePictureDialog($("#gallery_add_photo_name").attr('name'), function(id) {
		var url = document.location.href;
		url = url.split('?');

		$('#preview_' + fileid).attr('value', id);
			
		CloseChoosePictureDialog();
		});
	})
	$(".btnBrowseStart").live("click", function()
	{
		var fileid = $(this).attr('fileid');
		
		ShowChoosePictureDialog($("#gallery_add_photo_name").attr('name'), function(id) {
		var url = document.location.href;
		url = url.split('?');

		$('#start_' + fileid).attr('value', id);
			
		CloseChoosePictureDialog();
		});
	})
					
	$("#uploader_next_btn").live("click", function()
	{
		if (VideoStep == 1)
		{
			var link = url[0] + '?service=uploader&ftype=2&step=2&c_id=' + $(this).attr("chid");
			var content;
											
			$.get(link, function(data){
				content= data;
				dialogSetContentAndShow(content,  'dialog_edit_page', 'Добавление видео');
				ShowFolder(0);
				VideoStep = 2;

				if ($(".wuQ").length)
				{
					/**
					 * Disables the link
					 */
					jQuery.fn.disableLink = function(fn) {
						return jQuery(this).blur().unbind('click').fadeTo("fast", 0.2, (fn ? fn : null));
					};

					//
					// Constants
					jQuery.uploader.swfURL = 'flash/jQuery.uploader.swf';
			//		jQuery.uploader.backendScript = '../../backend.php';
					jQuery.uploader.backendScript = $("#sScriptSrc").attr('path');

					/**
					 * Removes the upload (manually) by clicking the eject button
					 */
					function uploadDelete(_link, _self) {
						var _id = _link.parent().parent().attr('id');

						//
						// Disable links to prevent user from clicking again which could screw up the interface
						jQuery('#' + _id + ' A.delete').disableLink();
						jQuery('#' + _id + ' A.start').disableLink();
						jQuery('#' + _id + ' A.cancel').disableLink();
						jQuery('#' + _id + ' A.retry').disableLink();

						//
						// Send the "remove" event to flash
						_self.remove(_id);
					}

					/**
					 * Starts the upload (manually) by clicking the play button
					 */
					function uploadStart(_link, _self) {
						_self.upload(_link.parent().parent().attr('id'), jQuery.uploader.backendScript);
					}

					/**
					 * Restores the element back to it's "not started" state.
					 */
					function uploadRestore(_elem, _self, _allowRemoveOrRetry) {
						//
						// Fix links
						jQuery('A.cancel', _elem).hide();
						if (_allowRemoveOrRetry) {
							jQuery('A.delete', _elem).fadeTo("fast", 1.0).click(function() { uploadDelete(jQuery(this), _self) });
							jQuery('A.retry', _elem).show();
						} else {
							jQuery('A.retry', _elem).show().disableLink();
						}

						//
						// Fix progressbar
						jQuery('DIV.wuICPB', _elem).hide();
						jQuery('div.wuICPBF', _elem).width(0).html(' 0% ');
						jQuery('DIV.wuICPT', _elem).hide();

						//
						// Animate the progressbar
						_elem.removeClass('active').animate({ height: 40 }, 200);
					}

					/**
					 * Document ready function
					 */
					jQuery(document).ready(function() {
							//
							// Step 1: Create fancy hover buttons
							jQuery('button:not(#btnBrowse)').hover(
																	function() {
																		$(this).css('background-position', '0px -30px');
																	},
																	function() {
																		$(this).css('background-position', '0px 0px');
																	}
																);

							//
							// Step 2: Add the uploader
							var oUpload = $('#uploaderOverlay').uploader({
																		swfURL: jQuery.uploader.swfURL,
																		buttonSkin: '/jQuery.uploader/img/demo1/buttonSkin.png',
																		logging: "1"
																	});

							//
							// Step 3: Overwrite the events (base events have allready been setup)
							jQuery.extend(oUpload.events, {

									/**
									 * SWF ready handler, used to bind the events the way YOU want.
									 */
									uploaderReady: function() {
											var _self = this;

											// Upload ALL files (therefore the empty '' at the beginning)
											jQuery('#btnUpload').click(function() { _self.upload('', jQuery.uploader.backendScript); });

											//
											// Upload ALL files (therefore the empty '' at the beginning)
											jQuery('#btnUpload').click(function() { _self.upload('', jQuery.uploader.backendScript); });

											//
											// Clear the entire queue (e.g. remove all files)
											jQuery('#btnCancel').click(function() { _self.remove(); });
										},

									/**
									 * File added handler
									 */
									fileAdded: function(args) {
											var _id = '';
											_id = args.fileData.id;

											var sHTML = '';
											sHTML+= '<div class="wuI" id="' + _id + '">';
											sHTML+= '<div class="wuIC">';
											sHTML+= '<a href="javascript://" class="delete" title="Delete upload">&nbsp;</a> ';
											sHTML+= '<a href="javascript://" class="start" title="Start upload">&nbsp;</a> ';
											sHTML+= '<a href="javascript://" class="cancel" title="Cancel upload">&nbsp;</a> ';
											sHTML+= '<a href="javascript://" class="retry" title="Retry upload">&nbsp;</a> ';
											sHTML+= '&nbsp;' + args.fileData.name + '<br />';
											sHTML+= '<div class="wuICPB"><div class="wuICPBF"> 0% </div></div>';
											sHTML+= '<div class="wuICPT"><span class="wuICBytesDone">0 bytes</span> of <span class="wuICBytesTotal">' + this.formatSize(args.fileData.size) + '</span> <span class="wuICSpeedTime">&nbsp;</span></div>';
											sHTML+= '</div></div>';
											var _elem = jQuery(sHTML);
											jQuery('.wuQ').append(_elem);

											// Add handler
											var _self = this;

											//
											// Triggers file removal
											jQuery('A.delete', _elem).click(function() { uploadDelete(jQuery(this), _self) });
											jQuery('A.start', _elem).click(function() { uploadStart(jQuery(this), _self) });
											jQuery('A.retry', _elem).click(function() { uploadStart(jQuery(this), _self) });
											jQuery('A.cancel', _elem).click(function() { _self.cancel(_id); });

											//
											// Prevent me from calling this in a later function ...
											_elem = null;
										},

									/**
									 * File removed handler
									 */
									fileRemoved: function(args) {
											jQuery('#' + args.fileData.id).slideUp("fast", function() { jQuery(this).remove(); });
										},

									/**
									 * File upload handler,
									 * in this demo used to change the icons
									 */
									fileUploadStarted: function(args) {
											var _id = args.fileData.id;
											var _self = this;
											var _elem = jQuery('#' + _id);

											//
											// You could delete the file while uploading,
											// (since this would trigger a "cancel" event in the Flash movie which is send to JavaScript)
											// but for the sake of the demo, we don't allow it.

											//
											// Fix links
											jQuery('A.delete', _elem).disableLink();
											jQuery('A.start', _elem).hide();
											jQuery('A.retry', _elem).hide();
											jQuery('A.cancel', _elem).show();

											//
											// Animate the progressbar
											_elem.addClass('active').animate({ height: 80 }, 200);

											//
											// Show elements
											jQuery('DIV.wuICPB', _elem).show();
											jQuery('DIV.wuICPT', _elem).show();
										},

									/**
									 * File progress handler
									 * in this demo used to update the progressbar
									 * @param args.fileData
									 * @param args.fileProgress
									 * @param args.queueProgress
									 */
									fileUploadProgress: function(args) {
											var _id = args.fileData.id;
											var _self = this;
											var _elem = jQuery('#' + _id);

											jQuery('h2.wuQS').html('Uploader queue: ' + args.queueProgress.progress + '% of ' + (args.queueProgress.filesAdded - args.queueProgress.filesCompleted) + ' files');
											jQuery('div.wuICPBF', _elem).css('width', args.fileProgress.progress + '%').html('&nbsp;' + args.fileProgress.progress + '%&nbsp;');
											jQuery('span.wuICBytesDone', _elem).html(this.formatSize(args.fileProgress.bytesCompleted));
											jQuery('span.wuICSpeedTime', _elem).html('at ' + this.formatSize(args.fileProgress.bytesPerSecond) + '/sec; ' + this.formatTime(args.fileProgress.timeRemaining) + ' remain');
										},

									/**
									 * Triggered when a file is cancelled
									 */
									fileUploadCancelled: function(args) {
											var _id = args.fileData.id;
											var _self = this;
											var _elem = jQuery('#' + _id);
											uploadRestore(_elem, _self, true);
										},

									/**
									 * Triggered when a file is uploaded (or has an error)
									 */
									fileUploadCompleted: function(args) {
											$("#nextframe1").css('display', 'none');
											$("#nextframe2").css('display', 'block');

											var _id = args.fileData.id;
											var _self = this;
											var _elem = jQuery('#' + _id);
											uploadRestore(_elem, _self, false);
										},

									/**
									 * Triggered when a file is uploaded (or has an error)
									 */
									fileUploadError: function(args) {
											var _id = args.fileData.id;
											var _self = this;
											var _elem = jQuery('#' + _id);
											uploadRestore(_elem, _self, false);
										},

									/**
									 * Triggered when there are no files in the queue
									 */
									queueErrorEmpty: function(args) {
											alert('No files to upload. Queue is empty!');
										},

									/**
									 * Called when the entire queue is cancelled
									 */
									queueCancelled: function() {
											jQuery('h2.wuQS').html('Uploader queue');
										},

									/**
									 * Called when the entire queue is cancelled
									 */
									queueCleared: function() {
											jQuery('h2.wuQS').html('Uploader queue');
										},

									/**
									 * Called when the entire queue is uploaded/completed
									 */
									queueCompleted: function() {
											jQuery('h2.wuQS').html('Uploader queue');
										}
								});
						});
				}
				});
		}
		if (VideoStep == 2)
		{
			var link = url[0] + '?service=uploader&ftype=2&step=3&timestamp=' + $(this).attr('timestamp') + '&c_id=' + $(this).attr("chid");
			var content;

			$.get(link, function(data){
				content= data;
				dialogSetContentAndShow(content,  'dialog_edit_page', 'Добавление видео');
				ShowFolder(0);
				VideoStep = 3;
				});
		}
	});
});