﻿
var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;

var popup;

PopupClass = Class.create({    

    _init: function() {
        var me = this;
        this.overlayOpacity =  0.6;
        this.overlayDuration = 400;
        this.popIsActive = false;
        this.pageUri='';
        this.popTop = 0;
        this.scrollTopOffset = 0;
        
        //this.keyboardAction = this.keyboardAction.bindAsEventListener(this);
        
        this.overlay = $('#pop-overlay');
        this.popcontainer = $('#pop-container');
        
        this.updateLinks();      
		this.resizeOverlay();        
        
        this.overlay.hide();
        this.overlay.unbind('click');
        this.overlay.click( function() { 
            me.close(); 
        });
    },
  
    displayRel: function(target) {  
        if (this.popIsActive) {
            // Handles clicks from a popup
            if (target.rel == 'poplink anchor') {
                var anchorID;
                var parts = target.href.split('#');
                anchorID = parts[parts.length-1];
                
                var pos = $(anchorID).cumulativeOffset();
                
                if (!IE6) {
                    window.scrollTo(pos[0], pos[1]);
                } else {
                    window.scrollTo(pos[0], pos[1]-120);
                }
                return false;
            } else {
                this.display(target.href);
            }
        
        } else {
            // Handles click from a non-popped page
            if (target.rel == 'poplink x' || target.rel == 'poplink anchor') {
                document.location.href = target.href;
            } else {
                this.display(target.href);
            }
        }       
    },
    
    display: function(pageUri) {    
        var me = this;
        this.pageUri = pageUri;
        this.overlay = $('#pop-overlay');
        this.popcontainer = $('#pop-container');
        this.popcontainer.css('display','block');  
        
        if (!this.popIsActive) {            
            this.scrollTopOffset = $(document).scrollTop();
            this.popTop = this.scrollTopOffset + 225;
            this.popcontainer.css('top',this.popTop + 'px');
            
            // center the popup 
            this.centerPopup();
            
            $('body').addClass('popped');
            
            this.resizeOverlay();              
            
            this.overlay.css('opacity',0.0);
            this.overlay.css('display','block');           
            
            this.overlay.animate({ 
                opacity: this.overlayOpacity
            }, this.overlayDuration );
        }
        
        var succeeded = function(content) {			            
            me.popcontainer.html(content);
            me.updateLinks();  
            
            
            // tip-a-friend
            Inspiration.TipAFriend.initialize();
            
            // init rating controls
            Inspiration.Rating.initRating();
                      
            var overlayResize = (function() { me.resizeOverlay(); });
            setTimeout(overlayResize, 700);
                         
            if (this.popIsActive) {
                //new Effect.PageScroll( null, { scrollTo: this.scrollTopOffset, duration: 0.5 } );  
            }           
        }
            
        var failure = function(result) { alert('Poploader.getPopContent : Failure : ?'); }
        
        API.Web.UI.Client.Service.Poploader.GetPopContent(pageUri, succeeded, failure);

       // this.enableKeyboardNav();        
       
        this.popIsActive = true;
    },
    
    displayElem: function(elemId) {    
        var me = this;
        this.pageUri = elemId;
        this.overlay = $('#pop-overlay');
        this.popcontainer = $('#pop-container');
        this.popcontainer.css('display','block');  
        
        if (!this.popIsActive) {            
            this.scrollTopOffset = $(document).scrollTop();
            this.popTop = this.scrollTopOffset + 225;
            this.popcontainer.css('top',this.popTop + 'px');
            
            // center the popup 
            this.centerPopup();
            
            $('body').addClass('popped');
            
            this.resizeOverlay();              
            
            this.overlay.css('opacity',0.0);
            this.overlay.css('display','block');           
            
            this.overlay.animate({ 
                opacity: this.overlayOpacity
            }, this.overlayDuration );
        }
        var content = $('#'+elemId).html();
                   
        me.popcontainer.html(content);
        me.updateLinks();
        try {
	        sIFRPop();
        } catch(err) {
        	
        }
        
        var overlayResize = (function() { me.resizeOverlay(); });
        setTimeout(overlayResize, 700);
                     
        if (this.popIsActive) {
            //new Effect.PageScroll( null, { scrollTo: this.scrollTopOffset, duration: 0.5 } );  
        }           
        this.popIsActive = true;
        return false;
    },
    
    updateLinks: function() {   
        var me = this;
        
        var clickHandler = function(e,item) {
            me.displayRel(item);
            return false;
        };
        $('a[rel^=poplink]').unbind('click');
        $('a[rel^=poplink]').click( function(e) { 
            if (!e) { e = window.event; };
            if (e.ctrlKey) {
                return true;
            }
            me.displayRel(this);
            return false; 
        }); 
    },

    resizeOverlay: function() {
        var pageHeight = $(document).height();
        var pageWidth = $(document).width();
        var contentHeight = this.popTop + this.popcontainer.height() + 75;
        if (contentHeight < pageHeight) {
            contentHeight = pageHeight;
        }
        //IE7 hot-fix??
//        if (contentHeight > 2048) {
//            contentHeight = 2048;
//       }
        try {
            $('#pop-overlay').css({ top: $(document).scrollTop() + 'px'});
            $('#pop-overlay').css({ width: pageWidth + 'px', height: contentHeight + 'px' });
        } catch(e) {
           
        }
    },
    
    centerPopup: function() {
        var popWidth = parseInt(this.popcontainer.css('width'));
        var pageWidth = $(document).width();
        var popLeft  = (pageWidth-popWidth)/2;
        this.popcontainer.css('left',popLeft+'px');
    },

    enableKeyboardNav: function() {
        document.observe('keydown', this.keyboardAction); 
    },

    disableKeyboardNav: function() {
        document.stopObserving('keydown', this.keyboardAction); 
    },
    
    keyboardAction: function(event) {
        var keycode = event.keyCode;

        var escapeKey;
        if (event.DOM_VK_ESCAPE) {  // mozilla
            escapeKey = event.DOM_VK_ESCAPE;
        } else { // ie
            escapeKey = 27;
        }

        var key = String.fromCharCode(keycode).toLowerCase();
        
        if (keycode == escapeKey){ // close lightbox
            this.close();
        } 
    },

    close: function() {
        var me = this;
        this.popIsActive = false;
        this.pageUri = '';
        //this.disableKeyboardNav();
        this.popcontainer.hide();
        this.popcontainer.css('display','none');
        this.popcontainer.html('');
        
        this.overlay.animate({ 
                opacity: 0.0
            }, me.overlayDuration, 'linear', function() { me.overlay.css('display','none'); });
            
        $('body').removeClass('popped');
    }
});

$(document).ready(function() {
    if (!popup) {
        popup = new PopupClass();
        popup._init();
    }    
});




