function CS(settings)
{
    this.rootUrl = settings.rootUrl;
    this.waitMessage = settings.waitMessage;
    this.sendMessageButton = settings.sendMessageButton;
    this.newMessagesSwitch = settings.newMessagesSwitch;
    this.newMessagesSwitchMessages = settings.newMessagesSwitchMessages;
    this.flipStatus = false;
    this.conversationMessageCount = 0;
    this.conversationMessageSelected = {};
    this.i = 1;
    this.helpTooltipCache = [];
    this.helpTooltipTimers = [];
    this.myAccount = {};
    this.myAccount.photoDialog = function(username, id)
    {
        if (window.webcam.loaded)
        {
            $('#webcamContainer').hide();
        }
        var dialog = $('#dialog-editphoto-' + id + '-div').dialog({
            height: 100,
            width: 100,
            modal: true,
            bgiframe: false,
            draggable: false,
            resizable: false,
            zIndex: 80
        });
        var image = {
            height: $('#dialog-editphoto-' + id + '-img').height(),
            width: $('#dialog-editphoto-' + id + '-img').width(),
            realHeight: $('#dialog-editphoto-' + id + '-hiddenimg').height(),
            realWidth: $('#dialog-editphoto-' + id + '-hiddenimg').width()
        };
        var form = {
            coords: $('#dialog-editphoto-' + id + '-thumb-coords')
        };
        if (image.height < 550)
        {
            dialog.dialog('option', 'height', 550);
        }
        else
        {
            dialog.dialog('option', 'height', image.height + 50);
        }
        dialog.dialog('option', 'width', image.width + 300);
        dialog.dialog('option', 'position', 'center');
        var onSelectChange = function(img, selection)
        {
            var scaleX = 150 / (selection.width || 1);
            var scaleY = 150 / (selection.height || 1);
            $('#dialog-editphoto-' + id + '-thumb').css(
            {
                width: Math.round(scaleX * image.width) + 'px',
                height: Math.round(scaleY * image.height) + 'px',
                marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
                marginTop: '-' + Math.round(scaleY * selection.y1) + 'px'
            });
        };
        var ias = $('#dialog-editphoto-' + id + '-img').imgAreaSelect(
        {
            x1: 0,
            y1: 0,
            x2: 150,
            y2: 150,
            zIndex: 100,
            instance: true,
            handles: true,
            hide: false,
            show: true,
            parent: '#dialog-editphoto-' + id + '-div',
            minHeight: 50,
            minWidth: 50,
            aspectRatio: '1:1',
            onSelectChange: onSelectChange,
            onSelectEnd: function (img, selection) {
                if ((selection.height == 0) ||
                    (selection.width == 0))
                {
                    var newCoords = {
                        x1: selection.x1,
                        y1: selection.y1,
                        x2: selection.x2,
                        y2: selection.y2,
                        width: selection.x2 - selection.x1,
                        height: selection.y2 - selection.y1
                    };
                    if (selection.width == 0)
                    {
                        newCoords.x2 += 50;
                        newCoords.width = 50;
                    }
                    if (selection.height == 0)
                    {
                        newCoords.y2 += 50;
                        newCoords.height = 50;
                    }
                    if (newCoords.x2 > image.width)
                    {
                        var widthDiff = newCoords.x2 - image.width;
                        newCoords.x1 = newCoords.x1 - widthDiff;
                        newCoords.x2 = newCoords.x2 - widthDiff;
                    }
                    if (newCoords.y2 > image.height)
                    {
                        var heightDiff = newCoords.y2 - image.height;
                        newCoords.y1 = newCoords.y1 - heightDiff;
                        newCoords.y2 = newCoords.y2 - heightDiff;
                    }
                    ias.setOptions(newCoords);
                    ias.setSelection(newCoords.x1, newCoords.y1, newCoords.x2, newCoords.y2);
                    onSelectChange(img, newCoords);
                }
                else
                {
                    var realSelection = {
                        x1: Math.floor((image.realWidth / image.width) * selection.x1),
                        y1: Math.floor((image.realHeight / image.height) * selection.y1),
                        x2: Math.floor((image.realWidth / image.width) * selection.x2),
                        y2: Math.floor((image.realHeight / image.height) * selection.y2)
                    };
                    form.coords.val(realSelection.x1 + ',' + realSelection.y1 + ',' + realSelection.x2 + ',' + realSelection.y2);
                }
            }
        });
        dialog.dialog('option', 'beforeClose', function()
        {
            if (window.webcam.loaded)
            {
                $('#webcamContainer').show();
            }
        });
    };
}

