if (!MY10) {
	var MY10 = { };
}

// global variables
var YUC = YAHOO.util.Connect;
var YUD = YAHOO.util.Dom;
var YUE = YAHOO.util.Event; 
var specs_div = "printable_specs_container";
var pricing_div = "pricing_container_";
var msrp_location = "msrp_price";
var msrp_to_location = "title_msrp";
var currentMeasurement = "us";
var compare_dataDiv = "compareData";

var model;
var language; 
var market; 
var market_type;
var pricing_file_ext;
var dwp;
var dwp_price;

var to_hdrPrintable = "printable_specs_header";
var from_hdrPrintable = "hdrPrintable";

var to_print = "printable_specs_print";
var from_print = "printButtonAlt";

var to_close = "printable_specs_close";
var from_close = "closeButton";

var to_backToTop = "back_top";
var from_backToTop = "backToTopButton";

var to_footer = "printable_specs_footer";
var from_footer = "footer";

/* se/police models */
var policeModels = new Object();
policeModels["frflhp"] = true;
policeModels["frflhtp"] = true;
policeModels["plflhp"] = true;
policeModels["plflhtp"] = true;
policeModels["plxl883l"] = true;
policeModels["plxl883"] = true;

var seModels = new Object();
seModels["ffflhr"] = true;
seModels["ffflhtcu"] = true;
seModels["ffflstc"] = true;
seModels["ffflstf"] = true;
seModels["poflhr"] = true;
seModels["poflhtcu"] = true;
seModels["poflstc"] = true;
seModels["poflstf"] = true;
seModels["srflhr"] = true;
seModels["srflhtcu"] = true;
seModels["srflstc"] = true;
seModels["srflstf"] = true;

