/**
 * @author tony.herford
 */
var iTop = 0;
var offset = 0;
var tracking = {disclosure:'INVESTOR INFORMATION | ADDITIONAL DISCLOSURES', terms:'INVESTOR INFORMATION | TERMS CONDITIONS'};

function createDisclosure(linkLabel, header, content, id)
{
	if(typeof id == 'undefined') id = "disclosure";

	jQuery('#disclosureLinks').append("<span class='disclosure-link' id='"+id+"-link'><a href='#' onclick='return false;'>" + linkLabel + "</a></span>");
	jQuery('#disclosureLinks').after(
		"<div class='disclosure-container' id='"+id+"-container' style='z-index: 10;' >" +
			"<div id='"+id+"-bg'>" +
				"<div id='title'>"+header+"</div>" +
				"<div class='disclosure-scroll-box' id='"+id+"-scroll-box'>" +
					"<div class='disclosure-content' id='"+id+"-content'>" + content + "</div>" +
				"</div>" +
				"<div class='scroll-btns' id='"+id+"-scroll-btns'>" +
					"<img class='scroll-up'id='"+id+"-scroll-up' src='/images/up_arrow.png'>" +
					"<img class='scroll-down' id='"+id+"-scroll-down' src='/images/down_arrow.png'>" +
				"</div>" +
			"</div>" +
			"<div id='"+id+"-bottom'>&nbsp;</div>" +
		"</div>"
	);

	var timer,
		hideTimer,
		event = {};
	jQuery('#'+id+'-link a').mouseenter(function()
	{
		timer = setTimeout('showDisclosure("'+id+'")', 500);
	});
	jQuery('#'+id+'-link a').mouseleave(function()
	{
		clearTimeout(timer);
		//hideDisclosure();
	});
	jQuery('#'+id+'-container').live( "mouseleave", function()
	{
		clearTimeout(timer);
		hideTimer = setTimeout( 'hideDisclosure( "' + id + '" )', 500 );
	});
	jQuery('#'+id+'-container').live( "mouseenter", function()
	{
		clearTimeout( hideTimer );
	});
	jQuery('#'+id+'-scroll-down').click(function (){
			iTop -= 200;
			if(iTop < offset) iTop = offset;
			jQuery('#'+id+'-content').animate({
				top: iTop
			},500);
			//console.log(iTop + " = " + offset);
			if(iTop <= offset)
			{
				jQuery('#'+id+'-scroll-down').hide();
			}
			jQuery('#'+id+'-scroll-up').show();
			return false;
		});
	jQuery('#'+id+'-scroll-up').click(function (){
			iTop += 200;
			if(iTop > 0) iTop = 0;
			jQuery('#'+id+'-content').animate({
				top: iTop
			},500);
			if(iTop == 0)
			{
				jQuery('#'+id+'-scroll-up').hide();
			}
			jQuery('#'+id+'-scroll-down').show();
			return false;
		});
}

var animating = false;

function showDisclosure(id)
{
	//alert(id);
	if(!animating)
	{
		// tracking
		//gClickTrack({linkName: s.pageName + ' | VIEW ADDITIONAL DISCLOSURES', prop20: s.pageName + ' | VIEW ADDITIONAL DISCLOSURES', prop24: 'EN', prop25:s.prop25});
		if(typeof(s) != 'undefined' && typeof( s.pageName ) != 'undefined' ){
			gClickTrack({linkName:s.pageName + ' | ' + id.toUpperCase(),  prop5:tracking[id], linkType:'o'});
		}
		animating = true;
		// reset to top each time it shows
		jQuery('#'+id+'-content').css({top: 0});
		jQuery('#'+id+'-scroll-down').show();
		jQuery('#'+id+'-scroll-up').hide();

		jQuery('#'+id+'-container').slideDown(400, function(){
			// get the disclosure pop-up height
			//alert(id);
			var containerHeight = (id == 'terms') ? 400 : 250;//had to hard code it for opera //jQuery('#disclosure-scroll-box').height();
			var contentHeight = jQuery('#'+id+'-content').height();
			var maxHeight = 0;

			if (contentHeight < containerHeight)
			{
				maxHeight = contentHeight
			}else
			{
			 	maxHeight = containerHeight
			}
			jQuery('#'+id+'-scroll-box').animate({height:maxHeight}, 400);
			offset = containerHeight - jQuery('#'+id+'-content').height();
			if(offset <= 0) jQuery('#'+id+'-scroll-btns').slideDown(400).delay(400);
			animating=false;});
		jQuery('#disclosureLinks').animate({textIndent:'-999px'});
		// calculate offset
		/*
		offset = containerHeight - jQuery('#gMdsDisclaimer #disclaimerContent').height();
		if(offset > 0) jQuery('#scroller').hide();*/
	}
}

function hideDisclosure(id)
{
	//alert(id);
	if(!animating)
	{
		animating = true;
		jQuery('#'+id+'-container').slideUp(400, function(){animating=false;});
		jQuery('#disclosureLinks').animate({textIndent:'0'});
	}
}

