if (!MY10) {
	var MY10 = { };
}

// global variables
var YUC = YAHOO.util.Connect;
var YUD = YAHOO.util.Dom; 
var YUE = YAHOO.util.Event;

var dataContainer = "dataContainer";
var pricing_div = "pricing_container_";
var modelOne_pricingDiv;
var modelTwo_pricingDiv;
var modelThree_pricingDiv;
var model;
var modelTwo;
var modelThree;
var writePos;
var writeModel;
var compare_dataDiv = "compareData";
var categoriesId = "catSelect";
var catArray = new Array("cmp_dimension_container", "cmp_powertrain_container", "cmp_wheel_container", "cmp_electrical_container",
					"cmp_chassis_container","cmp_color_container","cmp_pricing_container");

// This model will be used to generate the labels
// it is a model that is available in every locale/market
// combination
var emptyModel = "xl883n"; 
var isReset; // used to determine if we will be resting the data to init state

// div insided the compareData XML which contains the model groups
// used to write select boxes
var getModelSelectDiv = "model_list";

var to_hdrCompare = "compare_specs_selector_header";
var from_hdrCompare = "hdrCompareHdr";

var to_categories = "compare_specs_categories";
var from_categories = "categories";

var to_hdrCompareText = "compare_specs_selector_text";
var from_hdrCompareText = "hdrCompareText";

var to_print = "compare_specs_print";
var from_print = "printButton";

var to_back = "compare_specs_back";
var from_back = "backButton";

// overlay 
var loaderOpen = false;

