var millisec=700;
var catch_count=0;
var vsa_count=0;
var winclosed=0;


// The next functions are used to check that cookies are accepted, place cookies for timed pop-over/pop-under event

function set_session_cookie(message){
	if (message!='off'){
	var monicor="DontLeaveUs";
	var result=get_cookie(monicor);
	if (result==null){
		document.cookie=monicor + "=" + message;
	}
	else{
		if (result=="goodbye" && message!="goodbye"){
	    CatchTheProspect();
		document.cookie="DontLeaveUs=hello";
		}
		else if (result!=message){
			document.cookie=monicor + "=" + message;
		}
	}
    }
}

//Get cookie routine by Shelley Powers
function get_cookie(Name) {
  var search = Name + "=";
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search);
    // if cookie exists
    if (offset != -1) {
      offset += search.length;
      // set index of beginning of value
      end = document.cookie.indexOf(";", offset);
      // set index of end of cookie value
      if (end == -1) end = document.cookie.length;
      returnvalue=unescape(document.cookie.substring(offset, end))
      }
   }
  return returnvalue;
}

var clarity_timeout;
function timedVisibility(delay){
	var theDelay = delay *1000;
	clarity_timeout = setTimeout('VirtualShopAssistant();', theDelay);
}

function Click2Call(){
  clearTimeout(clarity_timeout);
  setVisibility('callme','visible');
  opacity('callme',0,99, millisec);
}

function Click2CallClose(){
 var fram = document.getElementById('c2c_frame');
 var id = document.getElementById(id);
 clearTimeout(clarity_timeout);
 changeOpac(0, 'callme')
 setVisibility('callme','hidden');
 fram.src="click2call/callme.html?id="+id+"&product=";
  }

function CatchTheProspect(){
    clearTimeout(clarity_timeout);
    setVisibility('catchtheprospect','visible');
    opacity('catchtheprospect',0,99, millisec);
}

function CatchTheProspectClose(){
  winclosed=1;
  var fram = document.getElementById('ctp_frame');
  var id = document.getElementById(id);
  clearTimeout(clarity_timeout);
  opacity('catchtheprospect',99,0, millisec);
  setVisibility('catchtheprospect','hidden');
  fram.src="click2call/catchtheprospect.html?id="+id+"&product=";
}

function VirtualShopAssistant(){
    clearTimeout(clarity_timeout);
    setVisibility('virtualshopassistant','hidden');
    opacity('virtualshopassistant',0,99, millisec);
}

function VirtualShopAssistantClose(){
  var fram = document.getElementById('vsa_frame');
  var id = document.getElementById(id);
  clearTimeout(clarity_timeout);
  opacity('virtualshopassistant', millisec);
  setVisibility('virtualshopassistant','hidden');
  fram.src="click2call/virtualshopassistant.html?id="+id+"&product=";
}


var clarity_timeout2;

function timedVisibility2(){
	var theDelay2 = 1;
	clarity_timeout2 = setTimeout('CatchTheProspect();', theDelay2);
}

function halt_layer(){
	clearTimeout(clarity_timeout2);
}

function setVisibility(objectID,state) {
	var obj = document.getElementById(objectID);
	obj.style.visibility = state;
}

function toggleVisibility(objectID) {
	obj = document.getElementById(objectID);
	state = obj.style.visibility;
	if (state == 'hidden')
		obj.style.visibility = 'visible';
	else {
		if (state == 'visible')
			obj.style.visibility = 'hidden';
		else obj.style.visibility = 'visible';
	}
}
// sets the layer off screen for Netscape browsers which don't fully support invisable layers  19/9/06
function movelayer(objectID,x,y){
object = document.getElementById(objectID);
		object.style.left=x+'px';
		object.style.top=x+'px';

}

// animating the pop-up layer
var animateSpeed = null;
var fX = null;
var fY = null;
var cX = null;
var cY = null;
var dX = null;
var dY = null;
var stepX = null;
var stepX = null;
var stepY = null;

function shiftOpacity(id, millisec) {
    //if an element is invisible, make it visible, else make it ivisible
    if(document.getElementById(id).style.opacity == 0) {
        opacity(id, 0, 100, millisec);
    } else {
        opacity(id, 100, 0, millisec);
    }
}
function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //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++;
        }
         toggleVisibility(id);
    } else if(opacStart < opacEnd) {
        document.getElementById(id).style.visibility= 'visible';
        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 + ")";
}

function initAnimate(objectID,x,y,animate){

	object = document.getElementById(objectID);
	fX = x;
	fY = y;
	animateSpeed = animate;

	cX = object.offsetLeft;
	cY = object.offsetTop;
	dX = Math.abs(fX-cX);
	dY = Math.abs(fY-cY);
	if((dX==0)||(dY==0)){slope=0;}
	else {slope=dY/dX;
		if(dX>=dY){
			if(cX<fX){stepX=animateSpeed;}
			else if(cX>fX){stepX=-animateSpeed;}
			if(cY<fY){stepY=animateSpeed*slope;}
			else if(cY>fY){stepY=-animateSpeed*slope;}
		}
		else if(dX<dY){
			if(cY<fY){stepY=animateSpeed;}
			else if(cY>fY){stepY=-animateSpeed;}
			if(cX<fX){stepX=animateSpeed/slope;}
			else if(cX>fX){stepX=-animateSpeed/slope;}
		}
	}
	animateObject();
}
function animateObject(){
	if((dX>0)||(dY>0)){
		object.style.left=Math.round(cX)+'px';
		object.style.top=Math.round(cY)+'px';
		cX=cX+stepX;
		cY=cY+stepY;
		dX=dX-Math.abs(stepX);
		dY=dY-Math.abs(stepY);
		setTimeout('animateObject()',0);
	}
	else{
		object.style.left=fX+'px';
		object.style.top=fY+'px';
	}
	return;
}