/*** elements ***/
function loadingGraphic( element_id, background_color )
{
   jQuery( "#" + element_id ).html( '<img id="loader" src="/images/loader' + background_color + '.gif" alt="" border="0" height="32" width="32" />' );
}

function buttonClick( element_id )
{
   jQuery( "#" + element_id ).removeClass( 'button' );
   jQuery( "#" + element_id ).removeAttr( 'href' );
   jQuery( "#" + element_id ).removeAttr( 'onclick' );
   jQuery( "#" + element_id ).addClass( 'buttondisabled' );
}

function buttonLeftClick( element_id )
{
   jQuery( "#" + element_id ).removeClass( 'button' );
   jQuery( "#" + element_id ).removeAttr( 'href' );
   jQuery( "#" + element_id ).removeAttr( 'onclick' );
   jQuery( "#" + element_id ).addClass( 'buttonleftdisabled' );
}

function buttonRightClick( element_id )
{
   jQuery( "#" + element_id ).removeClass( 'buttonright' );
   jQuery( "#" + element_id ).removeAttr( 'href' );
   jQuery( "#" + element_id ).removeAttr( 'onclick' );
   jQuery( "#" + element_id ).addClass( 'buttonrightdisabled' );
}

function loadCatchAll( element_id, relative_path )
{
   jQuery( ".catchall" ).html( '' );
   jQuery( ".catchall" ).removeClass( "catchall" );

   jQuery( "#" + element_id ).addClass( 'catchall' );
   loadingGraphic( element_id, 'black' );
   loadElement( element_id, relative_path );
}

function clearElement( element_id )
{
   if ( jQuery( "#newsandnoteslist" ).length > 0 )
   {
      jQuery( "#newsandnoteslist" ).css( "overflow", "auto" );
   }

   if ( jQuery( "#communitycommentslist" ).length > 0 )
   {
      jQuery( "#communitycommentslist" ).css( "overflow", "auto" );
   }

   jQuery( "#" + element_id ).html( '' );
   jQuery( "#" + element_id ).removeClass( "catchall" );
}

function loadElement( element_id, relative_path )
{
   if( typeof relative_path == "undefined" ) relative_path = "";

   jQuery( "#" + element_id ).load( relative_path + "library/scripts/elements/" + element_id + ".php?sid=" + Math.random() );
}

function processElement( element_id, form, relative_path ) 
{
   if( typeof relative_path == "undefined" ) relative_path = "";

   var request = jQuery.post
   (
      relative_path + "library/scripts/elements/" + element_id + ".php?sid=" + Math.random(),
      jQuery( "#" + form ).serialize(),
      function( html )
      {
         jQuery( "#" + element_id ).html( html );
         request = null;
      }
   );
}


/*** capture ***/
var currentLengthOfRecording = 0;

function validateForm( script_name, form, message_id, relative_path )
{
   if( typeof relative_path == "undefined" ) relative_path = "";

   var request = jQuery.post
   (
      relative_path + "library/scripts/helpers/" + script_name + ".php?sid=" + Math.random(),
      jQuery( "#" + form ).serialize(),
      function( html )
      {
         var explode = html.split( "\n" );
         var errors = false;
         
         for( var i in explode )
         {
            var explode_again = explode[ i ].split( "|" );
            if( explode_again[ 0 ] == "error" )
            {
               errors = true;
               jQuery( "#error_" + explode_again[ 1 ] ).html( explode_again[ 2 ] );
               jQuery( "#error_" + explode_again[ 1 ] ).show();
            }
            else if( explode_again[ 0 ] == "ok" )
            {
               jQuery( "#error_" + explode_again[ 1 ] ).html( "" );
               jQuery( "#error_" + explode_again[ 1 ] ).hide();
            }
         }
            
         if( errors )
         {
            // jQuery( "#" + message_id ).html( "Error(s) Occurred, please fix the identified error(s) and resubmit" );
            jQuery( "#" + message_id ).show();

         }
         else
         {
            if ( jQuery( "#button_capture" ).length > 0 )
            {
               /***
               jQuery( "#button_capture" ).removeClass( 'navright' );
               jQuery( "#button_capture" ).removeAttr( 'href' );
               jQuery( "#button_capture" ).removeAttr( 'onclick' );
               jQuery( "#button_capture" ).addClass( 'navrightdisabled' );
               ***/

               storyDialog( "save" );
            }
            else
            {
               jQuery( "#" + message_id ).html( "Submitting, please wait ..." );
               jQuery( "#" + message_id ).show();
               
               // jQuery( "#" + form ).hide();
               jQuery( "#" + form ).get(0).submit();
            }
         }

         request = null;
      }
   );
}

