﻿/*jslint white: true, browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true, strict: true */
/*global $: false, window: false */

(function () {
	"use strict";
	
	window.MIDCO = {
		init: function () {
			//this.handleNavigation();
			this.displayHints();
			this.displayShowHideRegions();
			this.displayShare();
			this.displayBottomBar();
		},
		
	// Adds handler to navigation to popup franchise form
		franchisePage: '/classlibrary/com/midcofranchise/franchise.cfm',
		handleNavigation: function () {
			var that = this, bundle = $('#bb'), hasSession = '';
			
			if (bundle.length) {
				hasSession = bundle.parent().attr('href').split('?').length > 1;

				$('#dc,#mb,#dp')
					.parent()
					.click(function (e) {
						var popup = $('#franchisepopup'), target = $(this).attr('href');
						
						if (!hasSession) {
							e.preventDefault();
							if (!popup.length) {
								$('<div />').attr('id', 'franchisepopup').appendTo('body');
							}
							$.get(that.franchisePage, {}, function (d, s) {
								var popup = $('#franchisepopup');
								
								if (s === 'success') {
									popup
										.html(d)
										.find('input[name=address]')
										.focus();
									
									popup
										.find('form')
										.attr('action', that.franchisePage + '?target=' + target);
								} else {
									popup
										.html('<p>There was an error.</p>');
								}
							}, 'html');
						}
					});
			}
		},
		
	// Make certain labels "hints"
		displayHints: function () {
			$("label.hint").each(function () {
				var text = $(this).text(), itemtarget = $(this).attr("for"), item = '', value = '';
				if (itemtarget.length > 0) {
					item = $("[name=" + itemtarget + "]")
						.focus(function () {
							if ($(this).val() === text) {
								$(this).val("");
							}
						})
						.blur(function () {
							if ($(this).val() === "") {
								$(this).val(text);
							}
						});
					value = item.val();
					if (value === "") {
						item.val(text);
					}
				}
			}).hide();
		},
		
		
	// Displays show-hide regions
		displayShowHideRegions: function () {
			window.createShowHideRegions('showhideregion');
			window.createShowHideRegions('showhideregionplus');
		},
		
		
	// Display share button methods
		displayShare: function () {
			$(".twitter-share-button").click(function (ev) {
				var href = $(this).attr('href');
				ev.preventDefault();
				window.open(href, 'ShareToolbox', 'height=300,width=500,status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes');
			});
		},
		
		
	// Display bottombar handlers
		displayBottomBar: function () {
			$('#bottombar_share .details ul')
				.children('.print')
					.click(function (ev) {
						ev.preventDefault();
						window.print();
					}).end()
				.children('.favorites')
					.click(function (ev) {
						var title = document.title, url = document.location;
						ev.preventDefault();
						if (window.sidebar) {
							window.sidebar.addPanel(title, url, "");
						} else if (window.external) {
							window.external.AddFavorite(url, title);
						} else if (window.opera && window.print) {
							return true;
						}
					}).end()
				.children('.more')
					.click(function (ev) {
						ev.preventDefault();
						$('#bottombar_share a.addthis_button_expanded').click();
					});
		}
	};
	
	$(document).ready(function () {
		window.MIDCO.init();
	});
}());

//Our Team expand JS
$(function(){
	
	if ($('.information #ourteam').length > 0) {
		// Convert HTML
		$('.information #ourteam td > br').remove();
		$('.information #ourteam td').has('img')
			.wrapInner('<div class="box" />')
		.find('.box')
			.wrapInner('<div class="inner" />')
			.append('<div class="box-bottom" />')
		.find('.inner img')
			.after('<a href="#toggle" class="collapsible-btn"><span>toggle</span></a>')
			.wrap('<a href="#profile" class="profile" />')
			.end()
		.find('.bio').addClass('collapsible-content');
	
		
		// Collapsible Buttons
		$('a.collapsible-btn').click(function(){
			if( $(this).hasClass('expanded') ) {
				$(this).removeClass('expanded').siblings('.collapsible-content').slideUp('normal');
			}else{
				
				var $old = $('a.collapsible-btn.expanded');
				$old.removeClass('expanded').siblings('.collapsible-content').slideUp('normal');
				
				$(this).addClass('expanded').siblings('.collapsible-content').slideDown('normal');
			}
			return false;
		});
		
		$('div.collapsible-content').each(function(){
			$(this).css({height: $(this).css('height')}).hide();
		});	
		
		$('a.profile, a.title, span.name').click(function(){
			$(this).siblings('.collapsible-btn').trigger('click');
			return false;
		});
	}
});

