﻿jQuery(document).ready(function () {

  initGlobalFunctions();  
initProductPage();
initBeregner();
  initSearchPage();
  productHeading();

  if(jQuery("div#yderligereInfo").length>0 && jQuery("div#yderligereInfoText").text().length<1)
    {
      jQuery("div#yderligereInfo").css("display","none");
    }
  initTopMenuContainer();
});

jQuery(window).bind("load",function() {

  
initCasePage();
});

function initSearchPage() {
  if (jQuery(".result-no-result").length > 2) {
    jQuery("#totalresultResponse").html( jQuery(".result-no-result:first").html());
  }
}


function productHeading() {
  var text = jQuery("div#subnav a.active").text() + " - " + jQuery("h3.productHeader").text();
  jQuery("h3.productHeader").text(text);
  Cufon.replace('.width33procent h1.h1', { fontSize: '25px', margin: '0px', color: '#ffffff', lineHeight:'0.5' });
  Cufon.replace('.width33procent h2.h2', { fontSize: '20px', margin: '0px', color: '#ffffff' });
  Cufon.replace('h3.productHeader');
  Cufon.replace('h1.h1')('h2.h2')('#pageLeftColTop h2.h2')('#pageRightColTop h2.h2');
  Cufon.replace('.rightTextArea a', { fontSize: '13px' });
  Cufon.replace('.rightTextArea li', { fontSize: '13px' });
   
  Cufon.replace('.page_header');
  Cufon.replace('.floatText h1'); //, { fontSize:'25px', margin: '0px', color:'#ffffff'}
  Cufon.replace('.floatText h2'); // , { fontSize: '20px', margin: '0px', color: '#ffffff' }



  
  Cufon.now();




}


function initBeregner() {


jQuery("#selVinduesType").change(function(){
    if(jQuery(this).val() == "Forsatsvindue")
     {
       jQuery("option.travindueGlas").css("display","block");
       jQuery("option.forsatsGem").css("display","none");
     }
     else
     {
        jQuery("option.travindueGlas").css("display","none");
        jQuery("option.forsatsGem").css("display","block");
     }
});

jQuery("#txtBeboetAreal").keyup(function(event) {
var tempVal = jQuery("#txtBeboetAreal").val();
if(!isNaN(tempVal))
{
jQuery("#txtVinduesAreal").val(tempVal*0.22);
}

});

  jQuery(".tipOpener").click(function () {
    var tipID = jQuery(this).attr("href").substring(jQuery(this).attr("href").indexOf("#"));


    var settingsshowMe = {
      opacity: 0.5,
      zIndex: 1999,
      animateTime: 100,
      closeOnBlockClick: true,
      onClosed: function () { jQuery(tipID).hide(); }
    };




    
    //alert(tipID)
    //alert(jQuery(tipID).length);
    jQuery(tipID).show();
    jQuery(tipID).Co3ModalWindowOpen(settingsshowMe);
  });




  jQuery("#beregnerForm").submit(function () {
    // Step 1
    if (!IsStringOK(jQuery("#txtVejnavn"))) { MarkWithError(jQuery("#txtVejnavn")); return false; }
    if (!IsStringOK(jQuery("#txtNr"))) { MarkWithError(jQuery("#txtNr")); return false; }
    if (!IsStringOK(jQuery("#txtPostNummer"))) { MarkWithError(jQuery("#txtPostNummer")); return false; }
    // Step 2
    if (!IsNumberOK(jQuery("#txtBeboetAreal"))) { MarkWithError(jQuery("#txtBeboetAreal")); return false; }
    if (!IsNumberOK(jQuery("#txtVinduesAreal"))) { MarkWithError(jQuery("#txtVinduesAreal")); return false; }
    if (!IsNumberOK(jQuery("#txtOplVinduer"))) { MarkWithError(jQuery("#txtOplVinduer")); return false; }
    if (!IsNumberOK(jQuery("#selDoors"))) { MarkWithError(jQuery("#selDoors")); return false; }
    if (!IsNumberOK(jQuery("#txtByggeAar"))) { MarkWithError(jQuery("#txtByggeAar")); return false; }
    if (!IsStringOK(jQuery("#selVarmeKilde"))) { MarkWithError(jQuery("#selVarmeKilde")); return false; }
    if (!IsStringOK(jQuery("#selVinduesType"))) { MarkWithError(jQuery("#selVinduesType")); return false; }
    if (!IsStringOK(jQuery("#selSprosser"))) { MarkWithError(jQuery("#selSprosser")); return false; }
    if (!IsStringOK(jQuery("#selGlastype"))) { MarkWithError(jQuery("#selGlastype")); return false; }
    // Step 3
    if (!IsStringOK(jQuery("#selProVinduesType"))) { MarkWithError(jQuery("#selProVinduesType")); return false; }
    


    return true;
  });

}
function MarkWithError(jObj) {
  jObj.addClass("field_error");
  jObj.focus();
}


