/**
 * @fileoverview js/module_profile
 * 
 * @description network main page functions<br />
 * 
 * @author bKarolyi
 */

/**
 * network frontpage namespace
 * @namespace
 */
n_frontpage = 
{
  deny: function( link_el )
  {
    var url = link_el.href;
    var contentbox = Dom.getAncestorByClassName( link_el, "n_contentbox" );
    nw.ajaxUpdate( url, contentbox );
    
    return true; 
  }
}

/**
 * utilities for offer box
 * @namespace
 */
n_frontpage.offer =
{
  picture_fading: false,
  text_fading: false,
  
  /**
   * change box content with ajax
   * @param {HTMLelement} link_el link for changing box content 
   */
  changeContent: function( link_el )
  {
    nw.ajaxUpdate( link_el.href, "n_offerclub_wrap" );
    return true;
  },
  /**
   * change tab
   * @param {Number} tab_num number of the tab
   */
  changeTab: function( tab_num )
  {
    if ( Dom.hasClass( "n_offertab_" + tab_num ))
    {
      return true
    }
    
    nw.changeTab( "offer", 3, tab_num );
    for( i_tab = 1; i_tab <= 3; i_tab++ )
    {
      if ( i_tab == tab_num )
      {
        nw.show( "n_offerpicture_" + i_tab + "_link" );
      }
      else
      {
        nw.hide( "n_offerpicture_" + i_tab + "_link" );
      }
    }
    return true;

    //returned without animation     
    if ( this.picture_fading || this.text_fading )
    {
      return false;
    }
    
    var animateout_picture;
    var animatein_picture;
    var animateout_text;
    var animatein_text;
    var active_tab = Dom.getElementsByClassName( "n_activetab", "a", Dom.get( "n_offerclubs_list" ) )[0];
    var active_id = active_tab.id.split( "_" )[2];
    
    animateout_picture = new YAHOO.util.Anim( Dom.get( "n_offercontent_" + active_id ), 
                {
                  opacity: { from: 1,
                             to: 0 }
                },
                0.5
    );
    
    animateout_picture.onComplete.subscribe( function()
    {
      for( i_tab_num = 1; i_tab_num <= 3; i_tab_num++ )
      {
        if ( i_tab_num == tab_num )
        {
          Dom.addClass( "n_offertab_" + i_tab_num, "n_activetab" );
          //Dom.setStyle( "n_offercontent_" + i_tab_num, "opacity", 0 );
          //Dom.removeClass( "n_offercontent_" + i_tab_num, "n_invisible" );
          nw.show( "n_offerpicture_" + i_tab_num + "_link" );
          nw.show( "n_offercontent_" + i_tab_num + "_link" );
        }
        else
        {
          Dom.removeClass( "n_offertab_" + i_tab_num, "n_activetab" );
          //Dom.addClass( "n_offercontent_" + i_tab_num, "n_invisible" );
          nw.hide( "n_offercontent_" + i_tab_num + "_link" );
          nw.hide( "n_offerpicture_" + i_tab_num + "_link" );
        }
      }
      animatein_picture.animate();
    }, this, true );

    animatein_picture = new YAHOO.util.Anim( Dom.get( "n_offercontent_" + tab_num ), 
                {
                  opacity: { from: 0,
                             to: 1 }
                },
                0.5
    );
    
    animatein_picture.onComplete.subscribe( function()
    {
      n_frontpage.offer.picture_fading = false;
    }, this, true );
    
    animateout_text = new YAHOO.util.Anim( Dom.get( "n_offerextracontent_" + active_id ), 
                {
                  opacity: { from: 1,
                             to: 0 }
                },
                0.5
    );
    
    animateout_text.onComplete.subscribe( function()
    {
      for( i_tab_num = 1; i_tab_num <= 3; i_tab_num++ )
      {
        if ( i_tab_num == tab_num )
        {
          Dom.setStyle( "n_offercontent_" + i_tab_num, "opacity", 0 );
          Dom.removeClass( "n_offerextracontent_" + i_tab_num, "n_invisible" );
        }
        else
        {
          Dom.addClass( "n_offerextracontent_" + i_tab_num, "n_invisible" );
        }
      }
      
      animatein_text.animate();
    }, this, true );

    animatein_text = new YAHOO.util.Anim( Dom.get( "n_offerextracontent_" + tab_num ), 
                {
                  opacity: { from: 0,
                             to: 1 }
                },
                0.5
    );
    
    animatein_text.onComplete.subscribe( function()
    {
      n_frontpage.offer.text_fading = false;
    }, this, true );
    this.picture_fading = true;
    animateout_picture.animate();
    
    //this.text_fading = true;
    //animateout_text.animate();
    
  }
}

