// Vars used by up and down
var popup;
var popupBody;
function upyoupop(elem,p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18,p19,p20) {  

// 0-elem=object,1-p0=Title,2-p1=Number of columns,3-p2=Width of column,4-p3=Alignment of column,5-p4=Filename
// 6-p5=Height of picture,7-p6=Width of column,8-p7=Alignment,9-p8=Filename,10-p9=Height of picture

var ArgCount = upyoupop.arguments.length;
var HtmlText='';

// Set tooltip for non IE browsers
elem.title = decodeURIComponent( p0 );

// Create Popup and populate it. Clear the tooltip if popup is working
popup=window.createPopup();
if ( popup ) { elem.title = ""; } 
popupBody=popup.document.body;
popupBody.style.border="solid 1px white";
popupBody.style.backgroundColor="black";
popupBody.style.color="white";
popupBody.style.fontFamily="Arial";
popupBody.style.fontSize="6px";

// Heading
HtmlText = '<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__________________________&nbsp;&nbsp;t<font color="gray">alking <\/font>w<font color="gray">alls<\/font><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-<font color="gray">---<\/font> | -<font color="gray">-----<\/font> | -<font color="gray">---</\font> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:. : :.. :..<BR>';

// Picture(s)
HtmlText += '<HTML><TABLE style="width:100%;" border=0><TR>';
limit=((upyoupop.arguments[ 2 ]*3)+3);
for( var i=3; i < limit; i=i+4 ) {
  if ( upyoupop.arguments[ i ] == 0 ) {
    HtmlText += '<TD style="height: 50px; vertical-align: center; text-align: ';
    if ( upyoupop.arguments[ i + 1 ] == "" ) { HtmlText += 'center; ">'; } else { HtmlText += upyoupop.arguments[ i + 1 ] + ';">' }
    HtmlText += '<img src="twc_showimage.php?image=' + upyoupop.arguments[ i + 2 ]+'&amp;mode=3&amp;height=' + upyoupop.arguments[ i + 3 ]/6 + '" ><\/TD>';
    }
  }

// Title
HtmlText += '<\/TR><\/TABLE><TABLE style="width:100%;" border=0><TR><TD style="text-align: center; font-size: 9; font-face: arial;">' + decodeURIComponent(upyoupop.arguments[ 1 ]) + '<\/TD><\/TR><\/TABLE><\/HTML>';

// Set popup window offset
var offset=250;
if (elem.id == "previous") { offset = -375; }

// Now show it
popupBody.innerHTML = HtmlText;
popup.show(offset,-90,150,100,elem);
}

function downyoupop() {
popup.hide();
popup = null;
}

// Variables used by fadeinandout
var myWords = ["stylish","inspiring","professional","unique","personalised","impact"];
var myWordsCnt = myWords.length - 1;
var myWordsPtr = Math.floor(Math.random() * (myWordsCnt+1))

function fadeinandout(id, speed, dwell, delay) {

  // Speed is fadeup/down speed, Dwell is time image stays on for, Delay is time between images
  // Get next word from global array
  var ThisWord = myWords[myWordsPtr];
  myWordsPtr++;
  if (myWordsPtr > myWordsCnt) {
    myWordsPtr = 0;
    }
  // Set random word positions...
  document.getElementById(id).style.top = 60 + (Math.random() * 240);
  document.getElementById(id).style.left = -140 + (Math.random() * 280);

  // Fade the word in, then out
  document.getElementById(id).innerHTML = ThisWord;
  opacity(id,1,100,(speed * 2));
  setTimeout("opacity('" + id + "', 100, 1, " + speed + ")", dwell);

  // Set trigger for the next word
  setTimeout("fadeinandout('" + id + "'," + speed + ", " + dwell + ", " + delay + " )", delay);

}

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 1;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

// Set firstrun variable to 0 so that on firstrun, the image fades in straight away.
var firstrun = 1;
var gspeed = 0;
var gpause = 0;

//find next image
function nextImage(o) {
    do o = o.nextSibling;
    while(o && o.tagName != 'IMG');
    return o;
}

//find first image inside an element
function firstChildImage(o) {

    o = o.firstChild;

    while(o && o.tagName != 'IMG') {
        o = o.nextSibling;
    }

    return o;
}

//set the opacity of an element to a specified value
function setOpacity(obj, o) {

    obj.style.opacity = (o / 100);
    obj.style.MozOpacity = (o / 100);
    obj.style.KhtmlOpacity = (o / 100);
    obj.style.filter = 'alpha(opacity=' + o + ')';
}

//make image invisible and set next one as current image
function getNextImage(image) {
	
    if (next = nextImage(image)) {
	image.style.display = 'none';
	image.style.zIndex = 0;

	next.style.display = 'block';
	next.style.zIndex = 100;

    } else {
	//if there is not a next image, get the first image again
        next = firstChildImage(image.parentNode);
    }

    return next;
}

//set default values for parameters and starting image
function blendImages(id, speed, pause, caption) {

    if(speed == null) {
        speed = 30;
    }
    gspeed = speed;

    if(pause == null) {
        pause = 1500;
    }
    gpause = pause;

    var blend = document.getElementById(id);

    var image = firstChildImage(blend);

    startBlending(image, speed, pause, caption);

}

//make image a block-element and set the caption
function startBlending(image, speed, pause, caption) {

    image.style.display = 'block';

    if(caption != null) {
	document.getElementById(caption).innerHTML = image.alt;
    }

    continueFadeImage(image, 0, speed, pause, caption);
}

// ASC: copied from http://www.sean.co.uk/a/webdesign/javascriptdelay.shtm
function pausecomp(millis) {
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); }
    while(curDate-date < millis);
} 

//set an increased opacity and check if the image is done blending
function continueFadeImage(image, opacity, speed, pause, caption) {

    if(firstrun == 0) {
        firstrun = 1;
        opacity = 100;
        pause = 2000;
	speed = 1;
        } else {
        pause = gpause;
	speed = gspeed;
        }
    opacity = opacity + 3;

    if (opacity < 103) {

	setTimeout(function() {fadeImage(image, opacity, speed, pause, caption)}, speed);

    } else {
	//if the image is done, set it to the background and make it transparent
	image.parentNode.style.backgroundImage = "url("+image.src+")";

	// ASC: pause 1sec here to prevent MSIE image flash ...

	var paws=pause-1000;

	if (paws < 0 ) {
		paws = 0;
	}
	pausecomp(1000);

	setOpacity(image,0);
	//get the next image and start blending it again
	image = getNextImage(image);
	setTimeout(function() {startBlending(image, speed, pause, caption)}, paws);		
    }
}

//set the opacity to a new value and continue the fading
function fadeImage(image, opacity, speed, pause, caption) {
    setOpacity(image,opacity);
    continueFadeImage(image, opacity, speed, pause, caption);
}

function OpenEnquiry(mylink, windowname)
  {
  if (! window.focus) return true;
  var href;
  if (typeof(mylink) == 'string')
    href=mylink;
  else
    href=mylink.href;
    window.open(href,windowname,'width=550,height=450');
    return false;
  }

function targetopener(mylink, closeme, closeonly)
  {
  if (! (window.focus && window.opener))return true;
    window.opener.focus();
  if (! closeonly)window.opener.location.href=mylink.href;
  if (closeme)window.close();
  return false;
  }
