﻿
function HomeInit() {
    var homeSlider = new Slider(document.getElementById('homeTab1').getElementsByTagName('DIV')[0]);
    var contentTabs = document.getElementById('homeTabsContainer').getElementsByTagName('A');
    var tabSpecified;

    for (var _i = 0; _i < contentTabs.length; _i++) {
        var originalHash = document.location.hash.substr(1);
        var tmpDecodedHash = unescape(originalHash);

        if (contentTabs[_i].getElementsByTagName('span')[0].innerHTML == tmpDecodedHash) {
            ToggleHomeTabs(_i);
            tabSpecified = true;

            break;
        }
    }

    if (!tabSpecified && document.location.hash != '') {
        document.location = '/';
    }

    if ($('hostHeader').value == 'vu.net') {
        AlignBottomBorder();
    }
}

function ToggleHomeTabs(index) {
    var strUri = '/Home/';
    var contentContainer = document.getElementById('restContentFrameBorder');
    var contentDivs = new Array();
    var contentTabs = document.getElementById('homeTabsContainer').getElementsByTagName('A');

    for (var i = 0; i < contentContainer.childNodes.length; i++) {
        if (contentContainer.childNodes[i].tagName == 'DIV')
            contentDivs[contentDivs.length] = contentContainer.childNodes[i];
    }

    contentDivs.pop();

    for (var i = 0; i < contentTabs.length; i++) {
        contentDivs[i].style.display = 'none';
        contentTabs[i].className = 'menu';
    }

    // Remove if-clause to enable Home - Thema's tab on vb.net
    switch ($('hostHeader').value) {
        case 'vu.net':
        case 'eg.net':
            {
                switch (index) {
                    case 1:
                        strUri += 'Media';
                        break;
                    case 2:
                        strUri += 'Theme';
                        break;
                    case 3:
                        strUri += 'TopOffer';
                        break;
                    case 4:
                        strUri += 'ActionOfTheDay';
                        break;
                    case 0:
                    default:
                        strUri += 'Index';
                        break;
                }
            }

            break;
        case 'vb.net':
            {
                switch (index) {
                    case 1:
                        strUri += 'Media';
                        break;
                    case 2:
                        strUri += 'TopOffer';
                        break;
                    case 3:
                        strUri += 'ActionOfTheDay';
                        break;
                    case 0:
                    default:
                        strUri += 'Index';
                        break;
                }
            }

            break;
        default:
            // Add another host (and index) if needed
            break;
    }

    document.location.hash = contentTabs[index].getElementsByTagName('span')[0].innerHTML;

    if (contentDivs[index].innerHTML == '&nbsp;') {
        contentDivs[index].innerHTML = Diversity.Web.UI.GenerateLoadingImage();

        contentDivs[index].style.display = 'block';
        contentTabs[index].className = 'active menu';

        Diversity.Web.Communication.MakeRequest(strUri, true, function (response) {
            contentDivs[index].innerHTML = response;

            // Remove switch-clause and leave the vu.net case alone when enabling Home - Thema's tab on vb.net
            switch ($('hostHeader').value) {
                case 'vu.net': case 'eg.net':
                    switch (index) {
                        case 2:
                            var listCountOptions = new Array();
                            listCountOptions[listCountOptions.length] = 8;
                            listCountOptions[listCountOptions.length] = 16;
                            //TODO: FOUAD: implement async pageing and return the response to the GoToPage method of the pager.

                            var pager = new Diversity.Web.UI.Pager(
								document.getElementById('homeTab3').getElementsByTagName('DIV')[1]
								, listCountOptions
								, function (iIntPageIndex, iIntListCount, iDEpageContainer) {
								    return Diversity.Web.Communication.MakeRequest(
										"/Home/Theme/ListCount/" + iIntListCount + "/Page/" + (parseInt(iIntPageIndex) + 1)
										, false
										, null
										, null
										, false
									)
								}
								, 'theme'
								, document.getElementById('rowCount').value
							);

                            document.getElementById('homeTab3').getElementsByTagName('DIV')[1].firstChild.className = 'themalinks';
                            break;
                        case 3:
                            new Slider(document.getElementById('homeTab4').getElementsByTagName('DIV')[1].getElementsByTagName('DIV')[0]);
                            break;
                        case 4:
                            //initialize countdown timer for action of the day tab
                            jsalarm.init();
                            break;
                        default:
                            break;
                    }
                    break;
                case 'vb.net':
                    switch (index) {
                        case 2:
                            new Slider(document.getElementById('homeTab3').getElementsByTagName('DIV')[1].getElementsByTagName('DIV')[0]);
                            break;
                        case 3:
                            jsalarm.init();
                            break;
                        default:
                            break;
                    }
                    break;
                default:
                    break;
            }

            // Scroll view to the beginning of page
            //ScrollIntoView();
            //ScrollIntoView($('homeTabsContainer'));

            // realign bottom border based on tab content
            AlignBottomBorder();
        }, null, false, false);
    } else {
        contentDivs[index].style.display = 'block';
        contentTabs[index].className = 'active menu';

        // Scroll view to the beginning of page
        //ScrollIntoView();
        //ScrollIntoView($('homeTabsContainer'));

        // realign bottom border based on tab content
        AlignBottomBorder();
    }
}

