/* SML Insurance */
/* Javascript Functions
----------------------------------------------------------------------------- */


	$(document).ready(function() {
	
		// Markup changes
		$('body.consultants div.display.section div.tags a').text('< Back to Consultant Listing');
	
	});


	$(window).load(function() {


/* HEADER
----------------------------------------------------------------------------- */


	function loadSlideshow() {

		$('#slideshow').cycle({
		
			fx:      'fade',
			speed:   4000
		
		});

	}

	$.get('/feeds/banners', function(data) {
	
		$(data).find('unit').each(function() {
				
			if ($(this).find('name').text() == "header") {
			
				var header = '';
				var first = '';
				
				$(this).find('banner').each(function() {
									
					var caption = $(this).children('description').text();
					var image = $(this).children('image').text();
										
					if (tag.match('financial') && caption.match('Nathan')) {
						
						first += '<img src="' + image + '" alt="' + caption + '" class="first" />';
						
					} else if (tag.match('connect') && caption.match('Leigh')) {

						first += '<img src="' + image + '" alt="' + caption + '" class="first" />';
					
					} else if (tag.match('life') && caption.match('Daphne')) {

						first += '<img src="' + image + '" alt="' + caption + '" class="first" />';

					} else if (tag.match('executive') && caption.match('Shawn')) {

						first += '<img src="' + image + '" alt="' + caption + '" class="first" />';

					} else if (tag.match('employee') && caption.match('Victoria')) {

						first += '<img src="' + image + '" alt="' + caption + '" class="first" />';

					} else if (tag.match('estate') && caption.match('Daphne')) {

						first += '<img src="' + image + '" alt="' + caption + '" class="first" />';
									
					} else if (section == 'contactus' && caption.match('Glynda')) {

						first += '<img src="' + image + '" alt="' + caption + '" class="first" />';
						
					} else if (!tag.match('consultants') && !tag.match('life')) {

						header += '<img src="' + image + '" alt="' + caption + '" />';
						
					}
					
					if (tag.match('life') && caption.match('Cynthia')) {

						first += '<img src="' + image + '" alt="' + caption + '" class="first" />';

					}	
					
					if (tag.match('connect') && !caption.match('Leigh')) {

						header += '<img src="' + image + '" alt="' + caption + '" />';
		
					} else if (tag.match('life') && !caption.match('Daphne') && !caption.match('Cynthia')) {

						header += '<img src="' + image + '" alt="' + caption + '" />';
					
					}
					
					// For Consultants Section
					
					if (tag.match('consultants') && caption.match('Victoria') && (!topic.match('daphne') && !topic.match('nathan'))) {

						first += '<img src="' + image + '" alt="' + caption + '" class="first" />';

					}
			
					if (topic.match('daphne') && caption.match('Daphne')) {

						first += '<img src="' + image + '" alt="' + caption + '" class="first" />';
						
					}
					
					if (topic.match('nathan') && caption.match('Nathan')) {

						first += '<img src="' + image + '" alt="' + caption + '" class="first" />';
						
					}
					
														
				});			

				$(first + header).appendTo('#slideshow');
						
			}
		
		});

		loadSlideshow();

	});
	
	
	$('ul#navigation li').hover(function() {
	
		$(this).addClass('hover');
	
	}, function() {

		$(this).removeClass('hover');	
	
	});
	
	



/* DISCLAIMER OVERLAYS FOR THIRD-PARTY LINKS
----------------------------------------------------------------------------- */


	// Fades out overlay upon proceed button action
	$('a.proceed').click(function() {

		var link = $(this).attr('href');

		$('div#dimmer').fadeOut('slow');
		$('div#overlay').hide();

		window.open(link);
		return false;

	});

	// Fades out overlay upon cancel button action
	$('a.cancel').click(function() {

		$('div#dimmer').fadeOut('slow');
		$('div#overlay').hide();
		
		return false;

	});

	function adjustDimmer() {

		$('#dimmer').css('height', $(window).height());
	
	}

	function applyDisclaimers() {
		
		$("a[href*='http']").not('#forgotpassword').each(function() {
		
			$(this).click(function() {
	
				var link = $(this).attr('href');
				
				$('div#dimmer').fadeIn('slow');
				$('div#overlay').fadeIn('slow');
				$('a.proceed').attr('href',link);

				adjustDimmer();
				$(window).resize(adjustDimmer());

				if (navigator.userAgent.match('MSIE 6')) {

					window.scrollTo(0,0);

				}

				return false;
		
			});
				
		});

	}
	
	applyDisclaimers();
	

/* HOME PAGE
----------------------------------------------------------------------------- */


	function validateForm() {

		if ($(this).val().length == 0 || $(this).val() == "Required") {
		
			$(this).parent().addClass('error');
			$(this).not('input[type=password]').val('Required');
			
			function noteRequired() {

				if ($(this).val() == "Required") {
					$(this).val('');
				} else if ($(this).val().length == 0) {
					$(this).val('Required');
				}
			
			}
			
			$(this).not('input[type=password]').blur(noteRequired);
			$(this).not('input[type=password]').focus(noteRequired);

		} else {
		
			$(this).parent().removeClass('error');
		
		}
	
	}


/* Brochure Form */


	$('form[name=brochure]').submit(function() {
	
		$('#brochure input.required').each(validateForm);
		
		if ($('#brochure label.error').length > 0) {
		
			return false;
	
		}
	
	});


/* Sign In Form */


	$('form[name=signin]').submit(function() {

		$('#signin input').each(validateForm);
		
		if ($('#signin label.error').length > 0) {
		
			return false;
	
		}		
	
	});

	$('a#forgotpassword').click(function() {
	
		var link = $(this).attr('href');
		
		window.open(link, 'password','status=0,toolbar=0,resizable=0,scrollbars=0,location=0,toolbar=0,menubar=0,width=300,height=200');	
		return false;
		
	});


/* WHAT WE DO
----------------------------------------------------------------------------- */





// Service Panel Focusing


	$('div.panel.ajax').each(function(index) {
	
		if (index != '0') {
		
			$(this).addClass('unfocus');
		
		}

		$(this).mouseover(function() {

			$('div.panel:not(this)').addClass('unfocus');
			$(this).removeClass('unfocus');
			$(this).fadeTo(2000, 1);
			
		});

		$(this).find('ul.headlines li:first-child a').addClass('selected');

	});


// Service Detail Tabs


	$('div.panel.ajax').each(function() {

		var $panel = $(this);

		$(this).find('ul.headlines').addClass('tabs').find('a.comments').remove();

		$(this).find('ul.tabs a').click(function() {

			var $insertion = $panel.children('div.headlines');
			var link = $(this).attr('href').replace('whatwedo','service');
			var topic = link.split('/');
			var topic = topic[3];

			$('<div class="' + topic + '"></div>').insertAfter($insertion).load(link, '', function() {
			
				$insertion.next().next().remove()
				Cufon.refresh();
				applyDisclaimers();
				
			});
			
			$(this).parent().siblings('li').children('a').removeClass('selected');
			$(this).addClass('selected');
			
			return false;
		
		});

	});


/* WHAT'S NEW
----------------------------------------------------------------------------- */


	if (page == "whatsnew") {

		// Load Feed

		function addArchive(year) {
		
			$('ul.headlines').append('<li><a href="#' + year + '">' + year + '</a></li>');
			$('ul.stories').append('<li id="' + year + '"><div class="story"><h4>' + year + '</h4><ul></ul></div></li>')
		
		}

		$.get('/feeds/external/sml-whatsnew-atom.xml', function(data) {

			$(data).find('entry').each(function() {

				var date = $(this).find('updated').text().slice(0,10);
				var title = $(this).find('title').text();
				var link = $(this).find('link').text();
				var date_string = Date.parse(date);
				date = date_string.toString("MMM d, yyyy");
				var year = date_string.toString("yyyy");

				if ($('#' + year).length == 0) {
			
					addArchive(year);
			
				}

				// Render Headlines
		
				var $headline = '<li>';
				$headline += '<strong>' + date + '</strong>';
				$headline += '<a href="' + link + '">' + title + '</a>';
				$headline += '</li>';

				$('#' + year + ' div ul').append($headline);

			});

			$('ul.tabs > li:first-child a').addClass('selected');
			$('ul.stories > li:not(:first-child)').addClass('hidden');

			Cufon.refresh();
	
			$('div.panel').each(function() {
		
				var $panel = $(this);
	
				$(this).find('ul.tabs a').click(function() {

					var year = $(this).attr('href').split('#');
					var year = year[1];
	
					$(this).parent().siblings('li').children('a').removeClass('selected');
					$(this).addClass('selected');
					$('ul.stories > li').addClass('hidden');
					$('ul.stories > li#' + year).removeClass('hidden');
					
					return false;
				
				});
		
			});

		});

	}


/* CONTACT US
----------------------------------------------------------------------------- */


	if (page.match('directions')) {
	
		var map = new GMap2(document.getElementById("map"));

		if ($('#map.oakland').length > 0) {

			map.setCenter(new GLatLng(37.809627, -122.264635), 15);
			var office = new GLatLng(37.809627, -122.264635);
		
		} else if ($('#map.santarosa').length > 0) {

			map.setCenter(new GLatLng(38.476295,-122.724967), 15);
			var office = new GLatLng(38.476295,-122.724967);

		} else if ($('#map.lafayette').length > 0) {

			map.setCenter(new GLatLng(37.890172, -122.129785), 15);
			var office = new GLatLng(37.890172, -122.129785);

		}		

		map.addOverlay(new GMarker(office));
		map.addControl(new GLargeMapControl());

	}


/* END
----------------------------------------------------------------------------- */


	cufon();
	
	});

