function changeActiveTab()
{
    if(typeof beforeChangeTab == 'function')
        if(!beforeChangeTab(this))
            return false;

    clickedObject = this;
    $(this).parent().children().each(
            function() {
                if(clickedObject == this) //user click here
                {
                    if($(this).attr('class') != 'selected')
                    {
                        $(this).attr('class','selected')
                        $('#_ajax_container').load($(this).attr('id'))
                    }//else do nothing , user click to already active tab
                }
                else
                {
                    if($(this).attr('class') == 'selected')
                    $(this).attr('class','')
                }
            }
        )

    if(typeof afterChangeTab == 'function')
        afterChangeTab(this)

    return false;
}


function swapSearchTab(hideEl,showEl, eventObj)
{
    $('#'+hideEl).hide();
    $('#'+showEl).show();

    eventObj.cancelBubble = true
}


function showAdvancedSearch()
{
    $('#searchButton').hide(500);
    $('#searchInput_def').hide(500);
    $('#searchInput_by').show(500);

    $('#advanced_search_1').show(500)
}

function hideAdvancedSearch()
{
    $('#photoTab').hide(500);
    $('#advanced_search_1').hide(500);
    $('#videoTab').hide(500);

    $('#searchButton').show(500);
    $('#searchInput_def').show(500)
    $('#searchInput_by').hide(500)

}

function hideBottomPanel()
{

 if ($('#panel_dashboard').length >0 && document.getElementById('panel_dashboard').style.display == "block")
        $('#panel_dashboard').hide();

    if ($('#panel_status').length >0 && document.getElementById('panel_status').style.display == "block")
        $('#panel_status').hide();

    if ($('#panel_notification').length >0 && document.getElementById('panel_notification').style.display == "block")
        $('#panel_notification').hide();

}


function hideForumPopups()
{


    if ($('#bookmarks_popup').length >0)
    {
        $('#bookmarks_popup').hide();
    }

    if ($('#settings_popup').length >0)
    {
        $('#settings_popup').hide();
    }

}

function selectboxes(tododo)
{
    if($.browser.msie && $.browser.version.substr(0,1)<7)
    {
        if (tododo == 'hide')
            $('select').hide();
        else
            $('select').show();
    }
}

function hidePoppedUpDivs()
{
    hideAdvancedSearch();

    hideBottomPanel();

    hideForumPopups();
    $('#bookmarks').hide();
    $('#search_calendar').hide();
    selectboxes('show');
}


function refreshUsersOnline(intoEl)
{
    $('#'+intoEl).html('<div style="padding-top: 80px;text-align:center;"><img src="http://mvpspot.com/public/images/images_3.5/19-1.gif" ></div>');
    $('#'+intoEl).load('/service/helper/getLastActiveUsers')
}

var userRefreshTimeout;
function setTimeoutRefreshUsers(intoEl,sec)
{
    if(parseInt(sec) <= 0)
        return false;

    clearInterval(userRefreshTimeout);
    userRefreshTimeout = setInterval("refreshUsersOnline('"+intoEl+"')",sec*1000)

    $.get('/service/helper/setUserRefreshTimeout/seconds/' + sec)
}


function collapse(id,todo)
{

    if(todo == 'show')
    {
        $('#'+id+'_div').show();
        $('#'+id+'_arrowUp').show();
        $('#'+id+'_arrowDown').hide();
        updateAdvacedUserParams(id,"Y");
    }
    else
    {
        $('#'+id+'_div').hide();
        $('#'+id+'_arrowUp').hide();
        $('#'+id+'_arrowDown').show();
        updateAdvacedUserParams(id,"N");
    }
}


function getRecentActivity(activityType,userID, pageType)
{

    $('#'+activityType+'Loader').html('<img src="http://mvpspot.com/public/images/images_3.5/28-0.gif" >');

    if(activityType == 'user')
    {
        $.getJSON('http://mvpspot.com/service/activity/getRecentUserActivity/',
        {
            userID : userID,
            limit : 5,
            pageType: pageType

        },
        function(data)
        {
            outputRecentActivity(data,activityType);
        });
    }
    else
    {
        $.getJSON('http://mvpspot.com/service/activity/getrecentactivity/',
        {
            activityType: activityType,
            userID : userID,
            limit : 5,
            pageType: pageType

        },
        function(data)
        {
            outputRecentActivity(data,activityType);
        });
    }
    setTimeout("getRecentActivity('"+activityType+"','"+userID+"','"+pageType+"');",30000);

}

