var bClose = true;
var bClose2 = true;

// this function is for the show and hide feature on the main practices page
$(document).ready(function() {
	$('.practicelist h2').next('ul').hide(); // hide all items
	$('.practicelist .arrowLink').click(function() {
		// handle arrow img switch
		var newSrc = "";
		var iDirection = 0;
		if (this.src.indexOf('on') > 0) {
			newSrc = "images/practicelist_arrow_off.gif";
			iDirection = 0;
		} else {
			newSrc = "images/practicelist_arrow_on.gif";
			iDirection = 1;			
		}
		$('.practicelist .arrowLink').attr('src', 'images/practicelist_arrow_off.gif');
		this.src = newSrc;
		
		// perform animation
		var idToUse = ".ul" + $(this).attr('id');
		var id = $(this).attr('id');

		$('.practicelist h2').next("ul").slideUp('400'); 	// hide all of them
		$('#textContainer span').css('color', '#006b4c');	// reset the text color
		
		if (iDirection == 1) {
			$(idToUse).slideToggle('400');					// expand the selected item
			$('#span' + id).css('color', '#00AD7B');		// change color to active state
			// set cookie for this item
			$.cookie('practiceActive',id, { expires: 2});
		}
		
	return false;
	});
	// get the cookie value and show the right node
	var cookieValue = $.cookie('practiceActive');
	if (cookieValue != null) {
		$('.ul' + cookieValue).show();
		$('#span' + cookieValue).css('color', '#00AD7B');
		$('img#' + cookieValue).attr('src', 'images/practicelist_arrow_on.gif');
	}
});

// this function is for the tabbed content on the home page
$(document).ready(function () {
    var tabContainers = $('.homefeatures > div');
    
    $('.homefeatures .nav a').click(function () {
        tabContainers.hide().filter(this.hash).show();
        
        $('.homefeatures .nav a').removeClass('active');
        $(this).addClass('active');
        
        return false;
    }).filter(':first').click();
});

// update title - takes a param and updates the page title with the correct format.. used mainly for modules
function updatePageTitle(sNewTitle) {
	document.title = sNewTitle +" | Thorp Reed & Armstrong";	
}
function updateBreadcrumbs(arrayOfPages) {
	var sResult = "<ul>";
	for (var i = 0; i < arrayOfPages.length; i ++) {
		var objects = arrayOfPages[i]; // objects[0] = url, objects[1] = display
		sResult += "<li";
		if (arrayOfPages.length-1 == i)
			sResult += " class='current'>"+ objects[1];
		else
			sResult += "><a href='"+ objects[0] +"'>"+ objects[1] +"</a></li>";
	}
	sResult += "</ul>";
	$('#crumbs').html(sResult);
	return true;
}
function getSearchSuggestions() {
	// run an ajax call to get the keywords
	$.get("modules/Search_ajax.aspx", { Keyword: $('#key').val() }, function(data) {
		$('#searchSuggest').html(data);
		$('#searchSuggest').show();
		$('table.searchSuggest').mouseover(function() {
			bClose = false;
		});
		$('table.searchSuggest').mouseout(function() {
			bClose = true;
		});
		
		$('table.searchSuggest').find("td").click(function() {
			$('#key').val($(this).text());
			bClose = true;
			closeSearchSuggest();
		});
	});	
}
function getSearchSuggestions2() {
	// run an ajax call to get the keywords
	$.get("modules/Search_ajax2.aspx", { Keyword: $('#key2').val() }, function(data) {
		$('#searchSuggest2').html(data);
		$('#searchSuggest2').show();
		$('table.searchSuggest2').mouseover(function() {
			bClose2 = false;
		});
		$('table.searchSuggest2').mouseout(function() {
			bClose2 = true;
		});
		
		$('table.searchSuggest2').find("td").click(function() {
			$('#key2').val($(this).text());
			bClose2 = true;
			closeSearchSuggest2();
		});
	});	
}
function closeSearchSuggest() {
	// only close if mouse is not over div
	if (bClose)
		$('#searchSuggest').hide();
}
function closeSearchSuggest2() {
	// only close if mouse is not over div
	if (bClose2)
		$('#searchSuggest2').hide();
}
// jquery cookie plugin
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
