/* CONTENT:
1. global variables
2. assisting functions
3. email writer
4. open widows
5. scroll bars
6. load and swap images
7. create flash object
8. pop-up messages
9. change class


global variables
*/
var pageloaded;
//for scrollbar
var ObjN = new Array();
var clipTop = new Array();
var clipWidth = new Array();
var clipBottom = new Array();
var clipLeft = new Array();
var topper = new Array();
var lefter = new Array();
var lyrheight = new Array();
var lyrwidth = new Array();
var done = 0;
var i = 0;
var time;
var amount;
var theTime;
//for images
var imgarray = new Array();











/*
assisting functions
*/
function check () {
 if (document.getElementById || document.all || document.layers) {
  return true;
 }
 else {
  alert("Please upgrade your browser");
 }
}

function isEven(v) {
 return parseInt(v) % 2 == 0;
}
function getimagename(f) {
 f = f.substring(f.lastIndexOf('/'))
 f = replacer(f, "/", "");
 f = replacer(f, ".jpg", "");
 return f;
}
function replacer(string, search, replace) { /*  **    s  string to be processed  **    t  token to be found and removed  **    u  token to be inserted */
 position = string.indexOf(search);
 newstring = "";
 if (position == -1) return string;
 newstring += string.substring(0,position) + replace;
 if ( position + search.length < string.length) {
  newstring += replacer(string.substring(position + search.length, string.length), search, replace);
 }
 return newstring;
}

function getObj(name) {
 if (document.getElementById) {
  this.obj = document.getElementById(name);
  if(this.obj) {
   this.style = document.getElementById(name).style;
  }
 }
 else if (document.all) {
  this.obj = document.all[name];
  if(this.obj) {
   this.style = document.all[name].style;
  }
 }
 else if (document.layers) {
  this.obj = document.layers[name];
  if(this.obj) {
   this.style = document.layers[name];
  }
 }
}










/*
 email writer to protect emails
*/
function emailwriter(contact, email, host, subject) {
 document.write('<a href="' + 'mail' + 'to:' + email + '@' + host + '?subject=' + subject + '">' + contact + ' &#64; ' + host + '</a>');
}











/*
 open windows
*/

function OWi(n){
 fln = "showimage.php?i=" + n;
 PU(fln, 1100, 700,'images',true,false,false,false,false);
 return false;
}

function OWipv(n){
 fln = "showimage.php?i=" + n + "&pv=1";
 PU(fln, 400, 200,'images',false,true,false,false,false);
 return false;
}

function OWppv(g){
 if(g == 0 || g == 1 || g == 2 || g == 9 || g == 10) {
  fln = "past.php?o=" + g;
 }
 else if(g == 3) {
  fln = "eat.php?d=1";
 }
 else if(g == 4) {
  fln = "drink.php?d=1";
 }
 else if(g == 5) {
  fln = "live.php";
 }
 else if(g == 6) {
  fln = "family.php";
 }
 else if(g == 7) {
  fln = "drink.php";
 }
 else if(g == 7) {
  fln = "eat.php";
 }
 else if(g == 11 || g == 15 || g == 16 || g == 17) {
  if(g == 11) {
   g = 0;
  }
  else {
   g = g - 13;
  }
  fln = "live.php?g=";
 }
 PU(fln, 1100, 700,'images',false,true,false,false,false);
 return false;
}


function OWd(n){
 fln = "pdfPopUp.php?file=" + n; //"/d/matterhorn_" + n + ".pdf";
 PU(fln, 770, 650,'documents',false,true,false,false,false);
}

function OWdFlash(n){
 fln = "pdfPopUp.php?file=" + n; //"/d/matterhorn_" + n + ".pdf";
 PU(fln, 550, 700,'documents',false,true,false,false,false);
}

function OWh(fln){
 fln = "http:\/\/" + fln;
 PU(fln, 780, 600,'family',false,true,false,false,false);
 return true;
}

function openNewWindow(v1, v2, v3, v4, v5, v6) {
 OWr();
}

function OWr(){
 fln = "horn_radio/radio_Horn.php"
 PU(fln, 875, 215,'radiohorn',false,false,false,false,false);
 return true;
}

function PU (url,x,y,name,sb,rs,st,lt,tb) {
 var win = null;
 var scrollbarstext = 'scrollbars = no,'; if (sb){scrollbarstext = 'scrollbars = yes,'}
 var resizabletext = 'resizable = no,'; if (rs){resizabletext = 'resizable = yes,'}
 var statustext = 'status = no,'; 	if (st){statustext = 'status = yes,'}
 var locationtext = 'location = no,'; 	if (lt){locationtext = 'location = yes,'}
 var toolbartext = 'toolbar = no,'; 	if (tb){toolbartext = 'toolbar = yes,'}
 var features = '';
 features = scrollbarstext+resizabletext+statustext;
 features = features + locationtext+toolbartext;
 features = features + 'width = '+x+',height = '+y;
 win = window.open(url, name, features);
 //win.document.onload = resizer;
 return true;
}