CS.prototype.initPage = function()
{
    var oThis = this;
    $('a.enlarge').lightBox();
    if (!(($.browser.msie) && ($.browser.version < 8)))
    {
        $("#slider").easySlider(
        {
            auto: true,
            continuous: true,
            speed: 2500,
            pause: 7500,
            numeric: true
        });
        $("#controls").append('<span id="slider_all"><a href="' + oThis.rootUrl + 'profileoverview"><img src="' + oThis.rootUrl + 'images/slider/all_members.png" style="" /></a></span>');
        $('ol#controls li a').each(function(index, value)
        {
            $(value).html('');
        });
    }
    else
    {
        $('.hiddenforie7').hide();
    }
    $('a.help').each(function(index, element)
    {
        var element = $(element);
        var helpFile = element.attr('href');
        element.attr('href', 'javascript:void(0);');
        element.hover(
            function() {
                if (oThis.helpTooltipTimers[helpFile])
                {
                    clearTimeout(oThis.helpTooltipTimers[helpFile]);
                    delete oThis.helpTooltipTimers[helpFile];
                }
                else
                {
                    var offset = element.offset();
                    var tLeft = offset.left;
                    var tTop = offset.top;
                    if (!oThis.helpTooltipCache[helpFile])
                    {
                        $.get(oThis.rootUrl + 'help/' + helpFile + '.html', function(data)
                        {
                            oThis.helpTooltipCache[helpFile] = data;
                            oThis.showHelpTooltip(helpFile, tLeft, tTop);
                        });
                    }
                    else
                    {
                        oThis.showHelpTooltip(helpFile, tLeft, tTop)
                    }
                }
            },
            function() {
                oThis.helpTooltipTimers[helpFile] = setTimeout(function()
                {
                    oThis.hideHelpTooltip(helpFile);
                }, 500);
            }
        );
    });
    if (this.newMessagesSwitch)
    {
        $(this.newMessagesSwitchMessages).each(function(index, value)
        {
            setTimeout(function()
            {
                var i = oThis.getI();
                $.gritter.add({
                    title: '<a href="' + oThis.rootUrl + 'inbox">' + value.senderName + '</a>',
                    text: '<a href="' + oThis.rootUrl + 'inbox">' + value.message + '</a>',
                    image: oThis.rootUrl + 'photo/' + value.senderName + '/' + value.senderImage + '/thumb',
                    sticky: true,
                    time: 10000
                });
            }, (2500 + Math.ceil(Math.random() * 5000) + (i * 200))); // Show the notification blob after a minimum of 2.5 secconds + up to 7.5 secconds randomly + 0.2 secconds between consecutive blobs
        });
    }
};

CS.prototype.showHelpTooltip = function(index, tLeft, tTop)
{
    var oThis = this;
    $("body").prepend("<div id=" + index + " class='tip'><div class='tipMid'>"  + oThis.helpTooltipCache[index] + "</div><div class='tipBtm'></div></div>");
    var tip = $('div#' + index + '.tip');
    var topOffset = tip.height();
    var xTip = (tLeft - 30) + 'px';
    var yTip = (tTop - topOffset - 60) + 'px';
    tip.css(
    {
        top: yTip,
        left : xTip
    });
    tip.animate(
    {
        top: '+=20px',
        opacity: 'toggle'
    }, 200);
};

CS.prototype.hideHelpTooltip = function(index)
{
    var oThis = this;
    delete oThis.helpTooltipTimers[index];
    $('div#' + index + '.tip').animate(
    {
        top: '+=20px',
        opacity: 'toggle'
    },
    {
        duration: 200,
        complete: function()
        {
            $(this).remove();
        }
    });
};

