var undefined;
var ASMEproduct = function (options) {

    var self = this;

    this.defaults = {
        overlayTriggers: $JQ.ASME.mainContentDiv.find('.view-larger'),
        rightColAccordions: $JQ.ASME.mainContentDiv.find('.right-col .accordion')
    };

    this.settings = $JQ().extend({}, this.defaults, options);

    function _init() {
        console.log("starting ASME Product JS...");

        //determine title, description, and author for later use. Used in Addthis inline JS on the article page
        $JQ.ASME.pageTitle = $JQ.ASME.mainContentDiv.find('h1').text();
        $JQ.ASME.pageDesc = $JQ.ASME.mainContentDiv.find('#product-description p:first').text();

        $JQ.ASME.pageMediaType = 'Product';
        var productType = $JQ.ASME.mainContentDiv.find('.blue-eyebrow').text();
        productType = $JQ.ASME.string.stripWhiteSpace(productType);
        productType = $JQ.ASME.string.replaceWhiteSpace(productType.toLowerCase(), '_');
        $JQ.ASME.trackingValues = ',product_type=' + encodeURIComponent(productType);
        ntptAddPair('product_type', productType);

        var productName = $JQ.ASME.string.replaceWhiteSpace($JQ.ASME.string.stripWhiteSpace($JQ.ASME.pageTitle), '_');
        productName = productName.toLowerCase();

        ntptAddPair('product_name', encodeURIComponent(productName));
        self.setPrimaryTopic();
        self.enableTracking();

        _checkRightColModules();

        //we need to check to see if a product has been added to the cart, and if so, display a popup
        var error = $JQ.ASME.mainContentDiv.find('.product-error').attr('error');
        var $productPopUp = $JQ.ASME.mainContentDiv.find('#product-popup');

        if (error) {
            self.productOverlay();
        } else if ($productPopUp.length) {
            var productTitle = $JQ.ASME.pageTitle;
            var cartID = $productPopUp.find('a').attr('id');
            var href = $productPopUp.find('a').attr('href');
            var aTag = '<a id="' + cartID + '" href="' + href + '">View Cart</a>';

            self.productOverlay(productTitle, aTag);
        }
    }

    //private functions
    function _checkRightColModules() {
        var $rightColModule = $JQ.ASME.mainContentDiv.find('.right-col .module:eq(0)');
        $rightColModule.css('borderTop', '3px solid #242424');
    }


    //public functions
    this.buildToolTips = function () {
        self.journalTooltips = new tooltip();
    }

    this.buildModuleAccordions = function () {
        console.log("building accordions...");
        self.settings.rightColAccordions.each(function (index) {
            var $this = $JQ(this);
            var $ul = $this.find('ul');
            $ul.hide();
            $ul.css('float', 'left');

            var $trigger = $this.find('h3');

            $trigger.bind('click', function (e) {
                if (!$trigger.hasClass('open')) {
                    $ul.slideDown(500);
                    $trigger.addClass('open');
                } else {
                    $trigger.removeClass('open');
                    $ul.slideUp(500);
                }
            });

            //hover state for triggers, blue background and blue arrow
            $trigger.bind('mouseover', function (e) {
                $trigger.removeClass('bg-beige').addClass('bg-light-blue');
            });

            $trigger.bind('mouseout', function (e) {
                $trigger.removeClass('bg-light-blue').addClass('bg-beige');
            });

        });

    }

    this.buildTabs = function () {
        var $productDesc = $JQ.ASME.mainContentDiv.find('#product-description');
        var $headers = $productDesc.find('h2');
        $headers.css('cursor', 'pointer');

        //we only need to wire up tab functionality if there are more than one header
        if ($headers.length > 1) {

            //hide tab contents beyond the initial tab
            var $tabs = $JQ.ASME.mainContentDiv.find('.tab-content');
            $JQ.ASME.mainContentDiv.find('.tab-content:gt(0)').hide();

            var hook = 0;

            $headers.each(function () {
                var $this = $JQ(this);

                //assigning a hook to the tab based on it's position, ie which tab is it, first, second, etc
                $this.attr('hook', hook);
                hook++;

                $this.bind('click', function (e) {
                    //the tab is the corrseponding tab-contnet div, stored earlier
                    var $tab = $JQ($tabs[parseInt($this.attr('hook'))]);
                    $tabs.hide();
                    $tab.show();
                    $this.addClass('on').siblings('h2').removeClass('on');
                });

            });

        }
        else
            return false;

    }

    //getting primary topic for product page, provided by .NET backend
    this.setPrimaryTopic = function () {
        console.log(primaryTopic);
        if (primaryTopic != undefined) {
            primaryTopic = $JQ.ASME.string.replaceWhiteSpace(primaryTopic.toLowerCase(), '_');
            $JQ.ASME.primaryTopic = primaryTopic;
        }
    }

    //enables product specific tracking events
    this.enableTracking = function () {
        var $rightColLinks = $JQ.ASME.mainContentDiv.find('.right-col ul li a');
        $rightColLinks.bind('click', function (e) {
            //e.preventDefault();
            var $this = $JQ(this);
            var moduleTitle = $this.parents('.module').find('h3').text();
            moduleTitle = $JQ.ASME.string.replaceWhiteSpace(moduleTitle, '_');
            moduleTitle = moduleTitle.toLowerCase();
            ntptAddPair('content_module', moduleTitle);
            ntptEventTag('ev=click_event');
        });
    }

    //overlay for add to cart button
    this.productOverlay = function (productTitle, aTag) {
        self.overlay = $JQ.ASME.newOverlay();
        var keepURL = window.document.location;
        if (productTitle != undefined) {
            var html = ['<div class="cart-overlay">',
							'<p><strong>' + productTitle + '</strong> has been added to your cart.</p>',
							'<div class="clear"></div>',
							'<div class="float-left buttons">',
								'<div class="blue-button"><a href="javascript:void(0)">Keep Shopping</a></div>',
								'<div class="green-button">' + aTag + '</div>',
							'</div>',
						'</div>'
			];

            ntptEventTag('ev=cartadd&rta=' + ProductCode + ';' + Quantity + ';' + Price);


        } else {

            var errorMessage = ErrorMsg;

            var html = ['<div class="cart-overlay error">',
					  '<p>' + errorMessage + '</p>',
					  '<div class="clear"></div>',
					  '<div class="float-left buttons">',
						  '<div class="blue-button"><a href="javascript:void(0)">OK</a></div>',
					  '</div>',
				  '</div>'
		  	];
        }

        html = html.join('');

        self.overlay.setContent(html);
        self.overlay.show();

        $JQ('#close-overlay').bind('click', function () {
            //window.document.location = keepURL;	
            $JQ.ASME.overlay.hide();
        });
        //self.overlay.divBlocker.unbind('click');
    }

    _init();

};
  
