$(document).ready(function(){
    var rating_text     = $('#rating_text').html();
    var rating_current  = $("input[id='current_rating']").val();
    $("#share_fantasie a").click(function(event) {
        event.preventDefault();
        $("#share_fantasie_box").slideToggle(); 
    });
    $("#flag_fantasie a").click(function(event) {
        event.preventDefault();
        $("#flag_fantasie_box").slideToggle();
    });

    $("#embed_fantasie a").click(function(event) {
        event.preventDefault();
        $("#embed_fantasie_box").slideToggle();
    });
    
    $("#close_flag").click(function(event) {
        event.preventDefault();
        $("#flag_fantasie_box").hide();
    });
    
    $("#close_share").click(function(event) {
        event.preventDefault();
        $("#share_fantasie_box").hide();
    });
    
    $("#close_embed").click(function(event) {
        event.preventDefault();
        $("#embed_fantasie_box").hide();
    });
    
    $("#close_favorite").click(function(event) {
        event.preventDefault();
        $("#favorite_fantasie_box").hide();
    });
    
    $("textarea[id='fantasie_embed_code']").click(function() {
        $(this).select();
        $(this).focus();
    });
    
    $("a[id*='favorite_fantasie_']").click(function(event) {
        event.preventDefault();
        var fav_id      = $(this).attr('id');
        var id_split    = fav_id.split('_');
        var fantasie_id    = id_split[2];
        user_posting('#response_message', 'Favoriting...');
        $.post(base_url + '/ajax/favorite_fantasie', { fantasie_id: fantasie_id },
        function (response) {
            if ( response.status == 0 ) {
                user_posting('#response_message', response.msg);
            } else {
                user_response('#response_message', response.msg);
            }    
        }, 'json');                                                
    });
    
    $("[id*='star_']").click(function(event) {
        event.preventDefault();
        var star_id     = $(this).attr("id");
        var id_split    = star_id.split('_');
        var rating      = id_split[2];
        var fantasie_id = id_split[3];
        $("#rating_text").html('Thanks for rating!');
        $.post(base_url + '/ajax/rate_fantasie', { fantasie_id: fantasie_id, rating: rating },
        function (response) {
            $("#rating").html(response.rating_code);
            $("#rating_text").html(response.msg);
        }, "json");            
    });
    
    $("[id*='star_']").mouseover(function(){
        var star_id     = $(this).attr('id');
        var id_split    = star_id.split('_');
        var rating      = id_split[2];
        var fantasie_id    = id_split[3];
        for ( var i = 1; i<=5; i++ ) {
            var star_sel = $("a[id='star_fantasie_" + i + "_" + fantasie_id + "']");
            if ( i <= rating )
                $(star_sel).removeClass().addClass('full');
            else
                $(star_sel).removeClass();
        }
        if ( rating == 1 ) {
            $('#rating_text').html('Lame');
        } else if ( rating == 2 ) {
            $('#rating_text').html('Bleh');
        } else if ( rating == 3 ) {
            $('#rating_text').html('Alright');
        } else if ( rating == 4 ) {
            $('#rating_text').html('Good');
        } else if ( rating == 5 ) {
            $('#rating_text').html('Awesome');
        }
    });
    
    $("ul[id='rating_container_fantasie']").mouseout(function(){
        var star_id     = $("[id*='star_fantasie_1_']").attr('id');
        var id_split    = star_id.split('_');
        var fantasie_id = id_split[3];
        for ( var i = 0; i < 5; i++ ) {
            var star        = i+1;
            var star_sel    = $("a[id='star_fantasie_" + star + "_" + fantasie_id + "']");
            if ( rating_current >= i+1 ) {
                $(star_sel).removeClass().addClass('full');
            } else if ( rating_current >= i+0.5 ) {
                $(star_sel).removeClass().addClass('half');
            } else {
                $(star_sel).removeClass();
            }     
        }
        $('#rating_text').html(rating_text);
    });
    
    $("a#show_related_fantasies").click(function(event) {
        event.preventDefault();
        $("#fantasie_comments").hide();
        $("#related_fantasies").show();
    });

    $("a#show_comments").click(function(event) {
        event.preventDefault();
        $("#related_fantasies").hide();
        $("#fantasie_comments").show();
    });
    
    $("input[id*='post_fantasie_comment_']").click(function() {
        var fantasie_msg   = $("#post_message");
        var input_id    = $(this).attr('id');
        var id_split    = input_id.split('_');
        var fantasie_id    = id_split[3];                    
        var comment     = $("textarea[id='fantasie_comment']").val();
        if ( comment == '' ) {
            fantasie_msg.show();
            return false;
        }
                    
        fantasie_msg.hide();
        user_posting_load('#fantasie_response', 'Posting', 1);
        reset_chars_counter();
        $.post(base_url + '/ajax/fantasie_comment', { fantasie_id: fantasie_id, comment: comment },
        function(response) {
            if ( response.msg != '' ) {
                $("textarea[id='fantasie_comment']").val('');
                user_posting('#fantasie_response', response.msg);
            } else {
                $(".no_comments").hide();
                $("textarea[id='fantasie_comment']").val('');
                var bDIV = $("#comments_delimiter");
                var cDIV = document.createElement("div");
                $(cDIV).html(response.code);
                $(bDIV).after(cDIV);
                user_response('#fantasie_response', 'Comment successfully posted!');
            }
        }, "json");
    });
    
    $("a[id*='p_fantasie_comments_']").livequery('click', function(event) {
        event.preventDefault();
        var page_id     = $(this).attr('id');
        var id_split    = page_id.split('_');
        var fantasie_id    = id_split[3];
        var page        = id_split[4];
        $.post(base_url + '/ajax/fantasie_pagination', { fantasie_id: fantasie_id, page: page },
        function(response) {
            if ( response != '' ) {
                var comments_id = $('#fantasie_comments_' + fantasie_id);
                $(comments_id).hide();
                $(comments_id).html(response);
                $(comments_id).show();
            }
        });
    });
    
	$("#fantasie_text").jTruncate({
        length: 3750,
        minTrail: 0
    });
            
    $("a[id*='_related_fantasies_']").livequery('click', function(event) {
        event.preventDefault();
        var bar_id      = $(this).attr('id');
        var id_split    = bar_id.split('_');
        var move        = id_split[0];
        var fantasie_id    = id_split[3];
        var page        = $("input[id='current_page_related_fantasies']").val();
        var prev_bar    = $('#prev_related_fantasies_' + fantasie_id);
        var next_bar    = $('#next_related_fantasies_' + fantasie_id);
        $('.center_related').show();
        $.post(base_url + '/ajax/related_fantasies', { fantasie_id: fantasie_id, page: page, move: move },
        function(response) {
            if ( response.status == '1' ) {
                var related_div = $('#related_fantasies_container');
                $(related_div).hide();
                $(related_div).html(response.fantasies);
                $(related_div).show();
                $("input[id='current_page_related_fantasies']").val(response.page);
                if ( response.pages <= 1 ) {
                    $(prev_bar).hide();
                    $(next_bar).hide();
                }
            
                if ( response.page > 1 ) {
                    $(prev_bar).show();
                } else {
                    $(prev_bar).hide();
                }
            
                if ( response.page >= response.pages ) {
                    $(next_bar).hide();
                } else {
                    $(next_bar).show();
                }
            }
            $('.center_related').hide();
        }, 'json');
    });
});

