function getElementsByClassName(node, classname)
{
    var a = []; var re = new RegExp('(^| )'+classname+'( |$)'); var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++) if(re.test(els[i].className))a.push(els[i]);
    return a;
}

function switchImages (el,classString) {
	for (x=0,uid = getUid(el.parentNode,"uid"),els = getElementsByClassName(el.parentNode.parentNode,classString); x<els.length;x++)
		if ( getUid(els[x],"image") == uid ) els[x].style.display = "block"; else els[x].style.display = "none";
}

function switchSelect (el,classString) {
	for (x=0,uid = getUid(el.parentNode,"uid"),els = getElementsByClassName(el.parentNode.parentNode,classString); x<els.length;x++)
		if ( getUid(els[x],"uid") == uid ) els[x].childNodes[0].className="selected"; else els[x].childNodes[0].className="";
}

function getUid (node,uidString) {
	for (nn=0,tmp = node.className.split(" ");nn<tmp.length;nn++)
		if ( tmp[nn].indexOf(uidString) > -1 ) return tmp[nn].substring(tmp[nn].indexOf(uidString)+uidString.length);
}

function setMouseOvers () {

	// get all topNews from modules
	var topNews = getElementsByClassName(document,"topNews");
	for (var x=0; x<topNews.length;x++) {
		allItems = topNews[x].getElementsByTagName("A");
		//for (var y=0; y <allItems.length; y++) {
		for (var y=allItems.length-1; y >-1; y--) {
			if ( allItems[y].parentNode.className.indexOf("newsItem")>-1) {
				allItems[y].onmouseover = function() { switchImages(this,"newsImg"); switchSelect(this,"newsItem"); };
				allItems[y].onmouseover();
			}
			
			// while here, also set categories
			/*var cat = allItems[y].parentNode.className.split(" ");
			for (var z=0; z<cat.length;z++) {
				if ( cat[z].indexOf("cat") > -1) {
					if (document.all)	var modulHead = allItems[y].parentNode.parentNode.parentNode.childNodes[0].childNodes[0];
					else var modulHead = allItems[y].parentNode.parentNode.parentNode.childNodes[1].childNodes[0];
					modulHead.innerHTML = cat[z].substring(3);
					break;
				}
			}*/
		}
	}
	
	
	if ( document.getElementById("izdvNewsWrapper") ) {
	
		//center flashNews images
		allItems = document.getElementById("izdvNewsWrapper").getElementsByTagName("IMG");
		for (var y=0; y <allItems.length; y++) {
			allItems[y].parentNode.parentNode.style.display="block";
			h = allItems[y].height; w = allItems[y].width; wh = allItems[y].parentNode.parentNode.style.height; ww = allItems[y].parentNode.style.width;
			wh="auto"; wh=allItems[y].parentNode.parentNode.offsetHeight; ww="auto";ww=allItems[y].parentNode.parentNode.offsetWidth;
			if (wh) allItems[y].style.marginTop = (wh-h)/2 + "px";
			if (ww) allItems[y].style.marginLeft = (ww-w)/2+ "px";
			allItems[y].parentNode.parentNode.style.display="none";
		}
	
	
		//set flashNews mouseovers
		allItems = document.getElementById("izdvNewsWrapper").getElementsByTagName("A");
		//for (var y=0; y <allItems.length; y++) {
		for (var y=allItems.length-1; y >-1; y--) {
			if ( allItems[y].parentNode.className.indexOf("izdvNews")>-1 ) {
				allItems[y].onmouseover = function() { switchImages(this,"izdvImg"); switchSelect(this,"izdvNews"); };
				allItems[y].onmouseover();
			}
		}
		
		//set category
		/*wrapper = document.getElementById("content_wrap");
		allItems = getElementsByClassName(wrapper,"modul");
		for (var y=0; y <allItems.length; y++) {
			modHead = getElementsByClassName(allItems[y],"modLink");
			category = getElementsByClassName(allItems[y],"category");
			modHead[0].innerHTML = category[0].innerHTML;
			for (var x=0; x <category.length; x++)
				category[x].parentNode.removeChild(category[x]);
		
		}*/
		
	}
}


function setSubCategories () {
	var heads = getElementsByClassName(document,"otherHeadLink");
	for (var x=0;x<heads.length;x++) {
		subCat = getSubCat ( heads[x] );
		heads[x].innerHTML = subCat;
	}
}

function getSubCat ( subCatHeader ) {
	tmp = getElementsByClassName(subCatHeader.parentNode.parentNode,"category");
	return tmp[0].innerHTML;
}

