var programs = {

  _tasks: {},

  _busyTimeout: {},

  _taskIndicator: null,

  _busy: false,

  _dateFormat: 'dd.mm.y',

  init: function() {
    this._cityFormEl	= $('#cityForm');
    this._cityFormEl.submit(function(){return !programs.loadDirections()});
    this._citiesEl	= $('#cityId');
    this._directionsEl	= $('#directions');
    this._directionLabelEl	= $('#directionLabel');
    this._newsEl	= $('#directionNews');
    this._relatedLinksEl	= $('#relatedLinks');
    this._toursEl	= $('#tours');
  },

  clearDirections: function () {
    this._directionsEl.html('');
  },

  clearTours: function () {
    this._toursEl.html('');
  },

  loadDirections: function () {
    this._cityId	= this._citiesEl.val();
    if (this._forceDirection) {
      this._directionId	= this._forceDirection;
      this.loadTours(this._cityId, this._directionId);
    }
    else {
      var url = baseUrl + '/programs/get-directions-json/';
      var data	= {
	city:	this._cityId
      }
      if (this._isLucky) {
	data.lucky	= 1;
      }
      $.getJSON(url, data, function(jd) {
		  programs.setDirections(jd);
		});
    }
    return true;
  },

  setDirections: function (directions) {
    this._directions	= directions;
    if (!this._directionId || !this._directions[this._directionId]) {
      for (var i in this._directions) {
	this._directionId	= parseInt(i);
	break;
      }
    }
    this.drawDirections();
    this.loadTours(this._cityId, this._directionId);
    return this;
  },

  drawDirections: function () {
    var h = [];
    for (var i in this._directions) {
      h[h.length]	= '<li><a href="';
      h[h.length]	= baseUrl;
      h[h.length]	= '/programs/index/city/';
      h[h.length]	= this._cityId;
      h[h.length]	= '/direction/';
      h[h.length]	= i;
      if (this._isLucky) {
	h[h.length]	= '/lucky/1';
      }
      h[h.length]	= '" onclick="return !programs.loadTours(';
      h[h.length]	= this._cityId;
      h[h.length]	= ',';
      h[h.length]	= i;
      h[h.length]	= ');">';
      h[h.length]	= this._directions[i];
      h[h.length]	= '</a></li>';
    }
    this._directionsEl.html(h.join(''));
  },

  setLucky: function (isLucky) {
    this._isLucky	= isLucky;
    return this;
  }
  ,
  loadTours: function (cityId, directionId) {
    programs._cityId	= cityId;
    programs._directionId	= directionId;
    var url = baseUrl + '/programs/get-tours-json/',
    data	= {
      city:	programs._cityId,
      direction:	programs._directionId,
      get_news: programs._get_news,
      enable_top_links: programs._enable_top_links,
      surl: programs._surl
    }
    if (programs._isLucky) {
      data.lucky	= 1;
    }
    $.getJSON(url, data, function(jd) {
		programs.setTours(jd.tours);
		programs.drawNews(jd.news);
		programs.drawDirection(jd.directionName);
		programs.drawRelatedLinks();
	      });
    return true;
  },

  setTours: function (tours) {
    this._tours	= tours;
    this.drawTours();
    return this;
  },

  drawTours: function () {
    var h = [];
    for (var i in this._tours) {
      if ('1' == this._tours[i].OnlySite) {
        h[h.length]	= '<h2><a href="';
	h[h.length]	= baseUrl;
	h[h.length]	= '/print/?tourID=';
	h[h.length]	= this._tours[i].ID;
	h[h.length]	= '&amp;cityID=';
	h[h.length]	= this._cityId;
	h[h.length]	= '&amp;' + this._layoutStr;
	h[h.length]	= this._sidebarStr;
	if (this._surl !='') h[h.length]	= '&amp;surl=' + this._surl;
	h[h.length]	= '" target="_blank">';
	h[h.length]	= this._tours[i].Name;
        h[h.length]	= '</a></h2>';
      }
      else if (this._tours[i]._sub && this._tours[i]._sub.length) {
	h[h.length]	= '<h3 class="trigger collapsed header" onclick="programs.toggleTreeNode(\'#toursList_';
	h[h.length]	= i;
	h[h.length]	= '\', this)" >';
	h[h.length]	= this._tours[i].LongName;
	h[h.length]	= '</h3><ul class="tours hidden" id="toursList_';
	h[h.length]	= i;
	h[h.length]	= '">';
	for (var j in this._tours[i]._sub) {
	  h[h.length]	= '<li>';
	  h[h.length]	= '<a href="';
	  h[h.length]	= baseUrl;
	  if ('1' == this._tours[i]._sub[j].OnlySite) {
	    h[h.length]	= '/print/';
	    h[h.length]	= '?tourID=';
	    h[h.length]	= this._tours[i]._sub[j].ID;
	    h[h.length]	= '&amp;cityID=';
	    h[h.length]	= this._cityId;
	    h[h.length]	= '&amp;' + this._layoutStr;
	    h[h.length]	= this._sidebarStr;
        if (this._surl != '') h[h.length]	= '&amp;surl=' + this._surl;
	    h[h.length]	= '" target="_blank">';
	  }
	  else {
	    h[h.length]	= '/programs/dates/city/';
	    h[h.length]	= this._cityId;
	    h[h.length]	= '/direction/';
	    h[h.length]	= this._directionId;
	    if (this._isLucky) {
	      h[h.length]	= '/lucky/1';
	    }
	    h[h.length]	= '?' + this._layoutStr;
	    h[h.length]	= this._sidebarStr;
        if (this._surl !='' ) h[h.length]	= '&amp;surl=' + this._surl;
	    h[h.length]	= '#';
	    h[h.length]	= this._tours[i]._sub[j].ID;
	    h[h.length]	= '">';
	  }
	  h[h.length]	= this._tours[i]._sub[j].Name;
	  h[h.length]	= '</a></li>';
	}
	h[h.length]	= '</ul>';
      }
      else {
        h[h.length]	= '<h2>';
	h[h.length]	= this._tours[i].Name;
        h[h.length]	= '</h2>';

      }
    }
    this._toursEl.html(h.join(''));
  },

  drawNews: function (html) {
    this._newsEl.html(html);
  },

  drawRelatedLinks: function () {
    var h = [];
    if (this._isLucky) {
      h[h.length]	= '<h2>Список&nbsp;программ&nbsp;Lucky</h2>';
    }
    else {
      h[h.length]	= '<h2>Список&nbsp;программ</h2>';
    }
    if (this._enable_top_links) {
        if (!this._isLucky) {
          h[h.length]	= '<h2><a href="';
          h[h.length]	= baseUrl;
          h[h.length]	= '/programs/index/city/';
          h[h.length]	= this._cityId;
          h[h.length]	= '/direction/';
          h[h.length]	= this._directionId;
          h[h.length]	= '/lucky/1';
          h[h.length]	= '?' + this._layoutStr;
          h[h.length]	= this._sidebarStr;
          h[h.length]	= '" onclick="return !programs.setLucky(1).loadDirections(';
          h[h.length]	= this._cityId;
          h[h.length]	= ',';
          h[h.length]	= this._directionId;
          h[h.length]	= ')">Раннее&nbsp;бронирование&nbsp;Lucky</a></h2>';
        }
        h[h.length]	= ' <h2><a target="_blank" href="http://www.lantatur.ru/pricelist/index_price.php?cityID=';
        h[h.length]	= this._cityId;
        h[h.length]	= '&amp;gid=';
        h[h.length]	= this._directionId;
        h[h.length]	= '&amp;view=1';
        h[h.length]	= '&amp;' + this._layoutStr;
        h[h.length]	= this._sidebarStr;
        h[h.length]	= '">Спецпредложения</a></h2> ';
        h[h.length]	= '<h2><a target="_blank" href="http://www.lantatur.ru/main/lanonline/index.php?countryKey=';
        h[h.length]	= this._directionId;
        h[h.length]	= '&amp;' + this._layoutStr;
        h[h.length]	= this._sidebarStr;
        h[h.length]	= '">Индивидуальное&nbsp;бронирование&nbsp;тура</a></h2>';
    }
    this._relatedLinksEl.html(h.join(''));
  },

  drawDirection: function (html) {
    this._directionLabelEl.html(html);
  },

  toggleTreeNode: function (nodeEl, controlEl, action) {
    nodeEl  = $(nodeEl);
    controlEl   = $(controlEl);
    if (!action) {
      action  = nodeEl.hasClass('hidden') ? 'show' : 'hide';
    }
    if ('hide' == action) {
      nodeEl.addClass('hidden');
      controlEl.addClass('collapsed');
      controlEl.removeClass('expanded');
    }
    else {
      nodeEl.removeClass('hidden');
      controlEl.removeClass('collapsed');
      controlEl.addClass('expanded');
    }
  },

  switchTaskState: function (taskId, isBusy, timeout) {
    this._tasks['task'+taskId]	= isBusy;
    if (this._busyTimeout[taskId]) {
      clearTimeout(this._busyTimeout[taskId]);
    }
    if (isBusy) {
      this._busy	= true;
      this._busyTimeout[taskId]	= setTimeout(function(){
				               searchForm.switchTaskState(taskId, 0)
			                     }, timeout || 10000);
    }
    else {
      var busy	= false;
      for (i in this._tasks) {
	if ('task' == i.substr(0, 4) && this._tasks[i]) {
	  busy	= true;
	  break;
	}
      }
      this._busy = busy;
    }
    this.switchTaskIndicator();
  },

  switchTaskIndicator: function () {
    if (this._busy) {
      if (!this._taskIndicator.is(':visible')) {
	var x	= ($(document.body).width() - this._taskIndicator.width()) / 2;
	this._taskIndicator.css({
				  'left': x+'px'
				});
	if ($.browser.msie) {
	  this._taskIndicator.css({
				    'position': 'absolute'
				  });
	}
	this._taskIndicator.show();
      }
    }
    else {
      if (this._taskIndicator.is(':visible')) {
	this._taskIndicator.hide();
      }
    }
  }
}

var loadTours	= programs.loadTours;

