﻿/**
 * Description: general javascript actions and functions for the news Module ProductCatalogue
 * User: antonio.r
 * Date: 18/Out/2011
 **/

/* todo completar descrição */
function onLoadCarousel() {

  $( '#myCarousel' ).jcarousel( {
    wrap: 'circular',
    scroll:1,
    start:$( "#myCarousel li>div#" + str_detailProductID ).parent().index() + 1
  } );

}

/* todo completar descrição */
function activateMachineHover() {
  $( "li.registerMachineItem" ).fadeTo( 0 , 0.3 ).css( "visibility" , "visible" );
  $( "li.registerMachineItem div.heading" ).hide();
  $( "li.registerMachineItem" ).hover(
      function () {
        $( this ).stop().fadeTo( 0 , 1 );
          $( "li.registerMachineItem div.heading" ).show();
      } ,
      function () {
        $( this ).stop().fadeTo( 0 , 0.3 );
          $( "li.registerMachineItem div.heading" ).hide();
      }
  );
}

/* todo completar descrição */
function onLoadBackgroundColorBlendChange() {
  $( ".blendList li" ).each( function(i) {
    if ( this.className != 'activeItem' ) {
      $( this ).hover( function() {
        $( this ).find( ".blendContent" ).css( {"background-color":"#" + arr_backgroundColorBlends[i]} )
      } , function() {
        $( this ).find( ".blendContent" ).css( {"background-color":"#000"} )
      } )
    }
  } );
}

/* todo completar descrição */
function displayBlock() {
  $( ".imgProduct img" ).removeClass( "displayNone" );
}

function activateVariant(int_variantIndex) {
  var str_variantImageSrc = arr_priceProduct[int_variantIndex].str_variantImage,
      str_variantImageSrcRaw = arr_priceProduct[int_variantIndex].str_variantImage,
      str_variantText = arr_priceProduct[int_variantIndex].str_variantName,
      str_variantAddToCartHref = arr_priceProduct[int_variantIndex].str_variantCartLink;

  str_variantImageSrc = "/admin/public/getimage.aspx?Image=" + str_variantImageSrc + "&AlternativeImage=/Files/Templates/Designs/DeltaQ/images/noImageBlend.png&Format=png&Height=300&Compression=80&Crop=1";
  if ( $( ".imgProduct img" ).attr( "src" ) != str_variantImageSrc ) {
    $( ".imgProduct img:last" ).addClass( "displayNone" );
    $( ".imgProduct img:last" ).attr( "src" , str_variantImageSrc );
    $( ".imgProduct link" ).attr( "href" , str_variantImageSrcRaw );
    FB.XFBML.parse();
  }

  if ( !$( "#variantColor" ).length ) $( 'h1' ).append( '<span id="variantColor"></span>' );
  $( "#variantColor" ).text( str_variantText );

  $( "div#cartProduct a" ).attr( "href" , str_variantAddToCartHref );

  if ( arr_priceProduct[int_variantIndex].str_variantPrice != '' )
    $( "div.priceSection span.price" ).text( arr_priceProduct[int_variantIndex].str_variantPrice );
}

/* function that verifies if that product is on Cart if so added custom style */
function onLoadActivateIfOrderLineHaveProduct(str_productId , variant) {

  $( ".arrowItemAdded" ).parent().removeClass( "displayBlock" );
  $( ".addToCart" ).removeClass( "added" );
  $( ".addToCart a" ).html( str_addToCartBeforeAdded );

  if ( typeof arr_orderLinesProductID != 'undefined' ) {
    for ( i = 0 ; i <= arr_orderLinesProductID.length - 1 ; i++ ) {
      if ( arr_orderLinesProductID[i] == str_productId + variant ) {
        $( ".arrowItemAdded" ).parent().addClass( "displayBlock" );
        $( ".addToCart" ).addClass( "added" );
        $( ".addToCart a" ).attr( "href" , "/Default.aspx?ID=" + int_pageIDShop );
        $( ".addToCart a" ).html( str_addToCartAfterAdded );
      }
    }
  }
}

