var contentToPopulate = '';
var existingAjaxContentWrapperHeight = 100;
var newAjaxContentWrapperHeight = 0;
var activeTabContainerHeight = 0;

var expDays = 3; // number of days the cookie should last
var page = "../newCapitalCardNotice.htm";
var pageX = 403;
var pageY = 313;

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
    return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}

function SetCookie (name, value) {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) {
  var exp = new Date();
  exp.setTime (exp.getTime() - 1);
  var cval = GetCookie (name);
  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function amt(){
  var count = GetCookie('count')
  if(count == null) {
    SetCookie('count','1')
    return 1
  } else {
    var newcount = parseInt(count) + 1;
    DeleteCookie('count')
    SetCookie('count',newcount,exp)
    return count
  }
}

function getCookieVal(offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
  endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function checkCount() {
  var count = GetCookie('count');
  if (count == null) {
    count=1;
    SetCookie('count', count, exp);
    //window.open(page, "", windowprops);
	//alert('first visit today - show div with content');
	//showDialog();
	
  } else {
    count++;
    SetCookie('count', count, exp);
  }
}


// global ajax vars
var acW = $('ajaxContentWrapper').getComputedSize(
{  		
	styles: ['border','padding','margin'] 
}
).totalWidth;


var acH = $('ajaxContentWrapper').getComputedSize(
{  		
	styles: ['border','padding','margin'] 
}
).totalHeight;



window.addEvent('domready', function()
{		
	checkCount();

	var mL = $('logo').getCoordinates();	
	$('menu').setStyle('left',mL.right - 5);
	$('menu').setStyle('display','block');
	
	var navMenu = new MorphList(($('menu').getElement('ul'),'menu'),
	{
		transition: Fx.Transitions.Bounce.easeInOut, 
		duration: 2000,
		onClick: function(ev, item)
		{
			ev.stop();			
			var position = item.id.indexOf('_');			
			var id = item.id.slice(position+1);	
			populateContent(id);
		}
	});
	
	$('homeFooter').addEvent('click', function(e){	
		new Event(e).stop();	
		navMenu.setCurrent($('menu_home'));
  		navMenu.morphTo($('menu_home'));
		existingAjaxContentHeight = $('ajaxContent').getHeight();
		populateContent('home');
	});

	$('aboutusFooter').addEvent('click', function(e){	
		new Event(e).stop();	
		navMenu.setCurrent($('menu_aboutus'));
  		navMenu.morphTo($('menu_aboutus'));
		existingAjaxContentHeight = $('ajaxContent').getHeight();
		populateContent('aboutus');
	});
		
	$('onlinebankingFooter').addEvent('click', function(e){	
		new Event(e).stop();
		navMenu.setCurrent($('menu_onlinebanking'));
  		navMenu.morphTo($('menu_onlinebanking'));
		existingAjaxContentHeight = $('ajaxContent').getHeight();
		populateContent('onlinebanking');
	});
	
	$('productsandservicesFooter').addEvent('click', function(e){	
		new Event(e).stop();
		navMenu.setCurrent($('menu_productsandservices'));
  		navMenu.morphTo($('menu_productsandservices'));
		existingAjaxContentHeight = $('ajaxContent').getHeight();
		populateContent('productsandservices');
	});
	
	$('locationsFooter').addEvent('click', function(e){	
		new Event(e).stop();
		navMenu.setCurrent($('menu_locations'));
  		navMenu.morphTo($('menu_locations'));
		existingAjaxContentHeight = $('ajaxContent').getHeight();
		populateContent('locations');
	});
	
	$('contactsFooter').addEvent('click', function(e){	
		new Event(e).stop();
		navMenu.setCurrent($('menu_contacts'));
  		navMenu.morphTo($('menu_contacts'));
		existingAjaxContentHeight = $('ajaxContent').getHeight();
		populateContent('contacts');
	});
	
	$('fdic_link').addEvent('click', function(e){	
			new Event(e).stop();
			offSite('http://www.fdic.gov');
	});
	
	$('mail_link').addEvent('click', function(e){	
			new Event(e).stop();
			offSite('https://webmail.fcbanker.com/exchange');						
	});

	
	$('privacy_link').addEvent('click', function(e){	
			new Event(e).stop();
			SqueezeBox.fromElement('privacy.htm',
			{				
				size:
				{
					x: 600,
					y: 300
				},
				
				onOpen: function()
				{
					$('privacyNotice_link').addEvent('click', function(e){	
						new Event(e).stop();
						SqueezeBox.fromElement('privacyNotice.htm',
						{				
							size:
							{
								x: 600,
								y: 300
							}
						});			
					});

				}
			});			
	});
	
	
	
	
	
	$('security_link').addEvent('click', function(e){	
			new Event(e).stop();
			SqueezeBox.fromElement('security.htm',
			{				
				size:
				{
					x: 600,
					y: 300
				}
			});			
	});
	
	$('transaction_link').addEvent('click', function(e){	
			new Event(e).stop();
			SqueezeBox.fromElement('transactionAccountGuaranteeProgram.htm',
			{				
				size:
				{
					x: 600,
					y: 300
				}
			});			
	});
	
	
	$('davidhinckle_link').addEvent('click', function(e){	
			new Event(e).stop();
			SqueezeBox.fromElement('davidhinckle.htm',
			{
				size:
				{
					x: 600,
					y: 300
				}
			});			
	});

	SqueezeBox.assign($$('a.boxed'), {parse: 'rel'});
	
	if (window.location.search)
	{	
		var mySearch = window.location.search.replace('?','');	
		
		if (mySearch == 'promotion'){
			//alert('this is a promo');
			
			SqueezeBox.fromElement('promotion.htm',
			{				
				size:
				{
					x: 600,
					y: 300
				}											
			});		
						
			populateContent('home');			
		}
		
		else{
			populateContent(mySearch);	
			navMenu.setCurrent($('menu_'+mySearch+''));
  		navMenu.morphTo($('menu_'+mySearch+''));		
		}
		
		
	}
	

	else{	
		populateContent('home');
	}		
		
}); // end domready

	
populateContent = function(x)
{

	var url = x+'.htm';	
	var req = new Request.HTML(
	{
		method: 'get',  
		url: url,
		update: $('ajaxContentWrapper'),	
		useWaiter: true,
		waiterOptions:
		{										
			layer:
			{ 			
				styles:
				{
					background: '#b0b58c', 
					opacity: 0.7 
				} 				
			}
		},		
		onSuccess: function(html) {						

			if (url == 'home.htm')
			{				
				//doDate();
				doBadge();
				
				if ($('freewiththree_link'))
				{
					$('freewiththree_link').addEvent('click', function(e)
					{	
						new Event(e).stop();
			
						SqueezeBox.fromElement('freewiththree.htm',
						{				
							size:
							{
								x: 600,
								y: 300
							}											
						});							
					});
				}
				
				if ($('newestbranch_link'))
				{
					$('newestbranch_link').addEvent('click', function(e)
					{	
						new Event(e).stop();
			
						SqueezeBox.fromElement('newestbranch2.htm',
						{				
							size:
							{
								x: 600,
								y: 300
							}
														
						});							
					});
				}				

				$('fdicCalulator_link').addEvent('click', function(e){	
					new Event(e).stop();
					offSite("http://www.fdic.gov/EDIE/");
				});


				$('facebook_link').addEvent('click', function(e){	
					new Event(e).stop();
					offSite('http://www.facebook.com/pages/First-Capital-Bank/107208759299234');
				});
					
				$('twitter_link').addEvent('click', function(e){	
					new Event(e).stop();
					offSite('http://twitter.com/fcbanker');
				});
					

				$('mastercard_link').addEvent('click', function(e)
				{	
					new Event(e).stop();		
					SqueezeBox.fromElement('mastercardgift.htm',
					{				
						size:
						{
							x: 600,
							y: 300
						},
						onOpen: function()
						{
							$$('.mastercardgift_link').addEvent('click', function(e)
							{	
								new Event(e).stop();					
								offSite('http://www.cardholder.comdata.com');								
							});
						}
													
					});	
						
				});
				
			};

			if (url == 'aboutus.htm')
			{	
			
			$$('ul.aboutusThumbs a').each(function(element) {
 		       
		        new ReMooz(element, {
		        'origin': 'img',
				'shadow': 'onOpenEnd', // fx is faster because shadow appears after resize animation
				'resizeFactor': 0.7, // resize to maximum 70% of screen size
				'cutOut': true, // don't hide the original
				'opacityResize': 0.6, // opaque resize
				'dragging': false, // disable dragging
				'centered': true // resize 
		        });		 
		    });
			}
			
			
			if (url == 'productsandservices.htm')
			
			{				
				$('mastercard_link').addEvent('click', function(e)
				{	
					new Event(e).stop();
		
					SqueezeBox.fromElement('mastercardgift.htm',
					{				
						size:
						{
							x: 600,
							y: 300
						},
						onOpen: function()
						{
							$$('.mastercardgift_link').addEvent('click', function(e)
							{	
								new Event(e).stop();					
								offSite('http://www.cardholder.comdata.com');								
							});
						}
													
					});	
						
				});
						
				$('expertchecks_link').addEvent('click', function(e)
				{	
					new Event(e).stop();					
					offSite('https://www.expertchecks.com/login.cfm');								
				});
				
				
				$('freewiththree_link').addEvent('click', function(e)
				{	
					new Event(e).stop();
		
					SqueezeBox.fromElement('freewiththree.htm',
					{				
						size:
						{
							x: 600,
							y: 300
						}											
					});							
				});

				
				$('calulator_link').addEvent('click', function(e)
				{	
					new Event(e).stop();
		
					SqueezeBox.fromElement('java/index.htm',
					{
						handler:'iframe',
						size:
						{
							x: 780,
							y: 440
						},
						
						onOpen: function()
						{
						
							if ($('calcTabs')) // init tabs
							{
							
							/*
							var calctabs = new SimpleTabs($('calcTabs'),
								{
									selector: 'h4'
								});
							$$('.tab-container').setStyle('height',370);	
							*/
							
							var calctabs = new SimpleTabs($('calcTabs'),
							{										
								selector: 'h4'		
							});
							
							}
						}
					
					});	
						
				});
																		
			};
			
			if (url == 'locations.htm')
			{
				doATMBadge();
				
				$('atmlocator_link').addEvent('click', function(e)
				{	
					new Event(e).stop();
					offSite('http://visa.via.infonow.net/locator/global/GoAdvanced.do');
				});
			}
			
			if (url == 'contacts.htm')
			{			
				new FormCheck('mailForm', 
				{			
					submitByAjax: true,
					onAjaxRequest: function()
					{      		
					  $('mailResponse').empty().set('html','Sending Message - Please Wait').addClass('processing');
					  $('mailSubmit').setProperty('disabled', 'disabled');
				    },
				
					onAjaxSuccess: function()
					{								
						mailForm.reset();
					},			
		    		ajaxResponseDiv: 'mailResponse'								
				});							
			};
			
			if ($('tabs')) // init tabs
			{
			var tabs = new SimpleTabs($('tabs'),
				{
					selector: 'h4'
				});
			}
			
			if ($('sidebar')) // init sidebar
			{
				var gW = $('gutter').getComputedSize(
					{  		
			  			styles: ['border','padding','margin'] 
					}
				).totalWidth;
		
				var mW = (acW * .75) - gW;
			
				$('main').setStyle('width', mW);
			
				var mW = $('main').getComputedSize(
					{  		
			  			styles: ['border','padding','margin'] 
					}
				).totalWidth;
			
				var cW = acW - mW - gW - 2; // 2 = sum of sidebar border width		
			
				$('sidebar').setStyle('width', cW);
			}
			
			// main only 
			if ($('main') && !$('tabs') && !$('sidebar'))
			{
				$('main').setStyles(
				{
   					borderWidth: '1px',
					width: $('constraint').getWidth() - 2
				});
			}
			
			// main and sidebar only
			if ($('main') && !$('tabs') && $('sidebar'))
			{			
				$('main').setStyle('borderWidth', '1px');
				$('sidebar').setStyle('marginTop',0);

				var mH = $('main').getComputedSize(
					{  		
			  			styles: ['border','padding','margin'] 
					}
				).totalHeight;
	
				var sbH = $('sidebar').getComputedSize(
					{  		
			  			styles: ['border','padding','margin'] 
					}
				).totalHeight;
			
				if (mH >= sbH)
				{
					var eH = mH;	
				}
				else 
				{
					var eH = (sbH);	
				}
				
				$$('div.equalize').setStyle('height',eH);
			}	
			
			// main and tabs only
			if ($('main') && $('tabs') && !$('sidebar'))
			{	
				$('main').setStyle('borderWidth', 0);
				
				var tcW = acW;
				$('tabOneContent').setStyle('width', tcW);
				$$('.tab-menu').setStyle('width', tcW);
				$$('.tab-wrapper').setStyle('width', tcW);	
				$$('.tab-container').setStyle('width', tcW);	
				
				var atH = $('tabOneContent').getScrollHeight();				
				$$('.tab-container').setStyle('height',atH);
			}
			
			// main, tabs and sidebar 
			if ($('main') && $('tabs') && $('sidebar'))
			{	
				var tcW = acW - $('sidebar').getWidth() - $('gutter').getWidth() -2;	
				$('main').setStyle('borderWidth', 0);
				$('sidebar').setStyle('marginTop','28px');						
				$('tabOneContent').setStyle('width', tcW);
				$$('.tab-menu').setStyle('width', tcW);
				$$('.tab-wrapper').setStyle('width', tcW);	
				$$('.tab-container').setStyle('width', tcW);				
				var eH;
				
				if (Browser.Engine.gecko)
				{
				var atH = $('main').getComputedSize(
					{  		
			  			styles: ['border','padding','margin'] 
					}).totalHeight;
				activeTabContainerHeight = 	atH
				}
				else
				{
				var atH = $('tabOneContent').getScrollHeight();
				activeTabContainerHeight = 	atH
				}
				
				
								
				
				$('sidebar').setStyle('marginTop',28);
				var sbH = $('sidebar').getScrollSize().y;
				
				if (atH >= sbH)
				{
					var eH = atH;	
				}
				else 
				{
					var eH = sbH;	
				}				
				$('sidebar').setStyle('height',eH);				
				$$('.tab-container').setStyle('height',eH);
			}
			
			ajaxTips();
			$$('.waitingDiv').dispose();
		},		
		onFailure: function() {
			$('ajaxContentWrapper').set('text', 'The request failed.');			
			//morphAjaxContentHeight();			
		}
	}).send();
};


ajaxTips = function()
{
	//store titles and text
	$$('a.ajaxTips').each(function(element,index) {
		var ajaxTipContent = element.get('title').split('::');
		element.store('tip:title', ajaxTipContent[0]);
		element.store('tip:text', ajaxTipContent[1]);
	});
		
	//create the tooltips
	var tipz2 = new Tips('.ajaxTips',{
		className: 'tipz',
		fixed: false,
		hideDelay: 50,
		showDelay: 50
	});
};
 
offSite = function(url)
{
	SqueezeBox.fromElement('http://www.fcbanker.com/dbq.aspx?template=offsite.htm&uri='+url+'', {
		handler:'iframe',
		size:
		{
			x: 300,
			y: 200
		}
	})
};

doDate = function()
{
	var mydate = new Date()
	var theYear = mydate.getFullYear()
	var day = mydate.getDay()
	var month = mydate.getMonth()
	var daym = mydate.getDate()
	if (daym < 10)
	daym = "0"+daym
	var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
	var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
	$('date').set('text', dayarray[day]+" - "+montharray[month]+" "+daym+", "+theYear);
};

doBadge = function()
{
	var l = ($('loginBadge').getPosition().x -30);
	var t = ($('loginBadge').getPosition().y -40);
	var myloginBadge = new Fx.Morph('loginBadge',
	{
		duration: 'long', transition: Fx.Transitions.Sine.easeOut
	});
	
	myloginBadge.start(
	{
		'left': [-1000, l],
		'top': [-1000, t]
	});			
};

doATMBadge = function()
{
	var t = ($('tabOneContent').getCoordinates().top +30);
	var l = ($('showcase').getCoordinates().right -600);
	var myATMBadge = new Fx.Morph('atmBadge',
	{
		duration: 'long', transition: Fx.Transitions.Sine.easeOut
	});
	
	myATMBadge.start(
	{
		'left': [-1000, l],
		'top': [-1000, t]
	});		
	
};



resetMailForm = function()
{
$('mailResponse').empty().set('html','Please note that all fields are required.').removeClass('processing');
$('mailSubmit').removeProperty('disabled');
}

morphAjaxContentHeight = function()
{
	existingAjaxContentWrapperHeight = $('ajaxContentWrapper').getHeight();
	//var nH  = $('ajaxContent').getHeight() ;
	
	var myEffect = new Fx.Morph('ajaxContentWrapper',
	{
		duration: 'long', 
		transition: Fx.Transitions.Sine.easeOut
	}); 
	myEffect.start(
	{
		'height': [existingAjaxContentWrapperHeight, newAjaxContentWrapperHeight] // morphs the style to the desired height...
	});			
};

morphAjaxContentHeight = function()
{
	var eH = $('ajaxContentWrapper').getHeight();		
	var nH  = $('ajaxContent').getHeight();	
	var myEffect = new Fx.Morph('ajaxContentWrapper',
	{
		duration: 'long', 
		transition: Fx.Transitions.Sine.easeOut
	}); 
	myEffect.start(
	{
		'height': [eH, nH] // morphs the style to the desired height...
	});	
			
};


equalizeTabs = function()
{
	var eH;
	
	var atH = activeTabContainerHeight;
	
	$('sidebar').setStyle('marginTop',28);
	
	var sbH = $('sidebar').getScrollSize().y;
	
	if (atH >= sbH)
	{
		var eH = atH;	
	}
	else 
	{
		var eH = sbH;	
	}
	
	$('sidebar').setStyle('height',eH);
	$$('.tab-container').setStyle('height',eH);
	//$$('.ajaxContent').setStyle('height',newAjaxWrapperContentHeight);
}

showDialog = function()
{
	SqueezeBox.fromElement(page,
	{				
		size:
		{
			x: pageX,
			y: pageY
		},
		
		onOpen: function()
		{
			if ($('newCapitalCard_link'))
			{
				$('newCapitalCard_link').addEvent('click', function(e)
				{	
					new Event(e).stop();
			
					SqueezeBox.fromElement('capitalcard.htm',
					{				
						size:
						{
							x: 600,
							y: 300
						}											
					});							
				});
			}
		}									
	});
						
}

setFocus = function(frameName, elementId)
{
      var domElement = document.frames[frameName].document.getElementById(elementId);
      if (domElement) {
      domElement.focus();
      }
}

