function isIE() {
	return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}
function $(id) {
	return document.getElementById(id);
}
function Numeric(string) {
	var num = parseInt(string.toString().replace(/[^0-9.,-]/g,''));	
	return (num) ? num:0;
}
function Alpha(string) {
	return string.toString().replace(/[0-9.,]/g,'');
}

// source: http://www.daniweb.com/forums/thread53662.html
function CopyClipboard(text) {
	// TODO
}

function CountKeysInObject(object) {
	var i = 0;
	for (k in object) if (object.hasOwnProperty(k)) i++;
	return i;
}

// source: http://www.leigeber.com/fader/fader.js
// main function to process the fade request
function ColorFade(id,element,start,end,callback,forceFX,steps,speed) {
	Debug("ColorFade on "+id,"FX");
	var startrgb,endrgb,er,eg,eb,step,rint,gint,bint,step;
	var target;
	if (typeof(id)=="string") { target = $(id); }
	else { target = id; }
	steps = steps || 30;
	speed = speed || 30;
	clearInterval(target.colorTimer);
	endrgb = ColorConv(end);
	er = endrgb[0];
	eg = endrgb[1];
	eb = endrgb[2];
	
	if(isIE()) steps = steps/2; // IE BUG: SLOW FX
	if(!themesConfig.effects&&!forceFX) steps=1; // DISABLE EFFECTS
		
	if(!target.r)  {
		startrgb = ColorConv(start);
		r = startrgb[0];
		g = startrgb[1];
		b = startrgb[2];
		target.r = r;
		target.g = g;
		target.b = b;
	}
	rint = Math.round(Math.abs(target.r-er)/steps);
	gint = Math.round(Math.abs(target.g-eg)/steps);
	bint = Math.round(Math.abs(target.b-eb)/steps);
	if(rint == 0) { rint = 1 }
	if(gint == 0) { gint = 1 }
	if(bint == 0) { bint = 1 }
	target.step = 1;
	target.colorTimer = setInterval( function() { AnimateColor(id,element,steps,er,eg,eb,rint,gint,bint,callback) }, speed);
	return true;
}

// incrementally close the gap between the two colors //
function AnimateColor(id,element,steps,er,eg,eb,rint,gint,bint,callback) {
	var target;
	if (typeof(id)=="string") { target = $(id); }
	else { target = id; }

	var color;
	if(target.step <= steps)
	{
		var r = target.r;
		var g = target.g;
		var b = target.b;
		if(r >= er)	{ r = r - rint; }
		else { r = parseInt(r) + parseInt(rint); }
		if(g >= eg) { g = g - gint; }
		else { g = parseInt(g) + parseInt(gint); }
		if(b >= eb)	{ b = b - bint; }
		else { b = parseInt(b) + parseInt(bint); }
		color = 'rgb(' + r + ',' + g + ',' + b + ')';	
	
		try {	
			if(element == 'background') {
				target.style.backgroundColor = color;
			} else if(element == 'border') {
				target.style.borderColor = color; 
			} else {
				target.style.color = color;
			}
		} catch (ex) {}
		target.r = r;
		target.g = g;
		target.b = b;
		target.step++;
	} else {
		clearInterval(target.colorTimer);
		color = 'rgb(' + er + ',' + eg + ',' + eb + ')';
		if(element == 'background')	{
		target.style.backgroundColor = color;
		} else if(element == 'border') {
			target.style.borderColor = color;
		} else {
			target.style.color = color;
		}
		if (typeof(callback) == 'function') callback();
	}
}

// convert the color to rgb from hex //
function ColorConv(color) {
	var rgb = [parseInt(color.substring(0,2),16), 
	parseInt(color.substring(2,4),16), 
	parseInt(color.substring(4,6),16)];
	return rgb;
}
function RGBtoHex(R,G,B) {return IntToHex(R)+IntToHex(G)+IntToHex(B)}
function IntToHex(N) {
	if (N==null) return "00";
	N=parseInt(N); if (N==0 || isNaN(N)) return "00";
	N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
	return "0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16);
}