CS.prototype.getI = function()
{
    return this.i++;
};

CS.prototype.initSearch = function()
{
    var oThis = this;
    $(".searchname").each(function(i)
    {
        $(this).keyup(function(event)
        {
            clearTimeout(this.timeoutSearchId);
            $(".searchattributes").val('');
            if ((event.keyCode == 8) || (event.keyCode == 46) ||
                !((event.keyCode < 32)  || ((event.keyCode >= 33) && (event.keyCode < 46))  || ((event.keyCode >= 112) && (event.keyCode <= 123))))
            {
                this.timeoutSearchId = setTimeout(function()
                {
                    oThis.doSearch('search', '#search_results');
                }, 250);
            }
        });
    });
    $(".searchattributes").each(function(i)
    {
        $(this).change(function()
        {
            $(".searchname").val('');
            oThis.doSearch('search', '#search_results');
        });
    });
};

CS.prototype.post = function(url, data, func)
{
    $.post(this.rootUrl + url, data, func);
};

CS.prototype.get = function(url, func)
{
    $.get(this.rootUrl + url, func);
};

CS.prototype.preAlert = function()
{
    $.modal(this.waitMessage,
    {
         modal: true,
         close: true,
         zIndex: 500,
         escClose: false,
         minWidth: '350px',
         minHeight: '200px'
    });
    $('#simplemodal-container').css('height', '200px');
    $('#simplemodal-data').css('top', '0px');
};

CS.prototype.alert = function(text, func)
{
    var oThis = this;
    this.preAlert();
    $("#simplemodal-data").html(text + '<br /><a id="modal-close"><img src="' + this.rootUrl + '/images/ok_button.png" style="border: 0px; cursor: pointer; margin: 10px;" /></a>');
    $("#modal-close").click(function(){
        oThis.hideAlert();
        if (func)
        {
            func();
        }
    });
};

CS.prototype.confirm = function(text, func)
{
    var oThis = this;
    this.preAlert();
    $("#simplemodal-data").html(text + '<br /><a id="modal-yes"><img src="' + this.rootUrl + '/images/yes_button.png" style="border: 0px; cursor: pointer; margin: 10px;" /></a><a id="modal-no"><img src="' + this.rootUrl + '/images/no_button.png" style="border: 0px; cursor: pointer; margin: 10px;" /></a>');
    $("#modal-yes").click(function(){
        oThis.hideAlert();
        if (func)
        {
            func();
        }
    });
    $("#modal-no").click(function(){
        oThis.hideAlert();
    });
};

CS.prototype.hideAlert = function()
{
    $.modal.close();
};

CS.prototype.reverseString = function(str)
{
    var outputStr = '';
    for (i = 0; i < str.length; i++)
    {
        outputStr = str.charAt(i) + outputStr;
    }

    return outputStr;
};

CS.prototype.convertResponseToArray = function(response, delimiter)
{
    return response.split(delimiter);
};

CS.prototype.insertAtCursor = function(myField, myValue)
{
    //IE support
    if (document.selection)
    {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
    }

    //MOZILLA/NETSCAPE support
    else if (myField.selectionStart || myField.selectionStart == '0')
    {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
    }
    else
    {
        myField.value += myValue;
    }

    $(myField).change();
};

CS.prototype.getFormValues = function(fname)
{
    if ((typeof(fname) == 'object') &&
        (fname instanceof Array))
    {
        var str = "";

        for(i = 0; i < fname.length; i++)
        {
            str += this.getFormValues(fname[i]) + "&";
        }

        str = str.substr(0, str.length - 1);

        return str;
    }
    else
    {
        return $("#" + fname).serialize();
    }
};

CS.prototype.refreshValidationImage = function()
{
     var tmp = new Date();
     var str = "?" + tmp.getTime();
     $("#comment_form_validation").attr("src", this.rootUrl + "validation" + str);
};

CS.prototype.createBookmark = function(url, title)
{
    if (window.sidebar)
    {
        window.sidebar.addPanel(title, url, "");
    }
    else if (window.external)
    {
        window.external.AddFavorite(url, title);
    }
    else if (window.opera && window.print)
    {
        return true;
    }
};

