function go(url) {
  if (url) {
    window.location.assign(url);
  }
}

function initSlides() {
  $(function() {
    $('a[rel*=lightbox]').lightBox({imageLoading: "/design/img/loading.gif",
                                     imageBtnClose: "/design/img/close.gif",
                                     imageBtnPrev: "/design/img/prev.gif",
                                     imageBtnNext: "/design/img/next.gif"});
    });
}

function initDatePicker() {
  initSlides();
  $(function(){ 
    $('.datepick').datePicker(); 
    $('textarea[name=msg]').val($.trim($('textarea[name=msg]').val()));
  });
}

function toggleFilters() {
  if (document.getElementById('additionalOptions').style.display == 'none') {
    $('#additionalOptions').css("display", "block");
    $('#opToggler').attr("class", 'opened');
    $("#RMC").attr("class", 'RMC2');
    $('#LMC').attr("class", 'LMC2');
    $('#b1').attr("class", 'bdarkl');
    $('#b2').attr("class", 'bdarkl');
    $('#b3').attr("class", 'bdarkr');
    $('#b4').attr("class", 'bdarkr');
  } else {
    $('#additionalOptions').css("display", "none");
    $('#opToggler').attr("class", 'closed');
    $("#RMC").attr("class", 'RMC1');
    $('#LMC').attr("class", 'LMC1');
    $('#b1').attr("class", 'blightl');
    $('#b2').attr("class", 'blightl');
    $('#b3').attr("class", 'blightr');
    $('#b4').attr("class", 'blightr');
  }
}

function filterSetCities(district) {
  if (!cities) {
    return;
  }
  $("select[name=filterCity] option").remove();
  var cityObj = $("select[name=filterCity]");
  cityObj.append("<option value='0'>" + filterWords["city"] + "</option>");
  if (district && cities[district]) {
    for (var i in cities[district]) {
      var city = cities[district][i];
      cityObj.append("<option value='" + city[0] + "'>" + city[1] + "</option>");
    }
  } else {
    for (var i in all_cities) {
      cityObj.append("<option value='" + all_cities[i][0] + "'>" + all_cities[i][1] + "</option>");
    }
  }
}

function presetFilters(district, city) {
  if (district) {
    $("select[name=filterDistrict]").val(district).attr("selected", "true");
    filterSetCities(district);
  } else {
    filterSetCities(0);
  }
  if (city) {
    $("select[name=filterCity]").val(city).attr("selected", "true");
  }
}

function enablePriceFilter() {
  var t = $("select[name=filterPriceType]").val();
  $("select[name=filterPrice]").attr("disabled", ("L" != t && "G" != t));
}

function wopen(url, w, h, r) {
  if (!url) {
    return false;
  }
  w = w ? w : 500;
  h = h ? h : 500;
  r = r ? 1 : 0;

  var left = Math.round((screen.width - w)/2);
  var top = Math.round((screen.height - h)/2);
  window.open(url, "_blank", "toolbar=0;scrollbars=0,resizable="+r+",width="+w+",height="+h+",left="+left+",top="+top);
  return false;
}

function contactFrmCheck() {
  if (!replace($("input[name=acode]").val())) {
    alert(CW2);
    return false;
  }
  var n = replace($("input[name=name]").val());
  var e = replace($("input[name=email]").val());
  var m = replace($("textarea[name=msg]").val());
  
  if (!n || !e || !m) {
    alert(CW1);
    return false;
  }
  return true;
}

function replace(value) {
  if (undefined == value) {
    return "";
  }
  return value.replace(/^( )+/g, "").replace(/( )+$/g, "").replace(/( {2})+/g, " ");
}

function showContactForm() {
  if ($("#agree").attr("checked")) {
    $("#contactForm").show("normal");
  } else {
    $("#contactForm").hide("normal");
  }
}

