//function to load when page is done loading
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function () {
            oldonload();
            func();
        }
    }
}

//vars for google tracking
var movieID = [];
var movieFrame = [];
var movieCat = [];
var movieLab = [];
var tempCounter = 0;

//tracking codes
function getTracker(var1, var2, var3, var4) {
    if (document.location.href.indexOf("hieu") >= 0) { alert(var1 + " -- " + var2 + " -- " + var3 + " -- " + var4); }
    var4 = parseInt(var4);
    if (window._gaq != undefined)
        _gaq.push(['_trackEvent', var1, var2, var3, var4]);

}

//counts the seconds that have elapse in movie
function framecount(fcat, flab, fid, fnum) {
    var foundID = false;
    if (movieID.length == 0) {
        //if movie array is empty create first record
        tempNum = movieID.length;
        movieID[tempNum] = fid;
        movieFrame[tempNum] = fnum;
        movieCat[tempNum] = fcat;
        movieLab[tempNum] = flab;
    } else {
        //search to see if record exist
        for (i = 0; i < movieID.length; i++) {
            if (fid == movieID[i]) {
                //if exist set frame number to array location
                movieFrame[i] = fnum;
                foundID = true;
            }
        }
        if (foundID == false) {
            //if doens't exist then create a new record at the bottom of array
            tempNum = movieID.length;
            movieID[tempNum] = fid;
            movieFrame[tempNum] = fnum;
            movieCat[tempNum] = fcat;
            movieLab[tempNum] = flab;
        }
    }
}

//when page closes, all event tracking data is sent
function endAction() {
    if (movieID.length != 0) {
        var tempStr = "Result:\n";
        var tempText;
        //loops through array of all objects being tracked
        for (i = 0; i < movieID.length; i++) {
            tempText = document.location.href.toLowerCase();
            tempText = tempText.replace(/http:\/\/westerngeco/, "http://www.westerngeco");
            tempText = tempText.replace(/http:\/\/slb/, "http://www.slb");
            tempText = tempText.replace(/https/, "http");
            tempText = tempText.replace(/http:\/\/public.slb/, "http://www.slb");

            getTracker(movieCat[i], tempText, movieID[i], parseInt(movieFrame[i]));
            tempStr += movieCat[i] + " - " + movieLab[i] + " - " + movieID[i] + " - " + parseInt(movieFrame[i]) + "\n";
        }
        //alert("woo hoo");
    }
}

//finds all media files on page and track them w/ google
function setAnchors() {
    var anchorx = document.getElementsByTagName('a');
    for (var i = 0; i < anchorx.length; i++) {
        if (anchorx[i].href.toLowerCase().indexOf("media/files") >= 0 || anchorx[i].href.toLowerCase().indexOf("media/premiumcontent") >= 0) {
            anchorx[i].onclick = function () { javascript: getTracker('Download', document.location.href.toLowerCase(), this.href,'0'); };
            //anchorx[i].onclick = function(){javascript:alert(this.href)};
        }
    }
}
addLoadEvent(setAnchors);


//function to find all tables for alternating rows
function fgetAllDataTables() {
    if (!document.getElementsByTagName) return false;

    var eleTables = document.getElementsByTagName("table");

    for (var i = 0; i < eleTables.length; i++) {
        if (eleTables[i].className == "slb_datatable") {
            fStripes(eleTables[i]);
        }
    }
}

//function to set a class for everyother row
function fStripes(eleTable) {
    var eleTableRows = eleTable.getElementsByTagName("tr");

    for (var i = 1; i < eleTableRows.length; i++) {
        eleTableRows[i].className = "slb_trgrey";
        i++;
    }
}
addLoadEvent(fgetAllDataTables);

//execute when page closes	
window.onbeforeunload = endAction;
