/*
 * General Functionality
 */

$(document).ready(function() {
	
	/*
	 * Temp Fix (while using dev-temp site)
	 */
	/*$('img[src^="/images/"]').each(function() {
		src = $(this).attr('src');
		$(this).attr('src', src.replace('/images/', '/cwl/www/images/'));
	});*/
	
	/*
	 * Get URL Params
	 * helper: to be used by other functions
	 * can pass a url, if no url passed will use the current url of the page
	 */
	function get_url_vars(url) {
		// if we're not passed the url, use the page url
		if (!url || url.length == 0)
		{
			url = window.location.href;
		}		 
	    var vars = [], hash;
	    var hashes = url.slice(url.indexOf('?') + 1).split('&');
	    for(var i = 0; i < hashes.length; i++)
		{
	        hash = hashes[i].split('=');
	        vars.push(hash[0]);
	        vars[hash[0]] = hash[1];
	    }
	    return vars;
	}
	
	/*
	 * Breadcrumbs
	 * - update breadcrumbs for news & promotions pages
	 */
	var page = get_url_vars()['page'];
	var breadcrumb = $('#breadcrumbs');
	
	if (get_url_vars()['date'] && page == 'promotions') {
		breadcrumb.append(' &rsaquo; <a href="?page=promotions">Promotions</a>')
	} else if (get_url_vars()['date'] && page == 'news') {
		breadcrumb.append(' &rsaquo; <a href="?page=news">News</a>')
	}

	/*
	 * Links
	 * - open external links in a new window
	 * - append url to title attribute for external links
	 * - if the title isn't set, add the text
	 */
	$('a[href^="http"]').not('[href*="commonwealthlegal.com"]').not('[href*="linkedin.com"]').not('[href*="twitter.com"]').attr('target', '_blank').addClass('external');
	$('#top-strip .links a').attr('target', '_blank');
	
	/* open pdfs in a new window */
	$('a[href$=".pdf"]').attr('target', '_blank');
	
	/* if this external link is an image, we don't want to show the external link icon */
	$('a.external').each(function() {
		if ($(this).html().substr(0, 4) == '<img') {
			$(this).addClass('noicon');
		}
	});

	/*
	 * Links
	 * - more links
	 * - back to top links
	 */
	$('a.more').prepend('&raquo; ');
	
	$('a.top').click(function() {
		$('html').scrollTop(0);
		return false;
	});

	/*
	 * Sidebar Menu
	 * show the submenus child elements only when active
	 */
	$('#right .menu ul li').not('.selected').find('ul').hide();

	/*
	 * Menu
	 * we can't assign a page to different menus, so in order to have privacy
	 * appear in the footer it also has to appear in the main nav... but we don't
	 * want it there... remove.
	 */
	$('#nav li a[href$="privacy"]').hide();

	/*
	 * Rounded Corners
	 * http://www.malsup.com/jquery/corner
	 * the plugin uses -moz-border-radius for supported firefox browsers, so we need to add
	 * some extra corners in those instances
	 */
	$('#nav').corner('5px');
	$('#main').corner('5px');
	
	$('#nav li.first a').corner('5px tl bl');
	$('#toolkit').corner('5px bl br');
	if (!$('body').hasClass('home')) {
		$('#right').corner('5px tr');
	}

	/*
	 * Template
	 * 
	 */
	
	/* if right is shorter than left, set right height to same as left (for css/bg) */
	if ($('#right').height() < $('#left').height()) {
		//$('#right').height($('#left').height() + parseInt($('#left .inner').css('padding-bottom')));
		$('#right').height($('#left').height());
	}
	
	/* top strip appears at the end of the page for SEO purposes,
	   but should appear at the top of the page */
	$('body').prepend($('#top-strip'));
	
	/* search submit */
	/*$('form#search a.submit').click(function() {
		//$(this).parents('#search').submit();
		return false;
	});*/

	/* navigation: home (menu output not themeable, add span for css to add icon) */
	$('#nav li:first a').html('<span class="home">' + $('#nav li.first a').html() + '</span>');
});
