var prInd, activeNav, sekuStatus, cLytoo, cSection;
var regy = new RegExp("\/"+site.toLowerCase()+"");
var primSlideSpeed = 150, sekuSpeed = 500, contentSpeed = 500, supersizedImages=[];

/* NAVI SLIDER */
function prInd_moveTo(el, animate) {
  if (el && el.currentTarget) el = this;
  if (el.length == 0) return false;
  if (typeof animate == "undefined") animate = true;
  var $el=$(el), trgtWidth = $el.outerWidth()+1, trgtPos = $el.position().left-1;
  if (animate) prInd.stop().animate({ width: trgtWidth+"px", left: trgtPos+"px" }, { duration: primSlideSpeed, queue: false });
  else prInd.stop().css({ width: trgtWidth+"px", left: trgtPos+"px" });
}

/* CLICK ON PRIM */
function primClick(ev) {
  setActiveNav(this);
  if ($("#seku ul").length) $("#seku").fadeOut(sekuSpeed, sekuShow);
  else sekuShow(false);
  cLytoo = getLytooFromLink(this);
  $("#innerContent").fadeOut(contentSpeed, onContentEffectHideOver);
  return false;
}

function onContentEffectHideOver(ev) {
  window.location.hash = cLytoo;
  cSection = this.id=="innerMain" ? "main" : "toc";
	//ajaxLoadLytoo(this.id=="innerMain" ? "main" : "toc", cLytoo);
}

function onContentEffectShowOver() {
	catchGalleries();
	/*catchAllLinks(cont);*/
}

function onLytooLoaded(msg) {
	msg = msg.replace("<!DOCTYPE html>", "").replace("<html>", "").replace("</html>", "");
	$("#content").html(msg).children("#innerContent").hide().fadeIn(contentSpeed, onContentEffectShowOver);
}

function sekuShow(onEnd) {
  if (typeof onEnd == "undefined") onEnd = true;
  var seku = $("#seku"), childUl = $(activeNav).parent().find("ul");
  if (childUl.length == 0) return;

  //if (!onEnd) seku.stop(true,true);
  seku.empty().append(childUl).fadeIn(sekuSpeed);
}

function setActiveNav(el) {
  activeNav = el;
}

function centerPage() {
	$("#page").css("padding-top", ($("#pageContainer").height() - $("#page").height())/2+"px");
}

function ajaxLoadLytoo(section, lytoo) {
	$.ajax({
		url: "/"+site+".asp?lytoo="+lytoo+"&solo=content&ajax=true",
		cache: false,
		success: onLytooLoaded
	});
}

function showDropTarget(id, msg) {
	//$("#"+id+"LOADING").hide();
	cont = $("#"+id).show();
	if (msg) cont.html(msg);
}

function hideDropTarget(id) {}

function getLytooFromLink(el) {
	var $el = $(el), elHref = $el.attr("href");
	if (typeof elHref=="undefined") return false;
	if (regy.exec(elHref.toLowerCase())!=null && !$el.hasClass("noRewrite")) {
		lytoo = elHref.replace(".htm", "");
		lytooArr = lytoo.split(",,");
		lytoo = (lytooArr[1] || "").replace(",,","").replace(",", "/");
		if (!lytoo) lytoo = "posebne/main";
		return lytoo;
	}
	return false;
}


function catchAllLinks(container) {
	var regy = new RegExp(/(\/ARTIDEA)/i);
	$("A", container).each(function() {
		m = regy.exec(this.href);
		if (m!=null && !$(this).hasClass("noRewrite")) {
			lytoo = this.href.replace(".htm", "");//.replace(",", "/");
			lytooArr = lytoo.split(",,");

			lytoo = (lytooArr[1] || "").replace(",,","").replace(",", "/");
			if (!lytoo) lytoo = "posebne/main";
			this.lytoo = lytoo;
			//this.hash = this.lytoo.replace(/^.*#/, '');
			/*if (this.hash != this.lytoo) {
				//this.href = "#";
			}*/

			$(this).bind("click", function() {
				hash = this.lytoo.replace(/^.*#/, '');
				/*if (this.lytoo == "posebne/main") {
					//thisMovie("primarna").closeNavigation();
				} else if (hash!=this.lytoo) {
					//scroller[0].scrollTo("#hash"+hash.replace(/ /g, ""));
					return true;
				} else {*/
					document.location.hash = this.lytoo;
					/*thisMovie("primarna").setLytoo(this.lytoo);
				}*/
				this.blur();
				return false;
			});
		}
	});
	/*$("A.backLink", container).each(function() {
		$(this).bind("click", function() {
			var gotolytoo = lastLytoo[1];
			if (gotolytoo == "posebne/main") {
				thisMovie("primarna").closeNavigation();
			} else {
				ajaxLoadLytoo(gotolytoo);
				thisMovie("primarna").setLytoo(gotolytoo);
			}
			this.blur();
			return false;
		});
	});*/
	/*$("A[name]", container).each(function() {
		el = $(this);
		name = el.attr("name");
		el.attr("id", "hash"+name.replace(/ /g, ""));
		el.attr("name", "");
	});*/
}

function initializeHashEvents() {
  $(window).hashchange(catchOnHashChange);
}

function catchOnHashChange() {
  var hsh = document.location.hash.substring(1);
  ajaxLoadLytoo(cSection, hsh);
}

$(window).bind('resize', centerPage);

$(function() {
  prInd = $("#primIndicator");
  $("#primarna").bind("mouseout", function() { prInd_moveTo(activeNav); });
  active = $("#primarna a.seku0").bind("mouseover", prInd_moveTo)
                                 .bind("click", primClick)
                                 .filter(".active0");
  if (active.length > 0) {
    setActiveNav(active[0]);
    sekuShow(false);
   }
  else setActiveNav($("#primarna a:first"));
  prInd_moveTo(activeNav, false);

  centerPage();

  initializeHashEvents();

	if (cLytoo=='posebne/main') {
		$(".sectionLink").bind("mouseover", function() {
			//$(this).animate({ skewY: '10deg' });
		})
	}

});
