
function changemodel( newmodel, panel ) {
  $("#substage_btn_operation").show();
  var panel = panel || "features";
  var model = $("#substage_cameramodel");
  if ((model.text()==null)||(model.text()=="")) { model.text("EXFH20"); }
  
  if ((newmodel==null) || (newmodel=="") ) { return; }
  
  model.text(newmodel);

  var btnname = "#substage_btn_" + panel;
  
   if ((model.text() == "EXFC100") || (model.text() == "EXFS10"))
  {
	$("#substage_btn_operation").hide();
  }
  
  btnclick($(btnname));
}


function btnclick(mybtn) {
    var thisbtn= mybtn;
    var substage_btns = $("#substage_btnbar .substage_btn .pngfix");
    var model = $("#substage_cameramodel").text();
    if ((model==null)||(model=="")) { model="EXFH20"; }
    var myid = thisbtn.attr("id");
    var me = thisbtn;
    var myimg = thisbtn.find("img");
    
    substage_btns.each(function() {
      $(this).attr("src",$(this).attr("src").replace("_on","_off"));
      if ((myimg!=null) && (myimg!="") && (myimg.attr("src"))) {
        myimg.attr("src",myimg.attr("src").replace("_off","_on"));
      }
    });
    
    var textid = "";
    var title  = "";
    if (myid) {
    if (myid.indexOf("_features")>-1) {
      textid = "#substage_features_" + model;
      title  = "Features";
    } else if (myid.indexOf("_operation")>-1) {
      textid = "#substage_operation_" + model;
      title  = "Operation";
    } else if (myid.indexOf("_expandability")>-1) {
      textid = "#substage_expandability_" + model;
      title  = "Expandability";
    } else {
      textid = "#substage_techspecs_" + model;
      title  = "Technical Specifications";
    }
    }
    $("#substage_content .substage_pane").hide();
    if (textid!="") {
      $("#mainstage").hide();
      $("#substage_super_backtocameras_lnk").attr("href","cameras.shtml?model="+model);
      //$("#substage_super_details").css("background-image","url(images/cameras_details_"+model.toLowerCase()+".png)");
      //$("#substage_super_details_img").attr("src","images/cameras_details_"+model.toLowerCase()+".png");
      $("#substage_super_details_img_"+model).removeClass("hiddenobj");
      $(".substage_btn_wheretobuy_lnk").attr("href","wheretobuy.shtml?model="+model+"&shop=online");
      $("#substage").removeClass("preloader");
      $(textid).show();
      $("#substage_maintitle").text(title);
      if (($.browser.msie==true) && ($.browser.version<7)) {
        $("#substage_body").css("height",$("#substage_body").height());
        $("#substage_body").css("background-image","url(../images/substage_body_bg.png)");
      }
    }
}

function camerasearch() {
    var camera = $("#wheretobuy_form_camera").text();
    var storetype = $("#wheretobuy_form_storetype").text();
    var zip = $("#wheretobuy_zip").attr("value");
    var radius = $("#wheretobuy_radius_value").val();
    /*var radius = $("#wheretobuy_radius_value").val();*/
    //alert(date.toDateString());
    var override="";//override=true";
    $.ajax({
      type: "POST",
      url: "search.php",
      data: "storetype=" + storetype + "&camera=" + camera + "&zip=" + zip + "&radius=" + radius + override,
      success: function(msg){
        if (override=="") {
          $("#wheretobuy_results").html(msg);

          $("#wheretobuy_results .viewmaplink").click(function() {
            var mapimg= $(this).attr("data");
            $("#wheretobuy_map").html("<img src=\""+mapimg+"\" alt=\"\" />");
          });
          if (($("#wheretobuy_results .wheretobuy_result_online").size()==0) && ($("#wheretobuy_results .wheretobuy_result_offline").size()==0)) {
            $("#wheretobuy_results").html('<div id="wheretobuy_noresults"></div>');
          }
          
        } else {
        }
      }
    });
}

