﻿Array.prototype.unique = function () {
  var r = new Array();
  o:for(var i = 0, n = this.length; i < n; i++){
    for(var x = 0, y = r.length; x < y; x++){
      if(r[x]==this[i]){
        continue o;
      }
    }
    r[r.length] = this[i];
  }
  return r;
}


// Generer random region til start
var regioner = new Array();
regioner[0] = "";
regioner[1] = "HOVEDSTADEN";
regioner[2] = "SJAELLAND";
regioner[3] = "SYDDANMARK";
regioner[4] = "MIDTJYLLAND";
regioner[5] = "NORDJYLLAND";
var randomNum = Math.ceil(Math.random()*5);
var randomReg = regioner[randomNum];
var klikket = false;

jQuery(document).ready(function () {
  initKamp2011regioner();
  initKamp2011map();
  initKamp2011FindSog();
  jQuery("#kam2011_banner_main_imgs .kam_rot_person .txt").css("display", "none");
  jQuery("#makeMeScrollable div.scrollableArea div.kam-forhandler[region='']").hide();
  dubletTandlaeger();
  initRandomRegion();
  iniKamp2011scrolling();
});


function iniKamp2011scrolling() {
  $("div#makeMeScrollable").smoothDivScroll({
    autoScroll: "onstart", 
    autoScrollDirection: "backandforth", 
    autoScrollStep: 1, 
    autoScrollInterval: 15, 
    startAtElementId: "startAtMe", 
    visibleHotSpots: "always",
    autoScrollRightLimitReached: function() {
      jQuery("div#makeMeScrollable").smoothDivScroll("stopAutoScroll");
      setTimeout('jQuery("div#makeMeScrollable").smoothDivScroll("startAutoScroll")',1500);
    },
    autoScrollLeftLimitReached: function() {
      jQuery("div#makeMeScrollable").smoothDivScroll("stopAutoScroll");
      setTimeout('jQuery("div#makeMeScrollable").smoothDivScroll("startAutoScroll")',1500);
    }
  });

  jQuery("div#makeMeScrollable").bind("mouseover", function() {
    jQuery(this).smoothDivScroll("stopAutoScroll");
  }).bind("mouseout", function() {
    jQuery(this).smoothDivScroll("startAutoScroll");
  });

}


function initRandomRegion(reg) {
  if(reg != undefined) {
    var vreg = reg;
  } else {
    var vreg = randomReg;
  }
  jQuery("#kam2011_banner_main_imgs .kam_rot_person").hide(); // gem alle personer og quotes
  jQuery("#kam2011_banner_main_imgs .kam_rot_person[region='"+vreg+"']").show(); // vis person for regionen
  jQuery("#kam2011_banner_rotate").html(jQuery("#kam2011_banner_main_imgs .kam_rot_person[region='"+vreg+"']").find(".txt").html()); // vis quote
  if(!klikket) {
    jQuery("#regkort area[region='"+vreg+"']").click(); // vælg region på kort
  }
  jQuery("#regkortimg").attr("src","/Files/Billeder/dinTandlage2008/gfx/regkort_"+vreg+".gif");
}


function initKamp2011SlideRegions(reg) {
//  jQuery("#kam2011_banner_slider ul li").hide();
//  jQuery("#kam2011_banner_slider ul li[region='"+reg+"']").show();
  jQuery("#makeMeScrollable div.scrollableArea div.kam-forhandler").hide();
  jQuery("#makeMeScrollable div.scrollableArea div[region='"+reg+"']").show();
  var elms = jQuery("#makeMeScrollable div.scrollableArea div[region='"+reg+"']").size();
  divLength = (elms*142)+35;
  jQuery("#makeMeScrollable div.scrollableArea").css("width",divLength+"px");
  jQuery("div#makeMeScrollable").smoothDivScroll("stopAutoScroll");
//  jQuery("#makeMeScrollable").smoothDivScroll("option","startAtElementId",jQuery("#makeMeScrollable div.scrollableArea div[region='"+reg+"']:first").attr("id"));
  jQuery("#makeMeScrollable").smoothDivScroll("moveToElement", "first");
  setTimeout('jQuery("div#makeMeScrollable").smoothDivScroll("startAutoScroll")',1000);
}


function initKamp2011regioner() {
  jQuery(".regionsinfo ul li").css("display", "none");
  jQuery("#regkort area").bind("click", function() { 
    Kamp2011regionskort(jQuery(this).attr("region"),jQuery(this).attr("title"));
  });
//  dubletTandlaeger();
}

function Kamp2011regionskort(region,titel) {
  var region = region; //region.replace(/regkort_/,"");
  var titel = titel;
  jQuery(".regionsinfo h2").text(titel);
  jQuery(".regionsinfo ul li.dobb-by span").each(function(){
    jQuery(this).hide();
  });
  jQuery(".regionsinfo ul li").css("display", "none");
  jQuery(".regionsinfo ul li[region='"+region+"']").each(function(){
    jQuery(this).show();
  });
//  dubletTandlaeger()

  jQuery("#regkort_NORDJYLLAND").mouseout(function() {
    jQuery("#regkortimg").attr("src","/Files/Billeder/dinTandlage2008/gfx/regkort_"+region+".gif");
  });
  jQuery("#regkort_MIDTJYLLAND").mouseout(function() {
    jQuery("#regkortimg").attr("src","/Files/Billeder/dinTandlage2008/gfx/regkort_"+region+".gif");
  });
  jQuery("#regkort_SYDDANMARK").mouseout(function() {
    jQuery("#regkortimg").attr("src","/Files/Billeder/dinTandlage2008/gfx/regkort_"+region+".gif");
  });
  jQuery("#regkort_SJAELLAND").mouseout(function() {
    jQuery("#regkortimg").attr("src","/Files/Billeder/dinTandlage2008/gfx/regkort_"+region+".gif");
  });
  jQuery("#regkort_HOVEDSTADEN").mouseout(function() {
    jQuery("#regkortimg").attr("src","/Files/Billeder/dinTandlage2008/gfx/regkort_"+region+".gif");
  });

  if(klikket) {
    initRandomRegion(region);
    initKamp2011SlideRegions(region);
  }
  klikket = true;
}

