/*
 * Tweeple.in
 * By Karmadude (http://www.vunite.com)
 * Copyright (c) VUnite Media
*/

jQuery(document).ready(function(){    
    jQuery('.profileimg a.thickbox').lightBox();  
    jQuery('.userheader .thickbox').lightBox();
});

function showLoadingIcon(id, type)
{
    jQuery(id).html("<img src='http://tweeple.in/wp-content/themes/tweeple/images/loading" + type + ".gif' alt='' />");    
}

function loadTwipics()
{     
    jQuery('.status').each(function() {    
        var id = this.id;
        showLoadingIcon('#'+id, 3);
        jQuery.get(twitterurl, { status_id: this.id },
          function(data){
                jQuery('#'+id).html(data);
          });
        
        var name = id.replace('_tistatus', '');
        showLoadingIcon('#'+name+'_pics', 3);
        jQuery.get(twitterurl, { screen_name: name },
          function(data){
                jQuery('#'+name+'_pics').html(data);
                jQuery('#'+name+'_pics a.thickbox').lightBox();
          });
    });
}

function loadUserSidebar(name)
{
    showLoadingIcon('#recenttwitpicscontent', 4);
    jQuery.get(twitterurl, { type: 'profile_twitpics', screen_name: name},
      function(data){
            jQuery('#recenttwitpicscontent').html(data);
            jQuery('.sidebar .thickbox').lightBox();  
    });
      
    showLoadingIcon('#rfollowerscontent',4);
    jQuery.get(twitterurl, { type: 'profile_followers', screen_name: name},
      function(data){
            jQuery('#rfollowerscontent').html(data);  
    });
    
    showLoadingIcon('#rfollowingscontent',4);
    jQuery.get(twitterurl, { type: 'profile_following', screen_name: name},
      function(data){
            jQuery('#rfollowingscontent').html(data);  
    });
}

function loadReviewStats()
{
    jQuery.get(twitterurl, { type: 'getreviewstats'},
      function(data){
            jQuery('#reviewstats').html(data);  
            jQuery("#reviewstats")
                    .animate( { backgroundColor: '#fffbb6' }, 200)
                    .animate( { backgroundColor: '#F3F4E8' }, 200);
    });

}

function createShareLinks(id)
{
    jQuery.get(twitterurl, 
        {type: 'shorturl', url: location.href},
      function(data){
          var h = "Share: <a href='" + data + "'>" + data + "</a>";
          h += " | <a href='http://twitter.com/home?status=" + escape(data) + "'>tweet this</a>";
          jQuery("#" + id).html(h);
          jQuery("#" + id).show('med');
    });
}

function ignoreUser(twitter_id, screen_name)
{
    if (confirm("Delete " + screen_name + '?'))
    {
        jQuery.get(twitterurl,
            {ignore: twitter_id},
            function(data){
                jQuery("#" + screen_name + ' .ignore').html("deleted!");
                jQuery("#" + screen_name).hide('fast');    
            });
    }
}


function toggleBackupTweets(twitter_id, screen_name)
{
    showLoadingIcon('#'+screen_name+' .backuptweets', 3);
    jQuery.get(twitterurl,
        {backuptweets: twitter_id},
        function(data){
            jQuery("#" + screen_name + ' .backuptweets').html(data);
        });
}

function getKDArchive(screen_name,y,m,p)
{    
    if( p == 1) {
        showLoadingIcon('#kdarchive', 4);
    } else {
        showLoadingIcon('#kdarchive-more', 5);
    }
        
    jQuery('#searchfield').val('Search Tweets');
    jQuery.get(twitterurl,
        {action: 'kdarchive', sn:screen_name, yr:y, mn:m, page:p},
        function(data){
            if(data == 'empty') {
                jQuery("#kdarchive-more").remove();
            }
            else if( p == 1 ) {
                jQuery("#kdarchive").html(data); 
            }
            else {
                jQuery("#kdarchive-more").before("<hr class='morehr'>" + data);
                jQuery("#kdarchive-more").unbind('click');                
                jQuery("#kdarchive-more").html('More');
            }
            
            jQuery("#kdarchive-more").bind("click",
                {sn:screen_name, yr:y, mn:m, page:p}, 
                function(e){
                    getKDArchive(e.data.sn, e.data.yr, e.data.mn, e.data.page + 1);
                    return false;
                });
        });
}

function getUserConnections(screen_name, cType, cCursor, cPage, cView, cListSlug)
{   
    if( cCursor == 0 ) {
        jQuery("#user-more").remove();
        return;
    }
     
    showLoadingIcon('#user-more', 5);
    
    jQuery.get(twitterurl,
        {action: 'userconnection', sn:screen_name, type:cType, cursor:cCursor, page:cPage, view:cView, listslug:cListSlug},
        function(data){
            if(data == 'empty') {
                jQuery("#user-more").remove();
            }
            else {
                jQuery(".users").append("<hr class='morehr xsize'>" + data); 
                jQuery(".users .dofadein").fadeIn('fast', function() {
                    jQuery('#userpage' + cPage + ' .profileimg a.thickbox').lightBox();
                });
                
                jQuery("#user-more").unbind('click');         
                jQuery("#user-more").html('More');
                
                var div = jQuery("#user-more")[0];
                cCursor = jQuery.data(div, "usercon").cursor;
                cPage2 = jQuery.data(div, "usercon").page;
                
                if( cCursor == 0 )
                    jQuery("#user-more").remove();
                else {
                    l = 'http://tweeple.in/user/' + screen_name + '/' + cType;
                    if( cType == 'list' ) 
                        l = l + '/' + cListSlug;
                        
                    l = l + '/?page=' + cPage2 + '&cursor=' + cCursor + '&view=' + cView;
                    jQuery("#user-more").attr('href', l);
                    
                    jQuery("#user-more").bind("click",
                    {sn:screen_name, type:cType, cursor:cCursor, page:cPage2, view:cView, listslug:cListSlug}, 
                    function(e){
                        getUserConnections(e.data.sn, e.data.type, e.data.cursor, e.data.page, e.data.view, e.data.listslug);
                        return false;
                    });
                }
            }
        });
}

