/*
	functions.dom.css
	DOM Interactivity Functions
	Created: April 23, 2008
	Creator: Matt Kircher, Mainline Media LLC
*/

function setupPage(){
	
	/*
	   Tiny tweaks to the page that conform to all browsers with CSS issues, 
	   as well as properly formatting complex structures such as floating columns
	   being treated as tables 
	*/
	
	//navigation
	$('#main-nav, #functional-nav, #sub-nav, #footer-main-nav, #footer-functional-nav').each(function(){ $(this).find('> ul > li:last').addClass('end_nav'); });
	$('#main-nav > ul > li.end_nav').prev().css({ borderRight:'1px solid #E8E8CE', marginRight:'0px', paddingRight:'15px' });
	$('#main-nav > ul > li:first').addClass('start_nav');
	if($('#sub-nav').length){ $('#sub-nav').prependTo($('#main-content')).show(); }
		
	//header / p proximity styles
	$('#main-content, #sub-content')
	.find('> :header:gt(0)')
	.css({ marginBottom:'0px', paddingTop:'0px', marginTop:'15px' })

	$('#content')
	.find('> :header').css({ paddingBottom:'2px' }).end()
	.find(':header + p, :header + ul, :header + ol, :header + blockquote').css({ marginTop:'0px', paddingTop:'0px' });
	
	$('#content, #footer-extra-content').find(':header + :header').addClass('subhead').css({ marginTop:'0px'});
		
	//img / properties
	$('#main-content img[@align="left"]').css({ margin:'0px 12px 12px 0px' });
	$('#main-content img[@align="right"]').css({ margin:'0px 0px 12px 12px' });
	$('img[@border="1"]').css({ border:'1px solid #ECECD6' });
		
	//position elements
	$('#footer-extra-content, .tri-columned-content, .double_securities_listing').append('<br class="clear_block" />');
	$('.double_securities_listing')
	.find('div:eq(0)').addClass("securities_listing").end()
	.find('div:eq(1)').addClass("nonsecurities_listing").end()
	.find('div').each(function(){
		if($(this).next().filter('div').length && $(this).height() > $(this).next().height()){ $(this).next()[0].style.height = eval($(this).height())+'px'; }
		if($(this).prev().filter('div').length && $(this).height() > $(this).prev().height()){ $(this).prev()[0].style.height = eval($(this).height())+'px'; }
	});
	
	$('.news_listing dt:eq(0), .news_listing dd p:eq(0), .news_listing dd :header:eq(0)').css({ marginTop:'0px', paddingTop:'0px' });
	
	//if($.browser.safari){ $('#logo h2').css({ left:'264px' }); }
	if($.browser.mac && $.browser.firefox){ $('#logo h2, #functional-nav').css({ left:'30px' }); }
	
	$('#footer-extra-content').prepend('<hr />').find('> hr:eq(0)').css({ position:'relative', top:'-15px', margin:'0px', marginBottom:'-5px' });
	
	//click-away message setup
	$('a.external_link').click(function(){
		var c = confirm('You are now leaving claimscompensation.com.\nCCB is not responsible for content or privacy policies on other sites.');
		if(c){ document.location.href = $(this).attr('href'); }
		return false;
	});
}

// ** requires jquery dimensions plugin **
function setupHeights(){	
	if($('#content').height() < $('#main-content').outerHeight() ||
	   $('#content').height() < $('#sub-content').outerHeight()){		
		
		var m = eval($('#main-content').outerHeight() + 20);
		var s = ($('#sub-content').length)?eval($('#sub-content').outerHeight() + 20):0;
		var h = (m >= s)?m:s;
		
		$('#content').height(h+"px");
		
		$('#main-content').height(eval($('#content').outerHeight() - 40)+"px");
		if($('#sub-content').length){ $('#sub-content').height(eval($('#content').outerHeight() - 40)+"px"); }
	}
	$(document).bind('resize', setupHeights);
}

function translateEmails(){
	$('span.email, address.email').each(function(){
		
		//example: <span class="email" title="[title] | [addr at domain dot com] | [subject of email]"> [link text] </span>
							   
		var spt = $(this);
		var at = / at /;
		var dot = / dot /g;		
		
		var inner_content = $(spt).html();						//inner HTML of span tag
		var t = $(spt).attr('title');						//email, link options from title attribute
		
		var title = t.substring(0, t.indexOf('|'));				//title for the link
		t = t.substring(t.indexOf('|')+1);
		
		var addr = t.substring(0, t.indexOf('|'));				//email address from id attribute
		addr = addr.replace(at,"@").replace(dot,".");				//replace words with chars
		
		var subject = t.substring(t.indexOf('|')+1);				//subject for email, if needed
		var fulladdr = ($.trim(subject) != "")?addr+'?subject='+subject:addr;	//full address formed with subject, if needed
		
		inner_content = ($.trim(inner_content) == "" || $.trim(inner_content) == "&nbsp;")?addr:inner_content;
		
		$(spt).after('<a href="mailto:'+fulladdr+'" title="'+title+'">'+ inner_content +'</a>')
		.hover(function(){window.status="Send an email!";}, function(){window.status="";});
		$(spt).remove();
	});
}

var client_count = 1;
function setupClientCarousel(){
	if( $('#client_carousel').length ){
		
		if($.browser.browser != "MSIE"){
			var id = setInterval(function(){
							  
				var step = "-"+eval(140 * client_count)+"px";
				var dur  = (client_count==0)?100:1200;
							  
				$('#client_carousel ul')
				.fadeTo(750, 0.25)
				.animate({ left:step }, dur)
				.fadeTo(750, 1);
				
				client_count = (client_count == $('#client_carousel ul li').length - 1)?0:(client_count + 1);
			}, 5500);
			
		} else {
			
			client_count -= 1;
			$('#client_carousel ul li:gt(0)').fadeOut(1);
			
			var id = setInterval(function(){
				
				$('#client_carousel ul li:eq(0)').fadeOut(750, function(){
					$('#client_carousel ul li:eq(0)').appendTo('#client_carousel ul');
					$('#client_carousel ul li:eq(0)').fadeIn(750);
				});				
			}, 5500);			
		}		
	}
}

function setupNewsSelector(){
	if($('#category_select').length){
		var cats = new Array();
		$('.article_listing').each(function(){
			var c = $(this).attr('title');
			if($.inArray(c, cats) == -1) cats.push(c);
		});
		
		for(x in cats){
			$('#category_select select').append('<option value="'+cats[x]+'">'+cats[x]+'</option>');
		}
		
		$('#category_select select').bind('change', function(){
			$('.article_listing').show();
			
			var v = $(this).find('option:selected').val();
			if(v != "all"){
				$('.article_listing').not('.article_listing[@title="'+v+'"]').hide();
			}
		})
		
		setupHeights();
	}
}

/* IE RELATED */

function applyIE6FlickerFix(){
	try {
	  document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
}

$(document).ready(function(){
	applyIE6FlickerFix();
	setupPage();
	translateEmails();
	setupHeights();
	
	setupClientCarousel();
	setupNewsSelector();
});
