// includes
document.write('<scr\ipt src="\/includes\/cookie.js" type="text\/javascript"><\/scr\ipt>');
document.write('<scr\ipt src="\/includes\/http_xml.js" type="text\/javascript"><\/scr\ipt>');

function favorites_display(id, total, added) {
    var uid = getCookie(COOKIE_USER_ID);
    total = (/(\d+)/.test(total))                 ? total : '';
    added = (added == 'true' || added == 'false') ? added : '';
    var values = new Object();
    var content = new Object();

    // values
    values = new favorites_values(id, total, added);

    //alert("Total: " + total + " Added: " + added + " && " + "Total: " + values.counter + " Added: " + values.flag);
    // exit statement
    if ((total == '' || added == '') && (values.counter == '' || values.flag == '') && (uid != "")) return false;

    // content
    content = new favorites_link(values.flag, values.counter, id);

    // toggle content
    if (document.getElementById("favorites_toggle_holder_" + id)) {
        var favorites_toggle = document.createElement("span");
        favorites_toggle.innerHTML = content.toggle;
        // clear holder
        var favorites_toggle_holder = document.getElementById("favorites_toggle_holder_" + id);
        while (favorites_toggle_holder.firstChild)
            favorites_toggle_holder.removeChild(favorites_toggle_holder.firstChild);
        // add content
        favorites_toggle_holder.appendChild(favorites_toggle);
    }

    // view content
    if (document.getElementById("favorites_view_holder")) {
        var favorites_view = document.createElement("span");
        favorites_view.innerHTML = content.view;
        // clear holder
        var favorites_view_holder = document.getElementById("favorites_view_holder");
        while (favorites_view_holder.firstChild)
            favorites_view_holder.removeChild(favorites_view_holder.firstChild);
        // add content
        favorites_view_holder.appendChild(favorites_view);
    }
}

function favorites_values(id, total, added) {
    // user logged in favorites
    if (getCookie(COOKIE_USER_ID) != "") {
        // first time called -> get values
        if (total == '' && added == '') {
            // not viewing a property -> get number of favorites
            if ((id == '' || id == '0') && getCookie(COOKIE_FAVORITES) != "") {
                this.counter = getCookie(COOKIE_FAVORITES) || '0';
                this.flag    = "false";
            } else {
                // send request
                favorites_request('get_favorites', id);
                this.counter = '';
                this.flag    = '';
            }
        // return values
        } else {
            this.counter = total;
            this.flag    = added;

        }

    // temporary favorites
    } else {
        this.counter = 0;
        this.flag = 'false';
        var cookie_array = new Array();
        cookie_array = getCookie(COOKIE_FAVORITES).split(",");

        for (var i = 0; i < cookie_array.length; i++) {
            if (/(\d+)/.test(cookie_array[i])) {
                this.counter++;

                if (cookie_array[i] == id && id != '')
                    this.flag = 'true';
            }
        }
    }
}

function favorites_link(added, total, id) {
    // toggle favorites
    if (added == 'true') {
        this.toggle = "<a href=\"\" onClick=\"favorites_remove('" + id + "'); return false;\"><img src=\"/images/button-remove-favorites.gif\" border=\"0\" /></a>";
    } else {
        this.toggle = "<a href=\"\" onClick=\"favorites_add('" + id + "'); return false;\"><img src=\"/images/button-add-favorites.gif\" border=\"0\" /></a>";
    }

    // view favorites
    //var total_favorites = (total > 0) ? " - (" + total + ")" : "";
    var total_favorites = " - (" + total + ")";
    var total_favorites_text = "Favorites" + total_favorites;
    this.view = "<a href=\"/vacation-rentals/public/favorites.shtml\" id=\"view_" + id + "\" style=\"font-weight:bold; text-decoration:none;\">"
        + total_favorites_text
        + "</a>";

    // update total favorites count for all occurrences
    var anchors = new Array();
    anchors = document.getElementsByTagName("a");
        for (var j = 0; j < anchors.length; j++) {
        if (/view_(\d+)/.test(anchors[j].id)) {
            var number = RegExp.$1;
            document.getElementById("view_" + number).innerHTML = total_favorites_text;
        }
    }
}

function favorites_add(id) {
    // user logged in favorites
    if (getCookie(COOKIE_USER_ID) != "") {
        // send request
        favorites_request('add_favorites', id);

    // temporary favorites
    } else {
        // get value
        var cookie_value = getCookie(COOKIE_FAVORITES);
        cookie_value += "," + id;

        // set cookie
        setCookie(COOKIE_FAVORITES,cookie_value,COOKIE_EXPIRE);

        // display links
        favorites_display(id);
    }
}

function favorites_remove(id) {
    // user logged in favorites
    if (getCookie(COOKIE_USER_ID) != "") {
        // send request
        favorites_request('remove_favorites', id);

    // temporary favorites
    } else {
        var cookie_value = '';

        var cookie_array = new Array();
        cookie_array = getCookie(COOKIE_FAVORITES).split(",");

        for (var i = 0; i < cookie_array.length; i++)
            if (cookie_array[i] != id && cookie_array[i] != '')
                cookie_value += "," + cookie_array[i];

        // set cookie
        setCookie(COOKIE_FAVORITES,cookie_value,COOKIE_EXPIRE);

        // display links
        favorites_display(id);
    }
}

function favorites_request(request_type, pid) {
    var path = '/cgi-bin/vacation-rentals/public/favorites.cgi';

    var http = new SimpleAJAX();
    http.setAction(path);
    http.setMethod('POST');
    http.addParameter(request_type,'1');
    http.addParameter('pid',pid);
    http.addParameter('cache_buster',new Date().getTime());
    http.setHandler(favorites_response);
    http.send();
}

function favorites_response(XML) {
    try {
        var info = XML.getNodeGroup('info');

        var pid = XML.getNodeValue('pid',info[0]);
        var total = XML.getNodeValue('total',info[0]);
        var added = XML.getNodeValue('added',info[0]);

        // display links
        favorites_display(pid, total, added);
    } catch(e) { }
}