﻿
var isLoaded = false;
var btnSubmitEntryID = '';

$(function() {

    $(".entry_image").lazyload({
        effect: "fadeIn"
    });

    $('.boxopener').bind('click', togglebox);

    $('.comment_textarea').keyup(function() {
        var len = this.value.length;

        if (len >= 140) {
            this.value = this.value.substring(0, 140);
            this.scrollTop = this.scrollHeight;
        }

        if (len > 140) { len = 140; }

        $($(this).attr('charrembox')).val(140 - len);
    });

    $('#anon_comment_submit').bind('click', function() {
        submitComment({
            commentBox: $($('#anon_comment_box').val()),
            entry: $('#anon_comment_entry').val(),
            list_id: $('#anon_comment_list').val(),
            list_title: $('#list_title').text(),
            list_friendly: $('.hdnListUrlFriendly').val(),
            name: $('#anon_comment_name').val(),
            website: $('#anon_comment_website').val(),
            email: $('#anon_comment_email').val()
        });

        $.unblockUI();
        return false;
    });

    $('.unblocker').bind('click', function() {
        $.unblockUI();
        return false;
    });

    $('#addentrybutton').bind('click', function() {
        $.blockUI({
            message: $('#addentry'),
            css: {
                cursor: 'default',
                width: 290,
                height: 250,
                padding: 20,
                left: '40%',
                top: '20%'
            },
            overlayCSS: {
                cursor: 'default'
            },
            fadeOut: 0
        });
        return false;
    });

    $(btnSubmitEntryID).bind('click', submitEntry);

    if ($.cookie('openmetip') != 'false') {

        $('.expander:first').qtip({
            content: '<div class="open_me_tip"><a href="#" class="drop_tip" onclick="dropOpenMeTip(); return false;">X</a>Expand Me Here!</div>',
            position: {
                corner: {
                    target: 'topMiddle',
                    tooltip: 'bottomRight'
                }
            },
            style: {
                name: 'blue',
                tip: {
                    corner: 'bottomRight'
                },
                border: {
                    width: 1
                }
            },
            show: {
                when: false,
                ready: true
            },
            hide: false
        });

    }

});

function dropOpenMeTip() {
    $('.expander:first').qtip('hide');
    $.cookie('openmetip', 'false', new Date() + 1);
}

function showThanks() {
    $.blockUI({
        fadeIn: 0,
        message: $('#submissionthanks'),
        css: {
            padding: 20
        }
    });
    setTimeout(function() { $.unblockUI(); }, 3000);
}

function submitEntry(e) {
    if (Page_ClientValidate() == true) {
        e.preventDefault();
        $.unblockUI({
            onUnblock: function() { eval($(btnSubmitEntryID).attr('href')); }
        });
        $.blockUI({ message: $('#loading') });

        return false;
    }
}

function addview(entry_id) {
    $.ajax({
        type: "GET",
        url: "rate.aspx",
        data: 'action=addview&entry=' + entry_id
    });
}

function submitComment(options) {
    var comment = escape(options.commentBox.val());
    if (comment != '')
    $.ajax({
        type: "GET",
        url: "submitcomment.aspx",
        data: 'action=comment&entry=' + options.entry +
                    '&list_id=' + options.list_id +
                    '&list_title=' + options.list_title +
                    '&list_friendly=' + options.list_friendly +
                    '&comment=' + comment +
                    '&name=' + options.name +
                    '&email=' + options.email +
                    '&website=' + options.website,
        success: function(msg) {
            options.commentBox.val('');
            $('.discussion_middle').prepend(msg);
        }
    });
}

var openTips = '';

