var pane_status = 'closed';
var first_load = 'true';

$( document ).ready( function()
{	
	// cityOverlay();
	selectCurrent();
	fontResizer();
	// styleFooter();
	styleBreadcrumbs();
	//lineUpTitle();
	getFullDate();
	applyDisplayFont();
	styleBlogCat();
	// isSubEmpty();
	
	// google maps
	$( '.map' ).each( function( i ){ drawmap( $( this ), i ); });	
	
	var c = readCookie( 'collins_font_size' );
	( c ) ? $( 'div.inner-list-long' ).css( { 'font-size' : c + 'px' } ) : c = createCookie( 'collins_font_size', '12', 7 );
});

function doSubmit()
{
	/* do search */
}

/* select city functionality */

function cityOverlay()
{
	$( 'div#header' ).append( "<div id='city-select-pane'></div>" );
	$( 'div#header div#city-select-pane' ).hide();
	
	$( 'a#city-select' ).bind( 'click', function( e )
	{
		if( pane_status == 'closed' )
		{
			openPane();
		}
		
		else
		{
			closePane();
		}
		
		this.blur();
	});
}

function closePane()
{
	$( 'div#header div#city-select-pane' ).fadeOut( 'slow' );
	pane_status = 'closed';
}

function openPane()
{
	if( first_load == 'true' )
	{
		$( 'div#header div#city-select-pane' ).fadeIn( 'slow' );
		$( 'div#header div#city-select-pane' ).html( "<img style='padding: 10px' src='/img/loading.gif' />" );
			
		$.ajax({
			url: 'http://www.collinsbarrowottawa.com/index.php/city_selection/',
			success: function( html )
			{
				$( 'div#header div#city-select-pane' ).html( html );
					
				$( 'div#header div#city-select-pane a.close' ).bind( 'click', function( e )
				{
					closePane();
					this.blur();
				});
					
				$( 'div#header div#city-select-pane div#city-options ul li a' ).bind( 'click', function( e )
				{
					selectCity( $( this ).html() );
					
					closePane();
					this.blur();
				});
			}});
		
		first_load = 'false';
	}
	
	else
	{
		$( 'div#header div#city-select-pane' ).fadeIn( 'slow' );
	}
	
	pane_status = 'open';
}

function selectCity( str )
{	
	$( $( 'div#header div#info span strong' )[ 0 ] ).html( str );
}

/* create current states for main nav and sub nav */

function selectCurrent()
{
	var page = document.location.pathname;
	var page_split = page.split( '/' )[ 2 ]; /* this will change if the index.php is removed from the url schema */
	
	// console.log( page_split );

	// style the main navigation

	$( 'div#container div#header div#nav ul li a' ).each(								   
	function( i )
	{
		var href_link = $( this ).attr( 'href' );
		var href_link_split = href_link.split( '/' )[ 4 ];
		
		if( page_split == href_link_split )
		{
			$( this ).contents().wrap( "<span class='current'></span>" );
		}
	});
	
	// style the sub menu
	
	var sub_page = page.split( '/' )[ 3 ];
	
	// console.log( sub_page );
	// added for CCH Content
	if( page.split( '/' )[ 4 ] && sub_page == 'tax-forms' )
	{
		sub_page = page.split( '/' )[ 4 ];
	};
	
	// console.log( sub_page );
	
	if( sub_page != '' && sub_page != undefined )
	{
		$( 'div#container div.inner-list ul.inside a' ).each(
		function( i )
		{
			var href_link = $( this ).attr( 'href' );
			var href_link_split = href_link.split( '/' );
			
			for( var i = 0; i < href_link_split.length; i++ )
			{
				if( sub_page == href_link_split[ i ] )
				{
					href_link = href_link_split[ i ];
				}
			};
			
			// console.log( href_link );

			// doc title and page title fixes. Nov. 9, 2009
			var in_url = window.location.href;
			// upload-your-data-file hard coded in for thank-you page. Jul. 13, 2010
			if( ( href_link == 'http://www.collinsbarrowottawa.com/index.php/resource-centre/upload-your-data-file' && in_url.search('resource-centre/thank-you') != -1 ) || sub_page == href_link || ( in_url.search('events/entry') != -1 && href_link.search('events/calendar') == -1 ) || ( in_url.search('news/entry') != -1 && href_link.search('/news/newsletter/') != -1 && in_url.search('cid=17') == -1 ) || ( in_url.search('news/category/') != -1 && href_link.search('/news/newsletter/') != -1 ) || ( href_link.search('/news/whats-new') != -1 && in_url.search('cid=17') != -1 ) )
			{
				// $( this ).contents().wrap( "<span style='color: #003366; font-weight: bold;'></span>" );
				$( this ).css( { 'color' : '#003366', 'font-weight' : 'bold' } );
				
				// doc title and page title fixes. Nov. 9, 2009
				// doc title fixes. Jul. 14, 2010
				//document.title = 'Collins Barrow Ottawa LLP - ' + $( this ).text();
				$( 'table#inside-left h2' ).text( $( this ).text() );
				
			}
		});
	}
	
	else /* added to highlight Overview */
	{
		$( 'div#container div.inner-list ul.inside a:first' ).css( { 'color' : '#003366', 'font-weight' : 'bold' } );
		// page title fix. Nov. 9, 2009
		//var section = document.title;
		//section = section.replace('Collins Barrow Ottawa LLP - ','');
		
		// doc title fixes. Jul. 14, 2010
		$( 'table#inside-left h2' ).text( $( 'div#container div#header div#nav ul li a span.current' ).text() );
	}
	
	
	// add current class to current breadcrumb
	
	$( 'div#container div#main div.breadcrumb a' ).each(
	function( i )
	{		
		if( i == $( 'div#container div#main div.breadcrumb a' ).length - 1 )
		{
			$( this ).addClass( 'current' );
			
			// breadcrumb fix. Nov. 9, 2009
			if (sub_page != 'category')
			{
				$( this ).text( $( 'table#inside-left h2' ).text() );
			}
		}
	});
}

