﻿/**
 * Description: general javascript actions and functions for the news Module News V2
 * User: antonio.r
 * Date: 19/Out/2011
 **/

/* todo completar descrição  */
function onLoadCustomerSupport() {
  $( '.paragraph' ).css( "min-height" , "320px" );
  $( 'ul#customerSupport li ul li div' ).css( "display" , "none" );
  $( 'ul#customerSupport li ul' ).css( "display" , "none" );
  $( 'ul#customerSupport h2' ).click( function() {
    var elementToSlide = $( this ).next();
    $( 'ul#customerSupport li ul' ).not( elementToSlide ).slideUp( "slow" );
    if ( $( this ).find( "span" ).text() != "-" ) {
      $( 'ul#customerSupport li h2 span' ).html( "+" );
      $( this ).find( "span" ).html( "-" );
    }
    else {
      $( this ).find( "span" ).html( "+" );
    }
    elementToSlide.slideToggle();
  } );
  $( 'ul#customerSupport li ul li h3' ).click( function() {
    var elementToSlide = $( this ).next();
    $( 'ul#customerSupport li ul li div' ).not( elementToSlide ).slideUp( "slow" );
    if ( $( this ).find( "span" ).text() != "-" ) {
      $( 'ul#customerSupport li ul li h3 span' ).html( "+" );
      $( this ).find( "span" ).html( "-" );
    }
    else {
      $( this ).find( "span" ).html( "+" );
    }
    elementToSlide.slideToggle();
  } );
}

/* todo completar descrição  */
function onLoadHomeHighlightImageSrc(str_imgUrl , str_newsHeading) {
  str_imageIfNoFlash = '<img src="/admin/public/getimage.aspx?Image=' + str_imgUrl + '&amp;Format=jpg&amp;Width=656&amp;Height=204&amp;Compression=80&amp;Crop=5" alt="' + str_newsHeading + '" />';
}

/* todo completar descrição  */
function onLoadHomeHighlightFlashNews(int_loopCounter , str_flashUrl) {
  if ( navigator.platform != 'iPad' && navigator.platform != 'iPhone' && navigator.platform != 'iPod' ) {

    var str_userAgent = navigator.userAgent.toLowerCase(),
        bol_isAndroid = str_userAgent.indexOf( "android" ) > -1; //&& ua.indexOf("mobile");

    if ( !bol_isAndroid ) {
      $( ".newsBanner_" + int_loopCounter + " >div>a" ).flash( {
        src: str_flashUrl,
        width: 656,
        height: 204,
        click:true
      } );
    }
    else {
      $( ".newsBanner_" + int_loopCounter + " >div>a" ).html( str_imageIfNoFlash );
    }
  }
  else {
    $( ".newsBanner_" + int_loopCounter + " >div>a" ).html( str_imageIfNoFlash );
  }
}

/* todo completar descrição  */
function onLoadHomeHighlightStaticNews(int_loopCounter) {
  $( ".newsBanner_" + int_loopCounter + " >div>a" ).html( str_imageIfNoFlash );
}

/* todo completar descrição  */
function addClickEvent(object) {
  window.location.href = object.href;
}

/* todo completar descrição  */
function onLoadChangeBannerPromotion() {
  $( "#homeHighlightBannerButtons li" ).click( function() {
    $( "#homeHighlightBanner li" ).css( {"display":"none"} );
    $( "#homeHighlightBanner li.newsBanner_" + $( this ).attr( "id" ).split( '_' )[1] ).css( {"display":"block"} );
    $( "#homeHighlightBannerButtons li" ).removeClass();
    $( this ).addClass( "activeItem" );
  } )
}

