﻿
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.popcontent = $('#pop-content-html');
           
        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.displayURL(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.displayURL(target.href);
            }
        }       
    },
    
    displayURL: function(pageUri) {    
        var me = this;

        var succeeded = function() {
			me.displayHTML(null);
        }
            
        var failure = function(result) { alert('Poploader.getPopContent : Failure : ?'); }
        
        me.popcontent.load(pageUri + ' #pop-area', succeeded,failure);
    },
    
    
    displayHTML: function(html) {    
        var me = this;
        
		$('object').not('.sIFR-flash').hide();
        $('select').hide();
		$('.flash_alternate').show();
        
        if(!this.popIsActive){
			me.showOverlay();
			me.showPopup();
		}

        //add content to popup
        if(html){
			me.popcontent.html(html);
		}
		
		if(this.popIsActive){
			if (typeof sIFRPop != "undefined") {
				sIFRPop();
			}
		}
				
		//add update links in popup content to enable popups from popup
        me.updateLinks();

        //setTimeout(sIFRPop,2000);

        //resize overlay according to content in popup
        var overlayResize = (function() { me.resizeOverlay();});
        setTimeout(overlayResize, 700);
        
	    this.popIsActive = true;
       
        return false;
    },
    
    displayElem: function(elemId) {
		var me = this;
		
		var content = $('#'+elemId).html();
		me.displayHTML(content);
		   
        return false;
    },
    
    displayStatic: function() {    
		displayHTML(null);
		
		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; 
        }); 
    },

    showOverlay: function() {   
        var me = this;
        
        //show overlay
        if (!me.popIsActive) {            
            me.overlay.show();
            me.overlay.animate({ 
                opacity: me.overlayOpacity
            }, me.overlayDuration );
            
            me.resizeOverlay();     
                        
            $('div.pop-overlay-segment').each( function() {
                $(this)
                    .css('opacity',0.0)
                    .css('display','block')        
                    .animate({ 
                        opacity: me.overlayOpacity
                    }, me.overlayDuration );
            });
        } 
    },
    
    showPopup: function() {   
        var me = this;
        
        //show popup
        var _showPopup = (function() { 
			//var stepMe = function(step){if(step==0){sIFRPop();}}
			
			$(me.popcontainer)
				//.css('opacity',0.0)
				.css('display','block')
				//.animate({opacity: 1.0}, {duration:900,step:stepMe} ); 
			
			if (typeof sIFRPop != "undefined") {
				sIFRPop();
			}
			
			if (!me.popIsActive) {  
				me.scrollTopOffset = $(document).scrollTop();
				me.popTop = me.scrollTopOffset + 45;
				me.popcontainer.css('top',me.popTop + 'px');
	            
				// center the popup 
				this.centerPopup();
				
				$('body').addClass('popped');
			}
        });
        
        setTimeout(_showPopup, 100);
    },
    
    resizeOverlay: function(fade) {    
        var me = this;        
        this.overlay.html('');        
        this.overlay.show();
        
        var pageHeight = $(document).height();
        var pageWidth = $(document).width();
        
        var contentHeight = this.popTop + this.popcontainer.height() + 75;
        
        if (contentHeight < pageHeight) {
            contentHeight = pageHeight;
        }       
      
        this.overlay.height(contentHeight);        
        
        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() {
                this.close(); 
	        });
	        
        $('#pop-container')
            .unbind('click')
            .bind('click', function(e) {
               if($(e.target).attr('id')=='pop-container') {me.close()}
            });
    },
    
    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.popcontent.html('');
        
        this.overlay.animate({ 
                opacity: 0.0
            }, me.overlayDuration, 'linear', function() { me.overlay.css('display','none'); me.overlay.height(0); me.overlay.html(''); });
            
        $('body').removeClass('popped');
        
        $('select').show();
		$('object').not('.sIFR-flash').show();
		$('.flash_alternate').hide();

    }
});

$(document).ready(function() {
    if (!popup) {
        popup = new PopupClass();
        popup._init();
    }    
});