﻿// Delta Q 2010 - Javascript
 
var BodyWidth = 0;
var BgFrame = "0";
var ContentWidth="";
var ContentHeight = "";
var ContentXpos = "116";
var finalContentXpos = "116";
var ContentYpos = "0";
 
var CookieName = "DeltaQ_ActiveCapsule";
var CookieOptions = { path: '/', expires: 365 };
var ActiveCapsule =  $.cookie(CookieName);
checkCookie();
 
var ParamIndex = $.getURLParam("BlendLink");
var BlendSlideShowIndex = 0;
if (ParamIndex != null) ParamIndex = parseInt(ParamIndex);
BlendSlideShowIndex = (ParamIndex != null) ? ParamIndex : ActiveCapsule;
BlendSlideShowIndex++;
 
var resizeTimer = null;
$(window).bind("resize", function() {
  if (resizeTimer) clearTimeout(resizeTimer);
  resizeTimer = setTimeout(LoadBgAnimation, 1000);
});


function OnLoadActions(TopPageID) {
  /* helpdeskStart - nuno.b - 25/07/2011 @ 11:40 (Forçar a carregar o fundo amarelo) */
  if (TopPageID == 487) {
    ActiveCapsule = 11;
    BlendSlideShowIndex = 11;
  }
  
   /* helpdeskEnd **/
  // Check Flash BgFrame
  if (TopPageID == 484 || TopPageID == 658 || TopPageID == 746 || TopPageID == 832 || TopPageID == 927 || TopPageID == 1467 || TopPageID == 1601 || TopPageID == 927) { BgFrame = 2; ContentYpos=-16; }
  else if (TopPageID == 485 || TopPageID == 627 || TopPageID == 715 || TopPageID == 804 || TopPageID == 903) {
    BgFrame = 0;
//    $('#DwContentCell').css('background-image','url(Files/System/DeltaQ_v2/layout/fumo' + ActiveCapsule + '.jpg)');
  }
  else if (TopPageID == 486 || TopPageID == 631 || TopPageID == 719 || TopPageID == 808 || TopPageID == 907) {
    BgFrame = 0;
//    $('#DwContentCell').css('background-image','url(Files/System/DeltaQ_v2/layout/fumo' + ActiveCapsule + '.jpg)');
  }
  else if (TopPageID == 487 || TopPageID == 647 || TopPageID == 735 || TopPageID == 824 || TopPageID == 919) {
    BgFrame = 1;
    ContentWidth="739";
    ContentHeight="307";
    ContentXpos="93";
    ContentYpos="117";
  }
  else if (TopPageID == 488 || TopPageID == 659 || TopPageID == 747 || TopPageID == 833 || TopPageID == 928) {
    BgFrame = 1;
    ContentWidth="639";
    ContentHeight="427";
    ContentXpos="166";
    ContentYpos="45";
  }
  else if (TopPageID == 489 || TopPageID == 660 || TopPageID == 748 || TopPageID == 834 || TopPageID == 929) {
    BgFrame = 0;
//    $('#DwContentCell').css('background-image','url(Files/System/DeltaQ_v2/layout/fumo' + ActiveCapsule + '.jpg)');
  }
  else { BgFrame = 0 }
  
  // Load Color Settings
  LoadNewColor();
  
  // Homepage Highlights
  $('.Homepage .HighlightHomepage .ParagraphContent a').wrap('<div class="LinkHolder"></div>');
  $('.Homepage .HighlightHomepage .ParagraphContent .LinkHolder').parent().append('<div class="clear"></div>');
  
  // Blends
  $('.BlendSlideShow .psParagraphsHolder .Paragraph').each(function() {
    BgImg = $("#" + $(this).attr('id') + " .BlendIntensityValue").attr('rel');
    if (BgImg != undefined) {
      $("#" + $(this).attr('id') + " .BlendIntensityValue").css('background-image', 'url("Files/System/DeltaQ_v2/blends/BlendIntensityBg' + BgImg + '.gif")');
    }
  });
  
  // Machines
  UpdateMachineName('.MachinesSlideShow .psParagraphsHolder .MachineName');
  UpdateMachineName('.MachinesList .ProductInfo .h2');
  UpdateMachineName('.ProductFactSheet .ProductName .h1');
  
  // Paragraph Setup
  if (TopPageID == 487 || TopPageID == 647 || TopPageID == 735 || TopPageID == 824 || TopPageID == 919 || TopPageID == 1472 || TopPageID == 1604) { /* helpdesk - nuno.b - 08/06/2011 @ 15:30 (adicionar TopPageID da loja FR-PT '1472' e FR-EN '1604' )*/
    /* helpdeskStart - nuno.b - 25/07/2011 @ 11:30 (Carregar o powerCoffee na página blends)*/
    /*if (TopPageID == 487) {
      $('.psParagraphsHolder .Paragraph:last').show("slow");
    }
    / * helpdeskEnd * /
    if (TopPageID != 487) {*/
      if (ParamIndex!=null) { $('.psParagraphsHolder .Paragraph:eq(' + (ParamIndex) + ')').show("slow"); }
      else { $('.psParagraphsHolder .Paragraph:eq(' + (ActiveCapsule) + ')').show("slow"); }
    //}
  }
  else {
    $('.psParagraphsHolder .Paragraph:first').show("slow");
  }
  
  // Forms Submit Manipulation
  $("form[name^='postform'] input[type='button'], form[name^='Newsletter'] input[type='button']").each(function() {
    OnClickValue = $(this).attr("onclick");
    ButtonValue = $(this).attr("value");
    NewButton = $('<div style="float:right;"></div>').append($('<div class="ProductButton"></div>').append($('<a>' + ButtonValue + '</a>').attr('href','javascript:;').bind('click',OnClickValue)));
    $(this).replaceWith(NewButton);
  });
  
  // IE PNG fix
  $('.Homepage .HighlightHomepage .ParagraphContent .LinkHolder, a, img, div.CartGraphic, #Pen').pngfix();
  
  CheckCampaigns();
  CheckAdSalesCampaigns();
  if($.browser.msie) {
    $("#TopLogo").css("z-index","80");
  }
  /* helpdeskStart */
    /*$('#DwContentCell').addClass('DwContentCellCorrection');*/
  /* helpdeskEnd */
}
 
 
 
