

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

    var yScroll;

    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
    } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
    }

    arrayPageScroll = new Array('',yScroll)
    return arrayPageScroll;
}



//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){

    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;
    if (self.innerHeight) {	// all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }


    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
    return arrayPageSize;
}

function getPageHeight() {
    return getPageSize()[1];
}

function jsLog(s) {
    return;
    var innerDiv = document.createElement('div');
    var txt = document.createTextNode(s + "\n");
    innerDiv.appendChild(txt);
    var logDiv = document.getElementById('logDiv');
    if (logDiv == null) {
        logDiv = document.createElement('div');
        logDiv.id = "logDiv";
        document.body.appendChild(logDiv);
    }
    while (logDiv.childNodes.length > 25) {
        logDiv.removeChild(logDiv.childNodes[0]);
    }
    document.getElementById('logDiv').appendChild(innerDiv);
}

function canLogToConsole() {
    //return true;
    try {
        var hasConsoleLog = console && console != null;
        if (hasConsoleLog) {
            hasConsoleLog = console['log'] && console['log'] != null;
        }
        return hasConsoleLog;
    } catch (err) {
        return false;
    }
}

function logToConsole(msg) {
    //if (true)
    return;
    try {
        if (canLogToConsole()) {
            console.log(msg);
        } else {
            jsLog(msg);
        }
    } catch (err) {
        jsLog("error2 for msg: " + msg + ", err=" + (err.description ? err.description : err));
    }
}

function logForFlash(flashId, msg) {
    //window.alert("logForFlash: " + msg);
    logToConsole("logForFlash: [" + flashId + "]: " + msg);
}

foxyFlashNum = 0;

foxyAliveFlashObjectIds = [];
foxyAliveFlashObjects = {};

//function checkFoxyAliveFlashObjects() {
//    var deadFlashObjs = [];
//
//    for (var i = 0; i < foxyAliveFlashObjectIds.length; i++) {
//        var flashId = foxyAliveFlashObjectIds[i];
//        var obj = foxyAliveFlashObjects[flashId];
//        var flash = (navigator.appName.indexOf("Microsoft") !=-1) ? window[flashId] : document[flashId];
//        if (flash == null) {
//            obj.idx = i;
//            deadFlashObjs.push(obj);
//            logToConsole("dead flash id=" + flashId);
//        } else {
//            logToConsole("not dead flash id=" + flashId + ", flash=" + flash);
//        }
//    }
//
//    alert("found dead flash objects: " + deadFlashObjs.length + ", " + deadFlashObjs);
//}

function foxyTagOrbTagSelected(flashId, tag, left, bottom) {
    if (foxyAliveFlashObjects[flashId] == null) {
        logToConsole("foxyTagOrbTagSelected: no alive flash object with id=" + flashId);
        return;
    }

    var f = foxyAliveFlashObjects[flashId].onTagSelected;
    if (f != null) {
        f(tag, left, bottom, left, bottom);
    }
}

function foxyTagOrbTagClicked(flashId, tag, left, bottom) {
    if (foxyAliveFlashObjects[flashId] == null) {
        logToConsole("foxyTagOrbTagClicked: no alive flash object with id=" + flashId);
        return;
    }

    var f = foxyAliveFlashObjects[flashId].onTagClicked;
    if (f != null) {
        f(tag, left, bottom, left, bottom);
    }
}

function foxyOrbSetTags(flashId) {
    //alert("set tags starts");
    if (foxyAliveFlashObjects[flashId] == null) {
        logToConsole("foxyOrbSetTags: no alive flash object with id=" + flashId);
        return;
    }

    if (foxyAliveFlashObjects[flashId].inited) {
        logToConsole("flash object already inited with id=" + flashId);
    //return;
    }

    foxyAliveFlashObjects[flashId].inited = true;
    var flash = (navigator.appName.indexOf("Microsoft") !=-1) ? window[flashId] : document[flashId];
    //window.alert("foxyTagOrbLoaded: flash=" + flash);
    if (!flash) {
        logToConsole("no flash with id=" + flashId);
        return;
    }
    var f = flash.setTags;
    if (!f) {
        logToConsole("no flash function setTags for flash id=" + flashId);
        return;
    }
    try {
        //alert("just before set tags");
        flash.setTags(foxyAliveFlashObjects[flashId].tags);
    } catch (err) {
        logToConsole("error setting tags for flash: " + err);
    }
}

function foxyTagOrbLoaded(flashId) {
    setTimeout(function () {foxyOrbSetTags(flashId);}, 1);
}