function processStoryTypeChange( story_type, relative_path )
{
   if( typeof relative_path == "undefined" ) relative_path = "";

   jQuery( "#prev_story_type" ).val( story_type );
   passLengthOfRecording( 0 );

   if( story_type != "" )
   {
      jQuery( "#descriptions" ).load( relative_path + "library/scripts/helpers/storytypedescs.php?sid=" + Math.random(), { story_type: story_type } );

      var request = jQuery.post
      (
         relative_path + "library/scripts/helpers/storytypedetails.php?sid=" + Math.random(),
         { story_type: story_type },
         function( data )
         {
            var explode = data.split( "\n" );
            
            for( var i in explode )
            {
               var explode_again = explode[ i ].split( "|" );

               if( explode_again[ 0 ] == "allow_text" )
               {
                  jQuery( "#toggletextvideo" ).children( "a" ).text( "Text" );
                  jQuery( '#storytext' ).css( "visibility", "hidden" );
                  jQuery( '#storyvideo' ).css( "visibility", "visible" );

                  if( explode_again[ 1 ] == "Y" ) { jQuery( "#toggletextvideo" ).show(); }
                  else { jQuery( "#toggletextvideo" ).hide(); }
               }
               else if( explode_again[ 0 ] == "length" )
               {
                  passLengthOfRecording( explode_again[ 1 ] );
                  currentLengthOfRecording = explode_again[ 1 ];
               }
            }
               
            request = null;
         }
      );
   }
   else
   {
      jQuery( "#descriptions" ).load( relative_path + "library/scripts/helpers/storytypedescs.php?sid=" + Math.random() );
      jQuery( "#toggletextvideo" ).children( "a" ).text( "Text" );
      jQuery( "#toggletextvideo" ).hide();
      jQuery( '#storytext' ).css( "visibility", "hidden" );
      jQuery( '#storyvideo' ).css( "visibility", "visible" );
      passLengthOfRecording( 0 );
      currentLengthOfRecording = 0;
   }
}

function loadStoryTypeDescs()
{
   jQuery( "#descriptions" ).load( "library/scripts/helpers/storytypedescs.php?sid=" + Math.random() );
}

function toggleTextVideo()
{
   toggleVisibility( '#storytext' );
   toggleVisibility( '#storyvideo' );

   if( jQuery( "#storyvideo" ).css( "visibility" ) == "visible" )
   {
      jQuery( "#toggletextvideo" ).children( "a" ).text( "Text" );
   }
   else
   {
      jQuery( "#toggletextvideo" ).children( "a" ).text( "Video" );
   }
}

function postCaptureForm( script_name, form, message_id, relative_path )
{
   if( typeof relative_path == "undefined" ) relative_path = "";

   if( jQuery( "#storyvideo" ).css( "visibility" ) == "visible" )
   {
      jQuery( "#is_video" ).val( "Y" );

      if( window.recording )
      {
         actualLength = window.document[ "recording" ].GetVariable( "actualLength" );
      }
   
      if( document.recording )
      {
         actualLength = document.recording.GetVariable( "actualLength" );
      }

      if( actualLength != "undefined" )
      {
         jQuery( "#story_length" ).val( actualLength );
      }
   }
   else
   {
      jQuery( "#is_video" ).val( "N" );
   }

   validateForm( script_name, form, message_id, relative_path );
}

function passLengthOfRecording( lengthOfRecording )
{
   if( window.recording )
   {
      window.document[ "recording" ].SetVariable( "lengthOfRecording", lengthOfRecording );
   }
   
   if( document.recording )
   {
      document.recording.SetVariable( "lengthOfRecording", lengthOfRecording );
   }
}

function toggleVisibility( id )
{
   if( jQuery( id ).css( "visibility" ) == "hidden" ) { jQuery( id ).css( "visibility", "visible" ); }
   else { jQuery( id ).css( "visibility", "hidden" ); }
}

