var preimg = "";
$(document).ready(function(){
  if (location.hash.slice(1).length > 0) load();
  init();
});
function init() {
  $(".pl").each(function(){
    $(this).children("a").children("img:last").mouseover(function(){
      $(this).fadeOut();
      $(this).prev().fadeIn();
      $(this).parent().parent().children("p").fadeIn();
    });
  });
  $(".pl").each(function(){
    $(this).children("a").children("img:first").mouseout(function(){
      $(this).fadeOut();
      $(this).next().fadeIn();
      $(this).parent().parent().children("p").fadeOut();
    });
  });
  $(".pl > a").click(function(){setTimeout('load()',50);});
  $("a[rel=init]").click(function(){setTimeout('load()',50);});
  $("a[rel=claim]").click(function(){setTimeout('claim()',50);});
  $("a[rel=submit]").click(function(){setTimeout('submit()',50);});
  $("input[type=checkbox]").click(function(){
    var s=0;
    $("input[type=checkbox][checked]").each(function(){
      v=$(this).val().split(".");
      s+=parseInt(v[1]);
    });
    $("#price").html(triads(s," "));
  });
}
function load(){
  a=location.hash.slice(1).split(".");
  if (a=='') {$("#logo").unbind('click')} else {$("#logo").click(function(){setTimeout('load()',50);});}
  try {pageTracker._trackPageview('/'+location.hash.slice(1));} catch(err) {}
  $("#iw").animate({
    left:'-'+$(window).width()+'px'
  },1000,"swing",function(){
    $("#iw").css('left',$(window).width()+"px");
    $.post("/ajax.php",{act:a[0],cont:a[1]},function(data) {
      $("#iw").html(data);
      init();
      $("#iw").animate({
        left:'0px'
      },1000,"swing");
    });
  });
}
function claim(){
  v='';
  $("input[type=checkbox][checked]").each(function(){
    v=v+$(this).val()+";";
  });
  v=v.slice(0,v.length-1);
  try {pageTracker._trackPageview('/'+location.hash.slice(1));} catch(err) {}
  $("#iw").animate({
    left:'-'+$(window).width()+'px'
  },1000,"swing",function(){
    $("#iw").css('left',$(window).width()+"px");
    $.post("/ajax.php",{act:'claim',cont:v,word:$("#txtWord").val()},function(data) {
      $("#iw").html(data);
      init();
      $("#iw").animate({
        left:'0px'
      },1000,"swing");
    });
  });
}
function submit(){
  t=true;
  if ($("#txtName").val().length==0) {
    $("#errName").slideDown();
    t=false;
  } else {
    $("#errName").slideUp();
  }
  if ($("#txtPhone").val().length==0 && (! /^\w+[-_\.]*\w+@\w+-?\w+\.[a-z]{2,4}$/.test($("#txtEmail").val()))){
    t=false;
    $("#errPhone").slideDown();
    $("#errEmail").slideDown();
  } else {
    $("#errPhone").slideUp();
    $("#errEmail").slideUp();
  }
  if ($("#txtPhone").val().length!=0 && $("#txtEmail").val().length!=0 && (! /^\w+[-_\.]*\w+@\w+-?\w+\.[a-z]{2,4}$/.test($("#txtEmail").val()))){
    t=false;
    $("#errEmail").slideDown();
  } else {
    $("#errEmail").slideUp();
  }
  if (t) {
    location.hash='thanks';
    setTimeout(function(){
      $("#iw").animate({
        left:'-'+$(window).width()+'px'
      },1000,"swing",function(){
        $("#iw").css('left',$(window).width()+"px");
        $.post("/ajax.php",{act:'thanks',name:$("#txtName").val(),phone:$("#txtPhone").val(),email:$("#txtEmail").val(),comments:$("#txtComments").val()},function(data){

          $("#iw").html(data);
          init();
          $("#iw").animate({
            left:'0px'
          },1000,"swing");
        });
      });
    },50);
  }
}
$.fn.center=function(){var w = $(window);this.css("position","absolute");this.css("top",(w.height()-this.height())/2+w.scrollTop()+"px");this.css("left",(w.width()-this.width())/2+w.scrollLeft() + "px");return this;}
function iload(img){$("body").append("<div id=\"preload\" style=\"display:none\"></div>");for (var i=0;i<img.length;i++) $("#preload").append("<img src=\""+img[i]+"\" />");}
function triads(m,tS){var n=parseInt(m).toString();var regEx=/(-?\d+)(\d{3})/;while(regEx.test(n)){n=n.replace(regEx,"$1"+tS+"$2");}return n;}