//  Menu functions
function showBlend (destinationURL, BlendLink) {
  window.location = "Default.aspx?ID=" + destinationURL + "&BlendLink=" + BlendLink;
}
 
function setColor (ColorIndex) {
  $.cookie(CookieName, ColorIndex, CookieOptions);
  window.location = window.location;
}
 
 
 
//  Activate Blend function
function ActivateBlend(BlendID) {
  ActivateElement(BlendID);
}
 
 
 
//  Machine functions
function ActivateMachine(MachineID) {
  ActivateElement(MachineID);
}
 
function MachineTextToogle(selector, ProdID) {
  if ($("#Machine" + ProdID + " ." + selector).css('display') == 'none') {
    $("#Machine" + ProdID + " .MachineOptionsHolder div").hide("slow", function() {
      $("#Machine" + ProdID + " ." + selector).show("slow");
      $("#Machine" + ProdID + " li a").removeClass("activeItem");
      $("#Machine" + ProdID + " li[rel='" + selector + "'] a").addClass("activeItem");
    });
  }
/* Helpdesk start - toogle buttons - by nuno.b @ 25/05/2011 @ 17:50 */
  if (selector == 'MachineDescription') {
    $('.MachineExtras').show("slow");
  }
  if (selector == 'MachineCharacteristics') {
    $('.MachineExtras').hide();
  }
/* Helpdesk end - toogle buttons */
}
 