function outputRecentActivity(objResp,activityType)
{

    if (objResp.redirect)
    {
        window.location.href = objResp.redirect;
        return false;
    }

    var output = '';

    if (activityType == 'friends')
        obj = objResp.activity.friends;

    if (activityType == 'other')
        obj = objResp.activity.other;

    if (activityType == 'user')
        obj = objResp.activity.user;


    var arrlenght = obj.length;

    for(i=0;i<arrlenght;i++)
    {
        //alert(obj[i].html);return false;
        output = output + obj[i].html;
    }
    //alert(output);
    //alert('#'+activityType+'Activity');
    if(output == '')
    {
        output = 'No activity yet.';
    }

    if(arrlenght>1)
    {
	output = output + '<div class="rec_activity_older_container"><a class="small_tahoma_link older_arrowed_link" href="/user/index/recentactivity">more events</a></div>';
    }


    $('#'+activityType+'Activity').html(output);
    $('#'+activityType+'Loader').html('');

    //alert('-'+$('#userActivity').html()+$('#friendsActivity').html()+'-');
    /*if ($('#recent_activity_div').attr('display') == 'none')
        $('#recent_activity_cont').hide();
    else
        $('#recent_activity_cont').show();*/
}
function chan(id)
    {
        $('#'+id).toggle();
    }

function addFriend(id)
{
     $('#add_friend_link_'+id).html('request sent');
     $('#add_friend_link_'+id).hide(3000);
     //$('#add_friend_link_'+id).attr('onclick','');
     $.getJSON('/user/profile/index/UserID/'+id+'/addTeammate/1/',
        {
        },
        function(data)
        {

        });

}


function bookmarkItem(itemID,itemType,itemText)
{

    $.getJSON('/service/helper/bookmarkitem/',
        {

            itemID : itemID,
            itemType : itemType,
            text : itemText
        },
        function(data,res)
        {
           $('#bookmark_result').val(data.ok);

        });

}

function unbookmarkItem(itemID,itemType)
{
    $.getJSON('/service/helper/unbookmarkitem/',
        {
            itemID : itemID,
            itemType : itemType
        },
        function(data)
        {
            $('#bookmark_result').val(data.ok);

        });

}

 function bookmark(ID,itemType,itemText,todo)
    {
    var res;
        if(todo == 'add')
        {
            bookmarkItem(ID,itemType,itemText);

            $('#bookmarked_'+ ID).show();
            $('#notbookmarked_'+ ID).hide();
            $('#bookmarked_popup').show();
            setTimeout("$('#bookmarked_popup').hide('slow')",5000);


        }
        else
        {
            unbookmarkItem(ID,itemType);

            $('#bookmarked_'+ ID).hide();
            $('#notbookmarked_'+ ID).show();
        }
    }


function trim(string)
{
    return string.replace(/(^\s+)|(\s+$)/g, "");
}


function getComments(id,itemType,container)
{
    $.getJSON('/service/comment/getitemcomments/',
    {
        itemId:id,
        itemType:itemType
    },
    function(objResp)
        {
            $('#'+container).html(objResp.content);
        });
}


function makePhotoDefault(ID)
{

    $.getJSON('/user/image/default/',
    {
        imageID: ID
    })
    $('#madedefault').show();
    setTimeout(function(){$('#madedefault').hide();}, 5000);
}

function voteItem(id,vote,itemType)
    {
        $('#voting').html('');
        $.getJSON('/service/helper/voteitem/',
        {
            itemID:id,
            vote:vote,
            itemType:itemType
        },
        function(objResp)
            {

                if( objResp.ok == 'ok')
                {
                    $('#voting').hide();
                    $('#voting_results').html(objResp.content);
                    $('#voting_results').show();
                }
            });
    }

function viewAlbum(albumID, page)
{
    $.post('/user/image/viewalbum/',
    {
        'albumID': albumID,
        'title': $('#title_' + albumID).val(),
        'page': page
    },
    outputResult,
        'json'
    );
}

function viewVideos(page, total)
{
    $.post('/user/video/index',
    {
        'totalVideos': total,
        'page': page
    },
    outputResult,
        'json'
    );
}

function outputResult(objResp)
{
    $('#other_container').empty();
    $('#other_container').html(objResp.content).show();
}

function updateAdvacedUserParams(param, param_val)
{
    $.post(
    '/service/helper/updateaup/',
    {

        'param': param,
        'param_val': param_val
    },
    function() {});
}

function deleteFriend2(userID,divtohide)
    {
        $('#'+divtohide).hide();
        $.get("/user/friends/delete/userID/" + userID);

    }

function checkEnter(id,execute)
{

    $('#'+id).bind('keypress', function(e) {
        if(e.keyCode==13){
                eval(execute);
        }
    });


}

