﻿
var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;

var popup;

PopupClass = Class.create({    

    _init: function() {
        var me = this;
        this.overlayOpacity =  0.7;
        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 + 100;
            this.popcontainer.css('top',this.popTop + 'px');
            
            // center the popup 
            this.centerPopup();
        
            $('body').addClass('popped');

            this.resizeOverlay();                
                        
            $('div.pop-overlay-segment').each( function() {
                $(this)
                    .css('opacity',0.0)
                    .css('display','block')        
                    .animate({ 
                        opacity: me.overlayOpacity
                    }, me.overlayDuration );
            });
        }
        
        var succeeded = function(content) {
        
            me.overlay.show();
            me.overlay.animate({ 
                opacity: me.overlayOpacity
            }, me.overlayDuration );
            			            
            me.popcontainer.html(content);
            me.updateLinks();  
            
            sifrReplace();
            
            var overlayResize = (function() { me.resizeOverlay(); });
            setTimeout(overlayResize, 700);
                         
            if (this.popIsActive) {
                //new Effect.PageScroll( null, { scrollTo: this.scrollTopOffset, duration: 0.5 } );  
            }
            
            /*
            $('#pop-window-functions')
                .unbind('click')
                .bind('click', function() {
                    me.close();
                });
            */
                
            $('#pop-window-functions a').each( function() {
                $(this)
                    .data('mouseover_animating',false)
                    .data('mouseout_animating',false)
                    .unbind('mouseover')
                    .unbind('mouseout')
                    .bind('mouseover', function() {
                        if (! $(this).data('mouseover_animating')) {
                            $(this)
                                .data('mouseover_animating',true)
                                .animate({ 
                                    marginLeft: 0
                                }, 300, 'linear', function() { $(this).data('mouseover_animating',false); } );
                        }
                    })
                    .bind('mouseout', function() {
                        $(this)
                            .data('mouseout_animating',true)
                            .animate({ 
                                marginLeft: -47
                            }, 200, 'linear', function() { $(this).data('mouseout_animating',false); } );
                        /*
                        if (! $(this).data('mouseout_animating')) {
                        }
                        */
                    });
            });           
        }
            
        var failure = function(result) { alert('Poploader.getPopContent : Failure : ?'); }
        
        API.Web.UI.Client.Service.Poploader.GetPopContent(pageUri, succeeded, failure);

       // this.enableKeyboardNav();        
       
        this.popIsActive = true;
    },
    
    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(fade) {    
        var me = this;
        
        this.overlay.html('');
        
        var pageHeight = $(document).height();
        var pageWidth = $(document).width();
        this.overlay.height(pageHeight);
        
        var contentHeight = this.popTop + this.popcontainer.height() + 75;
        
        if (contentHeight < pageHeight) {
            contentHeight = pageHeight;
        }
        
        var numberOfSections = parseInt((contentHeight / 512),10);
        var lastSectionHeight = contentHeight % 512;
        if (lastSectionHeight > 0) {
            numberOfSections++;
        }  
       
        for (var i=0; i<=numberOfSections-1; i++) {
            if (i==numberOfSections-1) {
                this.overlay.append('<div class="pop-overlay-segment" style="top:'+(i*512)+'px; height:'+lastSectionHeight+'px;"></div>');
            } else {
                this.overlay.append('<div class="pop-overlay-segment" style="top:'+(i*512)+'px; height:512px;"></div>');
            }
        }
        
        if (fade) {
            $('div.pop-overlay-segment').each( function() {
                $(this)
                    .css('opacity',0.0)
                    .css('display','block')        
                    .animate({ 
                        opacity: me.overlayOpacity
                    }, me.overlayDuration );
            });        
        } else {
            $('div.pop-overlay-segment').each( function() {
                $(this)
                    .css('opacity', me.overlayOpacity);
            });
        }
        
        $('div.pop-overlay-segment')
            .unbind('click')
            .bind('click', function() {
                me.close(); 
            });
    },

    /*
    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.hide(); });
            
        $('body').removeClass('popped');
    }
});

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