/**
 * 		GrafXJS core
 * 		  1.1.0.000
 *
 * @author NEO III
 * @date 2008
 *
 * Licence :
 * Vous pouvez utiliser, distribuer et modifier ce script à condition de laisser ce commentaire,
 * et de citer le nom de l'auteur (NEO III) sur la/les pages où le scrpit est utilisé.
 * Pour plus d'informations, visitez le site : http://neoprog.dedikam.com/GrafXJS
 *
 */
/** variables **/

var plugs = new Array(new Array("", "Core"));

/** fonctions **/

function g(id){
	return document.getElementById(id);
}

function GrafXJS(){
	/** Core **/
	this.coreNumVer = "110000";
	var coreNumVerSbstr = this.coreNumVer.substring(0, 1);
	for (i = 1; i < 3; i++) {
		coreNumVerSbstr += "." + this.coreNumVer.substring(i, i + 1);
	}
	coreNumVerSbstr += "." + this.coreNumVer.substring(3, 6);
	this.coreVer = coreNumVerSbstr;
	this.plugins = function(type){
		var theplugs = "";
		if (type == "js") {
			for (i = 0; i < plugs.length; i++) {
				theplugs += plugs[i][1] != "" ? plugs[i][1] : plugs[i][0];
				theplugs += "\n";
			}
		}
		else {
			for (i = 0; i < plugs.length; i++) {
				theplugs += plugs[i][1] != "" ? plugs[i][1] : plugs[i][0];
				theplugs += "<br>";
			}
		}
		return theplugs;
	}
	this.about = function(type){
		if (type == "js") {
			return "GrafXJS\nCore version " + this.coreVer + "\n2008, NEO III";
		}
		else {
			return "GrafX JS<br>Core version " + this.coreVer + "<br>2008, NEO III";
		}
	}
	this.include = function(plug, name){
		document.write("<script language=\"javascript\" src=\"" + plug + "\"></script>");
		var numplug = plugs.length;
		plugs[numplug] = new Array(plug);
		plugs[numplug][1] = name ? name : "";
	}
	this.includeVBS = function(plug, name){
		document.write("<script language=\"vbscript\" src=\"" + plug + "\"></script>");
		var numplug = plugs.length;
		plugs[numplug] = new Array(plug);
		plugs[numplug][1] = name ? name : "";
	}
	/** images functions **/
	this.image = function(conteneur, image, id){
		if (image.substring(image.length - 3, image.length).toLowerCase() != "gif" && image.substring(image.length - 3, image.length).toLowerCase() != "jpg" && image.substring(image.length - 4, image.length).toLowerCase() != "jpeg" && image.substring(image.length - 3, image.length).toLowerCase() != "bmp" && image.substring(image.length - 3, image.length).toLowerCase() != "png") {
			return false;
		}
		var GxJSimg = document.createElement("img");
		GxJSimg.id = id;
		GxJSimg.src = image;
		g(conteneur).appendChild(GxJSimg);
	}
	this.imageFloat = function(conteneur, image, id, x, y){
		if (image.substring(image.length - 3, image.length).toLowerCase() != "gif" && image.substring(image.length - 3, image.length).toLowerCase() != "jpg" && image.substring(image.length - 4, image.length).toLowerCase() != "jpeg" && image.substring(image.length - 3, image.length).toLowerCase() != "bmp" && image.substring(image.length - 3, image.length).toLowerCase() != "png") {
			return false;
		}
		var GxJSimgFlo = document.createElement("div");
		GxJSimgFlo.id = id;
		GxJSimgFlo.style.cssText = "position:absolute;top:" + x + "px;left:" + y + "px";
		g(conteneur).appendChild(GxJSimgFlo);
		new GrafXJS().image(id, image, id + "_imgFloat");
	}
	this.animatedPNG = function(conteneur, image, h, id, fps, maxFrame, x_frame){
		if (image.substring(image.length - 3, image.length).toLowerCase() != "png") {
			return false;
		}
		var GxJSaniPng = document.createElement("div");
		GxJSaniPng.id = id;
		GxJSaniPng.style.cssText = "border:none 0px;padding:0px;height:" + h + "px;width:" + x_frame + "px;background:url('" + image + "') " + x_frame + "px 0px repeat-x";
		g(conteneur).appendChild(GxJSaniPng);
		setTimeout("animerpng(\'" + GxJSaniPng.id + "\',\'" + image + "\'," + fps + "," + maxFrame + "," + x_frame + ",0)", fps);
	}
	this.animatedPNGimage = function(conteneur, image, h, id, fps, maxFrame, x_frame){
		var GxJSaniPngi = document.createElement("img");
		GxJSaniPngi.id = id;
		//GxJSaniPng.style.cssText = "border:none 0px;padding:0px;height:" + h + "px;width:" + x_frame + "px;background:url('" + image + "') " + x_frame + "px 0px repeat-x";
		GxJSaniPngi.src = image + "_1.png";
		g(conteneur).appendChild(GxJSaniPngi);
		setTimeout("animerpngimage(\'" + id + "\',\'" + image + "\'," + fps + "," + maxFrame + "," + x_frame + ",1)", fps);
	}
	/** 2D functions **/
	this.rectangle = function(conteneur, L, l, id, x, y, borderStyle, Bkg, contenu){
		var GxJSrect = document.createElement('div');
		GxJSrect.id = id;
		var cssvars = "width:" + L + ";height:" + l + ";position:absolute;top:" + y + ";left:" + x + ";border:" + borderStyle + ";background:" + Bkg;
		if (arguments[9]) 
			cssvars += ";" + arguments[9];
		GxJSrect.style.cssText = cssvars;
		g(conteneur).appendChild(GxJSrect);
		g(id).innerHTML = contenu;
	}
	this.cercle = function(conteneur, r, e, id, x, y, color, resolution){
		if (r < 1 || r > 200 || !isFinite(r)) {
			return false
		};
		var GxJScerc = document.createElement("div");
		GxJScerc.id = id;
		g(conteneur).appendChild(GxJScerc);
		var reso = 0.5;
		switch (resolution) {
			case 0:
				reso = 1;
				break;
			case 1:
				reso = 0.5;
				break;
			case 2:
				reso = 0.3;
				break;
			case 3:
				reso = 0.1;
				break;
			default:
				reso = 0.5;
				break;
		}
		for (i = 0; i < 360; i += reso) {
			a = i * Math.PI / 180;
			xi = Math.cos(a) * r;
			yi = Math.sin(a) * r;
			var pt = document.createElement('div');
			pt.appendChild(document.createTextNode('.'));
			pt.style.cssText = 'color:' + color + ';width:10px;font-size:' + e + 'px;position:absolute;top:' + (y + parseFloat(yi + r)) + 'px;left:' + (x + parseFloat(xi + r)) + 'px';
			g(id).appendChild(pt);
		}
		
	}
	/** fader functions **/
	this.opacity = function(id, opacStart, opacEnd, millisec){
		var speed = Math.round(millisec / 100);
		var timer = 0;
		if (opacStart > opacEnd) {
			for (i = opacStart; i >= opacEnd; i--) {
				setTimeout("new GrafXJS().changeOpac(" + i + ",'" + id + "')", (timer * speed));
				timer++;
			}
		}
		else 
			if (opacStart < opacEnd) {
				for (i = opacStart; i <= opacEnd; i++) {
					setTimeout("new GrafXJS().changeOpac(" + i + ",'" + id + "')", (timer * speed));
					timer++;
				}
			}
	}
	this.changeOpac = function(opacity, id){
		var object = document.getElementById(id).style;
		object.opacity = (opacity / 100);
		object.MozOpacity = (opacity / 100);
		object.KhtmlOpacity = (opacity / 100);
		object.filter = "alpha(opacity=" + opacity + ")";
	}
	this.shiftOpacity = function(id, millisec){
		if (document.getElementById(id).style.opacity == 0) {
			opacity(id, 0, 100, millisec);
		}
		else {
			opacity(id, 100, 0, millisec);
		}
	}
	this.blendimage = function(divid, imageid, imagefile, millisec){
		var speed = Math.round(millisec / 100);
		var timer = 0;
		document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
		changeOpac(0, imageid);
		document.getElementById(imageid).src = imagefile;
		for (i = 0; i <= 100; i++) {
			setTimeout("new GrafXJS().changeOpac(" + i + ",'" + imageid + "')", (timer * speed));
			timer++;
		}
	}
	this.currentOpac = function(id, opacEnd, millisec){
		var currentOpac = 100;
		if (document.getElementById(id).style.opacity < 100) {
			currentOpac = document.getElementById(id).style.opacity * 100;
		}
		new GrafXJS().opacity(id, currentOpac, opacEnd, millisec)
	}
	this.fader = function(hideid, showid, speed){
		new GrafXJS().opacity(hideid, 100, 0, speed);
		setTimeout("g('" + hideid + "').style.display='none';g('" + showid + "').style.display='block';new GrafXJS().opacity('" + showid + "',0 ,100, " + speed + ")", speed);
	}
	/** move object function **/
	this.moveObjectTo = function(id, x, y, org_x, org_y, time){
		// not implemented yet
	}
	/** delete function **/
	this.deleteElement = function(element, conteneur){
		g(conteneur).removeChild(g(element));
	}
	/** Flash functions **/
	this.Flash = function(src){
		var source = src;
		var h = this.arguments[1]?this.arguments[1]:576;
		var w = this.arguments[2]?this.arguments[2]:720;
		this.Create = function(id){
			var string = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="'+ w +'" height="'+ h +'" id="fullscreen" align="middle">\n';
			string += '<param name="allowScriptAccess" value="sameDomain" />\n';
			string += '<param name="movie" value="'+ source +'" />\n';
			string += '<param name="allowFullScreen" value="true" />\n';
			string += '<param value="transparent" name="wmode" />\n';
			string += '<param value="transparent" name="embed"  />\n';
			string += '<param name="quality" value="high" />\n';
			string += '<param name="loop" value="0" />\n';
			string += '<embed src="'+ source +'" loop="0" quality="high" wmode="transparent" embed="transparent" allowFullScreen="true" width="'+ w +'" height="'+ h +'" name="fullscreen" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />\n';
			string += '</object>';
			g(id).innerHTML += string;
		}
		
	}
}

/** animate function for animated PNG **/

function animerpng(id, image, fps, maxframe, x_frame, x_current){
	if (g(id)) {
		g(id).style.background = "url('" + image + "') " + (x_current + x_frame) + "px 0px repeat-x";
		x_current += x_frame;
		setTimeout("animerpng(\'" + id + "\',\'" + image + "\'," + fps + "," + maxframe + "," + x_frame + "," + x_current + ")", fps);
	}
}

function animerpngimage(id, image, fps, maxframe, x_frame, x_current){
	try {
		if (g(id)) {
			if (x_current != (maxframe + 1)) {
				g(id).src = image + "_" + x_current + ".png";
				x_current++;
				setTimeout("animerpngimage(\'" + id + "\',\'" + image + "\'," + fps + "," + maxframe + "," + x_frame + "," + x_current + ")", fps);
			}
			else {
				x_current = 1;
				g(id).src = image + "_" + x_current + ".png";
				setTimeout("animerpngimage(\'" + id + "\',\'" + image + "\'," + fps + "," + maxframe + "," + x_frame + "," + x_current + ")", fps);
			}
		}
	} 
	catch (e) {
	}
}

/** includes for Core **/

new GrafXJS().include('GxJS_Core/GxJS_md5.js', "MD5 Hash");