/**
 * utilities for seeded users
 * @namespace
 */
n_frontpage.seeded_users =
{
  act_left: 0,
  /**
   * width of a picture card in px
   * @type {Number}
   */
  cardwidth: 0,
  /**
   * picture cards in the box
   * @type {Array of HTMLelements}
   */
  cards: [],
  /**
   * whether animation is on
   * @type {Boolean}
   */
  moving: false,
  /**
   * whether Dom is ready
   * @type {Boolean}
   */
  ready: false,
  /**
   * initial functions
   */
  _init: function()
  {
    if ( !Dom.get( "slidecontainerwrap" ) )
    {
      return false;
    }
    
    this.cards = Dom.getElementsByClassName( "video_slide", "div", Dom.get( "slidecontainerwrap" ) );
    this.cardwidth = 97;//parseInt( Dom.getStyle( this.cards[0], "width" ) );
    
    Dom.get( "slidecontainerinlay" ).style.left = "0px";
    this.ready = true;
  },
  /**
   * inserts first card into the end of cards
   */
  cloneFirstCard: function()
  {
    var first_cloned = this.cards[ 0 ].cloneNode( true );
    Dom.insertAfter( first_cloned, this.cards[ this.cards.length - 1 ] );
    this.cards.push( first_cloned );
  },
  /**
   * inserts last card into the beginning of the cards
   */
  cloneLastCard: function()
  {
    var last_cloned = this.cards[ this.cards.length - 1 ].cloneNode( true );
    Dom.insertBefore( last_cloned, this.cards[ 0 ] );
    Dom.get( "slidecontainerinlay" ).style.left = this.act_left - this.cardwidth + "px";
    this.act_left -= this.cardwidth;
    //var act_left = parseInt( Dom.get( "slidecontainerinlay" ).style.left );
    //Dom.get( "slidecontainerinlay" ).style.left = act_left - this.cardwidth + "px";
    this.cards.unshift( last_cloned );
  },
  /**
   * removes first card
   */
  removeFirstCard: function()
  {
    this.cards[ 0 ].parentNode.removeChild( this.cards[ 0 ] );
    Dom.get( "slidecontainerinlay" ).style.left = this.act_left + this.cardwidth + "px";
    this.act_left += this.cardwidth; 
    
    //var act_left = parseInt( Dom.get( "slidecontainerinlay" ).style.left );
    //Dom.get( "slidecontainerinlay" ).style.left = this.act_left + this.cardwidth + "px";
    this.cards.shift();
    return true;
  },
  /**
   * removes last card
   */
  removeLastCard: function()
  {
    this.cards[ this.cards.length - 1 ].parentNode.removeChild( this.cards[ this.cards.length - 1 ] );
    this.cards.pop();
    return false;
  },
  /**
   * scrolls right
   */
  scrollRight: function()
  {
    if ( !this.ready )
    {
      return false;
    }
    
    if ( this.cards.length == 0 )
    {
      return false;
    }
    
    if ( this.moving )
    {
      return false;
    }
    
    this.cloneFirstCard();
    
    var new_left = this.act_left - this.cardwidth;
    
    var animate = new YAHOO.util.Anim( Dom.get( "slidecontainerinlay" ), 
                  {
                    left: { to: new_left } 
                  },
                  0.5,
                  YAHOO.util.Easing.backIn
    );
    
    animate.onComplete.subscribe( function()
    {
      Dom.setStyle( Dom.get( "slidecontainerinlay" ), "left", new_left + "px" );
      n_frontpage.seeded_users.act_left = new_left;
      n_frontpage.seeded_users.removeFirstCard();
      n_frontpage.seeded_users.moving = false;
    }, this, true );
    
    this.moving = true;
    animate.animate();
  },
  /**
   * scrolls left
   */
  scrollLeft: function()
  {
    if ( !this.ready )
    {
      return false;
    }
    
    if ( this.cards.length == 0 )
    {
      return false;
    }
    
    if ( this.moving )
    {
      return false;
    }
    
    this.cloneLastCard();
    
    var new_left = this.act_left + this.cardwidth;
    
    var animate = new YAHOO.util.Anim( Dom.get( "slidecontainerinlay" ), 
                  {
                    left: { by: n_frontpage.seeded_users.cardwidth } 
                  },
                  0.5,
                  YAHOO.util.Easing.backIn
    );
    
    animate.onComplete.subscribe( function()
    {
      Dom.setStyle( Dom.get( "slidecontainerinlay" ), "left", new_left + "px" );
      n_frontpage.seeded_users.act_left = new_left;
      n_frontpage.seeded_users.removeLastCard();
      n_frontpage.seeded_users.moving = false;
    }, this, true );
    
    this.moving = true;
    animate.animate();
  }
}