function storyDialog( cause )
{
   showDialog = false;

   if( cause == "save" )
   {
      showDialog = true;
   }
   else
   {
      if( jQuery( "#storyvideo" ).css( "visibility" ) == "visible" )
      {
         if( window.recording )
         {
            actualLength = window.document[ "recording" ].GetVariable( "actualLength" );
         }
      
         if( document.recording )
         {
            actualLength = document.recording.GetVariable( "actualLength" );
         }
      }
      else
      {
         actualLength = "undefined";
      }

      if( ( actualLength != "undefined" ) && ( actualLength > 0 ) )
      {
         showDialog = true;
      }
      else
      {
         if( cause.indexOf( "story_type_" ) != -1 )
         {
            processStoryTypeChange( jQuery( '#story_type' ).val() );
         }
         else
         {
            window.location = cause + '.php';
         }
      }
   }

   if( showDialog )
   {
      jQuery( ".catchall" ).html( '' );
      jQuery( ".catchall" ).removeClass( "catchall" );
   
      jQuery( "#storydialog" ).addClass( 'catchall' );
      loadingGraphic( 'storydialog', 'black' );
      jQuery( "#storydialog" ).load( "library/scripts/elements/storydialog.php?sid=" + Math.random(), { cause: cause } );
   }
}


/*** dashboard ***/
function getResponsesViewer( response_id )
{
   jQuery( ".catchall" ).html( '' );
   jQuery( ".catchall" ).removeClass( "catchall" );

   jQuery( "#communitycomment" ).addClass( 'catchall' );
   loadingGraphic( "communitycomment", 'black' );
   jQuery( "#communitycomment" ).load( "library/scripts/elements/communitycomment.php?sid=" + Math.random(), { response_id: response_id } );
}

function getNewsAndNotesPlayer( story_id )
{
   jQuery( "#newsandnoteslist" ).css( "overflow", "hidden" );
   jQuery( "#communitycommentslist" ).css( "overflow", "hidden" );

   jQuery( ".catchall" ).html( '' );
   jQuery( ".catchall" ).removeClass( "catchall" );

   jQuery( "#newsandnote" ).addClass( 'catchall' );
   loadingGraphic( "newsandnote", 'black' );
   jQuery( "#newsandnote" ).load( "library/scripts/elements/newsandnote.php?sid=" + Math.random(), { story_id: story_id } );
}

/*** player ***/
function getCommunityPlayer( account_id, video_id, type_id )
{
   jQuery( "#storieslist" ).css( "overflow", "hidden" );
   jQuery( "#communityplayer" ).addClass( "catchall" );
   loadingGraphic( "communityplayer", "black" );
   jQuery( "#communityplayer" ).load( "../library/scripts/elements/communityplayer.php?sid=" + Math.random(), { community_account_id: account_id, video_id: video_id, type_id: type_id } );
}

function closeCommunityPlayer()
{
   jQuery( "#storieslist" ).css( "overflow", "auto" );
   javascript:clearElement( "communityplayer" );
}

function togglePlayerResponse()
{
   if( jQuery( "#storyplayer" ).css( "display" ) == "block" )
   {
      jQuery( "#storyresponsemsg" ).html( "" );

      if( window.player )
      {
         window.document[ "player" ].SetVariable( "pauseVideo", "true" );
      }
      
      if( document.player )
      {
         window.document[ "player" ].SetVariable( "pauseVideo", "true" );
      }
   }
   
   jQuery( "#storyplayer" ).toggle();
   jQuery( "#storynavigation" ).toggle();
   jQuery( "#storyresponse" ).toggle();
   jQuery( "#storyresponsenavigation" ).toggle();

   if( jQuery( "#storyresponse" ).css( "display" ) == "block" )
   {
      jQuery( "#response" ).focus();
   }
}

function toggleResponseSubscribe()
{
   if( jQuery( "#storyplayer" ).css( "display" ) == "block" )
   {
      if( window.player )
      {
         window.document[ "player" ].SetVariable( "pauseVideo", "true" );
      }
      
      if( document.player )
      {
         window.document[ "player" ].SetVariable( "pauseVideo", "true" );
      }
   }
   
   jQuery( "#storyplayer" ).toggle();
   jQuery( "#storydetails" ).toggle();
   jQuery( "#storynavigation" ).toggle();
   jQuery( "#mustsubscribe" ).toggle();
   jQuery( "#mustsubscribenavigation" ).toggle();
}