/* rewrite certain areas with sIFR flash fonts */

function applyDisplayFont()
{	
	if( typeof sIFR == "function" )
	{
    	sIFR.replaceElement( 'table#inside-left h2, div#homepage-tabs h2', named( { sFlashSrc: "/img/helvetica-light.swf", sColor: "#FFFFFF", sWmode: "transparent" } ) );
		sIFR.replaceElement( 'div#homepage-img h3', named( { sFlashSrc: "/img/helvetica-light.swf", sColor: "#2F457D", sWmode: "transparent" } ) );
		// sIFR.replaceElement( 'div#right h2.right-title', named( { sFlashSrc: "/img/helvetica-medium.swf", sColor: "#8B8B8B" } ) ); */
		// sIFR.replaceElement( 'div.inner-list-long h2', named( { sFlashSrc: "/img/helvetica-light.swf", sColor: "#8e0600", sWmode: "transparent" } ) );
	};
}

/* add font resizer functionality with cookie */

function fontResizer()
{
	$( 'a.font-increase' ).click( function( e )
	{
		e.preventDefault();
		var size = eval( $( 'div.inner-list-long' ).css( 'font-size' ).split( 'px' )[0] );
		size++;
		createCookie( 'collins_font_size', size, 7 );
		$( 'div.inner-list-long' ).css( { 'font-size' : size + 'px' } );
		
		$( this ).blur();
	});
	
	$( 'a.font-decrease' ).click( function( e )
	{
		e.preventDefault();
		var size = eval( $( 'div.inner-list-long' ).css( 'font-size' ).split( 'px' )[0] );
		size--;
		createCookie( 'collins_font_size', size, 7 );
		$( 'div.inner-list-long' ).css( { 'font-size' : size + 'px' } );
		
		$( this ).blur();
	});
	
	/* reset was removed
	$( 'a.font-resize-reset' ).click( function( e )
	{
		$( 'div.inner-list-long' ).css( { 'font-size' : '12px' } );
		createCookie( 'collins_font_size', '12', 7 );
		$( this ).blur();
	});
	*/
	
	$( 'a.email' ).click( function( e ) { $( this ).blur(); });
	$( 'a.print' ).click( function( e ) { $( this ).blur(); });
}