MY10.printableSpecs = {	
	
	
	/**
	 * This method will make two AJAX request to get the specs file and the pricing file. The  
	 * two files will then be inserted in to the container page to create the printable specs
	 * page
	 * 
	 * @method getData
	 * @param  {String} model: bike model
	 * @param  {String} language: language to be used
	 * @param  {String} country: country used for pricing
	 * @param  {String} market: market used for specific specs
	 */
	getData : function (param_model, param_language, param_market, parm_market_type, param_pricing_file, param_dwp, parm_dwp_price) {		
		
		// set global variables
		model       = param_model;
		language    = param_language;
		market      = param_market;
		market_type = parm_market_type;				
		pricing_div += param_model;
		pricing_file_ext = param_pricing_file;
		dwp = param_dwp;
		dwp_price = parm_dwp_price;
		
		// variables
		var specsFilename;
		var pricingFilename;
		var dataFilename;			
		
		var isSeModel = seModels[model];
		var isPoliceModel = policeModels[model];
		
		// create data file name
		if(language && market && market_type)
			dataFilename = "/en_US/Media/data/Apps/2010_Motorcycles/specs/config_" + language + "_" + market_type + ".webxml";
						
		if(dataFilename)
			YUC.asyncRequest('GET', dataFilename, getDataCallback);
		
		if(model && language && market_type)
			specsFilename = "/en_US/Media/data/Apps/2010_Motorcycles/specs/specs_" + model + "_" + language +  "_" + market_type + ".webxml";		
		
		if(isSeModel && model){						
			var subModel = model.substring(2);					
			pricingFilename = "/en_US/Media/data/Apps/2010_Motorcycles/pricing/pricing_" + subModel + "_en_default.webxml";
		}			
		else if(model && language && market){			
			// check to see if we are are in the DWP
			// app and whether we should display price
			if(dwp && !dwp_price && market == "us")
				pricingFilename = "/en_US/Media/data/Apps/2010_Motorcycles/pricing/pricing_" + model + "_" + language +  "_default.webxml";
			else
				pricingFilename = "/en_US/Media/data/Apps/2010_Motorcycles/pricing/pricing_" + model + "_" + language +  "_" + pricing_file_ext + ".webxml";			
		}		
			
		// get the specs file, and run getSpecsCallback method
		if(specsFilename)
			YUC.asyncRequest('GET', specsFilename, getSpecsCallback);
		
		// get the pricing file and run getPricingCallback method
		if(pricingFilename && !isPoliceModel)
			YUC.asyncRequest('GET', pricingFilename, getPricingCallback);
			
		// write MY10 main header
		YUE.onContentReady(from_hdrPrintable, function(){
			MY10.printableSpecs.writeSimpleToFrom(to_hdrPrintable, from_hdrPrintable);	
		});		
		
		// write print button
		YUE.onContentReady(from_print, function(){
			MY10.printableSpecs.writeSimpleToFrom(to_print, from_print);	
			YUE.onContentReady(to_print, function(){
				YUE.addListener(to_print,'click',function(){					
					window.print();
				});		
			});	
		});	
		
		// write close button
		YUE.onContentReady(from_close, function(){
			MY10.printableSpecs.writeSimpleToFrom(to_close, from_close);
			YUE.onContentReady(to_close, function(){
				YUE.addListener(to_close,'click',function(){					
					window.close();
				});		
			});		
		});	
		
		// write backToTop
		YUE.onContentReady(from_backToTop, function(){
			YUE.onContentReady(to_backToTop, function(){
				MY10.printableSpecs.writeSimpleToFrom(to_backToTop, from_backToTop);
			});	
		});		
		
		// write footer
		YUE.onContentReady(from_footer, function(){
			MY10.printableSpecs.writeSimpleToFrom(to_footer, from_footer);	
		});								
		
	}, // end getData	
	
	/**
	 * This method will get the MSRP that is in the pricing xml that gets written to the page
	 * and write that value in the MSRP field at the top of the page.
	 *  
	 * @method writeMsrp
	 * @param  {String} locationFromId: id of div that contains the MSRP value from pricing xml
	 * @param  {String} locationToId: id of div that we will write the MSRP to
	 */
	writeMsrp: function(locationFromId, locationToId){
		
		if(YUD.get(locationFromId) && YUD.get(locationToId)){
			var msrpValue = YUD.get(locationFromId).innerHTML;
			YUD.get(locationToId).innerHTML = "* " + msrpValue;			
		}			
	
	}, // end writeMsrp	
	
	/**
	 * This method will write the show english or show metric button to the page.
	 * Along with writing the button a onclick event will be attached to switch
	 * between the english and metric units. Currently this is only used in the
	 * country us.
	 *  
	 * @method writeEnglishMetricButton
	 * @param  {String} writeLocation: the location we will be writing the button to
	 * @param  {String} country: country
	 */
	writeEnglishMetricButton: function(writeLocation, units){
		
		if("us" == market){			
			var elDiv = document.createElement("div");
			elDiv.id = "switchMeasurement";	
			elDiv.className = ("us" != units && (units)) ? "switchMeasurementUs" : "switchMeasurementMet";	
			YUD.insertAfter(elDiv, writeLocation);				
			YUE.onContentReady("switchMeasurement", function(){		
				YUE.addListener('switchMeasurement','click',function(){
					MY10.printableSpecs.switchMeasurement("printable_specs_container", "switchMeasurement", currentMeasurement);
				});
			});							
		}		
	
	}, // end writeEnglishMetricButton	
	
	/**
	 * This method will look at the market, if it is not DOM it will set the view
	 * to metric if it is dom set the view to english
	 * 
	 * @method isEnglishMetric
	 * @param  {String} id: id of div to set correct class to
	 * @param  {String} market: market used to determine if metric or english is displayed
	 */
	isEnglishMetric: function(id, units){	
		
		if(units && YUD.get(id) ) {
			
			if("us" != units){
				YUD.replaceClass(YUD.get(id), 'english', 'metric');
				currentMeasurement = "notDom";
			}
			else{
				YUD.replaceClass(YUD.get(id), 'metric', 'english');
				currentMeasurement = "dom";				
			}
			
		}
		else if(YUD.get(id)){			
			
			if ("us" != market) {
				YUD.replaceClass(YUD.get(id), 'english', 'metric');
				currentMeasurement = "notDom";
			}
			else {
				YUD.replaceClass(YUD.get(id), 'metric', 'english');
				currentMeasurement = "dom";
			}				
						
		}								
	
	}, // end isEnglishMetric
	
	/**
	 * This method will look at the market, if it is DOM it will set the view
	 * to metric if it is not dom set the view to english
	 * 
	 * @method switchMeasurement
	 * @param  {String} id: id of the element that controls the english/metric displays via CSS
	 * @param  {String} elId: id of the button that is used to switch between english/metric
	 * @param  {String} market: market
	 */
	switchMeasurement: function(id, elId) {			
	
		if (YAHOO.env.ua.ie)	
		{			
			var currentURL = window.location.href;
			var urlArray = currentURL.split("?");
					
			var params = "?model=" + model;
			params += "&lang=" + language;
			params += "&market=" + market;
			params += "&market_type=" + market_type;
			params += "&pricingExt=" + pricing_file_ext;
			params += "&dwp=" + dwp;
			params += "&dwp_price=" + dwp_price;			
			
			if("dom" == currentMeasurement){
				params += "&units=metric";
				currentMeasurement = "notDom";
			}				
			else{
				params += "&units=us";
				currentMeasurement = "dom";
			}				
				
			window.location.href = urlArray[0] + params;
			
		}
		else {
			if ("dom" == currentMeasurement) {
				YUD.replaceClass(YUD.get(id), 'english', 'metric');
				YUD.replaceClass(YUD.get(elId), 'switchMeasurementMet', 'switchMeasurementUs');
				currentMeasurement = "notDom";
			}
			else {
				YUD.replaceClass(YUD.get(id), 'metric', 'english');
				YUD.replaceClass(YUD.get(elId), 'switchMeasurementUs', 'switchMeasurementMet');
				currentMeasurement = "dom";
			}			
		}	
	
	}, // end switchMeasurements
	
	/**
	 * This method will write the units measurement whether it is
	 * us units or metric units
	 * 
	 * @method writeUnitsLabel	
	 */
	writeUnitsLabel: function(){
		
		// get the node to insert our new node after		
		var h2Els = YUD.getElementsByClassName("label", "h2", "dimensions");			
		
		var englishUnits = "&nbsp;";
		var metricUnits = "&nbsp;";		
		
		// get the meta data content if available
		if(YUD.get('unitsLabelEnglish'))
			englishUnits = YUD.get('unitsLabelEnglish').content;
		if(YUD.get('metricUnits'))
			metricUnits = YUD.get('unitsLabelMetric').content;			
		
		// create the element to insert
		var newEl = document.createElement("h2");			
		var elInnerHTML = "<span class='us_units'>" + englishUnits + "</span><span class='metric_units'>" + metricUnits + "</span>" 
		newEl.innerHTML = elInnerHTML;
		newEl.className = "units_label";
		
		// insert the element			
		YUD.insertAfter(newEl,h2Els[0]);		
		
	},
	
	/**
	 * This method will attach a onclick event to the back to top button, to anchor
	 * to the top of the page.
	 * 
	 * @method writeBackToTopButton
	 * @param  {String} id: id of the element that will get the onclick event
	 * @param  {String} country: country
	 */
	writeBackToTopButton: function(id){			
		YUE.onContentReady(id, function(){					
			var el = YUD.get(id);						
			YUE.addListener(el,'click',function(){					
				window.scrollTo(0,0);
			});	
		});		
	}, // end writeBackToTopButton	
	
	/**
	 * This method will get the innerHTML from the fromLocation
	 * and place it in the toLocation
	 * 
	 * @method writeSimpleToFrom
	 * @param  {String} toLocation: id where to write to
	 * @param  {String} fromLocation: id where to get the data from
	 */
	writeSimpleToFrom : function (toLocation, fromLocation) {
		var toEl = YUD.get(toLocation);
		var fromEl = YUD.get(fromLocation);
				
		if(fromEl){		
			toEl.innerHTML = fromEl.innerHTML;
		}
							
	} // end writeSimpleToFrom
	
};


/**
 * callback method for getting the specs data file and writing
 * it into the container page
 */
var getSpecsCallback = {	
	success: function(o){
		// write specs file into the container page
		YUD.get(specs_div).innerHTML = o.responseText;				
	},
	failure: function(o){
		// set failure logic if needed	
	}	
};

/**
 * callback method for getting the pricing data file and writing
 * it into the container page
 */
var getPricingCallback = {	
	success: function(o){
		
		// wait until the pricing_div is ready, which is in the specs data return that is
		// written into the page via the getSpecsCallback method, once ready write the 
		// pricing file into the page.
		YUE.onContentReady(pricing_div, function(){		
			YUD.get(pricing_div).innerHTML = o.responseText;
		});
		
		YUE.onContentReady(msrp_location, function(){		
			MY10.printableSpecs.writeMsrp(msrp_location, msrp_to_location);
		});		
				
	},
	failure: function(o){
		// set failure logic if needed		
	}	
};

/**
 * callback method for getting the compare data file and writing
 * it into the container page
 */
var getDataCallback = {	
	success: function(o){
		// write specs file into the container page
		YUD.get(compare_dataDiv).innerHTML = o.responseText;				
	},
	failure: function(o){
		// set failure logic if needed	
	}	
};