function UpdateMachineName(selector) {
  $(selector).each(function() {
    TheMachineName = $(this).text();
    if (TheMachineName.search(/-/) != -1) {
      NewName = TheMachineName.substring(0,TheMachineName.search(/-/)-1) + "<span>" + TheMachineName.substring(TheMachineName.search(/-/)+2, TheMachineName.length) + "<\/span>";
      $(this).html(NewName);
    }
  });
}
 
 
 
// Activate Element
function ActivateElement(ElementID) {
  if ($('#Paragraph' + ElementID).css('display') != 'block') {
    $('.psParagraphsHolder .Paragraph').each(function() {
      if ($(this).css('display') != 'none' && $(this).attr('id') != ("Paragraph" + ElementID)) {
        $(this).hide();
      }
    });
    $('#Paragraph' + ElementID).show();
  }
}
 
function LoadNewColor() {
  var CssFile = "";
  if (BgFrame == undefined) { BgFrame = '0' };
  switch(ActiveCapsule) {
    case '1':
      CssFile = "Decafeinatus.css";
      break;
    case '2':
      CssFile = "Deliqatus.css";
      break;
    case '3':
      CssFile = "Qonvivium.css";
      break;
    case '4':
      CssFile = "Qonvictus.css";
      break;
    case '5':
      CssFile = "aQtivus.css";
      break;
    case '6':
       CssFile = "Qharacter.css";
      break;
    case '7':
      CssFile = "Qalidus.css";
      break;
  }
  checkLoadedExtFile(CssFile,'css');
  LoadBgAnimation();
}
 
function LoadBgAnimation() {
  // Update ContentXpos
  finalContentXpos = ContentXpos;
  BodyWidth = parseInt($("body").width());
  if (BodyWidth < 1200) finalContentXpos = ContentXpos - parseInt((1200 - BodyWidth)/2);
  if (finalContentXpos < 16) finalContentXpos = 16;
  if (ContentYpos == -16) {
    ContentYpos = 0;
    $('#BackgroundDeltaQ').css("top", "133px");
  }
  $('#BackgroundDeltaQ').html('');
  $('#BackgroundDeltaQ').flash(
    { 
      src: 'Files/System/DeltaQ_v2/flash/background.swf',
      width: 1200,
      height: 500,
      flashvars: { contentType:BgFrame, color:ActiveCapsule, contentYpos: ContentYpos, contentXpos: finalContentXpos, contentWidth: ContentWidth, contentHeight: ContentHeight }
    },
    { version: 8 }
  );
}
 
function MyLoadFlash(myFilePath, myWidth, myHeight, myID, FlashVars) {
  if (FlashVars == undefined) { FlashVars = ""; }
  myFilePath = myFilePath.substring(0,myFilePath.length-4);
  AC_FL_RunContent(
    'codebase', 'https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0',
    'width', myWidth,
    'height', myHeight,
    'src', myFilePath,
    'quality', 'high',
    'pluginspage', 'https://www.adobe.com/go/getflashplayer',
    'align', 'middle',
    'play', 'true',
    'loop', 'true',
    'scale', 'showall',
    'wmode', 'transparent',
    'devicefont', 'false',
    'id', myID,
    'FlashVars', FlashVars,
    'bgcolor', '#000000',
    'name', myID,
    'menu', 'true',
    'allowFullScreen', 'false',
    'allowScriptAccess','sameDomain',
    'movie', myFilePath,
    'salign', 'tl'
  ); //end AC code
}
 
 
 
// Cookie functions
function checkCookie() {
  if (ActiveCapsule == '' || ActiveCapsule == undefined || ActiveCapsule == null) ActiveCapsule = '1';
  $.cookie(CookieName, ActiveCapsule, CookieOptions);
}
 
 
 
// Form functions
function ClearField(field, standardValue) {
  if ($('input[name=' + field + ']').val() == standardValue) {
    $('input[name=' + field + ']').val("");
  }
}
 
function CheckField(field, standardValue) {
  if ($('input[name=' + field + ']').val() == "") {
    $('input[name=' + field + ']').val(standardValue);
  }
}
 
 
 
