﻿$(document).ready(function () {
  $(".changelang").click(function () {
    $(".selector").slideToggle("fast");
  });

  $(".navitem").mouseover(function () {
    $(this).addClass("temp_active");
  });

  $(".navitem").mouseout(function () {
    $(this).removeClass("temp_active");
  });

  $(".homepage .content_block_header").click(function () {
    window.location.href = $(this).find("a").attr("href");
  });

  $(".homelink").click(function () {
    window.location.href = "http://www.westcordhotels.nl/";
  });

  $(".notifyGac").click(function () {
    notifyGac($(this).attr("href"));
    return false;
  });

  $(".hotelselector .item").click(function () {
    $(".hotelselector .item").removeClass("active");
    $(this).addClass("active");
    var filter = $(this).text();
    $(".loc_stad").show();
    $(".loc_wad").show();
    if (filter == "Wad") {
      $(".loc_stad").hide();
    }
    if (filter == "Stad") {
      $(".loc_wad").hide();
    }
  });

  $("a.gallery").fancybox();
  $("a.fancybox").fancybox();

  $("#mainHeader").randomClass(['header_1', 'header_2', 'header_3', 'header_5']);

  $(".search").submit(function () {
    window.location.href = "/Search?q=" + $(".searchbox").val();
    return false;
  });

  $.datepicker.regional['nl'] = {
    closeText: 'Sluiten',
    prevText: '←',
    nextText: '→',
    currentText: 'Vandaag',
    monthNames: ['januari', 'februari', 'maart', 'april', 'mei', 'juni', 'juli', 'augustus', 'september', 'oktober', 'november', 'december'],
    monthNamesShort: ['jan', 'feb', 'maa', 'apr', 'mei', 'jun', 'jul', 'aug', 'sep', 'okt', 'nov', 'dec'],
    dayNames: ['zondag', 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag'],
    dayNamesShort: ['zon', 'maa', 'din', 'woe', 'don', 'vri', 'zat'],
    dayNamesMin: ['zo', 'ma', 'di', 'wo', 'do', 'vr', 'za'],
    weekHeader: 'Wk',
    dateFormat: 'dd-mm-yy',
    firstDay: 1,
    isRTL: false,
    showMonthAfterYear: false,
    yearSuffix: ''
  };

  $.datepicker.setDefaults($.datepicker.regional['nl']);

  $.datepicker.setDefaults({ minDate: new Date() });

  var dates = $("#arrival").datepicker({
    defaultDate: "+1w",
    changeMonth: true,
    numberOfMonths: 2,
    showOn: "both",
    buttonImage: "../content/images/calendar.gif",
    buttonImageOnly: true,
    onSelect: function (selectedDate) {
      var option = this.id == "arrival" ? "minDate" : "maxDate",
					instance = $(this).data("datepicker"),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings);
      dates.not(this).datepicker("option", option, date);
      $("#departure").datepicker("setDate", date.AddDays(1));
    }
  });

  var dates = $("#departure").datepicker({
    defaultDate: "+1w",
    changeMonth: true,
    numberOfMonths: 2,
    showOn: "both",
    buttonImage: "../content/images/calendar.gif",
    buttonImageOnly: true,
    onSelect: function (selectedDate) {
      var option = this.id == "arrival" ? "minDate" : "maxDate",
					instance = $(this).data("datepicker"),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings);
      dates.not(this).datepicker("option", option, date);
    }
  });

  var fmDate = new Date();
  var tmDate = new Date().AddDays(1);

  var fmMonth = fmDate.getMonth() + 1;
  var tmMonth = tmDate.getMonth() + 1;

  $("#arrival").val(fmDate.getDate().toString().makeTwo() + "-" + fmMonth.toString().makeTwo() + "-" + fmDate.getFullYear());
  $("#departure").val(tmDate.getDate().toString().makeTwo() + "-" + fmMonth.toString().makeTwo() + "-" + tmDate.getFullYear());

  $("#rbRoom").change(function () {
    $("#listHotel").show();
    $("#listAppartment").hide();
  });

  $("#rbAppartment").change(function () {
    $("#listHotel").hide();
    $("#listAppartment").show();
    var myOptions = {
      '25776': 'Strandhotel Seeduyn',
      '25764': 'Residentie Vlierijck',
      '25763': 'Residentie Boschrijck',
      '25780': 'Hotel Noordsee',
      '25658': 'WTC Hotel Leeuwarden',
      '25881': 'Hotel Delft'
    };
    fillSelect("#listAppartment", myOptions);
  });

  fillHotels();

  // filter hotels by location
  $("#listLocatie").change(function () {

    selValue = $(this).val();

    if (selValue == "0") {
      fillHotels();

      var myOptions = {
        '25776': 'Strandhotel Seeduyn',
        '25764': 'Residentie Vlierijck',
        '25763': 'Residentie Boschrijck',
        '25780': 'Hotel Noordsee',
        '25658': 'WTC Hotel Leeuwarden',
        '25881': 'Hotel Delft'
      };
      fillSelect("#listAppartment", myOptions);

    }
    else {
      $("#listHotel option").remove();
      $("#listAppartment option").remove();
    }

    if (selValue == "VLIE") {
      var myOptions = {
        '0': 'Alle hotels',
        '25776': 'Strandhotel Seeduyn',
        '25764': 'Residentie Vlierijck',
        '25756': 'Hotel de Wadden'
      };
      fillSelect("#listHotel", myOptions);

      var myOptions = {
        '25776': 'Strandhotel Seeduyn',
        '25764': 'Residentie Vlierijck'
      };
      fillSelect("#listAppartment", myOptions);
    }

    if (selValue == "TERS") {
      var myOptions = {
        '0': 'Alle hotels',
        '25763': 'Residentie Boschrijck',
        '25762': 'Hotel Schylge'
      };
      fillSelect("#listHotel", myOptions);

      var myOptions = {
        '25763': 'Residentie Boschrijck'
      };
      fillSelect("#listAppartment", myOptions);

    }
    if (selValue == "25780") {
      var myOptions = {
        '25780': 'Hotel Noordsee'
      };
      fillSelect("#listHotel", myOptions);

      var myOptions = {
        '25780': 'Hotel Noordsee'
      };
      fillSelect("#listAppartment", myOptions);
    }

    if (selValue == "AMS") {
      var myOptions = {
        '0': 'Alle hotels',
        '25754': 'Art Hotel Amsterdam ***',
        '25755': 'Art Hotel Amsterdam ****',
        '25809': 'City Centre Hotel Amsterdam',
        '25657': 'Fashion Hotel Amsterdam'
      };
      fillSelect("#listHotel", myOptions);

      var myOptions = {
        '0': 'Geen'
      };
      fillSelect("#listAppartment", myOptions);
    }

    if (selValue == "25773") {
      var myOptions = {
        '25773': 'Hotel New York'
      };
      fillSelect("#listHotel", myOptions);

      var myOptions = {
        '0': 'Geen'
      };
      fillSelect("#listAppartment", myOptions);

    }

    if (selValue == "25685") {
      var myOptions = {
        '25658': 'WTC Hotel Leeuwarden'
      };
      fillSelect("#listHotel", myOptions);

      var myOptions = {
        '25658': 'WTC Hotel Leeuwarden'
      };
      fillSelect("#listAppartment", myOptions);
    }

    if (selValue == "25881") {
      var myOptions = {
        '25881': 'Hotel Delft'
      };
      fillSelect("#listHotel", myOptions);

      var myOptions = {
        '25881': 'Hotel Delft'
      };
      fillSelect("#listAppartment", myOptions);

    }

    if ($("#listHotel").is(":visible")) {
      $("#listHotel").fadeOut("fast");
      $("#listHotel").fadeIn("fast");
    } else {
      $("#listAppartment").fadeOut("fast");
      $("#listAppartment").fadeIn("fast");
    }

  });

  $(".booknow").click(function () {
    var keuze;

    if ($("#listHotel").is(":visible")) {
      keuze = $("#listHotel").val();
    }
    else {
      keuze = $("#listAppartment").val();
    }

    if (IsNumeric(keuze)) {
      keuze = "Hotel=" + keuze;
    }
    else {
      keuze = "DEST=" + keuze;
    }

    var arrive = $("#arrival").val();
    var departure = $("#departure").val();

    var adult = $("#adult").val();
    var child = $("#child").val();
    var rooms = $("#rooms").val();

    var locale = "nl-NL";

    var arriveSplitted = arrive.split('-');
    var departSplitted = departure.split('-');

    var oneDay = 1000 * 60 * 60 * 24;
    var firstDate = new Date(arriveSplitted[2], arriveSplitted[1] - 1, arriveSplitted[0]);
    var secondDate = new Date(departSplitted[2], departSplitted[1] - 1, departSplitted[0]);

    var verschil = 0;

    if (firstDate < secondDate) {
      verschil = Math.abs(firstDate.getTime() - secondDate.getTime());
      verschil = Math.round(verschil / oneDay)
    }

    if (firstDate > secondDate)
      verschil = -1;

    if (keuze == 'Hotel=0') {
      keuze = "DEST=" + $("#listLocatie").val();
    }

    if (verschil >= 0) {
      var extLink = "https://gc.synxis.com/rez.aspx?" + keuze + "&chain=10024&arrive=" + arrive + "&nights=" + verschil + "&start=1&adult=" + adult + "&child=" + child + "&rooms=" + rooms + "&promo=" + $("#corporateLogin").val() + "&locale=" + locale;
      notifyGac(extLink);
      window.location.href = extLink;
    }
    else {
      alert("Ongeldige aankomst / vertrek datum opgegeven!");
    }

  });

  $(".tab_content").hide();
  $("ul.tabs li:first").addClass("active").show();
  $(".tab_content:first").show();

  $("ul.tabs li").click(function () {

    $("ul.tabs li").removeClass("active");
    $(this).addClass("active");
    $(".tab_content").hide();

    var activeTab = "#" + $(this).find("a").attr("rel");
    $(activeTab).fadeIn();

    return false;
  });


  $(".navitem").mouseenter(function () {
    $(this).find("ul").fadeIn("fast");
  });
  $(".navitem").mouseleave(function () {
    $(this).find("ul").fadeOut("fast");
  });

  $(".navigation li ul li").mouseover(function () {
    $(this).addClass("active");
  });
  $(".navigation li ul li").mouseleave(function () {
    $(this).removeClass("active");
  });

  $(".booknow").mouseover(function () {
    $(this).addClass("booknowactive");
    $(this).removeClass(".booknow");
  });

  $(".booknow").mouseout(function () {
    $(this).removeClass("booknowactive");
    $(this).addClass(".booknow");
  });

  $(".hovereffect").mouseover(function () {
    $(this).css("background-color", "#fff");
    $(this).css("border", "1px #648eb7 solid");
    $(this).find(".button").css("background-position", "0% 0%");
    $(this).find(".omschrijving").css("border-right", "1px #e6e6e6 solid");
  });

  $(".hovereffect").mouseout(function () {
    $(this).css("background-color", "#eaf2f9");
    $(this).find(".button").css("background-position", "0% 100%");
    $(this).css("border", "1px #eaf2f9 solid");
    $(this).find(".omschrijving").css("border-right", "1px #fff solid");
  });

  $(".header-hovereffect").mouseover(function () {
    $(this).css("background-position", "0% 50%");
  });

  $(".header-hovereffect").mouseout(function () {
    $(this).css("background-position", "0% 0%");
  });
  $(".button_reserveren").mouseover(function () {
    $(this).css("background-position", "0% 0%");
  });

  $(".button_reserveren").mouseout(function () {
    $(this).css("background-position", "0% 100%");
  });

  toggleNext(0, 0);

  $(".attention").mouseover(function () {
    $(this).addClass("active");
  });
  $(".attention").mouseout(function () {
    $(this).removeClass("active");
  });

  $(".intro").find("iframe").remove();

  $(".block").mouseover(function () {
    $(this).css("background-color", "#468cb9");
  });

  $(".block").mouseout(function () {
    $(this).css("background-color", "#347cab");
  });

  $(".hotel .omschrijving, .arrangement .omschrijving, .locatie .omschrijving, .hotel .omschrijving_tab").jTruncate({
    length: 230,
    minTrail: 20,
    moreText: "",
    lessText: "",
    ellipsisText: " ...",
    moreAni: "fast",
    lessAni: "fast"
  });

  $(".arrangement").click(function () {
    window.location.href = "/arrangementen/" + $(this).attr("rel");
  });

  $(".locatie").click(function () {
    window.location.href = "/" + $(this).attr("rel");
  });

  $(".block").click(function () {
    window.location.href = $(this).find("a").attr("href");
  });

  $(".attention").click(function () {
    window.location.href = $(this).find("a").attr("href");
  });

  $(".content_block_inner").click(function () {
    window.location.href = $(this).find("a").attr("href");
  });

  $(".hotel").click(function () {
    window.location.href = "/hotels/" + $(this).attr("rel");
  });

  $("#corporateLoginBox").click(function () {
    $("#corporateLogin").toggle();
  });

  $("#listHotel").change(function () {
    var stad = "25754,25755,25809,25881,25657,25773,25658";
    var wad = "25763,25780,25762,25776,25764,25756";

    if (stad.contains($(this).val())) {
      $("#adult").val("1");
    }
    else {
      $("#adult").val("2");
    }

  });

  var tags = $(".tags").text().split(",");
  $(".tags").html("");
  jQuery.each(tags, function () {
    if (trim(this) != "") {
      $(".tags").append("<div class=\"tag\"><a href=\"/Search?q=" + trim(this) + "\">" + trim(this) + "</a></div>");
    }
  });

  $(".vacature_header").click(function () {
    $("#content_" + $(this).attr("id")).slideToggle("fast");
  });


  var querystring = location.search.replace('?', '').split('&');
  var queryObj = {};
  for (var i = 0; i < querystring.length; i++) {
    var name = querystring[i].split('=')[0];
    var value = querystring[i].split('=')[1];
    queryObj[name] = value;
  }

  if (queryObj["filter"] == "true") {
    var filter_wadstad = queryObj["f"];
    var filter_cat = queryObj["c"];
    var filter_hot = queryObj["h"];

    if (filter_wadstad.length > 0) {
      elem = $("input[name=wadstad]")[filter_wadstad];
      if (elem != null) {
        $("#" + elem.id).attr("checked", "checked");
      }
    }

    if (filter_cat.length > 0) {
      elem = $(".arr_cat option[value='" + filter_cat + "']");
      if (elem != null) {
        $(".arr_cat option[value='" + filter_cat + "']").attr('selected', 'selected');
      }
    }

    if (filter_hot.length > 0) {
      elem = $(".arr_hotel option[value='" + filter_hot + "']");
      if (elem != null) {
        $(".arr_hotel option[value='" + filter_hot + "']").attr('selected', 'selected');
      }
    }

    startFilter();
  }


});

