// @@ GLOBAL VARIABLES

var mainDomain="pinpointsolutions.co.uk";
var siteName="Pinpoint Management Training Solutions";
var postcode="AL1+3BE";
var styleSheet="css/default.css";
var qCount=16;
var now = new Date();
var quoteInterval = 15000;
var fadeDuration = 4;
var thisQuote = 1;
var lastQuote = qCount;

window.onload = function() {
  winOnResize(); // set initial position
  new xMenu1('t1','m1',1,'mouseover');
  new xMenu1('t2','m2',1,'mouseover');
  new xMenu1('t3','m3',1,'mouseover');
  new xMenu1('t5','m5',1,'mouseover');
  new xMenu1('t6','m6',1,'mouseover');
  new xMenu1('t8','m8',1,'mouseover');
  new xMenu1('t9','m9',1,'mouseover');
  xAddEventListener(window, 'resize', winOnResize, false);
  if (!xGetCookie("quoteSetting")) {
    thisQuote = generate(1,qCount);
    fadeIn(xGetElementById('q'+thisQuote),fadeDuration)
    setTimeout("xSetCookie('quoteSetting', "+thisQuote+", daysFromNow(364));",fadeDuration*1000);
  } else {
      thisQuote = xGetCookie("quoteSetting");
      if (document.referrer.indexOf(mainDomain)==-1) fadeIn(xGetElementById('q'+thisQuote),fadeDuration)
      else xShow('q'+thisQuote);
    }
  setTimeout("cycleQuote("+thisQuote+",1);",quoteInterval/1.5);
}

function fadeIn(obj,dur) {
  xHide(obj);
  if (xIE4Up) {
    obj.style.filter="blendTrans(duration="+dur+")";
    obj.filters.blendTrans.Apply();
    xShow(obj);
  } else {
    obj.style.opacity="0.05";
    xShow(obj);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.1';",200);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.15';",400);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.2';",600);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.25';",800);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.3';",1000);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.35';",1200);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.4';",1400);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.45';",1600);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.5';",1800);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.55';",2000);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.6';",2200);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.65';",2400);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.7';",2600);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.75';",2800);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.8';",3000);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.85';",3200);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.9';",3400);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.95';",3600);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='1.0';",3800);
  }
  if (xIE4Up) obj.filters.blendTrans.Play();
}

function fadeOut(obj,dur) {
  xShow(obj);
  if (xIE4Up) {
    obj.style.filter="blendTrans(duration="+dur+")";
    obj.filters.blendTrans.Apply();
    xHide(obj);
  } else {
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.95';",200);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.9';",400);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.85';",600);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.8';",800);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.75';",1000);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.7';",1200);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.65';",1400);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.6';",1600);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.55';",1800);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.5';",2000);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.45';",2200);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.4';",2400);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.35';",2600);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.3';",2800);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.25';",3000);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.2';",3200);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.15';",3400);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.1';",3600);
    setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='0.05';",3800);
    setTimeout("xHide('q"+thisQuote+"')",4000);
  }
  if (xIE4Up) obj.filters.blendTrans.Play();
}

function cycleQuote(lastQuote,incr) {
  fadeOut(xGetElementById('q'+lastQuote),fadeDuration);
  thisQuote = eval(lastQuote)+incr;
  if (thisQuote>qCount) thisQuote=1;
  if (thisQuote<1) thisQuote=qCount;
  fadeIn(xGetElementById('q'+thisQuote),fadeDuration);
  setTimeout("xSetCookie('quoteSetting', "+thisQuote+", daysFromNow(364));",fadeDuration*1000);
  setTimeout("cycleQuote("+thisQuote+",1);",quoteInterval);
}

function winOnResize() {
  xHeight('content',xClientHeight()-135);
  xHeight('news',xClientHeight()-250);
  xTop('right',xHeight('header')+xHeight('footer')-xClientHeight());
}

/* xMenu1 Object Prototype

  Parameters:
    triggerId   - id string of trigger element.
    menuId      - id string of menu.
    mouseMargin - integer margin around menu;
                  when mouse is outside this margin the menu is hid.
    openEvent   - string name of event on which to open menu ('click', 'mouseover', etc).
*/

function xMenu1(triggerId, menuId, mouseMargin, openEvent)
{
  var isOpen = false;
  var trg = xGetElementById(triggerId);
  var mnu = xGetElementById(menuId);
  if (trg && mnu) {
    xAddEventListener(trg, openEvent, onOpen, false);
  }
  function onOpen()
  {
    if (!isOpen) {
//      xMoveTo(mnu, xPageX(trg), xPageY(trg) + xHeight(trg));
      xMoveTo(mnu, xWidth(trg)+1, xPageY(trg)-134);
      xBackground(trg,'#37B');
      xColor(trg,'#FFF');
      xShow(mnu);
      xAddEventListener(document, 'mousemove', onMousemove, false);
      isOpen = true;
    }
  }
  function onMousemove(ev)
  {
    var e = new xEvent(ev);
    if (!xHasPoint(mnu, e.pageX, e.pageY, -mouseMargin) &&
        !xHasPoint(trg, e.pageX, e.pageY, -mouseMargin))
    {
      xBackground(trg,'#147');
      xColor(trg,'#DDF');
      xHide(mnu);
      xRemoveEventListener(document, 'mousemove', onMousemove, false);
      isOpen = false;
    }
  }
} // end xMenu1

  if (!styleSheet) var styleSheet="main.css";

// @@@@@ adding a favourite @@@@@

