/* ////////////////////////////////////////////////////////////////////////////////////////////////////

// core.js should only be used for functions that are site-wide
// separate functions that are page specific into page specific javascript files
// camelCase should be used for all variable and function names, e.g. "initLinks()" NOT "init_links()"

//////////////////////////////////////////////////////////////////////////////////////////////////// */

function initFormElems() {
	var noLbls = $('.noLabel');
	noLbls.each(function(i) {
		$(this).removeClass('noLabel').addClass('noLabel-replaced');
		if ($(this).children('input').data('defaultVal') == null  ) {
			$(this).children('input').data('defaultVal', $(this).children('label').text());
		} 
		
		if( ($(this).children('input').val() != "" && $(this).children('input').val() != null) && $(this).children('input').data('defaultVal') != $(this).children('input').val() ) {
			$(this).children('label').css('z-index', '-2');
		}
		
		$(this).children('input').focus(function() {
			$(this).siblings('label').css('z-index', '-2');
		});
		
		$(this).children('input').blur(function() {
			if ( $.trim($(this).val()) == "" ) {
				$(this).siblings('label').css('z-index', '10');
			}
		});
	});
}

$(initFormElems);

function clearFormElems() {
	//alert('hey');
    $('form')[0].reset();
}

//$(clearFormElems);

//function initNewsArticles() {
//	$('.news .fullArticle').css('display', 'none');
//	
//	$('.news .partialArticle a').click(function() {
//		var li = $(this).parents('.news li');
//		li.addClass('active');
//		$('.news li:not(.active)').fadeOut("slow", function() {
//			li.children('.partialArticle').hide();
//			li.children('.fullArticle').slideDown("normal");
//		});
//		//li.children('.fullArticle').css('display', 'block');
//		
//		return false;
//	});
//	
//	$('.news .fullArticle a').click(function() {
//		var li = $(this).parents('.news li');
//		
//		li.children('.fullArticle').slideUp("normal", function() {
//			$('.news li:not(.active)').fadeIn("slow");
//			li.removeClass('active');
//			$(this).parents('.news li').children('.partialArticle').show();
//		});
//		return false;
//	});
//}

//$(initNewsArticles);

function popUp(url) {
    var newwindow = window.open(url, 'extWin', 'height=800,width=1024,scrollbars=1,resizable=1,menubar=1,status=1,toolbar=1,location=1,personalbar=1');
    if (newwindow.focus) {
        newwindow.focus()
    }
    return false;
}

function initLinks() {
    var anchors = $('a');
    anchors.each(function(i) {
        var h = $(anchors[i]).attr('href');
        var r = $(anchors[i]).attr("rel");
        if (h != null && r != null) {
			if (r == 'external') {
                //popup script
                $(anchors[i]).click(function() {
                    return popUp($(this).attr("href"))
                });
				var title = $(anchors[i]).attr('title') + $(anchors[i]).text();
                $(anchors[i]).attr("title", title);
            }
        }
    });
}

$(initLinks);

function openArticle(o) {
    $(o).parent().find('blockquote').hide();
    $(o).parent().find('.fullArticle').show();
    $(o).parent().find('.close').show();
    $(o).parent().find('.readMore').hide();
}

function closeArticle(c) {
    $(c).parent().find('.fullArticle').hide();
    $(c).parent().find('blockquote').show();
    $(c).parent().find('.close').hide();
    $(c).parent().find('.readMore').show();
}

/*
$("#articles li .fullArticle").each(function(i) {
    if (i == 0) {
        openArticle(this);
    }
});*/
/* changed to document ready to avoid js errors and simplified first one */
function initArticles() {
	// openArticle($("#articles li .fullArticle")[0]);
	$(".heading").click(function() {
		if ($(this).parent().find('.fullArticle').is(':visible')) {
			closeArticle(this);
		}
		else {
			openArticle(this);
		}
	});

	$(".readMore").click(function() {
		openArticle(this);
	});

	$(".close").click(function() {
		closeArticle(this);
	});
}

$(initArticles);

function checkCountry(){
   if($("[id$='_selCountry'] option:selected").val() != "United States"){
        $("[id$='_selState']").val('null');
   } 
}

function checkState(){
   if($("[id$='_selState'] option:selected").val() != "null"){
        $("[id$='_selCountry']").val('United States');
   } 
}

//center popover
function centerPopover()
{
	var wcenter = $(window).width()/2;
	var hcenter = $(window).height()/2;
	$('.popover').css('left', wcenter-$('.popover').outerWidth()/2).css('top', hcenter-$('.popover').outerHeight()/2);
}

//show and hide popover
function showPopover()
{
	$('body').addClass('layerFix');
	//$('select').css('visibility', 'hidden');
	centerPopover();
	$('#screen').css('opacity', 0).show();
	$('.popover').fadeIn('normal');
	$('#screen').animate({opacity: 0.9}, 'normal');
}

function closePopover()
{
	$('body').removeClass('layerFix');
	//$('select').css('visibility', 'visible');
	$('.popover, #screen').fadeOut('normal', function(){$('.popover .popoverForm').show();$('.popover .popoverThankyou').hide();});
}