function activatePlaceholders() {
  var detect = navigator.userAgent.toLowerCase();
  if (detect.indexOf("webkit") > 0 || detect.indexOf("gecko") > 0) return false;
  var inputs = document.getElementsByTagName("input");
  for (var i=0;i<inputs.length;i++) {
    if (inputs[i].getAttribute("type") == "text") {
     if (inputs[i].getAttribute("placeholder") && inputs[i].getAttribute("placeholder").length > 0) {

      // Place placeholder text in input
      inputs[i].value     = inputs[i].getAttribute("placeholder");
      inputs[i].className = "placeholder-text";

      // Hide placeholder text on focus
      inputs[i].onfocus = function() {
       if (this.value == this.getAttribute("placeholder")) {
        this.value     = "";
        this.className = this.className.replace("placeholder-text", "");
       }
       return false;
      }

      // Put placeholder text back if it's empty
      inputs[i].onblur = function() {
       if (this.value.length < 1) {
        this.value     = this.getAttribute("placeholder");
        this.className = this.className + " placeholder-text";
       }
      }
     }
    }
  }
  return true;
}

function addRemoveStateOnHover() {
  var inputs = document.getElementsByTagName("input");
  for (var i=0; i < inputs.length; i++) {
    if (inputs[i].className.indexOf("added") >= 0) {
      inputs[i].onmouseover = function () {
        this.value = "× Remove";
      }
      inputs[i].onmouseout  = function () {
        this.value = "Added";
      }
    }
  }
}


window.onload=function() {
  activatePlaceholders();
  addRemoveStateOnHover();
  if (typeof(rcDropdown) != "undefined") {
    rcDropdown.init();
  }

  /* temporary */
  var topper = document.getElementById("topper");
  if (typeof(topper) != "undefined" && topper != null) {
    topper.onclick = function () {
    //  this.style.display = "none";
    }
  }

  var POPUP_FADE_TIME = 500;

  $(".popup-button").each(function (e) {
    var popup = $($(this).attr("name"));


    // Add button functionality
    $(this).click(function (event) {
		
      if ($(".ie7").length > 0) {}
	 // alert('');
	 
      popup.fadeToggle(POPUP_FADE_TIME);
      event.preventDefault();
	  event.stopPropagation();
    });

    // Add cancel link functionality
	$(".cancel").click(function(event){
		popup.fadeOut(POPUP_FADE_TIME);
      event.preventDefault();
	});
   /* $($(this).attr("name") + " .cancel").click(function (event) {
      popup.fadeOut(POPUP_FADE_TIME);
      event.preventDefault();
    });*/
  });
  
  $(".print-button").click(function () {
    window.print();
  });
  
  $(".copy-button").each(function () {
    console.log("copy button found");
    $(this).zclip({
      path: 'js/libs/ZeroClipboard.swf',
      copy: function () {
        console.log("Copied");
        return $($(this).attr("value")).val();
      }
    });
  });

  // Hide popup when you click somewhere else  
  $("body").click(function (event) {
    
    // Clicked on a popup button
    if ($(event.target).hasClass("popup-button")) {
      
      $(".popup").each(function () {
        if ("#" + $(this).attr("id") != $(event.target).attr("name")) {
          $(this).fadeOut(POPUP_FADE_TIME);
        }
      });
      
    // Clicked within the popup
    } else if ($(event.target).hasClass("popup") || $(event.target).parents(".popup").length > 0) {
    
      // Do nothing
      
    // Clicked somewhere else
    } else {
      $(".popup").fadeOut(POPUP_FADE_TIME);
    }
  });
}