function setUpFoxyTagOrb(eleId, flashId, tags, size, onTagSelected, onTagClicked) {
    logToConsole("setUpFoxyTagOrb: eleId=" + eleId + ", flashId=" + flashId);
    flashId = flashId + foxyFlashNum++;
    logToConsole("setUpFoxyTagOrb: numbered flashId=" + flashId);

    var myTags = new Array(tags.length);

    for (var i = 0; i < myTags.length; i++) {
        var ele = tags[i];
        myTags[i] = ele.toString();
    }

    foxyAliveFlashObjectIds.push(flashId);
    foxyAliveFlashObjects[flashId] = {
        inited:false,
        "flashId": flashId,
        "onTagSelected": onTagSelected,
        "onTagClicked": onTagClicked,
        "tags": myTags
    };
    
    //checkFoxyAliveFlashObjects();

    //alert("before setUpFoxyTagOrb2Inner call");
    //setUpFoxyTagOrb2Inner("", eleId, flashId, tags, size, onTagSelected, onTagClicked);
    //alert("after setUpFoxyTagOrb2Inner call");
    //if (true) return;
    
    logToConsole("setUpFoxyTagOrb: tags=" + tags);

    //swfobject.removeSWF(eleId);
    
    //    var el = document.getElementById(eleId);
    //    if (el == null) {
    //        logToConsole("no element with id=" + eleId + ", nothing to setUp");
    //        return;
    //    } else {
    //        el.innerHTML = "";
    //        logToConsole("cleared element with id=" + eleId);
    //    }
    
    //    var isFlashInWindow = //false; //
    //    navigator.appName.indexOf("Microsoft") != -1;
    //    var flashEle = isFlashInWindow ? window[flashId] : document[flashId];
    //    logToConsole("looked for flash object with id=" + flashId + " in "
    //        + (isFlashInWindow ? "window" : "document") + ", found flash: " + (flashEle != null));
    //    if (flashEle != null) {
    //        if (isFlashInWindow) {
    //            window[flashId] = null;
    //        } else {
    //            document[flashId] = null;
    //        //var flashEle2 = document.getElementById(flashId);
    //        //if (flashEle2 != null) {
    //        //    logToConsole("remove flash child from doc");
    //        //    document.removeChild(flashEle2);
    //        // }
    //        }
    //    }
    //
    //    var flash = (navigator.appName.indexOf("Microsoft") !=-1) ? window[flashId] : document[flashId];
    //    if (flash != null) {
    //        logToConsole("there is still flash object with id: " + flashId);
    //    }

    //    var loaded = false;
    //
    //    foxyTagOrbLoaded = function () {
    //        if (loaded) {
    //            logToConsole("already loaded!");
    //            return;
    //        }
    //        loaded = true;
    //        //tags = ["ala", "ma", "kota"];
    //        //window.alert("foxyTagOrbLoaded: start, tags=" + tags);
    //        setTimeout(setTags, 1);
    //    //window.alert("foxyTagOrbLoaded: done");
    //    }

    //    if (onTagSelected != null) {
    //        foxyTagOrbTagSelected = onTagSelected; /*function (t, left, top, right, bottom) {
    //         logToConsole("foxyTagOrbTagSelected: t=" + t);
    //         onTagSelected(t, left, top, right, bottom);
    //    };*/
    //    } else {
    //        foxyTagOrbTagSelected = function (t, left, top, right, bottom) {
    //            logToConsole("foxyTagOrbTagSelected: t=" + t);
    //        };
    //    }
    //    if (onTagClicked != null) {
    //        foxyTagOrbTagClicked = onTagClicked;
    //    } else {
    //        foxyTagOrbTagClicked = function (t, left, top, right, bottom) {
    //            logToConsole("foxyTagOrbTagClicked: t=" + t);
    //        };
    //    }
    
    //    var tcolor = "#4a87bb";
    //    var so = new window.SWFObject("flash/FoxyTagOrb.swf", flashId, size, size, "6", tcolor);
    //    so.addParam("wmode", "transparent");
    //    so.addParam("allowScriptAccess", "always");
    //
    //    so.addVariable("jsLog", "logForFlash");
    //    so.addVariable("onLoaded", "foxyTagOrbLoaded");
    //    if (onTagSelected != null) {
    //        so.addVariable("onTagSelect", "foxyTagOrbTagSelected");
    //    }
    //    if (onTagClicked != null) {
    //        so.addVariable("onTagClick", "foxyTagOrbTagClicked");
    //    }
    //    so.write(eleId);
    var flashvars = {
        onLoaded: "foxyTagOrbLoaded", //"foxyTagOrbLoaded",
        jsLog: "logForFlash",
        onTagSelect: "foxyTagOrbTagSelected",
        onTagClick: "foxyTagOrbTagClicked",
        foxyFlashId: flashId
    };
    //    //onTagSelect: "selectTag", onTagClick: "clickTag",
    //    if (onTagSelected != null) {
    //        flashvars.onTagSelect = "foxyTagOrbTagSelected";
    //    }
    //    if (onTagClicked != null) {
    //        flashvars.onTagClick = "foxyTagOrbTagClicked";
    //    }
    var params = {
        menu: "false",
        scale: "noScale",
        wmode: "transparent",
        allowScriptAccess: "always"
    };

    var attributes = { 
        name: flashId,
        id: flashId
    };

    //alert("just before embed");

    swfobject.embedSWF("flash/FoxyTagOrbD.swf", eleId, size, size, "9.0.0",
        "flash/expressInstall.swf", flashvars, params, attributes);

    var noFlashInfoEle = document.getElementById("noFlashInfo");
    if (noFlashInfoEle != null) {
        logToConsole("setUpFoxyTagOrb: no flash player");
        noFlashInfoEle.style.display = "";
    }

    logToConsole("setUpFoxyTagOrb: done");
}

function activeFoxyPageChanged(pageName) {
    //alert("activeFoxyPageChanged: " + pageName);
    pageTracker._trackPageview(pageName);
}

function setNewFoxyAdsUrl(url) {
    //alert("new ads url=" + url);
    window.frames['foxyAdSenseAds'].location = url;
    //document.getElementById('foxyAdSenseAds').src = url;
    //alert("url is set to:" + window.frames['foxyAdSenseAds'].location);
}
