﻿var onglets = new Array ("offers", "info", "city_deals");
           
function hideall() {
        for(var i=0; i < onglets.length; i++) {
            document.getElementById(onglets[i]).className="tabContent";
            document.getElementById(onglets[i]+"_title").className="";
        }
    }
	
function show(n) {
    hideall();
    document.getElementById(onglets[n]).className="tabContent current";
    document.getElementById(onglets[n]+"_title").className="current";
}

function disableAnchor(obj, disable){
  if(disable){
    var href = obj.getAttribute("href");
    if(href && href != "" && href != null){
       obj.setAttribute('href_bak', href);
    }
    obj.removeAttribute('href');
    obj.style.textDecoration="none";
  }
  else{
    if( obj.attributes['href_bak']){
        obj.setAttribute('href', obj.attributes['href_bak'].nodeValue);
    }
    obj.style.textDecoration="underline";
  }
}




var currentPage = 0;
function showPage(pageID, totalItems) {

    var startItem = 0;
    var endItem = 4;
    var itemBetween = 2;
    var topTag = document.getElementById("topPager");
    var bottomTag = document.getElementById("bottomPager");

    if (pageID > 2) {
        startItem = parseInt(pageID) - itemBetween;
        endItem = parseInt(pageID) + itemBetween;
    }
    
    if (topTag != null && bottomTag != null) {
        var aTopTag = topTag.getElementsByTagName("div")[0].getElementsByTagName("a");
        var aBottomTag = bottomTag.getElementsByTagName("div")[0].getElementsByTagName("a");
        var spanTopTag = topTag.getElementsByTagName("div")[0].getElementsByTagName("span");
        var spanBottomTag = bottomTag.getElementsByTagName("div")[0].getElementsByTagName("span");

        var countItem = document.getElementById("POIInformations").getElementsByTagName("div").length - 4;

        document.getElementById("page" + currentPage).className = "hide";

        for (i = 0; i < countItem; i++) {
            if (i >= startItem && i <= endItem) {
                if (i < countItem - 1) { spanTopTag[i].className = ""; spanBottomTag[i].className = ""; }
                aTopTag[i].className = "";
                aBottomTag[i].className = "";
            } else {
                if (i < countItem - 1) { spanTopTag[i].className = "hide"; spanBottomTag[i].className = "hide"; }
                aTopTag[i].className = "hide";
                aBottomTag[i].className = "hide";
            }
            
            disableAnchor(aTopTag[i],false);
            disableAnchor(aBottomTag[i],false);
        }
        
        document.getElementById("page" + pageID).className = "";

        aTopTag[pageID].className = "selectedNumItem";
        aBottomTag[pageID].className = "selectedNumItem";
        
        disableAnchor(aTopTag[pageID],true);
        disableAnchor(aBottomTag[pageID],true);
        
        var pagerSizeTagsTop = document.getElementById("topPager").childNodes[0].childNodes[0].rows[0].childNodes[0].getElementsByTagName("A").length;
        pagerSizeTagsTop = parseInt(pagerSizeTagsTop) - 1;
        var pagerSizeTagsBottom = document.getElementById("bottomPager").childNodes[0].childNodes[0].rows[0].childNodes[0].getElementsByTagName("A").length;
        pagerSizeTagsBottom = parseInt(pagerSizeTagsBottom) - 1;
        
        var prevTag = document.getElementById("topPager").childNodes[0].childNodes[0].rows[0].childNodes[0].getElementsByTagName("A")[0];
        var prevTag2 = document.getElementById("bottomPager").childNodes[0].childNodes[0].rows[0].childNodes[0].getElementsByTagName("A")[0];
        
        var nextTag = document.getElementById("topPager").childNodes[0].childNodes[0].rows[0].childNodes[0].getElementsByTagName("A")[pagerSizeTagsTop];
        var nextTag2 = document.getElementById("bottomPager").childNodes[0].childNodes[0].rows[0].childNodes[0].getElementsByTagName("A")[pagerSizeTagsBottom];
        
        
        disableAnchor(prevTag,false);
        disableAnchor(prevTag2,false);
        disableAnchor(nextTag,false);
        disableAnchor(nextTag2,false);       
        
        // Summary pager
        var startIndex = (iop * (parseInt(pageID) + 1)) - iop;
        startIndex = (startIndex > 0) ? startIndex : 1;
        document.getElementById("startIndex").innerHTML = startIndex;
        
        if (parseInt(pageID) == 0){
            disableAnchor(prevTag,true);
            disableAnchor(prevTag2,true);
        }
        
        var nextIndexSeq = iop * (parseInt(pageID) + 1);
        if (parseInt(totalItems) < parseInt(nextIndexSeq)){
            document.getElementById("endIndex").innerHTML = totalItems;
            disableAnchor(nextTag,true);
            disableAnchor(nextTag2,true);
        }
        else {
        document.getElementById("endIndex").innerHTML = iop * (parseInt(pageID) + 1);
        }
    }
    
    currentPage = parseInt(pageID);
    
    window.scrollTo(0,400);
}

function browse(direction, totalItems) {
    var countItem = document.getElementById("POIInformations").getElementsByTagName("div").length - 4;
    if (direction == "+" && currentPage + 1 < countItem) {
        showPage(currentPage + 1,totalItems);
    }
    if (direction == "-" && currentPage - 1 >= 0) {
        showPage(currentPage - 1,totalItems);
    }
}

function goPage(item) {
    var idx = item.selectedIndex;
    var iop = item.options[idx].value;
    var newUrl = "";
    if (getUrlParameter("iop") == null) {
        var separator = (window.location.href.indexOf('?') > -1) ? "&" : "?";
        newUrl = window.location.href + separator + "iop=" + iop;
    } else {
        newUrl = window.location.href.replace(/^(.*)(&|\?)iop=.+?(&.*$|$)/g, "$1$2iop=" + iop + "$3");
    }

    window.location.href = newUrl;
}

function getUrlParameter(paramName) {											// Get the query string value
    paramName = paramName.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + paramName + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null) {
        return null;
    } else {
        return results[1];
    }
}