var year;
var make;
var model;
var model_id;

var IA = {};

IA.poll_interval = 500;

IA.display_hold = 5;

IA.display_count = 0;

IA.tempX = 0;
IA.tempY = 0;

IA.getMouseCoords = function (e) {
  IA.tempX = e.pointerX(e);
  IA.tempY = e.pointerY(e);
  return true;
};

Event.observe(document, 'mousemove', IA.getMouseCoords);

IA.slider = Class.create({
  initialize: function(content, direction, duration, delay) {
    this.state = 'down';
    this.ymov = 0;
    this.content = content;
    this.direction = direction;
    this.duration = duration;
    this.delay = delay;
  },

  open: function (btn) {
    if (this.state != 'down') {
      if(Com.$('title').innerHTML === year + ' ' + make + ' ' + model) return;
      this.reopen = btn;
      this.ry = year;
      this.rma = make;
      this.rmo = model;
      this.rmid = model_id;
      return;
    }

    this.curbtn = btn;
    this.state = 'goingup';
    Com.$('title').innerHTML = year + ' ' + make + ' ' + model;
    this.reopen = undefined;
    this.ymov = - ($(this.content).getHeight() - 1);
    if(this.direction == 'down'){
      $(this.content).style.position = 'relative';
      $(this.content).style.top = -$(this.content).getHeight() + 'px';
      $('inc_alerts_container').appendChild($(this.content));
      IA.moveOverflow($(btn));
      $(this.content).style.visibility = 'visible';
      this.ymov = - this.ymov;
        }
        else{
      $(this.content).style.top = $('inc_alerts_container').getHeight() - $('inc_alerts_img').getHeight() + 'px';
      $(this.content).style.marginTop = 0;
      this.ymov += $('inc_alerts_img').getHeight();
        }
    new Effect.Move(this.content, {
      y: this.ymov,
      delay: this.delay,
      duration: this.duration,
      afterFinish: function () {
        incentiveAlerts.state = 'up';
        if(document.viewport.getHeight()-incentiveAlerts.curbtn.viewportOffset()['top'] < $('inc_alerts_tbl').getHeight())
          $('inc_alerts_tbl').scrollIntoView(false);
        $('incentive_name1').focus();
      }
    });
  },

  close: function (img, stop) {
     this.state = 'goingdown';
     if (stop==null) index = 1; else index = stop;
     if(stop)
       new Effect.Move($('inc_alerts_tbl'), {
        y: - this.ymov,
        duration: this.duration,
        afterFinish: function () {
          $(img).onmouseover = '';
          $('inc_alerts_form'+index).style.visibility = 'visible';
          incentiveAlerts.state = 'down';
        }
      });
    else
      new Effect.Move($('inc_alerts_tbl'), {
        y: - this.ymov,
        duration: this.duration,
        afterFinish: function () {
          incentiveAlerts.state = 'down';
          if(incentiveAlerts.reopen!=undefined){
            year = incentiveAlerts.ry;
            make = incentiveAlerts.rma;
            model = incentiveAlerts.rmo;
            model_id = incentiveAlerts.rmid;
            incentiveAlerts.open(incentiveAlerts.reopen);
          }
        }
      });
  },

  destruct: function() {
    $('inc_alert_placeholder').appendChild($(this.content));
  }
});

document.observe("dom:loaded", function() {
  if (typeof(checkPopup) === "function") {
    setTimeout("checkPopup()", IA.poll_interval);
  }
});

IA.setCookie = function (name) {
  var date = new Date();
  date.setTime(date.getTime()+(182*24*60*60*1000));
  var expires = "; expires="+date.toGMTString();
  var saved = QU.getCookie(name);
  if (saved == null) {
    document.cookie = name+"="+model_id+':'+year+expires+"; path=/";
  }
  else if(saved.indexOf(model_id + ':' + year) <= 0) {
    document.cookie = name + "=;-1; path=/";
    document.cookie = name + "=" + saved+'|' +model_id+':'+year+expires+"; path=/";
  }
};

IA.saveAlert = function (n) {
  form_number = n;
  var isValid = true;
  var summary = '';
  if (!QU.checkName($F('incentive_name' + n))) {
    isValid = false;
    summary += 'Please enter a valid first name';
  }
  if (!QU.checkEmail($F('incentive_email' + n))) {
    isValid = false;
    if (summary == '') {
      summary += 'Please enter a valid email address';
    }
    else {
      summary += ' and email address';
    }
  }
  if (!QU.checkZip($F('2f_zipcode' + n))) {
    isValid = false;
    if (summary == '') {
      summary += 'Please enter a valid ZIP Code';
    }
    else {
      summary += ' and ZIP';
    }
  }
  if (!isValid) {
    if (summary.lastIndexOf('and') != summary.indexOf('and')) {
     summary = summary.replace(' and', ',');
    }
    Com.$('valSummary'+n).innerHTML = summary + '.';
    Com.$('valSummary'+n).style.visibility = 'visible';
    return;
  }
  Com.$('valSummary'+n).style.visibility = 'hidden';
  var URL = '/ajax/saveAlert.php';
  if( n==1) {
    Com.$('inc_alerts_form'+n).style.visibility = 'hidden';
    Com.$('inc_alerts_loading'+n).style.visibility = 'visible';
  }
  else if (n==2) {
    Com.$('inc_alerts_form'+n).style.display = 'none';
    Com.$('inc_alerts_loading'+n).style.display = 'block';
  }
  var pars = 'first_name=' + $F('incentive_name'+n) + '&email=' + $F('incentive_email'+n) + '&zip_code=' + $F('2f_zipcode'+n) + '&model_id=' + model_id + '&year=' + year + '&model_name=' + model + '&make_name=' + make;
  var myAjax = new Ajax.Request(URL, {method: 'post', parameters: pars, onComplete: IA.thankYou});
};