function initDragDropScript () {
 // <![CDATA[
   Sortable.create("col1",
     {tag:'div',only:"modul",dropOnEmpty:true,handle:'modHead',ghosting:false,containment:["col1","col2","col3b"],constraint:false,onUpdate:function() {if(typeof snimiKolac == 'function') snimiKolac(); } });
   Sortable.create("col2",
     {tag:'div',only:"modul",dropOnEmpty:true,handle:'modHead',ghosting:false,containment:["col1","col2","col3b"],constraint:false,onUpdate:function() {if(typeof snimiKolac == 'function') snimiKolac(); } });
   Sortable.create("col3b",
     {tag:'div',only:"modul",dropOnEmpty:true,handle:'modHead',ghosting:false,containment:["col1","col2","col3b"],constraint:false,onUpdate:function() {if(typeof snimiKolac == 'function') snimiKolac(); } });
   
 // ]]>
}

function setRoundedCorners () {
	settings = {
      tl: { radius: 10 },
      tr: { radius: 10 },
      bl: { radius: 10 },
      br: { radius: 10 },
      antiAlias: true,
      autoPad: false
    }

	var cornersObj = new curvyCorners(settings, "modul");	cornersObj.applyCornersToAll();
	var cornersObj = new curvyCorners(settings, "fix");	cornersObj.applyCornersToAll();

	settings = {
      tl: { radius: 5 },
      tr: { radius: 5 },
      bl: { radius: 5 },
      br: { radius: 5 },
      antiAlias: true,
      autoPad: false
    }
	var divObj = document.getElementById("izdvojeno");
	var cornersObj = new curvyCorners(settings, divObj);
    cornersObj.applyCornersToAll();


}


function disableSelection(target){
if (typeof target.onselectstart!="undefined") //IE route
	target.onselectstart=function(){return false}
else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
	target.style.MozUserSelect="none"
else //All other route (ie: Opera)
	target.onmousedown=function(){return false}
target.style.cursor = "default"
}


function resizeTopNewsImg () { 
	topNews = getElementsByClassName(document,"topNews");
	for (x=0; x<topNews.length; x++) {
		topImgs = topNews[x].getElementsByTagName("IMG");
		for (y=0; y<topImgs.length; y++) {
			iH = topImgs[y].height; iW = topImgs[y].width; iO = iH/iW;
			wH = 99; wW = 132;
			if ( iH/wH > iW/wW ) {
				topImgs[y].width=wW; topImgs[y].height=wW*iO; 
				topImgs[y].style.marginTop = (wH-topImgs[y].height)/2 + "px";
			} else if ( iH/wH < iW/wW ) {
				topImgs[y].height=wH; topImgs[y].width=wH/iO;
				topImgs[y].style.marginLeft = (wW-topImgs[y].width)/2 + "px";
			} else {
				topImgs[y].height=wH; topImgs[y].width=wW;
			}
		}
	}
}

function doSubHeader() {
	//!!(window.attachEvent && !window.opera) ? document.all("prilagodi") : 
	sH = document.getElementById("subheader");
	
	if (sH && document.getElementById("prilagodi")) {
		
		sPrilagodi = document.getElementById("prilagodi");
		sPrilagodi.onclick = function () {
			sC = document.getElementById("customize");
			if ( sH.offsetHeight == 50 )	{ new Effect.Scale(sH, 440, {scaleX:false,scaleContent:false,scaleMode:{originalHeight:50}}); sC.style.display="block";}
			else { new Effect.Scale(sH, 22.72, {scaleX:false,scaleContent:false,scaleMode:{originalHeight:220}}); sC.style.display="none"; }
			return false;
		}
		
		sHomepage = document.getElementById("homepage");
		sHomepage.onclick = function () {
			if(document.all && !window.opera) { this.style.behavior='url(#default#homepage)';
			//document.setHomePage(location.hostname);
			return false;}
		}
		
		sFavoriti = document.getElementById("favoriti");
		sFavoriti.onclick = function () { 
			if(document.all && !window.opera){ window.external.AddFavorite(location.hostname, document.title);
			return false; }else{ this.title = document.title; }
		}
	
		cStyle = getElementsByClassName(sH,"cStyle");
		for (x=0; x<cStyle.length; x++) {
			cStyle[x].onclick = function () {
				switchCSS( this.id );
				return false;
			}
		}

	}

}

function switchCSS(filename) {
   //document.getElementById('css').href = 'fileadmin/templates/naslovnica/'+filename+'.css';
   setActiveStyleSheet(filename);
}

function setBlogCat () {
  blog = document.getElementById("modul_blog");
  if ( blog ) {
	cat  = getElementsByClassName(blog,'blogCategory');
	head = getElementsByClassName(blog,'modLink');
	if (head && cat)
		{ head[0].innerHTML = cat[0].innerHTML; cat.innerHTML = ""; }
  }
}

function loadComplete() {
	setSubCategories();
	setNewsItemCount();
	setMouseOvers();
	resizeTopNewsImg();
	setBlogCat();
	if (document.getElementById("izdvNewsWrapper")) {
		setRoundedCorners();
		initDragDropScript();
	}
	disableSelection(document.body);
	doSubHeader();
	subCatClick();
	
	  var cookie = readCookie("style");
	  var title = cookie ? cookie : 'plava';
	setActiveStyleSheet(title);
	
}
window.onload = loadComplete;
