/***
 * file:			<webroot>/scripts/home.js for use with new tabbed content in home.cfm 
 * created:			2011-09-14 jgPM :: Jeff Gnass/PangoMedia <jgnass@pangomedia.com>
 * dependencies:	jQuery v1.6.2
 * ***/

$(document).ready(function()
{
    // do stuff when DOM is ready
	
	SetNavigationTabColors();
	
	// clean-up screen-scrapped content from ADFG Commercial Fish Division Research Section pages
	$('.afterpadder > ul').css( {'list-style-type' : 'disc'} );
	$('.afterpadder > .overviewlink').css( {'display' : 'inline'} );
	
	$('#leftcolumn')
		.css('background-color','#80b3ff') // lightest gradient color is default
		.css('background-image','url(./images/akssf_org_column_bkgnd_blue.png)') // gradient (HSL 216 100-50 100)
		.css('background-repeat','repeat-x')
		.css('background-position-y','top')
		.css('background-size','100% 100%');
		// now set height with $(window).resize() callback function;
		// also triggering window resize event when DOM ready 
	
	SetColumnSectionBackgroundColor('div#leftcolumn h2','gray');
	
	$('div#centercolumn h3')
		.css('background-color','#ebebeb') // lightest gradient color is default
		.css('background-image','url(./images/akssf_org_section_bkgnd_gray.png)') // gradient (HSL 0 0 81-100)
		.css('background-repeat','repeat-x')
		.css('background-position-y','center')
		.css('background-size','100% 100%')
		.css('border','solid 1px #cfcfcf')
		.css('-webkit-border-radius-topleft','10px')
		.css('-webkit-border-radius-topright','10px')
		.css('-moz-border-radius-topleft','10px')
		.css('-moz-border-radius-topright','10px')
		.css('-o-border-radius-topleft','10px')
		.css('-o-border-radius-topright','10px')
		.css('border-top-left-radius','10px')
		.css('border-top-right-radius','10px');
	
	SetColumnSectionBackgroundColor('div#centercolumn h3','gray');
	
	$('div#centercolumn h3')
		.css('color','#336')
		.css('padding','1px 8px 3px 8px');
	
	$('#rightcolumn')
		.css('background-color','#fff') // lightest gradient color is default
		.css('background-image','url(./images/akssf_org_column_bkgnd_gray.png)') // gradient (HSL 0 0 81-100)
		.css('background-repeat','repeat-x')
		.css('background-position-y','top')
		.css('background-size','100% 100%');
		
	SetColumnSectionBackgroundColor('div#rightcolumn h2','blue');
	
	// create staff name panel by wrapping with span tag changing display attribute to block;
	// staff name is inside a p tag so cannot use div tag, etc
	$('#rightcolumn .afterpadder > p > strong').wrap('<span class="staff" />');
	
	// set basic styling of staff name panels
	$('.staff')
		.css('display','block')
		.css('background-color','#cfcfcf') // darkest gradient color is default background color but replaced with background image below
		.css('border','solid 1px #cfcfcf') // add contrast but border disappears when color same as default background color
		.css('margin','0')
		.css('padding','1px 8px 3px 8px')
		.next() // find following sibling br tag
		.remove(); // remove br tag (NOTE :: this is screen-scrapped content !!!)
	
	SetStaffNamePanelBackgroundColor();
	
	
var urlParams = {};
(function ()
{
	var e,
	a = /\+/g,  // Regex for replacing addition symbol with a space
	r = /([^&=]+)=?([^&]*)/g,
	d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
	q = window.location.search.substring(1);
	
	while (e = r.exec(q))
	urlParams[d(e[1])] = d(e[2]);
})();

	
	
	// bind navigation tab links to click event callback function
	$('a.tab').click(function ()
	{
		// remove any active tabs
		$('.active')
			.removeClass('active')
			.css('border-bottom','1px solid #336'); // re-apply styling to inactive tabs
		
		// set active tab on and re-apply styling lost in some browsers (Safari on iOS);
		// blur active tab as some browsers will retain clicked selection highlight outline (IE 9)
		$(this)
			.addClass('active')
			.css('border-bottom','1px solid #fff')
			.blur();
		
		SetNavigationTabColors();
		
		// 2011-09-15 jgPM :: FIXME :: detect iOS devices (iPad, iPhone, iPod) and fix
		// navigation menu tab position bottom border issue with active tab 
		// if ($.browser.webkit)
		if (navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
		{
			// Safari on iOS requires 1 pixel less bottom margin else active tab bottom border still revealed  
			//$('ul.tabs').css('margin-bottom','2px'); // set in stylesheet ul.tabs { margin: 3px 0; ... }
			$('ul.tabs').css('position','relative'); // set in stylesheet ul.tabs { margin: 3px 0; ... }
		}
		
		// slide all content elements up
		$('.content').slideUp(300); // default is 400 ms; slow 600 ms; fast 200 ms
		
		// slide active content element down
		var content_show = $(this).attr('label');
		$('#' + content_show).slideDown(300); // default is 400 ms; slow 600 ms; fast 200 ms
	});
	
	
	
	
	if (urlParams['menu'] != undefined && urlParams['menu'].length != 0)
	{
		if (urlParams['menu'] == 'news')
		{
			$('a.tab').eq(0).trigger('click');
		}
		else if (urlParams['menu'] == 'policies')
		{
			$('a.tab').eq(1).trigger('click');
		}
		else if (urlParams['menu'] == 'faq')
		{
			$('a.tab').eq(2).trigger('click');
		}
		else if (urlParams['menu'] == 'workshop')
		{
			$('a.tab').eq(3).trigger('click');
		}
	}
	
	if (urlParams['S_GUID'] != undefined && urlParams['S_GUID'].length == 36)
	{
		//$('#content_1').hide();
		//$('#content_2').hide();
		//$('#content_3').hide();
		//$('#content_4').show();
		
		$('a.tab').eq(3).trigger('click');
		
		$('div#student_login').hide();
		$('div#workshop_media').show();
	}
	else
	{
		$('div#student_login').show();
		$('div#workshop_media').hide();
	}
	
	
	
	
	// bind navigation tab links to hover event callback function
	$('a.tab').hover(
		// handerIn
		function()
		{
			$(this).css('color','#ffeeab'); // lighter gold
			// restore active tab color
			$('a.active').css('color','#336');
		},
		// handlerOut
		function ()
		{
			$(this).css('color','#fc0'); // gold
			// restore tab colors
			SetNavigationTabColors();
		}
	);
	
	$(window).resize(function()
	{
		SetStaffNamePanelBackgroundColor();
		SetColumnSectionBackgroundColor('div#rightcolumn h2','blue');
		SetColumnSectionBackgroundColor('div#centercolumn h3','gray');
		SetColumnSectionBackgroundColor('div#leftcolumn h2','gray');
		
		// recalculate leftcolumn height when rightcolumn text reflows as window is resized
		$('#leftcolumn').css('height',$('#rightcolumn').outerHeight() - 20); // 20px is bottom padding of right column
	});
	
	$(window).trigger('resize');
	
}); // $(document).ready()

function SetColumnSectionBackgroundColor(colParams,colColor)
{
	/* USAGE :: passed-in params
	 * 'div#rightcolumn h2','blue'	// gradient (HSL 216 100-50 100)
	 * 'div#centercolumn h3','gray'	// gradient (HSL 0 0 81-100)
	 * 'div#leftcolumn h2','gray'	// gradient (HSL 0 0 81-100)
	 */
	
	// handle invalid data passed-in
	if ((colParams || colColor) == undefined) { return false; }
	if ((colParams.length || colColor.length) == 0) { return false; }
	
	$(colParams).each(function()
	{
		$(this)
			.css('background-image','url(./images/akssf_org_section_bkgnd_' + colColor + '.png)')
			.css('background-size','100% 100%');
			// center reflection gradient images (10x120 pixels)
	});
	
}; // SetColumnSectionBackgroundColor(colParams,colColor)

function SetStaffNamePanelBackgroundColor()
{
	// style staff name panel background color from section view of gradient sprite image (10x1500 pixels)
	$('.staff').each(function(index)
	{
		$(this)
			.css('background-color','#cfcfcf') // darkest gradient color is default
			.css('border','solid 1px #cfcfcf') // add contrast but border disappears when color same as default background color
			.css('background-image','url(./images/akssf_org_column_bkgnd_gray.png)') // gradient (HSL 0 0 81-100)
			.css('background-repeat','repeat-x')
			.css('background-position-y',(((1500 / $('.staff').length) * index) - 1500 + $(this).outerHeight()));
			// calculate y-offset beginning with first element positioned at opposite panel location on gradient sprite image;
			// account for panel height else gradient section view may be positioned off top or bottom end of image
			// then default background color will be shown instead
	});
	
}; // SetStaffNamePanelBackgroundColor()

function SetNavigationTabColors()
{
	// set all tab text color and text decoration
	$('a.tab')
		.css('color','#fc0') // gold
		.css('text-decoration','none');
	
	// set active tab text color
	$('a.active').css('color','#336');
	
}; // SetNavigationTabColors()