function togglebox() {

    var me = $(this);
    var divtoopen = '#' + me.attr('divtoopen');
    var time = 250;
    var extratime = time + 50;

    var expander = $(divtoopen + ' .expander');

    var hideBG = $(divtoopen + ' .listbox_closed_bg');
    var showBG = $(divtoopen + ' .listbox_open_bg');
    var boxToOpen = $(divtoopen + ' .listbox_closed');
    var boxTopToOpen = $(divtoopen + ' .listbox_closed_top');
    var h1ToAnimate = $(divtoopen + ' .listbox_closed_top h1');
    var imageBox = $(divtoopen + ' .imagebox_small');
    var imgBG = $(divtoopen + ' .bg_image');
    var img = $(divtoopen + ' .entry_image');
    var listDescription = $(divtoopen + ' .listdescription_small');
    var showme = $(divtoopen + ' .showme');
    var summary = $(divtoopen + ' .list_summary');
    var openedFlag = $(divtoopen + ' .openedFlag');

    if (expander.html() == '+') {

        if ($.support.opacity) { hideBG.animate({ opacity: 0 }, time * 2) } else { hideBG.css('display', 'none') };
        showme.css('display', 'block');
        showBG.animate({ height: '308px' }, time);
        boxToOpen.animate({ height: '308px' }, time);
        boxTopToOpen.animate({ height: '40px' }, time);
        h1ToAnimate.animate({ fontSize: '20pt', color: '#ffffff', paddingTop: '2px', height: '40px' }, time);
        imageBox.animate({ width: '123px', height: '124px', top: '50px' }, time);
        imgBG.animate({ width: '123px', height: '124px' }, time);
        img.animate({ height: '100px', width: '100px', marginTop: '11px', marginLeft: '11px' }, time);
        listDescription.animate({ left: '135px', width: '325px' }, time);
        summary.animate({ width: '325px', height: '110px' }, time);
        setTimeout(function() { summary.css("overflow", "auto") }, extratime);

        expander.html('-');
        addview(expander.attr('entryid'));

        if (openedFlag.val() == 'false') {

            isLoaded = false;

            var allLargeRatings = $(divtoopen + ' .rating_large input');
            allLargeRatings.rating({
                required: true,
                starClass: 'big-star-rating',
                starWidth: 25,
                callback: function(value, link) {

                    if (isLoaded) {
                        $.ajax({
                            type: "GET",
                            url: "rate.aspx",
                            data: 'action=rate&entry=' + expander.attr('entryid') + '&value=' + value,
                            success: function(msg) {
                                $('#' + $(link).parents('.listbox_wrapper').attr('id') + ' .rating_small').css('background-position', '0px -' + msg + 'px');
                            }
                        });

                    }

                }
            }).rating('select', $('#' + expander.attr('divtoopen') + ' .rating_large').attr('rating'));

            if ($.cookie('ratemetip') != 'false') {

                $(divtoopen + ' .rating_large').qtip({
                    content: '<div class="rate_me_tip"><a href="#" class="drop_tip" onclick="dropRateMeTip(); return false;">X</a>Rate Me Here!</div>',
                    position: {
                        corner: {
                            target: 'rightMiddle',
                            tooltip: 'leftBottom'
                        }
                    },
                    style: {
                        name: 'blue',
                        tip: {
                            corner: 'leftBottom'
                        },
                        border: {
                            width: 1
                        }
                    },
                    show: {
                        when: false,
                        ready: true
                    },
                    hide: false
                });

                openTips += divtoopen + ' .rating_large, ';

            }

            isLoaded = true;

            openedFlag.val('true');
        }

    } else {

        if ($.support.opacity) { hideBG.animate({ opacity: 100 }, time * 2) } else { hideBG.css('display', 'block') };
        showme.css('display', 'none');
        showBG.animate({ height: '119px' }, time);
        boxToOpen.animate({ height: '119px' }, time);
        boxTopToOpen.animate({ height: '30px' }, time);
        h1ToAnimate.animate({ fontSize: '16pt', color: '#000000', paddingTop: '0px', height: '30px' }, time);
        imageBox.animate({ width: '70px', height: '71px', top: '40px' }, time);
        imgBG.animate({ width: '70px', height: '71px' }, time);
        img.animate({ height: '55px', width: '55px', marginTop: '8px', marginLeft: '8px' }, time);
        listDescription.animate({ left: '85px', width: '375px' }, time);
        summary.animate({ width: '365px', height: '55px' }, time);
        setTimeout(function() { summary.css("overflow", "hidden") }, extratime);

        expander.html('+');
        try { $(divtoopen + ' .rating_large').qtip('hide'); } catch (err) { }

    };

    return false;
}

function dropRateMeTip() {
    $(openTips).qtip('hide');
    $.cookie('ratemetip', 'false', new Date() + 1);
}