/**********
 *  behaviour.js
 *  Specially for the telecomshop
 *
 **********/
 
Array.prototype.find = function (element) {
  for (var keys in this) {
    if (this[keys] == element) {
      return keys;
      break;
    }  
  }
  return -1;
};

 
if (initFunctions) {
  initFunctions.push(
    function() { // Set up phone viewing handlers
      if ($("phone_visual_main") && $("magnify_phone")) {
        var phoneVisualMain = $("phone_visual_main");
        var phoneView = $("phone_view");
        var phoneLargeImages = new Array();
        var closePhotoViewer = $("close_photo_viewer");
        var showPreviousPhoto = $("previous_photo");
        var showNextPhoto = $("next_photo");
        var magnifyPhone = $("magnify_phone");
        var popupBg = $("popup_bg");
        var phoneViewerContainer = $("phone_viewer_container");
        magnifyPhone.onclick = function () {
          popupBg.style.display = "block";
          phoneViewerContainer.style.display = "block";
          
          popupBg.style.height =  getPageHeight() + "px";
          
          var currentPhoto = phoneLargeImages.find(phoneView.src);
          if ((currentPhoto == -1) || (currentPhoto == 0))
            showPreviousPhoto.style.display = "none";
          else
            showPreviousPhoto.style.display = "block";
          
          showNextPhoto.style.display = (phoneLargeImages.length - currentPhoto) == 1 ? "none" : "block";
            
          return false;
        }
        phoneVisualMain.style.cursor = "pointer";
        phoneVisualMain.onclick = magnifyPhone.onclick;
        
        // Set up working phonethumbnails
        var phoneThumbs = document.getElementsByClassName("phone_thumb");
        for (var i=0; i < phoneThumbs.length; i++) {
          phoneLargeImages[i] = phoneThumbs[i].href.substring(0, phoneThumbs[i].href.length - 5) + "l.jpg";
          phoneThumbs[i].onclick = function () {
            phoneVisualMain.src = this.href;
            phoneView.src = this.href.substring(0, this.href.length - 5) + "l.jpg";
            return false;
          }
        }
        
        // Close the photoviewer
        closePhotoViewer.onclick = function () {
          popupBg.style.display = "none";
          phoneViewerContainer.style.display = "none";
          return false;
        }
        
        // Show the previous photo
        showPreviousPhoto.onclick = function () {
          var currentPhoto = phoneLargeImages.find(phoneView.src);
          if ((currentPhoto == -1) || (currentPhoto == 0)) return false;
          phoneView.src = phoneLargeImages[--currentPhoto];
          
          if ((currentPhoto == -1) || (currentPhoto == 0))
            showPreviousPhoto.style.display = "none";
          else
            showPreviousPhoto.style.display = "block";
          
          showNextPhoto.style.display = (phoneLargeImages.length - currentPhoto) == 1 ? "none" : "block";
          return false;
        }
        
        // Show the next photo
        showNextPhoto.onclick = function () {
          var currentPhoto = phoneLargeImages.find(phoneView.src);
          if ((currentPhoto == -1) || (currentPhoto == phoneLargeImages.length)) return false;
          phoneView.src = phoneLargeImages[++currentPhoto];
          
          if ((currentPhoto == -1) || (currentPhoto == 0))
            showPreviousPhoto.style.display = "none";
          else
            showPreviousPhoto.style.display = "block";
          
          showNextPhoto.style.display = (phoneLargeImages.length - currentPhoto) == 1 ? "none" : "block";
          return false;
        }
      }
      
      // Create show/hide specificationshandle
      if ($("specification_handle") && $("phone_specs")) {
        var specificationHandle = $("specification_handle");
        var phoneSpecs = $("phone_specs");
        phoneSpecs.style.display = "none";
        specificationHandle.onclick = function () {
          if (phoneSpecs.style.display == "block") {
            phoneSpecs.style.display = "none";
            specificationHandle.className = "more";
            if (specificationHandle.firstChild.nodeType == 3)
              specificationHandle.firstChild.nodeValue = "Meer specificaties";
          }
          else {
            phoneSpecs.style.display = "block";
            specificationHandle.className = "less";
            if (specificationHandle.firstChild.nodeType == 3)
              specificationHandle.firstChild.nodeValue = "Minder specificaties";
          }
          return false;
        }
      }
    }
  );
  initFunctions.push(
    function () {  // Setup Idealchoice handlers
      if ($("pay_method_ideal_container")) {
        var payMethodIdealContainer = $("pay_method_ideal_container");
        payMethodIdealContainer.style.paddingBottom = 0 + "px";
      }
      
      var payMethodsIdeal = document.getElementsByClassName("pay_method_ideal");
      for (var i=0; i < payMethodsIdeal.length; i++) {
        payMethodsIdeal[i].style.display = "none";
      }
    }
  );
  initFunctions.push(
    function () {  // Setup Idealchoice behaviour
      if (!$("pay_method_ideal") || !$("pay_method_rembours")) return false;
            
      var payMethodsIdeal = document.getElementsByClassName("pay_method_ideal");
      var payMethodIdeal = $("pay_method_ideal");
      var payMethodRembours = $("pay_method_rembours");
      var payMethodIdealContainer = $("pay_method_ideal_container");
      
      payMethodIdeal.onchange = function () {
        if (payMethodIdeal.checked == true) {
          if ($("pay_method_ideal_container"))
            payMethodIdealContainer.style.paddingBottom = 0 + "px";
          for (var i=0; i < payMethodsIdeal.length; i++) {
            payMethodsIdeal[i].style.display = "block";
          }
        }
        else {
          if ($("pay_method_ideal_container"))
            payMethodIdealContainer.style.paddingBottom = 0 + "px";
          for (var i=0; i < payMethodsIdeal.length; i++) {
            if (payMethodsIdeal[i].childNodes[3].checked == true)
              payMethodsIdeal[i].childNodes[3].checked = false;
            payMethodsIdeal[i].style.display = "none";
            payMethodsIdeal[i].className = "pay_method_ideal";
          }
        }  
      }
      payMethodRembours.onchange = payMethodIdeal.onchange;
      payMethodIdeal.onclick = payMethodIdeal.onchange;
      payMethodRembours.onclick = payMethodIdeal.onchange;
      
      for (var i=0; i < payMethodsIdeal.length; i++) {
        payMethodsIdeal[i].onclick = function () {
          for (var j=0; j < payMethodsIdeal.length; j++) {
            payMethodsIdeal[j].className = "pay_method_ideal";
          }
          this.className += " checked";
          this.childNodes[3].checked = "checked";
        }
      }
    }
  );
  initFunctions.push(
    function() { // Set up Terms & Conditions popup
      if (!$("terms_conditions") || !$("popup_bg") || !$("popup_container") || !$("popup")) return false;
      var termsConditions = $("terms_conditions");
      var popupBg = $("popup_bg");
      var popupContainer = $("popup_container");
      var popup = $("popup");
      var closePopup = $("close_popup");
      
      termsConditions.onclick = function () {
        popupBg.style.display = "block";
        popupContainer.style.display = "block";

        popupBg.style.height = getPageHeight() + "px";
        return false;
      }
      
      // Close the popup
      closePopup.onclick = function () {
        popupBg.style.display = "none";
        popupContainer.style.display = "none";
        return false;
      }
      
      if ($("terms_conditions_agree"))
        $("terms_conditions_agree").onclick = termsConditions.onclick;
    }
  );
  initFunctions.push(
    function() { // Set up iDeal help popup
      if (!$("help_ideal_handle") || !$("popup_bg") || !$("help_ideal_container") || !$("help_ideal")) return false;
      var helpIdealHandle = $("help_ideal_handle");
      var popupBg = $("popup_bg");
      var helpIdealContainer = $("help_ideal_container");
      var helpIdeal = $("help_ideal");
      var closeHelpIdeal = $("close_help_ideal");
      
      helpIdealHandle.onclick = function () {
        popupBg.style.display = "block";
        helpIdealContainer.style.display = "block";

        popupBg.style.height =  getPageHeight() + "px";
        return false;
      }
      
      // Close the iDeal help
      closeHelpIdeal.onclick = function () {
        popupBg.style.display = "none";
        helpIdealContainer.style.display = "none";
        return false;
      }
    }
  );
  initFunctions.push(
    function() { // Set up rembours help popup
      if (!$("help_rembours_handle") || !$("popup_bg") || !$("help_rembours_container") || !$("help_rembours")) return false;
      var helpRemboursHandle = $("help_rembours_handle");
      var popupBg = $("popup_bg");
      var helpRemboursContainer = $("help_rembours_container");
      var helpRembours = $("help_rembours");
      var closeHelpRembours = $("close_help_rembours");
      
      helpRemboursHandle.onclick = function () {
        popupBg.style.display = "block";
        helpRemboursContainer.style.display = "block";

        popupBg.style.height =  getPageHeight() + "px";
        return false;
      }
      
      // Close the rembours help
      closeHelpRembours.onclick = function () {
        popupBg.style.display = "none";
        helpRemboursContainer.style.display = "none";
        return false;
      }
    }
  );
  initFunctions.push(
    function() { // Set up Terms & Conditions popup
      if (!$("offer_code_result")) return false;
      var offerCodeResult = $("offer_code_result");
      offerCodeResult.style.display = "none";
      Effect.BlindDown(offerCodeResult);
      setTimeout("new Effect.Highlight('offer_code_result', {startcolor:'#f78f1e', endcolor:'#e7f6fd', restorecolor:'#e7f6fd'})",500);
    }
  );
  initFunctions.push(
    function() { 
      // Set up check_address link
      if ($("check_address") && $("order_step1")) {
        $("check_address").onclick = function () {
          $("order_step1").submit();
          return false;
        }
      }
      
      // Set up change_address link
      if ($("change_address") && $("order_step1")) {
        $("change_address").onclick = function () {
          $("order_step1").submit();
          return false;
        }
      }
    }
  );
  initFunctions.push(
    function() {
      // Insert homepage flashbanner
      if ($("weektopper_banner")) {
        var fo = new SWFObject('./ui/beltegoed_homevisual.swf', 'weektopper_banner_flash', '274', '225', '8', 'flash'); 
        fo.addParam("allowScriptAccess", "always");
        fo.addParam("quality", "high"); 
        fo.addParam("menu", "false");
        fo.addParam("wmode", "transparent"); 
        fo.write("weektopper_banner");
      }
    }
  );
  initFunctions.push(
    function() {
      // Insert homepage flashbanner
      if ($("subhome_banner")) {
        var fo = new SWFObject('ah_mobiel.swf', 'subhome_banner_flash', '740', '275', '8', 'flash'); 
        fo.addParam("allowScriptAccess", "always");
        fo.addParam("quality", "high"); 
        fo.addParam("menu", "false");
        fo.addParam("wmode", "transparent"); 
        fo.write("subhome_banner");
      }
    }
  );
  initFunctions.push(
    function () {  // Setup 'Subscribe now' handlers
      if ($("subscribe_now")) {
        var subscribeNow = $("subscribe_now");
        subscribeNow.onclick = function() {
          window.open("http://myah.2organize.com/cgi/topup.cgi?type=t","brief","width=600,height=600,scrollbars=yes");
          return false;
        }
      }
    }
  );
}