/* todo completar descrição */
function onLoadProductMachine(str_productId) {

  if ( $.browser.msie && parseInt( $.browser.version , 10 ) == 7 ) {
    $( ".productMachineDescriptionArea" ).parents( ".paragraph" ).css( {"overflow":"visible"} )
  }

  if ( $( "ul#productVariants" ).length >= 1 ) {
    $( "ul#productVariants li" ).click( function() {
      activateVariant( $( "ul#productVariants li" ).index( this ) );
      onLoadActivateIfOrderLineHaveProduct( str_productId , arr_priceProduct[$( "ul#productVariants li" ).index( this )].str_variantId );
    /* helpdeskStart - nuno.b - 23/01/2012 @ 23:30 (Adicionar a possibilidade de termos diferentes variantes inactivas) #4963 */ 
      $( "div#cartProduct span").hide();
      $( "div#cartProduct span." + $(this).attr('class').substring(0,11)).show();
    /* helpdeskEnd */
    } );
  }

  $( "#mainContent" ).css( {background:'url("/Files/Templates/Designs/DeltaQ/images/borderBox.png") repeat scroll 0 0 transparent'} );

  if ( $( "ul#productVariants" ).length >= 1 ) {
    $( "ul#productVariants li:not(.outOfStock):first" ).trigger( "click" );
  }

  onLoadActivateIfOrderLineHaveProduct( str_productId , arr_priceProduct[0].str_variantId );
  onLoadActivateProductShareFacebook();
  onLoadProductMachineDetailMenu();
  $( ".imgProduct img" ).load( function() { displayBlock() } );

}

/* function that add mouseOver action on custom share button of facebook */
function onLoadActivateProductShareFacebook() {

  $( ".share" ).parents( "#body" ).append( $( ".share" ) );
  $( ".share" ).css( {"left":$( ".shareFacebook" ).offset().left + "px","top":$( ".shareFacebook" ).offset().top - 50 + "px"} )

  $( ".iconFB" ).hover( function() {
    if ( bol_animateBoxShare ) {
      $( ".share" ).stop().fadeIn( '2500' , function() {
        bol_animateBoxShare = false;
      } );
    }
  } )

  $( ".share" ).mouseleave( function() {
    if ( !bol_animateBoxShare ) {
      $( ".share" ).stop().fadeOut( '2500' , function() {
        setTimeout( function() {
          bol_animateBoxShare = true;
        } , 500 )
      } );
    }
  } )
}

/* todo completar descrição */
function onLoadActivateBlendProduct(productID , element) {
  $( "." + element + " a" ).each( function() {
    if ( $( this ).attr( "href" ).split( "&" )[1].split( "=" )[1] == productID ) {
      $( this ).parents( "li" ).addClass( "activeItem" );
    }
  } );
}

/* function that add the click action on <li> */
function onLoadBlendListClickAction(str_element) {

  if ( str_element == "machineContent" )
    bol_addBackgroundBorder = false;

  /* add click action on li */
  $( "." + str_element + " a" ).each( function() {
    $( this ).parents( "li" ).click( function() {
      window.location.href = $( this ).find( "a" ).attr( "href" );
    } );
  } );

}

/* todo completar descrição */
function onLoadMachineRegister() {

  var int_totalLiWidth = 0;

  $("div#registerMachineList").parents("div.paragraph").css("padding-bottom",0);
  $( "li.registerMachineItem" ).each( function(index) {
    int_totalLiWidth += parseInt( $( this ).outerWidth( true ) , 10 );
  } );
  $( "ul.machineRegisterList" ).css( "width" , (int_totalLiWidth+10) + "px" );

  activateMachineHover();
  $( "li.registerMachineItem" ).click( function() {
    activateMachineHover();
    $( this ).fadeTo( 0 , 1 ).unbind( 'mouseenter mouseleave' );

    $( "div#variantSelector" ).html( "" );
    $( this ).find( "ul.variants" ).clone().appendTo( "div#variantSelector" );
  } );

  $('.machineRegisterList li:eq(0)').click();
  $('#machinePROD718 div.imgMachine img').attr('src','/admin/public/getimage.aspx?Image=/Files/Billeder/DeltaQ2011/Maquinas/Cool11/QOOL_BRANCA_HR_CMYK_315Media.png&AlternativeImage=/Files/Templates/Designs/DeltaQ/images/blendNoImage.png&Format=png&Width=68&Compression=80&Crop=1');

}