function searchTweets(screen_name) {
    var sq = jQuery('#searchfield').val();
    showLoadingIcon('#kdarchive', 4);
    jQuery.get(twitterurl,
        {action: 'searchtweets', sn:screen_name, q:sq},
        function(data){
            jQuery("#kdarchive").html(data);   
        });
}

function approveUser(screen_name)
{    
    showLoadingIcon('#'+screen_name+' .approveuser', 5);
    jQuery.get(twitterurl,
        {approveuser: screen_name, isajax:1},
        function(data){
            if( data == 'Approved' )
            {
                jQuery("#" + screen_name + ' .approveuser').html("approved!");   
                jQuery("#" + screen_name).removeClass('notlisted'); 
                jQuery("#" + screen_name + ' .topsection')
                    .animate( { backgroundColor: '#fffbb6' }, 200)
                    .animate( { backgroundColor: '#fff' }, 200);
            }
        });
}

function updateUser(screen_name)
{    
    showLoadingIcon('#'+screen_name+' .updateuser', 5);
    jQuery.get(twitterurl,
        {updateuser: screen_name, isajax:1},
        function(data){
            if( data == 'Updated' ) {
                jQuery("#" + screen_name + ' .updateuser').html("updated!");
            }
            else {
                jQuery("#" + screen_name + ' .updateuser').html("Update");
            }
        });
}

function setUserProperty(n, p, v)
{
    if( p == 'date_of_birth' )
    {
        if( !v.match(/(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)\d\d/g) ) {
            return;
        }
    }
    
    jQuery.get(twitterurl,
        {screen_name: n, property: p, value: v},
        function(data){
            jQuery("#" + n + ' .topsection')
                .animate( { backgroundColor: '#fffbb6' }, 200)
                .animate( { backgroundColor: '#fff' }, 200);
        });
}

function getPropertyHtml(n, p, v)
{
    if( p == 'gender' )
    {
        var t = 'M';
        if( v == 'M') {
            t = 'F';
        }
        
        return "<p class='title'>Gender</p><p class='textcenter'><a href=\"\" onclick=\"setUserProperty('" + n + "', 'gender', '" + t + "'); return false;\">" + v + "</a></p>";
    }
}

function selectProperty(e, o, n, p)
{
    if( p == 'social' && e.which != 13 ) 
    {
        return false;
    }
    
    var v = jQuery(o).val();
    setUserProperty(n, p, v);    
    return false;
}

function showAddTagForm()
{
    jQuery("#addausertag").hide();
    jQuery("#addausertagform").show();
}

function addATag(n, tid, t)
{
    jQuery.get(twitterurl,
        {screen_name: n, twitter_id: tid, addtag: t},
        function(data){
            jQuery("#usertagslist").html(data);  
            jQuery("#usertag").val('');  
            jQuery("#usertag").focus();  
        });
}

function deleteTag(n, tid, t)
{
    jQuery.get(twitterurl,
        {screen_name: n, twitter_id: tid, deletetag: t},
        function(data){
            jQuery("#usertagslist").html(data);      
        });
}

function checkUser(username)
{
    if( username != '' )
    {
        var u = username.replace(/https?:\/\/twitter.com\//, '');
        u = baseurl + '/user/' + u;
        
        jQuery("#checkuser").val("");
        window.open(u, '_blank');
    }
}

function showLocationEditForm(id, i)
{
    var lid = id.replace(/loc/, '');
    jQuery.get(twitterurl,
        {type: 'getlocform', locid:lid},
        function(data){
            data = "<th>" + i + "</th><td colspan='5'>" + data + "</td>";
            jQuery("#" + id).html(data);      
        });    
}

function optimize()
{
    jQuery.get(twitterurl,
        {action: 'optimize'},
        function(data){
            jQuery("#optimize")
                .animate( { backgroundColor: '#fffbb6' }, 200)
                .animate( { backgroundColor: '#F3F4E8' }, 200);      
        });    
}

function addLocation(lid, lcity, lcity_old, lstate, lcountry)
{
    if( lcity != '' && lstate != '' && lcountry != '' )
    {
        var geocoder = new GClientGeocoder();
        var address = lcity + ',' + lstate + ',' + lcountry;
        geocoder.getLatLng(
            address,
            function(point) 
            {
                if (!point) {
                    alert(address + " not found");
                } 
                else 
                {
                    var lat = point.y;
                    var lng = point.x;
                    
                    jQuery.get(twitterurl,
                    {type:'addlocation', id:lid, city:lcity, city_old:lcity_old, state:lstate, country:lcountry, latitude:lat, longitude:lng},
                    function(data){                        
                            jQuery("#locationswrap").html(data);
                            jQuery("#location_city").val(''); 
                            jQuery("#location_city_old").val(''); 
                            jQuery("#location_state").val(''); 
                            jQuery("#location_city").focus();
                    }); 
                }
            }
          );   
    }
}
