//	event handling

function addEvent(element, eventType, lamdaFunction, useCapture) {
	if (element.addEventListener) {
		element.addEventListener(eventType, lamdaFunction, useCapture);
		return true;
	}
	if (element.attachEvent) return element.attachEvent('on' + eventType, lamdaFunction);
	return false;
}

function knackerEvent(e) {
	if (e && e.stopPropagation) e.stopPropagation();
    if (window.event && window.event.cancelBubble ) window.event.cancelBubble = true;
    if (e && e.preventDefault) e.preventDefault();
    if (window.event) window.event.returnValue = false;
}

//	rollover

function rolloverImage(e) {
	var target = window.event ? window.event.srcElement : e ? e.target : null;
	if (!target) return;
	target.src = target.src.replace('.gif', '_.gif');
}

function rolloutImage(e) {
	var target = window.event ? window.event.srcElement : e ? e.target : null;
	if (!target) return;
	target.src = target.src.replace('_.gif', '.gif');
}

function preloadImages() {
	var imgElements = document.images;
	if (!document.MM_preload) document.MM_preload = new Array();
	for (var i = 0, j = 0; i < imgElements.length; i++)
		if (imgElements[i].className.match(/\brollover\b/)) {
			document.MM_preload[j] = new Image;
			document.MM_preload[j++].src = imgElements[i].src.replace('.gif','_.gif');
			addEvent(imgElements[i], 'mouseover', rolloverImage, false);
			addEvent(imgElements[i], 'mouseout', rolloutImage, false);
		}
}

addEvent(window, 'load', preloadImages, false);

// other
function cov(o, v) {
	document.getElementById(o).style.visibility = v;
}

function popup(url, w, h) {
	window.open(url, 'popUp', 'resizable=no, scrollbars=no, menubar=yes, width=' + w + ', height=' + h);
}

function sh(id) {
	el = document.getElementById(id);
	if(el.style.display == 'block') el.style.display = 'none';
	else el.style.display = 'block';
}

function ajx(u,o){var a=[],e,k,m=o.m=='p',p=o.p,x;try{x=new ActiveXObject('Msxml2.XMLHTTP')}catch(e){try{x=new ActiveXObject('Microsoft.XMLHTTP')}catch(e){x=new XMLHttpRequest()}}if(x==undefined)return false;for(e in p){if(typeof p[e]!='object')s(e,p[e]);else for(k=0;k<p[e].length;k++)s(e,p[e][k])}p=a.join("&");if(!m&&p)u+=(u.indexOf('?')>-1?'&':'?')+p;if(typeof o.i=='function')o.i();x.open(m?'POST':'GET',u,true);x.onreadystatechange=function(){if(x.readyState==4){if(typeof o.c=='function')o.c();if(x.status>=200&&x.status<300){if(typeof o.s=='function')o.s(x.responseText)}else if(typeof o.f=='function')o.f()}};x.setRequestHeader('X-Requested-With','XMLHttpRequest');if(m)x.setRequestHeader('Content-Type','application/x-www-form-urlencoded');x.send(m?p:null);return true;function s(n,v){a.push(encodeURIComponent(n)+'='+encodeURIComponent(v))}}function frm(o){var a={},e,i,j,n,t;for(i=0;i<o.length;i++){e=o[i];n=e.name;if(!n||e.disabled)continue;t=e.type;if(/^(hidden|password|text(area)?)$/.test(t)||/^(checkbox|radio)$/.test(t)&&e.checked||/^select-one$/.test(t)&&e.selectedIndex>-1)s(n,e.value);else if(/^select-multiple$/.test(t)&&e.selectedIndex>-1){e=e.options;for(j=0;j<e.length;j++)if(e[j].selected)s(n,e[j].value)}}return a;function s(n,v){if(a[n]==undefined)a[n]=v;else{if(typeof a[n]!='object')a[n]=[a[n]];a[n].push(v)}}}

var xbox = document.createElement('div');