n_frontpage.club_tv = 
{
  link_counter: 0,
  link_array: [],
  playbutton_counter: 0,
  playbutton_array: [],
  ready: false,
  _init: function()
  {
    if ( !Dom.get( "n_clubtv_slider" ) )
    {
      return false;
    }
    
    var a_el = Dom.get( "n_clubtv_slider" ).getElementsByTagName( "a" );
    for( i_a = 0; i_a < a_el.length; i_a++ )
    {
      this.link_array.push( Dom.get( "n_morevideos_counter_" + ( i_a + 1 ) ).value );
      this.playbutton_array.push( "n_playbutton_" + ( i_a + 1 ) );
      var act_a = a_el[i_a];
      n_frontpage.club_tv.picture.cards.push( act_a );
    }
    
    n_frontpage.club_tv.picture.cards.push();
    n_frontpage.club_tv.text.cards = Dom.getElementsByClassName( "n_clubtvinfowrap", "div", Dom.get( "n_clubtvinfoslider" ) );
    
    n_frontpage.club_tv.picture.width = 215;
    n_frontpage.club_tv.text.width = 271;
    Dom.get( "n_clubtv_slider" ).style.left = "0px";
    Dom.get( "n_clubtvinfoslider" ).style.left = "0px";
    this.ready = true;
  },
  decreaseLinkCounter: function()
  {
    if ( this.link_counter == 0 )
    {
      this.link_counter = this.link_array.length - 1;
    }
    else
    {
      this.link_counter--;
    }
    
    Dom.get( "n_morevideos_link" ).href = this.link_array[ this.link_counter ]; 
  },
  increaseLinkCounter: function()
  {
    if ( this.link_counter == this.link_array.length - 1 )
    {
      this.link_counter = 0;
    }
    else
    {
      this.link_counter++;
    }
    
    Dom.get( "n_morevideos_link" ).href = this.link_array[ this.link_counter ];
  },
  decreasePlayCounter: function()
  {
    nw.hide( this.playbutton_array[ this.playbutton_counter ] );
    if ( this.playbutton_counter == 0 )
    {
      this.playbutton_counter = this.playbutton_array.length - 1;
    }
    else
    {
      this.playbutton_counter--;
    }
    
    nw.show( this.playbutton_array[ this.playbutton_counter ] );
  },
  increasePlayCounter: function()
  {
    nw.hide( this.playbutton_array[ this.playbutton_counter ] );
    
    if ( this.playbutton_counter == this.playbutton_array.length - 1 )
    {
      this.playbutton_counter = 0;
    }
    else
    {
      this.playbutton_counter++;
    }
    
    nw.show( this.playbutton_array[ this.playbutton_counter ] );
  },
  scrollLeft: function()
  {
    if ( !this.ready )
    {
      return false;
    }
    
    if ( this.picture.moving || this.text.moving )
    {
      return false;
    }
    
    this.picture.scrollLeft();
    this.text.scrollLeft();
    this.decreaseLinkCounter();
    this.decreasePlayCounter();
    return true;
  },
  scrollRight: function()
  {
    if ( !this.ready )
    {
      return false;
    }
    
    if ( this.picture.moving || this.text.moving )
    {
      return false;
    }
    
    this.picture.scrollRight();
    this.text.scrollRight();
    this.increaseLinkCounter();
    this.increasePlayCounter();
    return true;
  }
}

