/*
 * The following JavaScript is an adaptation of the original, by Shawn (?), to be used with std. HTML as well as 
 * 	any other source of disclaimer-based content (for example, embedded links in XML fed to a Flash movie/swf.
 * 
 * 	To enable the pop-up disclaimer functionality the following conditions must be met:
 * 	+ Add (within a script element) to any HTML-rendered content: addDisclaimer("disciPod","discGlobalEpa","[etc...]");
 * 	+ Create a link using the following format:
 * 		<a href="javascript:showDisclaimer('disciPod')" title="iPod">*</a> (necessary for Flash XML, etc)
 * 		<a href="javascript:showDisclaimer('EPA')" title="EPA">&dagger;</a> (necessary for Flash XML, etc)
 * 													- OR / [AND] -
 * 		<a href="#iPod" onclick="showDisclaimer('disciPod')" title="iPod">[&Dagger;]</a> (preferred for HTML content)
 * 		<a href="#discGlobalEpa" onclick="showDisclaimer('EPA')" title="EPA">[**]</a> (preferred for HTML content)
 * 
 * 	To view all the disclaimers in the pop-up, call showDisclaimer() with an empty [/any] string, as in the following link:
 * 		<a href="#VIEW_ALL" onclick="showDisclaimer('')" title="View All Disclaimers">View All Disclaimers</a> (preferred for HTML content)
 * 
 * 	The order in which disclaimers are added, using the addDisclaimer method, is the order they appear in the pop-up.
 * 	The symbol representing the disclaimer is determined by the order it is added (not the character used in the link).
 * 	The symbol order is provided below in the array variable, arrDisclaimerLegend.
 */

// Global Pop-Up HTML Variables
var pageTitle = "", pageHeight = 280;
var arrDisclaimerKeys = new Array(), arrDisclaimerData = new Array(), arrLoadedDisclaimerKeys = new Array();
var arrDisclaimerLegend = ["*","&dagger;","&Dagger;","**","&dagger;&dagger;","&Dagger;&Dagger;","***","&dagger;&dagger;&dagger;","&Dagger;&Dagger;&Dagger;"];
var disclaimerPop = $("<div><div class=\"popDiv popCenter\"><ul><div id=0/><div id=1/><div id=2/><div id=3/><div id=4/><div id=5/><div id=6/><div id=7/><div id=8/><div id=9/></ul></div></div>");

function loadDisclaimers(){
	
	// Get all of the JSON (data) for disclaimers at page load.
	for (var i=0; i < arrDisclaimerKeys.length; i++)
	{
		if(!(arrDisclaimerKeys[i] in arrayConverter(arrLoadedDisclaimerKeys) )) {

			arrLoadedDisclaimerKeys.addItems ( arrDisclaimerKeys[i]  );
			
			var strHtml = disclaimerPop.html();		
			$.getJSON("/MMNA/legalDisclaimerAction.do?disclaimerKey="+ arrDisclaimerKeys[i], 
				function(data) {
	    			arrDisclaimerData.addItems({
	    				key: data.disclaimerKey,
	    				data: data.body
	    			});
	     		}
			);
			 
		}
	}
}

function addDisclaimer()
{ 	
 	// This method may have one/many argument/s (the key, ie. addDisclaimer("disciPod"))
	// and it may be called more than once on pageload via JSP, HTML, properties, etc.
	for (var a=0; a < arguments.length; a++) arrDisclaimerKeys.addItems(arguments[a]);
	if (arrDisclaimerKeys.length > 0) arrDisclaimerKeys = arrDisclaimerKeys.getUnique();
 
	loadDisclaimers();
}

function clearDisclaimer()
{ 	
	arrDisclaimerKeys = new Array(), arrDisclaimerData = new Array(), arrLoadedDisclaimerKeys = new Array();
	disclaimerPop = $("<div><div class=\"popDiv popCenter\"><ul><div id=0/><div id=1/><div id=2/><div id=3/><div id=4/><div id=5/><div id=6/><div id=7/><div id=8/><div id=9/></ul></div></div>");
}


function getDisclaimerData(discKey){
	
	for (var i=0; i < arrDisclaimerData.length; i++)
	{
		if( arrDisclaimerData[i].key ==  discKey ) {
			return arrDisclaimerData[i].data;
		}
	}
	
	return '';
}