function sendContactForm() {
  if (!contactFrmCheck()) {
    return false;
  }
  $("#contactForm > form > div.bottomBut > a").hide();
  $("#errorMessage").html("&nbsp;");
  var url = $("#contactForm > form").attr("action");
  $.post(url, 
         {ff: $("input[name=ff]").val(),
          name: $("input[name=name]").val(),
          email: $("input[name=email]").val(),
          msg: $("textarea[name=msg]").val(),
          acode: $("input[name=acode]").val(),
          start: $("input[name=start]").val(),
          finish: $("input[name=finish]").val(),
          count: $("input[name=count]").val()},
         function(data) {
          if (!data) {
            alert("Error send data to the server");
            return;
          }
          if (data.result && "ERROR" == data.result) {
            $("#errorMessage").html(data.message);
          } else {
            $("#errorMessage").html(data.message);
            $("input[name=name]").val("");
            $("input[name=email]").val("");
            $("textarea[name=msg]").val("");
            $("input[name=acode]").val("");
            $("input[name=count]").val("");
            $("input[name=start]").val("");
            $("input[name=finish]").val("");
            var dt = new Date();
            $("#captcha").attr("src", "/captcha.php?t=" + dt.getTime())
          }
          $("#contactForm > form > div.bottomBut > a").show();
         },
         "json");
  return false;
}
function toggleCategory(id) {
  if ($("#header"+id).hasClass("opened")) {
    $("#header"+id).removeClass("opened");
    $("#header"+id+" > div[class=total], #header"+id+" > div[class=price], #sep"+id).show();
    $("#closedTable"+id).hide();
  } else {
    $("#header"+id).addClass("opened");
    $("#header"+id+" > div[class=total], #header"+id+" > div[class=price], #sep"+id).hide();
    $("#closedTable"+id).show();
  }
}
function openAll() {
  $("#articleText > h4").addClass("opened");
  $("#articleText > h4 > div[class=total]").hide();
  $("#articleText > h4 > div[class=price]").hide();
  $("#articleText > div[class=sep2]").hide();
  $("#articleText > div[class=opencontainer]").show();
}
function closeAll() {
  $("#articleText > h4").removeClass("opened");
  $("#articleText > h4 > div[class=total]").show();
  $("#articleText > h4 > div[class=price]").show();
  $("#articleText > div[class=sep2]").show();
  $("#articleText > div[class=opencontainer]").hide();
}
function goSelectedWeek(e) {
  var s = $(e).val();
  if (s) {
    go(s);
  }
}
function goSelectedPrice(u, s) {
  go(u+"&c="+s);
}
function placeDoskaBanner() {
  var p = $("#contentRow");
  var l = p.position().left + p.width() - 218;
  $("#brMenu").css("left", l+"px");
}

function commentFrmCheck() {
  if (!replace($("input[name=acode]").val())) {
    alert(CW2);
    return false;
  }
  var n = replace($("input[name=name]").val());
  var m = replace($("textarea[name=msg]").val());
  
  if (!n || !m) {
    alert(CW1);
    return false;
  }
  return true;
}

function sendCommentForm() {
  if (!commentFrmCheck()) {
    return false;
  }
  $("#errorMessage").html("&nbsp;");
  var url = $("#commentForm > form").attr("action");
  $.post(url, 
         {ff: $("input[name=ff]").val(),
          name: $("input[name=name]").val(),
          msg: $("textarea[name=msg]").val(),
          acode: $("input[name=acode]").val()},
         function(data) {
          if (!data) {
            alert("Error send data to the server");
            return;
          }
          if (data.result && "ERROR" == data.result) {
            $("#errorMessage").html(data.message);
          } else {
            $("#errorMessage").html(data.message);
            $("input[name=name]").val("");
            $("textarea[name=msg]").val("");
            $("input[name=acode]").val("");
          }
          var dt = new Date();
          $("#captcha").attr("src", "/captcha.php?t=" + dt.getTime())
          //$("#commentForm > form > div.bottomBut > input").show();
         },
         "json");
  return false;
}

function showCommentForm() {
  if ($("#commentForm").css("display") == "none") {
    $("#commentForm").show("normal");
  } else {
    $("#commentForm").hide("normal");
  }
}

