$( document ).ready( function()
{
  jQuery.fn.extend(
  {
    scrollTo : function( speed, easing )
    {
      return this.each(function()
      {
        var targetOffset = $( this ).offset().top;
        $( 'html,body' ).animate( { scrollTop: targetOffset }, speed, easing );
      });
    }
  });
  $( ".show_subcamp_form" ).each( function()
  {
    $( this ).click( function()
    {
      var id = getId( $( this ).attr( "id" ) );
      if( $( "#subfor_"+ id ).is( ":hidden" ) )
      {
        $.ajax(
        {
          url: 'campaign/showsubcampaigns',
          timeout: 3600,
          data: "id=" + id,
          success: function( html )
          {
            if(html == 'not') window.location = "http://partner.sms-flatrate.de/";
            $( "#existing_subs_"  + id ).html( html);
            $( "#campaign_start_" + id ).scrollTo( 1500 );
            $( "#subfor_" + id ).slideDown( "slow" );
          }
        });
      }
      else
      {
        $( "#campaign_start_" + id ).scrollTo( 1500 );
        $( "#subfor_" + id ).slideUp( "slow" );
      }
    });
  });
  $( ".Add_Subcamp" ).each( function()
  {
    $( this ).click( function()
    {
      var id = getId( $( this ).attr("id") );
      $.ajax(
      {
        url: 'campaign/createsubcampaign',
        timeout: 36000,
        data: "id=" + id + "&comment=" + $( "#sub_comment_" + id ).val(),
        success: function( html )
        {
          if(html == 'not') window.location = "http://partner.sms-flatrate.de/";
          $( '#existing_camps_' + id ).append( html );
        }
      });
    });
  });
  $( ".campaign_name" ).each( function()
  {
    $( this ).click( function()
    {
      var id = getId( $( this ).attr( "id" ) );
      var content = $( this ).text();
      $( '#campaign_name_field_' + id ).val( content );
      $( this ).hide( 'slow' );
      $( '#edit_campaign_name_' + id ).show( 'slow' );
    });
  });
  $( ".campaign_name_button" ).each( function()
  {
    $( this ).click( function()
    {
      var id   = getId( $( this ).attr( "id" ) );
      var data = $( "#campaign_name_field_" + id ).val();
      $.ajax(
      {
        url: 'campaign/updatecampaign',
        timeout: 36000,
        data: "name=" + data + "&id=" + id,
        success: function( html )
        {
          if(html == 'not') window.location = "http://partner.sms-flatrate.de/";
          $( '#edit_campaign_name_' + id ).hide( 'slow' );
          $( '#campaign_name_' + id ).html( html ).show( 'slow' );
        }
      });
    });
  });
  $( ".campaign_comment" ).each( function()
  {
    $( this ).click( function()
    {
      var id = getId( $( this ).attr( "id" ) );
      var content = $( this ).text();
      $( "#campaign_comment_field_" + id ).val( content );
      $( this ).hide( "slow" );
      $( "#edit_campaign_comment_" + id ).show( 'slow' );
    });
  });
  $( ".campaign_comment_button" ).each( function()
  {
    $( this ).click( function()
    {
      var id = getId( $( this ).attr( "id" ) );
      var data = $( "#campaign_comment_field_" + id ).val();
      $.ajax(
      {
        url: 'campaign/updatecampaign',
        timeout: 36000,
        data: "comment=" + data + "&id=" + id,
        success: function( html )
        {
          if(html == 'not') window.location = "http://partner.sms-flatrate.de/";
          $( '#edit_campaign_comment_'+ id).hide( "slow" );
          $( '#campaign_comment_'     + id).html( html ).show( "slow" );
        }
      });
    });
  });
  $( ".editable" ).each( function()
  {
    $( this ).mouseover( function()
    {
      $( this ).attr('title','Text Anklicken zum Editieren');
      $( this ).css('background-color','#FFFF99'  );
      $( this ).css('cursor','pointer');
    });
    $ ( this ).mouseout( function()
    {
      $( this ).css('background-color','transparent' );
      $( this ).css('cursor','auto');
    });
  });
  $( '#forgotten_password' ).click( function()
  {
    $( '#login_table' ).hide();
    $( '#request_password' ).show( 'slow' );
  });
  $( '#login_form' ).click( function()
  {
    $( '#request_password' ).hide();
    $( '#login_table' ).show( 'slow' );
  });

  $('#email_field').click(
  function()
  {
      $(this).val('');
      $(this).unbind('click');
  });

   $('#feedback_send').click(
    function(){
      $.ajax({
        url: '/partner/feedback',
        timeout: 36000,
        data: "feedback=" + $('#feedback_text').val()+'&url='+window.location,
        success: function( html )
        {
          $('#feedback_response').remove();
          $('#feedback_content').append( html );
          $('#feedback_text').val('');
        }
      });
    });

    $(window).scroll(function() {
      $('#scrollable').css('top',0+$(this).scrollTop() + "px");
    });

});

function getId( str )
{
  var pos = str.lastIndexOf( "_" ) + 1;
  if ( pos < 0 ) return false;
  return str.substr( pos );
}

function trim( str )
{
  str = "" + str;
  return str.replace(/^\s+|\s+$/g, '');
}

