
var dsDyn = {
	defaults: {
		mode:false,
		direction:'in',
		speed:10,
		ticks:1,
		mode:'open',
		closebtn:'on'
	},
	
	genProps: function(prop) {
	if(typeof(prop) == 'undefined') prop = new Object();

	for(key in this.defaults) {
		if(typeof(prop[key]) == 'undefined') {
			prop[key] = this.defaults[key];
		}
	}
	return prop;
	},
		
	init: function() {

	},
	
	dom: function(prop) {
		var funclist = ['document.getElementById(prop)', 'document[prop]', 'document.forms[prop]', 'document.images[prop]', 'document.getElementsByTagName(prop)'];

		func = false;
		for(var i=0; i<funclist.length; i++) {
			if(eval(funclist[i])) {
				var func = funclist[i];
				break;
				}
		}
		if(func != false) {
			if(eval(func).toString() == '[object HTMLCollection]' && eval(func).length == 0) {
				return false
			} else {
				return eval(func);
			}
		} else if(typeof(prop) == 'object') {
			return prop;
		} else {
			return false;
		}
	},
	
	fade: function(prop) {
		if(typeof(prop) != 'object') {
			// Do fade based on properties passed as parameters, quick and easier
			prop = {
				target:arguments[0],
				direction:arguments[1],
				speed:arguments[2],
				ticks:arguments[3]
			}
		}
		
		if(typeof(this.dom(prop.target)) != 'undefined' && this.dom(prop.target) != false) {
			prop = this.genProps(prop);
			if(typeof(fadeInterval) != 'undefined')
				clearInterval(fadeInterval[prop.target]);
			else
				fadeInterval = [];
			
			if(prop.direction && prop.speed) {
				var fadevalueIE = (prop.direction == 'in'?0:100);
				var fadevalueFF = (prop.direction == 'in'?0:1.0);
				var stopat		= (prop.direction == 'in'?100:0);
				var ticks		= (typeof(prop.ticks) != 'undefined'?prop.ticks:1);
				
				// perform fade
				fadeInterval[prop.target] = setInterval(function() {
					(prop.direction == 'in'?fadevalueIE+=ticks:fadevalueIE-=ticks);
					(prop.direction == 'in'?fadevalueFF+=(ticks/100):fadevalueFF-=(ticks/100));
					dsDyn.dom(prop.target).style.opacity = fadevalueFF;
					dsDyn.dom(prop.target).style.filter = 'alpha(opacity='+fadevalueIE+')';
					
					// Stop fade
					if((prop.direction == 'in' && fadevalueIE >= stopat) || (prop.direction == 'out' && fadevalueIE <= stopat)) {
						clearInterval(fadeInterval[prop.target]);
						dsDyn.dom(prop.target).style.opacity = (prop.direction == 'in'?1:0);
						dsDyn.dom(prop.target).style.filter = (prop.direction == 'in'?'':'alpha(opacity=0)');
						}
				}, prop.speed);
			}
		}
	},
	
	scroll: function(prop) {
		if(typeof(prop) != 'object') {
			// Get properties from ordernary non-object arguments
			prop = {
				target:arguments[0],
				mode:arguments[1],
				direction:arguments[2],
				speed:arguments[3],
				ticks:arguments[4]
			}
		}
		
		if(typeof(this.dom(prop.target)) != 'undefined' && this.dom(prop.target) != false) {
			prop = this.genProps(prop);
			
			if(typeof(scrollInterval) != 'undefined') {
				clearInterval(scrollInterval[prop.target]);
			} else {
				scrollInterval = [];
			}
				
			if(typeof(window.origstate) == 'undefined')
				window.origstate = [];
				
			if(prop.direction && prop.speed) {
				dsDyn.dom(prop.target).style.overflow = 'hidden';
				dsDyn.dom(prop.target).style.display = '';
				var height 	= ((prop.mode == 'open' && !window.origstate[prop.target])?0:dsDyn.dom(prop.target).clientHeight);
				var width	= ((prop.mode == 'open' && !window.origstate[prop.target])?0:dsDyn.dom(prop.target).clientWidth);
				var ticks	= (typeof(prop.ticks) != 'undefined'?prop.ticks:1);
				var stopath	= (window.origstate[prop.target]?window.origstate[prop.target].width:dsDyn.dom(prop.target).clientWidth);
				var stopatv	= (window.origstate[prop.target]?window.origstate[prop.target].height:dsDyn.dom(prop.target).clientHeight);
				var stoph	= false;
				var stopv	= false;
				var manualstop = false;
					
				if(!window.origstate[prop.target]) {
					window.origstate[prop.target] 		 = new Object();
					window.origstate[prop.target].width  = dsDyn.dom(prop.target).clientWidth;
					window.origstate[prop.target].height = dsDyn.dom(prop.target).clientHeight;
					
				}
				
				// Perform scrolling
				scrollInterval[prop.target] = setInterval(function() {
					if((prop.direction == 'vertical' || prop.direction == 'both') && !stopv && !manualstop) {
						(prop.mode == 'open'?height+=ticks:height-=ticks);
						if(Math.floor(height) > 0) dsDyn.dom(prop.target).style.height = Math.floor(height) + 'px';
						
						// Stop vertical scrolling
						if((prop.mode == 'open' && height >= stopatv) || (prop.mode == 'close' && height <= 0) || stopv) {
							if(prop.mode == 'close') {
								dsDyn.dom(prop.target).style.height 	= window.origstate[prop.target].height + 'px';
								dsDyn.dom(prop.target).style.display 	= 'none';
								
								if(prop.direction == 'both') {
									manualstop = true;
									dsDyn.dom(prop.target).style.width 	= window.origstate[prop.target].width + 'px';
								}
							}
							stopv = true;
						}
					}
					if((prop.direction == 'horizontal' || prop.direction == 'both') && !stoph && !manualstop) {
						(prop.mode == 'open'?width+=ticks:width-=ticks);
						if(Math.floor(width) > 0) dsDyn.dom(prop.target).style.width = Math.floor(width) + 'px';
						
						// Stop horizontal scrolling
						if((prop.mode == 'open' && width >= stopath) || (prop.mode == 'close' && width <= 10) || stoph) { // width <= 10 ->IE fix for 'both' direction (IE sux)
							if(prop.mode == 'close') {
								dsDyn.dom(prop.target).style.display 	= 'none';
								dsDyn.dom(prop.target).style.width		= window.origstate[prop.target].width + 'px';
								
								if(prop.direction == 'both') {
									manualstop = true;
									dsDyn.dom(prop.target).style.height = window.origstate[prop.target].height + 'px';
								}
							}
							stoph = true;
						}
					}
					
					if((prop.direction == 'both' && stopv && stoph) || (prop.direction == 'vertical' && stopv) || (prop.direction == 'horizontal' && stoph) || manualstop) {
						clearInterval(scrollInterval[prop.target]);
						window.origstate[prop.target] = false;
						}
				}, prop.speed);
			}
		}
	},
	
	drag: {
		init: function(prop, e) {
			if(typeof(prop) != 'object') {
				prop = {
					target:arguments[0],
					max_x:arguments[1],
					max_y:arguments[2]
				}
			}
			
			prop = dsDyn.genProps(prop);
			
			dsDyn.dom(prop.target).onmousedown = dsDyn.drag.start;
			dsDyn.dom(prop.target).onmouseup = dsDyn.drag.end;
		},
		start: function(e) {
			if(!window.zin) {
				window.zin = 100;
			} else {
				window.zin++;
			}
			this.style.position = 'absolute';
			this.style.zIndex = window.zin;
			this.style.cursor = 'move';

			e = dsDyn.drag.fixE(e);
			var leftpos = this.offsetLeft;
			var toppos 	= this.offsetTop;
			var mousex	= e.clientX;
			var mousey	= e.clientY;
			
			this.onmousemove = function(e) {
				e = dsDyn.drag.fixE(e);
				var offset = e.clientX - this.offsetLeft;
				this.style.left = e.clientX - (mousex - leftpos) + 'px';	
				this.style.top = e.clientY - (mousey - toppos) + 'px';
				}

			this.onselectstart = function() { return false; }
			return false;
		},
		
		end: function() {
			this.style.cursor = 'default';
			this.onmousemove = function() {
				return false;
			}
		},
		fixE : function(e) {
			if (typeof e == 'undefined') e = window.event;
			if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
			if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
			return e;
		}
	},
	
	preload: function(prop) {
		prop = this.genProps(prop);

		if(typeof(prop.imagelist) != false) {
			if(typeof(prop.onstart) == 'function')
				eval(prop.onstart());
			
			var percent = 0;
			var loaded	= 0;
			var imgObj 	= [];
			for(var i=0; i<prop.imagelist.length; i++) {
				imgObj[i] = new Image();
				imgObj[i].src = prop.imagelist[i];
				imgObj[i].onload = function() {
					loaded++;
					percent = Math.ceil((loaded * 100) / prop.imagelist.length);
					if(typeof(prop.onprogress) == 'function')
						eval(prop.onprogress(percent, this.src));
				}
			}
			
			checkFinish = setInterval(function() {
				if(loaded == prop.imagelist.length) {
					if(typeof(prop.onfinish) == 'function')
						eval(prop.onfinish());
						clearInterval(checkFinish);
				}
			}, 100);
		}
	},
	
	message: {
		init: function(prop) {
			if(typeof(prop) != 'object') {
				prop = {
					message:arguments[0],
					tint:arguments[1],
					msgclass:arguments[2],
					bgclass:arguments[3],
					closebtn:arguments[4],
					closemdg:arguments[5]
				}
			}
			
			prop = dsDyn.genProps(prop);
			
			if(typeof(prop.message) != 'undefined') {			
				if(typeof(prop.tint) != 'undefined' && prop.tint == true) {
					var bg = document.createElement('div');
					if(typeof(prop.bgclass) != 'undefined')
						bg.className = prop.bgclass;
					else {
						bg.style.background = '#000';
						bg.style.opacity 	= '0.5';
						bg.style.filter		= 'alpha(opacity=50)';
					}
					bg.id			  = 'dsDynMsgBack';
					bg.style.position = 'absolute';
					bg.style.top 	  = document.documentElement.scrollTop + 'px';
					bg.style.left 	  = document.documentElement.scrollLeft + 'px';
					bg.style.height   = '100%';
					bg.style.width 	  = '100%';
					if(window.zin)
						bg.style.zIndex = window.zin + 1;
					else
						bg.style.zIndex = 100;
					document.body.appendChild(bg);
				}
				
				var message = document.createElement('div');
				if(typeof(prop.msgclass) != 'undefined') {
					message.className = prop.msgclass;
				} else {
					//message.style.border 	 = '2px solid #777';
					//message.style.background = '#666';
					//message.style.color		 = '#FFF';
					//message.style.padding	 = '5px';
					//message.style.font		 = '12px Arial';
				}
				message.id		  = 'dsDynMsg';
				message.innerHTML = prop.message; //
				if(typeof(prop.closebtn) != 'undefined' && prop.closebtn == 'on')
					message.innerHTML += '<br /><input type="button" value="'+(prop.closemsg?prop.closemsg:'close')+'" onclick="dsDyn.message.close();" class="mainInputSubmit" style="display:block; margin:0px auto;" />';
				message.style.position 	= 'absolute';
				
				if(window.zin)
					message.style.zIndex = window.zin + 2;
				else
					message.style.zIndex = 101;
				message.style.opacity = '0';
				message.style.filter = 'alpha(opacity=0)';
				document.body.appendChild(message);
				
				message.style.top 		= (document.documentElement.clientHeight / 2) - (message.clientHeight / 2) + document.documentElement.scrollTop + 'px';
				message.style.left 		= (document.documentElement.clientWidth / 2) - (message.clientWidth / 2) + document.documentElement.scrollLeft + 'px';
				dsDyn.fade('dsDynMsg','in', 10, 3);
				
				document.onscroll = function() {
					message.style.top 		= (document.documentElement.clientHeight / 2) - (message.clientHeight / 2) + document.documentElement.scrollTop + 'px';
					message.style.left 		= (document.documentElement.clientWidth / 2) - (message.clientWidth / 2) + document.documentElement.scrollLeft + 'px';
					bg.style.top 	  = document.documentElement.scrollTop - 5 + 'px';
					bg.style.left 	  = document.documentElement.scrollLeft - 5 + 'px';
					return false;
				}
			}
		},
		close: function() {
			if(dsDyn.dom('dsDynMsgBack'))
				document.body.removeChild(dsDyn.dom('dsDynMsgBack'));
			document.body.removeChild(dsDyn.dom('dsDynMsg'));
		}
	}
};

var $ = dsDyn;