// Picturish
function ResizeFade(id,startWidth,endWidth,startHeight,endHeight,callback,steps,speed) {
	Debug("ResizeFade on "+id,"FX");
	var target;
	if (typeof(id)=="string") {
	target = $(id);
	} else {
	target = id;
	}
	steps = steps || 10;
	speed = speed || 10;
	if (target.style.display=='none') target.style.display='';
	if (target.style.visibility=='hidden') target.style.visibility='visible';
	
	clearInterval(target.resizeTimer);

	if (startWidth == null) startWidth = target.clientWidth;
	if (startHeight == null) startHeight = target.clientHeight;
	if (endWidth == null) endWidth = target.clientWidth;
	if (endHeight == null) endHeight = target.clientHeight;

	target.widthUnits = 'px';
	target.heightUnits = 'px';

	if (Alpha(endWidth)=='%') target.widthUnits = '%';
	if (Alpha(endHeight)=='%') target.heightUnits = '%';

	target.startWidth = Numeric(startWidth);
	target.endWidth =  Numeric(endWidth);
	
	target.startHeight =  Numeric(startHeight);
	target.endHeight =  Numeric(endHeight);
	
	target.style.width = target.startWidth + target.widthUnits;
	target.style.height = target.startHeight + target.heightUnits;
	
	if(!themesConfig.effects) steps=1; // DISABLE EFFECTS
	
	target.step = 1;
	target.resizeTimer = setInterval( function () { AnimateResize(id,steps,callback) }, speed);
}
function AnimateResize(id,steps,callback) {
	var target;
  if (typeof(id)=="string") {
  	target = $(id);
  } else {
  	target = id;
  }
	var step = target.step;
	var width = (target.startWidth + (target.endWidth - target.startWidth) * (step / steps));
	var height = (target.startHeight + (target.endHeight - target.startHeight) * (step / steps));
	if (target.step <= steps) 
	{
	  target.style.width = width + target.widthUnits;
	  target.style.height = height + target.heightUnits;
	  target.step++;
	} else {
	  target.style.width = target.endWidth + target.widthUnits;
	  target.style.height = target.endHeight + target.heightUnits;
	  clearInterval(target.resizeTimer);
	  if (typeof(callback) == 'function') callback();
	}
}

// Picturish
function ChangeOpacity(id, opacity) {
	var target;	
	if (typeof(id)=="string") { target = $(id); }
	else { target = id; }
	
	try {
		if (target.filters) {
			try {
				target.filters.item("DXImageTransform.Microsoft.Alpha").opacity = opacity;
			} catch (e) {
				target.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + opacity + ')';
			}
		}
	} catch(e) {}
	
	target.style.opacity = opacity / 100;

}
function OpacityFade(id,startOpacity,endOpacity,callback,steps,speed) {
	Debug("OpacityFade on "+id,"FX"); // DEBUG
	var target;
	if (typeof(id)=="string") { target = $(id); }
	else { target = id; }
	steps = steps || 20;
	speed = speed || 20;	
	if (target.style.display=='none') target.style.display='';
	if (target.style.visibility=='hidden') target.style.visibility='visible';
	
	target.fxOpacityTimer = (new Date()).getTime(); // DEBUG	
	
	clearInterval(target.opacityTimer); // RENEW FX
	
	if (startOpacity==null) { startOpacity = target.style.opacity * 100; }
	target.startOpacity = startOpacity;
	target.endOpacity = endOpacity;

	ChangeOpacity(target,startOpacity);
	
	if(!themesConfig.effects) steps=1; // DISABLE EFFECTS
	target.step = 1;
	target.opacityTimer = setInterval( function () { AnimateOpacity(id,steps,callback) }, speed);
}
function AnimateOpacity(id,steps,callback) {
	var target;
	if (typeof(id)=="string") {
	target = $(id);
	} else {
	target = id;
	}
	var step = target.step;
	var opacity;
	
	if (target.endOpacity < target.startOpacity)
	{
		opacity = target.startOpacity + (target.endOpacity - target.startOpacity) * (step / steps);
	} else {
		opacity = target.startOpacity + (target.endOpacity - target.startOpacity) * (step / steps);
	}

	if (target.step <= steps)
	{
	  ChangeOpacity(target,opacity);	
	  target.step++;
	} else {
	  ChangeOpacity(target,target.endOpacity);
	  //if(target.endOpacity==0) target.style.display="none";

	  target.fxOpacityElapsed = ((new Date).getTime()-target.fxOpacityTimer)
	  Debug("Opacity Fade took " + target.fxOpacityElapsed + "/" + target.step + "=" + Math.ceil(1000*target.step/target.fxOpacityElapsed) + "FPS on " + target.id + " " + target,"BENCHMARK"); // DEBUG
	  document.fxOpacityFrames += target.step;
	  document.fxOpacityTime += target.fxOpacityElapsed;

	  clearInterval(target.opacityTimer);
	  if (typeof(callback) == 'function') callback();
	}
}
// DEPRECATED
function addImage(picture, targetElementId, callback) {
	return;
}

// PICTURISH CLASS

function GetScrollY() {
	if (isIE()) { return document.body.parentElement.scrollTop; }
	else { return window.scrollY; }
}

function IsHidden(id) {
	var target;	
	if (typeof(id)=="string") { target = $(id); }
	else { target = id; }
	
	if(target.style.display=="none"||target.style.visibility=="hidden"||target.style.opacity==0)
		return true;
	else
		return false;
}

