/**
 *	If the YAHOO.Edmunds object is undefined, define it
 *	@access	public
 *	@Singleton
 *	@static
 */
if(typeof YAHOO.Edmunds == 'undefined') {
    YAHOO.Edmunds = {};
}


/**
 *	VehicleInfo class can be used as a helper to create URL Links for Left Nav and Nav Tabs inside Model Year Level pages,
 *  based on slyleId value from edmunds cookie.
 *	@requires	YAHOO, YAHOO.Edmunds, YAHOO.util.Connect
 *	@class		YAHOO.Edmunds.ModelYearNavInfo
 *	@param		string	baseStyleId		   Base StyleId
 *	@param		string  baseModelId 	   Base ModelId
 *  @param      string  currentPage        Current Page name
 *  @param      string  useInLabel         UseInLabel
 *	@return		void
 *	@constructor
 */
YAHOO.Edmunds.ModelYearNavInfo = function(baseStyleId, baseModelId, currentPage, useInLabel) {
    this.baseStyleId = baseStyleId;
    this.baseModelId = baseModelId;
    this.currentPage = currentPage;
    this.useInLabel = useInLabel;
    this.styleId = "";
};

/**
 * @property string Class version
 * @static
 */
YAHOO.Edmunds.ModelYearNavInfo.VERSION = '0.0.1';

/**
 *	Defining public methods for the ModelYearNavInfo class
 *	@access	public
 *	@class	YAHOO.Edmunds.ModelYearNavInfo
 */
YAHOO.Edmunds.ModelYearNavInfo.prototype = {

    /**
     *	Builds Url Links for Left Nav side and Quick Quote Nav tab inside Model Year Pages
     *	@method renderConfiguration
     *	@param	void
     *	@return	void
     */
    renderConfiguration: function() {

        if(typeof YAHOO.Edmunds.UserTrack != 'undefined') {
            var selectedStyleId = YAHOO.Edmunds.UserTrack.getCookieValue(this.baseModelId);
            if(selectedStyleId && selectedStyleId != this.baseStyleId) {

                this.styleId = selectedStyleId;

                this.replaceStyleId('pricingnavtabid');
                this.replaceStyleId('reviewsnavtabid');
                this.replaceStyleId('quickquotenavid');
                this.replaceStyleId('vdleftyearlevelbox1');
                this.replaceStyleId('vdleftyearlevelpdplink');
                this.replaceStyleId('vdleftyearlevelbox7');
                this.replaceStyleId('vdleftyearlevelbox6');
                this.replaceStyleId('vdleftyearlevelelplink');
                this.replaceStyleId('vdleftyearlevelbox2');
                this.replaceStyleId('vdleftyearlevelbox4');

                if(document.getElementById('vdleftyearlevelbox7')) {
                    this.setAjaxCall(selectedStyleId);
                }
            }
        }
    },

    /**
     *	Helps to replace styleId value inside href attribute of <A> tag to the new value from the cookie.
     *	@method replaceStyleId
     *	@param	string iD Value of ID attribute of <A> tag
     *	@return	void
     */
    replaceStyleId: function(iD) {
        var par = document.getElementById(iD);
        if(par) {
            var a = par.getElementsByTagName('a');
            var cnt = a.length;
            var link_src;
            var new_src;
            for(var i = 0; i < cnt; i++) {
                link_src = a[i].href;
                if(link_src.indexOf(this.useInLabel) != -1) {
                    //new_src = link_src.replace(/\/([\d]+)\/([\w.]+)$/, '/' + this.styleId + '/$2');
                    new_src = link_src.replace(/\/([\d]+)\/([^\/]+)$/, '/' + this.styleId + '/$2');
                    a[i].href = new_src;
                }
            }
        }
    },


    /**
     *	Makes AJAX call in order to build JSON Data set based on selected styleId from cookie
     *	@method setAjaxCall
     *	@param	string	styleId	 style id from cookie
     *	@return	void
     */
    setAjaxCall: function(styleId) {
        var postUrl = this.currentPage + '.' + styleId + '.modelyearnavjsondataset.html';

        var request = YAHOO.util.Connect.asyncRequest('GET', postUrl, {success:this.handleSuccessOnDropdown,
            failure:this.handleFailureOnDropdown, scope:this});
    },

    /**
     *	Failure callback AJAX handler.
     *	@method handleFailureOnDropdown
     *	@param	object o  object which represents AJASX response
     *	@return	void
     */
    handleFailureOnDropdown: function(o) {

    },

    /**
     *	Success callback AJAX handler.
     *	@method handleSuccessOnDropdown
     *	@param	object o  object which represents AJASX response
     *	@return	void
     */
    handleSuccessOnDropdown: function(o) {
        if(o.responseText != undefined) {
            var styleNameFromAJAX = eval(o.responseText);
            if(styleNameFromAJAX) {
                this.replaceStyleName('vdleftyearlevelbox7', this.encodeURL(styleNameFromAJAX[0]));
            }

        }
    },

    /**
     *	Encodes JSON Data set result value.
     *	@method encodeURL
     *	@param	string str2Encode  Style Name
     *	@return	string Encoded Style Name
     */
    encodeURL: function(str2Encode) {
        return str2Encode.replace(/\s/g, "+");
    },

    /**
     *	Helps to replace style name value inside href attribute of <A> tag to the new value from AJAX response.
     *	@method replaceStyleName
     *	@param	string iD Value of ID attribute of <A> tag
     * 	@param	string styleName New style Name value from AJAX response
     *	@return	void
     */
    replaceStyleName: function(iD, styleName) {
        var par = document.getElementById(iD);
        if(par) {
            var a = par.getElementsByTagName('a');
            var cnt = a.length;
            var link_src;
            var new_src;
            for(var i = 0; i < cnt; i++) {
                link_src = a[i].href;
                new_src = link_src.replace(/style=([^\&]+)/, 'style=' + styleName);
                a[i].href = new_src;
            }
        }
    }


}



