var toggleSection = function(section, subsection) {
	
	//if subsection isn't passed, use first subsection
	if (!subsection) {
		subsection = $('#' + section + '> .bsi_content > .bsi_subsection:first').attr('id'); 	
	}

	//show subsection
	if ($('#' + section + '> .bsi_content').css('display') == 'none') {
		//don't do effects if section is hidden, just show
		$('#' + subsection).show();
	} else {
		//do effects if section is open
		$('#' + subsection).slideDown();
	}

	//hide other subsections
	$('.bsi_subsection[id!=' + subsection + ']', $('#' + section)).hide();
	
	//hide other sections
	$('.tog', $('.bsi_section[id!=' + section + ']')).each(function(index, tog) {
		if ($(tog).hasClass('exp')) {
			$(tog).trigger('click');
		}
	});
	
	//show main section (if hidden)
	if (!$('.tog', $('#' + section)).hasClass('exp')) {
		$('.tog', $('#' + section)).trigger('click');
	}
	return false;
}

var getDashboardURL = function() {
	//get dash subsection
	var dash_subsection = window.location.href.substring(window.location.href.indexOf('section=') + 8).replace(/\W.*/g, '');
	//find subsection's parent section
	var dash_section = $('#' + dash_subsection).parents('.bsi_section').attr('id');
	if(dash_section) {
		//toggle the target section
		toggleSection(dash_section, dash_subsection);
	} else {
		//auto expand the first section
		var first_section = $('.bsi_section:first').attr('id');
		toggleSection(first_section);
	}
}

$(document).ready(function(){
	//if dashboard exists
	if($('#bsi_dashboard').length) {
		//give the last <li> in each <ul> a special class
		$('ul > li:last-child').addClass('last');
		
		var sections = $('.bsi_content');
		
		//show only the first subsection from each main section onload
		$('.bsi_content > .bsi_subsection:first-child').show();
		
		//add toggle events
		$('.tog').each(function(index, tog) {
			$(tog).click(function() {
				//change the text
				if ($(tog).hasClass('exp')) {
					$(tog).html('Expand [+]');
				} else {
					$(tog).html('Collapse [-]');
				}
				$(tog).toggleClass('exp');
				//do it
				$(sections[index]).slideToggle();	
				return false;
			});
		});
	}
	getDashboardURL();
});

function showOverlay(message) {
	//remove any existing overlays
	$('#bsi_overlay').remove();
	var overlay = $('<div id="bsi_overlay"><p>' + message + '</p></div>');
	//append to body and display for a bit
	overlay.appendTo($('body')).delay(1500).fadeOut();
}

function saveProfile() {
	$.ajaxSetup({
	  global: false,
	  type: "POST",
	  ajaxStart: showOverlay('Saving your profile...'),
	  ajaxComplete: showOverlay('Your profile information has been saved.')
	});
	
	$.post("process/dashboard.php",
	{ 
		profile_submit: '1',
		address: $('#address').val(),
		state: $('#state').val(),
		city: $('#city').val(),
		zip: $('#zip').val(),
		phone: $('#profile_phone').val(),
		phone2: $('#profile_phone2').val(),
		profile_cell: $('#profile_cell').val(),
		voice_part: $('#profile_voicepart').val(),
		joined_date: $('select[name=joined_Year]').val(),
		bio: tinyMCE.get('elm1').getContent(),
		email: $('#email').val(),
		sig: $('#sig').val(),
		old_pass: $('#old_pass').val(),
		new_pass: $('#new_pass').val(),
		notify: $('#notify:checked').val()
 	}, 
 	function (data){ 
 		$('#old_pass').val('');
 		$('#new_pass').val('');
		for (var i=0;i<data.form_errors.length;i++) {
			switch(data.form_errors[i]) {
				case 'restricted':
					$('#'+data.form_errors[i]).after('<span class=\'hint\'>The email you requested to change is already a registered email.</span>');
					break;
				case 'pass_restricted':
					$('#'+data.form_errors[i]).after('<span class=\'hint\'>The new password or old password did not match, password has not changed.</span>');
					break;
			}
		}
		if(data.changed) {
			window.location.href = '/?page=index';
		}
 	},"json");
}

function sendMessage() {
	$.ajaxSetup({
	  global: false,
	  type: "POST",
	  ajaxStart: showOverlay('Sending your message to ' + $('#compose_name').val() + '...'),
	  ajaxComplete: showOverlay('Your message has been sent to ' + $('#compose_name').val() + '.')
	});
	
	$.post("process/dashboard.php",
	{ 
		send_message: '1',
		to: $('#compose_name').val(),
		title: $('#compose_subject').val(),
		message: $('#compose_body').val()
 	}, 
 	function (data){ 
 		//reset fields
 		$('#compose_subject').val('');
 		$('#compose_body').val('');
 	},"json");
}

function viewMessage(id) {
	$.post("process/dashboard.php",
	{ 
		view_message: '1',
		id: id
 	}, 
 	function (data){ 
 		toggleSection('bsi_dashboard_inbox', 'bsi_dashboard_inbox_read');
 		$('#bsi_dashboard_inbox_read').html(data.msg_format);
	 },"json");
}

function deleteMsg(id) {
	$.post("process/dashboard.php.php",
	{ 
		delete_message: '1',
		id: id
 	}, 
 	function (data){ 
 		$('#bsi_dashboard_inbox_all').html(data.msg_format);
	 },"json");
}

function replyMsg(id,from,title) {
	toggleSection('bsi_dashboard_inbox', 'bsi_dashboard_inbox_compose');
	$('#compose_name').val(from);
	$('#compose_subject').val('RE: '+title);
}
