varienPopup.prototype = {
    initialize : function(itemId,date){
        this.containerSuffix = date ? date : itemId;
        this.containerId = 'product_' + this.containerSuffix;
        this.itemId  = itemId;
        this.date = date;
        this.cache  = '';
        this.loaded = false;
        this.baseUrl = previewURL+'id/';
        
        // Tim K, new popup handling, test
        /*
        if ( ! $(this.containerId)){
	        $$('.product_'+this.containerId).each(function(element) { 
	        	Event.observe(element ,'click',this.clickItem.bind(this));
	        });
        }else{
        	Event.observe($(this.containerId) ,'click',this.clickItem.bind(this));
        }
        */
        if (getCookie('currentPopUp') != "no"){
        	Event.observe($(this.containerId) ,'click',this.clickItem.bind(this));
        }
    },
    clickItem : function(event){
        if(!this.loaded) {
            this.updateContent(this.baseUrl+this.itemId+'/date/'+this.date,this);
        }else{
        	this.showContent(this.baseUrl+this.itemId+'/date/'+this.date,this);
        }
        Event.stop(event);
    },
    getPosition : function(pObj, this_left, this_top){
        if (pObj.offsetParent) {
            do {
                this_left += pObj.offsetLeft;
                this_top += pObj.offsetTop;
            } while (pObj = pObj.offsetParent);
        }
        return [this_left, this_top];
    },
    hideAll : function(){
        var pDivs = $$('.product-tab');
        for(i=0;i<pDivs.length;i++){
            pDivs[i].style.visibility = 'hidden';
        }
    },
    showContent : function(url,object){
        this.hideAll();
        var categoryContainer = $('preview_'+this.containerSuffix);
        categoryContainer.style.visibility = 'visible';
    },
    updateContent : function(url,object){
        this.hideAll();
        var categoryThumb = $('thumb_'+this.containerSuffix);
        var curleft = curtop = 0;
        var curPos = this.getPosition(categoryThumb, curleft, curtop);
        this.curleft = curPos[0];
        this.curtop = curPos[1];
        this.categoryContainer = $('preview_'+this.containerSuffix);

        new Ajax.Updater(
            this.categoryContainer,
            url + (url.match(new RegExp('\\?')) ? '&isAjax=true' : '?isAjax=true' ),
            {
                parameters:  false,
                method:      'post',
                loaderArea:  this.categoryContainer,
                onSuccess: function(transport) {
                    if (transport.responseText.isJSON()) {
                       var response = transport.responseText.evalJSON();
                       if (response.ajaxExpired && response.ajaxRedirect) {
                           setLocation(response.ajaxRedirect);
                           return false;
                       }
                    }
                },
                onComplete:  object.setLoaded.bind(object),
                evalScripts: true
            }
        );
    },
    setLoaded : function () {
    	this.categoryContainer.style.left = (this.curleft-thisLeft)+"px";
        if(this.curtop > thisMaxTop){
            this.categoryContainer.style.top = (this.curtop-(this.categoryContainer.offsetHeight+thisOffset))+"px";
        }else{
            this.categoryContainer.style.top = (this.curtop+thisTop)+"px";
            this.categoryContainer.className = this.categoryContainer.className+' product-tab-bot';
        }
        this.categoryContainer.style.visibility = "visible";
        this.loaded = true;
    }
}