CS.prototype.changePassword = function(form)
{
    var oThis = this;
    this.preAlert();
    this.post('account/newpassword', this.getFormValues(form), function(responseText)
    {
        oThis.alert(responseText, function()
        {
            window.location = oThis.rootUrl + 'account/settings';
        });
    });
};

CS.prototype.flirt = function(username, form)
{
    var oThis = this;
    this.preAlert();
    this.post('flirt/send/' + username, form, function(responseText)
    {
        oThis.alert(responseText);
    });
};

CS.prototype.changeTitleChannel = function(url, title)
{
    var oThis = this;
    this.preAlert();
    this.post(url, 'title=' + title, function(responseText)
    {
        var splittedResponse = oThis.convertResponseToArray(responseText, ';');
        oThis.alert(splittedResponse[0]);
    });
};

CS.prototype.newThumb = function(url, coords)
{
    var oThis = this;
    this.preAlert();
    this.post(url, 'coords=' + coords, function(responseText)
    {
        var splittedResponse = oThis.convertResponseToArray(responseText, ';');
        oThis.alert(splittedResponse[0]);
    });
};

CS.prototype.showVote = function(rating_el)
{
    $(rating_el).attr("class", "tr_visible");
};

CS.prototype.vote = function(url, form, target, func)
{
    var oThis = this;
    this.preAlert();
    this.post(url, this.getFormValues(form), function(responseText)
    {
        var splittedResponse = oThis.convertResponseToArray(responseText, '(_._)');
        $(target).html(splittedResponse[1]);
        $('input[type=radio].star').rating();
        oThis.alert(splittedResponse[2]);
        if (func)
        {
            func();
        }
    });
};

CS.prototype.addPageReaction = function(url, form, result)
{
    var oThis = this;
    this.preAlert();
    this.post(url, this.getFormValues(form), function(responseText)
    {
        var splittedResponse = oThis.convertResponseToArray(responseText, ';');
        oThis.alert(splittedResponse[0]);
        if (splittedResponse[1] == "1")
        {
            $(':input', '#' + form).not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');
        }
    });
};

CS.prototype.doSearch = function(form, target)
{
    this.post('search/new', this.getFormValues(form), function(responseText)
    {
        $(target).html(responseText);
    });
};

CS.prototype.sendMessage = function()
{
    var oThis = this;
    this.preAlert();
    $("#site_submit_button").val(this.waitMessage);
    $("#site_submit_button").attr('disabled', true);
    this.post('message/send', this.getFormValues('profile_message') + '&country=' + $("[name=land]").val(), function(responseText)
    {
        var splittedResponse = oThis.convertResponseToArray(responseText, ';');
        oThis.alert(splittedResponse[1], function()
        {
            if (splittedResponse[0] == 'success')
            {
                window.location = splittedResponse[2];
            }
            else
            {
                $("#site_submit_button").val(oThis.sendMessageButton);
                $("#site_submit_button").attr('disabled', false);
            }
        });
    });
};

CS.prototype.addToFavorites = function(user_name, func)
{
    var oThis = this;
    this.preAlert();
    this.get('account/flipfavorite/' + user_name, function(responseText)
    {
        var splittedResponse = oThis.convertResponseToArray(responseText, ';');
        oThis.alert(splittedResponse[1]);
        func(splittedResponse);
    });
};

CS.prototype.inboxToggleFavorites = function(user_name, div)
{
    var oThis = this;
    this.addToFavorites(user_name, function(splittedResponse)
    {
        if (splittedResponse[0])
        {
            $(div).html(oThis.inboxToggleFavNo);
        }
        else
        {
            $(div).html(oThis.inboxToggleFavYes);
        }
        $(div).css('color', 'white');
    });
};

CS.prototype.photoToggleFavorites = function(user_name, img_object)
{
    var oThis = this;
    this.addToFavorites(user_name, function(splittedResponse)
    {
        if (img_object != null)
        {
            //TODO: change the alt of the picture
            if(splittedResponse[0])
            {
                $(img_object).attr("src", oThis.rootUrl + 'images/layout/delfavo.jpg');
            }
            else
            {
                $(img_object).attr("src", oThis.rootUrl + 'images/layout/addfavo.jpg');
            }
        }
    });
};