// QuickSearch String
function CheckSearchString(standardValue, errorMessage) {
  CurrentSearchValue = $("input[name='q']").val();
  if (CurrentSearchValue == standardValue || CurrentSearchValue == "") {
    alert(errorMessage);
    return false;
  }
  else {
    return true;
  }
}
 
 
 
// Login functions
function CheckLogin(standardValue, errorMessage) {
  if ($('input[name="username"]').val() == standardValue || $('input[name="username"]').val() == '' || $('input[name="password"]').val() == '') {
    alert(errorMessage);
    return false;
  }
  else {
    return true;
  }
}
 
function CheckLoginHeader(errorMessage) {
  if ($('input[name="HeaderUsername"]').val() == '' || $('input[name="HeaderPassword"]').val() == '') {
    alert(errorMessage);
  }
  else {
    $('input[name="username"]').val($('input[name="HeaderUsername"]').val());
    $('input[name="password"]').val($('input[name="HeaderPassword"]').val());
    document.ExtFooterForm.submit();
  }
}
 
function CheckLoginDwContent(errorMessage) {
  if ($('form[name="ExtUserForm_Main"] input[name="Username"]').val() == '' || ($("form[name='ExtUserForm_Main'] input[name='Password']").val() == '' && !document.ExtUserForm_Main.ForgotPassword.checked) || (document.ExtUserForm_Main.ForgotPassword.checked && !validateEmail($('form[name="ExtUserForm_Main"] input[name="Username"]').val()))) {
    alert(errorMessage);
    return false;
  }
  else {
    return true;
  }
}
 
function CheckLoginDwContent2(errorMessage) {
  if ($('form[name="ExtUserForm_Main"] input[name="Username"]').val() == '' || ($("form[name='ExtUserForm_Main'] input[name='Password']").val() == '' && !document.ExtUserForm_Main.ForgotPassword.checked) || (document.ExtUserForm_Main.ForgotPassword.checked && !validateEmail($('form[name="ExtUserForm_Main"] input[name="Username"]').val()))) {
    alert(errorMessage);
  }
  else {
    document.ExtUserForm_Main.submit();
  }
}
 
 
 
// ParagraphSetup functions
function CheckPsMinHeight(selector) {
  $(function() {
    MinHeight = 410;
    
    $(selector).css('height', 'auto');
    NewHeight = parseInt($(selector).height());
    if (NewHeight < MinHeight)
      $(selector).css('height', MinHeight);
    else
      $(selector).css('height', NewHeight);
  });
}
 
function updateContentHeight(selector) {
  $(function() {
    MinHeight = 462;
    
    //$(selector).css('height', 'auto');
    NewHeight = parseInt($(selector).height());
    if (NewHeight < MinHeight)
      $(selector).css('height', MinHeight);
    else
      $(selector).css('height', NewHeight);
  });
}
 
 
 
// Cart functions
function CheckCartMinHeight() {
  MinHeight = parseInt($('.Cart').height());
  CartNotesHeight = 0;
  if ($('.CartNotes').length > 0) CartNotesHeight = parseInt($('.CartNotes').height());
  
  $('.Cart').css('height', 'auto');
  NewHeight = parseInt($('.Cart').height()) + CartNotesHeight + 80;
  if (NewHeight < MinHeight)
    $('.Cart').css('height', MinHeight);
  else
    $('.Cart').css('height', NewHeight);
}
 
function slideToggle(selector) {
  $(selector).slideToggle("slow", function() {
    if ($(selector).css('display') == 'none') {
      $(selector + "Link a").css('background-image','url(Files/System/DeltaQ_v2/graphics/GreyArrowLeft.gif)');
    }
    else {
      $(selector + "Link a").css('background-image','url(Files/System/DeltaQ_v2/graphics/GreyArrowDown.gif)');
      if ($('.DeliveryAddress .newListSelected').length == 0) {
        $('.DeliveryAddress select').sSelect({ddMaxHeight: '96px'}).change(function(){
          TheValue = $(this).getSetSSValue();
        });
      }
    }
    CheckCartMinHeight();
  });
}
 
