(function () {
    jQuery.fn.cumulativeOffset = function (e) {
        var t = 0,
            l = 0;
        do {
            t += e.offsetTop || 0;
            l += e.offsetLeft || 0;
            e = e.offsetParent
        } while (e);
        return [l, t]
    };
    var feed_mouseover_tid = null;
    var feed_mouseout_tid = null;
    jQuery(document).ready(function () {
        jQuery('a[rel*="external"]').click(function () {
            window.open(this.href);
            return false
        });
        jQuery('a[href!="#"][href!="#ViewPollResults"][href^="#"]').click(function () {
            jQuery.scrollTo(this.href.replace(/^.*#/g, '#'), 400, {
                offset: 0
            });
            return false
        });
        jQuery('div.post').each(function () {
            var _post = this;
            var id = jQuery(_post).attr('id');
            if (/^post\-[0-9]+$/.test(id)) {
                if (/blank_content/i.test(jQuery(_post).find('.content').attr('class'))) {
                    var toggle = jQuery('<a href="javascript:void(0);" class="toggle collapse"></a>');
                    toggle.toggle(function () {
                        expand(id, _post)
                    }, function () {
                        collapse(_post)
                    })
                } else {
                    var toggle = jQuery('<a href="javascript:void(0);" class="toggle expand"></a>');
                    toggle.toggle(function () {
                        collapse(_post)
                    }, function () {
                        expand(id, _post)
                    })
                }
                toggle.prependTo(jQuery(_post))
            }
        });
        searchboxInit('searchtxt', 'searchbtn', 'Type text to search here...');
        jQuery('#searchbox').appendTo(jQuery('#header div.inner')).fadeIn(800);
        jQuery('#social').appendTo(jQuery('#navigation div.inner')).fadeIn(800);
        jQuery('#donate').appendTo(jQuery('#header div.inner')).fadeIn(800);
        jQuery('#subscribe').hover(function () {
            var _self = this;
            clearTimeout(feed_mouseout_tid);
            feed_mouseover_tid = setTimeout(function () {
                jQuery(_self).find('a#feedrss').addClass('current');
                jQuery(_self).find('ul').css({
                    left: jQuery('#feedrss').cumulativeOffset(_self)[0],
                    top: jQuery('#feedrss').cumulativeOffset(_self)[1] + 30
                }).slideDown()
            }, 400)
        }, function () {
            var _self = this;
            clearTimeout(feed_mouseover_tid);
            feed_mouseout_tid = setTimeout(function () {
                jQuery(_self).find('ul').slideUp(function () {
                    jQuery('#subscribe a#feedrss').removeClass('current')
                })
            }, 400)
        })
    });

    function expand(id, post) {
        if (jQuery(post).find('.content').text() == '') {
            loadPost(id)
        }
        jQuery(post).find('.content').slideDown();
        jQuery(post).find('a.toggle').removeClass('collapse').addClass('expand')
    }
    function collapse(post) {
        jQuery(post).find('.content').slideUp();
        jQuery(post).find('a.toggle').removeClass('expand').addClass('collapse')
    }
    function searchboxInit(textfileId, buttonId, tip) {
        var searchtxt = document.getElementById(textfileId);
        var searchbtn = document.getElementById(buttonId);
        if (searchtxt.value == '' || searchtxt.value == tip) {
            searchtxt.className += ' searchtip';
            searchtxt.value = tip
        }
        searchtxt.onfocus = function (e) {
            if (searchtxt.value == tip) {
                searchtxt.value = '';
                searchtxt.className = searchtxt.className.replace(' searchtip', '')
            }
        };
        searchtxt.onblur = function (e) {
            if (searchtxt.value == '') {
                searchtxt.className += ' searchtip';
                searchtxt.value = tip
            }
        };
        searchbtn.onclick = function (e) {
            if (searchtxt.value == '' || searchtxt.value == tip) {
                return false
            }
        }
    }
    function loadPost(id) {
        var postId = id.slice(5);
        jQuery.ajax({
            type: 'GET',
            url: '?action=load_post&id=' + postId,
            cache: false,
            dataType: 'html',
            contentType: 'application/json; charset=utf-8',
            beforeSend: function (data) {
                loadPostContent(id, '<p class="ajax-loader">Loading...</p>')
            },
            success: function (data) {
                loadPostContent(id, data)
            },
            error: function (data) {
                loadPostContent(id, '<p>Oops, failed to load data. <small><a href="javascript:void(0);" onclick="POS.loadPost(\'' + id + '\');">[Reload]</a></small></p>')
            }
        })
    }
    function loadPostContent(id, data) {
        jQuery('#' + id + ' .content').html(data)
    }
})();