function createCookie( name, value, days )
{
	if( days )
	{
		var date = new Date();
		date.setTime( date.getTime() + ( days * 24 * 60 * 60 * 1000 ) );
		var expires = "; expires=" + date.toGMTString();
	}
	
	else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie( name )
{
	var nameEQ = name + "=";
	var ca = document.cookie.split( ';' );
	for ( var i = 0; i < ca.length; i++ )
	{
		var c = ca[i];
		while ( c.charAt( 0 ) == ' ' ) c = c.substring( 1, c.length );
		if ( c.indexOf( nameEQ ) == 0 ) return c.substring( nameEQ.length, c.length );
	}
	return null;
}

/* function to dynamically style footer ( make it pretty ) */

function styleFooter()
{	
	$( 'div#container div#footer div.column table tr td ul li' ).each(
	function()
	{
		if( $( this ).next()[ 0 ] != undefined )
		{
			if( $( this ).next()[ 0 ].offsetTop == this.offsetTop ) { $( this ).append( '|' ) };
		};
	});
}

/* function to split up the breadcrumbs and capitalize the first letter */

function styleBreadcrumbs()
{
	$( 'div#container div#main div.breadcrumb a' ).each(
	function(count)
	{
		var str = $( this ).text();
		var str_array = str.split( '-' );
		
		if (count < 2) // only style segment_1 and segment_2, segment_3 now uses side nav text
		{
			for( var i = 0; i < str_array.length; i++ )
			{
				// exceptions
				if( str_array[ i ] != 'and'
				&& str_array[ i ] != 'Home'
				&& str_array[ i ] != 'of'
				&& str_array[ i ] != 'the' )
				{
					val = str_array[ i ];
					val = val.replace( /\b([a-z]+)\b/ig, function( w )
					{
						return w.charAt( 0 ).toUpperCase()+w.substring( 1 );
					});
							
					str_array[ i ] = val;
				}
			};
			
			$( this ).text( str_array.join( ' ' ) );
		}
	});
}

/* function to line up baseline of page title, if it's two lines */

function lineUpTitle()
{
	obj = $( 'div#container div#main div#left div#inside-header table#inside-left h2' );
	
	if( obj.text().length >= 18 )
	{
		obj.css( { 'margin-top' : '36px' } );
	}
}

/* function to get the full date and place it in the header */

function getFullDate()
{	
	var d = new Date();
	var day = d.getDay();
	var month = d.getMonth();
	var date = d.getDate();
	var year = d.getFullYear();
	var dayname = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ];
	var monthname = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ];
	
	var str = '<strong style="padding-right: 20px">Today is ' + dayname[ day ] + ' ' + monthname[ month ] + ' ' + date + ', ' + year + '</strong>';
	
	$( 'div#container div#header div#info span.right' ).prepend( str );
}

/* add comma's to blog categories (Newsletter) */

function styleBlogCat()
{
	$( 'div.inner-list ul.inside div#blog-categories a:not(:last)' ).after( ',' );
}

/* used to determine is left sub navigation is empty ( this was later removed ) */

function isSubEmpty()
{
	if( jQuery.trim( $( 'ul.inside' ).text() ) == '' )
	{
		$( 'ul.inside' ).remove();
		$( 'ul.large' ).css( { 'margin-top' : '0px' } );
	}
}

// start google maps

function drawmap( map, i )
{
	var str = map.text();
	
	if( str )
	{
		var lat_lang = null;
		var zoom = 16;
		var add = null;
		
		for( var i = 0; i < str.split( '&' ).length; i++ )
		{
			var ll = str.split( '&' )[ i ].split( '=' )[ 0 ];
			if( ll == 'll' ) { lat_lang = str.split( '&' )[ i ].split( '=' )[ 1 ]; }
			if( ll == 'z' ) { zoom = eval( str.split( '&' )[ i ].split( '=' )[ 1 ] ); }
			if( ll == 'q' ) { add = str.split( '&' )[ i ].split( '=' )[ 1 ].split( '+' ).join( ' ' ); }
		}
		
		/*
		console.log( 'address is: ' + add );
		console.log( 'lat lang is: ' + lat_lang );
		console.log( 'zoom is: ' + zoom );
		*/
		
		var g_map = null;
		var geocoder = null;
		
		map.after( '<div class="' + i + '" style="width: 418px; height: 200px; border: 1px solid #336699; margin: 20px 0px 50px;"></div>' ).remove();
		
		if ( GBrowserIsCompatible() )
		{
			g_map = new GMap2( $( 'div.'+i )[ 0 ] );
			g_map.setMapType( G_SATELLITE_MAP );
			g_map.setUIToDefault();
			
			geocoder = new GClientGeocoder();
			
			if( lat_lang == null )
			{
				showAddress( geocoder, add, g_map, i, zoom ); 
			}
			
			else
			{
				g_map.setCenter( new GLatLng( lat_lang.split( ',' )[ 0 ], lat_lang.split( ',' )[ 1 ] ), eval( zoom ) );
				addMarker( g_map, new GLatLng( lat_lang.split( ',' )[ 0 ], lat_lang.split( ',' )[ 1 ] ) );
			}
		}
	}
}

function addMarker( map, point, icon, text )
{
	var marker = new GMarker( point, icon );
	map.addOverlay( marker );

	if( typeof( text ) != "undefined" )
	{
		GEvent.addListener( marker, "click", function()
		{
			marker.openInfoWindowHtml( '<div style="white-space:nowrap">' + text + '</div>' );
		});
	};
};

function showAddress( geocoder, address, map, num, zoom )
{
	if ( geocoder )
	{
		geocoder.getLatLng(
		address,
		function( point )
		{
			if ( !point )
			{
				$( $( 'div.' + num )[ 0 ] ).html( address + " not found" );
			}
			
			else
			{
				map.setCenter( point, zoom );
				addMarker( map, point );
			}
		});
	}
}

// end google maps