var url = 'http://vlada.hr/rss/feed/novosti_i_najave';
	
// JavaScript Document
	function hidekat(){
		$('ext').style.display = 'block';
		$('tip').style.display = 'none';
		$('kat').style.display = 'none';
		$('intime').style.display = 'none';
	}
	
	function showkat(){
		$('ext').style.display = 'none';
		$('tip').style.display = 'block';
		$('kat').style.display = 'block';
		$('intime').style.display = 'block';
	}
	
	
//Filter izbornik unutarnji tekst kada je otvoren
//var filter_sh_Opentext = 'Sakrij filter mjeseca';
//var docs_sh_text = 'Zatvori';

var slike_za_ucitavanje = new Array ('strelica_vise_1_h.gif', 'strelica_filter_1_h.gif','strelica_filter_2_h.gif','strelica_tockasta_h.gif','btn_lijevo_h.gif', 'btn_desno_h.gif', 'btn_gore_h.gif', 'btn_dolje_h.gif','icon_email_h.gif','icon_print_h.gif','icon_dodaj_favorite_h.gif', 'icon_obavjesti_h.gif');

//Brzina izmjena aktualnosti u milisekundama
var aktualno_speed = 8000;


//
var simple = false;
var no_slide = false; // za slide


//Toggle funkcija
var Toggle = new function (){
	this.Class = function(id){id.className == 'open' ? id.className ='' : id.className ='open';}
	this.Display = function(id){id.style.display == 'none' ? id.style.display ='block' : id.style.display ='none';}
	this.Text = function(id,newText,oldText){		
		id.innerHTML == oldText ? id.innerHTML = newText : id.innerHTML = oldText;}
}
function stripeId(element){
	var theid = element.id.substring(element.id.length, element.id.lastIndexOf('_')+1);
	return theid;
}



function preload (polje){
var slike = new Array();
	for (var i = 0; i<polje.length; i++){
		var	img = new Image();
		slike[i] = img;
		slike[i].src ='/design/vlada2/images/'+ polje[i];
		
	}
}




