/**
 * Class handlings affiliates programs
 * Detects it and stores in cookies
 * If details page detected special code is appended to sms code
 */

function isCookieEnabled(){
  document.cookie = "CookieTest=Enabled";
  var allcookies = document.cookie;
  var pos = allcookies.indexOf("CookieTest=");
  if (pos != -1) {
    var start = pos + 11;
    var end = allcookies.indexOf(";", start);
    if (end == -1) end = allcookies.length;
    var value = allcookies.substring(start,end);
    value = unescape(value);

    if (value == "Enabled") {
      return true;
    } else {
      return false;
    }
  }
  return false;
}


function MyAffiliates(){

  //affiliates list
  var affiliates = {
    afilo : {
      pattern : /,AFID,CD(.{1,4})$/,
      smscode : 'p'
    }
  }

  //current affiliate
  var currAffiliate = null;
  //redirect address
  var redirect = null;

  /**
   * main function
   **/
  this.affiliate = function(){

    if (checkAffiliateCode()){
      saveCode();
      redirectAffiliate();
    }

    //add suffix to sms code
    if (getCode()){
      hideNonAffil();
      if (isProductPage()){
        addCode();
      }
    }
  }

  /**
   * chack for affiliate code in address
   */
  function checkAffiliateCode(){
    for (var key in affiliates) {
      var pattern = affiliates[key].pattern;
      var character = location.href.search(pattern);
      if (character > 0){
        currAffiliate = {
          code: location.href.match(pattern)[1],
          name: key
        }
        redirect = location.href.replace(pattern,'');
        return true;
      }
    }
    return false;
  }

  /**
   * save affiliate to cookie
   */
  function saveCode(){
    if (isCookieEnabled()){
      //remove old one
      $.Jookie.Initialise('myAffiliates',60*24*30);
      $.Jookie.Unset('myAffiliates','affiliate');
      $.Jookie.Initialise('myAffiliates',60*24*30);
      $.Jookie.Set('myAffiliates', 'affiliate', currAffiliate);
    }
  }

  /**
   *  redirect to plain page
   */
  function redirectAffiliate(){
    window.location.assign(redirect);
  }

  /**
   * Get affiliate from cookie
   */
  function getCode(){
    if (isCookieEnabled()){
      $.Jookie.Initialise('myAffiliates',60*24*30);
      currAffiliate = $.Jookie.Get('myAffiliates', 'affiliate');
      return currAffiliate;
    }
    return false;
  }

  /**
   * Check if on product page
   */
  function isProductPage(){
    return ($('.details').length)?true:false;
  }

  /**
   * Hide elements that should be hidden on affiliate pages (redirections to external pages with premium content)
   */
  function hideNonAffil(){
    $('.nonAffil').hide();
  }


  /**
   * Append code on details pages
   */
  function addCode(){
    var prepend = '';
    if (currAffiliate.code.length==3) prepend = '0';
    if (currAffiliate.code.length==2) prepend = '00';
    if (currAffiliate.code.length==1) prepend = '000';

    var code = affiliates[currAffiliate.name].smscode + prepend + currAffiliate.code;

    $('.smsCode').append(code);

    //also changes link to fotocode
    $('#fotokod').attr('src',$('#fotokod').attr('src').replace('&choe=UTF-8','%2Caf%2C' + code + '&choe=UTF-8'));

  }

}

/**
 * It was to complicated to parametrize above function (probably not necessary soon
 * So below copy-paste
 */
function MyAffiliates2(){

  //affiliates list
  var affiliates = {
    afilo : {
      pattern : /,AFID,T5$/,
      prefix : 'AAT5'
    }
  }

  //current affiliate
  var currAffiliate = null;
  //redirect address
  var redirect = null;

  /**
   * main function
   **/
  this.affiliate = function(){

    if (checkAffiliateCode()){
      saveCode();
      redirectAffiliate();
    }

    //add suffix to sms code
    if (getCode()){
      if (isProductPage()){
        addCode();
      }
    }
  }


  /**
   * chack for affiliate code in address
   */
  function checkAffiliateCode(){
    for (var key in affiliates) {
      var pattern = affiliates[key].pattern;
      var character = location.href.search(pattern);
      if (character > 0){
        currAffiliate = {
          name: key
        }
        redirect = location.href.replace(pattern,'');
        return true;
      }
    }
    return false;
  }

  /**
   * save affiliate to cookie
   */
  function saveCode(){
    if (isCookieEnabled()){
      //remove old one
      $.Jookie.Initialise('myAffiliates2',60*24*1);
      $.Jookie.Unset('myAffiliates2','affiliate');
      $.Jookie.Initialise('myAffiliates2',60*24*1);
      $.Jookie.Set('myAffiliates2', 'affiliate', currAffiliate);
    }
  }

  /**
   *  redirect to plain page
   */
  function redirectAffiliate(){
    window.location.assign(redirect);
  }

  /**
   * Get affiliate from cookie
   */
  function getCode(){
    if (isCookieEnabled()){
      $.Jookie.Initialise('myAffiliates2',60*24*1);
      currAffiliate = $.Jookie.Get('myAffiliates2', 'affiliate');
      return currAffiliate;
    }
    return false;
  }

  /**
   * Check if on product page
   */
  function isProductPage(){
    return ($('.details').length)?true:false;
  }


  /**
   * Append code on details pages
   */
  function addCode(){

    function callback(indexInArray, valueOfElement) {
       var booleanKeepGoing;
       var oldCode = valueOfElement.innerText;
       oldCode = oldCode.substr(4, oldCode.length);
       valueOfElement.innerText = affiliates[currAffiliate.name].prefix + oldCode;
       return booleanKeepGoing; // optional, unless false
                                // and want to stop looping
     }

    $('.smsCode').each(callback);
    
    //also changes link to fotocode
    //$('#fotokod').attr('src',$('#fotokod').attr('src').replace('&choe=UTF-8','%2Caf%2C' + code + '&choe=UTF-8'));

  }

}