﻿/// <reference path="../../../../../jQuery/1.3.2/jquery-1.3.2-vsdoc.js" />

/**
*
* StockListFeature
* 
*/

    if (!mrm.global.isNamespaceDefined("mrm.common.display.views")) mrm.global.createNamespace("mrm.common.display.views", "1.0");

        mrm.common.display.views.StockListFeature = Object.subClass(
	    	{
	    	    /*
	    	    =============================
	    	    CONSTANTS
	    	    =============================
	    	    */
	    	    SELECTOR_FORM_MP_TOOLS				: "form.mp-tools-form",
	    	    SELECTOR_CONTAINER_STOCKLIST		: "div.vehicle-list-container",//"#divVehicleListContainer",
	    	    
	    	    CLASS_DISABLED						: "display-disabled",
	    	    
	    	    ATTRIBUTE_ACTION					: "action",
	    	    
	    	    BASE_ACTION_URL						: "/Handlers/Results.ashx?wflw=",
	    	    BASE_ACTION_URL_SUFFIX				: "&pg=",
	    	    
	    	    DEBUG : false,
	    	    
	    	    jqStockListContainer				: null,
	    	    jqFormMPTools						: null,
	    	    
	    	    
	    	    /*
	    	    =============================
	    	    CONSTRUCTOR
	    	    =============================
	    	    */
	    	    init : function()
	    	    {
	    	         // init jq containers
	    	         this.jqStockListContainer = $(document).find(this.SELECTOR_CONTAINER_STOCKLIST);
	    	         this.jqFormMPTools = $(document).find(this.SELECTOR_FORM_MP_TOOLS);
	    	         
	    	         var mpToolsFormAction = this._workflowKey(this.jqFormMPTools[0]);
	    	         
	    	         this.requestUrl = this.BASE_ACTION_URL + mpToolsFormAction + this.BASE_ACTION_URL_SUFFIX;
	    	         
	    	         //setup this class
	    	         this._setup();
	    	    },

	    	    /*
	    	    =============================
	    	    INTERNAL / PRIVATE METHODS
	    	    =============================
	    	    */
	    	    
	    	    callbackUtilities : {},
	    	       	    
	    	        /*
	    	        =============================
	    	        SETUP
	    	        =============================
	    	        */	
	    	        
	    	        _setup : function()
	    	        {
	    	            this._setupSortOrderHtmlList();

	    	            this._bindPagingCallbacks(); 
	    	            
	    	            this._bindDisplayModeImageControl();
	    	        },
					
					_workflowKey : function(formElement)
					{
						return mrm.global.utilities.workflow.getScopedWorkflowKey(formElement);
					},
					
	    	        _bindAjaxWorkflowLink: function(container)
	    	        {
	    	        	this._debugMessage("Start bind ajax workflow links");
	    	        	
	    	        	//makes a call to the global JS used to power the ajax enabled workflow links
	    				mrm.common.display.ajaxConditionalLinkManager.bindAllAjaxConditionalLinks(container);
	    				
	    				this._debugMessage("End bind ajax workflow links");
	    	        },
    				
    				_bindDisplayModeImageControl : function(container)
					{
						if (container == null)
						{
							container = $(document).find('div.vehicle-list-container form.vehicle-list-header fieldset');
						}
						
						container.find('img').click(function()
						{      
							//update the input that holds the viewmode value
							var container = $(this).parents('fieldset.imagegroup');
							var input = container.find('input:first');
							var displaymode = $(this).hasClass('photoflow') ? "photoflow" : "list";
					        
							if(input.val() != displaymode)
							{
								container.removeClass(input.val()).addClass(displaymode);
								input.val(displaymode);                
								$(this).parents('form.mp-tools-form').submit();
							}
						});
					
					},
    				
	    	        _bindPagingCallbacks : function ()
	    	        {
	    	            this._debugMessage("Start bind paging links");
	    	            var _this = this;
	    	            
	    	            this._debugMessage("[Paging links bind] stock list container count = " + this.jqStockListContainer.length);
	    	            
	    	            //bind up the callbacks used for the paging links
	    	            this.jqStockListContainer.find('.menu-results-pagination a').click(function(event) {
	    	                //stop the default click through
	    	                event.preventDefault();

	    	                // Display loading state for htmlListItems
	    	                _this._displayLoadingState();
							
	    	                $.ajaxManager.updateUrl($(this).attr('href'));
	    	                $.ajaxManager.onSuccess = function(p_response) {
	    	                    _this._processUpdatedDisplayData(p_response);
	    	                };
	    	                $.ajaxManager.makeRequest();
	    	            });
	    	            
	    	           this._debugMessage("End bind paging links");
	    	        },
	    	        
	    	        _bindToolTips : function(container)
					{
						BindToolTips(container);
					},
	    	        
	    	        _setupSortOrderHtmlList : function()
	    	        {
	    	            this._debugMessage("Start bind order html list");
	    	            var _this = this;
						$(document).find('div.vehicle-list-container form.vehicle-list-header fieldset div.htmllist-container').each(function() {
	    	                var htmlListContainer = $(this);
	    	                var htmlListUiContainer = htmlListContainer.find('div.ui-htmllist-container');
	    	                htmlListUiContainer.htmllist({
	    	                    delay: 1000,
	    	                    onChange: function(event, dataItem) {    	                           	                        
	    	                        // Display loading state for htmlListItems
	    	                        _this._displayLoadingState();
	    	                        
	    	                        $.ajaxManager.updateUrl(_this.requestUrl + 1 + '&srt=' + dataItem.value);
	    	                        $.ajaxManager.onSuccess = function(p_response) {
	    	                            _this._processUpdatedDisplayData(p_response);
	    	                        };
	    	                        $.ajaxManager.makeRequest();
	    	                    }
	    	                });
	    	            });
	    	            
	    	            this._debugMessage("End bind order html list");
	    	        },
	    	        
	    	        /*
				     =============================
				     DISPLAY CONTROL
				     =============================
				     */
				      _displayLoadingState: function()
				      {
				        this._debugMessage("Start display loading state.");
				        var _this = this;
				        
				        //if(!$.browser.msie && $.browser.version > 6)
				        //{
							this.jqStockListContainer.addClass(this.CLASS_DISABLED);
						//}
						
						this._debugMessage("End display loading state.");
				     },
				     
					_removeLoadingState : function ()
					{
						this._debugMessage("Start remove loading state class");
						var _this = this;
						
						//if(!$.browser.msie && $.browser.version > 6)
				        //{
							
							this.jqStockListContainer.removeClass(this.CLASS_DISABLED);
							
						//}
						
						this._debugMessage("Finished remove loading state class");
					},
				    
//				    _processPNGFix : function(jqContainer)
//					{
//						if (fix_PNGs && $.browser.msie && $.browser.version < 7)
//						{
//							fix_PNGs(jqContainer);
//						}
//					},
				     
					_processUpdatedDisplayData : function (data)
				    {
				        
				        var _this = this;
				        
				        // replace html with returned controls
				        this.callbackUtilities.processResultControls(data);
				        
				        this._debugMessage("Start bespoke control rendering for paged list result list.");
				        
				        this._debugMessage("Scope = " + _this + ".");
				        
				        //rebind the newly creating paging controls
				        _this._bindPagingCallbacks();
				        
				        //rebind the ajax workflow links						
				        var pagedDataControl = this.callbackUtilities.findResultDataControl(data,"PagedListResultsList");
				        if (pagedDataControl != null)
				        {
							jqPagedDataControl = $(pagedDataControl.targetDomId);
							
							//this._processPNGFix(jqPagedDataControl);
							
							if (jqPagedDataControl.length > 0)
							{
								//this._bindAjaxWorkflowLink(jqPagedDataControl);
								jqPagedDataControl.trigger(mrm.global.utilities.defaultControlBinder.MRM_RUN_INSTANCE, ["CompareCallBackLinks", jqPagedDataControl]);
								jqPagedDataControl.trigger(mrm.global.utilities.defaultControlBinder.MRM_RUN_INSTANCE, ["FavouritesCallBackLinks", jqPagedDataControl]);
								$(document).trigger("setupCo2CTAs");
							}
						}
				        
				        //rebind the sIFR
				        //sIFR.runDelayedStyling(sIFR.styles.STYLE_SEARCH_RESULTS_LIST_MODE);
				        
				        //rebind sIFR result count
						var pagedHeaderControl = this.callbackUtilities.findResultDataControl(data,"PagedListHeader");
				        if (sIFR && sIFR.styleResultsListResultsCount && pagedHeaderControl != null)
				        {
							// remove sIFR class, TODO look at replacing entire h3 instead
							$(pagedHeaderControl.targetDomId).removeClass("sIFR-replaced");
							
							sIFR.styleResultsListResultsCount();
						}
						
						//rebind the tooptips
						//this._bindToolTips(this.jqStockListContainer);
						
						this._removeLoadingState();
						
						this._debugMessage("Finished bespoke control rendering for paged list result list.");
					},
			     
			     
			     _debugMessage : function(message)
			     {
					if (this.DEBUG)
					{
						alert("[StockListFeature] " + message);
					}
			     }
			     
		    }
	    );
