jQuery.fn.anchorAnimate = function(settings) {

   settings = jQuery.extend({
    speed : 1100
  }, settings);  
  
  return this.each(function(){
    var caller = this
    $(caller).click(function (event) {
      event.preventDefault();
      var locationHref = window.location.href;
      var elementClick = $(caller).attr("href");
      var destination = $(elementClick).offset().top;

      $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
        window.location.hash = elementClick
      });
        return false;
    })
  })
}

var AJXC_Delim_1 = '~=1=~';
var AJXC_Delim_2 = '~=2=~';
var AJXC_Delim_3 = '~=3=~';
var AJXC_Delim_4 = '~=4=~';
var ProductType = '';
var ShowDischargeTab = 0;


function simple_tooltip(target_items, name){
  $(target_items).each(function(i){
    if ($(this).attr('title') != '') {
//      alert("Name: " + $(this).attr('name') + "\nID: " + $(this).attr('id') + "\nText: " + $(this).attr('title'));
      $("body").append("<div class='"+name+"' id='"+name+i+"'><p>"+$(this).attr('title')+"</p></div>");
      var my_tooltip = $("#"+name+i);

      $(this).removeAttr("title").mouseover(function(){
          my_tooltip.css({opacity:0.8, display:"none"}).fadeIn(400);
      }).mousemove(function(kmouse){
          my_tooltip.css({left:kmouse.pageX+15, top:kmouse.pageY+15});
      }).mouseout(function(){
          my_tooltip.fadeOut(400);
      });
    }
  });
}

function ProcessProduct(DivName) {
  $("#Product_Pump").removeClass('ProductSelected');
  $("#Product_Motor").removeClass('ProductSelected');
  $("#Product_Pump_Motor").removeClass('ProductSelected');
  $("#"+DivName).addClass('ProductSelected');
  
  if (DivName == "Product_Pump") {
     ProductType=1;
  }
  if (DivName == "Product_Motor") {
     ProductType=2;
  }
  if (DivName == "Product_Pump_Motor") {
     ProductType=3;
  }
  
  HideAmp();
  GetVoltages();
  if (DivName == "Product_Pump" || DivName == "Product_Pump_Motor") {
    Discharge_Size = 1;
    if (ShowDischargeTab == 1) {
      $("#Configure_Discharge_Size").show('slow');
    }
  } else {
    Discharge_Size = 0;
    $("#Configure_Discharge_Size").hide('slow');
  }
}

function ShowButton() {
  //$("#Configure_Button").show('slow');
}

function ProcessVoltage(Voltage) {
  HideAmp();
  GetAmperage(Voltage);
}

function ProcessAmperage(Voltage, Amperage) {
  HideFrame();
  GetFrameSize(Voltage, Amperage);
}

function ProcessFrameSize(FrameSize, Voltage, Amperage) {
  if (ShowDischarge() == true) {
    GetDischarge(Voltage, Amperage, FrameSize);
  } else {
    GetProducts(Voltage, Amperage, FrameSize);
  }
}

function ProcessDischarge(Discharge_Size, Voltage, Amperage, FrameSize) {
  GetProducts(Voltage, Amperage, FrameSize, Discharge_Size);
  //ShowButton();
}

function ShowDischarge() {
  ShowDischargeTab=1;
  if (Discharge_Size == 1) {
    $("#Configure_Discharge_Size").show('slow');
    $("#Configure_Button").hide();
    return true;
  } else {
    //$("#Configure_Button").show('slow');
    $("#Configure_Discharge_Size").hide();
    return false;
  }
}

function GetVoltages() {
  $.post("ajx/get_voltages.php", {
    ProductType: ProductType
  }, function(data){
    data_array = data.split(AJXC_Delim_1);
    html = "<p><h3>1) Voltage:</h3>";
    for (i=0; i<=data_array.length-1; i++) {
      html += '<input type="radio" name="Voltage" value="' + data_array[i] + '" onClick="ProcessVoltage(' + data_array[i] + ')"> ' + data_array[i] + "\n";
    }
    html += '</p>';
    WriteInnerHTML("Configure_Voltage", html);
    $("#Configure_Voltage").show('slow');
  });
}

function GetAmperage(Voltage) {
  $.post("ajx/get_amperage.php", {
    Voltage: Voltage,
    ProductType: ProductType
  }, function(data){
    data_array = data.split(AJXC_Delim_1);
    html = "<p><h3>2) Amp:</h3><br>Select the amperage closest to the one on your data tag.<br>Amperage may not be exact due to efficiancy gains and improvments in techology.<br>";
    for (i=0; i<=data_array.length-1; i++) {
      html += '<input type="radio" name="Amp" value="' + data_array[i] + '" onClick="ProcessAmperage(' + data_array[i] + ', ' + Voltage + ')"> ' + data_array[i] + "\n";
    }
    html += '</p>';
    WriteInnerHTML("Configure_Amp", html);
    $("#Configure_Amp").show('slow');
  });
}

