var hiding = false;
var showing = false;
var showPromo = true;

/*
	postavljenje visina ćelija
*/
function update() {
	var tmpHeight = 678;
	if (document.body.clientHeight > tmpHeight) tmpHeight = document.body.clientHeight;

	document.getElementById('shadow-right').style.height = tmpHeight-40;

	/* nažalost browser-dependant */
	var offset = 361;
	if (navigator.appName == 'Microsoft Internet Explorer') offset = 363;
	document.getElementById('main').style.height = tmpHeight-offset;

	offset = 424;
	if (navigator.appName == 'Microsoft Internet Explorer') offset = 408;
	document.getElementById('paragraph-content').style.height = tmpHeight-offset;
}

/* cashing rollover slikica */
function preloadImages() {
  var img_1 = new Image();
  img_1.src = "img/menu/about-hover.png";
  var img_2 = new Image();
  img_2.src = "img/menu/menu_item-hover.png";
  var img_3 = new Image();
  img_3.src = "img/menu/quick-link-1.png";
  var img_4 = new Image();
  img_4.src = "img/menu/quick-link-2.png";
  var img_5 = new Image();
  img_5.src = "img/menu/quick-link-3.png";  
  var img_6 = new Image();
  img_6.src = "img/menu/quick-link-4.png";    
/*
  var img_1 = new Image();
  img_1.src = "img/menu/about-hover.png";
  var img_2 = new Image();
  img_2.src = "img/menu/services-hover.png";
  var img_3 = new Image();
  img_3.src = "img/menu/products-hover.png";
  var img_4 = new Image();
  img_4.src = "img/menu/references-hover.png";
  var img_5 = new Image();
  img_5.src = "img/menu/contacts-hover.png";
  */
}

// some AJAX...
function makeRequest(pageUrl, title) {
  var http_request = false;

  if (window.XMLHttpRequest) { // Mozilla, Safari, ...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
          http_request.overrideMimeType('text/xml');
      }
  } else if (window.ActiveXObject) { // IE
      try {
          http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
          try {
              http_request = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e) {}
      }
  }

  if (!http_request) {
      alert('Giving up :( Cannot create an XMLHTTP instance!\nPlease use a browser with AJAX support to view this site.');
      return false;
  }

  http_request.onreadystatechange = function() { eval('loadPage(http_request, title);'); };

  http_request.open('GET', pageUrl, true);
  http_request.send(null);
}

// content loading...
function loadContent(title, page) {
	if (page != 'null') {
		var urlBase = 'content/';
		var pageUrl = urlBase + page + '.html';

		makeRequest(pageUrl, title);
	}
	if (page == 'home') { 
		showPromo = true 
	} else { 
		showPromo = false;
	}
}

function getInnerHTML(strDoc) {
	var beginIdx = strDoc.indexOf('<body');
	beginIdx = strDoc.indexOf('>', beginIdx)+1;

	var endIdx = strDoc.indexOf('</body>');

	var iHTML = strDoc.substring(beginIdx, endIdx);
	return iHTML;
}

function loadPage(http_request, title) {
	if (http_request.readyState == 4) {
      if (http_request.status == 200 || http_request.status == 0) {
          var doc = http_request.responseText;
					document.getElementById('paragraph-content').innerHTML = getInnerHTML(doc);
					document.getElementById('paragraph-title').innerHTML = title;

					// TODO set cookie for current page & extra graphics
					// ...

					// animation
					// home page special case...
					if (title == 'NEWS') showPicture();
					else hidePicture();

      } else {
          alert('There was a problem with the request.');
      }
  }
}

// animation...
function hidePicture() {
	if (!showing) {
		document.getElementById('promoMsg').style.display = 'none';
		var pic = document.getElementById('titlePicture');
		if (pic.style.display != 'none') {
			hiding = true;
			var height = pic.offsetHeight;
			var opc = pic.style.opacity;
			if (!opc || opc == null) opc = 1.0;
			if (height > 2) {
				pic.style.height = height/1.25;
				document.getElementById('paragraph-content').style.height = document.body.offsetHeight-228-height;
				pic.style.opacity = opc-0.1;

				// M$ IE way...
				if (pic.filters) {
					pic.filters.alpha.opacity = (opc-0.2)*100;
				} // and Firefox way...
				else {
					document.getElementById('main').style.height = document.body.offsetHeight-161-height;
				}

				t=setTimeout("hidePicture()", 40);
			}	else {
				hiding = false;
				pic.style.height = 0;
				pic.style.display = 'none';
				pic.style.opacity = null;

				document.getElementById('paragraph-content').style.height = document.body.offsetHeight-227;
				if (!pic.filters) {document.getElementById('main').style.height = document.body.offsetHeight-161};
			}
		}
	}
}

function showPicture() {
	if (!hiding) {
		showing = true;
		var pic = document.getElementById('titlePicture');
		if (!pic.style.height || pic.offsetHeight < 1) {
			pic.style.display = 'block';
			pic.style.height = 2;
		}

		if (pic.offsetHeight < 184) {
			var height = pic.offsetHeight;
			var opc = pic.style.opacity;
			if (!opc || opc == null) opc = 0.001;
			if (height < 184) {
				tmpHeight = height*1.5;
				if (tmpHeight > 184) tmpHeight = 184;
				pic.style.height = tmpHeight;
				document.getElementById('paragraph-content').style.height = document.body.clientHeight-267-height;
				pic.style.opacity = opc*2;

				// M$ IE way...
				if (pic.filters) {
					if (pic.filters.alpha) pic.filters.alpha.opacity = (pic.style.opacity)*100;
				}// and Firefox way...
				else {
					document.getElementById('main').style.height = document.body.offsetHeight-186-height;
				}
				t=setTimeout("showPicture()", 50);
			}
		}	else {
			showing = false;
			update();
			if (pic.filters) {
				pic.filters.alpha.opacity = 100;
			}
			pic.style.opacity = 1;
			document.getElementById('promoMsg').style.display = 'block';
		}
	}
}


/* utility functions */
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


function hidestatus(){
	window.status='';
	return true;
}

function hidestatusbar(){
	if (document.layers)
		document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
	document.onmouseover=hidestatus;
	document.onmouseout=hidestatus;
}



