var searchBar = {
	init: function(){
		searchValue = $('#search-input').attr('value');
		$('#search-input').focus(function(){
			$(this).css({'backgroundColor': '#fcfcfc', 'color': '#777'});
			if($(this).attr('value') == searchValue)
			{
				$(this).attr('value', '');
			}
		});
	
		$('#search-input').blur(function(){
			$(this).css({'backgroundColor': '#fff', 'color': '#bbb'});
			if($(this).attr('value') == '')
			{
				$(this).attr('value', searchValue);
			}
		});
		
		$('#search-input').keydown(function(e){
			if (e.which == '13') 
			{
				$('#search-loading').fadeIn();
			}
		});
	}
}

var commentBar = {
	init: function(){
		profileCommentValue = $('#comm_str').attr('value');
		$('#comm_str').focus(function(){
			$(this).css({'color': '#444'});
			if($(this).attr('value') == profileCommentValue)
			{
				$(this).attr('value', '');
			}
		});
	
		$('#comm_str').blur(function(){
			$(this).css({'color': '#bbb'});
			if($(this).attr('value') == '')
			{
				$(this).attr('value', profileCommentValue);
			}
		});
	}
}

var statusUpdate = {
	init: function(){
		statusValue = $('#status').attr('value');
		$('#status').focus(function(){
			$(this).css({'color': '#444'});
			if($(this).attr('value') == statusValue)
			{
				$(this).attr('value', '');
			}
		});
	
		$('#status').blur(function(){
			$(this).css({'color': '#bbb'});
			if($(this).attr('value') == '')
			{
				$(this).attr('value', statusValue);
			}
		});
	}
}

var photoComment = {
	init: function(){
		commentValue = $('#photo-comment-input').attr('value');
		$('#photo-comment-input').focus(function(){
			$(this).css({'color': '#666'});
			if($(this).attr('value') == commentValue)
			{
				$(this).attr('value', '');
			}
		});
		
		$('#photo-comment-input').blur(function(){
			$(this).css({'color': '#ccc'});
			if($(this).attr('value') == '')
			{
				$(this).attr('value', commentValue);
			}
		});
	}
}

var feeds = {
	init: function(){
		this.currentFeed == '';
		$('#twitter-feed').click(function(){
			if(feeds.currentFeed == 'fb')
			{
				$('#fb-feed').css({'background': 'url(/img/fb_off.png) no-repeat'});
				$(this).css({'background': 'url(/img/twitter_on.png) no-repeat'});
				feeds.currentFeed = 'twitter';
			}
			else if(feeds.currentFeed == 'twitter'){
				$(this).css({'background': 'url(/img/twitter_off.png) no-repeat'});
				feeds.currentFeed = '';
			}
			else{
				feeds.currentFeed = 'twitter';
				$(this).css({'background': 'url(/img/twitter_on.png) no-repeat'});
			}
		});
		
		$('#fb-feed').click(function(){
			if(feeds.currentFeed == 'twitter')
			{
				$('#twitter-feed').css({'background': 'url(/img/twitter_off.png) no-repeat'});
				$(this).css({'background': 'url(/img/fb_on.png) no-repeat'});
				feeds.currentFeed = 'fb';
			}
			else if(feeds.currentFeed == 'fb'){
				$('#fb-feed').css({'background': 'url(/img/fb_off.png) no-repeat'});
				feeds.currentFeed = '';
			}
			else{
				feeds.currentFeed = 'fb';
				$(this).css({'background': 'url(/img/fb_on.png) no-repeat'});
			}
		});
	}
}

