function openChat()
{
    window.open("http://chat.dutchvoyeur.nl/chatapp/chat","chat","width=962,height=700");
}

$.fn.extend({
    insertAtCaret: function(myValue){
            if (document.selection) {
        this.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
        this.focus();
            }
      else if (this.selectionStart || this.selectionStart == '0') {
          alert('oi');
        var startPos = this.selectionStart;
        var endPos = this.selectionEnd;
        var scrollTop = this.scrollTop;
        this.value = this.value.substring(0, startPos)+myValue+this.value.substring(endPos,this.value.length);
        this.focus();
        this.selectionStart = startPos + myValue.length;
        this.selectionEnd = startPos + myValue.length;
        this.scrollTop = scrollTop;
      } else {
        this.val(this.val() + myValue);
        this.focus();
      }
    }
});

$(document).ready(function()
    {
        $("#dialog").dialog({
            bgiframe: true,
            height: 200,
            modal: true,
            autoOpen: false
        });

        $("#feedbackdialog").dialog({
            bgiframe: true,
            width: 420,
            height: 340,
            modal: true,
            autoOpen: false
        });

        $('.smiley_image').click(function() {
            if (document.getElementById('message_text'))
            {
                $('#message_text').insertAtCaret($(this).attr("alt"));
            }
            else
            {
                $('#contentCommentForm_comment_form_text').insertAtCaret($(this).attr("alt"));
            }
            });
        $('.channel_item').corner().parent().css('padding', '1px').corner("round 8px");
        $('#filterbar').corner("round 9px").parent().css('padding', '1px').corner("round 8px");
        $('.breadcrumb').corner("round bottom");
        $('.boxheader').corner("round top");
        $('.boxcontent').corner("round bottom 9px").parent().css('padding', '1px').corner("round bottom 8px");
        $('.pagercontent').corner("round top 9px").parent().css('padding', '1px').corner("round top 8px");
        $('.closechannel').click(function() {
                var id = $(this).parent().parent().parent().parent().attr("id");
                var splittedId = id.split('_');
                var c = splittedId[2];
                $(this).parent().parent().parent().parent().slideUp("fast");
                $(this).parent().parent().parent().parent().parent().find('.channel_header_' + c).slideDown("fast");
                $.post(rootURL + 'accountpreference', { id: c, display: '0'});
            });
        $('#loginbuttonspace_button').hover(function() {
                    $(this).attr("src", rootURL + "images/" + language + "/member_hover.jpg");
                }, function() {
                    $(this).attr("src", rootURL + "images/" + language + "/member.jpg");
                });
        $('#channelselect').change(
                function() {
                    if (($(this).val()) != 0)
                    {
                        window.location = ($(this).val());
                    }
                });
    });

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

function openChannel(id)
{
    $('#' + id).slideDown("fast");
}

// ADD TO FAVOURITES
var favoriteToChange;

function addToFavorites(user_name, img_object, imageSize, showText)
{
    sendString = 'favorite/flipfavorite/' + user_name;

    favoriteToChange = img_object;
    favoriteImageSize = imageSize;
    favoriteShowText = showText;

    $.get(rootURL + sendString, function(data) {
        var splittedResponse = convertResponseToArray(data, ';');
        alert(splittedResponse[1]);

        var thumbSize = '_big';
        if (favoriteImageSize == 0)
        {
            thumbSize = '';
        }

        if (favoriteShowText == 1)
        {
            document.getElementById('favorite_button_url').innerHTML = splittedResponse[2];
        }

        if (splittedResponse[0] == 1)
        {
            favoriteToChange.src = rootURL + 'images/favoriet' + thumbSize + '.gif';
        }
        else
        {
            favoriteToChange.src = rootURL + 'images/favorietminus' + thumbSize + '.gif';
        }
    });
}

$(function() {
    $('#superhotimages').cycle({
        fx:      'curtainX',
        speed:    600,
        timeout:  5000
    });
    $('#superhottext').cycle({
        fx:      'fade',
        speed:    600,
        fit:      1,
        timeout:  5000
    });
});

function sendPoke(username)
{
    $.get(rootURL + 'poke/' + username, function(data) {
        alert(data);
    });
}

function changePhotoTitleChannel(image_id)
{
    var titleName = document.getElementById('image_title_' + image_id).value;

    $.post(rootURL + 'account/photo/photochangetitle/' + image_id, { title: titleName }, processChangePhotoTitleChannel);
}

function processChangePhotoTitleChannel(data)
{
    alert(data);
}

function changeVideoTitleChannel(video_id)
{
    var titleName = document.getElementById('video_title_' + video_id).value;

    $.post(rootURL + 'account/video/videochangetitle/' + video_id, { title: titleName }, processChangeVideoTitleChannel);
}

function processChangeVideoTitleChannel(data)
{
    alert(data);
}

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

    return outputStr;
}