var popupTimeoutInit = popupTimeout = 3;

function popupNotLoggedIn( elem, textPopup, offsetLeft, offsetTop )
{
  try
  {
    jQuery( '#box-extrainfo .inner' ).html( textPopup );

    var box = jQuery( '#box-extrainfo' );
    var offset = jQuery( elem ).offset();

    if ( !offsetLeft )
    {
      var offsetLeft = 10;
    }

    if ( !offsetTop )
    {
      var offsetTop = 10;
    }

    box.css( { display: 'block',
               width:   '300px',
               top:     ( offset.top  - box.height() + offsetTop ) + 'px' } );

    if ( ( offset.left - box.width() ) < offsetLeft )
    {
      box.css( { left: ( offset.left + jQuery( elem ).width() - offsetLeft ) + 'px' } );
    }
    else
    {
      box.css( { left: ( offset.left - box.width()  + offsetLeft ) + 'px' } );
    }

    box.click( function() { jQuery( this ).css( { width: 'auto' } ).hide(); } );
    box.blur( function() { jQuery( this ).css( { width: 'auto' } ); } );

    return false;
  }
  catch ( e )
  {
//    alert( e );
  }
}

function popupEmos( elem, popupId )
{
  try
  {
    var box = jQuery( popupId );
    var boxEmos = jQuery( '#box-emoticons' );
    var offset = jQuery( elem ).offset();

    boxEmos.css( { top:  ( offset.top + 5 ) + 'px',
                   left: ( offset.left - box.width() - 2 ) + 'px' } );

    box.click( function() { jQuery( this ).hide(); } )
       .show()
       .mouseover( function () { popupTimeout = popupTimeoutInit; } );

    popupTimeout = popupTimeoutInit;
    hidePopup( popupId );
  }
  catch ( e )
  {
//    alert( e );
  }

  return false;
}

function hidePopup( elem )
{
  try
  {
    if ( popupTimeout == 0 )
    {
      jQuery( elem ).hide();
      return;
    }

    popupTimeout--;

    setTimeout( "hidePopup( '" + elem + "' );", 500 );
  }
  catch ( e )
  {
//    alert( e );
  }
}

function popupEmosPos( elem, popupId, boxWidth, posLeft, posTop )
{
  try
  {
    var box = jQuery( popupId );
    var offset = jQuery( elem ).offset();

    box.css( { position: 'absolute',
               width:   boxWidth + 'px',
               top:     ( offset.top + posTop ) + 'px' } );

    if ( ( offset.left ) < boxWidth + posLeft )
    {
      box.css( { left:  posLeft + '10px' } );
    }
    else
    {
      box.css( { left: ( offset.left - ( boxWidth + posLeft ) ) + 'px' } );
    }

    box.click( function() { jQuery( this ).hide(); } );
    box.show();
  }
  catch ( e )
  {
//    alert( e );
  }

  return false;
}

