﻿$(document).ready(function () {

  // image gallery preset
  if ($(".aFileInfo").length > 0) {
    $("#divMainImage").css("background", "url('" + $(".aFileInfo").first().attr('path') + "') no-repeat right center");
    //debugger;
    $("#divMainImage").attr('href', $(".aFileInfo").first().attr('category') + '?cat=' + $(".aFileInfo").first().attr('categoryId'));
    $("#divMainImage").attr('title', $(".aFileInfo").first().attr('category'));
    $("img", $(".aFileInfo").first()).css('display', 'block');
    //$("#divProductFiles").css("background", "#fff url('" + $(".aFileInfo").first().attr("path") + "') no-repeat right center");
  }

  // offers
  hideAllOffers();
  $('#formTop').show();
});

function ShowHideElement(id, val) {
  var element = document.getElementById(id);
  if (element != null) {
    element.style.display = val;
  }
}

function ExpandColapse(imgContId, id) {
  var src = document.getElementById(imgContId).src;
  var divElement = document.getElementById(id);
  if ($(divElement) != null) {
    if (src.indexOf('Resources/Images/icon_colapsed.png') == -1) {
      src = baseURL + 'Resources/Images/icon_colapsed.png';
      document.getElementById(imgContId).src = src;
      $(divElement).slideUp("slow");
    } else {
      src = baseURL + 'Resources/Images/icon_expanded.png';
      document.getElementById(imgContId).src = src;
      $(divElement).slideToggle("slow");
    }
  }

}

function searchClick(qStrKey, id) {
  var pathname = window.location.href;
  if (pathname.indexOf('?') < 0) { pathname = pathname + '?' }
  var paramValue = getParameterByName(qStrKey);
  if (!$(id).val() == "") {
    if (paramValue != null) { window.location = (pathname.replace(paramValue, $(id).val())).replace(/&&/g, "&"); }
    else {
      window.location = (pathname + "&" + qStrKey + "=" + $(id).val()).replace(/&&/g, "&");
    }
  }
  else {
    window.location = pathname.replace(/&&/g, "&").replace(qStrKey + "=" + paramValue, "").replace(/&&/g, "&");

  }
}

function clickSearch(e, me, postUrl) {
  //debugger;
  if (!e) var e = window.event;
  if (e.keyCode == 13) {
    PostbackSearchButton3(postUrl, $(me).val());
  }
}

function searchPriceClick(idMin, idMax) {
  var pathname = window.location.href;
  var finalPathName = '';
  if (pathname.indexOf('?') < 0) { pathname = pathname + '?' }
  var qStrKey1 = 'fromprice'
  var qStrKey2 = 'toprice'
  var paramValue1 = getParameterByName(qStrKey1);
  var paramValue2 = getParameterByName(qStrKey2);
  if (!$(idMin)[0].outerText == "") {
    if (paramValue1 != null) { pathname = (pathname.replace(paramValue1, $(idMin)[0].outerText)).replace(/&&/g, "&"); }
    else {
      pathname = (pathname + "&" + qStrKey1 + "=" + $(idMin)[0].outerText).replace(/&&/g, "&");
    }
  }
  else {
    pathname = pathname.replace(/&&/g, "&").replace(qStrKey1 + "=" + paramValue1, "").replace(/&&/g, "&");

  }

  if (!$(idMax)[0].outerText == "") {
    if (paramValue2 != null) { pathname = (pathname.replace(paramValue2, $(idMax)[0].outerText)).replace(/&&/g, "&"); }
    else {
      pathname = (pathname + "&" + qStrKey2 + "=" + $(idMax)[0].outerText).replace(/&&/g, "&");
    }
  }
  else {
    pathname = pathname.replace(/&&/g, "&").replace(qStrKey2 + "=" + paramValue2, "").replace(/&&/g, "&");

  }

  window.location = pathname;
}

function getParameterByName(name) {

  var match = RegExp('[?&]' + name + '=([^&]*)')
                    .exec(window.location.search);

  return match && decodeURIComponent(match[1].replace(/\+/g, ' '));

}


