jQuery.noConflict();
(function ($) {
$.fn.channelFeedItem = function() {
    return this.each(function() {
        var self = $(this);
        var commentDefaultText = $(this).find('textarea').attr('defaultText');

        if ($(this).attr('data').length>0) {
            $(this).data(eval('('+$(this).attr('data')+')'));
            $(this).removeAttr('data');
        }

        // hover item
        $(this).hover(function(){
            $(this).css('background-color', '#ffffe8');//ffffe8
            $(this).find('.right .original:first').css('background-color', '#ffffe8');//ffffe8
            $(this).find('a.btn-right').css('color', '#206fb2');
            $(this).find('a.btn-close').css('background-position', '-30px -123px');
            $(this).find('a.btn-right span').width(16).height(16);
        }, function(){
            $(this).css('background-color', '#ffffff');
            $(this).find('.right .original:first').css('background-color', '#f2f2f2');//ffffe8
            $(this).find('a.btn-right').css('color', '#ffffff');
            $(this).find('a.btn-close').css('background-position','-40px -140px');
            $(this).find('a.btn-right span').width(0).height(0);
        });

        // toggle comment box
        $(this).find('.actions .action-comment:first').toggle(function(){
            self.find('.comment-box:first').show();
        },function(){
            self.find('.comment-box:first').hide();
        });

        // show filter box
        $(this).find('.actions .action-filter:first').bind('click', function(){
            $('.popup-box').remove();
            
            $.ajax({
                url: PUBLIC_URL+'ajax-feeds/filter-users',
                type: 'POST',
                dataType: 'json',
                success: function(jSon) {
                    if(jSon.result == 'success') {
                        $('#'+jSon.popupId).remove();
                        $("html body").append(jSon.html);
                        var xCenter = Math.round($(window).width() / 2);
                        var boxW = Math.round($('#'+jSon.popupId).width() / 2);
                        var yCenter = Math.round($(window).height() / 2) + $(window).scrollTop();
                        var boxH = Math.round($('#'+jSon.popupId).height() / 2);

                        $('#'+jSon.popupId).css({top: (yCenter-boxH), left:(xCenter-boxW)}).show();
                    }
                    else {

                    }
                },
                error: function() {

                },
                complete: function() {

                }
            });
        });

        // gift popup
        $(this).find('.wallet-gift-popup:first').bind('click', function(){
            $('.popup-box').remove();
            fileName = $(this).children('img:first').attr('data');
            $.ajax({
                url: PUBLIC_URL+'ajax-feeds/popup-gift',
                type: 'POST',
                dataType: 'json',
                data: {fileName: fileName},
                success: function(jSon) {
                    if(jSon.result == 'success') {
                        $('#'+jSon.popupId).remove();
                        $("html body").append(jSon.html);
                        var xCenter = Math.round($(window).width() / 2);
                        var boxW = Math.round($('#'+jSon.popupId).width() / 2);
                        var yCenter = Math.round($(window).height() / 2) + $(window).scrollTop();
                        var boxH = Math.round($('#'+jSon.popupId).height() / 2);

                        $('#'+jSon.popupId).css({top: (yCenter-boxH), left:(xCenter-boxW)}).show();
                    }
                    else {

                    }
                },
                error: function() {

                },
                complete: function() {

                }
            });

        });


        // textarea
        function composerFocus(o) {
            if (o.val() == commentDefaultText) o.val('');
        }
        $(this).find('textarea').bind('focus', function(){
            composerFocus($(this));
            //$(this).parent().next().show();
        }).bind('blur', function(){
            if ($(this).val() == '') {
                $(this).val(commentDefaultText);
            }
        }).autoResize({extraSpace:10});
        // close
        function btnCloseClick(o) {
            $('.popup-box').remove();
            $.ajax({
                url: PUBLIC_URL+'ajax-feeds/close-item',
                type: 'POST',
                dataType: 'json',
                data: { id: self.attr('id') },
                onBefore: function(){o.unbind('click');},
                success: function(jSon) {
                    if(jSon.result == 'success') {
                        $('#'+jSon.popupId).remove();
                        $("html body").append(jSon.html);
                        var xCenter = Math.round($(window).width() / 2);
                        var boxW = Math.round($('#'+jSon.popupId).width() / 2);
                        var yCenter = Math.round($(window).height() / 2) + $(window).scrollTop();
                        var boxH = Math.round($('#'+jSon.popupId).height() / 2);

                        $('#'+jSon.popupId).css({top: (yCenter-boxH), left:(xCenter-boxW)}).show();
                    }
                    else {

                    }
                },
                error: function() {

                },
                complete: function() {
                }
            });
        }
        $(this).find('a.btn-close').bind('click', function(){
            btnCloseClick($(this));
        });
        // actions
        $(this).find('a.btn-right').bind('click', function(){
            self.append('<div class="disable"></div>');
            $.ajax({
                url: PUBLIC_URL+'ajax-feeds/re-post',
                type: 'POST',
                dataType: 'json',
                data: { id: self.attr('id') },
                success: function(jSon) {
                    if(jSon.result == 'success') {
                        self.fadeOut(500, function() {
                            self.parent().prepend(jSon.html);
                            $("html, body").animate({scrollTop: 0}, 500, function() {
                                $('#'+jSon.eventId).fadeIn();
                            });
                            $('#'+jSon.eventId).channelFeedItem();
                            self.remove();
                        })
                    }
                    else {

                    }
                },
                error: function() {

                },
                complete: function() {
                    self.children('.disable').remove();
                }
            });

        });
        // comment
        $('#'+self.attr('id')+'_btnSend').click(function(){
            self.find('.comment-box:first').append('<div class="disable"></div>');
            
            $.ajax({
                url: PUBLIC_URL+'ajax-feeds/comment',
                type: 'POST',
                dataType: 'json',
                data: { id: self.attr('id'), body: $('#'+self.attr('id')+'_composer').val() },
                success: function(jSon) {
                    if(jSon.result == 'success') {
                        self.fadeOut(500, function() {
                        	var parent = self.parent();
                        	self.remove();
                        	parent.prepend(jSon.html);
                            $("html, body").animate({scrollTop: 0}, 500, function() {
                                $('#'+jSon.eventId).fadeIn();
                            });
                            $('#'+jSon.eventId).channelFeedItem();
                        });
                    }
                    else {

                    }
                },
                error: function() {

                },
                complete: function() {
                    self.find('.comment-box:first').children('.disable').remove();
                }
            });
        });

        //emoticon
        function emoticonClick(who) {
            where = $('#'+self.attr('id')+'_composer');
            composerFocus(where);
            where.val(where.val() + ' ' + who.attr('title') + ' ');
            $('#more-emoticons').hide();
        }
        self.find('.emoticons a:not(.more)').bind('click', function(){
            emoticonClick($(this));
        });
        self.find('.emoticons a.more:first').bind('click', function() {
            p = self.find('.emoticons a.more:first').offset();
            if ($('#more-emoticons').length==0) {
                $('html body').append('<div id="more-emoticons"><a class="btn-close"></a><div class="body"></div></div>');
                $('#more-emoticons div.body').load(PUBLIC_URL+'ajax-feeds/more-emoticons', function() {
                    var xCenter = Math.round($(window).width() / 2);
                    var boxW = Math.round($('#more-emoticons').width() / 2);
                    $('#more-emoticons a').bind('click', function() {
                        emoticonClick($(this));
                    });
                    $('#more-emoticons').css({top: p.top+20, left:xCenter-boxW}).show();
                });
            }
            else {
                $('#more-emoticons a:not(.btn-close)').unbind('click').bind('click', function() {
                    emoticonClick($(this));
                });
                $('#more-emoticons a.btn-close').unbind('click').bind('click', function(){
                    $('#more-emoticons').hide();
                });
                var xCenter = Math.round($(window).width() / 2);
                var boxW = Math.round($('#more-emoticons').width() / 2);
                $('#more-emoticons').css({top: p.top+20, left:xCenter-boxW}).show();
            }
        });

        function roundedIcons() {
            $(self).find('.left a .th32').parent().append('<img class="thumb-mask-32" src="'+staticContentImages+'channel-feeds/profile-thumb-mask-32.png" />');
            $(self).find('.left a .th45').parent().append('<img class="thumb-mask-45" src="'+staticContentImages+'channel-feeds/profile-thumb-mask-45.png" />');
            $(self).find('.left a .th64').parent().append('<img class="thumb-mask-64" src="'+staticContentImages+'channel-feeds/profile-thumb-mask-64.png" />');
            $(self).find('.thumb-mask-64').parent().pngFix();
            $(self).find('.thumb-mask-32').parent().pngFix();
            $(self).find('.thumb-mask-45').parent().pngFix();
        }
        roundedIcons();
    });
}})(jQuery);