function showReportPopup(id,rtype)
{
    $('#report_id').val(id);
    $('#report_type').val(rtype);
    $('.report_type_title').html(rtype);
    tb_init('a.thickbox, area.thickbox, input.thickbox');
    imgLoader = new Image();
    imgLoader.src = tb_pathToImage;
    tb_show('', '#TB_inline?height=367&width=624&inlineId=report_popup&modal=true', null);
}


function reportItem()
{

    var reason = $('#report_reason').val();
    var message = $('#report_message').val();
    var id = $('#report_id').val();
    var rtype = $('#report_type').val();

    if(reason.length==0 || message.length == 0)
    {
        alert('You should fill Reason and Message fields.');
        //$('#report_error').show();
        return false;
    }
    else
        $('#report_error').hide();

    $.post('/service/helper/reportitem/',
        {
            reportedID: id,
            reason: reason,
            message: message,
            rtype: rtype
        },
        function(objResp)
            {
                tb_remove();
            });

}

function showSendmessagePopup(id)
{
    $('#toUserID_').val(id);
    tb_init('a.thickbox, area.thickbox, input.thickbox');
    imgLoader = new Image();
    imgLoader.src = tb_pathToImage;
    tb_show('', '#TB_inline?height=367&width=624&inlineId=sendmessage_popup&modal=true', null);
}



function getNewMessagesForMenu()
{
    $('#voting').html('');
        $.getJSON('/user/message/getnumnewmess/',
        {
        },
        function(objResp)
            {

                if( objResp.num > 0)
                {
                    $('#nm_menu_counter').html('('+objResp.num+')');
                }
                else
                {
                    $('#nm_menu_counter').html('');
                }
            });
   setTimeout('getNewMessagesForMenu()',30000)
}

function getBookmarks()
{
    $('#bookmarks_header').html('<img src="http://mvpspot.com/public/images/images_3.5/28-1.gif" > loading...');
    $.getJSON('/service/helper/getbookmarks/',
    {
    },
    function(objResp)
        {

            $('#bookmarks_header').html(objResp.html);

        });
}

function checkSpecificBox(elementID)
{
    var checkedInput = $('#' + elementID + ' input:only-child').attr('checked');
    if (checkedInput == false)
        $('#remote_' + elementID).addClass('albums_photo_item_selected');
    else
        $('#remote_' + elementID).removeClass('albums_photo_item_selected');

    $('#' + elementID + ' input:only-child').attr('checked', !checkedInput);
}

function gotopage(url,page)
{
    url = url.replace(/%page%/,page);
    document.location.href=url;
}

function selectSelect(id)
{
    //alert ('lalal');return false;
    var val = $('#'+id).val();
    //alert(val);
    if(val == 0 || val == 1)
    {
        processAll(val);
    }
    else
    {
        if(val == 'read' || val == 'unread')
        {
            processMarked(val);
        }
    }
}

function deleteWallComment(commentID, itemType)
{
    $.ajax({
    url: '/user/comment/deletewallcomment/commentID/' + commentID + '/itemType/' + itemType,
    type: 'post',
    success: function(){$('#comment_' + commentID).hide()}
    });
}


function checkVideoStatus(commentID)
{
    $.post('/user/comment/checkstatus/',{
    'commentID' : commentID
    },
    function (data)
    {
        if (data == 'Y')
        {
            $('#ajaxLoader_' + commentID).hide();
            $('#MVPPlayer_' + commentID).show();
        }
        else
            setTimeout("checkVideoStatus(commentID);", 1000);
    });
}

function OpenReplyForm(replyID, formType)
{
    if (formType == 'reply')
    {
        mess1 = $('#wall_message_'+replyID).text();
        mess2 = mess1.replace(/^\s+|\s+$/, '');

        if(mess2.length >100)
        {
            pos = mess2.substring(0,100).lastIndexOf(" ");
            mess3 = mess2.substring(0,pos);
            mess = mess3+'...';

        }
        else
            mess = mess2;

        $('#comment_quote').html('"'+mess+'"');
        $('#comment_author').html($('#wall_author_'+replyID).html());

        $('#quotingSmb').css('display', 'block');
        $('strong[id*=titleComm]').text('Reply to Comment');
    }
    else
    {
        $('#quotingSmb').css('display', 'none');
        $('strong[id*=titleComm]').text('Post Comment');
    }

    tb_init('a.thickbox, area.thickbox, input.thickbox');
    tb_show('', '#TB_inline?height=614&width=624&inlineId=comments_reply_form&modal=true', null);
}