CS.prototype.flipFavouriteBackground = function(username, image_id)
{
    el = $('#favourite_' + username);

    if (el)
    {
        bg = el.css("backgroundImage").toString();
        if (bg.substr(bg.length - 7,5) == "thumb")
        {
            el.css("backgroundImage", "url(" + this.rootUrl + "images/favourite.jpg)");
        }
        else
        {
            el.css("backgroundImage", "url(" + this.rootUrl + "photo/" + username + "/" + image_id + "/thumb)");
        }
    }
};

CS.prototype.showProfileWebcam = function(url, wnd)
{
    var v_width = 800;
    var v_height = 520;

    if (! wnd) var wnd = window;

    var params = new Array(
                    'left=' + (screen.availWidth/2 - v_width/2),
                    'top=' + (screen.availHeight/2 - v_height/2),
                    'width=' + v_width,
                    'height=' + v_height,
                    'border=0',
                    'scrollbars=1',
                    'resize=0',
                    'status=0',
                    'toolbar=0',
                    'menubar=0',
                    'location=0'
                );
    wnd.open(url, 'profiel', params.join(','));
};

CS.prototype.noenter = function(e)
{
    var keyCode = window.event ? window.event.keyCode : (e.which ? e.which : e.keyCode);
    return !(keyCode == 13);
};

CS.prototype.flipCheckAll = function(selector, status)
{
    $(selector).each(function()
    {
        $(this).attr('checked', status);
    });
};

CS.prototype.flipCheckStatus = function()
{
    this.flipStatus = !this.flipStatus;
    return this.flipStatus;
};

CS.prototype.setConversationMessageCount = function(convoCount)
{
    this.conversationMessageCount = convoCount;
};

CS.prototype.getConversationMessageCount = function()
{
    return this.conversationMessageCount;
};

CS.prototype.addToggleConversationMessage = function(message_id)
{
    this.conversationMessageSelected[message_id] = false;
};

CS.prototype.toggleConversationMessage = function(message_id)
{
    if (this.conversationMessageSelected[message_id])
    {
        this.conversationMessageSelected[message_id] = false;
        $('#msg_convo_id_' + message_id).css('backgroundImage', 'url(' + this.rootUrl + 'images/but_standaard.jpg)');
        $('#msg_convo_a_id_' + message_id).html(this.select);
    }
    else
    {
        this.conversationMessageSelected[message_id] = true;
        $('#msg_convo_id_' + message_id).css('backgroundImage', 'url(' + this.rootUrl + 'images/but_selected_standaard.png)');
        $('#msg_convo_a_id_' + message_id).html(this.deselect);
    }
};

CS.prototype.toggleAllConversationMessages = function()
{
    var deselect = true;
    for (var i in this.conversationMessageSelected)
    {
        if (!this.conversationMessageSelected[i])
        {
            deselect = false;
        }
    }
    if (deselect)
    {
        for (var i in this.conversationMessageSelected)
        {
            this.conversationMessageSelected[i] = false;
            $('#msg_convo_id_' + i).css('backgroundImage', 'url(' + this.rootUrl + 'images/but_standaard.jpg)');
            $('#msg_convo_a_id_' + i).html(this.select);
        }
    }
    else
    {
        for (var i in this.conversationMessageSelected)
        {
            this.conversationMessageSelected[i] = true;
            $('#msg_convo_id_' + i).css('backgroundImage', 'url(' + this.rootUrl + 'images/but_selected_standaard.png)');
            $('#msg_convo_a_id_' + i).html(this.deselect);
        }
    }
};