function showDisclaimer(objName) 
{	
	// Pass in title via showDisclaimer("disciPod","iPod")
	if (disclaimerPop.find("li").html() == null)
	{
		for (var i=0; i < arrLoadedDisclaimerKeys.length; i++)
		{
			var strDisclaimerKey = arrLoadedDisclaimerKeys[i];
			var strDisclaimerData = getDisclaimerData ( strDisclaimerKey );			
			disclaimerPop.find("div#"+i).append($("<li />").append($("<sup />").append(
				$("<span />").html(arrDisclaimerLegend[i]).addClass("discListSymb"))
				).append(strDisclaimerData).addClass(strDisclaimerKey)
			);
			if (pageHeight < 500)
			{
				var charsPerLine = 100;
				var incrHeight = baseHeight = 35;
				var intDataChars = strDisclaimerData.length;
				incrHeight *= parseInt(intDataChars / charsPerLine);
				if (intDataChars % charsPerLine != 0) incrHeight += baseHeight;
				pageHeight += incrHeight;
			}
		}
	}
	
	disclaimerPop.find("li").removeClass("selectedDisc");
	if (objName != "") disclaimerPop.find("li."+ objName).addClass("selectedDisc");
	
	if (arguments.length > 1) pageTitle = arguments[1]+' ';
	else if (objName != "") pageTitle = objName.replace("disc",'').replace(/([a-z])([A-Z])/g,"$1 $2")+' ';
	
	var discWindow = window.open("","DisclaimerWindow","height="+pageHeight+",width=640,scrollbars=1,left=250,top=150,directories=no,titlebar=no,toolbar=no,menubar=no");
	discWindow.document.write('<html><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type" /><style type="text/css">');
	discWindow.document.write(
	    'body { font: 12px Tahoma,Verdana,Arial,Helvetica,sans-serif; color: #333; } '+
	    '#wrap { width: 96%; margin: 0 auto; }&#13; '+
	    '.popDiv {  } '+
	    '.popDiv ul { padding: 0; margin: 0; } '+
	    '.popDiv ul li { border: solid 1px #222; list-style-type: none; padding: 15px; padding-left: 35px; margin-bottom: 7px; font-size: 14px;} '+
	    '.discListSymb { margin-left: -25px; margin-right: 15px; width: 10px; height: 0px; display:inline-block; font-weight: bold; font-size: 100%; vertical-align: top; padding-top: 5px; } '+
	    '.selectedDisc { background-color: #AFDBFF; } '+
		'</style><body>'+
		'<div id="wrap"><div style="text-align: right;font-size: 120%">'+ (new Date().toDateString()) +'</div><br />'+
		'<img style="float:right;height:50px;" src="/MMNA/images/blp/Global_Images/mitsu_logo.jpg" /> <br /> <br />'+
		'<h2>'+ pageTitle +'Disclaimer</h2>'
	);	
	discWindow.document.write(disclaimerPop.html());
	discWindow.document.write("<p>From: "+ document.location.href +"</p></div></body></html>");
	discWindow.document.close();
	discWindow.focus();
}


/*
 * Converts Array to Obj
 */
function arrayConverter(arr)
{
  var objTo = {};
  for(var i=0;i<arr.length;i++)
  {
	  objTo[arr[i]]='';
  }
  return objTo;
}


/*
 * Disclaimer Pop Up Javascript Builder/Controller
 * by Shawn (not completed by Shawn, - Joe)
 * 
 * This should be added to any page with disclaimers (globally) and any disclaimers on that page should
 *  have their symbols wrapped in an anchor with a class of popDisclaimer and their rel attribute set to
 *  the disclaimer key they relate to.  This will allow the disclaimer being clicked to be marked.
 *  
 * Any other triggers to the Disclaimer Page can just have the popDisclaimer class added to them
 * 
 * Eample usage:
 * 
 * <a href="#" class="popDisclaimer"  rel="Owner_Warranty09"   rev="1"  title="Owners Site" >  * </a> 
 * 
 * Here class should be "popDisclaimer" - REQUIRED
 * rel = 'Disclaimer Key' - REQUIRED
 * rev = Order number , the disclaimer should be displayed - OPTIONAL
 * title = using for page title - OPTIONAL
 */


// Initialize flashDisclaimers object, this should be redefined with any disclaimers that appear in Flash on the page
// Form should be "Disclaimer_Key": "Disclaimer Symbol", ie "Owner_Warranty": "&Dagger;"
// These will be added to the end of the disclaimer pop-list
var flashDisclaimers = {};

