var IFrameObj; // our IFrame object
function callToServer(URL) {
	if (!document.createElement) {return true};
	var IFrameDoc;
	if (!IFrameObj && document.createElement) {
		try {
			var tempIFrame=document.createElement('iframe');
			tempIFrame.setAttribute('id','RSIFrame');
			tempIFrame.style.border='0px';
			tempIFrame.style.width='0px';
			tempIFrame.style.height='0px';
			IFrameObj = document.body.appendChild(tempIFrame);
			
			if (document.frames) {
				IFrameObj = document.frames['RSIFrame'];
			}
		} catch(exception) {
			iframeHTML='<iframe id="RSIFrame" style="';
			iframeHTML+='border:0px;';
			iframeHTML+='width:0px;';
			iframeHTML+='height:0px;';
			iframeHTML+='"><\/iframe>';
			document.body.innerHTML+=iframeHTML;
			IFrameObj = new Object();
			IFrameObj.document = new Object();
			IFrameObj.document.location = new Object();
			IFrameObj.document.location.iframe = document.getElementById('RSIFrame');
			IFrameObj.document.location.replace = function(location) {
				this.iframe.src = location;
			}
		}
	}
	
	if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument) {
		setTimeout('callToServer()',10);
		return false;
	}
	
	if (IFrameObj.contentDocument) {
		IFrameDoc = IFrameObj.contentDocument; 
	} 
	else if (IFrameObj.contentWindow) {
		IFrameDoc = IFrameObj.contentWindow.document;
	} 
	else if (IFrameObj.document) {
		IFrameDoc = IFrameObj.document;
	} 
	else {
		return true;
	}
	
	IFrameDoc.location.replace(URL);
	return false;
}
(function()
{
    if('HTMLElement' in this){
        if('insertAdjacentHTML' in HTMLElement.prototype){return;};
    }else{
        return;
    };
    function insert(w, n)
    {
        switch(w.toUpperCase())
        {
            case 'BEFOREEND':
                this.appendChild(n);
                break;
            case 'BEFOREBEGIN':
                this.parentNode.insertBefore(n, this);
                break;
            case 'AFTERBEGIN':
                this.insertBefore(n, this.childNodes[0]);
                break;
            case 'AFTEREND':
                this.parentNode.insertBefore(n, this.nextSibling);
                break;
        };
    };
    function insertAdjacentText(w, t){
        insert.call(this, w, document.createTextNode(t || ''));
    };
    function insertAdjacentHTML(w, h)
    {
        var r = document.createRange(); r.selectNode(this);
        insert.call(this, w, r.createContextualFragment(h));
    };
    function insertAdjacentElement(w, n)
    {
        insert.call(this, w, n);
        return n;
    };
    HTMLElement.prototype.insertAdjacentText = insertAdjacentText;
    HTMLElement.prototype.insertAdjacentHTML = insertAdjacentHTML;
    HTMLElement.prototype.insertAdjacentElement = insertAdjacentElement;
})();
function pre_del(nr,mes,ind){
	mes = mes ? mes : 'Sigur stergi?';
	ind = ind ? ind : 'indicedelete';
	if(nr && confirm(mes)){
		$(ind).value = nr;
		document.formus.submit();
	}
}
function pag_pics(inapoi){
	if($('sbmic') && $('sbmic1')){
		cur = window['cur'] ? window['cur'] : 1;
		if(inapoi)
			cur = $(String('sbmic')+ String(cur-1)) ? cur-1 : 'max';
		else
			cur = $(String('sbmic')+ String(Number(cur)+Number(1))) ? Number(cur)+Number(1) : 1;
		if(cur=='max'){
			cur = 1;
			for(i=1;i<100;i++)
				cur = $('sbmic'+i) ? i : cur;
			
		}
		for(i=1;i<100;i++){
			if(nn=$('sbmic'+i)){
				nn.style.visibility = i==cur ? 'visible' : 'hidden';			
			}
			else{
				break;
			}
		}
			
	}
}
function boldit(obj,ret){
	obj.className = ret ? '' : 'bld';
}
function menu_hov(obj,indic){
	if(indic){
		obj.parentNode.parentNode.className = '';
	}
	else{
		obj.parentNode.parentNode.className = obj.parentNode.parentNode.className + ' hov';	
	}
}
function opti_toggle_class(idvictim,clasa,clasa0,idindice,indclasa,indclasa0,forceclose){
	var victim = $(idvictim);
	var indice = $(idindice);
	if(!victim || (idindice && !indice)) return false;
	//daca e in stare curenta 0
	
	if( (clasa0 ? (victim.className.indexOf(clasa0)>=0) : (victim.className.indexOf(clasa)<0)) || forceclose   ){
		victim.className = victim.className.replace(clasa,'');
		if(clasa0){
			victim.className = victim.className.replace(clasa0,'');
		}
		victim.className = victim.className + ' ' + clasa;
		if(indice && indclasa){
			if(indclasa0){
				indice.className = indice.className.replace(indclasa0,'');
			}	
			indice.className = indice.className.replace(indclasa,'');			
			indice.className = indice.className + ' ' + indclasa;	
		}
	}
	else{
	
		victim.className = victim.className.replace(clasa,'');
		if(clasa0){
			victim.className = victim.className.replace(clasa0,'');					
			victim.className = victim.className + ' ' + clasa0;
		}
		if(indice && indclasa){		
			indice.className = indice.className.replace(indclasa,'');
			if(indclasa0){
				indice.className = indice.className.replace(indclasa0,'');				
				indice.className = indice.className + ' ' + indclasa0;
			}		
		}
	}
	
}
function opti_tab(prefixvictim,sel,nrvictim,clasasel,clasanosel,prefixindic,clasaindsel,clasaindnosel){
	if(!$(prefixvictim  + '1')){
		return;
	}
	for(i = 1; i <= nrvictim; i++){
		var m = $(prefixvictim + i);
		var ind = $(prefixindic + i);
		if(!m){
			continue;
		}
		m.className = clasasel ? m.className.replace(clasasel,'') : m.className;	
		m.className = clasanosel ? m.className.replace(clasanosel,'') : m.className;			
		if(ind){
			ind.className = clasaindsel ? ind.className.replace(clasaindsel,'') : ind.className;	
			ind.className = clasaindnosel ? m.className.replace(clasaindnosel,'') : ind.className;	
		}
		if(i == sel){
			m.className = clasasel ?  m.className + ' ' + clasasel : m.className;
			if(ind){
				ind.className = clasaindsel ? ind.className + ' ' + clasaindsel : ind.className;
			}
		}	
		else{
			if(clasanosel){
				m.className = m.className + ' ' + clasanosel;
			}
			if(ind && clasaindnosel){
				ind.className = ind.className + ' ' + clasaindnosel;
			}			
		}
	}
}

