﻿//
// PixEdit® ExportToWEB™ script file
// Copyright © 2006 Techsoft a.s.
//
// PixEdit® is a registered trademark of Techsoft a.s.
// US Pat.no: 2,194,188 - EU Reg.no: 000468439
//
// The following JavaScript code is the property of Techsoft a.s. It is a part
// of the PixEdit® product and protected by the PixEdit® trademark/patent.
// The use of this code is subject to the license terms stated in the PixEdit®
// installation package and user manual. You may distribute this script to your
// own WEB server(s) to allow searching in documents published through the 
// ExportToWEB™ function in PixEdit®. Any other distribution and any 
// modification of the code is strictly prohibited and will be considered a 
// violation of the PixEdit® license terms.
//

//
// If using frames, document is in an other frame
//
function getDocument()
{
	if (parent.thedocument!=null)
        return parent.thedocument.document;
		
	return document;
}

function isUsingFrames()
{
	return parent.thedocument!=null;
}

//
// Add a text message to the 'Search Results' DIV
//
function addSearchMessage(text, bItalic)
{
    var divSearchResults = document.getElementById('divSearchResults');
    if (divSearchResults==null)
        return;

    var pMessage = document.createElement('p');
    
    if (bItalic)
    {
        var emItalic = document.createElement('em');
        pMessage.appendChild(emItalic);
        emItalic.innerText = text;
    }
    else
        pMessage.innerText = text;

    divSearchResults.appendChild(pMessage);
}

function clearSearchResults()
{
    var divSearchResults = document.getElementById('divSearchResults');
    if (divSearchResults==null)
        return;
        
    while (divSearchResults.childNodes[0])
        divSearchResults.removeChild(divSearchResults.childNodes[0]);
}

//
// Type of DHTML support on this system
//
var bInnerHtml, 
	bIE, 
	bAllowFastGraphics,
	bDOM, 
	bMozilla, 
	bNetscape4;

function checkDHTMLSupport()
{
	var theDoc = getDocument();
	if (theDoc==null)
		return;
	
	bIE = typeof theDoc.body.insertAdjacentHTML != 'undefined';
	bDOM = (!bIE &&
		typeof theDoc.body.appendChild != 'undefined' &&
		typeof theDoc.createRange != 'undefined' &&
		typeof (i = theDoc.createRange()).setStartBefore != 'undefined' &&
		typeof i.createContextualFragment != 'undefined');
	bInnerHtml = !bIE && !bDOM &&  typeof theDoc.body.innerHTML != 'undefined';
	bAllowFastGraphics = bIE && theDoc.all && !window.opera;
	bMozilla = bDOM && typeof theDoc.body.style.MozOpacity != 'undefined';
	bNetscape4 = theDoc.layers && typeof theDoc.classes != 'undefined';
}

//
// A class that supports some simple drawing on top of the HTML. Technique is 
// using filled DIV tags in a separate layer. Based on jsGraphics by Walter Zorn.
// (www.walterzorn.com)
//
function graphicsOverlay(id)
{
	var theDoc = getDocument();
	if (theDoc==null)
		return;
		
	this.htm = '';
	
	this.setColor = function(clr)
	{
		this.color = clr.toLowerCase();
	};
	
	this.cnv = theDoc.getElementById(id);
	this.defhtm = (this.cnv && this.cnv.innerHTML) ? this.cnv.innerHTML : '';
	
	if (!(bIE || bDOM || bInnerHtml))
		checkDHTMLSupport();
		
	if (bDOM)
	{
		this.paint = function()
		{
			var theDoc = getDocument();
			var x = theDoc.createRange();
			x.setStartBefore(this.cnv);
			x = x.createContextualFragment(bAllowFastGraphics ? this.htmRpc() : this.htm);
			if (this.cnv) 
				this.cnv.appendChild(x);
			this.htm = '';
		};
	}
	else if (bIE)
	{
		this.paint = function()
		{
			if (this.cnv!=null)
				this.cnv.insertAdjacentHTML('BeforeEnd', this.htm);
			this.htm = '';
		};
	}
	else if (bInnerHtml)
	{
		this.paint = function()
		{
			if (this.cnv)
				this.cnv.innerHTML += this.htm;
			this.htm = '';
		};
	}
	else
	{
		this.paint = function()
		{
			this.htm = '';
		};
	}
		

	this.drawRect = function(x, y, w, h)
	{
		this.drawLine(x, y, x+w, y);
		this.drawLine(x+w, y, x+w, y+h);
		this.drawLine(x, y+h, x+w, y+h);
		this.drawLine(x, y, x, y+h);
	}

	this.fillRect = function(x, y, w, h, alpha)
	{
		this.htm += 
			'<div style="position:absolute;' +
			'left:' + x + 'px;' +
			'top:' + y + 'px;' +
			'width:' + w + 'px;' +
			'height:' + h + 'px;' +
			'clip:rect(0,' + w + 'px,' + h + 'px,0);' +
			'background-color:' + this.color + ';' +
			'filter: alpha (opacity=' + alpha + ');' +
			(!bMozilla ? 'overflow:hidden;' : '') +
			'"><\/div>';
	};
	
	this.frameRect = function(x, y, w, h, frameWidth)
	{
		this.htm += 
			'<div style="position:absolute;' +
			'left:' + x + 'px;' +
			'top:' + y + 'px;' +
			'width:' + w + 'px;' +
			'height:' + h + 'px;' +
			'clip:rect(0,' + w + 'px,' + h + 'px,0);' +
			'border-color: ' + this.color + ';' +
			'border-style: solid;' +
			'border-width: ' + frameWidth + ';' +
			(!bMozilla ? 'overflow:hidden;' : '') +
			'"><\/div>';
	};
	
	
	this.clear = function()
	{
		this.htm = '';
		if (this.cnv) 
			this.cnv.innerHTML = this.defhtm;
	};
}

