$(document).ready(function() {

    // Default value in the header search field
    swap_val = [];  
    $(".swap").each(function(i){  
        swap_val[i] = $(this).val();  
        $(this).focusin(function(){  
            if ($(this).val() == swap_val[i]) {  
                $(this).val("");  
            }  
        }).focusout(function(){  
            if ($.trim($(this).val()) == "") {  
                $(this).val(swap_val[i]);  
            }  
        });  
    });  

 /*   // Fixing menu size and align
    var count = 0;
    var maxSize = 500;
    $("#nav-top li").each(function() {
        count = count + $(this).width();
    });
    $("#nav-top").css("left", (((maxSize - count) / 2) + parseInt($("#nav-top").css("left"))) + "px");

  
    // Fixing menu item size
    $("#nav-top li a").each(function() {
        if($(this).width() % 2 == 0) {
            $(this).css("width", ($(this).width()+1) + "px");
        }
    });
    
    // Adding corners to nav
    $("#nav-top li a").each(function() {
        $(this).prepend('<span class="corner1"></span><span class="corner2"></span>');
    });*/




    // Prevent double click
 // $('form').submit(function(){
 // 	$(':submit', this).click(function() {
 // 		return false;
 // 	});
 // });  


    // Adding corners to buttons
    $("a.button").each(function() {
        $(this).prepend('<span class="corner1"></span><span class="corner2"></span>');
    });

    // Adding corners to buttons blue
    $("a.button-blue").each(function() {
        $(this).prepend('<span class="corner1"></span><span class="corner2"></span><span class="corner3"></span><span class="corner4"></span>');
    });

    // Fixing menu size
    $("a.button").each(function() {
        if($(this).width() % 2 == 1) {
            $(this).css("width", ($(this).width()+1) + "px");
        }
    });

    // Making submit when click
    $("a.button-submit").each(function() {
        $(this).click(function() {
            $(this).find("input").click();
        });
    });


    // Config tables
    //$('#content .boxRound table tr').each(function() {
    //    $(this).find("td").first().addClass("first");
     //   $(this).find("td").last().addClass("last");
    //});
    //$('#content .boxRound table tbody tr:last-child').find("td").css("padding-bottom", "6px");

    
    
    
    // Auto completion of the search field
    $(function() {
        $(".autocomplete").autocomplete('/search/autocomplete', {
          dataType: "json",
          parse: function(data) {
            return $.map(data, function(row) {
              return {
                data: row,
                value: row.name,
                result: row.name
              }
            });
          },
          formatItem: function(user) {
            return '<img src="' + user.image + '" alt="" height="24" width="24" />' +
                   '<span>' + user.name + '</span><small>' + user.location + '</small>';
          }
        }).result(function(e, item) {
        	location.href = '/profile/user/' + (item.id) + '/' + item.displayname;
          //$(e.target).parents('form').submit();
        });
      });
    
    
   
});


// Characters left
(function($) {

	$.fn.charCount = function(options){
	  
		// default configuration properties
		var defaults = {	
			allowed: 140,		
			warning: 25,
			cssWarning: 'warning',
			cssExceeded: 'exceeded',
			counterText: ' left',
			counterDiv: 'counter'
		}; 

		var options = $.extend(defaults, options); 
		
		var counter_div = $('#' + options.counterDiv);
		
		function calculate(obj){
			var count = $(obj).val().length;
			var available = options.allowed - count;
			if(available <= options.warning && available >= 0){
				counter_div.addClass(options.cssWarning);
			} else {
				counter_div.removeClass(options.cssWarning);
			}
			if(available < 0){
				counter_div.addClass(options.cssExceeded);
			} else {
				counter_div.removeClass(options.cssExceeded);
			}
			counter_div.html(available + options.counterText);
		};
				
		this.each(function() {  
			calculate(this);
			$(this).keyup(function(){calculate(this)});
			$(this).change(function(){calculate(this)});
		});
	  
	};

})(jQuery);    


$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
while(x<c.length){var m=r.exec(c.substr(x));
  if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
  }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
  o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});


function follow(id,displayname) {
	$.ajax({
		  type: 'POST',
		  url: '/network/follow/' + id + '/' + displayname,
		  success: function(data) {
		  		if (data.result == 1) {
		  			$('#follow' + id).html('<img src="/images/v3/icons/unfollow.png" width="18" height="18" style="margin-bottom:-5px" />&nbsp; Unfollow');
		  			$('#follow' + id).attr('href','javascript:unfollow(' + id + ',"' + displayname + '");');
		  		} else if (data.result == 0) {
		  			$('#follow' + id).html('<img src="/images/v3/icons/pending.png" width="18" height="18" style="margin-bottom:-5px" />&nbsp; <span style="color:#FF2000">Pending</span> - Cancel</a>');
		  			$('#follow' + id).attr('href','javascript:unfollow(' + id + ',"' + displayname + '");');
		  		} else if (data.result == 0) {
		  			alert('You cannot follow more than 20 new people per day');
		  		}
			},
		  dataType: 'json'
		});
};

function unfollow(id,displayname) {
	$.ajax({
		  type: 'POST',
		  url: '/network/unfollow/' + id + '/' + displayname,
		  success: function(data) { 
		  		$('#follow' + id).html('<img src="/images/v3/icons/follow.png" width="18" height="18" style="margin-bottom:-5px" />&nbsp; Follow');
		  		$('#follow' + id).attr('href','javascript:follow(' + id + ',"' + displayname + '");');
			},
		  dataType: 'json'
		});
};