MY10.compareSpecs = {	
		
	/**
	 * This method will 
	 * 
	 * @method init
	 * @param  {String} model: bike model	
	 */
	init: function(parm_model){		
		
		// set the model
		model = parm_model;	
		
		// create data file name
		if (language && 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){
			MY10.compareSpecs.initLabels();
			MY10.compareSpecs.initGetModelData(model,1);						
		}
		else {
			MY10.compareSpecs.initLabels();						
		}	
		
		if(YUD.get(my10CompareContainer) && locale){
			YUD.addClass(my10CompareContainer, locale);
		}						
		
		/*
		// write print button		
		YUE.onContentReady(from_print, function(){
			MY10.compareSpecs.dataMap.writeSimpleToFrom(to_print, from_print);
			YUE.onContentReady(to_print, function(){
				YUE.addListener(to_print,'click',function(){					
					window.print();
				});		
			});	
		});	
		*/
		
		// write back button
		YUE.onContentReady(from_back, function(){
			MY10.compareSpecs.dataMap.writeSimpleToFrom(to_back, from_back);	
			YUE.onContentReady(to_back, function(){
				YUE.addListener(to_back,'click',function(){					
					//MY10.closeCompareSpecs();					
					SWFAddress.back();
				});		
			});	
		});	
		
		// write compare section main header
		YUE.onContentReady(from_hdrCompare, function(){
			MY10.compareSpecs.dataMap.writeSimpleToFrom(to_hdrCompare, from_hdrCompare);	
		});	
		
		// write compare section main header
		YUE.onContentReady(from_hdrCompareText, function(){
			MY10.compareSpecs.dataMap.writeSimpleToFrom(to_hdrCompareText, from_hdrCompareText);	
		});			
		
		// write select boxes
		YUE.onContentReady(getModelSelectDiv, function(){
			MY10.compareSpecs.writeSelectBoxes(model);	
		});		
		
		// write categories boxes
		YUE.onContentReady(from_categories, function(){
			MY10.compareSpecs.dataMap.writeSimpleToFrom(to_categories, from_categories);
			MY10.compareSpecs.initCategories();	
		});														
	
	}, // end init
	
	/**
	 * This method will write the model data to the table 
	 * on init if a model paramater was sent via url 
	 * params.
	 * 
	 * @method initGetModelData
	 * @param  {String} model: bike model
	 * @param  {String} pos: column position
	 * @param  {String} reset: are we reseting the data to init state
	 */
	initGetModelData : function (model, pos, reset){	
			
		// we will check if the labels are still getting written
		// if so we will pause a moment and try again, two sets
		// of data cannot be written at the same time due to possible
		// html ID conflicts from the xml				
		if(!writeInProgress){			
			// set write in progress
			writeInProgress = true;			
			MY10.compareSpecs.getModelData(model, pos, reset);
		}			
		else{		
			setTimeout(function(){MY10.compareSpecs.initGetModelData(model, pos, reset);}, 500);
		}
			
	}, // end initGetModelData
	
	/**
	 * This method will get the compare data that was written to the page earlier
	 * and find the select box within that, and write that select box to its
	 * correct position in the page.
	 * 
	 * @method writeSelectBoxes	 
	 */
	writeSelectBoxes: function(){			
		
		// make sure the compare data is written to the page
		if(YUD.get(getModelSelectDiv)){			
			
			var modelGroupContainer = YUD.get(getModelSelectDiv);			
			var selectContainerIdsArray = ["modelSelectorContainerOne","modelSelectorContainerTwo","modelSelectorContainerThree"];
			var selectIdsArray = ["modelOneSelector","modelTwoSelector","modelThreeSelector"];		
			var selectData;
			var selectBox;
			
			// get each select box container and innerHTML the select box to it, while adding
			// a specific ID to that select box as well
			for(var x=0; x < selectContainerIdsArray.length; x++){				
				selectData = '<img class="selectNumber" src="/en_US/Media/images/Apps/2010_Motorcycles/specs/img_' + (x+1) + '_select.gif"/>';
				selectData += modelGroupContainer.innerHTML;								
				YUD.get(selectContainerIdsArray[x]).innerHTML = selectData;	
				selectBox = YUD.getElementsBy(function(){return true;}, "select", selectContainerIdsArray[x]);
				selectBox[0].id = selectIdsArray[x];										
			}					
			
			// if there is a model passed in we will attempt
			// to select that model in the select element
			if(model){				
				// get the select box
				var selectEl = YUD.get("modelOneSelector");				
				var selectEls = selectEl.options;	
							
				// select the correct model in the select box
				for (var x = 0; x < selectEls.length; x++) {					
					if (model == selectEls[x].value) {	
						selectEl.selectedIndex = selectEls[x].index;
						break;
					}
				}				
								
			} // end init selected element				
			
			// attach the onchange listeners		
			YUE.addListener(selectIdsArray[0], 'change', function(){				
				if ("empty" == this.options[this.selectedIndex].value) {
					MY10.compareSpecs.getModelData(emptyModel, 1, true);
					model = ""; // set the active model
				}
				else {
					MY10.compareSpecs.getModelData(this.options[this.selectedIndex].value, 1);
					model = this.options[this.selectedIndex].value; // set the active model
				}										
			});	
				
			YUE.addListener(selectIdsArray[1], 'change', function(){
				if ("empty" == this.options[this.selectedIndex].value) {
					MY10.compareSpecs.getModelData(emptyModel, 2, true);
					modelTwo = ""; // set the active modelTwo
				}
				else {
					MY10.compareSpecs.getModelData(this.options[this.selectedIndex].value, 2);
					modelTwo = this.options[this.selectedIndex].value; // set the active modelTwo
				}			
			});
			
			YUE.addListener(selectIdsArray[2], 'change', function(){
				if ("empty" == this.options[this.selectedIndex].value) {
					MY10.compareSpecs.getModelData(emptyModel, 3, true);
					modelThree = ""; // set the active modelThree
				}
				else {
					MY10.compareSpecs.getModelData(this.options[this.selectedIndex].value, 3);
					modelThree = this.options[this.selectedIndex].value; // set the active modelThree
				}			
			});				
				
		}
	
	}, // end writeSelectBoxes
	
	/**
	 * This method will get the empty xml file and write out all the labels 
	 * on DOM ready
	 * 
	 * @method initLabels	
	 */
	initLabels: function() {	
		
		// set write in progress
		writeInProgress = true;
		
		var labelSpecsFileName;
		var labelPricingFileName;			
		
		if(market && language){
			labelSpecsFileName   = "/en_US/Media/data/Apps/2010_Motorcycles/specs/specs_" + emptyModel + "_" +  language + "_" + market_type + ".webxml";
			labelPricingFileName = "/en_US/Media/data/Apps/2010_Motorcycles/pricing/pricing_" + emptyModel + "_" +  language + "_" + pricing_file_ext + ".webxml";			
		}	
		
		if(labelSpecsFileName && labelPricingFileName){
			YUC.asyncRequest('GET', labelSpecsFileName, getSpecsLabelCallback);
			YUC.asyncRequest('GET', labelPricingFileName, getPricingLabelCallback);
		}		
						
	}, // end initLabels		
	
	/**
	 * This method will get the xml file containing a specific bikes
	 * data, and innerHTML that data to the page
	 * 
	 * @method getModelData
	 * @param  {String} model: bike model
	 * @param  {String} modelPos: column to write the data to
	 * @param  {String} reset: are we reseting the data to init state
	 */
	getModelData: function(model, modelPos, reset){
		
		// are we initing the data?
		isReset = reset;
		
		// variables
		var specsFilename;
		var pricingFilename;
		
		// set global variables
		writePos = modelPos;
		writeModel = model;
			
		if(model && language && market)
			specsFilename = "/en_US/Media/data/Apps/2010_Motorcycles/specs/specs_" + model + "_" + language +  "_" + market_type + ".webxml";
			
		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)
			YUC.asyncRequest('GET', pricingFilename, getPricingCallback);					
	},
	
	initCategories: function(){
		var elCat = YUD.get(categoriesId);		
		if (elCat) {
			YUE.addListener(elCat, 'change', function(){
				
				var elSelected = this.options[this.selectedIndex].value;												
				
				if("showall" == elSelected){
					for(var x=0; x<catArray.length;x++){
						YUD.setStyle(YUD.get(catArray[x]), "display", "block");
					} 
				}
				else {
					for(var x=0; x<catArray.length;x++){						
						if(elSelected == catArray[x])
							YUD.setStyle(YUD.get(catArray[x]), "display", "block");	
						else					
							YUD.setStyle(YUD.get(catArray[x]), "display", "none");						
					} 					
				}				
				
			}); // end function
		} // end if		
	}, // end initCategories
	
	/**
	 * 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	 
	 */
	writeBackToTopButton: function(id){			
		YUE.onContentReady(id, function(){					
			var el = YUD.get(id);		
			var imgEls = YUD.getElementsBy(function(){return true;}, "img", el);							
			YUE.addListener(imgEls[0],'click',function(){					
				window.scrollTo(0,0);
			});						
		});		
	}, // end writeBackToTopButton	
	
	/**
	 * This method will reset all the bikes data in the page
	 * 
	 * @method resetPageData	 
	 */
	resetPageData: function(){					
		MY10.compareSpecs.initGetModelData(emptyModel,1, true);		
		MY10.compareSpecs.initGetModelData(emptyModel,2, true);		
		MY10.compareSpecs.initGetModelData(emptyModel,3, true);	
	} // end resetPageData		
	
};