function ClosePopUp() {
  $('.ErrorPopUp').addClass('invisible');
  $('.ErrorMsgs').addClass('invisible');
}
 
function SetSelectedLanguage(CustomerCountry, DeliveryCountry) {
  var opt = document.submitUserData.EcomOrderCustomerCountry.options;
  
  for (var i=1;i<opt.length;i++) {
    if (opt[i].value == CustomerCountry) {
      opt[i].selected = true;
    }
  }
  
  opt = document.submitUserData.EcomOrderDeliveryCountry.options;
  for (var i=1;i<opt.length;i++) {
    if (opt[i].value == DeliveryCountry) {
      opt[i].selected = true;
    }
  }
}
 
function CopyFields(OriginField, DestinationField) {
  if ($('.DeliveryAddress').css('display') == 'none') {
    $("#" +  DestinationField).val($("#" + OriginField).val());
  }
}
 
function CheckDeliveryAddress(FieldA1, FieldA2, FieldB1, FieldB2, FieldC1, FieldC2, FieldD1, FieldD2, FieldE1, FieldE2) {
  if ((FieldA1 != FieldA2 && FieldA2 != '') || (FieldB1 != FieldB2 && FieldB2 != '') || (FieldC1 != FieldC2 && FieldC2 != '') || (FieldD1 != FieldD2 && FieldD2 != '') || (FieldE1 != FieldE2 && FieldE2 != '')) {
    slideToggle(".DeliveryAddress");
  }
}
 
function AcceptOrder() {
  submit = true;
  
  if (document.terms.acceptTerms.checked != true) {
    $(".ErrorPopUp").css('display', 'block');
    submit = false;
  } 
  
  if (submit) {
    document.terms.submit(); 
  }  
}
 
 
 
function CheckCampaigns() {
  var campaignCookie = '',
      currentPathName = location.pathname,
      availableCampaigns = [""];
  
  for (i=0;i<availableCampaigns.length;i++) {
    if (currentPathName == ("/"+availableCampaigns[i])) {
      campaignCookie = currentPathName;
      break;
    }
  }
  
  if (campaignCookie != '') {
    $.cookie("DeltaQCampaigns", campaignCookie, { path: '/', expires: 30 });
  }
}
 
function CheckAdSalesCampaigns() {
  var seopid = $.getURLParam("seopid");
  if (seopid != null && seopid != undefined) {
    $.cookie("seopid", seopid, { path: '/', expires: 30 });
    //$("body").append('<iframe width="0" height="0" frameborder="0" id="seoclick1" src="http://work.salesengineonline.com/work/seoworkclick1.php?seopid=' + seopid + '"></iframe>');
    $("body").append('<iframe src="" width="0" height="0" frameborder="0" id="seoclick1"></iframe><script src="http://work.salesengineonline.com/work/seolclick1.php" type="text/javascript"></script><script src="http://work.salesengineonline.com/work/seolclick1.js" type="text/javascript"></script>');
  }
}
 
 
 
$.fn.wrapChildren = function(options) {
  
    var options = $.extend({
                              childElem : undefined,
                              sets : 1,
                              wrapper : 'div'
                            }, options || {});
    if (options.childElem === undefined) return this;
    
  return this.each(function() {
    var elems = $(this).children(options.childElem);
    var arr = [];
    
    elems.each(function(i,value) {
      arr.push(value);
      if (((i + 1) % options.sets === 0) || (i === elems.length -1))
      {
        var set = $(arr);
        arr = [];
        set.wrapAll(document.createElement(options.wrapper));
      }
    });
    });
}
function criarIpaper(numero) {
  showIpaper(numero);
  setTimeout("$('#popupContent" + numero + "').css('z-index', '99999');",500);
}