function resizer() {
 //alert(window.name);
 if(window.name == "images") {
  //alert("step 1");
  if(imgL = document.getElementById('largeimage')) {
   var wresize = 200 + imgL.width; //document.body.client
   var hresize = 200 + imgL.height; //document.body.client
   window.resizeTo(wresize, hresize);//1200-win.document.body.clientWidth,800-win.document.body.clientHeight
  }
 }
}













/*
 scroll bars
*/


function init(Obj, t, w, b, l, o, e, d, noi) {
 i++;
 ObjN[i] = Obj;
 clipTop[i] = t;
 clipWidth[i] = w;
 clipBottom[i] = b;
 clipLeft[i] = l;
 topper[i] = o;
 lefter[i] = e;
 if (!check()) return;
 var x = new getObj(ObjN[i]);
 if (document.layers) {
  lyrheight[i] = x.style.clip.bottom;
  lyrwidth[i] = x.style.clip.width;
  lyrheight[i] += 20;
  lyrwidth[i] += 20;
  x.style.clip.top = clipTop[i];
  x.style.clip.left = clipLeft[i];
  x.style.clip.right = clipWidth[i];
  x.style.clip.bottom = clipBottom[i];
 }
 else if (document.getElementById || document.all) {
  lyrheight[i] = x.obj.offsetHeight+20;
  lyrwidth[i] = 3900; //x.obj.offsetWidth+20;
  x.style.clip = 'rect('+clipTop[i]+'px,'+clipWidth[i]+'px,'+clipBottom[i]+'px,'+clipLeft[i]+'px)';
 }
 pageloaded = 1;
}

function scrollayer(x,amt,tim, sideways) {
 if (!check() || !pageloaded) {  alert("Please wait until the page is loaded"); return;}
 i = x;
 if (!check()) return;
 thelayer = new getObj(ObjN[x]);
 if (!thelayer) return;
 amount = amt;
 theTime = tim;
 if(sideways == 1) {
  sidescroll();
 }
 else {
  realscroll();
 }
}

function sidescroll() {
 clipLeft[i] += amount;
 clipWidth[i] += amount;
 lefter[i] -= amount;
 if (clipLeft[i] < 0 || clipWidth[i] > lyrwidth[i]) {
  clipLeft[i] -= amount;
  clipWidth[i] -= amount;
  lefter[i] += amount;
  return;
 }
 if (document.getElementById || document.all) 	{
  clipstring = 'rect('+clipTop[i]+'px,'+clipWidth[i]+'px,'+clipBottom[i]+'px,'+clipLeft[i]+'px)';
  thelayer.style.clip = clipstring;
  thelayer.style.left = lefter[i] + 'px';
 }
 else if (document.layers) {
  thelayer.style.clip.left = clipLeft[i];
  thelayer.style.clip.width = clipWidth[i];
  thelayer.style.left = lefter[i];
 }
 if(!done) {
  if(amount > 0 ) {
   var oi = new getObj("isright");
  }
  else {
   var oi = new getObj("isleft");
  }
  oi.obj.src = replacer(oi.obj.src, ".gif", "o.gif");
  oi.obj.onmousedown = function () {amount = Math.round(amount * 2); };
  oi.obj.onmouseup = function () {amount = Math.round(amount / 2); };
  done = 1;
 }
 time = setTimeout('sidescroll()',theTime);
}

function realscroll() {
 clipTop[i] += amount;
 clipBottom[i] += amount;
 topper[i] -= amount;
 if (clipTop[i] < 0 || clipBottom[i] > lyrheight[i]) {
  clipTop[i] -= amount;
  clipBottom[i] -= amount;
  topper[i] += amount;
  return;
 }
 if (document.getElementById || document.all) 	{
  clipstring = 'rect('+clipTop[i]+'px,'+clipWidth[i]+'px,'+clipBottom[i]+'px,'+clipLeft[i]+'px)';
  thelayer.style.clip = clipstring;
  thelayer.style.top = topper[i] + 'px';
 }
 else if (document.layers) {
  thelayer.style.clip.top = clipTop[i];
  thelayer.style.clip.bottom = clipBottom[i];
  thelayer.style.top = topper[i];
 }
 if(!done) {
  if(amount > 0 ) {
   var x = new getObj("isdw");
  }
  else {
   var x = new getObj("isup");
  }
  x.obj.src = replacer(x.obj.src, ".gif", "o.gif");
  x.obj.onmousedown = function () {amount = Math.round(amount * 2); };
  x.obj.onmouseup = function () {amount = Math.round(amount / 2); };
  done = 1;
 }
 time = setTimeout('realscroll()',theTime);
}

