/**
 * Javascript skin configurator library
 *
 * LICENSE: This code is licenced to skinflips.com
 *
 * @package    DACH_SHOP
 * @subpackage PHP_WEBSITE 
 * @copyright  2010 Tortoise Design GmbH
 * @license    http://www.skinflips.com
 * @author     Igor Vuckovic <igor.vuckovic@softserbia.com>
 * @version    $Id: jsConfiguratorLib.js 2685 2010-07-26 11:26:13Z ivana $
 * @link       http://www.skinflips.com
 * @since      File available since Release 0
*/

/**
 * object: Javascript skin configurator library
 *
 * This object contains functions that can be used on create page
 *
 * notice: prototype javascript framework needed          
 *
 * @author Igor Vuckovic <igor.vuckovic@softserbia.com>
 * @param  void
 * @return void
*/

var jsConfiguratorLib = {

  measured_device_own:      null,
  measured_device_10:       null,
  measured_device_13:       null,
  measured_device_14:       null,
  measured_device_15:       null,
  measured_device_15_4:     null,
  measured_device_15_6:     null,
  measured_device_16:       null,
  measured_device_17:       null,

  measured_device_own_max_width:  null,
  measured_device_own_max_height: null,
  errorMessage: null,

  showDeviceSelector: false,
  exitWarningMessage: false,
  
  // blink text on the DIY page
  blinkTextCounter: 0,
  blinkTimeOut: 1000,
  blinkClearTimeOut: null,

  /**
   * function: This function shows cfd-devices-container
   *           whit activ device categories   
   * 
   * @author Ivana Djordjevic <ivana.djordjevic@softserbia.com>
   * @param  {Object} object // Html <a> element
   * @return void  
  */
  selectDeviceCategories: function(object) {
    
    if (this.showDeviceSelector) {
      var url = jsCommonLib.siteUrl + '/cfd-devices/index/';
    
      // show container
      $('cfd-devices-container').setStyle({
        display: 'block'
      });
      
      // params
      var params = '';
      var ajaxRequest = '';
      // Another click on SELECT DEVICE shall close the device menu as well)
      if ($('show-device') == null) {
        ajaxRequest = new Ajax.Updater({success: 'cfd-devices-container'}, url,{method: 'get', parameters: params});
      } else if (object.id != 'select-device-link') {
        ajaxRequest = new Ajax.Updater({success: 'cfd-devices-container'}, url,{method: 'get', parameters: params});
      } else {
        jsConfiguratorLib.closeCfdDevicesContainer();
      }
    }
  },

  /**
   * function: This function set showDeviceSelector to true value
   *           it executes in the flash configurator
   *
   * @author Ivana Djordjevic <ivana.djordjevic@softserbia.com>
   * @return void
  */
  setDeviceSelector: function() {
      this.showDeviceSelector = true;
  },

  /**
   * function: This function set exitWarningMessage to true value
   *           it executes in the flash configurator
   *
   * @author Ivana Djordjevic <ivana.djordjevic@softserbia.com>
   * @return void
  */
  setExitWarningMessage: function() {
      this.exitWarningMessage = true;
  },

  /**
   * function: This function hides cfd-devices-container
   * 
   * @author Ivana Djordjevic <ivana.djordjevic@softserbia.com>
   * @return void  
  */
  closeCfdDevicesContainer: function() {

    $('cfd-devices-container').innerHTML = '';

    $('cfd-devices-container').setStyle({
      display: 'none'
    });

  },

  /**
   * function: This function updates device box with devices manufacturers
   * 
   * @author Ivana Djordjevic <ivana.djordjevic@softserbia.com>
   * @param  {Object} object // Html <a> element
   * @return void  
  */
  selectManufacturers: function(deviceCategory) {
    var url = jsCommonLib.siteUrl + '/cfd-devices/select-manufacturers/';
    
    // params
    var params  = '?device-category=' + deviceCategory;

    var ajax = new Ajax.Updater({success: 'cfd-devices-container'}, url,{method: 'get', parameters: params});
    
  },

  /**
   * function: This function updates device box with devices from selected manufacturer
   * 
   * @author Ivana Djordjevic <ivana.djordjevic@softserbia.com>
   * @param  {Object} object // Html <a> element
   * @return void  
  */
  devicesByManufacturer: function(manufacturerId) {
    var url = jsCommonLib.siteUrl + '/cfd-devices/devices-by-manufacturer/';
    
    // params
    var params  = '?manufacturer-id=' + manufacturerId;

    var ajax = new Ajax.Updater({success: 'cfd-devices-container'}, url,{method: 'get', parameters: params});
  },

  /**
   * function: This function updates device box with devices from selected manufacturer
   * 
   * @author Ivana Djordjevic <ivana.djordjevic@softserbia.com>
   * @param  {Object} object // Html <a> element
   * @return void  
  */
  laptopSize: function(manufacturerId) {
    var url = jsCommonLib.siteUrl + '/cfd-devices/laptop-size/manufacturer_id/' + manufacturerId + '/';
    var ajax = new Ajax.Updater({success: 'cfd-devices-container'}, url,{method: 'get', parameters: ''});
  },
  

  /**
   * function: It returns flash object
   * 
   * @author Igor Vuckovic <igor.vuckovic@softserbia.com>
   * @param  {String} movieName // flash name
   * @return void  
  */
  getFlashMovieObject: function(movieName) {
    
    if (window.document[movieName]) {
      return window.document[movieName];
    }
    
    if (navigator.appName.indexOf("Microsoft Internet")==-1) {
      if (document.embeds && document.embeds[movieName])
        return document.embeds[movieName]; 
    
    // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
    } else {
      return document.getElementById(movieName);
    }
  },

  /**
   * function: It triggers flash configurator
   * 
   * @author Igor Vuckovic <igor.vuckovic@softserbia.com>
   * @param  {String} value // flash name
   * @return void  
  */  
  changeFlashDevice: function(value){

    var flashMovie = jsConfiguratorLib.getFlashMovieObject("flashContent");
    flashMovie.SetVariable("externalCall", value);
  }, 

  /**
   * function: select selfmeasured for laptops
   * 
   * @author Ivana Djordjevic <ivana.djordjevic@softserbia.com>
   * @param  {String} measureRadio // checked measure radio
   * @param  {String} width // laptop width
   * @param  {String} height // laptop height
   * @return string  
  */
  selectSelfMeasured: function(measureRadio, width, height) {

    // Customsize should have a maximum size
    if ((width > this.measured_device_own_max_width) || (height > this.measured_device_own_max_height)) {
        $('measurements-error-message').innerHTML = this.errorMessage;
        return;

    // Do not permit in Configurator device selector to confirm own measurement if no sizes are entered by user
    } else if (((width * 1) != width) || ((height * 1) != height)) {
        $('measurements-error-message').innerHTML = this.errorMessage;
        return;
    } else {

      // Choose device id
      if (measureRadio == 'own') {
        deviceId = this.measured_device_own;
      } else if (measureRadio == '1')  {
        deviceId = this.measured_device_10;    
      } else if (measureRadio == '2')  {
        deviceId = this.measured_device_13;    
      } else if (measureRadio == '3')  {
        deviceId = this.measured_device_14;    
      } else if (measureRadio == '4')  {
        deviceId = this.measured_device_15;    
      } else if (measureRadio == '5')  {
        deviceId = this.measured_device_15_4;    
      } else if (measureRadio == '6')  {
        deviceId = this.measured_device_15_6;    
      } else if (measureRadio == '7')  {
        deviceId = this.measured_device_16;    
      } else if (measureRadio == '8')  {
        deviceId = this.measured_device_17;    
      }
    
      // Change flash data
      this.changeFlashData(deviceId, width, height);
    }
  },
    
  /**
   * function: Change flash data
   * 
   * @author Igor Vuckovic <igor.vuckovic@softserbia.com>
   * @param  {String} value // flash name
   * @return void  
  */  
  changeFlashData: function(id, width, height) {
    var url = jsCommonLib.siteUrl + '/create/select-device/deviceId/' + id + '/';
    
    if (width != '') {
        url = url + 'width/' + width + '/height/' + height + '/';
    }
    var ajax = new Ajax.Request(url, {method: 'get', parameters: '', onComplete: function(transport) {jsConfiguratorLib.changeFlashDevice('true');var jsonResponse = transport.responseText.evalJSON(true);jsConfiguratorLib.updateDeviceName(jsonResponse);}});
    
    // hide cfd-devices-container
    $('cfd-devices-container').innerHTML = '';
    $('cfd-devices-container').setStyle({
      display: 'none'
    });
  },

  /**
   * function: This function use on cfd function on Complete status
   * 
   * @author Ivana Djordjevic <ivana.djordjevic@softserbia.com>
   * @param  {Object} jsonResponse // json ajax response of addToShoppingBasket request
   * @return void  
  */  
  updateDeviceName: function(jsonResponse) {
    $('device-name').innerHTML = jsonResponse['device-name'];
    $('device-name').setAttribute('title', jsonResponse['device-name']);
  },

  /**
   * function: This function adds onclick event for every <a> element without onclick event
   * on configurator index page   
   * 
   * @author Ivana Djordjevic <ivana.djordjevic@softserbia.com>
   * @author Slavisa Miljanovic <slavisa.miljanovic@softserbia.com>
   * @param  void
   * @return void  
  */  
  onclickObserver: function() {
    
    var aTags = $$('a');
    for (var i=0; i<aTags.length; i++) {
     
      if ((aTags[i].href != '') && (typeof aTags[i].onclick != 'function')) {
        
        aTags[i].onclick = function() {
          if (jsConfiguratorLib.exitWarningMessage) { // if the user has not clicked the button OKAY
                                                      // on configurator
            var ajaxUrl = jsCommonLib.siteUrl + '/cfd-devices/warning-page/';
            return jsCommonLib.showFancybox(ajaxUrl, this.href);
          }
        }
      }
    }
  },

  /**
   * function: Clicking outside of device selector in configuartor shall close selector
   *
   * @author Ivana Djordjevic <ivana.djordjevic@softserbia.com>
   * @return void
  */
  clickedOutsideElement: function() {
    $j("#select-device-link").click(function(){
      if (this.showDeviceSelector) {
        $j("#cfd-devices-container").show();
      }
      return false;
    });

    $j('#cfd-devices-container').click(function(e) {
        e.stopPropagation();
    });
    
    $j(document).click(function() {
        $j('#cfd-devices-container').hide();
    });

  },
  /**
   * function: Set cookie skinFlash
   *
   * @author Ivana Djordjevic <ivana.djordjevic@softserbia.com>
   * @param {String} value / cookie value
   * @return
   * @type   null
  */
  setDiyBlinkCookie: function(value) {
    jsCommonLib.setCookie('diyFlash', value, 10);
  },

  /**
   * function: In DIY the SELECT DEVICE button became red and blink twice
   *           - but sequence starts after he has confirmed the welcome message.
   *
   * @author Ivana Djordjevic <ivana.djordjevic@softserbia.com>
   * @return
   * @type   null
  */
  blinkText: function() {
    // When user comes first time to SKINS (and only first time)
    if (this.showDeviceSelector) {
      var diyFlash = jsCommonLib.getCookie('diyFlash');
      if (diyFlash != null && diyFlash != '') {
        // set cookie again to extend validity
        this.setDiyBlinkCookie(diyFlash);
        // clear timeout
        clearTimeout(this.blinkClearTimeOut);
        return;
  }
    }
    // The button CHANGE DEVICE became red and blink twice
    if (this.blinkTextCounter < 5) {

      if (this.blinkTextCounter % 2) {
        $('select-device-box').setStyle({
          backgroundColor: '#ff0000'
        });
      } else {
        $('select-device-box').setStyle({
          backgroundColor: 'transparent'
        });
}
    } else if (this.blinkTextCounter == 5) {
        // set cookie firts time
        this.setDiyBlinkCookie(this.blinkTextCounter);
    }

    // sequence starts after he has confirmed the welcome message
    if (this.showDeviceSelector) {
      this.blinkTextCounter++;
    }

    this.blinkClearTimeOut = setTimeout('jsConfiguratorLib.blinkText();', this.blinkTimeOut);
  }

}