/* todo completar descrição  */
function onLoadNewsPagination(element) {

  if ( $( "." + element + " > a" ).length >= 1 ) {

    /* todo meter translates */
    var int_countPages = $( "." + element + " a" ).length,
        str_anchorHref = $( "." + element + " a:first" ).attr( "href" ),
        int_actualPage = parseInt( $( "." + element + " a b" ).html() ),
        str_htmlPagination;

    if ( int_actualPage >= 2 ) {
      str_htmlPagination = "<span class='firstPage'><a href=' " + str_anchorHref.split( "&" )[0] + "&" + str_anchorHref.split( "&" )[1] + "&" + str_anchorHref.split( "&" )[2] + "&" + str_anchorHref.split( "&" )[3] + "&currentPage=1'>firstPage</a></span><span class='previousPage'><a href=' " + str_anchorHref.split( "&" )[0] + "&" + str_anchorHref.split( "&" )[1] + "&" + str_anchorHref.split( "&" )[2] + "&" + str_anchorHref.split( "&" )[3] + "&currentPage=" + (int_actualPage - 1) + "'>previousPage</a></span><ul class='paginationNews'>";
      str_htmlPagination += "<li class='firstItem' ><a href='" + str_anchorHref.split( "&" )[0] + "&" + str_anchorHref.split( "&" )[1] + "&" + str_anchorHref.split( "&" )[2] + "&" + str_anchorHref.split( "&" )[3] + "&currentPage=" + (int_actualPage - 1) + " '>" + (int_actualPage - 1) + "</a> - </li>";
    }
    else {
      str_htmlPagination = "<ul class='paginationNews'>";
    }

    if ( int_actualPage != int_countPages )
      str_htmlPagination += "<li class='activeItem' ><a href='" + str_anchorHref.split( "&" )[0] + "&" + str_anchorHref.split( "&" )[1] + "&" + str_anchorHref.split( "&" )[2] + "&" + str_anchorHref.split( "&" )[3] + "&currentPage=" + int_actualPage + " '>" + int_actualPage + "</a> - </li>";
    else if ( int_actualPage == int_countPages )
      str_htmlPagination += "<li class='activeItem' ><a href='" + str_anchorHref.split( "&" )[0] + "&" + str_anchorHref.split( "&" )[1] + "&" + str_anchorHref.split( "&" )[2] + "&" + str_anchorHref.split( "&" )[3] + "&currentPage=" + int_actualPage + " '>" + int_actualPage + "</a></li>";

    if ( (int_actualPage + 1) <= int_countPages )
      str_htmlPagination += "<li ><a href='" + str_anchorHref.split( "&" )[0] + "&" + str_anchorHref.split( "&" )[1] + "&" + str_anchorHref.split( "&" )[2] + "&" + str_anchorHref.split( "&" )[3] + "&currentPage=" + (int_actualPage + 1) + " '>" + (int_actualPage + 1) + "</a> - </li>";

    if ( (int_actualPage + 2) <= int_countPages )
      str_htmlPagination += "<li class='lasItem' ><a href='" + str_anchorHref.split( "&" )[0] + "&" + str_anchorHref.split( "&" )[1] + "&" + str_anchorHref.split( "&" )[2] + "&" + str_anchorHref.split( "&" )[3] + "&currentPage=" + (int_actualPage + 2) + " '>" + (int_actualPage + 2) + "</a></li>";



    if ( int_actualPage == int_countPages ) {
      str_htmlPagination += "</ul>";
    }
    else {
      str_htmlPagination += "</ul><span class='nextPage'><a href=' " + str_anchorHref.split( "&" )[0] + "&" + str_anchorHref.split( "&" )[1] + "&" + str_anchorHref.split( "&" )[2] + "&" + str_anchorHref.split( "&" )[3] + "&currentPage=" + (int_actualPage + 1) + " '>nextPage</a></span><span class='lastPage'><a href=' " + str_anchorHref.split( "&" )[0] + "&" + str_anchorHref.split( "&" )[1] + "&" + str_anchorHref.split( "&" )[2] + "&" + str_anchorHref.split( "&" )[3] + "&currentPage=" + int_countPages + "'>lastPage</a></span>";
    }

    $( "." + element ).append( str_htmlPagination );
    $( "." + element + " > a" ).remove();
  }

}

/* todo completar descrição  */
function onLoadRedefineParagraph() {
  $( ".newsList" ).parent().parent( ":last" ).addClass( "redefineParagraph" );
}

/* todo completar descrição  */
function onLoadHomeNewsPagination() {

  var int_currentPage = 0,
      int_totalNews = $( ".newsListHome li.item1" ).length;

  $( "#totalPages" ).html( int_totalNews );
  $( ".newsListHome li.item1:first" ).show();
  $( ".newsListHome li.item0:first" ).show();

  if ( !int_currentPage ) {
    $( "#currentPage" ).html( (int_currentPage + 1) );
  }

  $( "#nextNews" ).click( function() {
    $( ".newsListHome li.item0" ).each( function(i) {
      if ( $( this ).css( "display" ) == "block" ) {
        int_currentPage = i;
      }
    } );
    if ( int_currentPage < int_totalNews - 1 ) int_currentPage++;
    $( "#currentPage" ).html( (int_currentPage + 1) );
    $( ".newsListHome li" ).hide();
    $( ".newsListHome li.item0:eq(" + int_currentPage + ")" ).show();
    $( ".newsListHome li.item1:eq(" + int_currentPage + ")" ).show();
  } );
  $( "#prevNews" ).click( function() {
    $( ".newsListHome li.item0" ).each( function(i) {
      if ( $( this ).css( "display" ) == "block" ) {
        int_currentPage = i;
      }
    } );
    if ( int_currentPage > 0 ) int_currentPage--;
    $( "#currentPage" ).html( (int_currentPage + 1) );
    $( ".newsListHome li" ).hide();
    $( ".newsListHome li.item0:eq(" + int_currentPage + ")" ).show();
    $( ".newsListHome li.item1:eq(" + int_currentPage + ")" ).show();
  } );
}

/* todo completar descrição  */
function changeBannerPromotion() {

  if ( $( "#homeHighlightBannerButtons li.activeItem" ).index() + 1 < $( "#homeHighlightBannerButtons li" ).length ) {
    $( "#homeHighlightBannerButtons li" ).eq( $( "#homeHighlightBannerButtons li.activeItem" ).index() + 1 ).click();
  }
  else {
    $( "#homeHighlightBannerButtons li" ).eq( 0 ).click();
  }

}