function init(){	
	//Dohvati elemente
	var filter_sh = $('filter_sh');
	var aktualno = $('aktualne-teme');
	var pro_min = $('projekti-min');
	var docs_sh = $('docs_sh');

	
	
	
	
	
	//Funkcija TimeFilter koja se izvršava samo ako element postoji
	function TimeFilter(){			
		var filter_sh_text = filter_sh.innerHTML;						
		if($('month').style.display != 'none') filter_sh.className = 'open';
		
		function open_filter(){
			//this.blur();						
			new Toggle.Class(filter_sh);	
			Toggle.Text(filter_sh, filter_sh_Opentext, filter_sh_text)
			//bez animacije
			if(simple || window.opera){				
				new 	Toggle.Display($('month'));
			}
			// S animacijom			
			else{							
				new Effect.toggle($('month'),'blind',{duration: 0.3 })
			}
			return false;
		}	
		
		filter_sh.onclick=open_filter;
		
		
		var url = '';
		var yurl = null;
		var murl = '';
		var selitem = null;
		
		//Filtriraj
		var years = $('fyear').getElementsByTagName('a');
		var month = $('month').getElementsByTagName('a');
		var current = document.getElementsByClassName('selected','fyear')[0];
		var tm = document.getElementsByClassName('selected','month')[0];
		var more = document.getElementsByClassName('more','fyear')[0];
		var more_ul = more.getElementsByTagName('ul')[0];
		
		////console.log(tm)		
		////console.log(stripeId(tm))
		if(current) {
			current.rel = 'current'
		}
		else{
			current = $('fyear').getElementsByTagName('a')[0];
			current.rel = 'current'
		}
		
		if(current.parentNode.parentNode.parentNode.className.indexOf('more') == 0){
			more.className += ' sel'
			more.getElementsByTagName('em')[0].innerHTML  = current.getElementsByTagName('span')[0].innerHTML
		}
		
		
		for(var i=0; i<years.length; i++){
			years[i].onclick = function(){				
				yurl = this.href;				
				
				
				
				
				if(this.className != 'selected'){
					for(var x=0; x<month.length; x++){
						month[x].className = '';
					}
						
					for (var x = 0; x<years.length; x++){
						years[x].className = '';
						
						//console.log(years[x])
						if(this.rel == 'current' && tm) month[stripeId(tm)].className = 'selected';
					}
				}
				
				more.className = more.className.replace(' sel', '');
				
				
				
				l = this.parentNode.parentNode.parentNode
				
				if(l.className.indexOf('more') == 0){
					more_ul.style.left = '-999em';
					if(l.className.indexOf(' sel') == -1) l.className += ' sel'
					more.getElementsByTagName('em')[0].innerHTML  = this.getElementsByTagName('span')[0].innerHTML
					t = setTimeout(function(){
						more_ul.style.left = '0';					
						clearTimeout(t)
					}, 250)
				}
				
				selitem = this;				
				if(selitem.className.indexOf('marked') != 0) this.className = 'marked'
				if($('month').style.display == 'none') open_filter();
				return false;
			}			
		}
		
		for(var i=0; i<month.length; i++){
		    
		    if(month[i].firstChild.nodeValue == 'siječnja'){
		        month[i].firstChild.nodeValue = 'siječanj';
		    } else  if(month[i].firstChild.nodeValue == 'veljače'){
		        month[i].firstChild.nodeValue = 'veljača';
		    } else  if(month[i].firstChild.nodeValue == 'ožujka'){
		        month[i].firstChild.nodeValue = 'ožujak';
		    } else  if(month[i].firstChild.nodeValue == 'travnja'){
		        month[i].firstChild.nodeValue = 'travanj';
		    } else  if(month[i].firstChild.nodeValue == 'svibnja'){
		        month[i].firstChild.nodeValue = 'svibanj';
		    } else  if(month[i].firstChild.nodeValue == 'lipnja'){
		        month[i].firstChild.nodeValue = 'lipanj';
		    } else  if(month[i].firstChild.nodeValue == 'srpnja'){
		        month[i].firstChild.nodeValue = 'srpanj';
		    } else  if(month[i].firstChild.nodeValue == 'kolovoza'){
		        month[i].firstChild.nodeValue = 'kolovoz';
		    } else  if(month[i].firstChild.nodeValue == 'rujna'){
		        month[i].firstChild.nodeValue = 'rujan';
		    } else  if(month[i].firstChild.nodeValue == 'listopada'){
		        month[i].firstChild.nodeValue = 'listopad';
		    } else  if(month[i].firstChild.nodeValue == 'studenog'){
		        month[i].firstChild.nodeValue = 'studeni';
		    } else  if(month[i].firstChild.nodeValue == 'prosinca'){
		        month[i].firstChild.nodeValue = 'prosinac';
		    } 


		    
			month[i].onmouseover = function(){
				
				for(var x=0; x<years.length; x++){
					if(years[x].className.indexOf('marked') != 0) years[x].className = years[x].className.replace(' marked','');
				}
				
				if(selitem != null){
					if(selitem.className.indexOf('marked') != 0) selitem.className += ' marked';				
				}
				
				
				murl = this.href;
				murl = murl.substring(murl.indexOf('/(month)'))
				if(yurl == null) yurl = current.href
				////console.log(current.href)
				url = yurl+murl;
				this.href = url	
			}
		}	
	}	 				
	
	
	
	//Funkcija Aktualnih tema
	
	function AktualneTeme(){			
		var items = document.getElementsByClassName('item',aktualno);
		var inav = $('aktualne-teme-nav').getElementsByTagName('a');
		var btnstop = $('akt-tema-stop');
		var lastitem = null;
		var timer;
		var x=-1,y=0;
		
		inav[0].className = 'selected';
		
		
		function stopSlide(){
			clearInterval(timer);
			btnstop.className = 'selected';
		}// end stopSlide();					
		
		
		var aktsimple = false; // za slide
		
		function display(id){
			if(aktsimple){items[stripeId(id)].style.display = 'block';}
			else{
				items[stripeId(id)].style.display = 'block';
				items[stripeId(id)].style.zIndex = '10';								
				items[stripeId(id)].style.filter = "alpha(opacity=100)";	 // za IE			
			}
		}//end display
		
		
		function hide(id){
			if(aktsimple){	items[id].style.display = 'none';}
			else{
				items[id].style.zIndex = '900';					
				new Effect.Fade(items[id],{duration:0.4});					
			}
		}// end hide
		
		
		if(!no_slide){
			btnstop.style.display = 'block';				
			btnstop.onclick = function(){
				this.blur();				
				if(this.className == 'selected'){
					slide();
					timer = setInterval(slide,aktualno_speed);
					this.className = '';
				}
				else{						
					stopSlide();
				}
			}
		}
		
		var inav = $('aktualne-teme-nav').getElementsByTagName('a');
		for(var i=0; i<inav.length; i++){

			
			
			inav[i].onclick = function PokaziTemu(){
				if(this.className == 'selected') return false;
				stopSlide();
				this.blur();						
				if(x < 0) x = 0;	
				if(lastitem == null) lastitem = x;					

				inav[lastitem].className = '';
				this.className = 'selected';				
				
				display(this);				
				hide(lastitem);	
				
				lastitem = stripeId(this);
				
				$('akt-tema-naslov').href = this.href;
				$('akt-tema-clanak').href = this.href;
				$('akt-tema-naslov').innerHTML = this.rel
				return false;
			}							
		}// end PokaziTemu

		
		function slide(){			
			y = x;				
			if(lastitem != y && lastitem != null && lastitem != x )	{
				y = x = lastitem;
				lastitem = null;
			}			
			
			x<items.length-1 ? x++ : x = 0;	
			inav[x].className = 'selected';
			
			display(inav[x]);
			
			if(y>=0)
			{
				inav[y].className = '';
				hide(stripeId(inav[y]));
			}			
			
			$('akt-tema-naslov').href = inav[x].href; // set link on Pokaži temu 
			$('akt-tema-clanak').href = inav[x].href; // set link on Pokaži temu 
			$('akt-tema-naslov').innerHTML = inav[x].rel
		}		
		
		
		
		
		
		
		
		if(!no_slide){
			slide();
			timer = setInterval(slide,aktualno_speed);	
		}
	} // end Aktualno teme

	
	
function projekti(){
	var items = document.getElementsByClassName('item',pro_min);
	var next = $('pro-next');
	var prev = $('pro-prev');
	
	var active = null;
	
	for(var i = 0; i<items.length; i++){
		if(items[i].style.display == 'block') active = i;
		if( active == items.length-1) {next.parentNode.className = 'inext';}		
		if( active == 0) {prev.parentNode.className = 'iprevious';}
	}
	
	next.onclick = function(){
		if(active == items.length-1){return false;}
		prev.parentNode.className = 'previous';
		var n = active+1;		
		if( n+1 > items.length-1) {this.parentNode.className = 'inext';}
		else{this.parentNode.className = 'next';}
	
		items[active].style.display = 'none';
		items[n].style.display = 'block';
		active = n;
		
		
		
		return false;
	}
	
	prev.onclick = function(){
	
		if(active == 0) {return false;}
		next.parentNode.className = 'next';
		var n = active-1;		
		//if( n-1 < 0) {this.parentNode.className = 'iprevious';
		if( n-7 < 0) {this.parentNode.className = 'iprevious';}
		else{this.parentNode.className = 'previous';}
		
		
		//if( n< 0) n = items.length-1;	
		if( n< 0) n = items.length-1;
		items[active].style.display = 'none';
		items[n].style.display = 'block';
		active = n;
		return false;
	}
	
 }
	
	
	function docs(){
		var x = docs_sh.innerHTML
		docs_sh.onclick = function(){
			
			//Toggle.Display ($('hidden_docs'));
			Toggle.Class(this);
			Toggle.Text(this,docs_sh_text,x);
			if(simple){				
				Toggle.Display($('hidden_docs'));
			}
			// S animacijom			
			else{							
				Effect.toggle($('hidden_docs'),'blind',{duration: 0.5 })
			}
			
			return false;
			
			
		}			
	}
	
	

	
	//Features ovisno o stranici
	if(filter_sh) TimeFilter();
	if(aktualno) AktualneTeme();
	if(pro_min) projekti();
	if(docs_sh) docs();
	
	preload(slike_za_ucitavanje);
}



