function collapse(id) {
    $('#comic-' + id).hide('slow');
    $('#collapse-button-' + id).text('[+]');
}

function expand(id) {
    $('#comic-' + id).show('slow');
    $('#collapse-button-' + id).text('[-]');
}

function toggle(id) {
    if ($('#comic-' + id).is(':hidden')) {
        expand(id);
    } else {
        collapse(id);
    }
    return false;
}


function saveOrder() {
    var followed = [];
    $("#comic-order li").each(function(i, elm) {
        followed[i] = elm.id;
    });
    $.ajax({
        type: "POST",
        url: "/actions/updateconfig",
        data: {comics: followed},
        dataType: 'json'
    });
    $("#configuration-changed").hide();
}

function changePassword() {
    if($('#newpassword').val() != $('#newpassword2').val()) {
        alert("Passwords don't match!");
        return false;
    }
    $.ajax({
        type: "POST",
        url: "/actions/changepassword",
        data: { oldpassword: $('#oldpassword').val(), newpassword : $('#newpassword').val() },
        dataType: 'json',
        success: function(data) {
            $('#submit').css('color', 'black');
            if(data.error == 'true') {
                $('#submit').css('background', 'red');
                $('#submit').attr('value', 'Error, Resubmit');
                $('#submit').CreateBubblePopup({
                            alwaysVisible: true,
                            innerHtml: data.message,
                            themeName: 	'all-black',
                            themePath: 	'/gfx/jquerybubblepopup-theme'
                        });
                $('#submit').ShowBubblePopup();
            } else {
                $('#submit').css('background', 'green');
                $('#submit').attr('value', 'OK');
                $('#submit').disable();
            }
            $('#submit').mouseout(function(){
                $('#submit').RemoveBubblePopup();
            });
        }
    });
    return false;
}

function comicToggle(comicId, name, img) {
    var comic = $('#item_' + comicId);
    // przelaczamy klase
    comic.toggleClass("comic-selected");
    // jesli dodalismy to wstawiamy go na liste orderu (na koncu)
    if(comic.hasClass("comic-selected")){
        $('#comic-order').append("<li id='id_" + comicId +
                "'><img src='/gfx/comicthumbs/"+ img +"' title='" + name + "'/></li>")
    } else {
        $('#id_' + comicId).remove();
    }
    // powiadamiamy o tym ze sa zmiany do zapisania
    $("#configuration-changed").show();
}

function propagationStop() {
    // http://www.quirksmode.org/js/events_order.html >_<
    if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}



//select all the a tag with name equal to modal
function preview(title, text) {
    propagationStop();
    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    //Set height and width to mask to fill up the whole screen
    $('#mask').css({'width':maskWidth,'height':maskHeight});

    //transition effect
    //$('#mask').fadeIn(200);
    $('#mask').fadeTo("slow",0.8);

    //Get the window height and width
    var winH = $(window).height();
    var winW = $(window).width();

    //Set the popup window to center
    var id = "#dialog";
    $(id).css('top',  winH/2-$(id).height()/2);
    $(id).css('left', winW/2-$(id).width()/2);


    $("#modal-title").empty();
    $("#modal-title").append(title);

    $("#modal-body").empty();
    $("#modal-body").append(text);
    //transition effect
    $(id).fadeIn(200);

}

function toggleLang(lang) {
    var isVisible = $('.comic-item.lang-' + lang).first().is(':visible');

    $('.comic-item.lang-' + lang).each(function(idx) {
        $(this).toggle();
    });
    $('#lang-toggle-' + lang).fadeTo('fast', isVisible ? 0.3 : 1);
}