function validateEmailAddress() {
    var validator = new Diversity.Web.Validation.FormValidator();
    return validator.ValidateForm(document.forms[1]);
}

function Slider(iDEslidesContainer) {
    var _this = this;
    var contentDivs = new Array();
    var radioBtnContainer;
    var radioButtons = new Array();
    var timer;
    var curIndex = 0;

    for (var i = 0; i < iDEslidesContainer.childNodes.length; i++) {
        switch (iDEslidesContainer.childNodes[i].tagName) {
            case "DIV":
                contentDivs[contentDivs.length] = iDEslidesContainer.childNodes[i];
                break;
            case "SPAN":
                radioBtnContainer = iDEslidesContainer.childNodes[i];
                break;
            default:
                break;
        }
    }

    for (var i = 0; i < contentDivs.length; i++) {
        var anchorElement = document.createElement("A");

        anchorElement.className = "clickable offer-item unchecked";
        anchorElement.setAttribute("slideIndex", i);
        anchorElement.onclick = function (ev) {
            _this.SlideClicked(
									(window.event) ? event.srcElement : ev.target
									, true
								);
        };
        radioBtnContainer.appendChild(anchorElement);
        radioButtons[radioButtons.length] = anchorElement;


    }

    this.SlideClicked = function (iDEanchor, iBlnStopSlideShow) {
        var clickedIndex = parseInt(iDEanchor.getAttribute("slideIndex"));
        for (var i = 0; i < contentDivs.length; i++) {
            contentDivs[i].style.display = "none";
            radioButtons[i].className = "clickable offer-item unchecked";
        }
        contentDivs[clickedIndex].style.display = "block";
        radioButtons[clickedIndex].className = "clickable offer-item checked";

        if (iBlnStopSlideShow)
            _this.StopSlideshow();

        // realign bottom border based on tab content
        AlignBottomBorder();
    }

    this.StartSlideshow = function () {
        timer = setInterval(function () {
            curIndex += 1;

            if (curIndex >= radioButtons.length) {
                curIndex = 0;
            }

            _this.SlideClicked(radioButtons[curIndex]);
        }, 5000);
    }

    this.StopSlideshow = function () {
        clearInterval(timer);
    }

    this.SlideClicked(radioButtons[0]);
    this.StartSlideshow();
}

