﻿/*	
	Updated @ 2009-06-20
	License: GNU General Public License v3
	Developer: Ehsun Behravesh
	email: ehsun7b@gmail.com
	Tested on Firefox 3.0.3, Opera 9.52, Safari 3.1.2, Internet Explorer 7
*/

var AjaxCollapse = Class.create({
	initialize: function(link, container, options) {
		try {
			this.container          = container;				    	
            this.link               = link;            		
			
			//	---------------------------------
			
			if (this.container.id == null) {
				this.container = $(this.container);
			}

			//	---------------------------------
			
			if (this.link.id == null) {
				this.link = $(this.link);
			}

			//	---------------------------------
			
			this.containerHeight    = (options != undefined) && (options.containerHeight != undefined) ? options.containerHeight : this.container.getStyle("height");
            this.status             = (options != undefined) && (options.status != undefined) ? options.status : false; // false = close - true = open - null = busy
			
			// ......correction of the containerHeight in IE 7 and older versions
			/*
			if (this.containerHeight == "0px") {
			    alert("was 0");
			    this.containerHeight = this.container.getHeight() + "px";
			    alert(this.containerHeight);
			}
			*/
			//  .................................................................
			
			//	---------------------------------
			
			this.containerRPadding = this.container.getStyle("paddingRight");
			this.containerLPadding = this.container.getStyle("paddingLeft");
			this.containerTPadding = this.container.getStyle("paddingTop");
			this.containerBPadding = this.container.getStyle("paddingBottom");
			
			//	---------------------------------
						
			this.container.setStyle({"overflow": "hidden"});
			if (!this.status) {
			    this.container.setStyle({"height": "0px"});
			    this.container.setStyle({"paddingTop": "0px"});
			    this.container.setStyle({"paddingBottom": "0px"});
			} else {
			    this.container.setStyle({"height": this.containerHeight});
			}
			
			//	---------------------------------
			
			this.link.onclick = this.click.bind(this);
			
		} catch (e) {
			alert("error: " + e.name + " - " + e.message);
		}
	},
	
	click: function() {	
	    if (this.status == null)
	        return;
	        
        if (this.status) {
            this.status = false;
            this.container.setStyle({"height": "0px"});
            this.container.setStyle({"paddingTop": "0px"});
			    this.container.setStyle({"paddingBottom": "0px"});            
        } else {
            this.status = true;
            this.container.setStyle({"height": this.containerHeight});
            this.container.setStyle({"paddingRight": this.containerRPadding, "paddingLeft": this.containerLPadding, "paddingTop": this.containerTPadding, "paddingBottom": this.containerBPadding});
        }          
        
          
		if(this.link.tagName == "A")
			return false;			
	}
}); 