//
// Global var that holds the drawing canvas for the DIV containing page IMG's
//
var grPages;

//
// Setup a drawing canvas for the DIV containing page IMG's
//
function initPageGraphics()
{
	grPages = new graphicsOverlay('divPages');
}

//
// Erase any drawn objects on the drawing canvas
//
function clearPageGraphics()
{
	if (grPages!=null)
		grPages.clear();
}

//
// Draw a rectangle on the specified page
//
function drawRectOnPage(pageName, X, Y, width, height, color)
{
	var theDoc = getDocument();
	var img = theDoc.images.item(pageName);
	if (img!=null)
	{
		grPages.setColor(color);
		grPages.fillRect(img.offsetLeft+X, img.offsetTop+Y, width, height, 50);
//		grPages.frameRect(img.offsetLeft+X, img.offsetTop+Y, width, height,1);
		grPages.paint();
	}
}

function onClickSearchResult()
{
	var pageName = event.srcElement.name;
	drawRectOnPage(pageName, 400, 400, 100, 20, '#ff8800');
}

//
// Search the ALT attributes of IMG's for the given text
//
function doSearch()
{
    var divSearchResults = document.getElementById('divSearchResults');
    if (divSearchResults==null)
        return;
        
    // remove any previous search results and messages first
    clearSearchResults();
        
    // get search text from form
	var txtboxSearch = document.getElementById('txtText');
	if (txtboxSearch==null)
	    return;
	    
	var strSearch = txtboxSearch.value;
	
	if (strSearch.search(/\S/)<0)
    {
        // no reason to search for blank text
        addSearchMessage('Please enter a text to search for', true);
        return;
    }
    
	// search in the document 
	var theDoc = getDocument();
    
    // walk through the images
    var bFound = false;
    var nMaxHitCount=1000;
    var nHitCount=0;
    for (i=0; i<theDoc.images.length; i++)
    {
        // get the alternative representation text (ALT attribute)
        var strAltTxt = theDoc.images.item(i).alt;
        
        // do a case insensitive search
        var reSearch = new RegExp(strSearch, 'i');
        var strTmp = strAltTxt;
        var nFound = strTmp.search(reSearch);
        
        while (nFound>=0 && nHitCount<nMaxHitCount)
        {
            // found it!
            bFound = true;

            // create a link to this page under 'Search Results'
            var aLink = document.createElement('a');

            if (isUsingFrames())
            {            
                aLink.href = theDoc.title+'_doc.htm#'+theDoc.images.item(i).title;
                aLink.target = 'thedocument';
            }
            else
            {            
                aLink.href = '#'+theDoc.images.item(i).title;
                aLink.target = '_self';
            }
            
            aLink.title = theDoc.images.item(i).title;
            
            var strQuoute = strTmp.substr(nFound, 20);
            
            // newlines will just mess up the result list
            var nNewLine = strQuoute.search(/[\r\n]/);
            if (nNewLine>0)
                strQuoute = strQuoute.substr(0, nNewLine);

            var tnLink = document.createTextNode(theDoc.images.item(i).title + ': "' + strQuoute + '..."');
            var pSearchResult = document.createElement('p');
            pSearchResult.appendChild(tnLink);
            aLink.appendChild(pSearchResult);
            divSearchResults.appendChild(aLink);
            strTmp = strTmp.substring(nFound+strSearch.length, strTmp.length-1);
            nFound = strTmp.search(reSearch);
            
            nHitCount++;
            
            if (nHitCount>=nMaxHitCount)
                addSearchMessage('Sorry, cannot show more than ' + nMaxHitCount + ' hits...', true);
        }
        
        if (nHitCount>=nMaxHitCount)
            break;
    }
    
    if (!bFound)
    {
        // no occurrences of the specfied text was found
        addSearchMessage('The search text was not found', true);
    }
}