/* function that add action at menu on the detail of the machine */
function onLoadProductMachineDetailMenu() {
  $( ".productMachineDetail li a" ).click( function() {

    $( ".productMachineDetail li" ).removeClass();
    $( this ).parent().addClass( "activeItem" );

    if ( $( this ).parent().index() == 1 ) {

      $( ".productMachineDescription" ).css( {"display":"none"} );
      $( ".productMachineCharacteristics" ).css( {"display":"block"} );

    } else if ( $( this ).parent().index() == 0 ) {

      $( ".productMachineDescription" ).css( {"display":"block"} );
      $( ".productMachineCharacteristics" ).css( {"display":"none"} );

    }
  } )
}

/* todo completar descrição */
/* helpdeskStart - nuno.b - 30/01/2012 @ 17:30 (The "str_lang", is used to check if the current language is 'fr') #4779 */
function updateRegisterMachineValue(str_machineNameAndModel , str_machineImageSrc , str_machineId, str_lang) {
  if (str_lang != 'fr') {
    $( "#DMForms_RegistoMaquina_Modelo_da_maquina" ).val( str_machineNameAndModel );
    $('#'+str_machineId + ' div.imgMachine img').attr('src','/admin/public/getimage.aspx?Image=' + str_machineImageSrc + '&AlternativeImage=/Files/Templates/Designs/DeltaQ/images/blendNoImage.png&Format=png&Width=68&Compression=80&Crop=1');
    if ( str_machineNameAndModel.search( "alQimia" ) != -1 || str_machineNameAndModel.search( "Qosmo" ) != -1 || str_machineNameAndModel.search( "Qool 1.1" ) != -1 ) {
      $( "input#DMForms_RegistoMaquina_Voucher" ).parents( "tr" ).show();
    }
    else {
      $( "input#DMForms_RegistoMaquina_Voucher" ).parents( "tr" ).hide();
    }
    $("form.registerMachine").css({'visibility':'visible'});
  }
  else if (str_lang == 'fr') {
    $( "#DMForms_RegistoMaquina_FR_ModeleDeLaMachine" ).val( str_machineNameAndModel );
    $('#'+str_machineId + ' div.imgMachine img').attr('src','/admin/public/getimage.aspx?Image=' + str_machineImageSrc + '&AlternativeImage=/Files/Templates/Designs/DeltaQ/images/blendNoImage.png&Format=png&Width=68&Compression=80&Crop=1');
    if ( str_machineNameAndModel.search( "alQimia" ) != -1 || str_machineNameAndModel.search( "Qosmo" ) != -1 || str_machineNameAndModel.search( "Qool 1.1" ) != -1 ) {
      $( "input#DMForms_RegistoMaquina_FR_Voucher" ).parents( "tr" ).show();
    }
    else {
      $( "input#DMForms_RegistoMaquina_FR_Voucher" ).parents( "tr" ).hide();
    }
    $("form.registerMachine").css({'visibility':'visible'});
  }
}
/* helpdeskEnd */

/* todo completar descrição */
function onLoadGetMachineShopListHeights() {
  var num_liIndex = 1,
      num_maxHeight = 0;

  $( '.machineShopList li' ).each( function(i) {
    if ( i <= 2 ) {
      $( this ).find( 'div.itemCorner.topLeft' ).remove();
      $( this ).find( 'div.itemCorner.topRight' ).css( {'top':'-5px'} );
    }
    if ( num_liIndex < 3 ) {
      if ( num_maxHeight < $( this ).height() ) {
        num_maxHeight = $( this ).height();
      }
      num_liIndex++;
    }
    else {
      if ( num_maxHeight < $( this ).height() ) {
        num_maxHeight = $( this ).height();
      }
      updateMachineShopListHeights( i - 2 , i , num_maxHeight );
      num_liIndex = 1;
      num_maxHeight = 0;
    }
  } )
}

/* todo completar descrição */
function updateMachineShopListHeights(num_firstIndex , num_lastIndex , num_maxHeightLine) {
  $( '.machineShopList li' ).each( function(i) {
    if ( i <= num_lastIndex && i >= num_firstIndex ) {
      $( this ).css( {'height':num_maxHeightLine} );
      onLoadsetDimentions();
    }
  } );
}

function onLoadsetDimentions() {
  $( ".titlePages" ).css( {"width":"650px","margin-right":"5px"} );
  $( "#mainContent" ).css( {"position":"relative"} );
  $( ".secondColumnContent" ).css( {"position":"absolute","top":"5px","right":"5px","height":$( "#mainContent" ).height() + "px"} );
  $( ".secondColumnContent .paragraph" ).css( {"height":$( "#mainContent" ).height() - 44 + "px"} );
}
