window.onload = init;

function init()
{
   
   var e = domClass('editorial', 'div', domID('container'));
   if (e.length>0)
   {
      if (e[0].offsetHeight>868)
         e[0].style.background = '#fff url(tpl/editorial-bg.png) no-repeat 0 100%';
   }
   if (domTN('input'))
      {
         var ip = domTN('input');
         for (var i=0; i<ip.length; i++)
         {
            if (domAttr(ip[i], 'title'))
            {
               ip[i].style.color = '#666';
               ip[i].value = domAttr(ip[i], 'title');
               domEvent(ip[i], 'focus', input_focus)
               domEvent(ip[i], 'blur', input_blur)
            }
         }
      }
   if (domID('search'))
      domID('search').focus();
}

function showMap(id)
{
   domID('australiamap').style.display='none';
   domID('australiamap').className='h';
   domID(id+'map').style.display='block';
}

function hideTypes()
{
   var l = domID('types');
   if (l)
      l.style.display = 'none';
}

function showTypes()
{
   hideLoc();
   var l = domID('types');
   if (navigator.appName.indexOf('MSIE')>0)
      l.style.top = '28px';
   l.style.display = 'block';
}

function getLoc()
{
   var s = domID('search');
   var l = s.value.toLowerCase();
   if (l.length>2)
   {
      s.style.background = '#fff url(/tpl/ajax-loader.gif) no-repeat right center';
      var p = new RegExp('[+]', 'ig');
      ajx('/tpl/meta_location.php', 'l='+ escape(l.replace(p,'%2B')), 'makeLoc')
   } else {
      domID('locations').style.display='none';
   }
   
}

function hideLoc()
{
   var l = domID('locations');
   l.style.display = 'none';
   var s = domID('search');
   s.style.background = '#fff';
}

function makeLoc()
{
   xml = this.xml;
   if (xml)
   {
      var l = domID('locations');
      l.innerHTML = '';
      var it = domTN('item', xml);
      if (it.length==0)
      {
         l.style.display = 'none';
      } else {
         if (it.length<16)
            l.style.height = ((it.length * 15)+10) +'px';
         else
            l.style.height = '';
   
         for (var i=0; i<it.length;i++)
         {
            var t = l.appendChild(domCreate('p'));
            var a = t.appendChild(domCreate('a', {href: 'javascript:selectLoc("'+ escape(domValue(it[i])) +'")'}, false, domValue(it[i])));
            //domEvent(a, 'click', selectLoc);
         }
         l.style.display = 'block';
      }
   }
   var s = domID('search');
   s.style.background = '#fff';
}

function input_focus(e)
{
   var o = domE(e);
   if (o)
   {
      if (o.value == domAttr(o, 'title'))
      {
         o.value = '';
         o.style.color = '#000';
      }
   }
}

function input_blur(e)
{
   var o = domE(e);
   if (o)
   {
      if (trim(o.value).length == 0)
      {
         o.style.color = '#666';
         o.value = domAttr(o, 'title');

      }
   }
}


function selectLoc(a)
{
   domID('search').value = unescape(a);
   var l = domID('locations');
   l.style.display = 'none';
}

function domClass(c, t, p)
{
   if (!p)
      var p = document.body;
   if (!t)
      var t = '*';
   var o = p.getElementsByTagName(t);
   var out = new Array();
   for (var i=0;i<o.length;i++)
   {
      var cl = o[i].className.toString().split(' ');
      for (var k=0; k<cl.length; k++)
         if (cl[k]==c)
            out[out.length] = o[i];
   }
   if (out.length>0)
      return out;
   return false;
}

function domID(i)
{
   if (document.getElementById)
      return document.getElementById(i);
   return false;
}

function domEvent(o, t, f)
{
   // attach event to object
   // o object
   // t type of even, eg 'keyup'
   // f function to execute
   if (typeof(t)=='string')
   {
      if (o.addEventListener)
         o.addEventListener(t, f, false);
      else if (o.attachEvent)
         o.attachEvent('on'+ t, f);
      else
         return false;
   } else {
      for (var i=0; i<t.length; i++)
      {
         if (o.addEventListener)
            o.addEventListener(t[i], f, false);
         else if (o.attachEvent)
            o.attachEvent('on'+ t[i], f);
         else
            return false;
      }
   }
   return true;
}

