
// function used in search higlightning
function findAndReplace(searchText, replacement, searchNode) {

	if (!searchText || typeof replacement === 'undefined') {
	    return;
	}
	
	searchWords = searchText.split(" ");
	var i = 0;
	for(i = 0; i < searchWords.length; i++){
		highlightWord(searchWords[i],replacement,searchNode );
	}
}


function highlightWord(searchText,replacement ,searchNode){
	
		var regex = typeof searchText === 'string' ? new RegExp('(' + searchText+')', 'gi') : searchText,
		    childNodes = (searchNode || document.body).childNodes,
		    cnLength = childNodes.length,
		    excludes = 'html,head,style,title,meta,script,object,iframe';
		    
		while (cnLength--) {
		    var currentNode = childNodes[cnLength];
		    if (currentNode.nodeType === 1 &&
		        (excludes + ',').indexOf(currentNode.nodeName.toLowerCase() + ',') === -1) {
		        arguments.callee(searchText, replacement, currentNode);
		    }
		    if (currentNode.nodeType !== 3 || !regex.test(currentNode.data) ) {
		        continue;
		    }
		    var parent = currentNode.parentNode,
		        frag = (function(){
		            var html = currentNode.data.replace(regex, "<span style=\"background-color: yellow;\">$1</span>"),
		                wrap = document.createElement('div'),
		                frag = document.createDocumentFragment();
		            wrap.innerHTML = html;
		            while (wrap.firstChild) {
		                frag.appendChild(wrap.firstChild);
		            }
		            return frag;
		        })();
		    parent.insertBefore(frag, currentNode);
		    parent.removeChild(currentNode);
		}
	}



//the following to functions lets javascript communicate with the spotviewer component [AndSan]
function getContainer(swfName) {
  if (navigator.appName.indexOf("Microsoft") != -1) {
    return window[swfName];
  } else {
    return document[swfName];
  }
}
var spotloaderCallInterval;
var intCounter = 0;


function initSpotViewer() {
       spotloaderCallInterval = setInterval("callAS()",500);
}


function callAS() {
      intCounter++;
     if( (getContainer('sotester') && getContainer('sotester').initSpots) || intCounter > 10) {
      if( (getContainer('sotester') && getContainer('sotester').initSpots) ) { getContainer('sotester').initSpots(); }
      clearInterval(spotloaderCallInterval);
     }
      
}


//function to show tooltips [AndSan]
function showToolTip(e){
              var parentFound = false;
              
              if(document.attachEvent){  //ie
                var evTarget = e.srcElement;
                //climb upwards in node tree to find the a tag
                while(!parentFound) {
                  if(evTarget.tagName == 'A') {
                    parentFound = true;
                    var note = evTarget.getElementsByTagName('div')[0];
                  } else {
                    evTarget = evTarget.parentNode; 
                  }
                }
                //check if event target is inner element of tooltip link or link itself. If former, reference the parent element
                //var note = e.srcElement.src ? e.srcElement.parentNode.getElementsByTagName('div')[0] : e.srcElement.getElementsByTagName('div')[0];
                
              } else {//mozilla
                var evTarget = e.target;
                //climb upwards in node tree to find the a tag
                while(!parentFound) {
                  if(evTarget.tagName == 'A') {
                    parentFound = true;
                    var note = evTarget.getElementsByTagName('div')[0];
                  } else {
                    evTarget = evTarget.parentNode; 
                  }
                }
              }
                
                note.style.display = 'block';
                note.style.zIndex='3000';

                //calculate page measurements and pointer position (ie : mozilla)
                var winWidth = note.attachEvent ? document.body.offsetWidth : (window.innerWidth + window.pageXOffset);
                var winHeight = note.attachEvent ? (document.documentElement.clientHeight + document.documentElement.scrollTop) : (window.innerHeight + window.pageYOffset);
                var pointerPosX = note.attachEvent ? (e.clientX + document.documentElement.scrollLeft - document.documentElement.clientLeft) : e.pageX;
                var pointerPosY = note.attachEvent ? (e.clientY + document.documentElement.scrollTop - document.documentElement.clientTop) : e.pageY;
                
                //get tooltip width and height
                var tooltipWidth = note.offsetWidth;
                var tooltipHeight = note.offsetHeight;
                          
                //place tooltip div in relation to pointer based on links placement on side, ie. move tooltip up or to sides if viewing space is too small
                if((winHeight - pointerPosY) < tooltipHeight)
                  var heightOffset = tooltipHeight - (winHeight - pointerPosY);
                else
                  var heightOffset = -20;
                            
                if((winWidth - pointerPosX) < tooltipWidth)
                  var widthOffset = (tooltipWidth - (winWidth - pointerPosX)) +30;
                else
                  var widthOffset = -20;
                            
                note.style.left= pointerPosX - widthOffset +'px';
                note.style.top= pointerPosY - heightOffset +'px';
}


