function prettyDate(time){
	var date = new Date(time),
		diff = (((new Date()).getTime() - date.getTime()) / 1000),
		day_diff = Math.floor(diff / 86400);

	if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
		return;

	return day_diff == 0 && (
			diff < 60 && "just now" ||
			diff < 120 && "1 minute ago" ||
			diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
			diff < 7200 && "1 hour ago" ||
			diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
		day_diff == 1 && "Yesterday" ||
		day_diff < 7 && day_diff + " days ago" ||
		day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}

// start tooltip for user proffile
$(document).ready(function() {
    if($('#proffile_body').length > 0){
        initTootip();
    }
});

function initTootip() {
    $(".tooltip").each(function(){
            $(this).qtip({
                content: {
                  text: '<img src="images/loader.gif" alt="" />',
                  url: '?c=ajaxProffile',
                  data: {screen_name: $(this).attr('rel')},
                  method: 'get'
               },
               style: {
                  border: {
                     width: 1,
                     radius: 4,
                     color: '#33CCFF'
                  },
                  background: '#FFF',
                  name: 'light',
                  tip: 'topMiddle' // Notice the corner value is identical to the previously mentioned positioning corners
               }
            })
        })
}

$(document).ready(function() {

    var originalText = '';

    $('input.input').click(function() {

        originalText = $(this).attr('title');
        if( $(this).val() == originalText || $(this).val() == '') {
           $(this).val('');
        }

    });

    $('input.input').blur(function() {

        if( $(this).val() == originalText || $(this).val() == '') {
           $(this).val( originalText );
        }

    });
    
});

$(document).ready(function() {

    var originalText = '';

    $('.textarea').click(function() {

        originalText = $(this).attr('title');
        if( $(this).html() == originalText || $(this).html() == '') {
           $(this).html('');
        }

    });

    $('.textarea').blur(function() {
        if( $(this).html() == originalText || $(this).html() == '') {
           $(this).html( originalText );
        }

    });

});

$(document).ready(function() {
    
    $("input[type=file]").filestyle({
         image: "images/browse.gif",
         imageheight : 25,
         imagewidth : 104,
         width : 427
    });

});

function frmSubmit() {
    var isErrors = 0;
    var elem = document.frm1.elements;

    for(var i = 0; i < elem.length; i++){
	if (elem[i].title != '' ){
            
            if(elem[i].title == elem[i].value || elem[i].value == '' ){
               //elem[i].style.background = '#EECCBF';
               $('#'+elem[i].id).css({'background':'#EECCBF'});
               isErrors += 1;
            }
            else {
               $('#'+elem[i].id).css({'background':'#FFF'});
            }
           
        }
    }
    

    if(isErrors == 0) {document.frm1.submit();}
    else {
        $("#errorForm").slideDown("slow");
    }

    
}

$(document).ready(function() {
    
    $("#address").blur(function(){
        createMap();
    });

});

var page = 1;
function listMore() {

    page += 1;

    $('#sharesLoader').show();
    $.post("/ajaxList", {page: page, 'results': results, 'id': id},
      function(data){
        $('#sharesLoader').hide();
        $('#sharesMore'+page).html(data+'<div id="sharesMore'+(page+1)+'"></div>');
        initTootip();
      });

}

var spage = 1;
function listMoreTweets() {

    spage += 1;

    $('#sharesLoader').show();
    $.post("/ajaxSearch", {page: spage, 'results': resultst, 'id': idt},
      function(data){
        $('#sharesLoader').hide();
        $('#sharesMore'+spage).html(data+'<div id="sharesMore'+(spage+1)+'"></div>');
        initTootip();
      });

}
/******* FORM LIMITATION SCRIPT START *********/
var textlength;
function limitChars(textid, limit, infodiv){

	 var text = $('#'+textid).val();
	 textlength = text.length;

	 if(textlength > limit){

		$('#' + infodiv).html('0');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else{
		$('#' + infodiv).html(limit - textlength);
		return true;
	}

}

// start limitation
/*$(function(){
	$('#mycomment').keyup(function(){
		limitChars('mycomment', 100, 'numbers');
	})
});*/
/******* FORM LIMITATION SCRIPT END *********/

function showComments() {
    $('#tweetsAround').hide();
    $('#tweetsAroundTab').removeClass('select');
    $('#commentsTab').addClass('select');
    $('#comments').show();
}

var tpage = 0;
function tweetsAround(longt, lat, title) {

    tpage++;

    $('#comments').hide();
    $('#commentsTab').removeClass('select');
    $('#tweetsAroundTab').addClass('select');
    $('#tweetsAround').show();
    $('#tweetsLoader').show();

    $.post("/ajaxTweetsAround", {page: tpage, 'results': 15, 'longt': longt, 'lat': lat, 'title':title},
      function(data){
        $('#tweetsLoader').hide();
        $('#tweetsMore'+tpage).html(data+'<div id="tweetsMore'+(tpage+1)+'"></div>');
        initTootip();
        $('#tweetsAroundMore').show();
      });
}
var comments = 0;
function submitComment( owner, share ) {

    comments++;

    text = $('#mycomment').val();
    video = $('#youtube').val();
    image = $('#image').val();

    if(text != '') {

        $('#commentLoader').show();

         $.post("/ajaxAddComment", {owner: owner, text: text, video:video, image:image, share: share},
          function(data){

            $('#commentsMore').html( '<div class="mycomment" id="mycomment'+comments+'">'+data+'</div>'+$('#commentsMore').html());

            $('#mycomment'+comments).slideDown('slow')

            initTootip();

            $('#mycomment').val('');
            $('#commentLoader').hide();
            $('#youtubeContainer').hide();
            $('#youtubeContainer input').val('');
            $('#imageContainer').hide();
            $('#imageContainer input').val('');

             $("a.thumb").fancybox({
                        'overlayOpacity'	:	0.7,
                        'overlayColor'		:	'#000'
            });

          });
    }
}

function replayComment( screen_name ){
   
    $('#mycomment').val('@'+screen_name+' ');
    $('#mycomment').focus();
    $.scrollTo('#googleAds468', 400);
    
}

function fbs_click()
{
  u=location.href;
  t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
  return false;
}

function rating(rate, id){

 $('#ratingResult').html('<img src="images/small-loader.gif" alt="" />');

    $.ajax({
                 type: "POST",
                 url: "/ajaxRating",
                 data: "rate="+rate+"&id="+id,
                 dataType: 'json',
                 success: function(data) {

                     $.each(data.rating, function(i,item){

                       message = item.message;
                       ratingPoints =item.rating;

                       $('#ratingResult').html(message);
                       $('#ratingNumber').html(ratingPoints);

                     });
                }
    });
}

var ppage = 1;
function listMoreUsers() {

    ppage++;

    $('#peopleLoader').show();

    $.post("/ajaxPeople", {page: ppage, 'results': presults, 'order': porder},
      function(data){
        $('#peopleLoader').hide();
        $('#morePeople'+ppage).html(data+'<div id="morePeople'+(ppage+1)+'"></div>');
      });
}

function closeLink( link ) {
   
   if(link =='youtube'){
       $('#youtubeContainer').hide();
       $('#youtubeContainer input').val('');
   }
   if(link =='image'){
       $('#imageContainer').hide();
       $('#imageContainer input').val('');
   }
    
}
function openLink( link ) {

   if(link =='youtube'){
       $('#youtubeContainer').show();
   }
   if(link =='image'){
       $('#imageContainer').show();
   }

}