function $(id){
	if(m = document.getElementById(id))
		return m;
	return false;
}
function $$(id){
	if(m = document.getElementById(id))
		return m.style;
	return false;
}
var com_w = null;
function openpop(url) {
		lungim = 950;
		inalt = 850;
		progP = com_w;
/* centru*/
		pnt_n = 1;			
		progP = window.open( url, 'compop', 'toolbar=0,scrollbars='+pnt_n+',location=0,statusbar=1,menubar=0,resizable=1,width='+lungim+',height='+inalt+',left = 140,top = 162' );
		progP.focus();
/* miscarea inversa */
		com_w = progP;	
}


function opti_control_nr(inpobj,afisobj,limit){
	nr = limit - inpobj.value.length;
	if(nr<0){
		inpobj.value = inpobj.value.substr(0,limit);
		return false;
	}	
	else{
		afisobj.innerHTML = nr;
	}
}
allow_new_slide = true;
function slide_rotate(backto,force){
	if(window['opti_slide_stop'] && !force ){
		return false;
	}
	
	if(!allow_new_slide){
		return;	
	}
	
	opti_nrslides = window['opti_nrslides'] ? opti_nrslides : 'Z';
	//cateexista din 10
	if(isNaN(opti_nrslides)){
		for(i = 1; i <11; i++){
			if($('slide' + i)){
				opti_nrslides = i;
			}
			else{
				break;
			}
		}
	}
	if(isNaN(opti_nrslides) || (opti_nrslides < 2)){
		return;
	}
	
	//care e curent
	opti_nrcur = window['opti_nrcur'] ? opti_nrcur : 1;

	
	if(!backto){
		var opti_deafis = ((Number(opti_nrcur) + 1) > opti_nrslides)  ? 1 : (Number(opti_nrcur) + 1);	
	}
	else{
		var opti_deafis = ((Number(opti_nrcur) - 1) < 1)  ? opti_nrslides : (Number(opti_nrcur) - 1);	
	}
//	opti_tab('slide',opti_deafis,opti_nrslides,'disp',null,'slb_','butsel',null);
	
	for(i = 1; i <= opti_nrslides; i++ ){
		if(opti_nrcur == i){
			allow_new_slide = false;
			$('slide'+i).style.top = '0px';			
			for(ji = 1; ji<=20; ji++){

				setTimeout("$('slide"+i+"').style.top='-"+Math.round(13.35*ji)+"px'",50*ji);	
			}
			
			setTimeout("$('slide"+i+"').className='slide'",1000);	
			setTimeout("allow_new_slide=true",1100);				

		}
		else if(opti_deafis == i){
			allow_new_slide = false;			
			$('slide'+i).style.top = '-270px';
			$('slide'+i).className = 'slide disp';
			for(ji = 1; ji<=20; ji++){
				setTimeout("$('slide"+i+"').style.top='"+Math.round(267-13.35*ji)+"px'",50*ji);	
			}			

		}
		else{
			$('slide'+i).className = 'slide';
		}
	
	
	}
	
	opti_nrcur = opti_deafis;
	setTimeout('slide_rotate()',5000);
}
var Base64 = {
    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
    // public method for encoding
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;
        input = Base64._utf8_encode(input);
        while (i < input.length) {
            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);
            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;
            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }
            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
        }
        return output;
    },
    // public method for decoding
    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;
        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
        while (i < input.length) {
            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));
            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;
            output = output + String.fromCharCode(chr1);
            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }
        }
        output = Base64._utf8_decode(output);
        return output;
    },
    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";
        for (var n = 0; n < string.length; n++) {
            var c = string.charCodeAt(n);
            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
        }
        return utftext;
    },
    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
        while ( i < utftext.length ) {
            c = utftext.charCodeAt(i);
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    }
}
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;		
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}		
started_ts = new Array();
contents_evs = new Array();
function do_ev(oj,evid,nopic){
	ev_pos = findPos(oj);
	//alert(mPos.x+' '+mPos.y +' '+ev_pos[0]+' '+ev_pos[1]);
	oj.setAttribute('title',''); 
//	ev_tot = findPos($('tot'));	
	var evleft = ev_pos[0];
	var evtop = ev_pos[1];	
	var evleft_max =  evleft + oj.offsetWidth;
	var evtop_max = evtop + oj.offsetHeight;
	oj.id = oj.id ? oj.id : 'evid' +evleft+evtop+ Math.random();	
	
	//devina opaca
	
	
	if(!started_ts[oj.id]){
		oj.className = oj.className + ' opactrue';
		if(!contents_evs[evid]){
			callToServer($STI+'/carted/'+ evid);
		}
		
		setTimeout("do_ev_sec('"+oj.id+"',"+evleft+","+evtop+","+evleft_max+","+evtop_max+","+evid+","+nopic+")",600);		
		started_ts[oj.id] = true;
	}
	else{
	//	alert(oj.id);
		}
}
function do_ev_sec(ojid,evleft,evtop,evleft_max,evtop_max,evid,nopic){
	if((mPos.x >= evleft) && (mPos.x <= evleft_max) && (mPos.y >= evtop) && (mPos.y <= evtop_max) ){
		oj = $(ojid);
		iduu = ojid+'_supra';
		if($(iduu)) return;
		//daca nu are date
		if(!contents_evs[evid]){
			setTimeout("do_ev_sec('"+ojid+"',"+evleft+","+evtop+","+evleft_max+","+evtop_max+","+evid+","+nopic+")",300);			
			return;
		}
		
		
		//curatare deja existente
		for(idd in started_ts){
			if($(idd+'_supra')){
				$('tot').removeChild($(idd+'_supra'));
				started_ts[idd] = false;				
			}	
		}	
		
		if(!$('tot'))		
			return;
		ev_tot = findPos($('tot'));	
		evl = mPos.x - ev_tot[0] + 10;
		evr = evtop - ev_tot[1] + 20;	
	
		
		html = '<a  href="#" class="hbook hbook_rec hbook_stab"  id="'+iduu+'" style="position:absolute; display:block;left:'+evl+'px;top:'+evr+'px">'+contents_evs[evid]+'</a>';	
		
		$('tot').insertAdjacentHTML('beforeEnd',html);
	
		oj.onmouseout = function(ev){
				if($(iduu)){
					
					$('tot').removeChild($(iduu));
					started_ts[oj.id] = false;
					
				}
				$(oj.id).className = $(oj.id).className.replace(/opactrue/g,'');
				return false;
		}		
	}
	else{
		$(ojid).className = $(ojid).className.replace(/opactrue/g,'');
		started_ts[ojid] = false;
		//alert(mPos.x+' '+mPos.y +' '+evleft+' - '+evleft_max+'          '+evtop+' - '+evtop_max);
		}
	
}

