$(function(){
if ($.browser.msie && $.browser.version < 9) {} else {

if ($.cookie('optHead') != 'false') { 

    if ($.cookie('tbs-js-disable-sky-animation') != 'true') {
	   $('#head-background').html('<canvas id="oblHeader" width="1000" height="148"></canvas>');

	   // 
	   var canvas = $('#oblHeader')[0];

	   // Получение сцены =))
	   var oblScene = new Kinetic.Stage(canvas);

	   // Функция получения случайного числа из указанного диапазона
	   getRandomInt = function(min, max) {
		  return Math.floor(Math.random() * (max - min + 1)) + min;
	   }

	   // Массив-хранилище
	   var obl = {
		  obl0: {
			 src: '/public/img/oblaka/0obl.png',
			 linearSpeed: 17 * oblScene.getTimeInterval() / 1000,
			 width: 131,
			 height: 74,
			 x: getRandomInt(0, canvas.width),
			 y: getRandomInt(-39, 75)
		  },
		  obl1: {
			 src: '/public/img/oblaka/0obl.png',
			 linearSpeed: 17 * oblScene.getTimeInterval() / 1000,
			 width: 131,
			 height: 74,
			 x: getRandomInt(0, canvas.width),
			 y: getRandomInt(-39, 75)
		  },

		  obl2: {
			 src: '/public/img/oblaka/1obl.png',
			 linearSpeed: 13 * oblScene.getTimeInterval() / 1000,
			 width: 89,
			 height: 51,
			 x: getRandomInt(0, canvas.width),
			 y: getRandomInt(-25, 75)
		  },
		  obl3: {
			 src: '/public/img/oblaka/1obl.png',
			 linearSpeed: 13 * oblScene.getTimeInterval() / 1000,
			 width: 89,
			 height: 51,
			 x: getRandomInt(0, canvas.width),
			 y: getRandomInt(-25, 75)
		  },

		  obl4: {
			 src: '/public/img/oblaka/2obl.png',
			 linearSpeed: 9 * oblScene.getTimeInterval() / 1000,
			 width: 66,
			 height: 39,
			 x: getRandomInt(0, canvas.width),
			 y: getRandomInt(-18, 75)
		  },
		  obl5: {
			 src: '/public/img/oblaka/2obl.png',
			 linearSpeed: 9 * oblScene.getTimeInterval() / 1000,
			 width: 66,
			 height: 39,
			 x: getRandomInt(0, canvas.width),
			 y: getRandomInt(-18, 75)
		  },

		  obl6: {
			 src: '/public/img/oblaka/3obl.png',
			 linearSpeed: 9 * oblScene.getTimeInterval() / 1000,
			 width: 50,
			 height: 30,
			 x: getRandomInt(0, canvas.width),
			 y: getRandomInt(-15, 75)
		  },
		  obl7: {
			 src: '/public/img/oblaka/3obl.png',
			 linearSpeed: 9 * oblScene.getTimeInterval() / 1000,
			 width: 50,
			 height: 30,
			 x: getRandomInt(0, canvas.width),
			 y: getRandomInt(-15, 75)
		  },
		  obl8: {
			 src: '/public/img/oblaka/3obl.png',
			 linearSpeed: 9 * oblScene.getTimeInterval() / 1000,
			 width: 50,
			 height: 30,
			 x: getRandomInt(0, canvas.width),
			 y: getRandomInt(-15, 75)
		  }
	   }
	   if ($.cookie('tbs-js-disable-sky-animation') == 'true') { } else {
		  // set updateStage method
		  oblScene.setUpdateStage(function(){
				for(i in obl) {
				    if (obl[i].x < canvas.width) {
					   obl[i].x += obl[i].linearSpeed;
				    } else {
					   if (getRandomInt(1, 50) == 1) {
						  obl[i].x = -obl[i].width;
						  obl[i].y = getRandomInt(-39, 75);
					   }
				    }
				}
		  });
	   }


	   // set drawStage method
	   oblScene.setDrawStage(function(){
	   var context = oblScene.getContext();

	   for(i in obl) {
		  if (!obl[i].obj) {
			 obl[i].obj = new Image();
			 obl[i].obj.src = obl[i].src;
			 obl[i].obj.onload = function(){
				context.drawImage(obl[i].obj, obl[i].x, obl[i].y);
			 };
		  } else {
			 context.drawImage(obl[i].obj, obl[i].x, obl[i].y);
		  }
	   }
	   });

	   // start animation
	   oblScene.start();
    }
}
}
});
