﻿// CPR support functions for Google Analytics

function cprPrefixPath(thePart, thePrefix) {
   if (thePart && thePart != '' && thePart != thePrefix) // not empty, not just prefix
        return ((thePart.indexOf(thePrefix) == 0? '' : thePrefix) + thePart.toLowerCase());
    return ('');
}

function cprEscapeTitle(theTitle) {
    return theTitle.replace(/\r/g, '').replace(/\n/g, '').replace(/</g, '{').replace(/>/g, '}').replace(/http\:\/\//g, '').replace(/https\:\/\//g, '');
}

function cprTrackNC(theBBNCAnalyticsURL) { //track NetCommunity including Donations
    var theStep = theBBNCAnalyticsURL.match(/\=DonationStep_(\w+)\&/)
    if (theStep) {
        document.title += ' - ' + theStep[1]; // append page title
    }
    return theBBNCAnalyticsURL; // this is passed to pageTracker._trackPageview
}

function cprTrackN(theLink, eventbutton) { //track local file downloads
    var t = document.title + ' - ' + cprEscapeTitle(theLink.title);
    var thePath = cprPrefixPath(theLink.pathname, '/'); // /path/script.ext or empty
    thePath += cprPrefixPath(theLink.search, '?'); // ?arg1=val1&arg2=val2 or empty
    thePath += cprPrefixPath(theLink.hash, '#'); // #scrollto or empty
    eventbutton = (eventbutton >= 2 ? 2 : 1); // 0 or 1 for left-click, 3 or 2 for right-click
    pageTracker._trackEvent('DownloadLink', thePath, t, eventbutton);
    // Category, Action, Label, Value is 1 for left-click, 2 for right-click
    return true;
}

function cprTrackX(theLink, eventbutton) { //track external links
    var t = document.title + ' - ' + cprEscapeTitle(theLink.title);
    var thePath = '/' + theLink.protocol.replace(/\:/g, ''); // /http
    thePath += cprPrefixPath(theLink.hostname.replace(/\./g, '_'), '/'); // /www_whatever_com
    thePath += cprPrefixPath(theLink.pathname, '/'); // /path/script.ext or empty
    thePath += cprPrefixPath(theLink.search, '?'); // ?arg1=val1&arg2=val2 or empty
    thePath += cprPrefixPath(theLink.hash, '#'); // #scrollto or empty
    eventbutton = (eventbutton >= 2 ? 2 : 1); // 0 or 1 for left-click, 3 or 2 for right-click
    pageTracker._trackEvent('ExternalLink', thePath, t, eventbutton);
    // Category, Action, Label, Value is 1 for left-click, 2 for right-click
    return true;
}

function cprTrackR(theId) { //redirect after tracking
    var e = document.getElementById(theId)
    if (e && e.href && e.href != '')
        window.location.href = e.href;
}

//called from iframe for on-now info tracking
function cprTrackO(_Playing, thePath, t, millisec) {
    var theCat = 'Stream';
    if (_Playing == 0)
        theCat = 'OnNow';
    pageTracker._trackEvent(theCat, thePath, t, Math.round(millisec /1000));
    // Category, Action, Label, Value is seconds that info was visible
}

function cprGetCookieVal (offset) {
    var endstr = document.cookie.indexOf (';', offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

function cprGetCookie (name) {
    var arg = name + '=';
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) {
            arg = cprGetCookieVal (j);
            return arg;
        }
        i = document.cookie.indexOf(' ', i) + 1;
        if (i == 0) break;
    }
    return null;
}

function cprSetCookie (name, value, expDays, expHrs, expMins, domain) {
    var theCookie = name + '=' + escape(value) + '; path=/; domain=' + domain;

    if (expDays > 0 || expHrs > 0 || expMins > 0) {
        var exp = new Date();
        exp.setTime(exp.getTime() + (expDays * 24 * 60 * 60 * 1000) + (expHrs * 60 * 60 * 1000) + (expMins * 60 * 1000));
        theCookie += '; expires=' + exp.toGMTString();
    }
    //alert(theCookie);
    document.cookie = theCookie;
}

function cprSetLink(elem, name) {
    var theLink = cprGetCookie(name);
    if (theLink && elem.href != theLink) {
        //alert (elem.href + ' -> ' + theLink);
        elem.href = theLink;
    }
}

function cprSetLinks(elem, name) {
    var func = "cprSetLink(this, '" + name + "');"
    elem.setAttribute("onfocus", func); 
    elem.setAttribute("onclick", func); 
    elem.setAttribute("onmousedown", func); 
    elem.setAttribute("onmouseover", func); 
}

// Functions for slideshow - photo viewer

var interval = 4; // delay between rotating images (in seconds)
var random_display = 0; // 0 = no, 1 = yes
var timer; var bPlay = false;
interval *= 1000;
var image_index = 0;
var image_list = new Array();
var image_list_caption = new Array();
var image_list_title = new Array();
var image_list_resourceid = new Array();
var image_list_credit = new Array();
var start_index = -1;
var number_of_image;
var strImageResourceName; var strImageDescriptionName;  var strImageCreditName; var strImageTitleName;
var strImageCounter; var intParentID = 0; var intParentType = 0;

function new_window(url) {
	link = window.open(url,"CapRadioPopUp","toolbar=yes,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=655,height=780");   
	link.focus()
	return link;
}

function imageItem(image_location) 
{
    this.image_item = new Image();
    this.image_item.src = image_location;
}
function get_ImageItemLocation(imageObj) 
{
    if(imageObj != null && imageObj.image_item != null) 
    {
        return(imageObj.image_item.src)
    }
}
function getNextImage() 
{
    image_index = (image_index+1) % number_of_image;
    return(image_index);
}
function getPreviousImage() 
{
    image_index = (image_index-1) % number_of_image;
    if(image_index < 0) image_index = number_of_image-1;
        return(image_index);
}
function playpauseimage(sender)
{
    if(!bPlay) 
    {
        var recur_call = "rotateImage(true)";
        rotateImage(true);
        clearTimeout(timer);
        timer = setTimeout(recur_call, interval);
        sender.setAttribute("class","pause");
        sender.setAttribute("title","Pause");
        sender.setAttribute("alt","Pause");
        bPlay = true; 
    }
    else 
    {
        clearTimeout(timer);
        sender.setAttribute("class","play");
        sender.setAttribute("title","Play");
        sender.setAttribute("alt","Play");
        bPlay = false; 
    }
    return false;
}
function updateimage(num)
{
    image_index = num -1;
    nextimage('imgPlayPause');
}
function nextimage(imgPlayPauseID) 
{
    bPlay = false;
    clearTimeout(timer);
    rotateImage(true);
    var imgPlayPause = document.getElementById(imgPlayPauseID);
    imgPlayPause.setAttribute("class","play");
    imgPlayPause.setAttribute("title","Play");
    imgPlayPause.setAttribute("alt","Play");
    return false;
}
function previousimage(imgPlayPauseID) 
{
    bPlay = false;
    clearTimeout(timer);
    rotateImage(false);
    var imgPlayPause = document.getElementById(imgPlayPauseID);
    imgPlayPause.setAttribute("class","play");
    imgPlayPause.setAttribute("title","Play");
    imgPlayPause.setAttribute("alt","Play");
    return false;
}

function rotateImage(bDirection)
{    
    if (bDirection)
        getNextImage();
    else
        getPreviousImage();
        
    var new_image = get_ImageItemLocation(image_list[image_index]);
    
    var objImage = document.getElementById(strImageResourceName);
    var objImageDesc = document.getElementById(strImageDescriptionName);
    var objImageCnt = document.getElementById(strImageCounter);
    var objImageTitle = document.getElementById(strImageTitleName);
    var objImageCredit = document.getElementById(strImageCreditName);
    
    objImage.src = new_image;

    if (intParentID > 0 && intParentType > 0 && image_list_resourceid[image_index] != null) {
        objImage.onclick = null;
        objImage.onclick = function() 
            { new_window("/resources/photoviewer.aspx?parentid=" + intParentID + "&parenttype=" + intParentType + "&resourceid=" + image_list_resourceid[image_index]); return false; };
    }
    
    if(objImageTitle != null)
        objImageTitle.innerHTML = image_list_title[image_index];

    if(objImageCredit != null) //for drivestart.ascx only
        objImageCredit.innerHTML = image_list_credit[image_index];

    if(!bSwapTitleDesc)    //for drivestart.ascx, photoviewer.ascx
    {
        objImageDesc.innerHTML = image_list_caption[image_index];
        objImage.alt = image_list_title[image_index];
        objImage.title = image_list_title[image_index];
    }
    else        //for resources.ascx
    {
        objImageDesc.innerHTML = image_list_title[image_index];
        objImage.alt = image_list_caption[image_index];
        objImage.title = image_list_caption[image_index];
    }
    
    if(objImageCnt != null)
        objImageCnt.innerHTML = '(Showing ' + (image_index+1) + ' of ';
        
    if (bPlay && number_of_image > 1) {
        timer = setTimeout("rotateImage(true)", interval);
    }
}

function CallRotateImage(paramSwapTitleDesc, paramImageResourceName, paramImageTitleName, paramImageDescriptionName, paramImageCreditName, paramImageCounter, paramTotalImages, paramCarousel, paramParentID, paramParentType)
{
    number_of_image = image_list.length;
    image_index = start_index;

    strImageResourceName = paramImageResourceName;
    strImageTitleName = paramImageTitleName;
    strImageCreditName = paramImageCreditName;
    strImageDescriptionName = paramImageDescriptionName;
    strImageCounter = paramImageCounter;
    intParentID = paramParentID;
    intParentType = paramParentType;
    bSwapTitleDesc = paramSwapTitleDesc;

    if(document.getElementById(paramTotalImages) != null) // only for resources.ascx
        document.getElementById(paramTotalImages).innerText = ' ' + number_of_image.toString() + ')';

    if(number_of_image > 1)
    {
        bPlay = true;
        timer = setTimeout("rotateImage(true)", interval);
    } else if(document.getElementById(paramCarousel) != null)
        document.getElementById(paramCarousel).style.display = 'none';
}