// create new script container
    function loadjs(file) {
		var js_file = document.createElement('script')
		js_file.setAttribute('type', 'text/javascript')
		js_file.setAttribute('src', file)
		// append new script file to the "head" container
		document.getElementsByTagName('head')[0].appendChild(js_file);
	}
	
	
	var src = document.getElementById('updater_js').getAttribute('src');
			var src_path = src.split('updater');
	
	if(!jQuery) {
	loadjs(src_path[0]+'updater/js/jquery.min.js');
	}
	loadjs(src_path[0]+'updater/js/base64.class.js');
	
	
	//LOAD $upd JQUERY 
	if(typeof($upd)=='undefined')  {
			var $upd = jQuery.noConflict();
			
			if(typeof(no_jquery_define)=='undefined')  { 
				var $ = jQuery.noConflict();
			}
	}

	$upd.ajax({ dataType:'json', url: src_path[0]+"updater/update.php?ajax", success: function(data){ 
				window.jdata = data;
					var upd_count = 0;
					window.d=0;
					//alert(data['files']);
				if(data!=null && data['files']!=null && data['files']) {
						var index_count='';
						window.updates=0;
					$upd.each(data['files'], function (index,val) {
						if(index_count!=index) { index_count=index; window.updates++; }
						upd_count = upd_count + val.length;
					});
				}

				window.upd_count = upd_count;
			
		if(upd_count==0) return false;			

			if(upd_count > 0) {
					
				$upd('head').append('<link rel="stylesheet" href="'+src_path[0]+'updater/images/style.css" type="text/css" />');
				$upd('body').prepend('<div id="updater_layer" style="display:none;">--</div>');
				
				$upd('div#updater_pl').css('height',$upd('body').height());
				
				$upd("html:not(:animated),body:not(:animated)").animate({scrollTop: 0}, 1100);
				
				$upd('#updater_layer').load(src_path[0]+'updater/views/start.php', function () {  
				
				$upd('#updater_over h3').css('display','block');
				$upd('a.big_button').css('display','block');
				
				$upd('#updater_over h3').animate({'opacity':0}, 1, function () {$upd('#updater_over h3').css('display','block'); });
				$upd('a.big_button').animate({'opacity':0}, 1, function () {$upd('a.big_button').css('display','block'); });
	
				$upd('#updater_over h3').animate({'opacity':1}, 1000, function () { 
					$upd('a.big_button').animate({'opacity':0.8}, 1000);
				});
				
	
				$upd('#updater_over').animate({'opacity':0}, 1, function () {$upd('#updater_layer').css('display','block'); });
				$upd('div#updater_pl').animate({'opacity':0},1, function () { $upd(this).css('display','block'); $upd(this).animate({'opacity':0.85},200,function(){
				$upd('#updater_debug').html('start');  $upd('#updater_over').animate({'height':315, 'opacity':1}, 700, function () { $upd('#updater_over div.header').css('display','block'); $upd('#updater_over div.content').css('display','block').animate({'opacity':1}, 300, function() {
			
			$upd('a.big_button').hover(function(){$upd(this).css({"opacity":1});}, function() {$upd(this).css({"opacity":0.8});});
			
			} ); }  ); 
			
			}); });
			

			});

	}
	} });
	
	

function var_dump(obj) {
   if(typeof obj == "object") {
      return "Type: "+typeof(obj)+((obj.constructor) ? "\nConstructor: "+obj.constructor : "")+"\nValue: " + obj;
   } else {
      return "Type: "+typeof(obj)+"\nValue: "+obj;
   }
}//end funct	

	
function install(index) {
}
	
function recur_ajax(files, files_c) {

	var val = files[window.it];
		if(typeof(val)=="undefined") return false;
	var val2 = val.split('==');
	var index = val2[val2.length-1];
		
		if(window.cat != window.it) {
			$upd('a#unique_big').html('Идет установка...');
			window.cat = window.it;
		}

	$upd.ajax({ type: "POST", async:true, url: src_path[0]+"updater/update.php?ajax", data: "path="+val, success: function(html){
		
		var descr = '';
		
		if(window.jdata['descr']) { if(window.jdata['descr'][index]) { 	var descr = Base64.decode(window.jdata['descr'][index]); }}

			window.it++;
			window.it2++;


		$upd('#updater_process').html(descr+'<br>'+val2[val2.length-3]+'<br>'+window.it2+' из '+files_c[index].length);
		$upd('#process_bar').css('width', function () { window.i++; var bar2 = (window.i/window.upd_count)*480; return bar2; });
				
			if(files_c[index].length==window.it2) { 
				window.it2=0; 
				//install(index); 
			
			$upd('a#unique_big').html('Устанавливается');
	$upd.ajax({ type: "POST", async:true, url: src_path[0]+"updater/update.php?ajax", data: "init="+index, success: function(html){ 
		$upd('a#unique_big').html('Установлено'); $upd('#updater_process').html('Обновление установлено. '+html); 
			
			if(window.it==files.length) { 
					$upd('a#unique_big').html('Перезагрузка');
$upd('div#updater_over').animate({'opacity':0},700, function() {  
					$upd('div#updater_pl').animate({'opacity':0},700, function () {
						location.reload();
				});
			});	
				return true;
			}

			//recur_ajax(files, files_c);
			
					}});
			recur_ajax(files, files_c);
			}
			else {	
			recur_ajax(files, files_c);
			}

	}});		
	return true;
}	
	
	
	

function blink(obj, val1, val2, speed, once) {
				if(once==1) { eval('window.'+$upd(obj).attr('id')+'_stop=0;'); }
		var w = eval('window.'+$upd(obj).attr('id')+'_stop;');
		if ((obj.stop == 1 || w == 1)  && val2 == 1) {
			return false; 
		}
	$upd(obj).animate({'opacity':val1},speed, function () {
		blink(obj, val2, val1, speed);
	});
}
	
	
	
function get_updates() {

	$upd('div#updater_process').html('Обновление устанавливается.');
	
	blink($upd('a#unique_big'),0.5,1,500,1);
	
	$upd('a.big_button').unbind('mouseenter mouseleave');
	$upd('a.big_button').css('cursor','default');

	document.getElementById("unique_big").removeAttribute('onclick');
	
$upd("a.big_button").click(function(){return false;});
	
		 window.i=0;
			$upd('#process_bar').css({width:0});

			window.fin=0;
			var files_b = new Array();
			var files_c = new Array();
			
			var i = 0;
$upd.each(window.jdata['files'], function (index,val) {
			var files = val;
			var i2 = 0;
			files_c[index] = new Array();
						files.sort();

$upd.each(files, function (index2,val2) {
			files_b[i]=val2;
			files_c[index][i2]=1;
			i++;
			i2++;
				});
//start

});

	window.it=0;
	window.it2=0;
	
	recur_ajax(files_b, files_c);
	
		//window.unique_big_stop=1;

		return false;
	}
	
	
	
function strstr (haystack, needle, bool) {
    var pos = 0;
    
    haystack += '';
    pos = haystack.indexOf( needle );    if (pos == -1) {
        return false;
    } else{
        if (bool){
            return haystack.substr( 0, pos );        } else{
            return haystack.slice( pos );
        }
    }
}