String.prototype.contains = function (it) { return this.indexOf(it) != -1; };
String.prototype.makeTwo = function () {
  if (this.length == 1)
    return "0" + this
  else
    return this;
};

Date.prototype.AddDays = function (days) {
  this.setDate(this.getDate() + days);
  return this;
}

function fillHotels() {
  var myOptions = {
    '0': 'Alle hotels',
    '25754': 'Art Hotel Amsterdam ***',
    '25755': 'Art Hotel Amsterdam ****',
    '25763': 'Residentie Boschrijck',
    '25809': 'City Centre Hotel Amsterdam',
    '25881': 'Hotel Delft',
    '25657': 'Fashion Hotel Amsterdam',
    '25773': 'Hotel New York',
    '25780': 'Hotel Noordsee',
    '25762': 'Hotel Schylge',
    '25776': 'Strandhotel Seeduyn',
    '25764': 'Residentie Vlierijck',
    '25756': 'Hotel de Wadden',
    '25658': 'WTC Hotel Leeuwarden'
  };

  fillSelect("#listHotel", myOptions);
}


function fillSelect(elementId, arrOptions) {
  $(elementId + " option").remove();
  $.each(arrOptions, function (val, text) { $(elementId).append($('<option></option>').val(val).html(text)); });
}

function toggleNext(current,previous) {

  currentElement = current;
  previousElement = previous;

  if (currentElement < 0) {
    $("#elem_" + previous).slideUp();
    $("#elem_0").slideDown();
  } else {
    $("#elem_" + previous).slideUp();
    $("#elem_" + current).slideDown();
  }

  nextElement = currentElement + 1;
  indexNextElement = $("#elem_" + nextElement).index();

  if (indexNextElement < 0) {
    nextElement = -1;
  }

  setTimeout(function () { toggleNext(nextElement,currentElement) }, 7000);
}