(function ($) {
$.fn.moreFeeds = function() {
    return this.each(function() {
        var self = $(this);
        $(this).bind('click', click);
        function click() {
            lastItem = $('.feeds .feed-item').last();

            $.ajax({
                url: PUBLIC_URL+'ajax-feeds/more-feeds',
                type: 'POST',
                dataType: 'json',
                data: { id: lastItem.attr('id'), u: $('#u').val() },
                beforeSend: function() {
                  jQuery('#action-loading').show();
                  self.unbind('click');
                },
                success: function(jSon) {
                    if(jSon.result == 'success') {
                        jQuery('.feeds').append(jSon.html);
                        $('.feeds .feed-item:gt('+(lastItem.index())+')').channelFeedItem();
                        newHiddenEvents = parseInt(jSon.hiddenEvents);
                        if (newHiddenEvents) {
                            hiddenEvents = parseInt(jQuery('#hidden-events').children('span').first().html());
                            hiddenEvents += newHiddenEvents;
                            jQuery('#hidden-events').children('span').first().html(hiddenEvents);
                        }
                    }
                    else {

                    }
                },
                error: function() {

                },
                complete: function() {
                  jQuery('#action-loading').hide();
                  self.bind('click', click);
                }
            });
        }
    });
}})(jQuery);