n_frontpage.club_tv.picture = 
{
  act_left: 0,
  width: 0,
  cards: [],
  moving: false,
  /**
   * inserts first card into the end of cards
   */
  cloneFirstCard: function()
  {
    var first_cloned = this.cards[ 0 ].cloneNode( true );
    Dom.insertAfter( first_cloned, this.cards[ this.cards.length - 1 ] );
    this.cards.push( first_cloned );
  },
  /**
   * inserts last card into the beginning of the cards
   */
  cloneLastCard: function()
  {
    var last_cloned = this.cards[ this.cards.length - 1 ].cloneNode( true );
    Dom.insertBefore( last_cloned, this.cards[ 0 ] );
    Dom.get( "n_clubtv_slider" ).style.left = this.act_left - this.width + "px";
    this.act_left -= this.width;
    this.cards.unshift( last_cloned );
  },
  /**
   * removes first card
   */
  removeFirstCard: function()
  {
    this.cards[ 0 ].parentNode.removeChild( this.cards[ 0 ] );
    Dom.get( "n_clubtv_slider" ).style.left = this.act_left + this.width + "px";
    this.act_left += this.width; 
    this.cards.shift();
    return true;
  },
  /**
   * removes last card
   */
  removeLastCard: function()
  {
    this.cards[ this.cards.length - 1 ].parentNode.removeChild( this.cards[ this.cards.length - 1 ] );
    this.cards.pop();
    return false;
  },
  /**
   * scrolls right
   */
  scrollRight: function()
  {
    this.cloneFirstCard();
    var new_left = this.act_left - this.width;
    var animate = new YAHOO.util.Anim( Dom.get( "n_clubtv_slider" ), 
                  {
                    left: { to: new_left } 
                  },
                  0.5,
                  YAHOO.util.Easing.backIn
    );
    
    animate.onComplete.subscribe( function()
    {
      Dom.setStyle( Dom.get( "n_clubtv_slider" ), "left", new_left + "px" );
      n_frontpage.club_tv.picture.act_left = new_left;
      n_frontpage.club_tv.picture.removeFirstCard();
      n_frontpage.club_tv.picture.moving = false;
    }, this, true );
    
    this.moving = true;
    animate.animate();
  },
  /**
   * scrolls left
   */
  scrollLeft: function()
  {
    this.cloneLastCard();
    var new_left = this.act_left + this.width;
    var animate = new YAHOO.util.Anim( Dom.get( "n_clubtv_slider" ), 
                  {
                    left: { to: new_left } 
                  },
                  0.5,
                  YAHOO.util.Easing.backIn
    );
    
    animate.onComplete.subscribe( function()
    {
      Dom.setStyle( Dom.get( "n_clubtv_slider" ), "left", new_left + "px" );
      n_frontpage.club_tv.picture.act_left = new_left;
      n_frontpage.club_tv.picture.removeLastCard();
      n_frontpage.club_tv.picture.moving = false;
    }, this, true );
    
    this.moving = true;
    animate.animate();
  }
}