function GetFrameSize(Amperage, Voltage) {
  HideProducts();
  $.post("ajx/get_framesize.php", {
    Voltage: Voltage,
    Amperage: Amperage,
    ProductType: ProductType
  }, function(data){
    data_array = data.split(AJXC_Delim_1);
    html = "<p><h3>3) Frame Size</h3>";
    for (i=0; i<=data_array.length-1; i++) {
      html += '<input type="radio" name="Frame" value="' + data_array[i] + '" onClick="ProcessFrameSize(' + data_array[i] + ', ' + Voltage + ', ' + Amperage + ')"> ' + data_array[i] + "\n";
    }
    html += '</p>';
    WriteInnerHTML("Configure_Frame", html);
    $("#Configure_Frame").show('slow');
    
  });
}

function GetDischarge(Voltage, Amperage, FrameSize) {
  HideProducts();
  $.post("ajx/get_discharge.php", {
    Voltage: Voltage,
    Amperage: Amperage,
    FrameSize: FrameSize,
    ProductType: ProductType
  }, function(data){
    data_array = data.split(AJXC_Delim_1);
    html = "<p><h3>4) Discharge Size:</h3><br><a href=\"#\" onClick=\"WatchVideo('How to read your Pump / Motor Data Tag', '/_uploaded_files/how_to_measure_spa_discharge_sizevideo_player1file.flv'); return false;\"><b>Watch video on how to measure discharge size.</b></a><br>";
    for (i=0; i<=data_array.length-1; i++) {
      if (data_array[i] != '') html += '<input type="radio" name="Discharge" value="' + data_array[i] + '" onClick="ProcessDischarge(' + data_array[i] + ', ' + Voltage + ', ' + Amperage + ', ' + FrameSize + ')"> ' + data_array[i] + "\n";
    }
    html += '</p>';
    WriteInnerHTML("Configure_Discharge_Size", html);
    $("#Configure_Discharge_Size").show('slow');
    
  });
}

function GetProducts(Voltage, Amperage, FrameSize, Discharge_Size) {
  WriteInnerHTML("Products_Output", Loading);
  $.post("ajx/get_products.php", {
    Voltage: Voltage,
    Amperage: Amperage,
    FrameSize: FrameSize,
    Discharge_Size: Discharge_Size,
    ProductType: ProductType
  }, function(data){
    data_array = data.split(AJXC_Delim_1);
    
    if (Discharge_Size == 1) {
      html = "<p><h3>5) Select Product:</h3><br>";
    } else {
      html = "<p><h3>4) Select Product:</h3><br>";
    }
    
    for (i=0; i<=data_array.length-1; i++) {
      html += '' + data_array[i] + "\n";
    }
    
    html += '</p>';
    WriteInnerHTML("Products_Output", html);
    $("#Products_Output").show('slow');
  });
}

function HideVoltage() {
  $("#Configure_Voltage").hide('slow');
  WriteInnerHTML("Configure_Voltage", '');
  HideAmp();
}

function HideAmp() {
  $("#Configure_Amp").hide('slow');
  WriteInnerHTML("Configure_Amp", '');
  HideFrame();
  HideProducts();
}

function HideFrame() {
  $("#Configure_Frame").hide('slow');
  WriteInnerHTML("Configure_Frame", '');
  $("#Configure_Discharge_Size").hide('slow');
  WriteInnerHTML("Configure_Discharge_Size", '');
  $("#Configure_Button").hide();
  HideProducts();
}

function HideProducts() {
  $("#Products_Output").hide('slow');
  WriteInnerHTML("Products_Output", '');
}

/*
###########################################
WRITE INFO TO DIV
###########################################
*/
function WriteInnerHTML(p_DivID, p_htmlstr) {
  if (document.getElementById) {
    document.getElementById(p_DivID).innerHTML= p_htmlstr;
  } else {
    with (document.layers[p_DivID].document) {
      open();
      write(p_htmlstr);
      close();
    }
  }
}

function WatchVideo(Name, Filename) {
  html  = "";
  html += "<h3>" + Name + "</h3>\n";
  html += "<div id=\"Video_Player\"></div>\n";
  html += "<center><span onClick=\"CloseVideo()\" style=\"cursor: pointer\">[ CLOSE VIDEO ]</span></center>\n";
  $("#Video_Content").hide();
  WriteInnerHTML("Video_Content", html);
  
  var so = new FlashObject('/swf/flv_player.swf', 'mpl', '640', '480', '0');
  so.addParam('allowscriptaccess', 'always');
  so.addParam('wmode', 'opaque');
  so.addParam('scaleMode', 'full');
  so.addParam('smoothVideo', 'true');
  so.addParam('buffer', '5');
  so.addParam('allowfullscreen', 'true');
  so.addParam('showScaleModes', 'false');
  so.addVariable("hiddenGui", "true");
  so.addVariable('flvToPlay', Filename);
  so.addVariable("autoStart", "true");
  so.write('Video_Player');
  
  $("#Video_Content").show('slow');
}

function CloseVideo () {
  $("#Video_Content").hide('slow');
  WriteInnerHTML(Video_Content, '');
}

/*
###########################################
WRITE INFO TO DIV
###########################################
*/
function WriteInnerHTML(p_DivID, p_htmlstr) {
  if (document.getElementById) {
    document.getElementById(p_DivID).innerHTML= p_htmlstr;
  } else {
    with (document.layers[p_DivID].document) {
      open();
      write(p_htmlstr);
      close();
    }
  }
}
