//! ################################################################
//! This file contains both original and merged/adapted code.
//! Except where indicated, all code is
//! Copyright (c) 2004-2006 Amazon.com, Inc., and its Affiliates.
//! All Rights Reserved.
//! Not to be reused without permission
//! ################################################################

var _amzn_cl_popup = {
    vertical_offset : '1px',
    horizontal_offset : '1px',
    start_delay : 400,
    end_delay : 400,
    clientX: 0,
    clientY: 0,
    onshow_callback: null,
    onhide_callback: null,
    eventObject:null,
    init_done:false,
    objName: '',


    init: function() {
        if (!this.init_done) {
            this.init_done = true;
        }
    },

    showpreview : function(innerHTML, obj, event, endDelay) {
        if (!this.init_done) {
            return;
        }
        if (this.eventObject == obj) {
            this._clear_tip();
            return;
            // do nothing if source is the same;
        }
        this.clientX = event.clientX;
        this.clientY = event.clientY;
        this.objName = obj.name;
        var delay = this.end_delay;
        if (typeof endDelay != "undefined") {
            delay = endDelay;
        }
        this.show(unescape(innerHTML), obj, event, delay);
    },

    show : function(content, obj, e, delay) {
        if (!this.init_done) {
            return;
        }
        this.objName = obj.name;
        if (window.event) {
            event.cancelBubble = true;
        } else if (e.stopPropagation) {
            e.stopPropagation();
        }
        if (typeof delay == "undefined") {
            delay = this.end_delay;
        }
        this._do_hide();
        var tooltip = document.getElementById('amzn-popup-div');
        tooltip.innerHTML = content;
        this.runPNGTransparencyHack();
        if ((e.type == 'click' && tooltip.style.display == 'none') || e.type == 'mouseover') {
            var self = this;
            this._show_handler = setTimeout(function() {
                self._do_show();
            }, delay);
        } else if (e.type == 'click') {
            tooltip.style.display = 'none';
        }
        tooltip.x = this._get_pos_offset(obj, true);
        tooltip.y = this._get_pos_offset(obj, false);
        tooltip.style.left = tooltip.x - this._clear_browser_edge(obj, true) + 'px';
        tooltip.style.top = tooltip.y - this._clear_browser_edge(obj, false) + obj.offsetHeight + 'px';
        this.eventObject = obj;
        return true;
    },

    hide : function(obj) {
        if (!this.init_done) {
            return;
        }
        this._must_hide = true;
        var self = this;
        var different_parent = false;
        if (obj && this.eventObject != obj) {
            different_parent = true;
        }

        if (this._am_visible && !different_parent) {
            this._clear_tip();
            this._hide_handler = setTimeout(function() {
                self._do_hide();
            }, this.start_delay);
        } else {
            this._do_hide();
            // if I am not open - hide myself immediately - so that there is no flicker
        }
        return true;
    },

    hideNow : function() {
        if (!this.init_done) {
            return;
        }
        this._must_hide = true;
        var self = this;
        this._hide_handler = setTimeout(function() {
            self._do_hide();
        }, 0);
        return true;
    },

    updateHTML: function (content) {
        var tooltip = document.getElementById('amzn-popup-div');
        tooltip.innerHTML = unescape(content);
        this.runPNGTransparencyHack();
    },


//---Private
    _show_handler : null,
    _hide_handler : null,
    _must_hide : true,
    _am_visible : false,

    _clear_tip : function() {
        if (typeof this._hide_handler != 'undefined' || this._hide_handler !== null) {
            clearTimeout(this._hide_handler);
            delete(this._hide_handler);
        }
        if (typeof this._show_handler != 'undefined' || this._show_handler !== null) {
            clearTimeout(this._show_handler);
            delete(this._show_handler);
        }
    },

    _get_pos_offset : function(what, is_left) {
        var total_offset = (is_left) ? what.offsetLeft : what.offsetTop;
        var parentEl = what.offsetParent;
        while (parentEl !== null) {
            total_offset = (is_left) ? total_offset + parentEl.offsetLeft : total_offset + parentEl.offsetTop;
            parentEl = parentEl.offsetParent;
        }
	parentEl = what.parentNode;
        while(parentEl != null && parentEl != document.body){
                if(is_left && parentEl.scrollLeft){
                        total_offset-=parentEl.scrollLeft;
                }else if(!is_left && parentEl.scrollTop){
                        total_offset-=parentEl.scrollTop;
                }
                parentEl = parentEl.parentNode;
        }
        return total_offset;
    },

    _clear_browser_edge : function(obj, is_horizontal) {
        var tooltip = document.getElementById('amzn-popup-div');
        var edge_offset = (is_horizontal) ? parseInt(this.horizontal_offset, 10) * -1 : parseInt(this.vertical_offset, 10) * -1;
        var is_ie = document.all && !window.opera;
        var window_edge, content_measure;
        if (is_ie) {
            var ie_body = this._ie_body();
        }
        if (is_horizontal) {
            window_edge = is_ie ? ie_body.scrollLeft + ie_body.clientWidth - 15 : window.pageXOffset + window.innerWidth - 15;
            content_measure = parseInt(tooltip.style.width);
            if (window_edge - tooltip.x < content_measure) {
                edge_offset = content_measure - obj.offsetWidth;
            }
            var objX = tooltip.x % window_edge;
            if(objX - edge_offset + content_measure > window_edge) {
                edge_offset = objX - (window_edge - content_measure - 15);
            }
        } else {
            window_edge = is_ie ? ie_body.scrollTop + ie_body.clientHeight - 15 : window.pageYOffset + window.innerHeight - 18;
            content_measure = parseInt(tooltip.style.height);
            if ((window_edge - tooltip.y < content_measure) && (this.clientY > content_measure)) {
                edge_offset = content_measure + obj.offsetHeight;
            }
        }
        return edge_offset;
    },

    _ie_body : function() {
        return (document.compatMode && document.compatMode != 'BackCompat') ? document.documentElement : document.body;
    },

    _do_show : function() {
        document.getElementById('amzn-popup-div').style.display = 'block';
        this._am_visible = true;
        if (this.onshow_callback != null && typeof this.onshow_callback == "function") {
            this.onshow_callback();
        }
    },

    _do_hide : function() {
        if (this._must_hide) {
            document.getElementById('amzn-popup-div').style.display = 'none';
            if (this.onhide_callback != null && typeof this.onhide_callback == "function" && this._am_visible) {
                this.onhide_callback();
            }
            this._am_visible = false;
            this.eventObject = null;
        }
        this._clear_tip();
    },

    _continue : function() {
        this._must_hide = false;
    },

    _stop : function() {
        this._must_hide = true;
        this.hide();
    },

    runPNGTransparencyHack: function () {
    	// IE Hack for PNG image
        if (/MSIE (5\.5|6\.)/.test(navigator.userAgent) && window.ActiveXObject) {
            var images = document.getElementById("amzn-popup-div").getElementsByTagName("IMG");
            for(var i = 0;i < images.length;i++){
			var image = images[i];
			if(image.src.match("png")!= null && typeof(image) != 'undefined' && image != null) {
        		        var src = image.src;
                		image.src = 'http://' + amzn_popovers_vip + '/images/transparent-pixel.gif';
	                	image.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + src + '\',sizingMethod=\'scale\')';
			   }
        	    
	  }
        }
    }


};

_amzn_cl_popup.init();

amzn_LibraryStatus.popupLoaded = true;
if( typeof(amzn_LibraryStatus.previewManagerLoaded) != 'undefined' &&
    amzn_LibraryStatus.previewManagerLoaded) {
    amzn_previewManager.init();
}