function IsStringOK(jObj) {
  if (jObj.val().length > 0) {
    return true;
  }
  return false;
}


function IsNumberOK(jObj) {
  if (jObj.val().length > 0) {
    var RE = /^-{0,1}\d*\.{0,1}\d+$/;
    return (RE.test(jObj.val()));
  }
  return false;


  
}




function initProductPage() {


  if (jQuery("#productlist").length > 0) {
    jQuery("#printfull").css("top","110px");
  }


  jQuery("#pageLeftColTop").click(function () {
    if(jQuery("a#logo").attr("class")=="dwArea2")
      location.href = "/en-GB/Front-page/Products/PRO-TEC-7/Vinduer-PRO-TEC-7-multi.aspx";
    else
      location.href = "/da-DK/Forside/Produkter/PRO-TEC-7-multi/Vinduer-PRO-TEC-7-multi.aspx";
  });
  jQuery("#pageMiddleColTop").click(function () {
    if(jQuery("a#logo").attr("class")=="dwArea2")
      location.href = "/en-GB/Front-page/Products/PRO-TEC-Classic/Windows.aspx";
    else
      location.href = "/da-DK/Forside/Produkter/PRO-TEC-Classic/Vinduer.aspx";
  });
  jQuery("#pageRightColTop").click(function () {
    if(jQuery("a#logo").attr("class")=="dwArea2")
      location.href = "/en-GB/Front-page/Products/PRO-TEC-Classic/Vinduer-PRO-TEC-Classic.aspx";
    else
      location.href = "/da-DK/Forside/Produkter/PRO-TEC-Classic/Vinduer-PRO-TEC-Classic.aspx";
  });


  jQuery("#infoColumn").prepend(jQuery("#infoColumnHidden").children());


  if (jQuery("#tabColumn").length > 0 && jQuery("#tabContent").length == 0) {
    jQuery("#tabColumn").convertToTabs();
  }


  /* http://webbies.dk/Sudo%20Slider/settings.html#example */
  var i = 1;
  var output = "<ul id=\"prodList\" style=\"height: 301px;\"><li id=\"area0\" class=\"area\">";
  jQuery("#imageThumbOutput li").each(function () {
    if (i % 4 == 0) {
      output += "</li><li id=\"area" + i + "\" class=\"area\">" + jQuery(this).html();
    } else {
      output += jQuery(this).html();
    }
    i++;
  });
  output += "</li></ul>";


  jQuery("#SlideArea").html(output);


  if (jQuery("#SlideArea li").length <= 1) {
    jQuery("#thumbScrollUp, #thumbScrollDown").hide();
  }


  jQuery("#SlideArea").sudoSlider({
    vertical: true,
    numeric: true,
    autoheight: false,
    prevNext: true
  });




  jQuery("#thumbScrollUp").click(function () {
    $("#SlideArea").sudoSlider('prev');
    return false;
  });


  jQuery("#thumbScrollDown").click(function () {
    $("#SlideArea").sudoSlider('next');
    return false;
  });






  jQuery("#SlideArea img").CreateGallery();


  jQuery(".thumbs").each(function () {
    if (jQuery(this).attr("src").indexOf("http://") != -1 && jQuery(this).attr("src").indexOf("/Files") == 0) {
      jQuery(this).attr("src", jQuery(this).attr("src").replace("/Files", ""))
      jQuery(this).attr("imagehref", jQuery(this).attr("imagehref").replace("/Files", ""))
    }
  });


  // Tabs from headers
  // save length of children
  childLength = jQuery("#tabContent").children().length;


  jValHeaders = jQuery("<ul id=\"tabHeaders\"></ul>");
  var p = 0;
  jQuery("#tabContent").children().each(function (i) {
    if (this.tagName == "H1") {
      jVal = jQuery("<div id=\"tab" + i + "\" class=\"tab tab" + i + "\"></div>");
      jValHeaders.append("<li class=\"tabHeader zIndex" + p + "\"><a href=\"#tab" + i + "\">" + jQuery(this).text() + "</a></li>");
      p++;
    }
    jVal.append(this);
    if (this.tagName == "H1" || i == childLength - 1) {
      jQuery("#tabContent").append(jVal);
    }
  });


  jQuery("#idTabs").append(jValHeaders);
  jQuery("#idTabs").idTabs({ click: clickEvent, selected: '.active' });



}