xbox.open = function (s, w, h) {
	var d = document, a, b = xbox;
	if(!b.x) {
		a = d.createElement('div');
		a.setAttribute('id', 'xbox_overlay');
		b.setAttribute('id', 'xbox');
		a.onclick = b.close;
		d.body.appendChild(b.a = a);
		d.body.appendChild(b);
	}
	b.w = w ? w : 650;
	b.h = h ? h : 532;
	b.x = b.y = true;
	b.ps();
	b.innerHTML = s;
};

xbox.close = function() {
	var b = xbox;
	b.y = false;
	b.a.style.display = b.style.display = 'none';
	b.innerHTML = '';
};

xbox.ps = function() {
	var d = document.documentElement, b = xbox, a = b.a, w = b.w, h = b.h;
	b.style.width = w + 'px';
	b.style.height = h + 'px';
	if(document.body.scrollLeft || document.body.scrollTop) d = document.body;
	x = d.scrollLeft + (d.clientWidth - w) / 2;
	y = d.scrollTop + (d.clientHeight - h) / 2;
	if(x < 0) x = 0;
	if(y < 0) y = 0;
	b.style.left = x + 'px';
	b.style.top = y + 'px';
	b.style.display = 'block';
	a.style.width = d.scrollWidth + 'px';
	a.style.height = Math.max(d.scrollHeight, d.clientHeight) + 'px';
	a.style.display = 'block';
};


window.onresize = function() {
	var b = xbox;
	if(!b.y) return;
	b.a.style.display = b.style.display = 'none';
	if(navigator.appName == 'Microsoft Internet Explorer') setTimeout(b.ps, 1);
	else b.ps();
};

function calendar(a, b) {
	document.getElementById('calendar_month').innerHTML = a;
	document.getElementById('calendar_day').innerHTML = b;
}

function oferta(id) {
	new ajx('oferta.php?f=ajax', {
		m: 'p',
		p: {id: id},
		i: function() {
			xbox.open('');
			xbox.className = 'ajx';
		},
		c: function() {
			xbox.className = '';
		},
		s: function(r) {
			xbox.innerHTML = r;
		},
		f: function() {
			xbox.innerHTML = 'eroare conexiune';
		}
	});
}

function supergol(id) {
	xbox.open('<a href="javascript:xbox.close();"><div style="text-align:right;"><img height="38" width="38" src="/images/supergol/close.png"></a></div><br/><iframe name="iFrameId" width="600" height="500" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" src="/supergol_detalii.php?sgid=' + id + '"></iframe>', 600, 552);
}

function bilet_add(id, optiune, cota) {
	//var num1 = document.getElementById('bilet_cota').value * document.getElementById('bilet_miza').value * cota;
	//if(num1<500000){
		//alert(num1);
		var bilet = document.getElementById('bilet');
		new ajx('oferta.php?f=ajax_bilet_add', {
			m: 'p',
			p: {id: id, optiune : optiune, cota:cota},
			i: function() {
				if(!bilet.innerHTML) bilet.innerHTML = '&nbsp;';
				bilet.className = 'ajx';
			},
			c: function() {
				bilet.className = '';
			},
			s: function(r) {
				bilet.innerHTML = r;
				bilet_castig_refresh();
			},
			f: function() {
				bilet.innerHTML = 'eroare conexiune';
			}
			
		});
	//} else { alert("Castigul nu poate sa depaseasca 500.000 lei")}
}

function bilet_del(id) {
	var bilet = document.getElementById('bilet');
	new ajx('oferta.php?f=ajax_bilet_del', {
		m: 'p',
		p: {id: id},
		i: function() {
			bilet.className = 'ajx';
		},
		c: function() {
			bilet.className = '';
		},
		s: function(r) {
			bilet.innerHTML = r;
			bilet_castig_refresh();
		},
		f: function() {
			bilet.innerHTML = 'eroare conexiune';
		}
	});
}

function bilet_del_tot() {
	var bilet = document.getElementById('bilet');
	new ajx('oferta.php?f=ajax_bilet_sterge_tot', {
		
	});
}