function initKamp2011map() {
  var selreg = "";
  if(jQuery("#regkort").length > 0) {
    jQuery("#regkort_NORDJYLLAND").mouseover(function() {
      jQuery("#regkortimg").attr("src","/Files/Billeder/dinTandlage2008/gfx/regkort_NORDJYLLAND.gif");
    });
    jQuery("#regkort_MIDTJYLLAND").mouseover(function() {
      jQuery("#regkortimg").attr("src","/Files/Billeder/dinTandlage2008/gfx/regkort_MIDTJYLLAND.gif");
    });
    jQuery("#regkort_SYDDANMARK").mouseover(function() {
      jQuery("#regkortimg").attr("src","/Files/Billeder/dinTandlage2008/gfx/regkort_SYDDANMARK.gif");
    });
    jQuery("#regkort_SJAELLAND").mouseover(function() {
      jQuery("#regkortimg").attr("src","/Files/Billeder/dinTandlage2008/gfx/regkort_SJAELLAND.gif");
    });
    jQuery("#regkort_HOVEDSTADEN").mouseover(function() {
      jQuery("#regkortimg").attr("src","/Files/Billeder/dinTandlage2008/gfx/regkort_HOVEDSTADEN.gif");
    });

  }
}



function initKamp2011FindSog() {
  jQuery("#kam2011sog").bind("click", function() {
    initKamp2011FindSogAjax();
  });

  jQuery("#kam2011q").keyup(function(e) {
    if(e.keyCode == 13) {
      initKamp2011FindSogAjax();
    }
  });
}


function initKamp2011FindSogAjax() {
  var q = jQuery("#kam2011q").val();

  // validering af input
  error = 0;

  // Tjekker om feltet indeholder tal  
  if(!q.match(/^[0-9\+ ]+$/) && (error==0)){
    alert("Postnummer skal være et tal.");
    jQuery("#kam2011q").focus();
    error = 1;
  } 
  if(error == 0) {
    pageTracker._trackEvent('Forside', 'Søg', q);
    jQuery("#kam2011findRes").load("Default.aspx?ID=6529&Purge=True&Dealerq="+q+" #kam2011ajaxList", function() { 
      if(jQuery("#kam2011ajaxList").html()==="") {
        jQuery("#kam2011ajaxList").html("<li><em>Der er ingen dinTANDLÆGE i dette postnummer.</em></li>");
      }
      reArrKamp2011FindSogAjax();
    }); 
  }
}


function dubletTandlaeger() {
  var dobbbyer = [];
  jQuery(".regionsinfo ul li[dubletby='JA']").each(function(){
    dobbbyer.push(jQuery(this).attr("by"))
  });
  dobbbyer = dobbbyer.unique();
  for(i=0;i<dobbbyer.length;i++) {
    jQuery(".regionsinfo ul li[by='"+dobbbyer[i]+"']:first").after("<li class='kam-region_forhandler dobb-by' region='"+jQuery(".regionsinfo ul li[by='"+dobbbyer[i]+"']:first").attr("region")+"' by='"+dobbbyer[i]+"'><a href='#'>dinTANDLÆGE "+ dobbbyer[i] + "</a><br /><span></span></li>");
  }

  jQuery(".regionsinfo ul li[dubletby='JA']").each(function(){
    jQuery(".regionsinfo ul li.dobb-by[by='"+jQuery(this).attr("by")+"']").find("span").append("- "+jQuery(this).html()+"<br />");
    jQuery(this).remove();
  });

  jQuery("li.dobb-by").bind("click", function() {
    jQuery(".regionsinfo ul li.dobb-by span").each(function(){
      jQuery(this).hide();
    });
    jQuery(this).find("span").show();
  });
}


function findTandlaege() {
  var dtID = document.getElementById('FindTandlaegeDealerID').value;
  if(dtID == "0" || dtID == "" || dtID == "84") {
    location.href = "Default.aspx?ID=4358";
  } else {
    location.href = "Default.aspx?ID=4358&DealerID="+dtID;
  }
}

function findTandlaegeNy() {
  var url = jQuery("#FindTandlaegeDealerID").val();
  var dtID = "";
  if(url.indexOf("DealerID")) {
    dtID = url.split("DealerID=")[1];
  } else {
    dtID = "0";
  }
  pageTracker._trackEvent('Find dinTANDLÆGE box', url);

  if(dtID == "0" || dtID == "" || dtID == "84") {
    location.href = "Default.aspx?ID=4358";
  } else {
    location.href = url;
  }
}


function reArrKamp2011FindSogAjax() {
  jQuery("#kam2011ajaxList li").each(function(){
    var soegtPostNr = jQuery("#kam2011q").val();
    var OrgPostNr = jQuery(this).attr("OrgPostNr");
    if(soegtPostNr === OrgPostNr) {
      jQuery(this).prependTo("#kam2011ajaxList");
    }
  });
}