document.onmousemove = mMove;
document.onmouseup   = mUp;
window.onload=function(){
	if($('slide1')){
		setTimeout('slide_rotate()',5000);
	}
}
var dObj  = null;
var mOffset = null;

function get_mOff(target, ev){
	ev = ev || window.event;
	var mPos  = mCoords(ev);	
	dif_x = slis.left.substr(0,(slis.left.length-2));	
	dif_y = slis.top.substr(0,(slis.top.length-2));	
	return {x:mPos.x - dif_x, y:mPos.y - dif_y};
}
function mMove(ev,do_ev){
	ev = ev || window.event;
	mPos = mCoords(ev);		
	
}
function mUp(){
	if(dObj)
		endsli();	
	dObj = null;
}
function endsli(){
	sli.onmousedown = null;	
	Draggable(sli);return;
	//doar pt calculate din pas in pas
	s_t = slis.left.substr(0,(slis.left.length-2));
	de_mov = (s_t - start_sli) % marj_sli;
	if(de_mov <= marj_sli/2)
		mov_sli(s_t-de_mov,1);
	else
		mov_sli((marj_sli-de_mov) + Number(s_t),1);	
	Draggable(sli);
}
function mov_sli(m,tr){
	m = (m < end_sli) ? m : end_sli;
	m = (m > start_sli) ? m : start_sli;	
	slis.left = m + 'px';
	mov_cont(m - start_sli,tr);
}
///atatea diviziuni cate elemente, ce e inauntru descrie spatiul necesar ca multiplu fata de cel mai mic dintre ele
//factori = [7,3,1,5,10,1];
function conts_left(nrr){
	conts.left = (nrr) + 'px';
	
}
function mov_cont(pozitie,tr){
	coW =  get_WH('specialscroll',null,true);
	cont_w = coW-947;
	if(cont_w<0) return;
	
	//aflam la care procent trebuie sa ajungem
	diviz = tr ? 750 : (end_sli - start_sli);
	proce = (100 * pozitie) / diviz;
	proce = (proce < 100) ? proce : 100;
	proce = (proce > 1) ? proce : 0;	
	
	//adunam matematic factorii pentru a ne da pixelii reali / unitate de factor
	suma_fact = 0;
	for(i=0;i<factori.length;i++){
		suma_fact = Number(suma_fact) + Number(factori[i]);
	}
	pix_per_fact = cont_w / suma_fact;
	
	//vedem care este pragul din factori peste care este
	
	prag = 100 / factori.length;
	
	
	//adunam pixelii / factor * factorii din pasii anteriori si luam restul procentual din cuprinsul factorului curent 
	pix_fin = 0;
	for(i=1;i<=factori.length;i++){
		prag_act = factori[i-1];
		if(proce <= prag*i){
			proce_mic = proce % prag;
			if((proce > 0) && proce_mic==0){
				proce_mic = prag;
			}
			
			nou = Number(pix_per_fact*prag_act*(proce_mic/prag));		
			pix_fin = Number(pix_fin) +  Number(nou);	
	
			break;
		}
		else{
			nou = Number(pix_per_fact*prag_act);
			pix_fin = Number(pix_fin) + Number(nou);				
		}
	}
	pix_fin = 0 - Math.round(pix_fin);
	//lenta
	if(tr){
		pix_acum = conts.left.substr(0,(conts.left.length-2));
		pasi = Math.abs(Math.ceil((pix_acum-pix_fin) / (cont_w/25)));
		pasi = pasi > 5 ? pasi : 5;
		pas = (pix_fin - pix_acum) / pasi;
		
		for(i=0;i<pasi;i++){
			timp = i > pasi/3 ? 14 : 30;
			nrac = Math.round(Number(pix_acum) + Number((i+1)*pas)); 
			setTimeout("conts_left("+nrac+")",i*timp);
		}
		
	}	

/*
	cont_w = coW-947;
	$nr = 0 - Math.round((cont_w*pozitie)/ (end_sli-start_sli));	
	if($nr>0) $nr=0;*/
	conts.left = (pix_fin) + 'px';

}
marj_sli = 1;
function Draggable(item){
	if(!item) return;
	sli = $('bkv_move'), slis = sli.style,start_sli = 7,end_sli = 723; cont_y = 0; conts = $$('specialscroll');	
	item.onmousedown = function(ev){
		dObj  = this;
		mOffset = get_mOff(this, ev);		
		mMove(ev);
		return false;
	}
//face posibile clic pe titluri
	ells = getElementsByClassName('l','div');
	for(i=0;i<ells.length; i++){
		ells[i].onmousedown = function(ev){
			ev = ev || window.event;
			//daca e sub el miscatorul, il misca
			var ne = findPos($('bkv_move'));
			mp = mCoords(ev);
			nrsp = mp.x-ne[0];
			if((nrsp>0) && (nrsp < 150)){				
				dObj  = $('bkv_move');
				mOffset = get_mOff($('bkv_move'), ev);		
				mMove(ev);
			}
			else{
				move_on_bar(ev);				
			}
			return false;
		}
	}	
}
function mCoords(ev){
	if(ev.pageX || ev.pageY){
	
		return {x:ev.pageX, y:ev.pageY};
	}
//ie
	if (document.documentElement){
	// IE6 +4.01 
	
	
		xu = 1  + ev.clientX + document.documentElement.scrollLeft - ( document.body ? document.body.clientLeft : 0);
		yu = ev.clientY + document.documentElement.scrollTop  - ( document.body ? document.body.clientTop : 0);
	}
	else if (document.body && document.body.scrollTop){
	// IE5 or DTD 3.2	
		xu = ev.clientX + document.body.scrollLeft - document.body.clientLeft;
		yu = ev.clientY + document.body.scrollTop  - document.body.clientTop;	
	}
	
	return {x:xu,y:yu};	
	
	
}
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;		
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}		
function move_on_bar(ev){
	var poz_c = findPos($('proj_move'));
	mp = mCoords(ev);
	nrsp = mp.x-poz_c[0]-start_sli - 70;	
	nrsp = (nrsp > 0) ? nrsp : 0;	
	
	mov_sli(nrsp,true);
//	mov_cont(nrsp,true);
};	
function move_arrow(dreap){
	var poz_m = $('bkv_move').style.left.substr(0,($('bkv_move').style.left.length-2));	
	if(dreap){
		mov_sli(Number(poz_m) + Number(150) ,true);	
	}
	else{
		mov_sli(Number(poz_m) - Number(150) ,true);	
	}
}