/*** community ***/
function getCommunityStories( account_id, story_id )
{
   loadingGraphic( "communitystories", "white" );

   if( story_id != "" )
   {
      jQuery( "#communitystories" ).load( "../library/scripts/elements/communitystories.php?sid=" + Math.random(),
                                          { community_account_id: account_id },
                                          function() { getCommunityPlayer( account_id, story_id, '' ); } );
   }
   else
   {
      jQuery( "#communitystories" ).load( "../library/scripts/elements/communitystories.php?sid=" + Math.random(),
                                          { community_account_id: account_id } );
   }
}

function getLoginSubscribe( account_id )
{
   jQuery( "#loginsubscribe" ).load( "../library/scripts/elements/loginsubscribe.php?sid=" + Math.random(), { community_account_id: account_id } );
}

function getCommunityLogin( account_id ) {
   jQuery( '#communitylogin' ).addClass( 'catchall' );
   loadingGraphic( 'communitylogin', 'black' );
   jQuery( "#communitylogin" ).load( "../library/scripts/elements/communitylogin.php?sid=" + Math.random(), { community_account_id: account_id } );
}

function getCommunitySubscription( account_id )
{
   jQuery( '#subscription' ).addClass( 'catchall' );
   loadingGraphic( 'subscription', 'black' );
   jQuery( "#subscription" ).load( "../library/scripts/elements/subscription.php?sid=" + Math.random(), { community_account_id: account_id } );
}

function getCLV( account_id, video_id ) {
   jQuery( '#communitylogin' ).addClass( 'catchall' );
   loadingGraphic( 'communitylogin', 'black' );
   jQuery( "#communitylogin" ).load( "../library/scripts/elements/communitylogin.php?sid=" + Math.random(), { community_account_id: account_id, video_id: video_id } );
}

function getCSV( account_id, video_id )
{
   jQuery( '#subscription' ).addClass( 'catchall' );
   loadingGraphic( 'subscription', 'black' );
   jQuery( "#subscription" ).load( "../library/scripts/elements/subscription.php?sid=" + Math.random(), { community_account_id: account_id, video_id: video_id } );
}


function getCommunityLoginError()
{
   jQuery( '#loginerrormsg' ).addClass( 'catchall' );
   loadingGraphic( 'loginerrormsg', 'black' );
   loadElement( 'loginerrormsg', '../' );
}

function getCommunityHelp()
{
   jQuery( '#communityhelp' ).addClass( 'catchall' );
   loadingGraphic( 'communityhelp', 'black' );
   loadElement( 'communityhelp', '../' );
}

function getCommunityForgotPassword()
{
   jQuery( '#communityforgotpassword' ).addClass( 'catchall' );
   loadingGraphic( 'communityforgotpassword', 'black' );
   loadElement( 'communityforgotpassword', '../' );
}

function getCommunityContact()
{
   jQuery( '#communitycontact' ).addClass( 'catchall' );
   loadingGraphic( 'communitycontact', 'black' );
   loadElement( 'communitycontact', '../' );
}

/*** tutorials ***/
function getTutorial( tutorial_id )
{
   loadingGraphic( 'tutorials', 'black' );
   jQuery( "#tutorials" ).load( "library/scripts/elements/tutorials.php?sid=" + Math.random(), { tutorial_id: tutorial_id } );
}

/*** my account ***/
function toggleAddDeleteStoryTypes()
{
   jQuery( ".error" ).html( '' );
   jQuery( ':input' ).each( function() {
      var type = this.type;
      var tag = this.tagName.toLowerCase();
      if (type == 'text' || type == 'password' || tag == 'textarea')
         this.value = "";
      else if (type == 'checkbox' || type == 'radio')
         this.checked = false;
      else if (tag == 'select')
         this.selectedIndex = 0;
   } );
   
   jQuery( "#myaccountdeletestorytype" ).toggle();
   jQuery( "#myaccountdeletestorytypenav" ).toggle();
   jQuery( "#myaccountaddstorytype" ).toggle();
   jQuery( "#myaccountaddstorytypenav" ).toggle();
   jQuery( "#myaccountstorytypedescs" ).toggle();
}

