/* 
	JavaScript for the site
	@author: Beau Durrant (http://zoomwebvideo.com)
*/

function addView( webcast_id )
{
	var url = '/lobby/addppvview/' + webcast_id;
	new Ajax.Request( url );
}

function addEcommerceWebcast( ecommerceid )
{
	var webcastName = $('webcastName').value;
	var start = 0; var end = webcastName.indexOf('.');
	var webcastid = webcastName.substr(start, end);
	var url = '/ecommerce/addwebcast?webcastid=' + webcastid + '&ecommerceid=' + ecommerceid;
	new Ajax.Request( url, 
	{ 
		onComplete: function()
		{ 
			document.location.reload(); 
		},
		on404: function()
		{
			alert("Error adding webcast, please try again")
		}
	});
}

function checkLogin()
{
	var url = '/lobby/checklogin';
	new Ajax.Request( url , { onComplete: function(t)
	{ 
		if( t.responseText == "refresh" )
		{
			document.location = '/user/login';
		}
	}} );
}

var searchTerms = '';
function toggleSearch()
{
	var searchInput = document.getElementById('search');
	var searchBox = document.getElementById('searchbox');
	if(searchBox.style.display == "none")
	{
		searchTerms = searchInput.value;
		searchInput.value = '';
		searchBox.style.display = "block"; 
	}
	else
	{
		searchInput.value = searchTerms;
		searchBox.style.display = "none";
	}
}

function addToSelect( textElement , selectElement )
{
	if( textElement.value == "" ) return;
	var option = new Option( textElement.value , textElement.value );
	selectElement.options[selectElement.options.length] = option;
	textElement.value = '';
	populateHiddenSelectValues( selectElement );
}

function removeFromSelect( selectElement )
{
	for (var i = 0; i < selectElement.options.length; i++) if (selectElement.options[i].selected) selectElement.remove(i);
	populateHiddenSelectValues( selectElement );
}

function populateHiddenSelectValues( selectElement )
{
	var hiddenName = selectElement.name + '_values';
	var selectHiddenValues = document.getElementById( hiddenName ); var value = '';
	for (var i = 0; i < selectElement.options.length; i++) value += selectElement.options[i].value + ',';
	value = value.substring( 0 , value.length - 1 );
	selectHiddenValues.value = value;
}

function emailToFriend( friendsemail, friendsname, yourname, webcast_id )
{
	var url = '/lobby/emailtofriend?friendsemail=' + friendsemail + '&friendsname=' + friendsname + '&yourname=' + yourname + '&webcast_id=' + webcast_id;
	new Ajax.Request( url , { onComplete: function(){ alert('Email Sent'); } } );
}

function submitQuestion( question, webcast_id )
{
	var url = '/lobby/submitquestion?question=' + escape(question) + '&webcast_id=' + webcast_id;
	new Ajax.Request( url , { onComplete: function(){ alert('Question Submitted'); } } );
}

function openPollWindow( poll_id )
{
	window.open('/lobby/showpoll/'+poll_id, 'pollWindow', 'location=0,status=0,scrollbars=1,width=1000,height=630');	
}

function openSlideWindow( slide_number, webcast_id )
{
	window.open('/lobby/showslide?slide=' + slide_number + '&webcast_id=' + webcast_id, 'slideWindow', 'location=0,status=0,scrollbars=0,width=800,height=630');	
}

function openSlideControlWindow( webcast_id )
{
	window.open('/webcasts/controlslides/' + webcast_id , 'controlSlideWindow', 'location=0,status=0,scrollbars=0,width=615,height=590');	
}

function openChatAdminWindow( webcast_id )
{
	window.open('/lobby/adminchat/' + webcast_id , 'adminChatWindow', 'location=0,status=0,scrollbars=0,width=615,height=590');
}

function openRequirements()
{
	window.open('/lobby/requirements', 'requirementsWindow', 'location=0,status=0,scrollbars=0,width=400,height=450');
}

function toggleQuestionsEmail()
{
	var qAndATrue = document.getElementById('qAndATrue').checked;
	var questionsEmail = document.getElementById('qAndAEmail');
	var disabled;
	if( !qAndATrue ) disabled = "disabled";
	questionsEmail.disabled = disabled;
}

function togglePassword()
{
	var publicRadio = document.getElementById('publicRadio').checked;
	var passwordElement = document.getElementById('password');
	var disabled;
	if( publicRadio ) disabled = "disabled";
	passwordElement.disabled = disabled;
}

function sendInvites(webcast_id)
{
	var send = confirm('Are you sure you want to send out invites for this webcast?');
	if( !send ) return;
	new Ajax.Request( '/webcasts/sendinvites/' + webcast_id, { onComplete: function(){ alert('Invites Sent'); } } );
}

