﻿/**
 * static object that handles page logic
 */
var Logic = function($) {
    /**
    * private methods and variables
    */
    var priv = {
        debug: false, //(resources.debugmode == 'true'),		//enables/disables the error logging
        debugSeverity: 10, //level of error logging
        console: $("<ul>").addClass("debug-console"),

        initConsole: function() {
            //if the console has not yet been added to the document, do so now
            if (!priv.console.parentNode) {
                $("body").append(priv.console);
            }
        },

        submitKeywordSearch: function() {
            var whiteLabelQuery = (($(".whiteLabelField").attr("value") != "") ? ("&WhiteLabel=" + $(".whiteLabelField").attr("value")) : "");
            var whiteLabelQueryFirst = (($(".whiteLabelField").attr("value") != "") ? ("?WhiteLabel=" + $(".whiteLabelField").attr("value")) : "");

            var searchType = location.href.indexOf('searchmap.aspx') == -1 ? 'search' : 'map';
            var skeyword = $('#txtTextSearch').val().replace("%", "").replace("#", "").replace("&", "").replace(";", "").replace("<", "").replace(">", "");

            var fh_location = '';
            if (searchType == 'search') {
                fh_location = decodeURI($.query.get('fh_location'));
            } else {
                fh_location = decodeURIComponent(decodeURIComponent(Logic.getHashValue(location.href, 'fh_query')));
                fh_location = $.query.load('?' + fh_location).get('fh_location');
            }

            if (fh_location) {
                if (fh_location.indexOf('/$s') != -1) {
                    var startOfS = fh_location.indexOf('/$s');
                    var endOfs = fh_location.indexOf('/', startOfS + 1);
                    if (endOfs == -1) {
                        fh_location = fh_location.substring(0, startOfS);
                    }
                    else {
                        fh_location = fh_location.substring(0, startOfS) + fh_location.substring(endOfs);
                    }
                    fh_location = fh_location + '/$s=' + skeyword;
                }
                else {
                    fh_location += '/$s=' + skeyword;
                }
            }
            else {
                fh_location = '//vakantiemakelaar/nl_NL';
                var sortBy = $.query.get('fh_sort_by');
                if (sortBy != '') {
                    sortBy = '&fh_sort_by=' + sortBy;
                }
                fh_location = fh_location + '/$s=' + skeyword + sortBy;
            }

            if (searchType == 'search') {
                document.location.href = global.pathPrefix + '/search.aspx?fh_location=' + encodeURI(fh_location) + whiteLabelQuery;
            }
            else if (searchType == 'notification') {
                document.location.href = global.pathPrefix + '/mynotifications.aspx?fh_location=' + encodeURI(fh_location) + whiteLabelQuery;
            } else {
                document.location.href = global.pathPrefix + '/searchmap.aspx' + whiteLabelQueryFirst + '#fh_query=' + encodeURIComponent('fh_location=' + encodeURIComponent(fh_location));
                setTimeout("Searchmap.facetClick('" + location.href + "')", 150);
            }
        },

        initSubmenu: function() {
        if ($('div.submenu, div#submenu').length) {
                $('div.submenu ul li a, div#submenu ul li a').each(function(i) {
                    if (location.href.indexOf($(this).attr('href')) != -1) {
                        if (location.href.indexOf('view=') == -1 || $(this).attr('href').indexOf('view=') != -1) {
                            $(this).addClass('active');
                        }
                    }
                });
            }
        },

        bindEvents: function() {
            var photoCarousel = new PCarousel({ "speed": 500, "listItem": $("div.carousel ul").get(0), "prevButton": $("#pc-up").get(0), "nextButton": $("#pc-down").get(0), "direction": "vertical", "continues": false });
            if (location.href.indexOf('search.aspx') != -1) {
                $.cookie(global.cookiePrefix + "_searchmap", '', { expires: -1, path: '/' });
            }

            if ($('#most-viewed').length) {
                var mostviewedCarousel = new PCarousel({ "speed": 500, "listItem": $("#most-viewed ul").get(0), "prevButton": $("#most-viewed .lnk-left").get(0), "nextButton": $("#most-viewed .lnk-right").get(0), "direction": "horizontal", "continues": false });
            }

            $('#homeNewsEmailButton').bind('click', function() {
                var whiteLabelQuery = (($(".whiteLabelField").attr("value") != "") ? ("&WhiteLabel=" + $(".whiteLabelField").attr("value")) : "");
                var whiteLabelQueryFirst = (($(".whiteLabelField").attr("value") != "") ? ("?WhiteLabel=" + $(".whiteLabelField").attr("value")) : "");

                var email = $('#homeNewsEmail').val();
                var name = $('#newsletter #first').val();
                if (email != null && email != '') {
                    location.href = global.pathPrefix + "/newsletter.aspx?name=" + encodeURI(encodeURI(name)) + "&email=" + encodeURI(encodeURI(email)) + whiteLabelQuery;
                }
                else {
                    location.href = global.pathPrefix + "/newsletter.aspx" + whiteLabelQueryFirst;
                }
            });
        }
    };

    /**
    * public methods
    */
    return {

        bindFreeTextSearchEvents: function() {
            $('#txtTextSearch').keydown(function(e) {
                if (e.keyCode == 13) {
                    priv.submitKeywordSearch();
                    return false;
                }
            });
            $('#btnTextSearch').bind('click', function() {
                priv.submitKeywordSearch();
            });
        },

        getHashValue: function(href, key) {
            var hashString = '';
            var params = new Array();

            if (href == null) {
                href = location.href;
            }

            if (href.indexOf('#') != -1) {
                hashString = href.split('#')[1];
            }

            if (hashString.indexOf("&") != -1) {
                params = hashString.split("&");
            }
            else if (hashString.indexOf("=") != -1) {
                params[params.length] = hashString;
            }

            for (var j = 0; j < params.length; j++) {
                param = params[j].split("=");
                if (param[0] == key) {
                    return param[1];
                }
            }

            return '';
        },

        /**
        * initializes the page logic
        * to be called on $(document).ready
        */
        OnReady: function() {
            //add trim method to the string object
            String.prototype.stripTags = function() { return this.replace(/<(.|\n)*?>/g, ""); }
            String.prototype.endsWith = function(str) { return (this.match(str + "$") == str) }

            Logic.bindFreeTextSearchEvents();
            priv.bindEvents();
            priv.initSubmenu();

            if ($('#birthdate').length && $('#birthdate input[type=hidden]:eq(0)').val() != '-1') {
                $('#birthdate select[name=days]').val($('#birthdate input[type=hidden]:eq(0)').val());
                $('#birthdate select[name=months]').get(0).selectedIndex = $('#birthdate input[type=hidden]:eq(1)').val();
                $('#birthdate select[name=years]').val($('#birthdate input[type=hidden]:eq(2)').val());
            }

            if (typeof (Search) != "undefined") {
                Search.OnReady();
            }
            if (typeof (Searchmap) != "undefined") {
                Searchmap.OnReady();
            }
            if (typeof (Objects) != "undefined") {
                Objects.OnReady();
            }
            if (typeof (StaticPage) != "undefined") {
                StaticPage.OnReady();
            }
            if (typeof (Address) != "undefined") {
                Address.OnReady();
            }
        },

        OnResize: function() {

        },


        /**
        * handles the writing of debug messages
        * @param {String} msg
        * @param {Int} severity: 0 info, 1 warning, 2 error, 3 critical
        */
        writeDebug: function(msg, severity) {
            if (typeof (severity) == "undefined") {
                severity = 0;
            }

            if (priv.debug && severity <= priv.debugSeverity) {
                //check whether debug console exists
                if (typeof (console) != "undefined" && console.log) {
                    console.log(msg);
                }
                //if no debug console exists: create debug div
                else {
                    priv.initConsole();
                    $(priv.console).append(
						$("<li>").html(msg)
					);
                }
            }
        }

    }
} (jQuery);