try
{
//  jQuery( window ).load( function()
  jQuery( document ).ready( function()
  {
    var inp_username     = jQuery( '.ipt-login-username' );
    var inp_password     = jQuery( '.ipt-login-password' );
    var inp_email        = jQuery( '.ipt-newsletter-email' );
    var inp_event        = jQuery( '.ipt-search-event' );
    var inp_city         = jQuery( '.ipt-search-city' );
    var inp_zipcode      = jQuery( '.ipt-search-zipcode' );
    var inp_location     = jQuery( '.ipt-search-location' );
    var inp_video_search = jQuery( '.ipt-video-search' );

    if ( inp_username )
    {
      inp_username.each( function() {
        addClassIfNotEmpty( this, 'ipt-username' );
      } );

      inp_username.focus( function()
      {
        jQuery( this ).removeClass( 'ipt-username' );
      } );

      inp_username.blur( function()
      {
        addClassIfNotEmpty( this, 'ipt-username' );

//          if ( jQuery( this ).val() == '' )
//          {
//            jQuery( this ).addClass( 'ipt-username' );
//          }
      } );
    }

    if ( inp_password )
    {
      inp_password.each( function() {
        addClassIfNotEmpty( this, 'ipt-password' );
      } );

      inp_password.focus( function()
      {
        jQuery( this ).removeClass( 'ipt-password' );
      } );

      inp_password.blur( function()
      {
        addClassIfNotEmpty( this, 'ipt-password' );
//          if ( jQuery( this ).val() == '' )
//          {
//            jQuery( this ).addClass( 'ipt-password' );
//          }
      } );
    }

    if ( inp_email )
    {
      inp_email.each( function() {
        addClassIfNotEmpty( this, 'ipt-email' );
      } );

      inp_email.focus( function()
      {
        jQuery( this ).removeClass( 'ipt-email' );
      } );

      inp_email.blur( function()
      {
        addClassIfNotEmpty( this, 'ipt-email' );
//          if ( jQuery( this ).val() == '' )
//          {
//            jQuery( this ).addClass( 'ipt-email' );
//          }
      } );
    }

    if ( inp_event )
    {
      inp_event.each( function() {
        addClassIfNotEmpty( this, 'ipt-event' );
      } );

      inp_event.focus( function()
      {
        jQuery( this ).removeClass( 'ipt-event' );
      } );

      inp_event.blur( function()
      {
        addClassIfNotEmpty( this, 'ipt-event' );
//          if ( jQuery( this ).val() == '' )
//          {
//            jQuery( this ).addClass( 'ipt-event' );
//          }
      } );
    }

    if ( inp_city )
    {
      inp_city.each( function() {
        addClassIfNotEmpty( this, 'ipt-city' );
      } );

      inp_city.focus( function()
      {
        jQuery( this ).removeClass( 'ipt-city' );
      } );

      inp_city.blur( function()
      {
        addClassIfNotEmpty( this, 'ipt-city' );
//          if ( jQuery( this ).val() == '' )
//          {
//            jQuery( this ).addClass( 'ipt-city' );
//          }
      } );
    }

    if ( inp_zipcode )
    {
      inp_zipcode.each( function() {
        addClassIfNotEmpty( this, 'ipt-zipcode' );
      } );

      inp_zipcode.focus( function()
      {
        jQuery( this ).removeClass( 'ipt-zipcode' );
      } );

      inp_zipcode.blur( function()
      {
        addClassIfNotEmpty( this, 'ipt-zipcode' );
//          if ( jQuery( this ).val() == '' )
//          {
//            jQuery( this ).addClass( 'ipt-zipcode' );
//          }
      } );
    }

    if ( inp_location )
    {
      inp_location.each( function() {
        addClassIfNotEmpty( this, 'ipt-location' );
      } );

      inp_location.focus( function()
      {
        jQuery( this ).removeClass( 'ipt-location' );
      } );

      inp_location.blur( function()
      {
        addClassIfNotEmpty( this, 'ipt-location' );
//          if ( jQuery( this ).val() == '' )
//          {
//            jQuery( this ).addClass( 'ipt-location' );
//          }
      } );
    }

    if ( inp_video_search )
    {
      inp_video_search.each( function() {
        addClassIfNotEmpty( this, 'ipt-video-search' );
      } );

      inp_video_search.focus( function()
      {
        jQuery( this ).removeClass( 'ipt-video-search' );
      } );

      inp_video_search.blur( function()
      {
        addClassIfNotEmpty( this, 'ipt-video-search' );
//          if ( jQuery( this ).val() == '' )
//          {
//            jQuery( this ).addClass( 'ipt-video-search' );
//          }
      } );
    }
  } );
}
catch ( e ) {}

function addClassIfNotEmpty( elem, cssClass )
{
  if ( jQuery( elem ).val() == '' )
  {
    jQuery( elem ).addClass( cssClass );
  }
}

function debug( msg )
{
  try
  {
    var elem = null;

    if ( jQuery )
    {
      elem = jQuery( '#output' );
    }

    if ( window.console && window.console.log )
    {
      console.log( msg );
    }
    else if ( elem.length )
    {
      elem.html( elem.html() + '<br />' + msg );
    }
    else
    {
      alert( msg );
    }
  }
  catch ( e )
  {
//    alert( e );
  }
}

function processResponse( xhr, params )
{
  var result = [false];

  try
  {
    if ( xhr )
    {
      var response = jQuery.secureEvalJSON( xhr );

      if ( response )
      {
        switch ( response.status )
        {
          case 'error':
            var msg = '';
            jQuery.each( response.errors, function() {
              msg += this + '<br />';
            } );
            showError( msg, params.elemId );
            result = [ false, msg ];
            break;

          case 'success':
            result = [ true, 'MSG_ACTION_OK' ];
            showSuccess( response.message, params.elemId );
            break;

          default:
            result = [ false, 'ERR_UNKNOWN' ];
            break;
        }
      }
    }
  }
  catch ( e ) {
//    alert(e);
  }

  return result;
}

function showSuccess( msg, parentElem, msgClass )
{
  if ( !msgClass )
  {
    msgType = 'success';
  }

  showError( msg, parentElem, msgType );
}