var updateStatusOptions = {
	init: function(){
		$('#update-options').hover(
			function(){
				$('#update-options-helper-text').stop(true,true).fadeIn('fast');
			},
			function(){
				$('#update-options-helper-text').stop(true,true).fadeOut('fast');
			}
		);
		
		$('.update-twitter').toggle(
		function(){
			$('#status').focus();
			var cls = $(this).attr('class');
			$(this).attr('id', cls+'-on');
			$(this).css({'background': 'url(/img/light_twit_on.png) no-repeat'})
		},
		function(){
			$('#status').focus();
			var cls = $(this).attr('class');
			$(this).attr('id', cls+'-off');
			$(this).css({'background': 'url(/img/light_twit_off.png) no-repeat'})
		}
		);
		
		$('.update-fb').toggle(
			function(){
				$('#status').focus();
				var cls = $(this).attr('class');
				$(this).attr('id', cls+'-on');
				$(this).css({'background': 'url(/img/light_fb_on.png) no-repeat'})
			},
			function(){
				$('#status').focus();
				var cls = $(this).attr('class');
				$(this).attr('id', cls+'-off');
				$(this).css({'background': 'url(/img/light_fb_off.png) no-repeat'})
			}
		);
	}
}

var groupChat = {
	variables: function(url, groupID){
		this.url = url+groupID;
	}
}

var messageSelect = {
	init: function(){
		$('#select-all').data('selected', false);
		$('#select-all').click(function(){
			messageSelect.clicked();
		});
	},
	clicked: function()
	{
		if($('#select-all').data('selected') == true)
		{	
			$('#select-all').html('Select All');
			$('#select-all').data('selected', false);
			$('#content input').attr('checked', '');
		}
		else if($('#select-all').data('selected') == false)
		{	
			$('#select-all').html('Deselect All');
			$('#select-all').data('selected', true);
			$('#content input').attr('checked', 'checked');
		}
	}
}

$(document).ready(function(){
	searchBar.init();
	statusUpdate.init();
	photoComment.init();
	updateStatusOptions.init();
	feeds.init();
	commentBar.init();
	messageSelect.init();
	
	$('#mail-submenu').data('open', false);
	$('#notifications-submenu').data('open', false);
	
	assignHeights();

	$('#membership-type li div').click(function(){
		$('#membership-type li div').css({'backgroundColor': '#f4fbff', 'color' : '#666'})
		$(this).css({'backgroundColor': '#222', 'color' : '#f7f7f7'})
		$('#membershipType').attr('value', $(this).attr('id'));
	});
	
	$('.box-holder').hover(
		function(){
			$(this).find('.item-info').stop(true, true).fadeIn();
		},
		function(){
			$(this).find('.item-info').stop(true, true).fadeOut();
		}
	);
	
	$('.organization .user-holder').toggle(
		function(){
			$(this).css({'backgroundColor': '#f4fbff'})
		},
		function(){
			$(this).css({'backgroundColor': 'transparent'})
		}
	);
	
	$('#account-link').hover(
		function()
		{
			$(this).addClass('selected');
			$(this).find('.submenu').stop().show();
		},
		function()
		{
			$(this).removeClass('selected');
			$(this).find('.submenu').stop().hide();
		}
		
	);
	
	//MAIL AND MESSAGES LINKS IN HEADER
	$('#mail-link').click(function(){
		$(this).find('.notification').hide();
		$(this).find('img').attr('src', '/img/envelope_on.png');
		if($('#mail-submenu').data('open') == false)
		{
			$(this).addClass('selected');
			$('#mail-submenu').stop().show();
			$('#mail-submenu').data('open', true);
			hideNotificationsSubmenu();
		}
		else
		{
			hideMailSubmenu();
		}		
	});
	
	$('#notifications-link').click(function(){
		$(this).find('.notification').hide();
		$(this).find('img').attr('src', '/img/chat_bubble_on.png');
		if($('#notifications-submenu').data('open') == false)
		{
			$(this).addClass('selected');
			$('#notifications-submenu').stop().show();
			$('#notifications-submenu').data('open', true);
			hideMailSubmenu();
		}
		else
		{
			hideNotificationsSubmenu();
		}
	});
	
	$('.notify-submenu li').click(function(){
		$(this).removeClass('new-notify');
	});
	
	//POPUP ACTIONS
	$('.close-popup-button').click(function(){
		$('.popup-holder').fadeOut();
		$('.popup-content-holder').fadeOut();
	});
	
	$('.show-popup').click(function(){
		var id = $(this).attr('id');
		$('.popup-holder').fadeIn();
		$('#'+id+'-popup').fadeIn();
	});
	
	//TWITTER REPLY BUTTON ACTIONS
	$('.twit-reply-button').live('click', function(){
		$(this).parents('.feed-content').find('.twit-reply').slideDown();
	});
	
	$('.cancel-twit-reply').live('click', function(e){
		e.stopPropagation();
		$(this).parents('.feed-content').find('.twit-reply').slideUp();
	});
	
	//CHANGE LANGUAGES POPUP
	$('#closePopup').click(function() {
		$('#lang-transparent-bg').fadeOut();		
	});
	
	//RESOURCES DOCUMENT ACTIONS
	$('.document').live('mouseenter', function(){
		$(this).css({'backgroundColor': '#f3f3f3'});
	});
	
	$('.document').live('mouseleave', function(){
		$(this).css({'backgroundColor': 'transparent'});
	});
	
	$('#view-all-members').click(function(){
		$('#popup-holder-members').fadeIn('slow');
	});

	$('#close-photo-viewer-button').click(function(){
		$('#photo-viewer-holder').fadeOut();
	});

	$('.section-menu li').click(function(){
		$('.section-menu li').removeClass('current');
		$(this).addClass('current');
		var id = $(this).attr('id');
		$('.section-content .sub-section').hide();
		$('#'+id+'-holder').show();
	});
	
	//RECIPIENTS MOUSE OVER
	$('.recipient').live('mouseenter',
		function(){
			$(this).css({'backgroundColor': '#444'})
	});
	$('.recipient').live('mouseleave',
		function(){
			$(this).css({'backgroundColor': '#888'})
		}
	);
	
	//TABS
	$('.section-menu li').click(function(){
		var id = $(this).attr('id');
		$(this).addClass('current');
		$('.sub-section').hide();
		$('#'+id+'-holder').show();
		if(id == 'chat')
		{
			$('#chatFrame').attr('src', groupChat.url);
			$('#loading-chat').hide();
		}
		if($(this).attr('class') != 'current')
		{
			assignHeights();
		}
	});	
});