function ShowHidePanel(Id) {

  var divId = document.getElementById(Id);
  if ($(divId) != null) {
    if ($(divId).is(":visible")) {
      $(divId).slideUp("slow");
    } else { $(divId).slideDown("slow"); }

  }

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//postback functions

function redirectToHomePage(homePageUrl) {
  //debugger;
  var curentLocation = window.location.href;
  var lastind = curentLocation.substring(curentLocation.lastIndexOf("/") + 1, curentLocation.length);
  if (lastind == '/') {
    lastind = lastind.substring(0, lastind.lastIndexOf("/"));
    lastind = lastind.substring(lastind.lastIndexOf("/") + 1, lastind.length);
  }

  if ((lastind.toUpperCase() != "HOME") && window.location.href.indexOf(homePageUrl) == -1) {
    var path = setSearchUrl(homePageUrl);
    window.location = path;
  }
}

function PostbackSearchButton(urlpost, crtPage, urlpost1, urlpost2, urlpost3) {
  if (crtPage == null) { crtPage = 1; }
  $("#ProgressDialog").dialog("open");
  $.post(urlpost, {
    // searchWord: document.getElementById('txtSearch').value,
    minprice: document.getElementById('spanMinValue').innerHTML,
    maxprice: document.getElementById('spanMaxValue').innerHTML,
    reg: document.getElementById('hiddenSearchRegionCode').value,
    city: document.getElementById('hiddenSearchCity').value,
    fromdate: document.getElementById('txtSearchDateFrom').value,
    todate: document.getElementById('txtSearchDateTo').value,
    theme: getThemesForSearch(),
    cat: document.getElementById('selectedCat').value,
    page: crtPage,
    months: document.getElementById('hiddenMonths').value,
    promo: getPromoValue(), //document.getElementById('hiddenIsPromo').value,
    sort: getSortValue()
  },
  function (response) {
    $("#ProgressDialog").dialog("close");
    $("#divResults").html(response);
  })
  return false;
}

function PostbackSearchButton2(urlpost, searchWord) {
  //debugger;
  var queryStrings = window.location.href.toString();
  urlpost += '?search=true&promo=1';
  if (queryStrings.indexOf('&') != -1) { urlpost += queryStrings.substring(queryStrings.indexOf('&'), queryStrings.length); }
  else if (queryStrings.indexOf('?') != -1) { urlpost += '&' + queryStrings.substring(queryStrings.indexOf('?') + 1, queryStrings.length); }
  window.location = urlpost;
}

function PostbackSearchButton3(urlpost, searchWord) {
  // debugger;
  urlpost += '?search=true&searchWord=' + searchWord;
  window.location = urlpost;
}

function PostbackSearchButton4(urlpost, category) {
  //debugger;
  var queryStrings = window.location.href.toString();

  if (category != '') { urlpost += '?search=true&promo=3&cat=' + category }
  else { urlpost += '?search=true&promo=4' }
  window.location = urlpost;
}

function PostbackSearchButton5(urlpost, category) {
  //debugger;
  var queryStrings = window.location.href.toString();
  urlpost += '?search=true&promo=3'
  if (category != '') { urlpost += '&cat=' + category; }
  window.location = urlpost;
}

function PostbackMainSearchButton(urlpost) {
  //debugger;
  var path = setSearchUrl(urlpost);
  window.location = path;
}

function PostbackSearchItems(urlpost) {
  $("#ProgressDialog").dialog("open");
  //debugger;
  var myCategory = document.getElementById('selectedCat').value;
  if (getParameterByName('cat') != null) myCategory = getParameterByName('cat');

  $.post(urlpost, {
    minprice: document.getElementById('spanMinValue').innerHTML,
    maxprice: document.getElementById('spanMaxValue').innerHTML,
    country: document.getElementById('hiddenSearchCountry').value,
    reg: document.getElementById('hiddenSearchRegionCode').value,
    city: document.getElementById('hiddenSearchCity').value,
    period: document.getElementById('hiddenSearcPeriod').value,
    fromdate: document.getElementById('txtSearchDateFrom').value,
    todate: document.getElementById('txtSearchDateTo').value,
    cat: myCategory,
    theme: getThemesForSearch(),
    months: document.getElementById('hiddenMonths').value
  },
  function (response) {
    $("#ProgressDialog").dialog("close");
    $("#divSearchThemesExt").html(response);
    $("#divSearchGlobalTop").html(response);
  })
}

function setSearchUrl(searchURL) {
  searchURL += '?search=true';
  if (document.getElementById('hiddenSearchCountry').value !== '' && document.getElementById('hiddenSearchCountry').value != 'Zoek op land' && document.getElementById('hiddenSearchCountry').value !== 'Zoek op land') searchURL += '&country=' + document.getElementById('hiddenSearchCountry').value;
  if (document.getElementById('hiddenSearchRegionCode').value !== '' && document.getElementById('hiddenSearchRegionCode').value != 'Zoek op skigebied' && document.getElementById('hiddenSearchRegionCode').value !== 'Zoek op region') searchURL += '&region=' + document.getElementById('hiddenSearchRegionCode').value;
  if (document.getElementById('hiddenSearchCity').value !== '' && document.getElementById('hiddenSearchCity').value != 'Zoek op skidorp') searchURL += '&city=' + document.getElementById('hiddenSearchCity').value;
  if (document.getElementById('hiddenSearcPeriod').value !== '' && document.getElementById('hiddenSearcPeriod').value != 'Zoek op periode') searchURL += '&period=' + document.getElementById('hiddenSearcPeriod').value;
  if (document.getElementById('spanMinValue').innerHTML !== '') searchURL += '&minprice=' + document.getElementById('spanMinValue').innerHTML;
  if (document.getElementById('spanMaxValue').innerHTML !== '') searchURL += '&maxprice=' + document.getElementById('spanMaxValue').innerHTML.toString().replace(" ", "");
  if (document.getElementById('hiddenMonths').value !== '') searchURL += '&months=' + document.getElementById('hiddenMonths').value;
  if (document.getElementById('txtSearchDateFrom').value !== '') searchURL += '&fromdate=' + document.getElementById('txtSearchDateFrom').value;
  if (document.getElementById('txtSearchDateTo').value !== '') searchURL += '&todate=' + document.getElementById('txtSearchDateTo').value;
  if (document.getElementById('selectedCat').value !== '') searchURL += '&cat=' + document.getElementById('selectedCat').value;
  var valueToReturn = getThemesForSearch();
  if (valueToReturn != '') searchURL += "&theme=" + valueToReturn;
  if (document.getElementById('hiddenIsPromo') != null && document.getElementById('hiddenIsPromo').value != '' && document.getElementById('hiddenIsPromo').value != '0') searchURL += '&promo=' + getPromoValue();
  return searchURL;
}

function PostbackPageButton(me, urlpost, crtPage) {
  if (crtPage == null) { crtPage = 1; }
  urlpost += '?r=' + Math.random;
  if (!$(me).hasClass('nav-disabled')) $("#ProgressDialog").dialog("open");
  var myCategory = document.getElementById('selectedCat').value;
  if (getParameterByName('cat') != null) myCategory = getParameterByName('cat');
  $.post(urlpost, {
    //searchWord: document.getElementById('txtSearch').value,
    minprice: document.getElementById('spanMinValue').innerHTML,
    maxprice: document.getElementById('spanMaxValue').innerHTML,
    country: document.getElementById('hiddenSearchCountry').value,
    reg: document.getElementById('hiddenSearchRegionCode').value,
    region: document.getElementById('hiddenSearchRegionCode').value,
    city: document.getElementById('hiddenSearchCity').value,
    period: document.getElementById('hiddenSearcPeriod').value,
    fromdate: document.getElementById('txtSearchDateFrom').value,
    todate: document.getElementById('txtSearchDateTo').value,
    theme: getThemesForSearch(),
    cat: myCategory,
    page: crtPage,
    months: document.getElementById('hiddenMonths').value,
    promo: getPromoValue(),
    sort: getSortValue(),
    selToCmpare: selectedToCompare
  },
  function (response) { $("#ProgressDialog").dialog("close"); $("#divResults").html(response); })
}

function getThemesForSearch() {
  var i;
  var valueToReturn = '';
  for (i = 1; i <= 4; i++) {
    span = document.getElementById(i);
    if (span != null) {
      if (span.style.display == 'block') {
        valueToReturn += i.toString() + ',';
      }
    }
  }
  return valueToReturn;
}

function getPromoValue() {
  var x = document.getElementById('hiddenIsPromo');
  if (x != null) {
    return x.value;
  } else {
    return '1';
  }
}
function getSortValue() {
  var x = document.getElementById('selectSort');
  if (x != null) {
    return x.value;
  } else {
    return '1';
  }
}

//global search

function PostbackSearch(postUrl) {
  //debugger;
  $("#ProgressDialog").dialog("open");
  $.post(postUrl, {
    searchWord: document.getElementById('txtSearch').value
  },
  function (response) { $("#ProgressDialog").dialog("close"); $("#divResults").html(response); })
}

// post back regions
function loadRegionAndCity(urlPostRegion, urlPostCity) {
  //debugger;
  urlPostRegion += "?search=True";
  urlPostCity += "?search=True";
  if (document.getElementById('spanMinValue').innerHTML.toString() != '') {
    urlPostRegion += "&minprice=" + document.getElementById('spanMinValue').innerHTML;
    urlPostCity += "&minprice=" + document.getElementById('spanMinValue').innerHTML;
  }

  if (document.getElementById('spanMaxValue').innerHTML.toString() != '') {
    if (document.getElementById('spanMaxValue').innerHTML.toString() == "2 500") {
      urlPostRegion += "&maxprice=2500";
      urlPostCity += "&maxprice=2500";
    }
    else {
      urlPostRegion += "&maxprice=" + document.getElementById('spanMaxValue').innerHTML;
      urlPostCity += "&maxprice=" + document.getElementById('spanMaxValue').innerHTML;
    }
  }

  if (document.getElementById('hiddenMonths').value != '') {
    urlPostRegion += "&months=" + document.getElementById('hiddenMonths').value;
    urlPostCity += "&months=" + document.getElementById('hiddenMonths').value;
  }

  if (document.getElementById('txtSearchDateFrom').value != '') {
    urlPostRegion += "&fromdate=" + document.getElementById('txtSearchDateFrom').value;
    urlPostCity += "&fromdate=" + document.getElementById('txtSearchDateFrom').value;
  }

  if (document.getElementById('txtSearchDateTo').value != '') {
    urlPostRegion += "&todate=" + document.getElementById('txtSearchDateTo').value;
    urlPostCity += "&todate=" + document.getElementById('txtSearchDateTo').value;
  }

  var themes = getThemesForSearch();
  if (themes != '') {
    urlPostRegion += "&theme=" + themes;
    urlPostCity += "&theme=" + themes;
  }

  urlPostRegion += "&rnd=" + Math.random(5).toString();
  urlPostCity += "&rnd=" + Math.random(5).toString();

  $('#searchRegions').load(urlPostRegion);
  $('#searchCities').load(urlPostCity);
}

// render watermarks
function focusWatermak(me, defaultText) {
  if ($(me).val(defaultText)) {
    $(me).val('');
  }
}

function blurWatermak(me, defaultText) {
  var textValue = $(me).val();

  if (textValue == defaultText || textValue.length == 0) {
    $(me).val(defaultText);
  }
}

// translate interface
function toggleEpandChildren(me) {
  //debugger;
  if ($(me).attr('src') == '../Resources/Images/Plus.gif') { $(me).attr('src', '../Resources/Images/Minus.gif'); }
  else { $(me).attr('src', '../Resources/Images/Plus.gif'); }

  $("a.child-resource[parentElement='" + $(me).attr('mainElement') + "']").each(function () {
    $(this).slideToggle(300);
  });
}

function returnParameters() {
  var parametersString = '';
  $('form input[name]').each(function () {
    if ($(this).val() !== '') parametersString += $(this).attr('name') + '=' + $(this).val() + ',';
  });

  $('form select[name]').each(function () {
    if ($(this).val() !== '') parametersString += $(this).attr('name') + '=' + $(this).val() + ',';
  });

  return parametersString.substring(0, parametersString.length - 2);
}

//image gallery
function changeImageBackground(el) {
  //debugger;
  $("#divMainImage").css("background", "url('" + el.attributes['path'].value + "') no-repeat right center");
  $("#divMainImage").attr('href', el.attributes['category'].value + '?cat=' + el.attributes['categoryId'].value);
  $("#divMainImage").attr('title', el.attributes['category'].value);
  hideSelectionImage();
  $("img", $(el)).css('display', 'block');
}

function hideSelectionImage() {
  $(".aFileInfo").each(function () {
    $("img", $(this)).css('display', 'none');
  });
}

// offers
function hideAllOffers() {
  $('#formTop').hide();
  $('#formMiddle').hide();
  $('#formBottom').hide();
  $('#formBrochures').hide();
}

function showCurrentOffer(me) {
  hideAllOffers();
  switch ($(me).attr('value')) {
    case '1':
      $('#formTop').show();
      break;

    case '2':
      $('#formMiddle').show();
      break;

    case '3':
      $('#formBottom').show();
      break;

    case '4':
      $('#formBrochures').show();
      break;
  }
}

// Google Maps
var map = null;
var gdir = null;
var geocoder = null;
var latitude;
var longitude;
var googleAddress;
var description;

function updateCoordinates() {
  // Create the geocoder
  geocoder = new GClientGeocoder();
  // Get the geocodes
  geocoder.getLatLng(googleAddress, mycallbackupdate);
}

function mycallbackupdate(point) {
  if (point) {
    var marker = new GMarker(point);
    //marker.title = 'gigi';
    latitude = marker.getPoint().lat();
    longitude = marker.getPoint().lng();

    loadMap();
  }
}

function load() {
  if (GBrowserIsCompatible()) {

    if (latitude == null || longitude == null) {
      updateCoordinates();
    }
    else {
      loadMap();
    }
  }
}

function loadMap() {
  map = new GMap2(document.getElementById("googleMap"));
  // Pan/zoom the map
  map.addControl(new GSmallMapControl());
  // Switch between Map and Satellite modes
  map.addControl(new GMapTypeControl());
  map.setCenter(new GLatLng(latitude, longitude), 19);

  // remove MapTypes Hibryd and Satellite 
  map.removeMapType(G_HYBRID_MAP);
  map.removeMapType(G_SATELLITE_MAP);
  // Add marker to the map at point location
  var current_branch = new GLatLng(latitude, longitude);
  var description = '<span style="color: #333; font-size: 11px;">Skifriends<br />Nijverheidsstraat 17<br />B-2530 Boechout<br />Tel: 03 460 14 04<br />Fax: 03 460 18 34'
  map.addOverlay(createMarker(current_branch, '<span style="font-size:12px">' + description + '</span>'));

}

// Creates a marker at the given point with the given number label
function createMarker(point, theName) {
  var marker = new GMarker(point);
  GEvent.addListener(marker, "click", function () {
    marker.openInfoWindowHtml(theName, { maxHeight: 75 });
  });
  return marker;
}

function refreshDocuments() {
  __doPostBack(lnkRefreshDocumentID, '');
}

function refreshDocumentsWithAlert() {
  __doPostBack(lnkRefreshDocumentWithAlertID, '');
}

function hideRegions(me) {
  if (!$(me).hasClass('listLabels') && $(me).attr('associatedID') != 'divSearchCountriesExt') { $('#divSearchCountriesExt').hide(300); $('#imgSearchCountries').attr('src', baseURL + 'Resources/Images/icon_colapsed.png'); }
  if (!$(me).hasClass('listLabels') && $(me).attr('associatedID') != 'divSearchRegionsExt') { $('#divSearchRegionsExt').hide(300); $('#imgSearchRegions').attr('src', baseURL + 'Resources/Images/icon_colapsed.png'); }
  if (!$(me).hasClass('listLabels') && $(me).attr('associatedID') != 'divSearchCitiesExt') { $('#divSearchCitiesExt').hide(300); $('#imgSearchCities').attr('src', baseURL + 'Resources/Images/icon_colapsed.png'); }
  if (!$(me).hasClass('listLabels') && $(me).attr('associatedID') != 'divSearchPeriodsExt') { $('#divSearchPeriodsExt').hide(300); $('#imgSearchPeriods').attr('src', baseURL + 'Resources/Images/icon_colapsed.png'); }
}

function setBookDialog(action) {
  //debugger;
  window.location.href = action
//  $("#BookDialog").dialog({ modal: true, draggable: false, position: top, closeText: "X", title: 'Online boekingen moeten online betaald worden via VISA, MasterCard of bancontact/Mister Cash ', dialogClass: 'book-dialog',
//    buttons: { "Ok": function () { window.location.href = action } }
//  });
}

function completeMessage(isSent) {
  if (isSent == 'True') {
    $("#completeMessage").dialog({ modal: true, draggable: false, position: top, closeText: "X", title: 'Bedankt voor uw aanvraag', dialogClass: 'book-dialog',
      buttons: { "Ok": function () { $(this).dialog("close"); var url = window.location.href; window.location.href = url; } }
    });
  }
}

function zoomImage(me) {
  var $overlay = $('<div class="zoomed-image" onclick="setParametersAfterZoom(this);"><div class="ui-widget-overlay" style="position:fixed;"></div>' + '<img alt="" style="position:fixed;" src="' + $("img." + $(me).attr('class') + '-o').attr('src') + '" /></div>').hide().appendTo('body');
  $overlay.fadeIn();
  //debugger;
  $('body').css('overflow', 'hidden');
  var topDist = $(window).height();
  var leftDist = $(window).width();

  if (topDist <= $('.zoomed-image img').height()) topDist = 0;
  else topDist = (topDist - $('.zoomed-image img').height()) / 2;

  if (leftDist <= $('.zoomed-image img').width()) leftDist = 0;
  else leftDist = (leftDist - $('.zoomed-image img').width()) / 2;

  $('.zoomed-image img').css('top', topDist);
  $('.zoomed-image img').css('left', leftDist);
  //setTimeout("$('.zoomed-image').focus();", 500);
}

function setParametersAfterZoom(me) {
  $('body').css('overflow', 'visible');
  $(me).remove();
}

function setParameterOnEscape(e) {
  //debugger;
  //  if (!e) var e = window.event;
  //  if (e.keyCode == 27) {
  //    $('body').css('overflow', 'visible');
  //    $('.zoomed-image').remove();
  //  }
}