function bilet_save_miza() {
	new ajx('oferta.php?f=ajax_bilet_miza', {
		m: 'p',
		p: {bilet_miza: document.getElementById('bilet_miza').value},
		i: function() {
		},
		c: function() {
		},
		s: function(r) {
		},
		f: function() {
		}
	});
}

function bilet_castig_refresh() {
	var num = document.getElementById('bilet_cota').value * document.getElementById('bilet_miza').value;
	if(num<=500000){
		document.getElementById('bilet_castig').innerHTML = num.toFixed(2);
		document.getElementById('ai_depasit').style.display='none';
	} else { num=500000; 
			document.getElementById('bilet_castig').innerHTML = num.toFixed(2); 
			document.getElementById('ai_depasit').style.display='table-row';
			}
}

function bilet_castig() {
	bilet_castig_refresh();
	if(typeof(timer_save_miza) != 'undefined') clearTimeout(timer_save_miza);
	timer_save_miza = setTimeout(bilet_save_miza, 2000);
}

function bilet_print() {
	popup('/oferta.php?f=bilet_print', 360, 480);
}

var rezultate_live_requests = 0, rezultate_live_update = 0;

function rezultate_live_refresh() {
	new ajx('rezultate_live.php?f=ajax', {
		m: 'p',
		p: {sport_id: sport_id, category_id: category_id},
		i: function() {
		},
		c: function() {
		},
		s: function(r) {
			document.getElementById('rezultate_live').innerHTML = r;
		},
		f: function() {
		}
	});
}

function rezultate_live_home_refresh() {
	new ajx('rezultate_live.php?f=ajax_home', {
		m: 'p',
		p: {},
		i: function() {
		},
		c: function() {
		},
		s: function(r) {
			document.getElementById('rezultate_live').innerHTML = r;
		},
		f: function() {
		}
	});
}

function rezultate_live_refresh_update() {
	new ajx('rezultate_live.php?f=ajax_update', {
		m: 'p',
		p: {sport_id: sport_id, category_id: category_id},
		i: function() {
			rezultate_live_requests++;
		},
		c: function() {
			rezultate_live_requests--;
		},
		s: function(r) {
			if(rezultate_live_update < r) {
				if(rezultate_live_update > 0) rezultate_live_refresh();
				rezultate_live_update = r;
			}
		},
		f: function() {
		}
	});
}

function rezultate_live_init() {
	setInterval(function() {
		if(!rezultate_live_requests)
			rezultate_live_refresh_update();
	}, 15000);
	setInterval(rezultate_live_refresh, 60000);
}

function rezultate_live_home_init() {
	setInterval(rezultate_live_home_refresh, 60000);
}

function rezultate_live(id) {
	new ajx('rezultate_live.php?f=ajax_popup', {
		m: 'p',
		p: {id: id},
		i: function() {
			xbox.open('');
			xbox.className = 'ajx';
		},
		c: function() {
			xbox.className = '';
		},
		s: function(r) {
			xbox.innerHTML = r;
		},
		f: function() {
			xbox.innerHTML = 'eroare conexiune';
		}
	});
}

function alerta_gol_refresh() {
	new ajx('rezultate_live.php?f=ajax_alerta_gol', {
		m: 'p',
		p: {},
		i: function() {
		},
		c: function() {
		},
		s: function(r) {
			if(r) alerta_gol(r);
		},
		f: function() {
		}
	});
}

function alerta_gol(s) {
	var d = document.documentElement;
	if(document.body.scrollLeft || document.body.scrollTop) d = document.body;
	x = d.scrollLeft + (d.clientWidth - 574) / 2;
	if(x < 0) x = 0;
	y = d.scrollTop + (d.clientHeight - 85) - 20;
	if(y < 0) y = 0;






	o1 = document.getElementById('alerta_gol');
	o1.style.left = x + '50px';
	o1.style.top = y + 'px';
	o2 = document.getElementById('alerta_gol_info');
	o2.innerHTML = s;
	o1.style.display = 'block';
	setTimeout("document.getElementById('alerta_gol').style.display = 'none';", 10000);
}

function alerta_gol_init() {
	setInterval(alerta_gol_refresh, 30000);
}


