function blinker(code_generated) {
	code_generated.style.borderColor='red';
	setTimeout(function(){code_generated.style.borderColor='black';},500);
}

function set_active(id){
	var m1 = {'code_html':1,'code_bbcode':1};
	var m2 = {'thumb_l':1,'thumb_m':1,'thumb_s':1};
	var m = {};
	if(m1[id]){
		m=m1;
		gallery_active_code['code1'] = id;
	}
	else if(m2[id]){
		m=m2;
		gallery_active_code['code2'] = id;
	}
	for (var k in m){
		if(k == id){
			$(k).className = 'thumb_act';
		}
		else{
			$(k).className = 'thumb_ina';
		}
	}
	blinker($('code_generated'));
	if(gallery_active_code['code1'] && gallery_active_code['code2']){
		var code = gallery_code[gallery_active_code['code1']][gallery_active_code['code2']];
		gallery_gen_button(code);
		$('code_generated').value = code;
	}
	
	return false;
}

function gallery_gen_button(text) {

	var flashvars = {     
		normal: "http://www.ag.ru/i/copy_scr_1.png",
		pressed: "http://www.ag.ru/i/copy_scr_3.png",
		hover: "http://www.ag.ru/i/copy_scr_2.png",
		clipboard: ""
	};   

	flashvars['clipboard'] = text;
	
	var params = {     
		wmode: "transparent",
		bgcolor: "#FBAF0B"
	};   

	var attributes = {     
		style: "margin:1px 0 0 0;display:block;_display:none;background-color:#FBAF0B"
	};     

	swfobject.embedSWF("http://www.ag.ru/i/clipboard.swf", "swf_button", "180", "18", "9.0.0", "http://video.ag.ru/expressinstall.swf", flashvars, params, attributes);
}

function switch_type(type){
	switch_code(type,gallery_data['cur_size']);
}

function switch_size(size){
	switch_code(gallery_data['cur_type'],size);
}

function switch_code(type,size){
	gallery_data['cur_type'] = type;
	gallery_data['cur_size'] = size;

	var cur_type = {
		1:"code_html",
		2:"code_bbcode"
	};

	var cur_size = {
		1:"thumb_l",
		2:"thumb_m",
		3:"thumb_s"
	};

	for(var i in cur_type){
		if(i == gallery_data['cur_type']){
			$(cur_type[i]).className = 'thumb_act'
		}
		else{
			$(cur_type[i]).className = 'thumb_ina';
		}
	}

	for(var i in cur_size){
		if(i == gallery_data['cur_size']){
			$(cur_size[i]).className = 'thumb_act'
		}
		else{
			$(cur_size[i]).className = 'thumb_ina';
		}
	}

	blinker($('code_generated'));
	generate_code_list();
}

function generate_code_list(){
	type = gallery_data['cur_type'];
	size = gallery_data['cur_size'];
	if(!type || !size) return;
	var data = {
		1: {
			1: '<a target="_blank" title="Посмотреть полноразмерный скриншот" href="http://www.ag.ru/go/scr/http://www.ag.ru/screenshots/#{gsn}/#{sid}#show"><img src="http://i.ag.ru/ag/thumbs/#{gid}/#{file}" width="#{w}" height="#{h}" border="1" style="margin:5px 10px 10px 0;border:1px solid black"></a>',
			2: '<a target="_blank" title="Посмотреть полноразмерный скриншот" href="http://www.ag.ru/go/scr/http://www.ag.ru/screenshots/#{gsn}/#{sid}#show"><img src="http://i.ag.ru/ag/thumbs/#{gid}/#{file}" width="#{w}" height="#{h}" border="1" style="margin:5px 10px 10px 0;border:1px solid black"></a>',
			3: '<a target="_blank" title="Посмотреть полноразмерный скриншот" href="http://www.ag.ru/go/scr/http://www.ag.ru/screenshots/#{gsn}/#{sid}#show"><img src="http://i.ag.ru/ag/thumbs/#{gid}/#{file}" width="#{w}" height="#{h}" border="1" style="margin:5px 10px 10px 0;border:1px solid black"></a>'
		},
		2: {
			1: "[url=http://www.ag.ru/go/scr/http://www.ag.ru/screenshots/#{gsn}/#{sid}#show][img]http://i.ag.ru/ag/thumbs/#{gid}/#{file}[/img][/url] ",
			2: "[url=http://www.ag.ru/go/scr/http://www.ag.ru/screenshots/#{gsn}/#{sid}#show][img]http://i.ag.ru/ag/thumbs/#{gid}/#{file}[/img][/url] ",
			3: "[url=http://www.ag.ru/go/scr/http://www.ag.ru/screenshots/#{gsn}/#{sid}#show][img]http://i.ag.ru/ag/thumbs/#{gid}/#{file}[/img][/url] "
		}
	}
	var size2id = {
			1: "screens_l_float",
			2: "screens_m_float",
			3: "screens_s_float"
	};	
	var size_id	= size2id[size];

	var t = new Template(data[type][size]);

	var s = '';
	for(var id = 0; id < gallery_data['total']; id++){
		if(gallery_data['chosen'][id]){
			var d = get_screen_dim(gallery_data[size_id][id]);
			d['file'] = gallery_data[size_id][id];
			d['gid'] = gallery_data['game_id'];
			d['gsn'] = gallery_data['gsn'];
			d['sid'] = gallery_data['sid'][id];
			s += t.evaluate(d);
		}
	}
	gallery_list_gen_button(s);
	$('code_generated').value = s;
}