function SetHeaderImage(strImage) {
  if (strImage.length > 0) {
    $(".header").css("background-image", "url('http://www.westcordhotels.nl/documenten/" + strImage + "')");
  }
}

function IsNumeric(sText) {
  var ValidChars = "0123456789.";
  var IsNumber = true;
  var Char;


  for (i = 0; i < sText.length && IsNumber == true; i++) {
    Char = sText.charAt(i);
    if (ValidChars.indexOf(Char) == -1) {
      IsNumber = false;
    }
  }
  return IsNumber;

}

function notifyGac(link) {
 _gaq.push(['_link', link])
}

(function ($) {
  $.fn.jTruncate = function (options) {

    var defaults = {
      length: 300,
      minTrail: 20,
      moreText: "more",
      lessText: "less",
      ellipsisText: "...",
      moreAni: "",
      lessAni: ""
    };

    var options = $.extend(defaults, options);

	try{
    return this.each(function () {
      obj = $(this);
      var body = obj.html();

      if (body.length > options.length + options.minTrail) {
        var splitLocation = body.indexOf(' ', options.length);
        if (splitLocation != -1) {
          // truncate tip
          var splitLocation = body.indexOf(' ', options.length);
          var str1 = body.substring(0, splitLocation);
          var str2 = body.substring(splitLocation, body.length - 1);
          obj.html(str1);

          // insert more link
          obj.append(
						'<div class="clearboth">' +
							'<a href="#" class="truncate_more_link">' + options.moreText + '</a>' +
						'</div>'
					);

          // set onclick event for more/less link
          var moreLink = $('.truncate_more_link', obj);
          var moreContent = $('.truncate_more', obj);
          var ellipsis = $('.truncate_ellipsis', obj);
          moreLink.click(function () {
            if (moreLink.text() == options.moreText) {
              moreContent.show();
              moreLink.text(options.lessText);
              ellipsis.css("display", "none");
            } else {
              moreContent.hide();
              moreLink.text(options.moreText);
              ellipsis.css("display", "inline");
            }
            return false;
          });
        }
      } // end if

    });
    }catch(err){
    	alert("Truncate error"); 
    }
  };
  
})(jQuery);

