// JavaScript Document

function initRollovers() {
    if (!document.getElementById) return
      
    var aPreLoad = new Array();
    var sTempSrc;
    var aImages = document.getElementsByTagName('img');
    for (var i = 0; i < aImages.length; i++) {
	if (aImages[i].className == 'rollover') {
	    var src = aImages[i].getAttribute('src');
	    var ftype = src.substring(src.lastIndexOf('.'), src.length);
	    var hsrc = src.replace(ftype, '_roll'+ftype);

	    aImages[i].setAttribute('hsrc', hsrc);
	    
	    aPreLoad[i] = new Image();
	    aPreLoad[i].src = hsrc;
	    
	    aImages[i].onmouseover = function() {
		sTempSrc = this.getAttribute('src');
		this.setAttribute('src', this.getAttribute('hsrc'));
	    }
	    aImages[i].onmouseout = function() {
		if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_roll'+ftype, ftype);
		this.setAttribute('src', sTempSrc);
	    }
	}
    }
}


var newWin = null;
function popUp(strURL, strType, strHeight, strWidth) {
 if (newWin != null && !newWin.closed)
   newWin.close();
 var strOptions="";
 if (strType=="console")
   strOptions="resizable,height="+strHeight+",width="+strWidth;
 if (strType=="fixed")
   strOptions="status,height="+strHeight+",width="+strWidth;
 if (strType=="elastic")
   strOptions="toolbar,menubar,scrollbars,"+"resizable,location,height="+strHeight+",width="+strWidth;
 newWin = window.open(strURL, 'newWin', strOptions);
 newWin.focus();
}


//Any changes to this must be copied to the Rails terrischeer site's rollover.js
function tracked_download(cat, cat2, cat3, url)
{
  pageTracker._trackPageview(url);
  top.location.href = url;
  
  // var first_loaded = false;
  // var second_loaded = false;
  // var third_loaded = false;
  // 
  // var axel = Math.random() + "";
  // var num = axel * 1000000000000000000;
  // var spotpix = new Image();
  // spotpix.onload = function()
  // {
  //   if(second_loaded && third_loaded) top.location.href = url;
  //   first_loaded = true;
  // };
  // spotpix.src = "http://ad.doubleclick.net/activity;src=1783192;type=verog347;cat=" + cat + ";ord=" + num;
  // 
  // var spotpix2 = new Image();
  // spotpix2.onload = function()
  // {
  //   if(first_loaded && third_loaded) top.location.href = url;
  //   second_loaded = true;
  // };
  // spotpix2.src = "http://au.track.decideinteractive.com/n/71627/71629/" + cat2 + "/x/e?value=0&trans=&domain=au.track.decideinteractive.com";
  // 
  // var spotpix3 = new Image();
  // spotpix3.onload = function()
  // {
  //   if(first_loaded && second_loaded) top.location.href = url;
  //   third_loaded = true;
  // };
  // spotpix3.src = "http://pixel2257.everesttech.net/2257/p?ev_TS" + cat3 + "AppForm=1";
}



function add_embargoes()
{
  $("input.embargoed").each(function()
  {
    var input = $(this);

    input.keypress(function(event)
    {
      if(event.which == 13) event.preventDefault();
    });
    
    input.change(function(event)
    {
      var input = $(event.target);

      var val = parseInt(input.val().replace(/[^0-9]/, ''), 10);
      
      var single_postcodes = [2396, 2831, 2840, 800, 822];
      var postcode_ranges = [
      ];

      var failed = false;
      
      for(var i = 0; i < single_postcodes.length; i++)
      {
        if(single_postcodes[i] == val)
        {
          failed = true;
          break;
        }
      }

      for(var i = 0; i < postcode_ranges.length; i++)
      {
        if(val >= postcode_ranges[i][0] && val <= postcode_ranges[i][1])
        {
          failed = true;
          break;
        }
      }
      
      if(failed)
      {
        $.fn.colorbox({ href: '/embargo.html', iframe: true, width: 450, height: 280, overlayClose: false, close: '' });
      }
    });
  });
}

window.onload = function()
{
  initRollovers();
  //if(jQuery) add_embargoes();
};