function stopscroll() {
 var oi = new getObj("isdw");
 if(oi) {oi.obj.src = replacer(oi.obj.src, "o.gif", ".gif"); }
 var oi = new getObj("isup");
 if(oi) {oi.obj.src = replacer(oi.obj.src, "o.gif", ".gif"); }
 done = 0;
 if (time) clearTimeout(time);
}

function stopsidescroll() {
 var oi = new getObj("isright");
 if(oi) {oi.obj.src = replacer(oi.obj.src, "o.gif", ".gif"); }
 var oi = new getObj("isleft");
 if(oi) {oi.obj.src = replacer(oi.obj.src, "o.gif", ".gif"); }
 done = 0;
 if (time) clearTimeout(time);
}










/*
load and swap images
*/

function load_images() {
 if (!check()) return;
 var nav = document.getElementById('sws');
 var imgs = nav.getElementsByTagName('img');
 for (var icnt =0;icnt<imgs.length;icnt++) {
  imgs[icnt].onclick = change_image;//function () {return !change_image(icnt); };
  imgs[icnt].onmouseover = increasei;
  imgs[icnt].onmouseout = decreasei;
  var imgn = replacer(imgs[icnt].src, "\/S\/","\/M\/");
  imgarray[icnt] = new Image();
  imgarray[icnt].src = imgn;
  imgs[icnt].number = icnt;
 }
 highligher(0, 1);
 for(q = 1; q <= i; q++) {
  lyrwidth[q] = (icnt * 35) + 200;
 }
}

function increasei() {
 this.className = "msi";
}


function decreasei() {
 this.className = "";
}

function change_image () {
 if (!check()) return;
 var i1 = this.number;//this.number;
 var i2 = i1;
 if(isEven(i1)) {
  i2++;
  var img1 = new getObj('mei');
  var img2 = new getObj('mui');
  var lin1 = new getObj('amei');
  var lin2 = new getObj('amui');
 }
 else {
  i2--;
  var img1 = new getObj('mui');
  var img2 = new getObj('mei');
  var lin1 = new getObj('amui');
  var lin2 = new getObj('amei');
 }
 img1.obj.src = imgarray[i1].src;
 img2.obj.src = imgarray[i2].src;
 highligher(i1, i2);
 lin1.obj.onclick = function() { OWi(getimagename(imgarray[i1].src)); return false;};
 lin2.obj.onclick = function() { OWi(getimagename(imgarray[i2].src)); return false;};
 lin1.obj.href = "showimage.php?i=" + getimagename(imgarray[i1].src);
 lin2.obj.href = "showimage.php?i=" + getimagename(imgarray[i2].src);
 return false;
}

function highligher(tobehigh1, tobehigh2) {
 var nav = document.getElementById('sws');
 var imgs = nav.getElementsByTagName('img');
 for (var icnt =0;icnt<imgs.length;icnt++) {
  if(icnt == tobehigh1 || icnt == tobehigh2 ) {
   imgs[icnt].className = "msi";
   imgs[icnt].onmouseout = "";
  }
  else {
   imgs[icnt].className = "";
   imgs[icnt].onmouseout = decreasei;
  }
 }
}

function arrow_hl (name) {
 if (!check()) return;
 var oi = new getObj(name);
 oi.obj.src = replacer(oi.obj.src, ".gif", "o.gif");
}

function arrow_ul (name) {
 if (!check()) return;
 var oi = new getObj(name);
 oi.obj.src = replacer(oi.obj.src, "o.gif", ".gif");
}







/*
Create a flash object
*/

function makeflash(name, width, height) {
 return; //to be taken out in final version!
 if (!check()) return;
 name = 'v/' + name + '.swf';
 var box = new getObj("flasher");
 if(box) {
  var fla = createflashObject(name, width, height);
  box.obj.appendChild(fla);
  fla.appendChild(createParam('movie', name));
  fla.appendChild(createParam('quality', 'high'));
  fla.appendChild(createParam('bgcolor', '#000000'));
  return box;
 }
}

function createflashObject(name, width, height) {
 var obj = document.createElement('object');
 obj.setAttribute('type', 'application/x-shockwave-flash');
 obj.setAttribute('data', name);
 obj.setAttribute('width', width);
 obj.setAttribute('height', height);
 return obj;
}

function createParam(n, v) {
 var el = document.createElement('param');
 el.setAttribute('name', n);
 el.setAttribute('value', v);
 return el;
}


/*
 Double check messages
*/

function disclaimer() {
 alert("Matterhorn endeavours to keep all items as current as possible. This website is intended as a guide only and all food and beverages are subject to seasonal changes and price variations.");
 return true;
}

function dc() {
 return !(confirm('You are about to permanently delete the image. Do you want to go ahead?')==false)
}

/*
Change class
*/


function hc(imageobject) {
 imageobject.className = "iabh";
}
function nc(imageobject) {
 imageobject.className = "iab";
}
