var IsasLogin = {
	submitLoginForm : function(form){
		$.ajax({
			url: $(form).attr('action'),
			data: $(form).serialize(),
			type: 'POST',
			dataType: 'json',
			error: function(){},
			success: function(reply){
				
				if(reply.code == 1){
					
					IsasLogin.setLoggedInHeaderLinks(reply.isas_uid, reply.username);
					if(reply.fb_uid){
						IsasLogin.showLoggedInFBUser(reply.fb_uid);
						//fb_uid = reply.fb_uid;
					}
	
					isas_uid = reply.isas_uid;
					$.modal.close();
				}else{
					displayFormErrors(reply.errors);
				}
			}
		});
		return false;
	},
	
	setLoggedInHeaderLinks : function(isas_uid, username){
		var html = '<ul>';
		html += '<span id="profilelink_top"></span>&nbsp;';
		html += '<li><a href="/' + username + '">My Account</a> | </li>';
		html += '<li><a href="#" onclick="return facebook.logOut(IsasLogin.logout);" title="Click here to logout.">Logout</a></li>';
		html += '</ul>';
		$('.account-links').html(html);
		$('.fb-connect').html('');
	},
	
	logout : function(){
		top.location = '/logout';
		return false;
	},
	
	FBLogin : function(uid, formData){
		$.ajax({
			url: '/login/fb',
			data: formData,
			type: 'POST',
			dataType: 'json',
			success: function(reply){
				
				if(reply.code == 1){
					IsasLogin.setLoggedInHeaderLinks(reply.isas_uid, reply.username);
					IsasLogin.showLoggedInFBUser(reply.fb_uid);
					
					isas_uid = reply.isas_uid;
					username = reply.username;
					
					if (typeof $.modal.close() == 'undefined' ){
						
						if( (isas_uid > 0) && (callBack != null) && (callBack.action) ){
							callBack.action();
						} else {
							callBack = null;
						}
						
					} else {
						$.modal.close();
					}
					
				}else{
					$.modal.close();
				}
			}
		});
		return false;
	},
	
	FBdisassociate: function(){
		$.ajax({
			url:'/users/fb_disassociate/',
			type:'POST',
			dataType:'json',
			success: function(reply){
				top.location = '/' + username + '/?fbLogout=1'; 
			}
		});
	},
	
	showLoggedInFBUser : function(uid){
		var containerElem=$('#profilelink_top');
		containerElem.html('<span id="fb_identity_profilepic" uid="'+uid+'" linked="false" facebook-logo="true" width="16px" height="16px" style="width:16px;height:16px;"></span>');
		var profilePicContainer=$('#fb_identity_profilepic');
		FB.XFBML.Host.addElement(new FB.XFBML.ProfilePic(profilePicContainer[0]));
	}
}

function getFormErrors(objId){
	return $.parseJSON($(objId).html());
}

function displayFormErrors(errors){
	$(".error").html('');
	for(var field in errors){
		$("."+field+"_error").html(errors[field]);
		if(field.indexOf('recaptcha') != -1){
			Recaptcha.reload();
		}
	}
}

function deleteField(elem, fieldName , redir){
	var delete_field = confirm('Are you sure you want to delete this ' +fieldName+ '?');
	if (delete_field) {
		var url = elem.href;
		$.ajax({
			url: url,
			dataType: 'json',
			success: function(response){
				if (response.code == 1) {
				//success!;
				}
				else {
				//error!
				}
				top.location = redir;
			}
		});
	}
	return false;
}

function getVideoNote(elem, updateField){
	var site_id = elem.value;
	$.ajax({
		url: '/news/get_video_code',
		data: { site_id : site_id },
		type: 'post',
		dataType: 'json',
		success: function(response){
			if(response.code == 1){
				$(updateField).html(response.videoNote)
			} else {
				$(updateField).html('');
			}
		}
	});
}