/*
 * RandomClass - 	A jQuery plugin to add a random class from a selection of classes to an element.
 * 
 * Copyright (c) 2010 Fredi Bach
 * www.fredibach.ch
 *
 * Usage:

	$("p span").randomClass( [ 'color1','color2','color3' ] );
 
 * Or with options:

	$("p span").randomClass( [ 'color1','color2','color3' ], { randomness: 'pattern', removeClasses: true } );

 * Plugin page: http://fredibach.ch/jquery-plugins/randomclass.php
 *
 */
(function($) {
	
	$.fn.randomClass=function(classes,settings){
		var defaults = {
			randomness: 'default',
			removeClasses: false
		};
		var s = $.extend(defaults, settings);
		
		if (s.randomness == 'pattern'){
			classes = shuffle(classes);
		}
		
		var p = -1;
		var cnt = 0;
		
		this.each( function() {
			if (s.removeClasses){
				for (c in classes){
					var cn = classes[c];
					if ( $(this).hasClass(cn) ){
						$(this).removeClass(cn);
					}
				}
			}
			if (s.randomness == 'pattern'){
				cnt++;
				$(this).addClass(classes[cnt%classes.length]);
			} else {
				do {
					var r = Math.floor(Math.random() * classes.length);
				} while (p == r && s.randomness == 'successive');
				
				$(this).addClass(classes[r]);
				p = r;
			}
		});
		
		shuffle = function(o){
			for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
			return o;
		};
		
		return this;
	};
	
})(jQuery);


function trim(str, chars) {
  return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


