//////////////////////////////////////////////////
//      Copyright 2011 SVSD_VAL
//     
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.
//
//	Site  : http://rtbt.ru
//	Jabber: svsd_val@jabber.ru & svsd_val@qip.ru
//	MailTo: valdim_05@mail.ru
//////////////////////////////////////////////////

const
MaxSpeed     = 4;
screenWidth  = 320;
screenHeight = 240;
fps = 25;
spf = 1/fps * 1000;

var
curvar,curid,rtext, logo,last_time, frame_time, h_timer, mousex = mousey = mousekey = mouse_offsetx = mouse_offsety =0;

l = { pos : { x: screenWidth*0.5, y: screenHeight*0.5 }, acc : { x: 1, y:1 } , rotate : 0, scale: 0, rfact:0};

info_text 	='Сайт Разработчиков Игр\n\n'
		+'Kav:\n'
		+'Программист и пацифист\n'
		+'неплохо варит во всём\n\n'

		+'Keha:\n'
		+'Моделер, Художник\n'
		+'Прогер и\n'
		+'очень хороший человек\n\n'

		+'Zephyr:\n'
		+'Любитель всего всего\n'
		+'древнего (Sega,Nintendo)\n'
		+'и многого другого\n'
		+'умеет много чего\n\n'

		+'Чёкнутый SVSD_VAL:\n'
		+'Вумный но ленивый очень\n'
		+'знает много,но пока не\n'
		+'пнёшь не полетит :(\n\n'

		+'Сеньор SinnerSeth:\n'
		+'Неплохой моделер,\n'
		+'однако так же нуждается\n'
		+'в стимулах ...\n\n'
		+'В общем хорошая\n'
		+'и весёлая тима ^_^';
rinfo;


function loadimg() {
  logo	 =  new Image();
  logo.src	 = "http://rtbt.ru/img/logo.png";
  var rinfo = info_text.split('\n');

  rtext = new Array( rinfo.length );
  for (i=0; i< rtext.length;i++)
  rtext[i]   = { hint : rinfo[i] , pos : { x:0, y:0 }, org: { x:25, y: screenHeight + i *24 } , color: '#000', rtime:0, canmove:0, splash:0 };
}

function onMouseMove(e) {
//Left button   – 1
//Middle button – 4
//Right button  – 2
// if (e.which) rightclick = (e.which == 3);
// else if (e.button) rightclick = (e.button == 2);

	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) {
		posx = e.pageX - canvas.offsetLeft;
		posy = e.pageY - canvas.offsetTop;
	} else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft - canvas.offsetLeft;
		posy = e.clientY + document.body.scrollTop  + document.documentElement.scrollTop  - canvas.offsetTop;
	}

	mouse_offsetx = mousex - posx;
	mouse_offsety = mousey - posy;

	mousex = posx;
	mousey = posy;
}


function onClick(e) {
}

function initialize() {
	canvas		= document.getElementById('canvas');
	canvas.width	= screenWidth;
	canvas.height	= screenHeight;
	if (canvas.getContext) ctx = canvas.getContext('2d'); else return;
	canvas.addEventListener("mousemove", onMouseMove, false);
	canvas.addEventListener("mousedown", onClick, false);
	canvas.addEventListener("mouseup"  , function (e) { mousekey = 0 }  , false);

	loadimg();
	frame_time = 0;
	curid      = 0;
	curvar     = Math.floor(Math.random() *1.71);
	last_time  = (new Date).getTime()+16;

	ctx.fillStyle = 'rgba(225,225,225,255)';
	ctx.fillRect(0, 0,  canvas.width, canvas.height);
	h_timer    = setInterval( Process ,spf);
}

function DrawImg(image, x,y, rotate, sx,sy,alpha)
{
  var hx = image.width *0.5;
  var hy = image.height*0.5;
  ctx.save();
   ctx.translate(x - hx*sx ,y - hy*sy);
   ctx.scale(sx,sy);
    ctx.translate( hx , hy);
      ctx.rotate(rotate);
    ctx.translate(-hx ,-hy);
   ctx.globalAlpha = alpha;
   ctx.drawImage(image, 0, 0);
  ctx.restore();
}

function clone(obj){
    if(obj == null || typeof(obj) != 'object') return obj;
    var temp = new obj.constructor();
     for (var key in obj) temp[key] = clone(obj[key]);
    return temp;
}

