﻿Type.registerNamespace('MattBerseth.WebControls.AJAX.ImageReflectorControl');

MattBerseth.WebControls.AJAX.ImageReflectorControl.ImageReflectorControlBehavior = function(element) {
    MattBerseth.WebControls.AJAX.ImageReflectorControl.ImageReflectorControlBehavior.initializeBase(this, [element]);

    //  Properties
    this._height = null;
    this._opacity = null;
    
    //  members
    this._imgLoadHandler;
}

MattBerseth.WebControls.AJAX.ImageReflectorControl.ImageReflectorControlBehavior.prototype = {

    initialize : function() {
        MattBerseth.WebControls.AJAX.ImageReflectorControl.ImageReflectorControlBehavior.callBaseMethod(this, 'initialize');
        
        //  handle the application load
        this._imgLoadHandler = Function.createDelegate(this, this._onImgLoad);
        $addHandler(this.get_element(), 'load', this._imgLoadHandler);      
        
        if(this.get_element().complete) {
            this.add();
        }      
    },

    dispose : function() {
        //  if the load handler was added, go ahead and remove it
        if(this._imgLoadHandler) {
            $removeHandler(this.get_element(), 'load',this._imgLoadHandler);            
        }
    },
    
    _onImgLoad : function(args) {
        this.add();
    },
     
    add : function(){
        //  remove the reflection if it has already been applied
        this.remove();
    
        //  create the container element for the image and its relfection
		var container = document.createElement('DIV');
		container.__isReflectionContainer = true;
        
        //  obtain a reference to the image
		var e = this.get_element();
		
		if (Sys.Browser.agent == Sys.Browser.InternetExplorer) {
    		
		    //  calculate the height of the reflected image
		    var reflectionHeight = Math.floor(this.get_Height() * e.height);
		    //  calculate the height of the div
		    var divHeight = Math.floor(e.height * (1 + this.get_Height()));			
			
			//  copy original image's classes & styles to div
			container.className = e.className;
			container.style.cssText = e.style.cssText;
			e.style.cssText = 'vertical-align: bottom';
		
			var reflection = document.createElement('IMG');
			reflection.src = e.src;
			reflection.style.width = e.width + 'px';
			
			reflection.style.marginBottom = "-" + (e.height - reflectionHeight) + 'px';
			reflection.style.filter = 'flipv progid:DXImageTransform.Microsoft.Alpha(opacity=' + (this.get_Opacity() * 100) + ', style=1, finishOpacity=0, startx=0, starty=0, finishx=0, finishy=' + (this.get_Height() * 100) + ')';
			
			container.style.width = e.width + 'px';
			container.style.height = divHeight + 'px';
			e.parentNode.replaceChild(container, e);
			
			container.appendChild(e);
			container.appendChild(reflection);
		} 
		else {
            
		    e.parentNode.replaceChild(container, e);
		    container.appendChild(e);
        
            var p = e.parentNode
            var n = e.nextSibling;
            var d = 1.0 / (e.height * this.get_Height());
            
            for(var i = 0; i < e.height * this.get_Height(); i++) {
                var h = document.createElement('DIV');
                h.style.height = "1px";
                h.style.overflow = "hidden";
                
                var img = document.createElement('IMG');
                img.src = e.src;
                img.style.marginTop = '-' + (e.height - i - 1) + 'px';
                h.appendChild(img);
                
                p.insertBefore(h, n);
                $common.setElementOpacity(h, (1 - d * i) * this.get_Opacity());
            } 
		}
    },  
    
    remove : function() {
        var e = this.get_element();
        if(e.parentNode && e.parentNode.__isReflectionContainer == true) {
		    e.className = e.parentNode.className;
		    e.parentNode.parentNode.replaceChild(e, e.parentNode);        
		}
    },  
    
    get_Height : function() {
        return this._height;
    },

    set_Height : function(value) {
        this._height = value;
        
        if(this.get_isInitialized()) {
            this.add();
        }        
    },
    
    get_Opacity : function() {
        return this._opacity;
    },

    set_Opacity : function(value) {
        this._opacity = value;
        
        if(this.get_isInitialized()) {
            this.add();
        }        
    }
}

MattBerseth.WebControls.AJAX.ImageReflectorControl.ImageReflectorControlBehavior.registerClass('MattBerseth.WebControls.AJAX.ImageReflectorControl.ImageReflectorControlBehavior', AjaxControlToolkit.BehaviorBase);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();