var 
_=jQuery.noConflict(),
news_id = '',
news_stop=0,
tn_t = new tmr(250,0),
img_t = new tmr(5000,10000),
pop_t = new tmr(250,0),
pop_h_t = new tmr(350, 0),
promo_t = new tmr(7000, 12000)
;

_(document).ready(function(){ _document_ready(); });

	
function _document_ready() {
	forms_setup();
	img_t.start('','img_t_func');
	_('#AT').hover(function(){_.get('x.php',{'a':'mto'},function(d){if(d!='') _('#AT').attr('href',d);});},function(){_(this).attr('href','#');});	
	_('.pop').hover(function(){pop_h_t.stop();},function(){pop_h_t.start('', 'popups_hide');});
	_('img.tn').hover(function(){tn_t.start(this.id,'tn_t_hover_func');},function(){tn_t.stop();});
	_('.popT h2').hover(function(){pop_t.start(this.id,'pop_t_hover_func');},function(){pop_t.stop();});
	_('#popCntPrev,#popCntNext').click(function(){		
		var curr=_('#popCntWhere').html();
		var who=_('#'+curr).parent();
		
		if(this.id=='popCntPrev')
			who = who.nextAll('.popT').eq(0);
		else
			who = who.prevAll('.popT').eq(0);
		if (who.length == 1) {
			who = who.children().eq(0);
			who = who.attr('id');
			if (who!='') {
				pop_t.id = who;
				pop_t.elapse();
			}
		}
		return false;
	});
	_('#promo').hover(function(){promo_t.stop();},function(){promo_t.restart();});
	promo_t.start('', 'promo_t_func');
	_('a.blank').live('click',function(){_(this).attr('target','_blank');});
	
//	if(location.hash!='')
//		if (location.hash.substr(1, 4)=='news')
//			pop_t.start(location.hash.substr(1),'pop_t_hover_func');
}
function promo_t_func(){
	_('#promo a').animate({'opacity':0.1},3000,'linear',function(){_('#promo a').animate({'opacity':1.0},100);})	;
}

function tn_t_hover_func() {
	var id=tn_t.id;
	_.get('x.php', {'a':'tn','id':id},function(data) {
		if(''!=data) 
			popup('#popImg', data, '#'+id, '#C', 0);
	});
}
function img_t_func() {
	var t = _('#bimg img');
	var s = t.attr('src');
	var p = s.lastIndexOf('/');
	var i = s.substr(p+1);
	_.get('x.php',{a:'img',img:i},function(d){
		if (d!=''){
			img_new = d;
			t.fadeOut(1500,function(){
				t.attr('src',s.substr(0, p+1)+img_new);
				t.fadeIn(1500, function(){ img_t.restart(); });
			});
		}
	});
}
function pop_t_hover_func() {
	var id=pop_t.id;
	var tid = 'text' + id.substr(4);
	var data = _('#'+id).html();
	data = data.substr(0, data.lastIndexOf('&nbsp;'));
	data = 
			'<div class="popCntT">'+data+'</div>'
			+_('#'+tid).html();
	if (data != '') {
		// next/prev setup
		if (_('#'+id).parent().prevAll('.popT').length > 0)
			_('#popCntNext').show();
		else
			_('#popCntNext').hide();
		if (_('#'+id).parent().nextAll('.popT').length > 0)
			_('#popCntPrev').show();
		else
			_('#popCntPrev').hide();
		_('#popCntWhere').html(id);
		// show/replace content
		if (_('#popCnt').css('display')=='none')	
			popup('#popCnt', data, '#'+id, '#C', 0);
		else
			_('#popCnt .pop_cnt').html(data);
	}
}
function popup(who, data, where, container, valign) {

	popups_hide_forced();
	
	var t = _(where);
	var to = t.offset();
	var w = _(window);
	var wh = w.height();
	
	var a = _(who);
	_(who + ' div.pop_cnt').html(data);	
	
	var sy = w.scrollTop();

	//*
	var y=to.top;// + sy;
	var hh = a.height();
	var hvis = wh - to.top + sy;
	if (hh > hvis)
		y -= Math.min(hh - hvis, to.top);
	/**/
	/*
	var y = sy + 60;
	*/
	
	if (y < sy + 10)
		y = sy + 10;

	var c=_(container);
	var co=c.offset();
	var x = Math.min(to.left, co.left + c.width() - a.width() - 10);
	a.css({top:y,left:x}).show();
}

function popups_hide(){
	_('.pop').fadeOut(250);
}
function popups_hide_forced(){
	_('.pop').hide();
}

function forms_setup(){
	
	// no autocomplete for the code field...
	_('#CF, #PF').each(function(){
		_('#SEC_INPUT').attr('autocomplete', 'off');
		var ff = {
	    	url: 'x.php',
	    	success: function(data){
	    		
	    		_('#FINFO_CNT').eq(0).hide().html(data.substr(2)).fadeIn(400);
	    		
	    		var code = data.substr(0, 2);
	    		if (code == 'OK') {
	    			_('#F1,#F2,#F3,#SEC_INPUT').attr('value', '');
	    			_('#T1').attr('value', '');
//	    			_('#SEC_IMG').attr('src', location.protocol + '//' + location.hostname + '?a=400&t=' + (new Date()).getTime());
	    			_('#P3,#P4,#P10,#P11,#P13').attr('checked', '');
	    			_('#P1,#P5,#P6,#P7,#P8,#P9,#P12,#P14,#P15,#P16').attr('value', '');
	    		}
	
    			var i=_('#SEC_IMG');
    			var s=i.attr('src');
    			var p=s.indexOf('&');
    			if (p>0)
    				s=s.substr(0, p);
    			i.attr('src',s+'&t='+(new Date()).getTime());
	    		
	    	}
		};
		_(this).ajaxForm(ff);
	});
}

function tmr(t, t_rnd) {
	this.timer=0;
	this.time=t;
	this.id='';
	this.rand_time=t_rnd;
	this.func='';
}
tmr.prototype.stop=function() {
	if (this.timer) {
		clearTimeout(this.timer);
		this.timer=0;
	}
}
tmr.prototype.start=function(id, func){
	this.id=id;
	this.func=func;
	this.restart();
}
tmr.prototype.restart=function(){
	this.stop();
	
	var new_time = this.time;
	if (this.rand_time > 0)
		new_time += this.rand_time * Math.random();
	
	var ie = this;
	this.timer = setTimeout(function(){ 
		ie.elapse();
	}, new_time);
}
tmr.prototype.elapse=function(){
	this.stop();
	eval(this.func+'();');
}

