Event.observe(window, 'resize', initPage);

var contextUrl;
function init(context) {
	contextUrl = context;

  // In IE6, adjust height of floating panel
  if (Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE") + 5)) == 6) {
    document.observe('dom:loaded', initFloatingPanel);
  }
}

function initPage() {
	initTabs();
	if ($('submenu') != null) {
		initSubmenu();
	}
}

function initFloatingPanel() {
  $$('.floating-panel').each(function(fp) {
    if (fp.getHeight() % 2 ==1) {
      fp.setStyle({ height: (fp.getHeight()+1) + 'px' });
    }
    if (fp.getWidth() % 2 ==1) {
      fp.setStyle({ width: (fp.getWidth()+1) + 'px' });
    }
  });
  $$('.panel-img-wrapper').each(function(fp) {
    if (fp.getHeight() % 2 ==1) {
      fp.setStyle({ height: (fp.getHeight()+1) + 'px' });
    }
    if (fp.getWidth() % 2 ==1) {
      fp.setStyle({ width: (fp.getWidth()+1) + 'px' });
    }
  });
}

function initPageWithSubmenu() {
	initTabs( function() {
		initSubmenu();
	});
}

var selectedTab = null;
function initTabs(callback) {
	var total = 0;
	$('tabMenuWrapper').select('li').each( function(li, index) {
		if (li.hasClassName('selected')) {
			selectedTab = li;
		}

		var w = li.getWidth();
		var m = parseInt(li.getStyle('margin-right'));
		total = total + w + m;
	});
	$('tabMenuWrapper').setStyle( {
		width : total + 'px'
	});

	if (Object.isFunction(callback)) {
		callback.call();
	}
}

function initSubmenu() {
	if (selectedTab != null) {
		var tabLeft = selectedTab.cumulativeOffset().left;
		var tabWidth = parseInt(selectedTab.getWidth()) + 10;
		var tabPaddingLeft = parseInt(selectedTab.getStyle('padding-left'));

		var divider = $('submenuDivide');
		var leftPos = tabLeft - tabPaddingLeft;
		divider.setStyle( {
			left : leftPos + 'px',
			width : tabWidth + 'px'
		}).show();

		var submenu = $('submenu');
		var submenuInner = $('submenuInner');
		var submenuLeft = '0px';
		var dividerWidth = divider.getWidth();
		var submenuWidth = submenu.getWidth();
		var adjustedSubmenuWidth = null;

		var edgeDifference = 9;
		if (submenuWidth > dividerWidth + 9) {
			edgeDifference = (submenuWidth - dividerWidth) / 2;
		} else {
			adjustedSubmenuWidth = (dividerWidth + 18);
		}

		var tabMenuLeft = $('tabMenuWrapper').cumulativeOffset().left;
		var left = leftPos - edgeDifference;

		if (left < (tabMenuLeft - 15)) {
			left = (tabMenuLeft - 15);
		}
		left = (left + 3);
		
		if (submenu.getHeight() % 2 == 1) {
			submenu.setStyle({ height: (submenu.getHeight()+1) + 'px' });
		}
		
		if (adjustedSubmenuWidth) {
		  if (adjustedSubmenuWidth > 795) {
			  adjustedSubmenuWidth = 785;
		  }
			submenuInner.setStyle({ width : adjustedSubmenuWidth + 'px' });
			submenu.setStyle( {
				width : adjustedSubmenuWidth + 'px',
				textAlign : 'center'
			});
		}
		
		if (submenu.getWidth() % 2 == 1) {
			submenu.setStyle({ width: (submenu.getWidth()+1) + 'px' });			
			submenuInner.setStyle({ width: (submenuInner.getWidth()+1) + 'px' });
		}
		
		submenu.setStyle({
			left : left+'px'
		}).show();

		// if too far right, reposition - can only be calculated after menu is
		// shown (bit naf)
		var submenuRight = (left + submenu.getWidth());
		var mainWrapperLeft = $('mainWrapper').cumulativeOffset().left;
		var mainWrapperRight = mainWrapperLeft + 995;
		if (submenuRight > mainWrapperRight) {
			submenuInner.setStyle({ width : (adjustedSubmenuWidth) + 'px' });
			submenu.setStyle( {
				left : (mainWrapperRight - submenu.getWidth() - 2) + 'px',
				width : (adjustedSubmenuWidth) + 'px'
			});
		}

	}
}

var branchPk = null;
var branchname = null;
function selectFinderBranch() {
	branchPk = $F('finderSelect');
	$$('#finderSelect option').each( function(op) {
		if (op.value == branchPk) {
			branchname = op.text;
		}
	});
	viewBranch();
}
function selectDetailBranch() {
	branchPk = $F('detailSelect')
	$$('#detailSelect option').each( function(op) {
		if (op.value == branchPk) {
			branchname = op.text;
		}
	});
	viewBranch();
}
function viewBranch() {		
	if (!branchPk.blank() && branchPk != 'choose an office') {
		location = contextUrl + '/page/branchDetailPage/'
				+ branchPk + '/' + branchname + '/menu/findYourOffice';
	} else {
		return false;
	}
}
