﻿function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}
var infiniteLoopBreaker = 0;
var animationGifHtml = '<img src="images/update.gif" />'
var curSource='';
function addToBasket(anchor,productName) {
    if(curSource!='') {
        alert('update in process, please wait a few seconds');
        return;
    }
    curSource=productName;
    infiniteLoopBreaker = 0
    var coordinates = getAnchorPosition(anchor.id);
    var x = coordinates.x;
    var y = coordinates.y;
    var targetCoord = getAnchorPosition('commentsBox');
    var targX = targetCoord.x;
    var targY = targetCoord.y;
   // alert(targX);
   //alert(targY);
    var div =  document.getElementById('addToBasketDiv');
    div.innerHTML =  animationGifHtml +productName;
    adjustaddToBasketDiv(div,x,y);
   //alert(x);
   //alert(y);
    //alert('setting timeout');
	setTimeout('moveDiv()',1);
	//alert('set');
}
var incrementer = 40;
var refreshRate = 25;
function getDecrementAmount(x, targX) {
        if(x-targX>incrementer) {
	        return incrementer;
	    }
	    else {
	        var half = (x-targX) / 2;
	        if(half  <1 ) {
	            return 1;
	        }
	        else {
	            return half;
	        }
	    }
}
function getIncrementAmount(x, targX) {
        if(targX-x>incrementer) {
	        return incrementer;
	    }
	    else {
	        var half = (targX-x) / 2;
	        if(half  <1 ) {
	            return 1;
	        }
	        else {
	            return half;
	        }
	       
	    }
}
function moveDiv(productName) {
    //alert('move it');
    if(infiniteLoopBreaker>10000) {
        alert('infinite loop detected');
        return;
    }
    var coordinates = getAnchorPosition('addToBasketDiv');
    var x = coordinates.x;
    var y = coordinates.y;
    //alert(x);
    //alert(y);
    var targetCoord = getAnchorPosition('commentsBox');
    var div =  document.getElementById('addToBasketDiv');
    var targX = targetCoord.x;
    var targY = targetCoord.y;
    
	if(targX<x) {
	    x = x-getDecrementAmount(x,targX);
	}
	if(targX>x) {
	    x = x + getIncrementAmount(x,targX);
	}
	if(targY<y) {
	    y = y - getDecrementAmount(y,targY);
	}
	if(targY>y) {
	    y = y + getIncrementAmount(y,targY);
	}
	infiniteLoopBreaker++;
	 //alert(x);
    //alert(y);
	adjustaddToBasketDiv(div,x,y);
	if(targX!=x||targY!=y) {
        setTimeout('moveDiv()',refreshRate);
    }
    else {
         try {
            var ta =  document.getElementById('commentsBox');
            ta.value+='\r\n'+curSource;
            setCookie("basket",ta.value,1);
            document.forms[0].commentsBox.focus();
            if(document.forms[0].commentsBox.createTextRange) {
                var range = document.forms[0].commentsBox.createTextRange();
                range.move('character', document.forms[0].commentsBox.value.length);
                range.select();
            }
            else {
                if(document.forms[0].commentsBox.selectionStart) {
                    document.forms[0].commentsBox.focus();
                    document.forms[0].commentsBox.setSelectionRange(document.forms[0].commentsBox.value.length, document.forms[0].commentsBox.value.length);
                }
                else {
                    document.forms[0].commentsBox.focus();
                }
            }
         }
         catch(exception){alert(exception)}
         curSource='';
         div.style.visibility = "hidden";
        
    }
}

function adjustaddToBasketDiv(div,x,y) {
    div.style.left = x + "px";
	div.style.top = y + "px";
	div.style.visibility = "visible";
}
function getAnchorPosition(anchorname) {
    //alert(anchorname);
	// This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;
	// Browser capability sniffing
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }
	else if (document.all) { use_css=true; }
	else if (document.layers) { use_layers=true; }
	// Logic to find position
 	if (use_gebi && document.all) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_gebi) {
		var o=document.getElementById(anchorname);
		x=AnchorPosition_getPageOffsetLeft(o);
		y=AnchorPosition_getPageOffsetTop(o);
		}
 	else if (use_css) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==anchorname) { found=1; break; }
			}
		if (found==0) {
			coordinates.x=0; coordinates.y=0; return coordinates;
			}
		x=document.anchors[i].x;
		y=document.anchors[i].y;
		}
	else {
		coordinates.x=0; coordinates.y=0; return coordinates;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
}

// getAnchorWindowPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the window
function getAnchorWindowPosition(anchorname) {
	var coordinates=getAnchorPosition(anchorname);
	var x=0;
	var y=0;
	if (document.getElementById) {
		if (isNaN(window.screenX)) {
			x=coordinates.x-document.body.scrollLeft+window.screenLeft;
			y=coordinates.y-document.body.scrollTop+window.screenTop;
			}
		else {
			x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
			y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
			}
		}
	else if (document.all) {
		x=coordinates.x-document.body.scrollLeft+window.screenLeft;
		y=coordinates.y-document.body.scrollTop+window.screenTop;
		}
	else if (document.layers) {
		x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
		y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}

// Functions for IE to get position of an object
function AnchorPosition_getPageOffsetLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
	}
function AnchorPosition_getWindowOffsetLeft (el) {
	return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
	}	
function AnchorPosition_getPageOffsetTop (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
	}
function AnchorPosition_getWindowOffsetTop (el) {
	return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
	}
	
function PopupWindow_showPopup(anchorname) {
	this.getXYPosition(anchorname);
	this.x += this.offsetX;
	this.y += this.offsetY;
	if (!this.populated && (this.contents != "")) {
		this.populated = true;
		this.refresh();
		}
	if (this.divName != null) {
		// Show the DIV object
		if (this.use_gebi) {
			document.getElementById(this.divName).style.left = this.x + "px";
			document.getElementById(this.divName).style.top = this.y + "px";
			document.getElementById(this.divName).style.visibility = "visible";
			}
		else if (this.use_css) {
			document.all[this.divName].style.left = this.x;
			document.all[this.divName].style.top = this.y;
			document.all[this.divName].style.visibility = "visible";
			}
		else if (this.use_layers) {
			document.layers[this.divName].left = this.x;
			document.layers[this.divName].top = this.y;
			document.layers[this.divName].visibility = "visible";
			}
		}
	else {
		if (this.popupWindow == null || this.popupWindow.closed) {
			// If the popup window will go off-screen, move it so it doesn't
			if (this.x<0) { this.x=0; }
			if (this.y<0) { this.y=0; }
			if (screen && screen.availHeight) {
				if ((this.y + this.height) > screen.availHeight) {
					this.y = screen.availHeight - this.height;
					}
				}
			if (screen && screen.availWidth) {
				if ((this.x + this.width) > screen.availWidth) {
					this.x = screen.availWidth - this.width;
					}
				}
			var avoidAboutBlank = window.opera || ( document.layers && !navigator.mimeTypes['*'] ) || navigator.vendor == 'KDE' || ( document.childNodes && !document.all && !navigator.taintEnabled );
			this.popupWindow = window.open(avoidAboutBlank?"":"about:blank","window_"+anchorname,this.windowProperties+",width="+this.width+",height="+this.height+",screenX="+this.x+",left="+this.x+",screenY="+this.y+",top="+this.y+"");
			}
		this.refresh();
		}
	}
// Hide the popup
function PopupWindow_hidePopup() {
	if (this.divName != null) {
		if (this.use_gebi) {
			document.getElementById(this.divName).style.visibility = "hidden";
			}
		else if (this.use_css) {
			document.all[this.divName].style.visibility = "hidden";
			}
		else if (this.use_layers) {
			document.layers[this.divName].visibility = "hidden";
			}
		}
	else {
		if (this.popupWindow && !this.popupWindow.closed) {
			this.popupWindow.close();
			this.popupWindow = null;
			}
		}
	}
function setCookie(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1)
        {
        c_start=c_start + c_name.length+1;
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}