IA.thankYou = function (originalRequest, json) {
  var feed = originalRequest.responseText.evalJSON();
  if( feed > 0 ){
    Com.$('inc_alerts_loading' + form_number).style.visibility = 'hidden';
    if(typeof(incentiveAlerts.curbtn) === "undefined") {
      incentiveAlerts.curbtn = 'inc_alerts_img';
    }
    if($(incentiveAlerts.curbtn).src.indexOf('/rebates/alerts_icon.gif') >= 0) {
      Com.$(incentiveAlerts.curbtn).src = (Boolean(IMAGEPATH) ? IMAGEPATH:'/images') + '/rebates/alerts_check_icon.gif';
    }
    else {
      Com.$(incentiveAlerts.curbtn).src = (Boolean(IMAGEPATH) ? IMAGEPATH:'/images') + '/rebates/alerts_collapsed_check_tab.png';
    }
    Com.$(incentiveAlerts.curbtn).onmouseover = '';

    if(form_number === 1) {
      incentiveAlerts.close(incentiveAlerts.curbtn, form_number);
      IA.addToolTip(incentiveAlerts.curbtn);
    }
    else if (form_number === 2) {
      IA.addToolTip('inc_alerts_img');
      Com.$('inc_alerts_loading' + form_number).style.display = 'none';
      Com.$('inc_alerts_thanks' + form_number).style.display = 'block';
    }
    IA.setCookie('inc_alert');
    if(feed === 2){
      popInfo.open();
    }
  }
  else {
    if(form_number === 1){
      Com.$('inc_alerts_loading' + form_number).style.visibility = 'hidden';
      Com.$('inc_alerts_form' + form_number).style.visibility = 'visible';
    }
    else{
      Com.$('inc_alerts_loading' + form_number).style.display = 'none';
      Com.$('inc_alerts_form' + form_number).style.display = 'block';
    }

    if (feed == -1){
      Com.$('incentive_email' + form_number).value = '';
      Com.$('incentive_email' + form_number).focus();
      if(form_number == 1 && $('valSummary' + form_number).innerHTML.match('Earthlink')){
        IA.adjustWindow(0);
      }
      Com.$('valSummary' + form_number).innerHTML = 'Please enter a valid email address.';
    }
    else if (feed == -3){
      Com.$('2f_zipcode' + form_number).value = '';
      Com.$('2f_zipcode' + form_number).focus();
      if(form_number == 1 && $('valSummary' + form_number).innerHTML.match('Earthlink')){
        IA.adjustWindow(0);
      }
      Com.$('valSummary' + form_number).innerHTML = 'Please enter a valid ZIP Code.';
    }
    else
      alert(feed);
    Com.$('valSummary' + form_number).style.visibility = 'visible';
  }
};

IA.adjustWindow = function (n) {
  ymov = $('valSummary1').getHeight() - 13;
  if (incentiveAlerts.direction === 'up') {
    if(n==1){
      ymov = -ymov;
    }
    new Effect.Move($('inc_alerts_tbl'), {
      y: ymov,
      delay: 0,
      duration: 0.2,
      afterFinish: function () {
        incentiveAlerts.state = 'up';
        incentiveAlerts.ymov = -$('inc_alerts_tbl').getHeight() + $('inc_alerts_img').getHeight();
      }
    });
  }
  else {
    IA.moveOverflow(incentiveAlerts.curbtn);
    incentiveAlerts.ymov = $('inc_alerts_container').getHeight();
  }
};

IA.moveOverflow = function (obj) {
  Com.$('inc_alerts_container').style.visibility = 'visible';
  Com.$('inc_alerts_container').style.left = $(obj).cumulativeOffset()['left'] + 'px';
  Com.$('inc_alerts_container').style.top = $(obj).cumulativeOffset()['top'] + $(obj).getHeight() + 3 + 'px';
  Com.$('inc_alerts_container').style.width = $('inc_alerts_tbl').getWidth() + 'px';
  Com.$('inc_alerts_container').style.height = $('inc_alerts_tbl').getHeight() + 'px';
};

IA.addToolTip = function (node) {
  Com.$(node).rel = "hlpChecked";
  new Tip(node, Com.$(Com.$(node).rel).innerHTML, {
    hook: {target: 'topMiddle', tip: 'bottomRight'},
    stem: 'bottomRight',
    style: 'creamy',
    width: 181
  });
};
