
$(document).ready(function() {
	Cufon.replace('#intro h1, #features h3 big');
	
	$("tr:odd").addClass("odd");
	$('.toggle-handle').click(function(){
		$(this).toggleClass('off');
		$(this).next('div.toggle').toggle();
	});
	
	if ($('#flash-msg').length > 0) {
		var msg = $('#flash-msg').html();
		show_notice(msg);
	}	
	
	
	//Global form ajaxSubmit function
	$("form.ajaxSubmit").submit(function() {
		var url = $(this).attr('action');
		var target = $(this).attr('target');
		if (target) {
			var formData = $(this).serialize(); 
			$.ajax({
			    type: "GET",
			    url: url,
			    data: formData,
			    success: function(data) {
					$('#'+target).html(data);
					$('#'+target).attr('class', 'ajax');
			    }
			  });
		}
		  return false;
	});
	
	$("#listWrapper.ajax div.pagination a").live('click', function() {
		var listContainer = 'listWrapper';
		$.ajax({
			type: 'get',
			dataType: 'html',
			success: function(data){
			  $('#'+listContainer).html(data);
			},
			url:$(this).attr('href')
		});	
		return false;
	});
	
	
	//for invite list
	$('#inviteList li').live('click', function(event) {
		if ($(this).hasClass('invited')) {
			jAlert('This person is already invited','Information');
		} else if ($(this).hasClass('selected')) {
			$(this).removeClass('selected');
			$(this).children('input').removeAttr('checked');
		} else {
			$(this).addClass('selected');
			$(this).children('input').attr('checked','checked');
		}
	});
	
	$('div.filterShow a.filterAll').live('click', function(event) {
		if (!$(this).hasClass('selected')) {
			$('div.filterShow a').removeClass('selected');
			$(this).addClass('selected');
			$('div#inviteList li').fadeIn(100);
		}
		return false;
	});
	
	$('div.filterShow a.filterSelected').live('click', function(event) {
		if (!$(this).hasClass('selected')) {
			$('div.filterShow a').removeClass('selected');
			$(this).addClass('selected');
			$.each($('div#inviteList li'), function() {
				if (!$(this).hasClass('selected')) {
					$(this).fadeOut(100);
				}
			});
		}
		return false;
	});
	
	$('div#inviteList li').hover(
		function() { $(this).addClass('hover'); },
		function() { $(this).removeClass('hover'); }
	);
	
	$('input#inviteSearch').keyup(function() {
		var input = $(this).val();
		$.each($('div#inviteList li'), function() {
			var name = $(this).children('span.memberName').text();
			var query = new RegExp(input, "i");
			if (query.test(name)) {
				$(this).fadeIn(200);
			} else {
				$(this).fadeOut(200);
			}
		});	
	});

	$('input#inviteSearch').mousedown( function() {
		$(this).val('');
		$(this).focus();
	  });
	
	$('input#inviteSearch').focusout( function() {
		$(this).val('Start typing contacts name');
	  });
	
	$('a.inviteContact').live('click', function(event){
		var $pressed = $(this);
		var href = $(this).attr('href');
		jConfirm('User will have to confirm your request!', 'Friendship request', function(r) {
			if (r){
				jQuery.ajax({
					type: 'get',
					dataType: 'html',
					success: function(data){ $pressed.fadeOut(); show_notice('New friendship request sent');},
					url:href
				});
			}
		});
		return false;
	});
	
	$('a.approveContact').live('click', function(event){
		var $pressed = $(this);
		var href = $(this).attr('href');
		$pressed.unbind('click');
		jQuery.ajax({
				type: 'get',
				dataType: 'html',
				url: href,
				success: function(data) {
					if (data.indexOf('connected') != 0) {
						$pressed.fadeOut(); 
						show_notice(data); 
					}
					else {
						show_notice('Something went wrong! Please try again later');
					}
				}
			});
			
		return false;
	});
	
	$('a.removeContact').live('click', function(event){ 
		var $pressed = $(this);
		var href = $(this).attr('href');
		jConfirm('Are you sure that you want to remove this friend?', 'Friendship removal request', function(r) {;
			if (r){
				jQuery.ajax({
					type: 'get',
					dataType: 'html',
					success: function(data){ $pressed.fadeOut(); show_notice ('Contact removed'); },
					url:href
				});
			}
		});
		return false;
	});
	
	$('a.sendMessage').live('click', function (event){
		var href = $(this).attr('href');
		jQuery.ajax({
			type: 'get',
			dataType: 'html',
			url:href, 
			success: function(data){ 
		      $.modal(data, {
				onOpen: function (dialog) {
					dialog.overlay.fadeIn();
					dialog.container.fadeIn(function () { dialog.data.show(); });
					},
				/*onClose: function (dialog) {
					dialog.data.fadeOut(function () {
						dialog.container.hide();
					});
					dialog.overlay.fadeOut(function () {
						$.modal.close(); // must call this!
					});
				},*/
				overlayClose: true }
			  );
			}
		});
		
		return false;
	});
	
	$('#sendMessageToUser').livequery('submit', function(event) {
		var url = $(this).attr('action');
		var formData = $(this).serialize(); 
		
		if ($('#message_subject').val() == '') return false;
		if ($('#message_message_text').val() == '') return false;
		
		$.ajax({
			type: "POST",
			url: url,
			data: formData,
			success: function(data) {
				$.modal.close();
				show_notice(data);
			    //$('#simplemodal-data').html(data);
			}
		  });
		return false;
	});
	
	$.ajaxSetup({
		error:function(x,e){
			if(x.status==0){
			show_notice('You are offline!!\n Please Check Your Network.');
			}else if(x.status==401){
			jConfirm('You are either not authorized to view this content or your session has expired.\nPlease log in again!', 'Authorization error', function(response){
				if (response) {
					window.location = '/signin';
				}
			});
			}else if(x.status==404){
			show_notice('Requested URL not found.');
			}else if(x.status==500){
			show_notice('Internal Server Error. Please try again.');
			}else if(e=='parsererror'){
			show_notice('Error.\nParsing JSON Request failed.');
			}else if(e=='timeout'){
			show_notice('Request Time out.');
			}else {
			show_notice('Unknown Error.\n'+x.responseText);
			}
		}
	});
	
});

function show_notice(message, hide)
{
  if (typeof (hide) == 'undefined') hide = true;
  is_notice_set = $('body div.notice-field').length>0;
  if (is_notice_set) {
    $('.notice-field').fadeTo(0,0).html('<h1>Notice</h1>'+message).css("top", "250px");
    fade_flash(hide);
  } else {
    $('body').prepend('<div class="notice-field"><h1>Notice</h1>' + message + '</div>');
    fade_flash(hide);
  }
}
function fade_flash(hide)
{
    $('.notice-field').fadeTo(0,0).animate({top: "380px", opacity: 1}, 400);
	if (hide) $('.notice-field').delay(1500).fadeOut();    
}