function hideToolTip(e){
              var parentFound = false;
              
              if(document.attachEvent) {  //ie
                var evTarget = e.srcElement;
                //climb upwards in node tree to find the a tag
                while(!parentFound) {
                  if(evTarget.tagName == 'A') {
                    parentFound = true;
                    var note = evTarget.getElementsByTagName('div')[0];
                  } else {
                    evTarget = evTarget.parentNode; 
                  }
                }
              } else { //mozilla
                var evTarget = e.target;
                 
                //climb upwards in node tree to find the a tag
                while(!parentFound) {
                  if(evTarget.tagName == 'A') {
                    parentFound = true;
                    var note = evTarget.getElementsByTagName('div')[0];
                  } else {
                    evTarget = evTarget.parentNode; 
                  }
                }
              }

                note.style.display = 'none';
}
            
function createTooltipListeners(obj) {
            //create additional listeners (move, out) on mouseover event for tooltip links
            if(obj.attachEvent) {
              obj.attachEvent('onmousemove',showToolTip);
              obj.attachEvent('onmouseout',hideToolTip);
            } else {
              obj.addEventListener('mousemove',showToolTip,false);
              obj.addEventListener('mouseout',hideToolTip,false);
            }
           
}


/* ### Function for ajax loading of screenshot images, used by img-gallery.xsl ### */
function screenShotLoader(path){
	new Ajax.Request('/media/screenshots/ajaxparts.html?__toolbar=0&sel-image=' + path, 
		{method: 'get', 
			onSuccess: function(transport){
				img = $('img-gall-selected-image');	//img-gall-selected-image-img
				ttext = $('img-gall-selected-titleText');
				prevnext = $('img-gallery-prev-next');
				Element.remove(ttext);
				new Effect.Shrink('img-gall-selected-image-img', {duration: 0.5,afterFinish:
					function dodisplay(){
						// only img left, replace it with new img, text + next/prev data
						Element.replace(img, transport.responseText);
						img.style.display = 'none'; // needed for appear effect
						Effect.multiple([img, ttext], Effect.Appear);
					}
				})
			}
		}
	)
}


function changeScreenGallery(path){
	new Effect.Fade('img-gall', {to:0.01, duration:0.3
		, afterFinish: 
			function loadNewGallery(){
				new Ajax.Updater('img-gall', '/media/screenshots/ajaxparts.html?showFolder=' + path, {
					onComplete:
						function bahab(){
							Effect.Appear('img-gall', {duration:0.3});
						}
					}
				)
			}
		}
	)
}


function overlay(path){
	el = $("overlay");
	el_img = $("overlay-img");
	//alert(el_img.style.visibility);
	if (el.style.visibility != 'visible'){
		el.style.visibility = 'visible';
		el_img.style.visibility = 'visible';
		//	load this image with ajax to use roxens img transformation
		if (path){
			new Ajax.Updater(el_img, '/media/screenshots/ajaxparts.html?loadFullSizeImg=' + path, {method:'get', onComplete:
				function displayfullsize(){
					Effect.multiple([el, el_img], Effect.Appear);
				}
			})
		}
	}
	else{
		Effect.multiple([el, el_img], Effect.Fade, {afterFinish: 
				function(){
					el.style.visibility = 'hidden'; 
					el_img.style.visibility = 'hidden';
				}
			}
		);
	}
	//el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
	//el_img.style.visibility = (el_img.style.visibility == "visible") ? "hidden" : "visible";
}


function flvPlayer(file,vtitle,w,h) {
    Modalbox.show('/swf/flv-player.xml?vidObj='+file, {title: vtitle,
        overlayOpacity:.9, 
        width: w, 
        height: h, 
        xafterHide: redirectBack }); // afterhide disabled with an x
}


function alterTextField(id){
    // used for /tip.xsl
    field = document.getElementById(id);
    field.value = '';
    field.style.color = '#333;';
}


function tip_friend(popup_url, path) {
    Modalbox.show('/tip.xml?__noframe=1&pagePath='+path, {title: '&nbsp;',
        overlayOpacity:.20, 
        width: 380, 
        height: 420 });
}


// makes a blinddown, also changes click behaviour so that open boxes fold again
		function toggle_pagepart_blind(url){
			thisbox = $(url + '-content');
			if (thisbox.style.display == 'none'){
				Effect.BlindDown(url + '-content', { duration: 0.3, afterFinish:$(thisbox).style.height='auto' })
			}
			else{
				Effect.BlindUp(url + '-content', { duration: 0.3 })
			}
		}