/* 
  (former server side check for Safari)
*/
if (navigator.userAgent.indexOf('Safari/3') != -1) {
  document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"/mobiel/ui/safari.css\"/>");
}

function getPageHeight() {
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		documentHeight = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		documentHeight = document.body.scrollHeight;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		documentHeight = document.body.offsetHeight;
  }
  return documentHeight;
}

function unSetAnimatedHover(n) {
  if (!n) return;
  function hover(e) {
    if (n.effect) n.effect.cancel();
  }
  function unhover(e) {
    if (n.effect) n.effect.cancel();
  }
  Event.observe(n, "mouseover", hover);
  Event.observe(n, "focus", hover);
  Event.observe(n, "mouseout", unhover);
  Event.observe(n, "blur", unhover);
}


addInit(function() {
  CorneredBox.setup([$("to_main_site"), $("to_delivery"), $("to_service"), $("to_telecom_home"), $("to_telecom_offerings"), $("to_mobiel")]);
  /*
  if (!document.body.hasClassName("home")) setAnimatedHover($("to_telecom_home"), "#FFFFFF", "#E7F6FD");
  if (!document.body.hasClassName("telecom_offerings") && !document.body.hasClassName("details")
      && !document.body.hasClassName("overview") && !document.body.hasClassName("order")) 
    setAnimatedHover($("to_telecom_offerings"), "#FFFFFF", "#E7F6FD");
  if (!document.body.hasClassName("mobiel") && !document.body.hasClassName("subhome")) 
    setAnimatedHover($("to_mobiel"), "#FFFFFF", "#E7F6FD");
  if (!document.body.hasClassName("main_site")) setAnimatedHover($("to_main_site"), "#FFFFFF", "#E7F6FD");
  if (!document.body.hasClassName("service") && !document.body.hasClassName("klantenservice")) 
    setAnimatedHover($("to_service"), "#FFFFFF", "#E7F6FD");
  if (!document.body.hasClassName("bezorging")) setAnimatedHover($("to_delivery"), "#FFFFFF", "#E7F6FD");
  if (!document.body.hasClassName("overtelecom")) setAnimatedHover($("to_about"), "#FFFFFF", "#E7F6FD");
  */
});

