﻿/** 
 * checkArticle.js
 * Check Article AJAX
 *
 *     
 * @author         ecomplexx Germany, www.ecomplexx.com, APobst
 * @version        $Revision: $
 * @date           $Date: $
 *
 * @requires     jQuery
 */
// create namespace, if it doesn't already exist
if (ECMPX === null || typeof(ECMPX) !== 'object')
{ 
    var ECMPX = new Object();
}

// assign members to the namespace
ECMPX =
{
    productAvailable:     false,
    
    // shall the popup hint be shown?
    showHint:             true,
    popupStatus:          false,

    // the URL of the webservice which performs the real magic
    webserviceUri:         '/consumer-b2c/shop2web/ws/1.0/Service.svc/ajax/GetShopLink',
    
    /**
     * Open popup hint
     * 
     * @param String    responseUri        The returned shop uri
     */
    loadPopup:            function (responseUri)
    {
        //loads popup only if it is disabled  
        if (!this.popupStatus)
        {
            this.popupStatus = true;

            // attach close events
            jQuery("#popupClose").click(function () {
                ECMPX.disablePopup();
            });
            
            jQuery("#popupBackground").click(function () {
                ECMPX.disablePopup();
            });

            jQuery(document).keypress(function (e) {
                var code = e.keyCode ? e.keyCode : e.which;
                if (code == 27 && this.popupStatus)
                {
                   ECMPX.disablePopup(); 
                }
            });

            // hide all select boxes -> IE6 Hack
            jQuery('select').each(function ()
            {
                jQuery(this).hide();
            });
            
            // show the popup
            jQuery("#popupBackground").css({"opacity": "0.7"});  
            jQuery("#popupBackground").fadeIn("slow");
            jQuery("#popupHint").fadeIn("slow");
            jQuery("#popupUri").attr("href", responseUri);
            

        }
    },
    
    /**
     * Disable popup hint
     * 
     */
    disablePopup:        function ()
    {
        if (this.popupStatus)
        {
            jQuery("#popupBackground").fadeOut("slow");
            jQuery("#popupHint").fadeOut("slow");
            
            // show all selectboxes -> IE6 Hack
            jQuery('select').each(function ()
            {
                jQuery(this).show();
            });

            this.popupStatus = false;
        }
    },
    
    /**
     * Center Popup hint
     * 
     */
    centerPopup:        function ()
    {
        // gather data for centering
        var windowWidth      = document.documentElement.clientWidth;
        var windowHeight     = document.documentElement.clientHeight;
        var popupHeight      = jQuery("#popupHint").outerHeight();
        var popupWidth       = jQuery("#popupHint").width();
        
        // centering
        jQuery("#popupHint").css(
            {
                "position":  "absolute",
                "top":       windowHeight / 2 - popupHeight / 2,
                "left":      windowWidth / 2 - popupWidth / 2
            });
        
        // ie6 hack: force height
        jQuery("#popupBackground").css(
            {
                "height":    windowHeight,
                "width":     windowWidth
            });
    },

    /**
     * Display or hide the shop elements
     *
     * @param (array[String])    data     the ajax response as json
     */
    setShopVisibilty:    function (data, errorcode)
    {
        // original request parameters
        var requestSite             = data.requestSite;
        var requestLanguage         = data.requestLanguage;
        var requestProductId        = data.requestProductId;
        var requestAcceptVariant    = data.requestAcceptVariant;
        
        // response values
        var responseStatusCode      = data.responseStatusCode;
        var responseProductId       = data.responseProductId;
        var responseUri             = data.responseUri;
        
        // check if all necessary values are available
        if (responseUri && responseStatusCode === 200)
        {
            // show hint if shop is not available in current language
            if (this.showHint) 
            {
                jQuery('#prodshoplink').click(function ()
                {
                    ECMPX.centerPopup();
                    ECMPX.loadPopup(responseUri);
                    return false;
                });
            }
            else 
            {
                // attach uri to shop link if hint is not necessary 
                jQuery('#prodshoplink').attr('href',responseUri);
            }

            // enable shop button
            //jQuery('#shopbutton').show();
            jQuery('#shopbutton').css('visibility','visible');
        }
        else
        {
            // disable shop button
            //jQuery('#shopbutton').hide();
            jQuery('#shopbutton').css('visibility','hidden');
        }
    },
    
    /**
     * Call the webservice and pass the returned values
     * 
     * @param (String)    site            equates to content element "country_abbreviation" in CMS
     * @param (String)    language        current language variant, equates to content element "language_iso" in CMS
     * @param (String)    productId       productId of current product
     * @param (boolean)   acceptVarint    show variants if current article is not available, equates to content element "ShopLinkAcceptVariant" in cms
     */
    checkProductData:    function (site, language, productId, acceptVariant)
    {
        // define the query string for the product you are looking for
        var querystring;
        if (site && language && productId && language && acceptVariant) 
        {
            querystring = "site=" + site;
            querystring += "&language=" + language;
            querystring += "&productId=" + productId;
            querystring += "&acceptVariant=" + acceptVariant;
        }

        // do some ajax magic
        jQuery.ajax(
        {
            'url':            ECMPX.webserviceUri,
            'data':           querystring,
            'type':           'GET',
            'dataType':       'json',
            'ifModified':     true,
            'error':          function (XMLHttpRequest, textStatus, errorThrown)
            {
                // an error occurred --> define error handling please
                switch (textStatus)
                {
                case 'error':         
                    break;
                case 'parseerror':     
                    break;
                case 'timeout':
                    break;
                }
            },
            'success':         function (data, textStatus)
            {
                // the request was successful
                if (true)
                {
                    ECMPX.setShopVisibilty(data);
                }
            }
        });
    }
};

jQuery(document).ready(function()
{
    // show hint if shop is not available in current language
    if (ECMPX.showHint) 
    {
        jQuery('#navtopshop').click(function ()
        {
            ECMPX.centerPopup();
            ECMPX.loadPopup(jQuery(this).attr('href'));
            return false;
        });
    } 
});