CS.prototype.deleteToggledConversationMessages = function()
{
    var form_string = '';
    for (var i in this.conversationMessageSelected)
    {
        if (this.conversationMessageSelected[i])
        {
            form_string += 'message_id[]=' + i + '&';
            var convoCount = this.getConversationMessageCount();
            convoCount--;
            this.setConversationMessageCount(convoCount);
        }
    }
    if (form_string != '')
    {
        form_string = form_string.substr(0, form_string.length - 1);
        var oThis = this;
        this.preAlert();
        this.post('message/delete', form_string, function(responseText)
        {
            var splittedResponse = oThis.convertResponseToArray(responseText, '(_._)');
            if (splittedResponse[0])
            {
                oThis.hideAlert();
                var convoCount = oThis.getConversationMessageCount();
                if(convoCount == 0)
                {
                    window.location = oThis.rootUrl + 'inbox';
                }
                for (var i in oThis.conversationMessageSelected)
                {
                    if (oThis.conversationMessageSelected[i])
                    {
                        $('#convo_message_' + i).fadeTo(300, 0.01, function()
                        {
                            $(this).slideUp();
                        });
                        delete oThis.conversationMessageSelected[i];
                    }
                }
            }
            else
            {
                oThis.alert(splittedResponse[1]);
            }
        });
    }
};

CS.prototype.removeConversationMessage = function(message_id, div)
{
    var oThis = this;
    this.preAlert();
    this.post('message/delete', 'message_id=' + message_id, function(responseText)
    {
        var splittedResponse = oThis.convertResponseToArray(responseText, '(_._)');
        if (splittedResponse[0])
        {
            oThis.hideAlert();
            var convoCount = oThis.getConversationMessageCount();
            convoCount--;
            oThis.setConversationMessageCount(convoCount);
            if(convoCount == 0)
            {
                window.location = oThis.rootUrl + 'inbox';
            }
            $(div).fadeTo(300, 0.01, function()
            {
                $(this).slideUp();
            });
        }
        else
        {
            oThis.alert(splittedResponse[1]);
        }
    });
};

CS.prototype.messagesConversationToggle = function(div)
{
    $(div).slideToggle();
};

CS.prototype.voteHotOrNot = function(hotness)
{
    var oThis = this;
    var hotorNotUsername = this.hotorNotUsername;
    this.preAlert();
    this.post('hotornot/vote/' + this.hotorNotUsername + '/' + this.hotorNotImageId + '/', 'hotornot_value=' + hotness, function(responseText)
    {
        var prevImage = $('#hotornot_photo_img').attr('src');
        var splittedResponse = oThis.convertResponseToArray(responseText, '(_._)');
        oThis.loadProfileHotorNot(splittedResponse);
        oThis.hideAlert();
        $('#hotornot_photo_img_prev').attr('src',prevImage).click(function()
        {
            window.location = oThis.rootUrl + 'profile/' + hotorNotUsername
        });;
        $('#hotornot_the_rest_prev').html(splittedResponse[3]);
        $('#hotornot_you_prev').html(hotness);
        if ($('#hotornot_photo_prev_div').css('display') == 'none')
        {
            $('#hotornot_photo_prev_div').slideDown();
        }
    });
};

CS.prototype.loadProfileHotorNot = function(splittedResponse)
{
    if (splittedResponse[1] == 0)
    {
        $('.hotornot_main').each(function()
        {
            $(this).slideUp();
        });
        $('#hotornot_warning').slideDown();
    }
    else
    {
        this.setHotorNot(splittedResponse[1], splittedResponse[0]);
        $('#hotornot_photo_img').attr('src', this.rootUrl + 'photo/' + splittedResponse[0] + '/' + splittedResponse[1] + '/thumb');
        $('#hotornot_photo_a').attr('href', this.rootUrl + 'photo/' + splittedResponse[0] + '/' + splittedResponse[1]);
        $('#hotornot_age').html(splittedResponse[2]);
        var oThis = this;
        $('#hotornot_username').html(splittedResponse[0]).click(function()
        {
            window.location = oThis.rootUrl + 'profile/' + splittedResponse[0]
        });
        $('#hotornot_photo_a').unbind('click').lightBox();
        $('#hotornot_profile_a').attr('href', this.rootUrl + 'profile/' + splittedResponse[0]);
        $('#hotornot_message_a').attr('href', this.rootUrl + 'message/' + splittedResponse[0]);
    }
};