function showError( msg, parentElem, msgType )
{
  try
  {
    var elem = jQuery( parentElem );

    if ( elem )
    {
      msg = msg.trimBr( '<br />' );

      var msgClass, msgClassInner;

      switch ( msgType )
      {
        case 'success':
          msgClass = 'success-box';
//          msgClassInner = 'success-msg';
          hideError( parentElem, 'error' );
          break;

        case 'error':
        default:
          msgClass = 'error-box';
//          msgClassInner = 'error-msg';
          hideError( parentElem, 'success' );
          break;
      }

      var elemBox = elem.closest( 'div.box' );
      var elemErr = null;

      if ( elemBox )
      {
        elemErr = elemBox.find( 'div.' + msgClass + ':first:visible' );

//        debug( elemErr );

        if ( elemErr.length == 1 )
        {
//          debug( 'elemErr exists' );
          elemErr.fadeOut( 500, function() {
            jQuery( this ).find( 'div.msg' )
                     .html( msg )
                     .end()
                     .fadeIn( 1000, function() { showElemInView( this ); } );
          } );

          return;
        }
        else
        {
//          debug( 'elemErr does not exist' );
          elemErr = jQuery( '<div />' ).addClass( msgClass )
                                  .click( function() { jQuery( this ).fadeOut( 500 ) } )
                                  .html( jQuery( '<div />' ).addClass( 'msg' )
                                                       .html( msg ) )
                                  .append( jQuery( '<span />' ).addClass( 'close' ) );
        }

        // insert error-box after head (if head exists)
        var elemBoxHead = elemBox.children( 'div.head:first' );

        if ( elemBoxHead.length == 1 )
        {
//          debug( 'elemErr.insertAfter( elemBoxHead )' );
          elemErr.insertAfter( elemBoxHead );
        }
        else
        {
          // insert error-box before content (if content exists)
          var elemBoxContent = elemBox.find( 'div.content:first' );

          if ( elemBoxContent.length == 1 )
          {
//            debug( 'elemErr.insertBefore( elemBoxContent )' );
            elemErr.insertBefore( elemBoxContent );
          }
          else
          {
            // insert error-box as first element in box
//            debug( 'elemErr.prependTo( elemBox )' );
            elemErr.prependTo( elemBox );
          }
        }

        elemErr.show( 'slow', function() { showElemInView( this ); } );
      }
    }
  }
  catch ( e )
  {
//    alert( e );
  }
}

function hideError( parentElem, msgType )
{
  try
  {
    var elem = jQuery( parentElem );

    if ( elem )
    {
      var msgClass, msgClassInner;

      switch ( msgType )
      {
        case 'success':
          msgClass = 'success-box';
//          msgClassInner = 'success-msg';
          break;

        case 'error':
        default:
          msgClass = 'error-box';
//          msgClassInner = 'error-msg';
          break;
      }

      var elemBox = elem.closest( 'div.box' );
      var elemErr = null;

      if ( elemBox )
      {
        elemErr = elemBox.find( 'div.' + msgClass + ':first:visible' );

//        debug( elemErr );

        if ( elemErr.length == 1 )
        {
//          debug( 'elemErr exists' );
          elemErr.fadeOut( 500, function() {
            jQuery( this ).find( 'div.msg' )
                     .html( '' )
                     .end()
                     .hide();
          } );

          return;
        }
      }
    }
  }
  catch ( e )
  {
//    alert( e );
  }
}

function showElemInView( elem )
{
  if ( !isVisibleOnScreen( elem, true ) )
  {
    jQuery.scrollTo( jQuery( elem ), 500, { axis: 'y' } );
  }
}

function isVisibleOnScreen( elem, completely )
{
  var elemWnd = jQuery( window );
  var elemXY = jQuery( elem );

  var docViewTop = elemWnd.scrollTop();
  var docViewBottom = docViewTop + elemWnd.height();

  var elemTop = elemXY.offset().top;
  var elemBottom = elemTop + elemXY.height();

  var result = false;

  if ( completely )
  {
    return ( ( docViewTop < elemTop ) && ( docViewBottom > elemBottom ) );
  }

  return ( ( elemBottom >= docViewTop ) && ( elemTop <= docViewBottom ) );
}

function getQueryVariable( variable, url )
{
  if ( url )
  {
    var urlParts = url.split( '?' );

    if ( urlParts.length < 2 )
    {
      return null;
    }

    query = urlParts[1];
  }
  else
  {
    query = window.location.search.substring( 1 );
  }

  var vars = query.split( '&' );

  for ( var i = 0; i < vars.length; i++ )
  {
    var pair = vars[i].split( '=' );

    if ( pair[0] == variable )
    {
      return pair[1];
    }
  }

  return null;
}

// Custom trim functions
String.prototype.lTrim = function () {
  return ( this.replace( /^\s+/, '' ) );
};
String.prototype.rTrim = function () {
  return ( this.replace( /\s+$/, '' ) );
};
String.prototype.trim = function () {
  return ( this.lTrim().rTrim() );
};
String.prototype.lTrimBr = function () {
  return ( this.replace( /^(<br \/>)+/, '' ) );
};
String.prototype.rTrimBr = function () {
  return ( this.replace( /(<br \/>)+$/, '' ) );
};
String.prototype.trimBr = function () {
  return ( this.lTrimBr().rTrimBr() );
};
String.prototype.ucfirst = function() {
  str = this;
  var f = this.charAt( 0 ).toUpperCase();
  return f + this.substr( 1 );
}
