﻿/** 
 * 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();
}

    var amIinEU = 'FRB'; //ENG for OE - OUK for OUK

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

    // the URL of the webservice which performs the real magic
    webserviceUri:         '/consumer-b2c/shop2web/ws/1.1/Service.svc/ajax/GetShopLink',

    // the current culture from RedDot
    currentCulture:       'fr-be',
    /**
     * 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;
        if (amIinEU == 'ENG') {
            var responseUri             = "http://special.olympus.eu/shop/?product_id="+responseProductId+"&olyref=shpbtn";
        } else {
            var responseUri             = data.responseUri;        
        }
        var responsePriceString     = data.responsePriceString;
        
        // insert price above shop button (OUK only 05/2011)
        if ('fr-be' == 'xx-xx') 
        {

        }

        // 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 
            {


                var itCode =  '<script type="text/javascript"> function itShopButtonAction() { var YWATracker = YWA.getTracker(_ith_project_id); YWATracker.setAction("50"); YWATracker.setSKU(getArticleId()); YWATracker.submit_action(); } </script>';
                jQuery('#shopbutton').prepend(itCode);      
                // attach uri to shop link if hint is not necessary 
                jQuery('#prodshoplink').attr('href',responseUri + "&olyref=shpbtn");
                jQuery('#prodshoplink').attr('onclick','javascript:itShopButtonAction();return true;');
      
                //jQuery('#shopbutton').prepend('<a href="#" onclick="javascript:itShopButtonAction();">o</a>');     



            }
            // 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)
    {
        if (false) {
            return;
        }
        // define the query string for the product you are looking for
        var querystring;
        if (site && language && productId && language && acceptVariant) 
        {
            if (amIinEU == 'ENG') {
                querystring = "site=" + "uk";
            } else {
                querystring = "site=" + site;
            }            
            querystring += "&language=" + language;
            querystring += "&productId=" + productId;
            querystring += "&acceptVariant=" + acceptVariant;
        }

        // supplement query string with current culture for currency formatting
        if (ECMPX.currentCulture)
        {
            querystring += "&culture=" + ECMPX.currentCulture;
        }

        // 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;
        });
    } 
});
