
var Paging = {
  items : new Array(), 
  limitItems : 5,
  currentPage : 1,
  limitPages : 7,
  filters : new Object(),
  filteredItemIndexes : Array(),
  
  priceLimit : 0,
  showNotice : false,
  isNoticeShown : false,
  
  textTranslations : new Array(),
	
  getItems : function() {
    return this.items;
  },
  
  setItems : function(items) {
    this.items = items;
    for (var i = 0; i< items.length; i++) {
      this.filteredItemIndexes.push(i);
    }
  },

  addItem : function(item) {
    this.items.push(item);
    this.filteredItemIndexes.push(this.filteredItemIndexes.length);
  },
  
  getCountItems : function(){
    return this.filteredItemIndexes.length;
  },
  
  getLimitItems : function() {
    return this.limitItems;
  },

  setLimitItems : function(limitItems) {
    this.limitItems = limitItems;
  },
  
  getCurrentPage : function() {
    return this.currentPage;
  },
  
  setCurrentPage : function(currentPage) {
    if(currentPage < 1) {
      this.currentPage = 1;
    } else {
      if(currentPage > this.getCountPages()) {
        this.currentPage = this.getCountPages();
      } else {
        this.currentPage = currentPage;
      }
    }
    dojo.cookie("villaList.page", this.currentPage, {expires: 1});
  },
  
  getCountPages : function() {
    return Math.ceil(this.getCountItems() / this.getLimitItems());
  }, 
  
  getFirstItemIndex : function () {
    return ((this.getCurrentPage() - 1) * this.getLimitItems() + 1);
  },

  getLastItemIndex : function() {
    var lastIndex = this.getCurrentPage() * this.getLimitItems();
    var countItems = this.getCountItems();
    if (lastIndex > countItems) {
      lastIndex = countItems;
    }
    return lastIndex;
  },
  
  isOnePage : function() {
    return this.getLimitItems() >= this.getCountItems();
  },
  
  getLimitPages : function() {
    return this.limitPages;
  },

  setLimitPages : function(limitPages) {
    this.limitPages = limitPages;
  },
  
  isCurrentPage : function (page) {
    return page == this.getCurrentPage();
  },

  isCurrentSection : function(page) {    
    var x = (this.getLimitPages() - 1) / 2;
    var middle = this.getCurrentMiddle();
    if (((middle - x) <= page) && ((middle + x) >= page)) {
      return true;
    }
    return false;
  },
  
  getCurrentMiddle : function () {
    var middle = this.getCurrentPage();
    var first = 1;
    var last = this.getLimitPages();
    if (middle >= first && middle < last) {
      middle = (first + last) / 2;
    } else {
      first = this.getCountPages() - this.getLimitPages() + 1;
      last = this.getCountPages();
      if (middle > first && middle <= last) {
        middle = (first + last) / 2;
      }
    }
    return middle;
  },

  isFirstPage : function(page) {
    return page == 1;
  },

  isLastPage : function(page) {
    return page == this.getCountPages();
  },
    
  addFilter : function(key, value, sectionName) {
    if (typeof (this.filters[sectionName]) == "undefined") {
      this.filters[sectionName] = new Object();
    }
    this.filters[sectionName][key] = value;
    dojo.cookie("villaList.filters", dojo.toJson(this.filters), {expires: 1});
  },
  
  deleteFilter : function(key, sectionName) {
		if (typeof (this.filters[sectionName][key]) != "undefined") {
		  delete this.filters[sectionName][key];
		  dojo.cookie("villaList.filters", dojo.toJson(this.filters), {expires: 1});
		}
  },
  
  retriveDataFromCookie : function() {
	  var fromBack = dojo.cookie("villaList.back") || null;
	  if (fromBack == null) {
		  return false;
	  }
	  var page = dojo.cookie("villaList.page") || null;
	  if (page) {
		this.setCurrentPage(page);
	  }
	  var filtersJson = dojo.cookie("villaList.filters") || null;
	  if (filtersJson) {
		this.filters = dojo.fromJson(filtersJson);
	  }
	  dojo.cookie("villaList.back", null, {expires: -1});
	  dojo.cookie("villaList.go", null, {expires: -1});
  },
  
  restoreCheckBoxes : function() {
	  for (section in this.filters) {
		for (key in this.filters[section]) {
		  var checkbox = document.getElementById(key) || null;
		  if (checkbox) {
		    checkbox.checked = true;
		    allItemsId = "All-" + section + "-Products";
			var allCheck = document.getElementById(allItemsId) || null;
			if (allCheck) {
		      allCheck.checked = false;
			}
		  }
		}
	  }
  },
  
  getPriceLimit : function() {
    return this.priceLimit;
  },

  setPriceLimit : function(priceLimit) {
	  this.priceLimit = priceLimit;
  },
  
  showNotice : function() {
    this.showNotice = true;
  },

  doNotShowNotice : function() {
	  this.showNotice = false;
  },
  
  addTextTranslation : function(key, value) {
	  this.textTranslations[key] = value;
  },
  
	paginate : function(page) {
	  this.clearItems();
	  this.setCurrentPage(page);
	  var offset = (this.getCurrentPage() - 1) * this.getLimitItems();
	  this.filterItems();
	  for (var i = 0; i < this.getLimitItems(); i++) {
	    if (typeof (this.filteredItemIndexes[i + offset]) != "undefined" && typeof (this.items[this.filteredItemIndexes[i + offset]]) != "undefined") {
	      this.showItem(this.items[this.filteredItemIndexes[i + offset]]);
	    }
	  }
	  this.changePager("-Top");
	  this.changePager("-Bottom");
	  return false;
	},
	
	clearItems : function() {
	  for (var i = 0; i < this.items.length; i++) {
	    var div = document.getElementById("Product-Div" + i) || null;
	    if (div) {
	      div.style.display = "none";
	    }
	 }
	 var noticeDiv = document.getElementById("Notice-Div") || null;
	 if (noticeDiv) {
	   noticeDiv.style.display = "none";
	   this.isNoticeShown = false;
	 } 
  },
  
	showItem : function(item) {
		var div = document.getElementById("Product-Div" + item.index) || null;
		if (div) {
		  div.style.display = "block";
		}
		if(item.pictureExists) {
		  var img = document.getElementById("Product-Img" + item.index) || null;
		  if (img) {
            if (item.pictureUrl !== "") {
	          img.src = item.pictureUrl;
	        }
	      }
		}
		var noticeDiv = document.getElementById("Notice-Div") || null;
		if (this.showNotice == true && noticeDiv) {
			if (this.isNoticeShown == false && item.minPricePerDay > this.getPriceLimit()) { 
			  noticeDiv.style.display = "block";
			  this.isNoticeShown = true;
			}
		}
	},
  
	changePager : function(position) {
	  var div = document.getElementById("Paging" + position) || null;
	  if (div == null) {
	    return false;
	  }
	  var innerHTML = "";
    var bottomPostfix = "";
	  
    if (position == "-Top") {
        innerHTML += "<h1><span id='Products-Count'>" + this.getCountItems() + "</span> ";
        if (this.getCountItems() == 1) {
          innerHTML += this.textTranslations["villa"];
        } else {
          innerHTML += this.textTranslations["villas"];	
        }
        if (rcTitle){
            innerHTML += " in " + rcTitle;	
        }
        innerHTML += "</h1>";
        bottomPostfix = "";
    } else {
        bottomPostfix = "2";
    }
    if (this.getCountItems() > this.getFirstItemIndex()) {
      innerHTML += "<div class='Objects-Displayed" + bottomPostfix + "'>" + this.textTranslations["offersDispalyed"]
          + " <span id='Products-Browsing" + position +"'>" + this.getFirstItemIndex() + "-"
          + this.getLastItemIndex() + "</span></div>";
    } else {
      innerHTML += "<div class='Objects-Displayed" + bottomPostfix + "'>" + this.textTranslations["offersDispalyed"]
          + " <span id='Products-Browsing" + position + "'>" + this.getCountItems() + "</span></div>";
    }
    if (this.isOnePage() == false) {
      if (this.getCurrentPage() > 1) {
        innerHTML += "<div class='Page-Back" + bottomPostfix + "'>"
            + "<a id='Prev-Link" + position + "' href='?page=" + (this.getCurrentPage() - 1) + "' "
            + "onclick='Paging.paginate(" + (this.getCurrentPage() - 1) + "); return false' title='" + this.textTranslations["previousOffers"] + "'>"
            + "<img src='/images/base/ic_arrow_left_active.gif' alt='" + this.textTranslations["previousOffers"] + "' /></a></div>";   
      } else {
        innerHTML += "<div class='Page-Back" + bottomPostfix +"'>"
            + "<a id='Prev-Link" + position + "' href='#' title='" + this.textTranslations["previousOffers"] + "'>"
            + "<img src='/images/base/ic_arrow_left_passive.gif' alt='" + this.textTranslations["previousOffers"] + "' /></a></div>";
      }
      innerHTML += "<div class='Pagelist" + bottomPostfix + "'>"
          + this.textTranslations["page"] + "<ul>";
      for (var i = 1; i <= this.getCountPages(); i++) {             
        if (this.isCurrentSection(i)) {
          if (this.isCurrentPage(i)) {
            innerHTML += "<li><a id='Page" + i + "-Link" + position + "' href='?page=" + i + "' title='" + this.textTranslations["offersPage"] + " " + i + "' "
                + "onclick='Paging.paginate(" + i + "); return false' class='Pagelink-Active'>" + i + "</a></li>";
          } else {
            innerHTML += "<li><a id='Page" + i + "-Link" + position + "' href='?page=" + i + "' title='" + this.textTranslations["offersPage"] + " " + i + "' " 
                + "onclick='Paging.paginate(" + i + "); return false' class='Pagelink-Passive'>" + i + "</a></li>";
          }
         } else {
           if (this.isFirstPage(i)) {
             innerHTML += "<li><a id='Page" + i + "-Link" + position + "' href='?page=" + i + "' title='" + this.textTranslations["offersPage"] + " " + i + "' "
                + "onclick='Paging.paginate(" + i + "); return false' class='Pagelink-Passive'>" + i + "</a></li>"
                + "<li>&hellip;</li>";
           }
           if (this.isLastPage(i)) {
             innerHTML += "<li>&hellip;</li>"
                + "<li><a id='Page" + i + "-Link" + position + "' href='?page=" + i + "' title='" + this.textTranslations["offersPage"] + " " + i + "' "
                + "onclick='Paging.paginate(" + i + "); return false' class='Pagelink-Passive'>" + i + "</a></li>";
           }
        }
      }
      innerHTML += "</ul></div>";
      if (this.getCurrentPage() < this.getCountItems()) {  
        innerHTML += "<div class='Page-Next" + bottomPostfix + "'>"
            + "<a id='Next-Link" + position + "' href='?page=" + (this.getCurrentPage() + 1) + "' "
            + "onclick='Paging.paginate(" + (this.getCurrentPage() + 1) + "); return false' title='" + this.textTranslations["nextOffers"] + "'>"
            + "<img src='/images/base/ic_arrow_right_active.gif' alt='" + this.textTranslations["nextOffers"] + "' /></a></div>";
      } else {
        innerHTML += "<div class='Page-Next" + bottomPostfix + "'>"
            + "<a id='Next-Link" + position + "' href='#' title='" + this.textTranslations["nextOffers"] + "'>"
            + "<img src='/images/base/ic_arrow_right_passive.gif' alt='" + this.textTranslations["nextOffers"] + "' /></a></div>";
      }
    }
    div.innerHTML = innerHTML;
	},
	
	filter : function filter(obj, sectionName) {
	  allItemsId = "All-" + sectionName + "-Products";
		if (obj.id == allItemsId && obj.checked) {
		  this.clearFilters(sectionName);
		} else {
		  var checkbox = document.getElementById(allItemsId) || null;
		  if (checkbox) {
		    checkbox.checked = false;
		  }
		  var key = this.stripSpaces(obj.id);
		  var value = this.stripSpaces(obj.value);
		  if (key != allItemsId) {
		    if (obj.checked) {
		      this.addFilter(key, value, sectionName);
		    } else {
	          this.deleteFilter(key, sectionName);
		    }
		  }
		}
		this.doFilter();
	},
	
	stripSpaces : function(aString) {
		aString = aString.replace(/\s/g, "");
		return aString;
	},
	
	clearFilters : function(sectionName) {
		for (key in this.filters[sectionName]) {
			var checkbox = document.getElementById(key) || null;
			if (checkbox) {
        checkbox.checked = false;
			}
			this.deleteFilter(key, sectionName);
		}
        this.filters[sectionName] = new Object();
        dojo.cookie("villaList.filters", dojo.toJson(this.filters), {expires: 1});
	},
	
	getFiltersLength : function() {
		var count = 0;
		for (section in this.filters) {
			for (key in this.filters[section]) {
			  count++;
			}
		}
		return count;
	},
	
	doFilter : function() {
		this.clearItems();
		this.filterItems();
		for (var i = 0; i < this.getLimitItems(); i++) {
			if (typeof (this.filteredItemIndexes[i]) != "undefined" && typeof (this.items[this.filteredItemIndexes[i]]) != "undefined") {
        this.showItem(this.items[this.filteredItemIndexes[i]]);
      }
		}
		this.changePager("-Top");
		this.changePager("-Bottom");
	},
	
	filterItems : function() {
		this.filteredItemIndexes = new Array();
		for (var i = 0; i < this.items.length; i++) {
		  if (this.checkFilterConditions(this.items[i])) {
		    this.filteredItemIndexes.push(i);
		  }
		}
	},
	
	getFilteredItems : function() {
		var result = new Array();
	    for (var i = 0; i < this.filteredItemIndexes.length; i++) {
	      result.push(this.items[this.filteredItemIndexes[i]]);
	    }
	    return result;
	},
	
	checkFilterConditions : function(item) {
	  for (section in this.filters) {
			for (key in this.filters[section]) {
			  if (this.checkCondition(section, key, item) == false) {
			    return false;
			  }
			}
		}
		return true;
	},

  checkCondition : function(sectionName, key, item) {
    if(key == "Wassersport-Products") {
      return this.checkWassersport(item);
    }
    if(key == "Fishing-Products") {
      return this.checkFishing(item);
    }
    if (item[this.filters[sectionName][key]] == false) {
      return false;
    }
    return true;
  },
  
  checkWassersport : function(item) {
    if (item["waterSki"] == true
        || item["surfing"] == true
        || item["kiteSurfing"] == true
        || item["windSurfing"] == true
        || item["canoying"] == true
        || item["swimming"] == true
        || item["boatCharter"] == true) {
        
      return true;
    }
    return false;
  },
  
  checkFishing : function(item) {
    if (item["fishing"] == true
        || item["flyFishing"] == true
        || item["deepWaterFishing"] == true) {
        
      return true;
    }
    return false;
  }

};