function toggleDeleteStoryTypes()
{
   jQuery( ".error" ).html( '' );

   if( jQuery( "#myaccountareyousure" ).is( ":visible" ) )
   {
      jQuery( "#myaccountdeletestorytype" ).toggle();
      jQuery( "#myaccountdeletestorytypenav" ).toggle();
      jQuery( "#myaccountareyousure" ).toggle();
      jQuery( "#myaccountareyousurenav" ).toggle();
   }
   else
   {
      jQuery( ':input[type=radio]' ).each( function() {
         if( this.checked )
         {
            jQuery( "#myaccountdeletestorytype" ).toggle();
            jQuery( "#myaccountdeletestorytypenav" ).toggle();
            jQuery( "#myaccountareyousure" ).toggle();
            jQuery( "#myaccountareyousurenav" ).toggle();

            return false;
         }
      } );

      if( jQuery( "#myaccountareyousure" ).is( ":hidden" ) )
      {
         if( jQuery( "#form_deletepersonalstorytypes" ).length > 0 )
         {
            jQuery( '#delete' ).attr( 'value', 'Y' );
            processElement( 'personalstorytypes', 'form_deletepersonalstorytypes' );
         }
         else if( jQuery( "#form_deleteorgstorytypes" ).length > 0 )
         {
            jQuery( '#delete' ).attr( 'value', 'Y' );
            processElement( 'orgstorytypes', 'form_deleteorgstorytypes' );
         }
      }
   }
}

function handleImageChange()
{
   jQuery( '#form_personalimageupload' ).submit();
   jQuery( "#personalimage" ).html( '<div style="position: relative; margin: 10px;"><img src="/images/loaderblack.gif" alt="" border="0" height="32" width="32" /></div>' );
}

function toggleBillingTerms()
{
   jQuery( "#myaccountbillingleft" ).toggle();
   jQuery( "#myaccountbillingright" ).toggle();
   jQuery( "#myaccountbillingbuttons" ).toggle();
   jQuery( "#myaccountterms" ).toggle();
   jQuery( "#myaccounttermsclose" ).toggle();
}

function toggleEditDeleteStory() {
   jQuery( ".error" ).html( '' );
   jQuery( ':input' ).each( function() {
      var type = this.type;
      var tag = this.tagName.toLowerCase();
      if (type == 'text' || type == 'password' || tag == 'textarea')
         this.value = "";
      else if (type == 'checkbox' || type == 'radio')
         this.checked = false;
      else if (tag == 'select')
         this.selectedIndex = 0;
   } );
   
   jQuery( "#myaccountmystories" ).toggle();
   jQuery( "#myaccountmystoriestitle" ).toggle();
   jQuery( "#myaccountmystoriestitleedit" ).toggle();
   jQuery( "#myaccountdeletestorynav" ).toggle();
   jQuery( "#myaccounteditstory" ).toggle();
   jQuery( "#myaccounteditstorynav" ).toggle();
   jQuery( "#myaccountmystoriesdescs" ).toggle();
   jQuery( "#myaccounteditstoryplayertext" ).toggle();

   if( jQuery( "#myaccounteditstoryplayertext" ).is( ":hidden" ) ) {
      if( window.player ) {
         window.document[ "player" ].SetVariable( "pauseVideo", "true" );
      }
      
      if( document.player ) {
         window.document[ "player" ].SetVariable( "pauseVideo", "true" );
      }
   }
}

function toggleDeleteStory() {
   jQuery( ".error" ).html( '' );

   if( jQuery( "#myaccountareyousure" ).is( ":visible" ) ) {
      jQuery( "#myaccountmystories" ).toggle();
      jQuery( "#myaccountmystoriestitle" ).toggle();
      jQuery( "#myaccountmystoriesdescs" ).toggle();
      jQuery( "#myaccountdeletestorynav" ).toggle();
      jQuery( "#myaccountareyousure" ).toggle();
      jQuery( "#myaccountareyousurenav" ).toggle();
   } else {
      jQuery( ':input[type=radio]' ).each( function() {
         if( this.checked ) {
            jQuery( "#myaccountmystories" ).toggle();
            jQuery( "#myaccountmystoriestitle" ).toggle();
            jQuery( "#myaccountmystoriesdescs" ).toggle();
            jQuery( "#myaccountdeletestorynav" ).toggle();
            jQuery( "#myaccountareyousure" ).toggle();
            jQuery( "#myaccountareyousurenav" ).toggle();

            return false;
         }
      } );

      if( jQuery( "#myaccountareyousure" ).is( ":hidden" ) ) {
         if( jQuery( "#form_deletemystories" ).length > 0 ) {
            jQuery( '#delete' ).attr( 'value', 'Y' );
            processElement( 'mystories', 'form_deletemystories' );
         }
      }
   }
}