function initGlobalFunctions() {


  if (jQuery("#contentColumn").length > 0) {
    jQuery("#rightColumnWide").height(jQuery("#contentColumn").height() + "px");
    jQuery("#inspirationTable").css("height", "100%").css("width", "100%");
  }






  // Flyt de sidste 2 menu punkter til h?jre
  var menuCount = jQuery("#nav li").length
  jQuery("#nav li").eq(menuCount - 1).css("float", "right");
  jQuery("#nav li").eq(menuCount - 2).css("float", "right");
  jQuery("#nav li").eq(menuCount - 3).css("float", "right");








  // Position sub menu to match parent item  
  if (jQuery("#subnav ul li").length > 0) {
    jQuery("#subnav").addClass("menuBorderBottom");


    var menuPriPos = jQuery("#nav li.active").position();
    var subMenuWidth = jQuery("#subnav ul").width() * 1;
    jQuery("#subnav ul").css("margin-left", menuPriPos.left + "px");
    var CalculatedSubMenuPositionWidth = subMenuWidth + jQuery("#subnav ul").css("margin-left").replace("px", "") * 1;
    if (CalculatedSubMenuPositionWidth > 926) {
      jQuery("#subnav ul").css("margin-left", 926 - subMenuWidth + "px");
    }
  }


  // fjern margin p? den sidste forsida paragraph
  jQuery("#frontpageBanners .frontpageBox:nth-child(4n)").css("margin-right", "0px");
}


function initCasePage() {
  if (jQuery("#inspirationBanners").length>0){
    jQuery("#printfull").remove();
  }
    
  if (jQuery("#chkluxury").val()==='Luxury apartments'){
    jQuery(".englishOnly").css("display","inline");
  }
  
  if (jQuery("#inspiration").length>0){
    jQuery("#contentColumn").prepend( jQuery("#printfull") );
  }


  
  if (jQuery("#imageBox").length > 0) {
    jQuery("#imageBox").easySlider({
      auto: false,
      speed: 400,
      continuous: false,
      numeric: true
    });
  }


  // S?rg for at luppen fungerer
  jQuery("#imageBox img.galImage").CreateGallery();
  jQuery("#lup").click(function () {
    var index = jQuery("#imageBox ul").css("marginLeft").replace("px", "") * -1;
    if (index > 0) {
      index = index / 614;
    }
    jQuery("#imageBox img.galImage").eq(index).click();
  });




  jQuery("#frmFilter input").click(function () {
    FilterInspiration();
  })
}


function clickEvent() {
  jQuery(this).parent().siblings().removeClass("active");
  jQuery(this).parent().addClass("active");
  return true;
}


function FilterInspiration() {
  jQuery("#inspirationBanners li").removeClass("mod4_0").hide();


  var valg = new Array();
  var typeFilt = ":checked";
  if (jQuery("#frmFilter input.inspType:checked").length <= 0) {
    typeFilt = "";
  }


  jQuery("#frmFilter input.inspType" + typeFilt).each(function () {
    valg[valg.length] = jQuery(this).val();
  })


  var valg2 = new Array();
  var omraadeFilt = ":checked";
  if (jQuery("#frmFilter input.inspOmraade:checked").length <= 0) {
    omraadeFilt = "";
  }


  jQuery("#frmFilter input.inspOmraade" + omraadeFilt).each(function () {
    valg2[valg2.length] = jQuery(this).val();
  })


  jQuery("#inspirationBanners li").each(function () {
    var insType = jQuery(this).attr("instype");
    var insOmraade = jQuery(this).attr("omraade");


    for (i = 0; i < valg.length; i++) {
      if (insType == valg[i]) {
        for (p = 0; p < valg2.length; p++) {
          if (insOmraade == valg2[p]) {
            jQuery(this).show();
          }
        }
      }
    }
  });

  jQuery("#inspirationBanners li:visible").each(function (t) {
    var i = t+1;
    if (i > 1 && i % 4 == 0) {
      jQuery(this).addClass("mod4_0");
    }
  });
}


/*
Google maps code. Obsolete
if ( jQuery("#map").length > 0 ) {
//if(GMap2){GMap2.prototype.centerAndZoomOnBounds = function(){return true;};}
jQuery('#map').jMapping( {  
map_config: function(map){
map.setUIToDefault();
map.setCenter(new GLatLng(57.007455510806804,9.65673828125), 7);
}
});}

*/
//Damngood - START
function initTopMenuContainer(){
  var jTopMenuContainer=jQuery("#topMenuContainer"),topMenuContainerWidth=30;
  jTopMenuContainer.children().each(function(){
    topMenuContainerWidth+=jQuery(this).outerWidth(true);
  });
  jTopMenuContainer.width(topMenuContainerWidth);
}
//Damngood - END