/**
 * Initiate onload methods and functions
 */
$(document).ready(
	function(){
		Logic.OnReady();
	}
);

/**
 * Bind resize eventes
 */
$(window).bind('resize', 
	function(){
		Logic.OnResize();
});

/* Javascript Date Selector
   by Warren Brown (03/01/2004 Radiokop South Africa)

   Script to place Month/day/year onto a web page, leap year enabled
*/

var date_arr = new Array;
var days_arr = new Array;

date_arr[0]=new Option("januari",31);
date_arr[1]=new Option("februari",28);
date_arr[2]=new Option("maart",31);
date_arr[3]=new Option("april",30);
date_arr[4]=new Option("mei",31);
date_arr[5]=new Option("juni",30);
date_arr[6]=new Option("juli",31);
date_arr[7]=new Option("augustus",30);
date_arr[8]=new Option("september",30);
date_arr[9]=new Option("oktober",31);
date_arr[10]=new Option("november",31);
date_arr[11]=new Option("december",30);

function fill_select(f)
{
        document.writeln("<SELECT name=\"days\" onchange=\"set_hiddeninputs()\"></SELECT>");
        document.writeln("<SELECT name=\"months\" style=\"width:71px;\" onchange=\"update_days(document.aspnetForm)\">");
        for(x=0;x<12;x++)
                document.writeln("<OPTION value=\""+date_arr[x].value+"\">"+date_arr[x].text);
        document.writeln("</SELECT>");
        selection=f.months[f.months.selectedIndex].value;
}

function update_days(f)
{
        temp=f.days.selectedIndex;
        for(x=days_arr.length;x>0;x--)
        {
                days_arr[x]=null;
                f.days.options[x]=null;
         }
        selection=parseInt(f.months[f.months.selectedIndex].value);
        ret_val = 0;
        if(f.months[f.months.selectedIndex].value == 28)
        {
                year=parseInt(f.years.options[f.years.selectedIndex].value);
                if (year % 4 != 0 || year % 100 == 0 ) ret_val=0;
                else
                        if (year % 400 == 0)  ret_val=1;
                        else
                                ret_val=1;
        }
        selection = selection + ret_val;
        for(x=1;x < selection+1;x++)

        {
                days_arr[x-1]=new Option(x);
                f.days.options[x-1]=days_arr[x-1];
        }
        if (temp == -1) f.days.options[0].selected=true;
        else
             f.days.options[temp].selected=true;
             
        set_hiddeninputs();
}
function set_hiddeninputs(){
    $('#birthdate input[type=hidden]:eq(0)').val($('#birthdate select[name=days]').val());
    $('#birthdate input[type=hidden]:eq(1)').val($('#birthdate select[name=months]').get(0).selectedIndex);
    $('#birthdate input[type=hidden]:eq(2)').val($('#birthdate select[name=years]').val());
}
function year_install(f)
{
        document.writeln("<SELECT name=\"years\" onchange=\"update_days(document.aspnetForm)\">")
        var curYear = new Date().getFullYear();
        for(x=(curYear-100);x<=curYear;x++) document.writeln("<OPTION value=\""+x+"\">"+x);
        document.writeln("</SELECT>");
        update_days(f)
}