// for testing
jQuery(document).ready(function()
{
	//console.log( 'before if' );
	//this will add the test disclaimers on the new November section.
	if( location.href.indexOf('/') > -1 || location.href.indexOf('/news-article.jsp') > -1){
		// create holder for links
		jQuery('#gContentContainer').append("<div id='disclosureLinks'></div>")
		//this will add the test disclaimers on the new November section .
		if (jQuery('.investors-disclaimers').length > 0)
		{
			//console.log('has disclaimers')
			jQuery('.investors-disclaimers #title').remove();
			jQuery('.investors-disclaimers').hide();
			createDisclosure("View Additional Disclosures", "Additional Disclosures", jQuery(".investors-disclaimers").html(), "disclosure");
		}
		createDisclosure("View Terms And Conditions","", '<p><b>LEGAL NOTICES<br>      PLEASE READ THESE TERMS CAREFULLY BEFORE USING THIS SITE</b></p>    <p>THIS WEB SITE (THE "SITE") IS PROVIDED BY GM FOR INFORMATIONAL PURPOSES ONLY. BY USING THE SITE, OR BY DOWNLOADING MATERIALS      FROM THIS SITE, YOU AGREE TO ABIDE AND BE BOUND BY THE TERMS AND CONDITIONS      CONTAINED BELOW AND ELSEWHERE WITHIN THE SITE. IF YOU DO NOT AGREE TO ABIDE      BY AND BE BOUND BY THESE TERMS AND CONDITIONS, DO NOT USE THIS SITE OR      DOWNLOAD MATERIALS FROM THE SITE. </p>    <p> LINKS TO OTHER MATERIALS: As a convenience to you, GM may provide on      this Site links to Web sites (the "linked sites") that are operated by      other entities. The linked sites are not under the control of GM and GM      is not responsible for the content of any linked site or any link contained      in a linked site. GM reserves the right to terminate any link or linking      program at any time. GM does not endorse companies or products to which      it links and reserves the right to note as such on its Web pages. If you      decide to access any of the third party sites linked to this Site, you      do this entirety at your own risk and it is your responsibility to take      all protective measures to guard against viruses or other destructive elements. </p>    <p> APPLICABLE LAWS: This Site is controlled by GM from its offices within      the United States of America. GM makes no representation that information      in this Site is appropriate or available for use in other locations, and      access to this Site from countries or territories where the Site\'s content      is illegal is prohibited. If you access this Site from outside the United      States, you do so on your own initiative and you are responsible for compliance      with all applicable local laws. These Terms and Conditions of use are governed      by the laws of the State of New York, without giving effect to its conflict      of laws provisions.</p>    <p> GENERAL: GM reserves the right, at its sole discretion, to change, modify,      add or remove any portion of these Terms and Conditions, in whole or in      part, at any time. Changes will be effective when notice of such change      is posted. Your continued use of the Site after any changes are posted      will be considered acceptance of those changes. You should visit this page      from time to time to review the then-current Terms and Conditions because      they are binding on you.</p>    <p> THE INFORMATION CONTAINED HEREIN MAY CONTAIN INACCURACIES AND TYPOGRAPHICAL      ERRORS. GM DOES NOT WARRANT THE ACCURACY OR COMPLETENESS OF THE INFORMATION      OR THE RELIABILITY OF ANY ADVICE, OPINION, STATEMENT OR OTHER INFORMATION      DISPLAYED OR DISTRIBUTED THROUGH THIS SITE OR ANY LINKED WEB SITE. YOU      ACKNOWLEDGE THAT ANY RELIANCE ON ANY SUCH OPINION, ADVICE, STATEMENT, MEMORANDUM,      OR INFORMATION SHALL BE AT YOUR SOLE RISK. GM RESERVES THE RIGHT, IN ITS      SOLE DISCRETION, TO CORRECT ANY ERRORS OR OMISSIONS IN ANY PORTION OF THE      SITE. GM MAY MAKE ANY OTHER CHANGES TO THIS SITE AND THE INFORMATION CONTAINED      IN THIS SITE AT ANY TIME WITHOUT NOTICE.</p>    <p> IN NO EVENT SHALL GM OR ANY OF ITS SUBSIDIARIES BE LIABLE TO YOU FOR      ANY DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL OR OTHER DAMAGES (INCLUDING,      WITHOUT LIMITATION, ANY LOST PROFITS, BUSINESS INTERRUPTION, LOSS OF INFORMATION      OR PROGRAMS OR OTHER DATA ON YOUR INFORMATION HANDLING SYSTEM) THAT ARE      RELATED TO THE USE OF, OR THE INABILITY TO USE, THE CONTENT, MATERIALS,      AND FUNCTIONS OF THIS SITE OR ANY LINKED WEB SITE, EVEN IF GM IS EXPRESSLY      ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.</p>    <p>On this website, our use of the words "expect," "anticipate," "possible," "potential," "target," "believe," "commit," "intend," "continue," "may," "would," "could," "should," "project," "projected," "positioned" or similar expressions is intended to identify forward-looking statements that represent our current judgment about possible future events. We believe these judgments are reasonable, but these statements are not guarantees of any events or financial results, and our actual results may differ materially due to a variety of important factors. Among other items, such factors might include:  our ability to realize production efficiencies and to achieve reductions in costs as a result of our restructuring initiatives and labor modifications; our ability to maintain quality control over our vehicles and avoid material vehicle recalls; our ability to maintain adequate liquidity and financing sources and an appropriate level of debt, including as required to fund our planning significant investment in new technology; our ability to realize successful vehicle applications of new technology; and our ability to continue to attract new customers, particularly for our new products. GM\'s most recent annual report on Form 10-K and quarterly reports on Form 10-Q provide information about these and other factors, which we may revise or supplement in future reports to the SEC.</p>', "terms");

	} else if( jQuery( '#gContent').length > 0 && jQuery( '#gDisclamers').length > 0){//this will add the disclaimes on other pages
		var title = jQuery( "#gDisclamers" ).attr( 'title' ) != "" ? jQuery( "#gDisclamers" ).attr( 'title' ) : "Disclaimers";
		jQuery( '#gContent').after( "<div id='disclosureLinks'></div>" );
		createDisclosure( title, title, "", "disclosure");
		jQuery( '#disclosure-content' ).append( jQuery( "#gDisclamers" ) );
	}
})