function ToggleSearchResultTabs(index) {
    var strUri = '/Home/TopOffer/Search';
    var contentContainer = document.getElementById('searchResult');
    var contentDivs = new Array();
    var contentTabs = contentContainer.getElementsByTagName('DIV')[0].getElementsByTagName('A');

    for (var i = 0; i < contentContainer.childNodes.length; i++) {
        if (contentContainer.childNodes[i].tagName == 'DIV')
            contentDivs[contentDivs.length] = contentContainer.childNodes[i];
    }

    contentDivs.shift();

    for (var i = 0; i < contentTabs.length; i++) {
        contentDivs[i].style.display = 'none';
        contentTabs[i].className = '';
    }

    switch (index) {
        case 1:
            strUri += '/2';
            break;
        case 2:
            strUri += '/3';
            break;
        case 0:
        default:
            strUri += '/1';
            break;
    }

    if (contentDivs[index].innerHTML == '&nbsp;') {
        contentDivs[index].innerHTML = Diversity.Web.UI.GenerateLoadingImage();

        contentDivs[index].style.display = 'block';
        contentTabs[index].className = 'active';

        Diversity.Web.Communication.MakeRequest(strUri, true, function (response) {
            switch (index) {
                // Map tab                                         
                case 2:
                    var resp = Diversity.Web.XML.ParseXMLdoc(response);
                    resp = resp.getElementsByTagName('locations_search')[0];

                    // Load bingmap script.
                    //Diversity.Web.UI.LoadBingMapScript();

                    // Create container for map.
                    var containerId = 'topOfferMapContainer';
                    var container = document.createElement('DIV');

                    container.setAttribute('id', containerId);
                    container.setAttribute('style', 'position: relative; width: 570px; height: 400px;');
                    container.setAttribute('class', 'mapContainer');

                    // removes loading image
                    contentDivs[index].innerHTML = '';

                    // add map container
                    contentDivs[index].appendChild(container);

                    // initialise map
                    var map = new Diversity.Web.UI.Map('/CR/JS/BingMap.js');
                    var _params = {};
                    _params.containerId = containerId;
                    _params.zoom = 5;

                    _params.lat = Diversity.Web.XML.GetText(resp.childNodes[0].getElementsByTagName('latitude')[0]);
                    _params.long = Diversity.Web.XML.GetText(resp.childNodes[0].getElementsByTagName('longitude')[0]);

                    window.setTimeout(
						function () {
						    // initialise and load map to container
						    map.Init(_params);

						    for (var i = 0; i < resp.childNodes.length; i++) {
						        // add pin based-on xml data (response)
						        var _latLong = {};
						        _latLong.lat = Diversity.Web.XML.GetText(resp.childNodes[i].getElementsByTagName('latitude')[0]);
						        _latLong.long = Diversity.Web.XML.GetText(resp.childNodes[i].getElementsByTagName('longitude')[0]);



						        // location title variables
						        var rating = '', location_name = '', city = '';

						        for (var j = 1; j < parseInt(Diversity.Web.XML.GetText(resp.childNodes[i].getElementsByTagName('starrating_id')[0])); j++) {
						            rating = '*' + rating;
						        }

						        location_name = Diversity.Web.XML.GetText(resp.childNodes[i].getElementsByTagName('name')[0]);
						        city = Diversity.Web.XML.GetText(resp.childNodes[i].getElementsByTagName('city')[0]);

						        // set location title
						        _latLong.title = '<b>' + rating + ' ' + location_name + ' (' + city + ')</b>';



						        // location desc variables
						        var package_name = '', package_price = '', accommodation_base = '', legend = '', photo_id = '', photo_name = '', photo = '', base_url = '', location_id = '', location_name_url = '', moreInfoLabel = '';

						        package_name = Diversity.Web.XML.GetText(resp.childNodes[i].getElementsByTagName('package_name')[0]);
						        package_price = Diversity.Web.XML.GetText(resp.childNodes[i].getElementsByTagName('for_price')[0]);

						        // legend
						        accommodation_base = Diversity.Web.XML.GetText(resp.childNodes[i].getElementsByTagName('accommodation_base')[0]);
						        if (accommodation_base == 'accomodation') {
						            legend = $('paLabel').value;
						        } else {
						            legend = $('pppaLabel').value;
						        }

						        // photo
						        photo_id = Diversity.Web.XML.GetText(resp.childNodes[i].getElementsByTagName('photo_id')[0]);
						        photo_name = Diversity.Web.XML.GetText(resp.childNodes[i].getElementsByTagName('photo_name')[0]);
						        photo = '/Images/' + photo_id + '_' + photo_name;

						        base_url = '/Location/View/Name/';
						        location_id = Diversity.Web.XML.GetText(resp.childNodes[i].getElementsByTagName('id')[0]);
						        location_name_url = Diversity.Web.XML.GetText(resp.childNodes[i].getElementsByTagName('name_url')[0]);

						        moreInfoLabel = $('moreInfoLabel').value;

						        // set location description
						        _latLong.desc = package_name + '<br /><span class="price">&euro; ' + package_price + '</span> (' + legend + ')<br /><img src="' + photo + '" height="60" width="85" alt="no photo" /><a href="' + base_url + location_id + '_' + location_name_url + '" target="_parent" >' + moreInfoLabel + ' &gt;&gt;</a>';



						        // set location icon
						        if ($('hostHeader').value == 'vu.net') {
						            _latLong.customIcon = "<img src='/CR/Style/Img/pin_location.png' alt='pin' width='12' height='18'/>";
						        } else {
						            _latLong.customIcon = "<img src='/CR/Style/VbImg/pin_location.png' alt='pin' width='12' height='18'/>";
						        }



						        // add pin to map
						        map.AddPin(_latLong);
						    }
						}
						, 7000
					);


                    break;
                default:
                    contentDivs[index].innerHTML = response;

                    // realign bottom border based on tab content
                    AlignBottomBorder();
                    break;
            }
        }, null, false, true);
    } else {
        contentDivs[index].style.display = 'block';
        contentTabs[index].className = 'active';

        // realign bottom border based on tab content
        AlignBottomBorder();
    }
}

var ctref;

var jsalarm = {
    padfield: function (f) {
        return (f < 10) ? "0" + f : f
    },
    showcurrenttime: function () {
        var dateobj = new Date();

        if ((dateobj.getHours() == 0) && (dateobj.getMinutes() == 0) && (dateobj.getSeconds() == 0)) {
            clearInterval(jsalarm.timer);
            //alert('reload page');
            disableButton('boekNu1');
            disableButton('boekNu2');
        } else {
            var ct = (23 - dateobj.getHours()) + " uren, " + (59 - dateobj.getMinutes()) + " minuten, " + (59 - dateobj.getSeconds()) + " seconden";
            ctref.innerHTML = ct;
            ctref.setAttribute("title", ct);
        }
    },
    init: function () {
        //var dateobj=new Date();
        ctref = document.getElementById("jsalarm_ct");
        jsalarm.showcurrenttime();
        jsalarm.timer = setInterval(function () { jsalarm.showcurrenttime() }, 1000);
    }
}

function disableButton(id) {
    var button = document.getElementById(id);
    if (button != null) {
        button.className += ' disabled';
        button.href = '#';
        button.onclick = function () { return false; };
    }
} 