CS.prototype.setHotorNot = function(imageId, username)
{
    this.hotorNotImageId = imageId;
    this.hotorNotUsername = username;
};

CS.prototype.getHotorNot = function()
{
    return {imageId: this.hotorNotImageId, username: this.hotorNotUsername};
};

CS.prototype.filterHotorNot = function(gender)
{
    var oThis = this;
    this.preAlert();
    this.post('hotornot/gender/', 'hotornot_filter=' + gender, function(responseText)
    {
        var splittedResponse = oThis.convertResponseToArray(responseText, '(_._)');
        oThis.loadProfileHotorNot(splittedResponse);
        oThis.hideAlert();
    });
    this.setFilterHotorNot(gender);
};

CS.prototype.setFilterHotorNot = function(gender)
{
    switch(gender)
    {
        case 0:
            $('#horornot_gender_f').attr('checked', 'checked').attr('disabled', 'disabled');
            $('#horornot_gender_m').attr('checked', '').attr('disabled', '');
            $('#horornot_gender_b').attr('checked', '').attr('disabled', '');
            break;
        case 1:
            $('#horornot_gender_f').attr('checked', '').attr('disabled', '');
            $('#horornot_gender_m').attr('checked', 'checked').attr('disabled', 'disabled');
            $('#horornot_gender_b').attr('checked', '').attr('disabled', '');
            break;
        case 2:
            $('#horornot_gender_f').attr('checked', '').attr('disabled', '');
            $('#horornot_gender_m').attr('checked', '').attr('disabled', '');
            $('#horornot_gender_b').attr('checked', 'checked').attr('disabled', 'disabled');
            break;
    }
};

CS.prototype.favHotorNot = function()
{
    var oThis = this;
    this.preAlert();
    this.addToFavorites(this.hotorNotUsername, function(splittedResponse)
    {
        //
    });
};

CS.prototype.flirtModal = function(username)
{
    this.post('flirt/getflirts/', 'username=' + username, function(responseText)
    {
        $.modal('<span style="text-align: left;">' + responseText + '</span>',
        {
            modal: true,
            close: true,
            zIndex: 500,
            escClose: false,
            minWidth: '350px',
            minHeight: '250px'
        });
        $('#simplemodal-container').css('height', '250px');
        $('#simplemodal-data').css('top', '0px');
    });
};

CS.prototype.toggleCheckbox = function(selector)
{
    if ($(selector).attr("checked"))
    {
        $(selector).attr("checked",'');
    }
    else
    {
        $(selector).attr("checked",'checked');
    }
};

CS.prototype.commentsAjaxLoad = function(username, imageId, targetDiv, func)
{
    this.get('photopage/' + username + '/' + imageId + '/ajax_comments/', function(responseText)
    {
        $(targetDiv).html(responseText);
        $('input[type=radio].star').rating();
        if (func)
        {
            func();
        }
    });
};

$.getDocHeight = function()
{
    return Math.max(
        $(document).height(),
        $(window).height(),
        /* For opera: */
        document.documentElement.clientHeight
    );
};

$.getDocWidth = function()
{
    return Math.max(
        $(document).width(),
        $(window).width(),
        /* For opera: */
        document.documentElement.clientWidth
    );
};

CS.prototype.switchStyle = function(style)
{
    var currentStyle = $('#customStyle');
    if (currentStyle.length > 0)
    {
        currentStyle.remove();
    }
    switch (style)
    {
        case 'wit':
        case 'grijs':
            $('head').append('<link id="customStyle" rel="stylesheet" href="' + this.rootUrl + 'css/' + style + '.css" />');
            var postStyle = style;
            break;
        default:
            var postStyle = 'default';
            break;
    }
    $.post(this.rootUrl + 'styleswitcher',
    {
        style: postStyle
    });
};

CS.prototype.addToMessageBlock = function(user_name)
{
    var oThis = this;

    this.get('messageblock/flipmessageblock/' + user_name, function(data)
    {
        var splittedResponse = oThis.convertResponseToArray(data, ';');
        oThis.alert(splittedResponse[1]);

        document.getElementById('messageblock_button_url').innerHTML = splittedResponse[2];
    });
};

