var favorites = [];
var fav_id = '';
var fav_count = 0;

var c = $.cookie('favorites_id');
if (c) {
    fav_id = c;
} else {
    var i = 0;
    var charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    var charsetlen = charset.length, charIndex;

    for (i; 8 > i; i += 1) {
        charIndex = Math.random() * charsetlen;
        fav_id += charset.charAt(charIndex);
    }

    $.cookie('favorites_id', fav_id, { expires: 3650 });
}

$(document).ready(function() {
    $.ajax({
        type: "GET",
        url: "userfavs.php",
        data: "fav_id="+fav_id,
        success: function(data) {
            favorites = data;
            for (x = 0; x <= data.length-1; x++) {
                favorites[x] = data[x];
            }

            fav_count = favorites.length;
            $('#favcount span').text(fav_count);

            $('div.thumb_large,div.thumb').each(function(index) {
                var t = $(this).attr('rel');
                var l = $(this).find('a');
                var fav_url = $(l).attr('rel');
                var i = $(this).find('img');
                var d = $(i).attr('title');
                var c = 'fav_btn_off';
                var s = 'none';
                var k = false;

                if ($(this).hasClass('thumb_hide_on_unfav')) k = true;
                var cur_fav = findfav(fav_url);

                if (cur_fav) {
                    c += ' cur_fav';
                    s = 'block';
                }

                var h = $('<div class="fav_btn '+c+'" style="display:'+s+';" onclick="toggle_user_fav(this,\''+escape(fav_url)+'\',\''+escape(t)+'\',\''+escape(d)+'\','+k+')"></div>');

                $(this).append(h);

                $(this).mousemove(function() {
                    showfavicon(this);
                }).mouseout(function() {
                    hidefavicon(this);
                });
            });
        }
    });
});

function showfavicon(el) {
    $(el).find('.fav_btn').css('display','block');
}

function hidefavicon(el) {
    if ($(el).find('.fav_btn').hasClass('cur_fav')) return;
    $(el).find('.fav_btn').css('display','none');
}

function findfav(fav_url) {
    if (favorites && favorites.length > 0) {
        for (x in favorites) {
            if (fav_url == favorites[x].url) {
                return true;
            }
        }
    }
    return false;
}

function addfav(fav_url, fav_thumb, fav_title) {
    var new_fav = {url: unescape(fav_url), thumb: unescape(fav_thumb), title: unescape(fav_title)};
    favorites[favorites.length] = new_fav;
    $.ajax({
        type: "POST",
        url: "userfavs.php",
        data: "action=add&fav_id="+fav_id+"&url="+escape(fav_url)+"&thumb="+escape(fav_thumb)+"&title="+escape(fav_title)
    });
}

function removefav(fav_url, el) {
    if (favorites && favorites.length > 0) {
        for (x in favorites) {
            if (fav_url == favorites[x].url) {
                delete favorites[x];
            }
        }
    }
    $.ajax({
        type: "POST",
        url: "userfavs.php",
        data: "action=delete&fav_id="+fav_id+"&url="+escape(fav_url)
    });
}

function toggle_user_fav(el, fav_url, thumb, title, hide) {
    var action = 'add';
    var fav = findfav(unescape(fav_url));
    var count = Number($('#favcount span').text());

    if (fav) {
        removefav(fav_url);
        if (hide) {
            $(el).parent().fadeOut();
        } else {
            $(el).removeClass('cur_fav').attr('title', 'Add Favorite').hide();
        }
        $('#favcount span').text(count-1);
    } else {
        addfav(fav_url, thumb, title);
        $(el).removeClass('fav_btn_on').addClass('fav_btn_off').addClass('cur_fav').attr('title', 'Remove Favorite');
        $('#favcount span').text(count+1);
    }
}