Event.observe(window, 'load', init, false);




/*--------------------------------*/
function suckerfish(type, tag, parentId, all) {
	if (window.attachEvent) {
		window.attachEvent("onload", function() {
			var sfEls = (parentId==null) ? document.getElementsByTagName(tag) : document.getElementById(parentId).getElementsByTagName(tag);
			type(sfEls, all);
		});
	}
}

sfHover = function(sfEls, all) {
	for (var i=0; i<sfEls.length; i++) {
		if(all || (sfEls[i].className == 'button' || sfEls[i].className == 'btn')){
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}

suckerfish(sfHover, "input");
if($('fyear')) suckerfish(sfHover, "LI", "fyear", true);





/*--------------------------*/
/*
	AJAX
*/
Event.observe(window, 'load', function(){
	
	var cont = $('novosti_spec')
	
	if(!cont) return;
	
	var news;


	var x = null;
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(transport){
			result = transport.responseText
			
			news = Try.these(
				function() { return new DOMParser().parseFromString(result, 'text/xml'); },
				function() { var xmldom = new ActiveXObject('Microsoft.XMLDOM'); xmldom.loadXML(result); return xmldom;}
			);
			
			parseXML(news);
		}
	});
	
	
	function checkNode(node){
			rez = ((node.firstChild != null)) ? node.firstChild.nodeValue : '';
			rez = (rez != "Object") ? rez : ''
			return rez;
	}
	
	function addZero(date){		
		return (date<10) ? '0' + date : date;
	}
	
	function getHumanDate(razmak){
		switch(razmak) {
			case 0:
				return 'danas';
				break;
			case 1:
				return 'jučer';
				break;
			default:
				return ''
				break;
		}
	}
	
	var MJESECI = new Array('siječnja', 'veljače', 'ožujka', 'travnja', 'svibnja', 'lipnja', 'srpnja', 'kolovoza', 'rujna', 'listopada', 'studenoga', 'prosinca')	
	var danas  = new Date;
	var human_day = '';
	function parseXML(xml){
		var v = new Array();
		var items = xml.getElementsByTagName('item');	
	


		for (var i = 0; i < items.length; i++) {		
			var d = checkNode(items[i].getElementsByTagName('pubDate')[0])		
			d = new Date(d)
		
		/*	current_day = ((danas.getDate() - d.getDate()))
			if (d==danas) {human_day = ' danas';}
			else if	(current_day  == 1){human_day = ' jučer';}
			else{human_day = '';}
			*/
//			d = addZero(d.getDate()) + '. ' + MJESECI[d.getMonth()-1] + ' ' + d.getFullYear() +'. '+ getHumanDate((danas.getDate() - d.getDate()))

			d = addZero(d.getDate()) + '.' + addZero(d.getMonth()+1) + '.' + d.getFullYear();
			v[i]={
				date : d,
				title : checkNode(items[i].getElementsByTagName('title')[0]),				
				link : checkNode(items[i].getElementsByTagName('link')[0])
			}
		}
		buildDOM(v)
	}
	
	function n$(tag){
		return document.createElement(tag)
	}
	
	
	function buildDOM(a){
		
	
		
		
		brojvijest = (a.length<10) ? a.length : 10;
		
		for (var i = 0; i < brojvijest; i++) {
			//console.log(i)
			
			pn = (i%2==0) ? ' odd' : ' even'
			
			d = n$('div')
			d.className = 'content-view-line float-break to_col_2' + pn;			
			cont.appendChild(d)
			
			da = n$('div');
			da.className = 'class-article';
			d.appendChild(da);
			
			db = n$('div');
			db.className = 'in2';
			da.appendChild(db);
			
			dc = n$('div')
			dc.className = 'floater'
			db.appendChild(dc);

				dc_a = n$('div')
				dc_a.className = 'attribute-date'
				dc.appendChild(dc_a);
	
					dc_a_a = n$('p')
					dc_a_a.innerHTML = a[i].date
					dc_a.appendChild(dc_a_a);
				
				
				dc_b = n$('div')
				dc_b.className = 'attribute-title'
				dc.appendChild(dc_b);
				
					dc_b_a = n$('h2')
					dc_b.appendChild(dc_b_a);
					
					link = n$('a')
					link.href = a[i].link
					link.innerHTML = a[i].title
					dc_b_a.appendChild(link)

		
			
		}
	}
}, false);