$(function () {
  
  $("#substage_btnbar .substage_btn").click(function() {
    btnclick($(this));
  });
  
  $(".changemodel img.pngfix").click(function() {
    var mymodel = $(this).attr("id").split("debug_img_")[1];
    changemodel(mymodel);
  });
  var pageid = $("body").attr("id");
  
  if ((pageid!=null) && (pageid!="")) {
    $("#header .head_menuitem").removeClass("head_menuitem_selected");
    var iconid = "#header #head_"+pageid;
    $(iconid).addClass("head_menuitem_selected");
    $(iconid).find("img").each(function() {
      $(this).attr("src",$(this).attr("src").replace("_off","_on"));
    });
  }
  
  $("#camerabar .camerabar_camera").click(function() {
    var cameras = $("#camerabar .camerabar_camera img");
    var self = $(this);
    var me = self.find("img");
    cameras.each(function() {
      $(this).attr("src",$(this).attr("src").replace("_on","_off"));
      me.attr("src",me.attr("src").replace("_off","_on"));
      $("#wheretobuy_form_camera").text("EX-" + self.attr("id").split("_ex")[1].toUpperCase());
      
      if ($("#wheretobuy_form_storetype").text() == "online") {
        camerasearch();
      } else if (($("#wheretobuy_form_storetype").text() == "offline") &&
                (($("#wheretobuy_zip").attr("value")!="Zip") && ($("#wheretobuy_zip").attr("value")!="") )) {
        camerasearch();
      }
      
      
    });
  });
  
  $("#xlinebar .xline_btn").click(function() {
    var xlinebtns = $("#xlinebar .xline_btn img");
    var self = $(this);
    var me = self.find("img");
    xlinebtns.each(function() {
      $(this).attr("src",$(this).attr("src").replace("e_on","e_off"));
      me.attr("src",me.attr("src").replace("e_off","e_on"));
    });
    var storetype = self.attr("id").split("_")[1];
    $("#wheretobuy_form_storetype").text(storetype);
    if (storetype=="online") {
      camerasearch();
    } else if ((storetype=="offline") &&
               (($("#wheretobuy_zip").attr("value")!="Zip") && ($("#wheretobuy_zip").attr("value")!="") )
              ) {
      camerasearch();
    } else {
      $("#wheretobuy_results").html("");
      $("#wheretobuy_map").html("");
    }
  });
  
  $("#wheretobuy_zip").mouseover(function() {
    if ($(this).attr("value")=="Zip") { $(this).attr("value",""); }
  });
  
  $("#wheretobuy_zip").mouseout(function() {
    if ($(this).attr("value")=="") { $(this).attr("value","Zip"); }
  });
  
  $("#wheretobuy_search").click(function() {
    $("#xlinebar_online img").attr("src",$("#xlinebar_online img").attr("src").replace("e_on","e_off"));
    $("#xlinebar_offline img").attr("src",$("#xlinebar_offline img").attr("src").replace("e_off","e_on"));
    $("#wheretobuy_form_storetype").text("offline");
    camerasearch();
  });
  
  $("#head_news a").click(function() {
    $("#newspop").hide("slow");
  });
  
  $("#newspop_close").click(function() {
    $("#newspop_subscribe").show();
    $("#newspop").hide("slow");
    $("#newspop_text").text("");
    $("#newspop_text").removeClass("newspop_confirm");
    $("#newspop_text").removeClass("newspop_error");
  });
  
  $("#newspop_unsubscribe").click(function() {
    var email = $("#newspop_input").val();
    var objRegExp = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{2,3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
    if (objRegExp.test(email)) {
      $("#newspop_text").text("Processing...");
      $.ajax({
        type: "POST",
        url: "newsletter.php",
        data: "subscribe=false&email="+email,
        success: function(msg){
          $("#newspop_text").text("You have been unsubscribed.");
          $("#newspop_text").addClass("newspop_confirm");
        }
      });
    } else {
      $("#newspop_text").text("Please enter a valid email address.");
          $("#newspop_text").addClass("newspop_error");
    }
  });
  
  $("#newspop_subscribe").click(function() {
    var email = $("#newspop_input").val();
    var objRegExp = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{2,3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
    if (objRegExp.test(email)) {
      $("#newspop_subscribe").hide();
      $("#newspop_text").text("Processing...");
      $.ajax({
        type: "POST",
        url: "AddAContact.php",
        data: "subscribe=true&email="+email,
        success: function(msg){
          $("#newspop_text").text("Thank you for subscribing.");
          $("#newspop_text").addClass("newspop_confirm");
        }
      });
    } else {
      $("#newspop_text").text("Please enter a valid email address.");
          $("#newspop_text").addClass("newspop_error");
    }
  });
  
  if ($("body").attr("id")=="where") { 
    var deepLinkModel = $(document).getUrlParam("model");
    var deepLinkShop = $(document).getUrlParam("shop");
    var deepLinkZip = $(document).getUrlParam("zip");
    var deepLinkRadius = $(document).getUrlParam("radius");
    if (deepLinkShop==null) {
      camerasearch();
    }
    if ((deepLinkShop=="online") || ((deepLinkShop==""))) {
      if ((deepLinkModel=="EXFH20") || (deepLinkModel=="EXF1") || (deepLinkModel=="EXFC100") || (deepLinkModel=="EXFS10")) {
        // select xline
        var xlinebtns = $("#xlinebar .xline_btn img");
        var self = $("#xlinebar_online");
        var me = self.find("img");
        xlinebtns.each(function() {
          $(this).attr("src",$(this).attr("src").replace("e_on","e_off"));
          me.attr("src",me.attr("src").replace("e_off","e_on"));
        });
        var storetype = self.attr("id").split("_")[1];
        $("#wheretobuy_form_storetype").text(storetype);
    
        // select camera
        var imgstr = "#camerabar_" + deepLinkModel.toLowerCase() + " img";
        var cameraimg = $("#camerabar_" + deepLinkModel.toLowerCase() + " img");
        $("#camerabar .camerabar_camera img").each(function() {
          $(this).attr("src",$(this).attr("src").replace("_on","_off"));
        });
        cameraimg.attr("src",cameraimg.attr("src").replace("_off","_on"));
        $("#wheretobuy_form_camera").text("EX-" + deepLinkModel.substr(2).toUpperCase());
        
        // set zip
        if ((deepLinkZip!=null) && (deepLinkZip!="")) {
          $("#wheretobuy_zip").attr("value",deepLinkZip);
        }
        
        // set radius
        if ((deepLinkRadius!=null) && (deepLinkRadius!="")) {
          $("#wheretobuy_radius_value").val("deepLinkRadius");
        }
        
        var camera = $("#wheretobuy_form_camera").text();
        var storetype = $("#wheretobuy_form_storetype").text();
        var zip = $("#wheretobuy_zip").attr("value");
        var radius = $("#wheretobuy_radius_value").val();
        var override="";//override=true";
        $.ajax({
          type: "POST",
          url: "search.php",
          data: "storetype=" + storetype + "&camera=" + camera + "&zip=" + zip + "&radius=" + radius + override,
          success: function(msg){
            if (override=="") {
              $("#wheretobuy_results").html(msg);
    
              $("#wheretobuy_results .viewmaplink").click(function() {
                var mapimg= $(this).attr("data");
                $("#wheretobuy_map").html("<img src=\""+mapimg+"\" alt=\"\" />");
              });
            } else {
            }
          }
        });
      } // if valid model
    } else if (deepLinkShop=="offline") {
      if ((deepLinkModel=="EXFH20") || (deepLinkModel=="EXF1") || (deepLinkModel=="EXFC100") || (deepLinkModel=="EXFS10")) {
        // select xline
        var xlinebtns = $("#xlinebar .xline_btn img");
        var self = $("#xlinebar_offline");
        var me = self.find("img");
        xlinebtns.each(function() {
          $(this).attr("src",$(this).attr("src").replace("e_on","e_off"));
          me.attr("src",me.attr("src").replace("e_off","e_on"));
        });
        var storetype = self.attr("id").split("_")[1];
        $("#wheretobuy_form_storetype").text(storetype);
    
        // select camera
        var imgstr = "#camerabar_" + deepLinkModel.toLowerCase() + " img";
        var cameraimg = $("#camerabar_" + deepLinkModel.toLowerCase() + " img");
        $("#camerabar .camerabar_camera img").each(function() {
          $(this).attr("src",$(this).attr("src").replace("_on","_off"));
        });
        cameraimg.attr("src",cameraimg.attr("src").replace("_off","_on"));
        $("#wheretobuy_form_camera").text("EX-" + deepLinkModel.substr(2).toUpperCase());
        
        // set zipLet
        if ((deepLinkZip!=null) && (deepLinkZip!="")) {
          $("#wheretobuy_zip").attr("value",deepLinkZip);
        }
        
        // set radius
        if ((deepLinkRadius!=null) && (deepLinkRadius!="")) {
          $("#wheretobuy_radius_value").val("deepLinkRadius");
        }
        
        var camera = $("#wheretobuy_form_camera").text();
        var storetype = $("#wheretobuy_form_storetype").text();
        var zip = $("#wheretobuy_zip").attr("value");
        var radius = $("#wheretobuy_radius_value").val();
        var override="";//override=true";
        $.ajax({
          type: "POST",
          url: "search.php",
          data: "storetype=" + storetype + "&camera=" + camera + "&zip=" + zip + "&radius=" + radius + override,
          success: function(msg){
            if (override=="") {
              $("#wheretobuy_results").html(msg);
    
              $("#wheretobuy_results .viewmaplink").click(function() {
                var mapimg= $(this).attr("data");
                $("#wheretobuy_map").html("<img src=\""+mapimg+"\" alt=\"\" />");
              });
            } else {
            }
          }
        });
      } // if valid model
    } // if offline
  } // if body = where
  
  $("#substage_content .substage_pane").hide();
});