(function ($) {
$.fn.optionsFeeds = function() {
    return this.each(function() {
        var self = $(this);
        $(this).bind('click', click);

        function userClick() {
            userId = parseInt($(this).attr('data'));
            li = $(this).parents('li').first();
            $.ajax({
                url: PUBLIC_URL+'ajax-feeds/show-event',
                type: 'POST',
                dataType: 'json',
                data: {userId: userId},
                beforeSend: function() {
                    $(this).unbind('click');
                },
                success: function(jSon_) {
                    if (jSon_.result=='success') {
                        hiddenEvents = parseInt(jQuery('#hidden-events').children('span').first().html());
                        $('.feeds .feed-item:hidden').each(function(idx, item){
                            if (parseInt($(item).data().userId)==userId) {
                                $(item).show();
                                hiddenEvents--;
                            }
                        });
                        if (parseInt(hiddenEvents) > 0) {
                            jQuery('#hidden-events').removeClass('hidden');
                        }
                        else {
                            jQuery('#hidden-events').addClass('hidden');
                        }
                        jQuery('#hidden-events span').html(hiddenEvents);

                        li.fadeIn(500, function(){$(this).remove()});
                    }
                    else {

                    }
                },
                error: function() {

                },
                complete: function() {
                  
                }

            });
        }

        function click() {
            $('.popup-box').remove();
            $.ajax({
                url: PUBLIC_URL+'ajax-feeds/options-feeds',
                type: 'POST',
                dataType: 'json',
                beforeSend: function() {
                  self.unbind('click');
                },
                success: function(jSon) {
                    if(jSon.result == 'success') {
                        $('#'+jSon.popupId).remove();
                        $("html body").append(jSon.html);
                        var xCenter = Math.round($(window).width() / 2);
                        var boxW = Math.round($('#'+jSon.popupId).width() / 2);
                        var yCenter = Math.round($(window).height() / 2) + $(window).scrollTop();
                        var boxH = Math.round($('#'+jSon.popupId).height() / 2);

                        $('#'+jSon.popupId).css({top: (yCenter-boxH), left:(xCenter-boxW)}).show();
                        $('#'+jSon.popupId).find('input[type="button"]').bind('click', userClick);
                    }
                    else {

                    }
                },
                error: function() {

                },
                complete: function() {
                  jQuery('#action-loading').hide();
                  self.bind('click', click);
                }
            });
        }
    });
}})(jQuery);


function deleteEvent(id) {
    if (jQuery('#'+id).length>0) {
        jQuery.ajax({
            url: PUBLIC_URL+'ajax-feeds/delete-event',
            type: 'POST',
            dataType: 'json',
            data: { id: id },
            success: function(jSon) {
                if(jSon.result == 'success') {
                    jQuery('#'+id).fadeOut(500, function() {
                        jQuery('#'+id).remove();
                    });
                }
                else {

                }
            },
            error: function() {

            },
            complete: function() {
                jQuery('#popup-box-'+id).remove();
            }
        });
    }
}

function hideEvent(id) {
    o = jQuery('#'+id);
    if (o.length>0) {
        jQuery.ajax({
            url: PUBLIC_URL+'ajax-feeds/hide-event',
            type: 'POST',
            dataType: 'json',
            data: { id: id },
            success: function(jSon) {
                if(jSon.result == 'success') {
                    userId = parseInt(jSon.userId);
                    hiddenEvents = parseInt(jQuery('#hidden-events').children('span').first().html());
                    jQuery('.feeds .feed-item').each(function() {
                        u = parseInt(jQuery(this).data().userId);
                        if (u>0 && u==userId) {
                            jQuery(this).hide();
                            hiddenEvents++;
                        }
                    });
                    if (parseInt(hiddenEvents) > 0) {
                        jQuery('#hidden-events span').html(hiddenEvents);
                        jQuery('#hidden-events').removeClass('hidden');
                    }
                }
                else {

                }
            },
            error: function() {

            },
            complete: function() {
                jQuery('#popup-box-'+id).remove();
            }
        });
    }
}

function showEvents() {
    jQuery('.feeds .feed-item:hidden').show();
    jQuery('#hidden-events').hide();
}

function saveUsersFilter() {
    jQuery.ajax({
        url: PUBLIC_URL+'ajax-feeds/save-filter-users',
        type: 'POST',
        dataType: 'json',
        data: jQuery('#saveSettingsForm').serialize(),
        success: function(jSon) {
            if(jSon.result == 'success') {
                jQuery('#'+jSon.popupId).remove();
            }
            else {

            }
        }
    });
}

jQuery(document).ready(function() {
    jQuery('.feed-item:not(.feed-item-advertising)').channelFeedItem();
    jQuery('#more-feeds').moreFeeds();
    jQuery('#hidden-events').bind('click',showEvents);
    jQuery('#feed-options').optionsFeeds();
});

