jQuery.noConflict();

jQuery(document).ready(function () {
  (function ($) {

    var productTypes = {
      'HD7805': {f: 627, t: 843},'HD7810': {f: 627, t: 843},'HD7811': {f: 627, t: 843},'HD7812': {f: 627, t: 843},'HD7814': {f: 627, t: 843},'HD7816': {f: 627, t: 843},
      'HD7820': {f: 627, t: 847},'HD7822': {f: 627, t: 847},'HD7823': {f: 627, t: 847},'HD7824': {f: 627, t: 847},'HD7826': {f: 627, t: 847},'HD7830': {f: 627, t: 847},'HD7832': {f: 627, t: 847},'HD7841': {f: 627, t: 847},'HD7842': {f: 627, t: 847}
    };
    var productDates = '0627,0628,0629,0630,0631,0632,0633,0634,0635,0636,0637,0638,0639,0640,0641,0642,0643,0644,0645,0646,0647,0648,0649,0650,0651,0652,0701,0702,0703,0704,0705,0706,0707,0708,0709,0710,0711,0712,0713,0714,0715,0716,0717,0718,0719,0720,0721,0722,0723,0724,0725,0726,0727,0728,0729,0730,0731,0732,0733,0734,0735,0736,0737,0738,0739,0740,0741,0742,0743,0744,0745,0746,0747,0748,0749,0750,0751,0752,0801,0802,0803,0804,0805,0806,0807,0808,0809,0810,0811,0812,0813,0814,0815,0816,0817,0818,0819,0820,0821,0822,0823,0824,0825,0826,0827,0828,0829,0830,0831,0832,0833,0834,0835,0836,0837,0838,0839,0840,0841,0842,0843,0844,0845,0846,0847';
    
    
    var steps = {0: 'step-number', 1: 'step-date', 2: 'step-result'};
    var currentStep = 0;
    
    var container = jQuery('#stepForm');
    
    var form = jQuery('div.recall form');
    
    jQuery("#register").click(function() {
      form.submit();
      return true;
    });
    
    //Set step class, which will show show/hide needed fields
    function setStep (step_id) {
      if (steps[step_id] !== undefined)
      container.removeClass(steps[currentStep]).addClass(steps[step_id]);
      currentStep = step_id;
      RecallPopup.hide();
    }
    
    function sifrResultTitle () {
      sIFR.replace(_page.sIFR.swf.GillSansLight, {
        selector: 'div.recall h2'
        ,css: '.sIFR-root {background-color:#FFFFFF;color:#005AFF;}'
        ,wmode: 'transparent'
        ,ratios: [7, 1.32, 11, 1.31, 13, 1.24, 14, 1.25, 19, 1.23, 27, 1.2, 34, 1.19, 42, 1.18, 47, 1.17, 48, 1.18, 69, 1.17, 74, 1.16, 75, 1.17, 1.16]
        ,tuneHeight: -4
      });
      
      //Object had a name attribute, which was also sent when form was submitted
      $('div.recall object[name|=""]').removeAttr('name');
    }
    
    function fillList (list, values) {
      $('option[value|=""]', list).remove();
      
      for(var i in values) {
        if (values.hasOwnProperty(i)) {
          $(list).append($('<option value="' + values[i].value + '">' + values[i].text + '</option>'));
        }
      }
    }
    
    function fillDates () {
      var values = [];
      var product_type = $(this).val();
      
      if (product_type) {
        var from = productTypes[product_type].f;
        var to = productTypes[product_type].t;
        
        for(var i in productDates) {
          if (i >= from && i <= to) {
            values.push(productDates[i]);
          }
        }
        
        setStep(1);
      } else {
        setStep(0);
      }
      
      fillList ($('select[name="productionDate"]', container), values);
    }
    
    function stepDate () {
      var val = $(this).val();
      
      if (val) {
        showResult();
      } else {
        setStep(1);
      }
    }
    
    function showResult () {
      setStep(2);
      sifrResultTitle();
    }
    
    var pd = productDates.split(',');
    productDates = {};
    for(var i=0,j=pd.length; i<j; i++) {
      var pd_id = parseInt(pd[i].replace(/^0*/, ''));
      productDates[pd_id] = {'value': pd[i], 'text': pd[i]};
    }
    
    var selects = $('#ProductId, select[name="productionDate"]', container);
    selects.eq(0).change(fillDates);
    selects.eq(1).change(stepDate);
    
    var RecallPopup = window.RecallPopup = {
      visible: false,
      element: $('div.popup'),
      deco: $('div.popup div.deco'),
      contents: ['#popup_content_1', '#popup_content_2'],
      setContent: function (content_id) {
        var c_name = '#popup_content_' + content_id;
        for(var i=0,j=this.contents.length; i<j; i++) {
          if (c_name != this.contents[i]) {
            $(this.contents[i]).css('display', 'none');
          }
        }
        $(c_name).css('display', 'block');
      },
      show: function (content_id, ref_node) {
        if (content_id) this.setContent(content_id);
        this.element.css('display', 'block');
        if (ref_node) this.move(ref_node);
        
        if (content_id == 1 && $.browser.msie && $.browser.version < 7) {
          selects.eq(1).hide();
        }
        
        this.visible = true;
        
        return false;
      },
      hide: function () {
        if (!this.visible) return;

        this.element.css('display', 'none');

        if ($.browser.msie && $.browser.version < 7) {
          selects.eq(1).show();
        }
        
        this.visible = false;
      },
      move: function (ref_node) {
        var p = $(ref_node).offset();
        var off = this.element.offset();
        
        this.element.css({top: p.top + 25 + 'px'});
        this.deco.css({left: p.left + 6 - off.left + 'px'});
      },
      init: function () {
        
        $('#showPopup1, #showPopup2').click(function (e) {
          if (RecallPopup.visible) return false;
          var content_id = $(this).attr('id').match('[0-9]+')[0];
          RecallPopup.show(content_id, this);
          
          return false;
        });
      
        //Hide popup when close icon is clicked
        $('div.close', this.element).click(function () {
          RecallPopup.hide();
        });
        
        selects.focus(function () {
          RecallPopup.hide();
        })
        
        //When clicked outside the popup close it too							
        $(document).click(function () {
          RecallPopup.hide();
        });
        
        //If clicked inside the popup don't do anything
        this.element.click(function (e) {
          e.stopPropagation();
        });
      }
    };
    
    RecallPopup.init();

  })(jQuery);
});