/**
 * callback method for getting the model specs label file and writing
 * it into the data container on the page
 */
var getSpecsLabelCallback = {	
	success: function(o){
		// write specs file into the container page
		if(YUD.get(dataContainer)){
			YUD.get(dataContainer).innerHTML = o.responseText;					
		}					
	},
	failure: function(o){
		// set failure logic if needed	
	}	
};

/**
 * callback method for getting the model pricing label file and writing
 * it into its correct place within the model specs label data.
 */
var getPricingLabelCallback = {	
	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.
		var writeToLocation = pricing_div + emptyModel;
		YUE.onContentReady(writeToLocation, function(){		
			YUD.get(writeToLocation).innerHTML = o.responseText;		
			MY10.compareSpecs.dataMap.writeLabels();
		});	
	},
	failure: function(o){
		// set failure logic if needed		
	}	
};

/**
 * callback method for getting the model specs file and writing
 * it into the data container on the page
 */
var getSpecsCallback = {	
	success: function(o){		
		YUD.get(dataContainer).innerHTML = o.responseText;								
	},
	failure: function(o){
		// set failure logic if needed	
	}
};

/**
 * callback method for getting the model pricing file and writing
 * it into its correct place within the model specs data.
 */
var getPricingCallback = {	
	success: function(o){	
		var writeToLocation = pricing_div + writeModel;
		YUE.onContentReady(writeToLocation, function(){				
			YUD.get(writeToLocation).innerHTML = o.responseText;					
			MY10.compareSpecs.dataMap.writeModelData(writeModel, writePos, isReset);					
		});		
	},
	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	
	}	
};
