/*
-----------------------------------------------
University at Buffalo
UB Believers
Global Javascript
----------------------------------------------- */

$(function() {

	var global = {

		scope: {
			page: $('#page'),
			content: $('#content'),
			sidebar: $('#sidebar'),
			main: $('#main'),
			is_home: $('body').is('#home'),
			is_wide: $('body').is('#wide')
		},

		attachCorners : function() {
			$s.page.find('.corner_container').each(function() {$(this).markupCorners()});
		},

		colorizeListStyles : function(e) {
			e.each(function() {
				var content = $(this).html();
				content = '<div class="color_wrap">'+content+'</div>';
				$(this).empty().html(content);
			});
		},

		buildBreadcrumbs : function() {
			var bc = $('.breadcrumbs');
			var bc_cur = $('<li class="current_page"><a href="#"></a></li>');
			var title = null;
			if ($('#main_content h2:first').length) {
				title = $('#main_content h2:first').text();
			} else if ($('#ga-smpHeader b:first').length) {
				title = $('#ga-smpHeader b:first').text();
			}
			if (title && title.length) {
				bc_cur
					.appendTo(bc)
					.find('a').text(title);
			}
		},

		stripeTables : function() {
			$("tbody tr:nth-child(even)").addClass("even");
		},

		catchPopups : function(e) {
			e.click(function() {window.open($(this).attr('href'),'_blank','toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=800,height=660'); return false;});
		},

		labelLists : function(e) {
			e.find('li:first').addClass('first');
			e.find('li:last').addClass('last');
		},

		general : function() {
			// warning for member sign-up page
			attachEmailWarning();

			// members login/logout box
			var cookieName = $.cookie('loginName');
			if(cookieName) {
				setupLoginModule(1, cookieName);
				}
			else {
				setupLoginModule(0);
				}
			lookupMemberInfo($('link[@rel=getactive_base]').attr('href'));
		},

		init : function() {
			global.attachCorners();
			global.colorizeListStyles($('#main_content li').not($('.ga-tab, .getbanner li')));
			global.stripeTables();
			global.catchPopups($('a[rel=popup]'));
			global.labelLists($('.featured_events'));
			cleanupProfileEditor();

			if ($s.is_home) {
				$('.ga-homeLower table:first').remove();
				$s.main.find('.module').equalizeCols();
				// $('.ga-homeLower h1').each(function() {
				//     if ($(this).text().match("Previous Advocacy Campaigns")) {
				//         $(this).next('table').hide();
				//     }
				// });
			} else {
				global.buildBreadcrumbs();
			}

			global.general();
			$('.login .content').touchHeight();
		}

	}

	var $s = global.scope;
	global.init();

});

$(document).bind('memberInfoReady', function(e, data) {
	if(data.logged_in) {
		$.cookie('loginName', data.first_name);
		setupLoginModule(1, data.first_name);
		}
	else {
		$.cookie('loginName', null);
		setupLoginModule(0);
		}
	});

function setupLoginModule(loggedIn, name) {
	if(loggedIn) {
		$('.member_options_name').text(name);
		$('#member_options_login').hide();
		$('#member_options_logout').show();
		}
	else {
		$('#member_options_logout').hide();
		$('#member_options_login').show();
		}
	}

function lookupMemberInfo(base) {
	var url = base + '/smp.html?show_subs=profile&' + Math.random();
	$.get(url, {}, function(data) {
		var d = $('<div id="memberInfoBlock"></div>').css('display', 'none').appendTo('body');
		d = $('#memberInfoBlock');
		var stripped = data.match('<body\\b[\\S\\s]*</body>', 'i')[0];
		d.append(stripped);

		var info = {
			logged_in : $('#ga-smpBody a[@href$=logout.html]', d).length,
			email : safe_get_field('email'),
			first_name : safe_get_field('first_names'),
			last_name : safe_get_field('last_name')
			};
		d.remove();
		$(document).trigger('memberInfoReady', [info]);

		function safe_get_field(name) {
			var f = $('#ga-smpBody input[@name='+name+']', d);
			return f.length ? f[0].value : "";
			}
		});
	}

function cleanupProfileEditor() {
	var main_table = $('#main_content #ga-smpBody .ga-tab-content>table');
	if ($('#edit_my_profile_Frm').length) {
		$('#main_content #ga-smpBody .ga-tab-content table table td.table_big').each(function() {
			var text = $(this).text();
			text = $.trim(text);
			$(this).empty();
			$(this).text(text);
			});
		main_table.addClass('main_table');
		main_table.children('tbody').children('tr').children('td:last-child').addClass('column_two');
	}

	main_table
		.find('td > strong')
			.children('img')
			.remove()
		.end()
		.css({
			'display':'block',
			'width':'100%'
		});
	// convert image buttons to regular ones
	// $('#main_column #ga-smpBody').find('input[@type=image]').each(function() {
	// 	$(this)
	// 		.attr('type','submit')
	// 		.attr('value',$(this).attr('alt'));
	// });
	}

function attachEmailWarning() {
	var field = $('.ga-signUp form input[@name=email]');
	if(!field.length) return;
	var func = function() {
		if (field[0].value.match(/@\S*buffalo\.edu/i)) {
			showEmailWarning();
			}
		else {
			hideEmailWarning();
			}
		};
	field.keyup(func).blur(func).change(func);
	}

function showEmailWarning() {
	if($('#email_warning_message').length) return;
	$('.ga-signUp form > table tr').eq(2).after('<tr id="email_warning_message"><td colspan="2"><div class="warning">Instead of using your "buffalo.edu" address, please consider using your personal email address. This will have more impact on elected officials.</div></td></tr>');
	}

function hideEmailWarning() {
	$('#email_warning_message').remove();
	}


jQuery.fn.extend({
	markupCorners : function() {
		var tl = $('<div class="corner top_left"><!-- --></div>');
		var tr = $('<div class="corner top_right"><!-- --></div>');
		var bl = $('<div class="corner bottom_left"><!-- --></div>');
		var br = $('<div class="corner bottom_right"><!-- --></div>');

		if ($(this).hasClass('tl')) {$(this).append(tl)}
		if ($(this).hasClass('tr')) {$(this).append(tr)}
		if ($(this).hasClass('bl')) {$(this).append(bl)}
		if ($(this).hasClass('br')) {$(this).append(br)}
		if ($(this).hasClass('all')) {$(this).append(tl).append(tr).append(bl).append(br)}
	},
	minHeight : function(height) {
		if ($.browser.msie && (typeof window.XMLHttpRequest == "undefined")) {$(this).height(height);}
		else {$(this).css('min-height', height)}
	},
	touchHeight : function() {
		var $this = this;
		$this.minHeight("1%");
	},
	equalizeCols : function() {
		var max_height = 1;
		this.each(function() {
			if ($(this).height() > max_height) {max_height = $(this).height();}
		});
		this.each(function() {
			$(this).minHeight(max_height);
		});
	}
});