n_frontpage.club_tv.text = 
{
  act_left: 0,
  width: 0,
  cards: [],
  moving: false,
  /**
   * inserts first card into the end of cards
   */
  cloneFirstCard: function()
  {
    var first_cloned = this.cards[ 0 ].cloneNode( true );
    Dom.insertAfter( first_cloned, this.cards[ this.cards.length - 1 ] );
    this.cards.push( first_cloned );
  },
  /**
   * inserts last card into the beginning of the cards
   */
  cloneLastCard: function()
  {
    var last_cloned = this.cards[ this.cards.length - 1 ].cloneNode( true );
    Dom.insertBefore( last_cloned, this.cards[ 0 ] );
    Dom.get( "n_clubtvinfoslider" ).style.left = this.act_left - this.width + "px";
    this.act_left -= this.width;
    this.cards.unshift( last_cloned );
  },
  /**
   * removes first card
   */
  removeFirstCard: function()
  {
    this.cards[ 0 ].parentNode.removeChild( this.cards[ 0 ] );
    Dom.get( "n_clubtvinfoslider" ).style.left = this.act_left + this.width + "px";
    this.act_left += this.width;
    this.cards.shift();
    return true;
  },
  /**
   * removes last card
   */
  removeLastCard: function()
  {
    this.cards[ this.cards.length - 1 ].parentNode.removeChild( this.cards[ this.cards.length - 1 ] );
    this.cards.pop();
    return false;
  },
  /**
   * scrolls right
   */
  scrollRight: function()
  {
    this.cloneFirstCard();
    var new_left = this.act_left - this.width;
    var animate = new YAHOO.util.Anim( Dom.get( "n_clubtvinfoslider" ), 
                  {
                    left: { to: new_left } 
                  },
                  0.5,
                  YAHOO.util.Easing.backIn
    );
    
    animate.onComplete.subscribe( function()
    {
      Dom.setStyle( Dom.get( "n_clubtvinfoslider" ), "left", new_left + "px" );
      n_frontpage.club_tv.text.act_left = new_left;
      n_frontpage.club_tv.text.removeFirstCard();
      n_frontpage.club_tv.text.moving = false;
    }, this, true );
    
    this.moving = true;
    animate.animate();
  },
  /**
   * scrolls left
   */
  scrollLeft: function()
  {
    this.cloneLastCard();
    var new_left = this.act_left + this.width;
    var animate = new YAHOO.util.Anim( Dom.get( "n_clubtvinfoslider" ), 
                  {
                    left: { to: new_left } 
                  },
                  0.5,
                  YAHOO.util.Easing.backIn
    );
    
    animate.onComplete.subscribe( function()
    {
      Dom.setStyle( Dom.get( "n_clubtvinfoslider" ), "left", new_left + "px" );
      n_frontpage.club_tv.text.act_left = new_left;
      n_frontpage.club_tv.text.removeLastCard();
      n_frontpage.club_tv.text.moving = false;
    }, this, true );
    
    this.moving = true;
    animate.animate();
  }
}

n_frontpage.searchbox =
{
  changeTab: function( num, link_el )
  {
    if ( Dom.hasClass( link_el, "active" ) )
    {
      return false;
    }

    if ( num == 1 )
    {
      Dom.addClass( Dom.get("n_searchclub_tab"), "active" );
      Dom.removeClass( Dom.get("n_searchclub_content"), "n_invisible" );

      Dom.removeClass( Dom.get("b_searchfriend_tab"), "active" );
      Dom.addClass( Dom.get("n_searchfriend_content"), "n_invisible" );
    }
    else if ( num == 2 )
    {
      Dom.addClass( Dom.get("b_searchfriend_tab"), "active" );
      Dom.removeClass( Dom.get("n_searchfriend_content"), "n_invisible" );

      Dom.removeClass( Dom.get("n_searchclub_tab"), "active" );
      Dom.addClass( Dom.get("n_searchclub_content"), "n_invisible" );
    }
  }
}

