// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function initializeCommentPostLink($) {
	$('a.comment-post-link').facebox();
}
function initializeTellAFriend($) {
	$('.tellafriend').facebox();
}
function initializeHeaderLogin($) {
	$('#login-content').hide();
	$('#close-page-login, #login-link').click(function(e){
		e.preventDefault();
		// $('#login-content').toggle('slow');
		if ($('#login-content').is(":hidden")) { $("#login-content").slideDown("slow");} 
		else { $("#login-content").fadeOut("slow"); }
	});	
}
function initializeDeletePostLink($) {
	$('.preview_post a.delete-post-link, .view_post a.delete-post-link').each(function(i){
		$(this).click(function(e){
			e.preventDefault();
			if(confirm("Are you sure you want to delete this content?")) {
				url = this.href;
				$.ajax({
					url: url, 
					data: {_method: 'delete'},
					type: 'POST',  
					success: function(data) {
						post = uie.utils.evalJson(data); 
						$('#post-'+post.id).slideUp('slow').remove();
					}
				});
			}
		});
	});
}
function initializeFeaturePostLink($) {
	$('.preview_post a.feature-post-link, .view_post a.feature-post-link').each(function(i){
		$(this).click(function(e){
			e.preventDefault();
			url = this.href;
			$.ajax({
				url: url, 
				data: {_method: 'delete'},
				type: 'POST',  
				success: function(data) {
					post = uie.utils.evalJson(data); 
					if($('#post-'+post.id).length > 0) { 
						$('.feature-post-link', $('#post-'+post.id)[0]).toggleClass('featured'); 
					}
				}
			});
		});
	});
}
function initializeDeleteGroupLink($) {
	$('a.delete-group-link').each(function(i){
		$(this).click(function(e){
			e.preventDefault();
			if(confirm("Are you sure you want to delete this group?")) {
				auth_token = this.id.split('-')[2];
				url = this.href;
				$.ajax({
					url: url, 
					data: {_method: 'delete', authenticity_token: auth_token},
					type: 'POST',  
					success: function(data) {
						group = uie.utils.evalJson(data); 
						if($('#group-preview-'+group.id).length > 0) { $('#group-preview-'+group.id).slideUp('slow').remove(); }
					}
				});
			}
		});
	});
}
function initializeDeleteCommentLink($) {
	$('a.delete-comment-link').each(function(i){
		$(this).click(function(e){
			e.preventDefault();
			if(confirm("Are you sure you want to delete this comment?")) {
				auth_token = this.id.split('-')[2];
				url = this.href;
				$.ajax({
					url: url, 
					data: {_method: 'delete', authenticity_token: auth_token},
					type: 'POST',  
					success: function(data) {
						comment = uie.utils.evalJson(data); 
						if($('#comment-preview-'+comment.id).length > 0) { $('#comment-preview-'+comment.id).slideUp('slow').remove(); }
					}
				});
			}
		});
	});
}
function initializeFBLogin($) {
/*
	$('a.fb-login').click(function(e){
		e.preventDefault();
		var fburl = 'http://'+wwdomain+'/fbconnect/login?backurl='+getSafeUrl();
		FB.Connect.requireSession( function(){facebook_onlogin_ready(fburl);});
	});
*/	
	$('.custom-fb-login').click(function(e){
		e.preventDefault();
		FB.Connect.requireSession(doFacebookLogin, isUserActionHint=true);
/*		
		var width = 480;
    var height = 460;
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
    var windowFeatures = "width=" + width + ",height=" + height + ",status,resizable=yes,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
		window.open(this.href, '_blank', windowFeatures, true);
*/		
	});
}
function getSafeUrl() {
	encodeURIComponent(window.location.href);
}
function doFacebookLogin() {
	//console.debug('doFacebookLogin');
	//if(!window.location.href.match(/\/login/)){
		jQuery.ajax({
			url: '/login/facebook/connect.js', 
			success: function(data) {
				jQuery.facebox(data);
				$('#fb-registration-form').append("<input type='hidden' name='fb_return_to' value='"+window.location.href+"' />");
			}, 
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				var txt = XMLHttpRequest.responseText;
				console.debug(txt);
			}
		});
	//}
}
function updateFbBox() {
	//console.debug('updateFbBox');
	var h = "<div class='fb-info'>" + 
		"<fb:profile-pic uid=loggedinuser facebook-logo=\"true\" linked=\"false\"></fb:profile-pic>" + 
		"<br/>" + 
		"Welcome, <fb:name uid=loggedinuser useyou=false></fb:name>" + 
		"</div>";
	//jQuery('#fb-login-item').html(h);
	//FB.XFBML.Host.parseDomTree(); 
}

jQuery(function($){
	initializeCommentPostLink($);
	initializeFeaturePostLink($);
	initializeTellAFriend($);
	initializeHeaderLogin($);
	initializeDeletePostLink($);
	initializeDeleteGroupLink($);
	initializeDeleteCommentLink($);
	initializeFBLogin($);
});
