/*************************************************
*
*	ImFlexi : Forum Events
*	Coding : Halit YESIL
*	Company: Globya Information Technologies (GIT)
*
**************************************************/

	var guiCollapseConfig = {
		'animation':0,
		'savecache':1,
		'sendcache':1,
		'icon':{ 'expand':'expand.gif', 'collapse':'collapse.gif' }
	};
	
	var guiCollapseCookies = {
		'set':	function ( c_name, value, expiredays ){
			var exdate = new Date();
			exdate.setDate( exdate.getDate() + expiredays );
			document.cookie = c_name + "=" + escape( value )+ (( expiredays == null ) ? "" : ";expires=" + exdate.toGMTString() );
		},
		
		'get': function ( c_name ){
			if ( document.cookie.length > 0 ){
  				c_start = document.cookie.indexOf( c_name + "=" );
  				if ( c_start != -1 ){ 
    				c_start = c_start + c_name.length+1; 
    				c_end = document.cookie.indexOf( ";", c_start );
    				
    				if ( c_end == -1 ) 
    					c_end = document.cookie.length;
    				
    				return unescape(document.cookie.substring(c_start,c_end));
				}
			}
			return "";
		}
	};
	
	function guiCollapse( name, status, url, config ){
		
		this.myBoxBtnName = name;
		this.ajaxHref = url;
		
		if( !document.getElementById( this.myBoxBtnName ) ){ 
			return; 
		}
		
		this.myBoxBtn = document.getElementById( this.myBoxBtnName );
		
		this.myBoxName = this.myBoxBtn.getAttribute( 'rel' );
		
		
		
		if( this.myBoxName == '' || !this.myBoxName ){
			return;
		}
		
		if( !document.getElementById( this.myBoxName ) ){
			return;
		}
		
		this.myBox = document.getElementById( this.myBoxName );
		
		if( typeof config == 'object' ){
			this.config = config;
		}else{
			this.config = guiCollapseConfig;
		}
		
		this.boxHeight = 0;
		this.running = 0;
		
		if( this.config.savecache == 1 ){
			var boxcookie = guiCollapseCookies.get( "collapse_" + this.myBoxName );
		}else{
			var boxcookie = null;
		}
		
		this.status = (status == 1 || ( boxcookie == "c" && boxcookie != null && boxcookie != '' ) ) ? 1 : 0;
		
		this.myBox.myob = this;
		this.myBoxBtn.myob = this;
		
		this.setIcons = function( s ){
			
			if( this.config.icon.btntype == 'string' ){
				if( s == 'expand' ){
					eval( this.config.icon.expand );
				}else{				
					eval( this.config.icon.collapse );
				}
			}else if( this.config.icon.btntype == 'none' ){
			}else{
				if( s == 'expand' ){
					this.myBoxBtn.innerHTML = '<a href="javascript:document.getElementById( \'' + this.myBoxBtnName + '\' ).myob.run();"><img src="' + this.config.icon.expand + '" border="0" /></a>';
				}else{				
					this.myBoxBtn.innerHTML = '<a href="javascript:document.getElementById( \'' + this.myBoxBtnName + '\' ).myob.run();"><img src="' + this.config.icon.collapse + '" border="0" /></a>';
				}
			}
		}
		
		this.hideBox = function(){
			
			this.setIcons( 'expand' );
			this.myBox.style.display = 'none';
		}
		
		this.showBox = function(){
			
			this.setIcons( 'collapse' );
			this.myBox.style.display = 'block';
		}		
		
		this.collapse = function(){
			
			this.setHeight();
			
			if( this.config.animation == 1 ){
				
				var fadeAnim = new YAHOO.util.Anim( this.myBoxName, { opacity:{ to:0 } }, 0.1 );
											
				fadeAnim.myob = this;
				
				var fadecompletefunc = function(s, o){
					
					var collapseAnim = new YAHOO.util.Anim( this.myob.myBoxName, { height:{ to:0 } }, 0.3 );
					
					collapseAnim.myob = this.myob;
					
					var completefunc = function(){
						this.myob.hideBox();
					};
					
					collapseAnim.onComplete.subscribe( completefunc );
					collapseAnim.animate();
				};
				
				fadeAnim.onComplete.subscribe( fadecompletefunc );
				fadeAnim.animate();
			}else{
				this.hideBox();
			}
		}
		
		this.expand = function(){

			this.showBox();
			
			if( this.config.animation == 1 ){
				
				var expandAnim = new YAHOO.util.Anim( this.myBoxName, { height:{ to:this.boxHeight } }, 0.3 );
				
				expandAnim.myob = this;
				
				var completefunc = function(){
					
					var fadeAnim = new YAHOO.util.Anim( this.myob.myBoxName, { opacity:{ to:1 } }, 0.4 );
					fadeAnim.animate();
				};
				
				expandAnim.onComplete.subscribe( completefunc );
				expandAnim.animate();
			}
		}
		
		this.getHeight = function(){
			return this.myBox.offsetHeight;
		}
		
		this.setHeight = function(){
			this.boxHeight = this.myBox.offsetHeight;
		}
		
		this.run = function(){
			if( this.myBox.style.display == "block" ){
				this.collapse();
				if( this.config.savecache == 1 ){
					guiCollapseCookies.set( "collapse_" + this.myBoxName, "c", 365);
				}
			}else{
				this.expand();
				if( this.config.savecache == 1 ){
					guiCollapseCookies.set( "collapse_" + this.myBoxName, "e", 365);
				}
			}
			
			if( this.config.sendcache == 1 ){
				var request = YAHOO.util.Connect.asyncRequest('GET', this.ajaxHref, { success:function(o){ }, failure:function(o){ }, argument:[] } );
			}
		}

		this.setHeight();
		
		if( this.config.icon.btntype == 'none' ){
			this.myBoxBtn.onclick = function(){
				this.myob.run();
			}
		}
		
		if( this.status == 1 ){
			this.setIcons( 'expand' );
		}else if( this.status == 0 ){
			this.setIcons( 'collapse' );
		}
		
		if( this.status == 1){
			this.collapse();
		}
	}