//VALIDATE NEW PRAYER REQUEST FORM
function validateForm()
{
	valid = true;
	$('.required').each(function(){
		if($(this).attr('value') == '')
		{
			valid = false;
		}
	});
	if(valid == false)
	{
		alert('*Required fields are empty. Please try again.')
	}
	return valid;
}

//SEARCH RESULTS TABS
function toggle_ss(target_ss)
{
	$('#'+target_ss).slideToggle();
}

function assignHeights()
{
	var mainHeight = $('#main').height();
	$('#sub-sidebar').css({'minHeight': mainHeight});
	$('#content').css({'minHeight': mainHeight - 5});
}

function hideMailSubmenu()
{
	$('#mail-submenu').stop().hide();
	$('#mail-link').removeClass('selected');
	$('#mail-link').find('img').attr('src', '/img/envelope.png');
	$('#mail-submenu').data('open', false);
}

function hideNotificationsSubmenu()
{
	$('#notifications-submenu').stop().hide();
	$('#notifications-link').removeClass('selected');
	$('#notifications-link').find('img').attr('src', '/img/chat_bubble.png');
	$('#notifications-submenu').data('open', false);
}

function submit_language_change(lang_id)
{
	$('#languages').hide();
	$('#closePopup').hide();
	$('#languagePopup h3').hide();	
	$('#loading').fadeIn();
	//Code for getting language_id to PHP here (don't forget to write the php reciever too!)
	$.get(baseURL+'languages/set_language/user_id/'+userID+"/lang_id/"+lang_id, function() {
		$.get(baseURL+'languages/set_language/user_id/'+userID+"/lang_id/"+lang_id, function() {
			window.location.reload();
		});
	});
}

function show_language_form()
{
	var trans = $('#lang-transparent-bg');
	var popup = $('#languagePopup');
	trans.css('height', $('body').height());
	popup.css('marginTop', $('body').scrollTop() + 60)
	trans.fadeIn();
}

function hide_language_form()
{
	$('#transparent-bg').fadeOut();
	$('#language-form').fadeOut();
}