function choose_screen(id){
	if(gallery_data['chosen'][id]){
		gallery_data['chosen'][id] = 0;
	}
	else{
		gallery_data['chosen'][id] = 1;
	}
	repaint_all_chosen();
}

function repaint_all_chosen(){
	['screens_s_float','screens_m_float','screens_l_float'].each(repaint_chosen);
	generate_code_list();

}

function repaint_chosen(it){
	var cnt = 0;
	var no_update = 0;
	for(var id = 0; id < gallery_data['total']; id++){

		if(!gallery_data['cache'][it+'_'+id]){
			gallery_data['cache'][it+'_'+id] = $(it+'_'+id);
		}
		obj = gallery_data['cache'][it+'_'+id];

		if(!obj) {
			no_update = 1;
			continue;
		}
		if(gallery_data['chosen'][id]){
			obj.className = 'selected';
			cnt++;
		}
		else{
			obj.className = '';
		}
	}
	if(!no_update) $('chosen_count').innerHTML = cnt;
}

function build_list(id){
	if(gallery_data[id+'_done']){
		return;
	}
	gallery_data[id+'_done'] = 1;
	var container = $(id);
	var s = '';
	for(var i = 0; i < gallery_data[id].length; i++){
		var screen = gallery_data[id][i];
		if(!screen || screen == '') {
			continue;
		};
		var dim = get_screen_dim(screen);
		var w = dim['w'];
		var h = dim['h'];
		if(w == 110 || !w || w==''){
			w=110;
			h=80;
		}
		s += "<div id='"+id+"_"+i+"' onclick='choose_screen("+i+")'><img src='http://i.ag.ru/ag/thumbs/"+gallery_data['game_id']+"/"+screen+"' width="+w+" height="+h+" border=1></div>";
		if(w == 200 && i && (i+1)%3 == 0){
			s+="<br class=clr>";
		}
		if(w == 320 && i && (i+1)%2 == 0){
			s+="<br class=clr>";
		}
	}

	container.innerHTML = '<span>'+s+'</span>';
}

function get_screen_dim(name){
	var re = /t(\d+)x(\d+)\.[\w0-9]+$/i;
	var m = name.match(re);
	var dim = {w:110,h:80};
	if(m){
		dim['w'] = m[1];
		dim['h'] = m[2];
	}
	return dim;
}

function switch_tab(id){
	var curr_tab = '';
	var curr_table = '';
	var curr_list = '';
	if(id == 1){
		curr_tab = 'tab_110px';
		curr_table = 'screens_s_table';
		curr_list = 'screens_s_float';
	}
	else if(id == 2){
		curr_tab = 'tab_200px';
		curr_table = 'screens_m_table';
		curr_list = 'screens_m_float';
	}
	else if(id == 3){
		curr_tab = 'tab_320px';
		curr_table = 'screens_l_table';
		curr_list = 'screens_l_float';
	}
	['tab_110px','tab_200px','tab_320px'].each(function(it){
		if(it != curr_tab){
			$(it).hide();
		}
	});
	['tab_110px','tab_200px','tab_320px'].each(function(it){
		if(it == curr_tab){
			$(it).show();
		}
	});

	['screens_s_table','screens_m_table','screens_l_table'].each(function(it){
		if(it != curr_table){
			$(it).hide();
		}
	});

	['screens_s_table','screens_m_table','screens_l_table'].each(function(it){
		if(it == curr_table){
			$(it).show();
		}
	});

	setTimeout(function(){
		build_list(curr_list);
		repaint_all_chosen();
	},20);
}

function gallery_list_gen_button(text) {

	var flashvars = {     
		normal: "http://www.ag.ru/i/copy_group_1.png",
		pressed: "http://www.ag.ru/i/copy_group_3.png",
		hover: "http://www.ag.ru/i/copy_group_2.png",
		clipboard: ""
	};   

	flashvars['clipboard'] = text;
	
	var params = {     
		wmode: "transparent",
		bgcolor: "#FBAF0B"
	};   

	var attributes = {     
		style: "margin:1px 0 0 0;display:block;_display:none;background-color: #FBAF0B"
	};     

	swfobject.embedSWF("http://www.ag.ru/i/clipboard.swf", "swf_button", "400", "18", "9.0.0", "http://video.ag.ru/expressinstall.swf", flashvars, params, attributes);
}