// This is the function that opens the Pop-Up window and fills in the Disclaimer HTML
function Disclaim(er) {
	// Remove any previously selected disclaimers
	disclaimerPop.find('li').removeClass('selectedDisc');
	
	// If this is a specific disclaimer click, mark it
	if(er.rel) { 
		disclaimerPop.find('li.'+er.rel).addClass('selectedDisc');
	}
	
	// Create Pop-up window... this needs to open a real page (this one doesn't exist and I didn't test it with IE yet) first to pass to, IE 7 gives Access Denied if it isn't passed a real blank page (ie  about:blank)
	var discWindow = window.open("about:blank", "_name", "width=800,height="+pageHeight+",scrollbars=1,screenX=250,screenY=150,directories=no,titlebar=no,status=no,toolbar=no,menubar=no");
	// Write out the new page in to the pop-up window
	discWindow.document.write('<html><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/><style type="text/css">');
	discWindow.document.write(
	    'body { font: 12px Tahoma,Verdana,Arial,Helvetica,sans-serif; color: #333; } '+
	    '#wrap { width: 85%; margin: 0 auto; }&#13; '+
	    '.popDiv {  } '+
	    '.popDiv ul { padding: 0; margin: 0; } '+
	    '.popDiv ul li { border: solid 1px #222; list-style-type: none; padding: 15px; padding-left: 35px; margin-bottom: 7px; font-size: 14px;} '+
	    '.discListSymb { margin-left: -25px; margin-right: 15px; width: 10px; height: 0px; display:inline-block; font-weight: bold; font-size: 100%; vertical-align: top; padding-top: 5px; } '+
	    '.selectedDisc { background-color: #AFDBFF; } '
	);
	
	var today = new Date();
	// Get Page Title, this variable should appear somewhere on the page to indicate the page title, otherwise defaults to a generic
	
	//set the title
	if ( er.title ) {
		pageTitle = er.title;
	}
	
	discWindow.document.write(
		'</style><body>'+
		'<div id="wrap"><div style="text-align: right;font-size: 120%">'+today.toDateString()+'</div><br />'+
		'<img style="float:right;height:50px;" src="/MMNA/images/blp/Global_Images/mitsu_logo.jpg" /> <br /> <br />'+
		'<h2>'+pageTitle+' disclaimer</h2>'
	);
	// Output HTML of object we created earlier
	discWindow.document.write(disclaimerPop.html());
	discWindow.document.write(
		'<p>From: '+document.location.href+'</p>'+
		'</div></body></html>'
	);
	discWindow.document.close();
	discWindow.focus();
}

// This runs after the document is ready
$(function() {
		                  
	// Performs AJAX post to given URL for JSON of disclaimer information
	var check_dup = new Object();  // Create new object to help with removing duplicate disclaimer listings from page
	// Grab each element with the clas popDisclaimer and add any disclaimers it represents to the disclaimer pop list
    $('.popDisclaimer').each(function() {
    	
    	if( this.rel) {
    		
    		// Check if it's a duplicate
	    	if(!(this.rel in check_dup) && this.rel != 'VIEW_ALL' ) {
	    		
	    		if( pageHeight < 550 ) {
	    			pageHeight = pageHeight + 150;
	    		}
	    		
	    		check_dup[this.rel] = true;
	    		
	    		var innerHTML = 'body_key_'+this.rel;
	    		
	    		$.getJSON("/MMNA/legalDisclaimerAction.do?disclaimerKey="+this.rel,function(data)
	     		{
	    			var bodyKey = "body_key_"+data.disclaimerKey;
	    			var htm = disclaimerPop.html();
	    			htm = htm.replace( bodyKey , data.body );
	    			disclaimerPop.html ( htm );
	     		});
	    		
	    		var phText = "ul";
	    		if( this.rev ) {
	    			phText = "div#"+ (this.rev - 1);
	    		} 
	    		
			    disclaimerPop.find(phText).append(
					     $('<li />').append(
					     	$('<sup />')
			            	.append(
					                $('<span />').html(this.innerHTML).addClass('discListSymb')
					        )).append(innerHTML).addClass(this.rel)
			    );  

		    	//set the title
		    	if ( this.title ) {
		    		pageTitle = this.title;
		    	}
	    	}
    	}
    }).click(function() {
    	// On Click, run this function
    	Disclaim(this);
    });
    
    // Add flash disclaimers to list
    $.each(flashDisclaimers, function(discId,discSymb) { 
    	disclaimerPop.find('ul').append(
            $('<li />').append(
            	$('<span />').html(discSymb).addClass('discListSymb')
            ).append(discHash[discId]).addClass(discId)
    	)
    })
});