n_frontpage.fresh_members =
{
  actual_contentbox: null,
  next_contentbox: null,
  contentboxes: [],
  fading: false,
  getMoreTags: function( link_el )
  {
    if ( this.contentboxes.length == 0 )
    {
      this.contentboxes = Dom.getElementsByClassName( "n_contentboxinlay", "div", Dom.get( "n_freshmembersbox" ) );
    }
    
    this.actual_contentbox = Dom.getAncestorByClassName( link_el, "n_contentboxinlay" );
    
    this.setNextContentbox( this.actual_contentbox );
    
    if ( this.fading )
    {
      return false;
    }
    
    var animateout = new YAHOO.util.Anim( this.actual_contentbox, 
                {
                  opacity: { from: 1,
                             to: 0 } 
                },
                0.5
    );
    
    animateout.onComplete.subscribe( function()
    {
      Dom.addClass( this.actual_contentbox, "n_invisible" );
      Dom.setStyle( this.next_contentbox, "opacity", "0" );
      Dom.removeClass( this.next_contentbox, "n_invisible" );
      animatein.animate();
    }, this, true );

    var animatein = new YAHOO.util.Anim( this.next_contentbox, 
                {
                  opacity: { from: 0,
                             to: 1 } 
                },
                0.5
    );
    
    animatein.onComplete.subscribe( function()
    {
      this.fading = false;
      this.actual_contentbox = this.next_contentbox;
      this.setNextContentbox();
    }, this, true );
    
    this.fading = true;
    animateout.animate();

  },
  setNextContentbox: function()
  {
    for( i_box = 0; i_box < this.contentboxes.length; i_box++ )
    {
      var act_box = this.contentboxes[i_box];
      if ( act_box == this.actual_contentbox )
      {
        if ( i_box == this.contentboxes.length - 1 )
        {
          this.next_contentbox = this.contentboxes[0];
        }
        else
        {
          this.next_contentbox = this.contentboxes[i_box + 1];
        }
      }
    }
  }
}

n_frontpage.possible_friends =
{
  getMore: function()
  {
    var fade = new nw.effect.fadeManager( 
    {
      "fadeout_element": "possible_friends_box_0",
      "fadein_element": "possible_friends_box_1"
    } );
    fade.fadeOut();
  }
}

n_frontpage.popular_members =
{
  getMore:function( url, id )
  {
    var fade = new nw.effect.fadeManager(
    {
      "fadeout_element": "n_populartags_box"
    } );
    
    fade.afterFadeOut = function()
    {
      var parent = Dom.get( id ).parentNode;
      parent.removeChild( Dom.get( id ) );
      parent.innerHTML = this.ajax_result;
      this.fadeout_element = Dom.get( id );
      this.fadein_element = Dom.get( id );
      Dom.setStyle( this.fadein_element, "opacity", 0 );
      this.fadeIn();
      return true;
    }
    
    new n_ajax_class( url, id, function( o )
    {
      fade.ajax_result = o.responseText;
      fade.fadeOut();
    } ).send();
  }
}

n_frontpage.search_box =
{
  back: function()
  {
    var fade = new nw.effect.fadeManager(
    {
      "fadeout_element": "n_searchresults",
      "fadein_element": "n_searchform"
    } );
    
    fade.fadeOut();
  },
  offerClub: function( form_el )
  {
    var fade = new nw.effect.fadeManager(
    {
      "fadeout_element": "n_searchform",
      "fadein_element": "n_searchresults"
    } );
    
    fade.afterFadeOut = function()
    {
      var parent = Dom.get( this.fadeout_element ).parentNode;
      nw.hide( this.fadeout_element );
      Dom.setStyle( this.fadein_element, "opacity", 0 );
      nw.show( this.fadein_element );
      Dom.get( this.fadein_element ).innerHTML = this.ajax_result;
      this.fadeIn();
      return true;
    }
    
    new n_ajax_class( null, "n_searchform", function( o )
    {
      fade.ajax_result = o.responseText;
      fade.fadeOut();
    } ).sendForm( this, form_el );
  }
}

n_frontpage.myfriendsclubs =
{
  getMore:function( url, id )
  {
    var fade = new nw.effect.fadeManager(
    {
      "fadeout_element": id
    } );
    
    fade.afterFadeOut = function()
    {
      Dom.get( id ).innerHTML = this.ajax_result;
      this.fadeout_element = Dom.get( id );
      this.fadein_element = Dom.get( id );
      Dom.setStyle( this.fadein_element, "opacity", 0 );
      this.fadeIn();
      return true;
    }
    
    new n_ajax_class( url, id, function( o )
    {
      fade.ajax_result = o.responseText;
      fade.fadeOut();
    } ).send( this );
  }
}

Event.onDOMReady( function()
{
  n_frontpage.seeded_users._init();
  n_frontpage.club_tv._init();  
} )
