var pageVisual;

/**
 * Searches for page visual by looking for the first <img> tag inside element with id 'pagevisual'
 * @author Peter Kruithof
 */
function getPageVisual()
{
    var pageVisualDiv = document.getElementById('pagevisual');

    if ((typeof(pageVisualDiv) == 'object') && pageVisualDiv.hasChildNodes()) {

        // loop through childnodes, search for img tag
        var child;
        for (var i = 0; i < pageVisualDiv.childNodes.length; i++) {
            child = pageVisualDiv.childNodes[i];
            if (child.nodeName.toLowerCase() == 'img') {
                pageVisual = child;
                break;
            }
        }
    }
}

/**
 * Swaps pagevisual with given visual src
 * @author Peter Kruithof
 */
function swapPageVisual(src)
{
    if (typeof(pageVisual) == 'undefined') getPageVisual();

    if (typeof(pageVisual) == 'object') {
        pageVisual.setAttribute('src', src);
    }
}

function printWindow()
{
    window.print();
}

function replaceLargeImage(id, src){
    // replace image
    obj = document.getElementById(id);
    obj.setAttribute('src', src);
}

function openColorPallet(e)
{
    if (e) {
        var target = getTarget(e);
        popup(target, 830, 400, 'no', 'no', 'no', 'no', 'yes', 'yes');
        cancelEvent(e);
    }
}

function selectPalletColor(e, id)
{
    var td = getTarget(e);
    cancelEvent(e);

    if (td) {

        var trClassName = "name";

        // bubble up to td with class
        if (!td.className) {
            while (td) {
                td = td.parentNode;

                if ((td.nodeName.toLowerCase() == 'tr') && td.className) {
                    trClassName = td.className;
                }

                if ((td.nodeName.toLowerCase() == 'td') && td.className) break;
            }
        }

        if (td.className) {

            var textContent = '';

            // get all <td> elements to get contents
            var tds = td.getElementsByTagName('td');
            if (tds && (tds.length > 0)) {
                for (var i = 0; i < tds.length; i++) {

                    textContent += tds.item(i).innerHTML;
                    if (i != (tds.length - 1)) textContent += ': ';
                }
            }

            var example = document.getElementById(id + '-example');
            var className = td.className + ' ' + trClassName;

            example.className = className;
            example.innerHTML = textContent;

            document.location = '#' + id + '-example';
        }
    }
}

function initPalletColors(id)
{
    var pallet = document.getElementById(id);

    if (pallet) {
        var tds = pallet.getElementsByTagName('td');
        if (tds && (tds.length > 0)) {
            for (var i = 0; i < tds.length; i++) {
                applyEvent(tds.item(i), 'click', function(e) { selectPalletColor(e, id); });
            }
        }
    }
}

function googleAnalytics(site_title, url_title, url_href)
{
	// get dynamic url for google analytics
	var tag = 'uitgaande_links/'+site_title+'/'+url_title+'/'+url_href;
	try {
		pageTracker._trackPageview(tag);
	} catch(err) {}
	// open new window
	var win      = window.open(url_href);
    win.focus();
}

function getCookie(cookiename) {
    if (document.cookie.length > 0) {
        cookiestart = document.cookie.indexOf(cookiename + "=");
        if (cookiestart != -1) {
            cookiestart = cookiestart + cookiename.length+1;
            cookieend = document.cookie.indexOf(";", cookiestart);
            if (cookieend == -1) cookieend = document.cookie.length;
            return unescape(document.cookie.substring(cookiestart,cookieend));
        }
    }
    return "";
}



