/*
 * éáéúőúüó
 *
 *
 */

function miss0_box_dovote(type,side) {
	var pars={act: 'dovote', type: type, dataid: miss0_box_current_id[side], vote: 1};
	new Ajax.Request('/missplay.php',{
		 parameters: pars
		,onComplete: function(_t) {
			var t=_t.responseText;
			var hash=$H(t.isJSON()?t.evalJSON():{error: -1000});
			if(hash.get('success')) {
				miss0_box_next(side);
			} else {
				if(hash.get('error')==-1000) location.href=location.href;
					else alert(hash.get('error'));
			}
		}
	});
	reload_median();
}

function miss0_box_timer_controll(run) {
	miss0_box_config.set('run',run?true:false);
}
function miss0_box_timer_func() {
	if(miss0_box_config.get('current')===false) {
		miss0_box_config.set('current',0);
	}
	var step=miss0_box_config.get('step');
	if(!miss0_box_config.get('run')) {
		miss0_box_config.set('current',miss0_box_config.get('default'));
	} else {
		if(miss0_box_config.get('current')<=0) {
			miss0_box_next(0);
			miss0_box_next(1);
			miss0_box_config.set('current',miss0_box_config.get('default'));
		} else {
			miss0_box_config.set('current',miss0_box_config.get('current')-step);
		}
	}
	miss0_box_timer_func.delay(step);
}

function miss0_box_next(side) {
	if(miss0_box_config.get('ajax')) return;
	if(miss0_box_hash.keys().size()<16) {
		var pars={act: 'ajaxGetMorePlayer' };
		miss0_box_config.set('ajax',true);
		new Ajax.Request('/missplay.php',{
			 parameters: pars
			,onComplete: function(_t) {
				var t=_t.responseText;
				var hash=$H(t.isJSON()?t.evalJSON():{error: true});
				if(hash.get('error')) return;
				if(!hash.size()) {
					location.href=location.href;
					return;
				}

				var lastid=miss0_box_current_id[side];
				miss0_box_hash.unset(lastid);
				miss0_box_hash=miss0_box_hash.merge(hash)
				miss0_box_hash.unset(miss0_box_current_id[side?0:1]);
				miss0_box_current_id[side]=miss0_box_hash.keys().first();
				//console.log(miss0_box_hash.inspect());
				miss0_box_config.set('ajax',false);
				miss0_box_redraw(side);
			}
		});

	} else {
		var lastid=miss0_box_current_id[side];
		if(!lastid) lastid=miss0_box_hash.keys().first();
		miss0_box_hash.unset(lastid);
		miss0_box_current_id[side]=miss0_box_hash.keys().without(miss0_box_current_id[side?0:1]).first();
		miss0_box_redraw(side);
		//console.log(side+'---'+miss0_box_current_id.inspect());

	}
}

function miss0_box_redraw(side) {
	var id=miss0_box_current_id[side];

	if(id) {
		var hash=$H(miss0_box_hash.get(id));
		var o=$('miss0_box_thumb_'+side);
		//console.log(side+'--'+hash.inspect());
		o.src=hash.get('avatarurl');
		var name=hash.get('name')?hash.get('name'):''
		o.title=name;
		o.alt=name;
		$('miss0_box_thumb_href_'+side).href=hash.get('link');
		$('miss0_box_thumb_href_'+side+'0').href=hash.get('link');
		$('miss0_box_thumb_href_'+side+'0').update(name);
	}
}

function miss0_box_last_next() {
	if(miss0_box_last_hash.keys().size()==0) {
		$('js_miss0_box_script').remove();
		var sc=new Element('script');
		sc.id='js_miss0_box_script';
		// last_script
		sc.src='http://static.imgsrv.farm.hu/CACHE/miss0last.js?'+miss0_box_last_timestamp+(miss0_box_last_count++);
		$('js_crossdomain_script_container').appendChild(sc);
	} else {
		var h=miss0_box_last_hash.values().first();
		h=$H(h);
		$('miss0_box_last_img').src=h.get('avatarurl');
		$('miss0_box2_name').update(h.get('name'));
		$('miss0_box2_name').href=h.get('link');
		$('miss0_box2_score').update(h.get('score'));
		$('miss0_box2_sumscore').update(miss0_box_last_sumscore);
		$('miss0_box2_img_link').href=h.get('link');
		miss0_box_last_hash.unset(miss0_box_last_hash.keys().first());
		miss0_box_last_next.delay(3.0);
	}
}


// kedvenchez pakolás
var miss0profilefavorite=function(dataid,del) {
	var pars={act:'dofavorite',del:del?1:0,dataid:dataid};
	new Ajax.Request('/missplay.php',{
		 parameters:pars
		,onComplete:function(_t) {
			var t=_t.responseText;
			var h=$H(t.isJSON()?t.evalJSON():{error:1000,errortext:'Rendszerhiba!'});

			if(h.get('error')) alert(h.get('errortext'));
				else location.href=location.href;
		}
	});
}


function miss0_billboard_send(dataid) {
	var pars={act:'billboard_donewmessage',text:$F('miss0_textarea'),dataid: dataid};
	new Ajax.Request('/missplay.php',{
		 parameters: pars
		,onComplete:function(t) { location.href=location.href; }
	});
}

/**
 * kedvenc törlése
 */
function miss0_delplayer(dataid) {
	if(confirm('Biztosan vissza szeretnéd vonni a versenyből a kedvenced?\n(A visszavont jelentkezésű kedvenceket nem lehezt ismételten benevezni!!)'))
		location.href='/missplay.php?act=delmypet&dataid='+dataid;
}