function addBookmark() {
  if (window.external)
    external.AddFavorite("http://www."+mainDomain+"/",siteName)
  else
    alert("We're sorry, your browser does not support this feature.\n\nPlease manually add our site to your Bookmarks\n(typically, keystroke Ctrl+D), thank you.");
}

function drawAddFav(linkText) {
  if (!linkText) var linkText="Add us to your Favourites";
  document.write('<a href="javascript:addBookmark();" target="_self" title="Click now to add us to your Favourites">'+linkText+'</a>');
}


// @@@@@ generating a spam-safe e-mail address @@@@@

function drawEmail(alias,text,dn,title) {
  var at = '&#064;'; // at symbol encoded
  var p1 = '&#109;&#097;&#105;&#108;'; // protocol name encoded part 1
  var p2 = '&#116;&#111;&#58;'; // protocol name encoded part 2
  if (!dn) dn=mainDomain;
  if (title) title=' title="'+title+'"'
    else title='';
  if (!text) text=alias+at+dn;
  document.write('<a href="'+p1+p2+alias+at+dn+'"'+title+'>'+text+'</a>');
}


// @@@@@ popup window to Image size and wrap in HTML @@@@@

function popupImg(img,addH,addW,caption) {
  var w = ((addW) ? addW : 0);
  var h = ((addH) ? addH : 0);
  var c = ((caption) ? ('<p align="center">' + caption + '</p>') : '');
  remoteWin = window.open("","photo","resizable=1,width=200,height=200,status=1,scrollbars="+(((h<0)||(w<0)) ? "1" : "0"));
  with (remoteWin.document) {
    open('text/html', 'replace');
    write('<html><head><meta http-equiv="imagetoolbar" content="no"><title>'+siteName+'</title><link rel="stylesheet" type="text/css" href="http://www.'+mainDomain+'/'+styleSheet+'"></head><body style="margin:20px;font-size:11px;" onload="resizeTo(document.popupImg.width+60+'+w+',document.popupImg.height+190+'+h+'); window.focus()"><p align="center"><img border="0" src="' + img + '" name="popupImg"></p>' + c + '<p align="center">[<a href="javascript:window.close()">close window</a>]</p></body></html>');
    close();
  }
}

function popupTxt(txt,s,w,h,r) {
  remoteWin=window.open("","text","scrollbars=" + s + ",width=" + w + ",height=" + h + ",resizable=" + r);
  with (remoteWin.document) {
    open('text/html', 'replace');
    write('<html><head><title>'+siteName+'</title><link rel="stylesheet" type="text/css" href="http://www.'+mainDomain+'/'+styleSheet+'"></head><body style="margin:10px;">'+txt+'<p>[<a href="javascript:window.close()">close window</a>]</p></body></html>');
    close();
  }
}

// @@@@@ generic popup window @@@@@

function popup(doc,name,scroll,width,height,resize,status,toolbar,locbar,menubar,dirs) {
  var scr = ((scroll) ? ",scrollbars="+scroll : "");
  var wid = ((width) ? ",width="+width : "");
  var hei = ((height) ? ",height="+height : "");
  var res = ((resize) ? ",resizable="+resize : "");
  var sta = ((status) ? ",status="+status : "");
  var too = ((toolbar) ? ",toolbar="+toolbar : "");
  var loc = ((locbar) ? ",location="+locbar : "");
  var men = ((menubar) ? ",menubar="+menubar : "");
  var dir = ((dirs) ? ",directories="+dirs : "");
  remoteWin=window.open(doc,name,scr+wid+hei+res+sta+too+loc+men+dir);
  remoteWin.focus();
}


function drawDate(D,d,M,y) {
  var now = new Date();
  now.setTime(now.getTime() - 0);
  var day = now.getDay();
  var date = now.getDate();
  var month = now.getMonth();
  var year = now.getFullYear();
  year += (year < 1900) ? 1900 : 0;
  var days = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
  var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
  var theDate = "";
  theDate += (D) ? days[day] + ", " : "";
  theDate += (d) ? date + " " : "";
  theDate += (M) ? months[month] + " " : "";
  theDate += (y) ? year : "";
  document.write(theDate);
}


// @@@@@ DirectMe @@@@@

var validPC=true;
function isPC(pc) {
  if (validPC) popup('http://www.google.co.uk/local?f=d&hl=en&q=from%3A+'+pc+'+to%3A+'+postcode,'glocal','1','795','580','1')
    else alert("Invalid post code.\nPlease try again.");
}
// 'http://www.mapquest.co.uk/cgi-bin/mqtrip?link=btwn/twn-ddir_options_jumppage&print.x=1&LangOpt=1&CC_0=GB&CC_1=GB&CITY_1=AYLESBURY&ZIP_1='+postcode+'&ZIP_0='+pc


// @@@@@ PrintPage @@@@@

function drawPrintLink(text,title,conf) {
  if (title) title=' title="'+title+'"'
    else title=' title="Print this page"';
  if (!text) text='print';
  if (conf)
  document.write('<a href="javascript:window.print();" onclick="return confirm(\'Conserve our environment.  \\n\\nOnly print this page if you\\nneed to. Continue?\');"'+title+'>'+text+'</a>');
}


function daysFromNow(days) {
  delete now;
  now = new Date();
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    now.setTime(now.getTime() - skew);
  // above 'fix' from DocJS.com, for older browsers I think :S
  now.setTime(now.getTime() + days * 24 * 60 * 60 * 1000);
  return now;
}

function generate(x, y) { // creates a random number between two given integers

  var range = y - x + 1;
  return (Math.floor(Math.random() * range) + x);

}; // END generate()

