﻿var contentAccordianSwitch = function()
{
	this.items    = [];
	this.hideItem = null;
	this.openItem = null;
	
	/*------------------------------------------------------------------------*
	 *
	 *------------------------------------------------------------------------*/
	this.setItem = function(switchID , targetID , hideDfltImg , hideOverImg , openDfltImg , openOverImg)
	{
		var item = {
			sw : document.getElementById(switchID) ,
			tg : document.getElementById(targetID)
		};
		item.sw.hideImage  = {dflt : hideDfltImg , over : hideOverImg};
		item.sw.openImage  = {dflt : openDfltImg , over : openOverImg};
		item.tg.defaultHeight  = parseInt(item.tg.offsetHeight);
		item.tg.currentOpacity = 0;
		item.tg.style.height   = '0px';
		item.tg.style.display  = 'none';
		item.tg.style.overflow = 'hidden';
		
		item.switchDfltImageChange = function()
		{
			if(img = (this.tg.style.display == 'none') ? this.sw.hideImage.dflt : this.sw.openImage.dflt)
			{
				if(this.sw.tagName == 'IMG') this.sw.src = img; else this.sw.style.backgroundImage = 'url(' + img + ')'
			}
		}
		item.switchOverImageChange = function()
		{
			if(img = (this.tg.style.display == 'none') ? this.sw.hideImage.over : this.sw.openImage.over)
			{
				if(this.sw.tagName == 'IMG') this.sw.src = img; else this.sw.style.backgroundImage = 'url(' + img + ')'
			}
		}
		item.tg.setOpacity = function(opacity)
		{
			this.currentOpacity   = opacity;
			this.style.filter     = 'alpha(opacity=' + opacity + ')';
			this.style.opacity    = opacity / 100;
			this.style.MozOpacity = opacity / 100;
		}
		item.tg.getOpacity = function()
		{
			return this.currentOpacity;
		}
		item.sw.onmouseover = (function(item){return function(){item.switchOverImageChange()}})(item);
		item.sw.onmouseout  = (function(item){return function(){item.switchDfltImageChange()}})(item);
		item.tg.setOpacity(0);
		
		this.items.push(item);
	}
	/*------------------------------------------------------------------------*
	 *
	 *------------------------------------------------------------------------*/
	this.setOnclickEvent = function(flag)
	{
		for(var i = 0; i < this.items.length; i++)
		{
			this.items[i].sw.onclick = (flag) ? (function(_this , i){return function(){_this.step1(i)}})(this , i) : null;
		}
	}
	/*------------------------------------------------------------------------*
	 *
	 *------------------------------------------------------------------------*/
	this.step1 = function(openKey)
	{
		this.setOnclickEvent(false);
		this.openItem = this.items[openKey];
		
		if(this.hideItem != null)
		{
			this.step2();
		}
		else
		{
			this.step3();
		}
	}
	/*------------------------------------------------------------------------*
	 *
	 *------------------------------------------------------------------------*/
	this.step2 = function()
	{
		this.hideItem.tg.style.height = this.hideItem.tg.offsetHeight  - Math.ceil(this.hideItem.tg.offsetHeight / 2) + 'px';
		this.hideItem.tg.setOpacity(this.hideItem.tg.getOpacity() - this.hideItem.tg.getOpacity() / 2);
		
		if(this.hideItem.tg.offsetHeight > 5)
		{
			setTimeout((function(_this){return function(){_this.step2()}})(this) , 60);
		}
		else
		{
			this.hideItem.tg.style.height     = '0';
			this.hideItem.tg.style.display    = 'none';
			this.hideItem.tg.setOpacity(0);
			this.hideItem.switchDfltImageChange();
			this.step3();
		}
	}
	/*------------------------------------------------------------------------*
	 *
	 *------------------------------------------------------------------------*/
	this.step3 = function()
	{
		if(this.openItem != null)
		{
			this.openItem.tg.style.height     = '0';
			this.openItem.tg.style.display    = 'block';
			this.openItem.switchDfltImageChange();
			this.step4();
		}
	}
	/*------------------------------------------------------------------------*
	 *
	 *------------------------------------------------------------------------*/
	this.step4 = function()
	{
		this.openItem.tg.style.height = this.openItem.tg.offsetHeight  + Math.ceil((this.openItem.tg.defaultHeight - this.openItem.tg.offsetHeight) / 2) + 'px';
		this.openItem.tg.setOpacity(this.openItem.tg.getOpacity() + (100 - this.openItem.tg.getOpacity()) / 2);
		
		if(this.openItem.tg.offsetHeight < this.openItem.tg.defaultHeight)
		{
			setTimeout((function(_this){return function(){_this.step4()}})(this) , 60);
		}
		else
		{
			this.setOnclickEvent(true);
			this.openItem.sw.onclick = null;
			this.openItem.tg.setOpacity(100);
			this.openItem.tg.style.height = this.openItem.tg.defaultHeight +'px';
			this.hideItem = this.openItem;
		}
	}
}
/*----------------------------------------------------------------------------*
 *
 *----------------------------------------------------------------------------*/
contentAccordianSwitch.windowOnload = function()
{
	var cas =  new contentAccordianSwitch();
	var tags = document.getElementsByTagName('*');
	
	for(var i = 0; i < tags.length; i++)
	{
		if(tags[i].getAttribute && tags[i].getAttribute("rel") && tags[i].getAttribute("rel").match(/accordian (.*)/))
		{
			var param = RegExp.$1.split(" ");
			var param1 = tags[i].id;
			var param2 = (param[0]) ? param[0] : null;
			var param3 = (param[1]) ? param[1] : null;
			var param4 = (param[2]) ? param[2] : null;
			var param5 = (param[3]) ? param[3] : null;
			var param6 = (param[4]) ? param[4] : null;
			
			cas.setItem(param1 , param2 , param3 , param4 , param5 , param6);
		}
	}
	cas.step1(0);
}
/*----------------------------------------------------------------------------*
 *
 *----------------------------------------------------------------------------*/
if(window.addEventListener)
{
	window.addEventListener('load' , contentAccordianSwitch.windowOnload , false);
}
else if(window.attachEvent)
{
	window.attachEvent('onload' , contentAccordianSwitch.windowOnload);
}
else
{
	window.onload = contentAccordianSwitch.windowOnload;
}