function displayError(error)
{
	document.getElementById("error").innerHTML = '<ul class="errors"><li>' + error + '</li></ul>';
}

function addSlides(webcast_id)
{
	document.location.href = "/webcasts/powerpoint/" + webcast_id;
}

var webcast_id;
function saveSlides(string)
{
	openProgressWindow();
	var postVarArray = string.split('?');
	new Ajax.Request( postVarArray[0] , { method: 'post', parameters: postVarArray[1], onComplete: function(){ closeModalWindow(); } } );
}
function saveSlidesAndContinue(string)
{
	openProgressWindow();
	var postVarArray = string.split('?');
	new Ajax.Request( postVarArray[0] , { method: 'post', parameters: postVarArray[1], onComplete: function(){ document.location.href='/webcasts/design/' + webcast_id; } } );
}
function saveDesign(string)
{
	openProgressWindow();
	new Ajax.Request( string , { method: 'post', onComplete: function(){ closeModalWindow(); } } );	
}
function saveDesignAndContinue(string)
{
	openProgressWindow();
	new Ajax.Request( string , { method: 'post', onComplete: function(){ document.location.href='/webcasts/preview/' + webcast_id; } } );	
}

function openVideoCoverWindow(cover_id)
{
	window.open('/webcasts/videocover/' + cover_id, 'videoCoverWindow', 'location=0,status=0,scrollbars=0,width=460,height=680')
}

function openAudioImageWindow(webcast_id)
{
	window.open('/webcasts/audioimage/' + webcast_id, 'audioImageWindow', 'location=0,status=0,scrollbars=0,width=460,height=630')
}

/*slide builder functions */
function selectSlideTheme(name)
{
	switch(name) {
		case 'light':
			$('customTheme').hide();
			$('textColor').value = '3e4346';
			$('backgroundImage').value = '/images/slidethemes/light.gif';
			break;
		case 'dark':
			$('customTheme').hide();
			$('textColor').value = 'ebf2f4';
			$('backgroundImage').value = '/images/slidethemes/dark.gif';
			break;
		case 'custom':
			$('customTheme').show();
			break;
		default:
			alert("We could not find that theme.");
			break;
	}
	
	updatePreviewStyle();
}

function updatePreviewStyle()
{
	editor.destroy();
	createSlideCss();
	//alert(slidecss);
	editor = CKEDITOR.replace( 'slide_html' );
}

function createSlideCss()
{
	slidecss = '';
	
	if($('textColor').value.length > 1)
		slidecss += "body { color: #" + $('textColor').value + "; } ";
	if($('backgroundColor').value.length > 1)
		slidecss += "body { background-color: #" + $('backgroundColor').value + "; } ";
	if($('backgroundImage').value.length > 1)
		slidecss += "body { background: url(" + $('backgroundImage').value + ") no-repeat; background-size: 100%; } ";
}

function formatTimeDisplay(time, showneg)
{
       if(time >= 0)
       {
               s = time;
               h = Math.floor(s / 3600);
               s = s % 3600;
               m = Math.floor(s / 60);
               s = s % 60;
               return h+':'+(m<10? '0'+m:m)+':'+(s<10? '0'+s:s);
       }
       else if(showneg && (time < 0))
       {
               return '-'+formatTimeDisplay( Math.abs(time) );
       }

       return 'n/a';
}

function parseTime( time )
{
	parts = time.split(":",3);

	if(parts.length == 3)
		toReturn = parseInt(parts[0], 10)*3600 + parseInt(parts[1], 10)*60 + parseInt(parts[2], 10);
	else if(parts.length == 2)
		toReturn = parseInt(parts[0], 10)*60 + parseInt(parts[1], 10);
	else if((parts.length == 1) && !isNaN(time))
		toReturn = parseInt(time, 10);
	else
		return -1;

	return toReturn;
}

function sendTimeToFlash( objectName, slideNum, time)
{
	var flashMovie = getFlashMovieObject( objectName );
    flashMovie.setSlideTime(slideNum, parseTime(time));
}

function trace( string )
{
	//alert( string );
}

function sendSaveToFlash( objectName )
{
	var flashMovie = getFlashMovieObject( objectName );
    flashMovie.saveToFlash();
}

function sendSaveAndContinueToFlash( objectName )
{
	var flashMovie = getFlashMovieObject( objectName );
    flashMovie.saveAndContinueToFlash();
}

// http://www.permadi.com/tutorial/flashjscommand/
function getFlashMovieObject(movieName)
{
	if( window.document[movieName] ) 
	{
		return window.document[movieName];
	}
	if( navigator.appName.indexOf("Microsoft Internet") == -1 )
	{
		if ( document.embeds && document.embeds[movieName] )
			return document.embeds[movieName]; 
	}
	else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
	{
		return document.getElementById(movieName);
	}
}