// Everything is under a 'webcam' Namespace
window.webcam = {
    version: '1.0.9',

    // globals
    ie: !!navigator.userAgent.match(/MSIE/),
    protocol: location.protocol.match(/https/i) ? 'https' : 'http',
    callback: null, // user callback for completed uploads
    swf_url: 'webcam.swf', // URI to webcam.swf movie (defaults to cwd)
    shutter_url: 'shutter.mp3', // URI to shutter.mp3 sound
    loaded: false, // true when webcam movie finishes loading
    quality: 100, // JPEG quality (1 - 100)
    get_html: function(width, height, server_width, server_height)
    {
        // Return HTML for embedding webcam capture movie
        // Specify pixel width and height (640x480, 320x240, etc.)
        // Server width and height are optional, and default to movie width/height
        if (!server_width)
        {
            server_width = width;
        }
        if (!server_height)
        {
            server_height = height;
        }

        var html = '';
        var flashvars = 'shutter_enabled=' + (this.shutter_sound ? 1 : 0) +
            '&shutter_url=' + escape(this.shutter_url) +
            '&width=' + width +
            '&height=' + height +
            '&server_width=' + server_width +
            '&server_height=' + server_height;

        if (this.ie)
        {
            html += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="' + this.protocol + '://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="' + width + '" height="' + height + '" id="webcam_movie" align="middle"><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="false" /><param name="movie" value="' + this.swf_url + '" /><param name="loop" value="false" /><param name="menu" value="false" /><param name="quality" value="best" /><param name="bgcolor" value="#ffffff" /><param name="flashvars" value="' + flashvars + '"/></object>';
        }
        else
        {
            html += '<embed id="webcam_movie" src="' + this.swf_url + '" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="' + width + '" height="' + height + '" name="webcam_movie" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="' + flashvars + '" />';
        }

        this.loaded = false;

        return html;
    },
    flash_notify: function(type)
    {
        switch(type)
        {
            case 'flashLoadComplete':
                this.loaded = true;
                $('#webcamContainer input').show();
                break;
            case 'noCam':
                this.loaded = false;
                $('#webcamContainer').hide();
                break;
            case 'noImage':
                CS.alert("Er is iets fout gegaan, ververs de pagina en probeer het opnieuw");
                break;
        }
    },

    get_movie: function()
    {
        // get reference to movie object/embed in DOM
        if (!this.loaded)
        {
            CS.alert("De webcam is nog niet geladen, een ogenblik geduld a.u.b.");
            return false;
        }

        var movie = document.getElementById('webcam_movie');
        if (!movie)
        {
            CS.alert("Er is iets fout gegaan, ververs de pagina en probeer het opnieuw");
            return false;
        }

        return movie;
    },

    snap: function()
    {
        var movie = this.get_movie();
        if (movie)
        {
            movie._snap(this.api_url, this.quality, this.shutter_sound ? 1 : 0, this.stealth ? 1 : 0 );
        }
    },

    reset: function()
    {
        var movie = this.get_movie();
        if (movie)
        {
            movie._reset();
        }
    },

    flash_image_catcher: function(data)
    {
        var boundary = "";
        var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        for (var i = 0; i < 20; i++)
        {
            boundary += possible.charAt(Math.floor(Math.random() * possible.length));
        }

        var body = "--" + boundary + "\r\n";
        body += "Content-Disposition: form-data; name='image'; filename='webcam.jpg'\r\n";
        body += "Content-Type: image/jpeg\r\n\r\n";
        body += data + "\r\n";
        body += "--" + boundary + "\r\n";
        body += "Content-Disposition: form-data; name='base64'\r\n";
        body += "Content-Type: application/octet-stream\r\n\r\n";
        body += "1\r\n";
        body += "--" + boundary + "--";
        CS.preAlert();
        $.ajax(
        {
            cache: false,
            contentType: "multipart/form-data, boundary=" + boundary,
            data: body,
            url: CS.rootUrl + "account/photo",
            type: "POST",
            success: function(d)
            {
                document.location = CS.rootUrl + "account/photo";
            }
        });
        this.reset();
    }
};