function domAttr(o, a)
{
   if (o)
   {
      if (o.hasAttribute)
      {
         if (o.hasAttribute(a))
            return o.getAttribute(a);
      } else
         if (o.getAttribute(a) != null)
            return o.getAttribute(a);
      return false;
   }
   return '';
}

function domE(e)
{
   if (!e)
      var e = window.event;
   if (e.target)
      return e.target;
   else if (e.srcElement)
      return e.srcElement;
   else
      return false;
}


function domTN(t, p)
{
   // return child elements of a parent with the given tagname
   // t tagname
   // p parent node
   if (p)
      return p.getElementsByTagName(t);
   else
      return document.getElementsByTagName(t);
}

function domValue(t, p)
{
   // return either value of a object node (text contained within the node) (p optional)
   // or return the value of an object node with the tagname t from a parentnode
   // t either object or tagname
   // p parentNode
   if (typeof(t)=='string')
      if (p.getElementsByTagName)
         if (p.getElementsByTagName(t))
            if (p.getElementsByTagName(t)[0])
               if (p.getElementsByTagName(t)[0].firstChild)
                  return p.getElementsByTagName(t)[0].firstChild.nodeValue;
   if (typeof(t)=='object' && t!=null)
      if (t.firstChild)
         return t.firstChild.nodeValue;
   return '';
}

function domCreate(t, a, c, s)
{
   // create a new node
   // t tagname
   // a attributes (array)
   // c classname
   // s string to place within
   var o = document.createElement(t);
   if (a)
      for (var i in a)
         if (i.length>2)
            o.setAttribute(i, a[i]);
   if (c)
      o.className = c;
   if (s)
   {
      if (typeof(s)=='object')
         o.appendChild(s);
      else
         o.appendChild(document.createTextNode(s));
   }
   return o;
}

function showPic(o)
{
   var s = o.style.backgroundImage.toString();
   s = s.replace('url\(', '').replace('/75-','/').replace('\)','');
   var i = domID('preview');
   if (i)
         i.src = s;
}

function showContact(o)
{
   domTN('div', o.parentNode.parentNode)[0].style.display = 'block';
   domTN('div', o.parentNode.parentNode)[1].style.display = 'none';
}

var map      = false;   

function initMap()
{
   if (address)
   {
        map = new google.maps.Map2(document.getElementById('map'));
        map.addControl(new GSmallMapControl());
        map.removeMapType(G_SATELLITE_MAP);
        map.addControl(new GMapTypeControl());
        var geocoder = new GClientGeocoder();
        var g = geocoder.getLatLng(address, drawMap);
   }
}

function drawMap(p)
{
  if (p && map)
  {
      map.setCenter(p, 15);
      var m = new GMarker(p);
      map.addOverlay(m);
  } else
      domID('map').style.display='none';
}


var ajx_r = false;
var ajx_c = false;
var ajx_u = false;
var ajx_q = false;

function ajx(u, q, c)
{
   if (window.XMLHttpRequest)
      ajx_r = new XMLHttpRequest();
   else if (window.ActiveXObject)
      ajx_r = new ActiveXObject('Microsoft.XMLHTTP');
   else
      return false;
   ajx_c = c;
   ajx_u = u;
   ajx_q = q;
   var qu = q +'&r='+ Math.random(10000, 999999999); // prevent browser caching
   if (c)
      ajx_r.onreadystatechange = _ajxProcess;
   ajx_r.open('POST', u, true);
   ajx_r.setRequestHeader('Content-length', qu.length);
   ajx_r.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
   ajx_r.setRequestHeader('Connection', 'close');
   ajx_r.send(qu);
   return true;
}

function _ajxProcess()
{
   if (ajx_r.readyState==4)
   {
      if (ajx_r.status==200)
      {
          this.text  = ajx_r.responseText;
          this.xml   = ajx_r.responseXML;
          this.url   = ajx_u;
          this.query = ajx_q;
          this.go    = eval(ajx_c);
          ajx_r = false;
          ajx_c = false;
          ajx_s = false;
          ajx_u = false;
          ajx_q = false;
          this.go();
      } else {
         ajx_r = false;
      }
   }
}

function trim(s)
{
   if (s)
      return s.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
   return '';
}