function DrawText2() {

	ctx.textBaseline = 'top';
	ctx.font = 'italic 20px sans-serif';

	for (i=0;i<rtext.length; i++)
	if (rtext[i].hint.length >1)
        {

		rtext[i].org.y += -frame_time *0.01;
		rtext[i].pos.x = rtext[i].org.x;
		rtext[i].pos.y = rtext[i].org.y;
		if ((rtext[i].pos.y > - 24) && (rtext[i].pos.y < screenHeight + 24))
		{
			if (rtext[i].rtime < last_time) {
	
			rtext[i].pos.x = rtext[i].org.x + Math.random()*14-7;
			rtext[i].pos.y = rtext[i].org.y + Math.random()*14-7;
	
			rtext[i].color = 'rgba('+
					Math.floor(Math.random()*255)+','+
					Math.floor(Math.random()*255)+','+
					Math.floor(Math.random()*255)+',1)';
	
				if (rtext[i].rtime+250 < last_time) {
				rtext[i].pos.x = rtext[i].org.x;
				rtext[i].pos.y = rtext[i].org.y;
				rtext[i].rtime = last_time + 2000+ Math.random()*500;
				rtext[i].color = '#000';
				}
			}
		ctx.fillStyle = rtext[i].color;
		ctx.fillText (rtext[i].hint , rtext[i].pos.x,rtext[i].pos.y);
		}
	}
	if (rtext[ rtext.length-1 ].org.y < -canvas.height *0.5)
	for (i=0; i< rtext.length;i++)
	rtext[i].org.y = screenHeight + i *24;

}


function DrawText() {
	function next()
	{
	  ++curid;
	  if (curid > rtext.length) curid =0;
	  if (rtext[curid].hint.length <2) next();
	}

	ctx.textBaseline = 'top';
	ctx.font = 'italic 20px sans-serif';

i=curid;

	if (rtext[i].hint.length >1)
        {

		if (rtext[i].canmove) {
		rtext[i].org.x += -frame_time *4.0;

		if (rtext[i].org.x < -canvas.width * 20) 
		rtext[i].canmove=0;
		rtext[i].splash =0;
		next();
		}
		else
		rtext[i].org.x = 25;
		rtext[i].org.y = canvas.height *0.5 -10;

		rtext[i].pos.x = rtext[i].org.x;
		rtext[i].pos.y = rtext[i].org.y;

			if (rtext[i].rtime < last_time) {
			rtext[i].pos.x = rtext[i].org.x + Math.random()*14-7;
			rtext[i].pos.y = rtext[i].org.y + Math.random()*14-7;
	
			rtext[i].color = 'rgba('+
					Math.floor(Math.random()*255)+','+
					Math.floor(Math.random()*255)+','+
					Math.floor(Math.random()*255)+',1)';
	
				if (rtext[i].rtime+250 < last_time) {
				rtext[i].pos.x = rtext[i].org.x;
				rtext[i].pos.y = rtext[i].org.y;
				rtext[i].rtime = last_time + 2000+ Math.random()*500;
				rtext[i].color = '#000';
				if (++rtext[i].splash > 2) rtext[i].canmove = 1;
				}
			}

		if (rtext[i].org.x > -canvas.width) {
		ctx.fillStyle = rtext[i].color;
		ctx.fillText (rtext[i].hint , rtext[i].pos.x,rtext[i].pos.y);}
		
	}
	if (rtext[ rtext.length-1 ].org.y < -canvas.height *0.5)
	for (i=0; i< rtext.length;i++)
	rtext[i].org.y = screenHeight + i *24;

}


function Process() {
//  ctx.clearRect (0, 0,  canvas.width, canvas.height);
  ctx.fillStyle = 'rgba(225,225,225,0.2)';
  ctx.fillRect(0, 0,  canvas.width, canvas.height);
  ctx.save();
  DrawImg( logo, l.pos.x, l.pos.y, l.rotate, 1.0,1.0, Math.abs(Math.sin(last_time *0.0005)) );
  ctx.restore();
  ctx.strokeRect(0, 0,  canvas.width, canvas.height);

  l.pos.x += l.acc.x * frame_time *0.1;
  l.pos.y += l.acc.y * frame_time *0.1;
  l.rotate+= frame_time*0.0001 * l.acc.x * l.acc.y * l.rfact ;

  if (l.rotate> 3.14) l.rotate = 0;
  var lw = logo.width*0.5 , lh = logo.height *0.5;
  if (l.pos.x+lw < 0) { l.pos.x = -lw; l.acc.x = - l.acc.x; l.rfact = (Math.random(10) - 5) *0.2; }
  if (l.pos.y+lh < 0) { l.pos.y = -lh; l.acc.y = - l.acc.y; l.rfact = (Math.random(10) - 5) *0.2; }
  if (l.pos.x-lw > canvas.width ) { l.pos.x = canvas.width + lw; l.acc.x = - l.acc.x; l.rfact = (Math.random(10) - 5) *0.2; };
  if (l.pos.y-lh > canvas.height) { l.pos.y = canvas.height+ lh; l.acc.y = - l.acc.y; l.rfact = (Math.random(10) - 5) *0.2; };
 
 frame_time = (new Date).getTime() - last_time;
 last_time  = (new Date).getTime();
 if (curvar)
 DrawText(); else DrawText2();
}


