var dots = new Array("one", "two", "three");

function threepositionslide(myDirection, myBlock, myDistance) {
  var mypositions = new Array(0, 0 - myDistance, 0 - myDistance * 2);

  /* This is to prevent a ghetto 1px difference when window is sized in certain ways */
  var currentPos = $("#" + myBlock + "window").css("left");
  currentPos = currentPos.replace("px", "");

  for($i = 0; $i < mypositions.length; $i++) {
    if (currentPos == mypositions[$i]) {
      if (myDirection == "right") { myIndex = $i + 1; }
      if (myDirection == "left") { myIndex = $i - 1; }
      
      if (myIndex < 0) { myIndex = 0; }
      if (myIndex > mypositions.length - 1) { myIndex = mypositions.length - 1; }
      $("." + myBlock + "one").attr({src: "/customcode/images/lightdot.jpg"});
      $("." + myBlock + "two").attr({src: "/customcode/images/lightdot.jpg"});
      $("." + myBlock + "three").attr({src: "/customcode/images/lightdot.jpg"});
      $("." + myBlock + dots[myIndex]).attr({src: "/customcode/images/darkdot.jpg"});
      $("#" + myBlock + "window").animate({ left: mypositions[myIndex] + "px"}, 500, "easeOutQuad");
    }
  }
}

function videogallery(name, category, page, number) {
  $.post("/customcode/actions.cfm?rand=" + o_ajaxrand, { func: "o_videogallery", name: name, category: category, page: page, number: number }, function(data) { 
    $("#videogallery").html(data); 
  }
  );
}

function o_getajaxrand() {
  var o_ajaxrand = String(new Date().getTime()) + String(Math.floor(Math.random()*100));
  return o_ajaxrand;
}

function tabs(olink, oblock, olist) {
  $("#" + oblock + " .tabs .active").removeClass("active");
  olink.className = "active";

  $("#" + oblock + " .lists .active").removeClass("active");
  $("#" + oblock + " .lists ." + olist).addClass("active");
}

function o_logout() {
  $.get("/customcode/owen_callcfc.cfm?component=o_user&func=logout&type=url&rand=" + o_ajaxrand, "", function(data) {
    if (data.code == "1") {
      alert("User logged out");
      window.location.reload(true);
    } else {
      alert(data.message);
    }
  }, "json");
}

function answerpoll(pollid) {
  $("#poll_form_" + pollid + " .pollsubmit").removeAttr("onclick");
  $.get("/customcode/polls/submit.cfm?rand=" + o_ajaxrand, $("#poll_form_" + pollid).serialize(), function(data) {
    if (data.code == 0) {
      $("#poll_form_" + pollid + " .pollsubmit").attr("onClick", "answerpoll(" + pollid + "); return false;");
      alert("Required field has been left blank.");
    } else if (data.code == 1) {
      $(".poll_id_" + pollid).html(data.data);
    }
  }, "json");
  return false;
}
  
function pollresults(pollid) {
  $.get("/customcode/polls/results.cfm?rand=" + o_ajaxrand, { pollid: pollid }, function(data) {
    if (data.code == 1) {
      $(".poll_id_" + pollid).html(data.data);
    }
  }, "json");
  return false;
}

function o_login(email, password) {
  var myreturn;
  var temp = $.ajax({
    type: "POST",
    async: false,
    url: "/customcode/owen_callcfc.cfm?component=o_user&func=login&type=form&rand=" + o_ajaxrand,
    data: ({email: email, password: password}),
    dataType: "json",
    success: function(data) {
      if (data.code == 1) {
        alert(data.message);
        myreturn = true;
      } else {
        alert(data.message);
        myreturn = false;
      }
    }
  });
  return myreturn;
}

sfHover = function() {     
  var sfEls = document.getElementById("topnav").getElementsByTagName("LI");     
  for (var i=0; i<sfEls.length; i++) {         
    sfEls[i].onmouseover=function() {             
      this.className+=" sfhover";         
    }         
    sfEls[i].onmouseout=function() {             
      this.className=this.className.replace(new RegExp(" sfhover\\b"), "");         
    }     
  } 
}

if (window.attachEvent) window.attachEvent("onload", sfHover);

function togglequickloginform() {
  $("#quickloginform").toggle();
}

function loadsitewideheader() {
  if (location.href.indexOf('/secure/') == -1) {
    if (document.getElementById("sitewideheader") == null) {
      var oSiteWide = document.createElement("div");
      oSiteWide.id = "sitewideheader";
      var oBrother = document.getElementById("site");
      document.body.insertBefore(oSiteWide, oBrother);
    }
    oSiteWide = document.getElementById("sitewideheader");
    $.get("/customcode/users/getsitewide.cfm?rand=" + o_ajaxrand, "", function(data) {
      oSiteWide.innerHTML = data;
      $("#quickloginform #login").submit(function() {
        var myform = document.getElementById("login");
        var email = myform.email.value;
        var password = myform.password.value;
        if (o_login(email,password)) {
          window.location.reload(true);
        }
        return false;
      });
    });
  }
}