// credits: http://andylangton.co.uk/articles/javascript/get-viewport-size-javascript/
function GetViewport() {
	var viewportWidth;
	var viewportHeight;
	
	if (typeof window.innerWidth != 'undefined')
	{
		viewportWidth = window.innerWidth,
		viewportHeight = window.innerHeight
	} else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
		viewportWidth = document.documentElement.clientWidth,
		viewportHeight = document.documentElement.clientHeight
	} else {
		viewportWidth = document.getElementsByTagName('body')[0].clientWidth,
		viewportHeight = document.getElementsByTagName('body')[0].clientHeight
	}

	result={width: viewportWidth, height: viewportHeight};
	//Debug("GetViewport "+var_dump(result));
	return result;
}
function FindOffsetTop(id) {
	var target;	
	if (typeof(id)=="string") { target = $(id); }
	else { target = id; }	
	var curtop = 0;
	if (target.offsetParent) {
		do {
			curtop += target.offsetTop;
		} while (target = target.offsetParent);
	return [curtop];
	}
}

function OpenDiv(id,callback,noScroll) {
	var target;	
	if (typeof(id)=="string") { target = $(id); }
	else { target = id; }
	target.style.display="";
	target.style.height="";
	OpacityFade(target,0,100, function () {
		if(!noScroll) { window.scrollTo(0,FindOffsetTop(target)); }
		if(typeof(callback)=="function") callback();
	} );
}
function CloseDiv(id,callback,disableResizeFade) {
	var target;	
	if (typeof(id)=="string") { target = $(id); }
	else { target = id; }
	target.style.overflow = "hidden";
	
	if(disableResizeFade) { 
		OpacityFade(target,100,0, function () {
			target.style.height="0px";
			if(typeof(callback)=="function") callback();
		} );
	} else {
		OpacityFade(target,100,0);
		ResizeFade(target,null,null,null,0,callback);
	}
}

/* Main functions */
function MakeButton(id,onclick,width) {
	var contentDiv = $(id).cloneNode(true);
		contentDiv.className = "button";					
		contentDiv.onclick = onclick;
		
		contentDiv.style.width = (width)?width+'px':'';
		contentDiv.style.margin = "auto";
		$(id).parentNode.replaceChild(contentDiv, $(id));			
}
function MakeContainer(id, title, useSubFrame, containerStyle) {
	var mainName = id.replace(/Div\b/g,'');
	var containerDiv = document.createElement('div');
		containerDiv.id = id;
		containerDiv.className = 'containerMainContent';
		containerDiv.style.backgroundColor = '#'+themes[themesConfig.current].bar;						
		var topDiv = document.createElement('div');
			topDiv.id = mainName+'TopDiv';
			topDiv.className = 'containerTitleBar';
			topDiv.style.backgroundColor = '#'+themes[themesConfig.current].bar;
			topDiv.innerHTML = title;
		if (useSubFrame) {
			var subFrame = document.createElement('div');
				subFrame.className = "subFrame";
				var subFrameTop = document.createElement('div');
					subFrameTop.className = "subFrameTop";
				var subFrameContent = document.createElement('div');
					subFrameContent.className = "subFrameContent";
				var subFrameBottom = document.createElement('div');
					subFrameBottom.className = "subFrameBottom";
				subFrame.appendChild(subFrameTop);
				subFrame.appendChild(subFrameContent);
				subFrame.appendChild(subFrameBottom);
		}
		var contentDiv = $(id);
			contentDiv.id = mainName+'ContentDiv';
			contentDiv.style.padding = (!useSubFrame)?"10px":"";
		var bottomDiv = document.createElement('div');
			bottomDiv.id = mainName+'BottomDiv';
			bottomDiv.style.clear = "both";
			bottomDiv.style.overflow = "auto";
			bottomDiv.style.height = "auto";
			var leftCornerImg = document.createElement('img');
				leftCornerImg.src = "http://picturish.com/images/bottom-left-corner.png";
				leftCornerImg.align = "left";
			var rightCornerImg = document.createElement('img');
				rightCornerImg.src = "http://picturish.com/images/bottom-right-corner.png";
				rightCornerImg.align = "right";							
			bottomDiv.appendChild(leftCornerImg);
			bottomDiv.appendChild(rightCornerImg);

	containerDiv.style.display=contentDiv.style.display;
	contentDiv.style.display='';
	contentDiv.style.clear='both';
	containerDiv.appendChild(topDiv);
	if (useSubFrame) {
		containerDiv.appendChild(subFrame);
			subFrameContent.appendChild(contentDiv.cloneNode(true));
	} else {
		containerDiv.appendChild(contentDiv.cloneNode(true));
	}
	containerDiv.appendChild(bottomDiv);
						
	contentDiv.parentNode.replaceChild(containerDiv, contentDiv);
}
