var current_url = '';
var prev_menu = 'home';

function OnLoad()
{
  Slider();

  try
  {
    prev_menu = getElementsByClass('selected', document.getElementById('nav'), 'a')[0].id;
  }
  catch(e)
  {
    prev_menu = '';
  }

  loaded = true;
  Loader();
  return false;
}

function clearInput(obj, defval)
{
	if (obj.value == defval) {
		obj.value = '';
	}

	return false;
}

function putInput(obj, defval)
{
	if (obj.value == '') {
		obj.value = defval;
	}

	return false;
}

function browse(id, item, func)
{
  if (loaded == false) return true;
  if ($('loader').style.display == 'inline') return false;

  $('content').innerHTML = '';
  $('loader').style.display = 'inline';

  if (typeof(func) != 'undefined')
  {
    current_url = $('nav_' + item).href;
    new Ajax.Updater(id, current_url + "&type=js" , { onComplete: function() { func(item); $('loader').style.display = 'none'; $('whole').style.display = 'none'; } });
  }
  else
  {
    current_url = item;
    new Ajax.Updater(id, current_url + "&type=js" , { onComplete: function() { if (prev_menu != '') { $(prev_menu).className = ''; if($(prev_menu + '2')) { $(prev_menu + '2').className = ''; } } prev_menu = ''; $('loader').style.display = 'none'; $('whole').style.display = 'none'; } });
  }

  document.location = '#top';

  new Ajax.Request(current_url + "&type=title", {
  method: 'get',
  onSuccess: function(transport) {
  	document.title = transport.responseText;
  }
	});

  if (current_url.indexOf('/index.php?page=page&name=buy') != -1) {
    return refreshImages();
  }
  else {
    return refreshProducts(currentCategory);
  }

  return false;
}

function refreshProducts(id)
{
  new Ajax.Updater("adverts", "index.php?page=selected&type=js&id=" + id);

  return false;
}

function refreshImages()
{
  new Ajax.Updater("adverts", "index.php?page=needed&type=js");

  return false;
}

var arrOpenIDs = new Array();
var arrObjects = new Array();
var currentCategory = 0;

function toggleMenu(id, obj, parent, products)
{
  if ( arrOpenIDs[parent] > 0 && arrOpenIDs[parent] != id ) /* close old menu */
  {
    toggleMenu(arrOpenIDs[parent], arrObjects[arrOpenIDs[parent]]['obj'], parent, 0);
  }

  if ( arrOpenIDs[parent] == id ) /* close menu */
  {
    if ( arrOpenIDs[id] > 0 ) /* close lower levels if any open */
    {
      toggleMenu(arrOpenIDs[id], arrObjects[arrOpenIDs[id][2]], id, 0);
    }

    if ( currentCategory == id ) /* if category is selected, move it to the parent cause we're closing this menu  */
    {
      currentCategory = parent;
    }

    $('submenu' + id).hide();
    arrObjects[id][2].className = '';

    arrOpenIDs[parent] = 0;
    arrObjects[id] = 0;
  }
  else /* open menu */
  {
    arrOpenIDs[parent] = id;
    arrObjects[id] = new Array(id, parent, obj);

    $('submenu' + id).show();
    arrObjects[id][2].className = 'selected';

    currentCategory = id;
  }

  if (products && current_url.indexOf('/index.php?page=page&name=buy') == -1)
  {
    refreshProducts(currentCategory);
  }

  return false;
}

function NavMenu(id)
{
  if (id != prev_menu) {
    func = new Function("item", "if (prev_menu != '') { $(prev_menu).className = ''; if($(prev_menu +'2')) { $(prev_menu + '2').className = ''; } } $('nav_' + item).className = 'selected'; if ($('nav_' + item + '2')) { $('nav_' + item + '2').className = 'selected'; } prev_menu = 'nav_' + item;");
  }
  else {
    func = new Function("");
  }

  return browse("content", id, func);
}

function getElementsByClass(searchClass, node, tag)
{
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\\\s)"+searchClass+"(\\\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function _utf8_encode (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;
}

function urlencode(string) {
  return escape(_utf8_encode(string));
}

function switchLang(obj, lang)
{
  if (current_url != '')
  {
    document.location = current_url + '&l=' + lang;
  }
  else
  {
    document.location = obj.href;
  }

  return false;
}

function checkOther(obj)
{
  var other = document.getElementById('other');

  if (obj.value == 'other')
  {
    other.style.display = 'inline';
  }
  else
  {
    other.style.display = 'none';
    other.value = '';
  }

  return false;
}

function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;

//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth;
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = xScroll;
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}