//===========================================================================//
// INTERNET EXPLORER (ACTIVEX CONTROL) PLUG-IN PARAMETERS 
//===========================================================================//
//var cab_source = "http://www.2002graduates.com/ugur/on2/cab_deploy.CAB";
var cab_source = "http://www.pagefish.com/album/IeCab/cab_deploy.CAB";
var cab_version = "2,8,0,0";
var ie_class_id = "CLSID:F0268D8B-0F0A-4511-900F-ADA89B11FC80";
//===========================================================================//

//===========================================================================//
// NETSCAPE (MOZILLA-COMPATIBLE) PLUG-IN PARAMETERS       
//===========================================================================//
var xpi_source = "http://www.pagefish.com/album/FFXpi/nppagefishuploader.xpi";
var xpi_version = "2.8.0.0";
var np_mime_type = "application/x-pagefish-plugin";
var min_ff_version = "1.5.0.0";
//===========================================================================//

//===========================================================================//
// Script Globals
// Browser Detection Globals:
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
var isMac = (navigator.userAgent.indexOf("Mac") != -1) ? true : false;
var isFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox") != -1) ? true : false;
var firefox_needs_update = false;

// make sure firefox is >= 1.5:
if (isWin && isFirefox) {    
    var rv = dotted_version_string_to_float(min_ff_version);
    var temp = navigator.userAgent.split("Firefox/");
    var ver = dotted_version_string_to_float(temp[1]);
    if (ver < rv) firefox_needs_update = true;
}

// xpi object used during install in firefox
var xpi = new Object();

// var for holding publisher object
var pFish2;


//===========================================================================//
// function dotted_version_string_to_float()
// Convert a dotted version string to a floating point number:
// 1.1          => 101.0000
// 1.2.0.0      => 102.0000
// 1.2.0.1      => 102.0001
// 1.2.0.2      => 102.0002
// 1.2.0.3      => 102.0003
// 1.2.0.4      => 102.0004
// 1.2.0.5      => 102.0005
// 1.3.0.0      => 103.0000
// 2.0.0.0      => 200.0000
// 2.0.0.1      => 200.0001
// 2.0.0.2      => 200.0002
// 2.0.0.3      => 200.0003
// 2.0.0.4      => 200.0004
// 2.0.1.0      => 200.0100
// 2.0.1.1      => 200.0101
// 2.0.1.2      => 200.0102
function dotted_version_string_to_float(string)
{
    var vf = 0.0;

    try {
        var temp = string.split(".");
        var s_chief = temp[0];

        var s_major;

        try {
            s_major = temp[1];
        } catch (e) {
            s_major = "0";
        }

        var s_minor;

        try {
            s_minor = temp[2];
        } catch (e) {
            s_minor = "0";
        }

        var s_patch;
        try {
            s_patch = temp[3];
        } catch (e) {
            s_patch = "0";
        }

        var vf_str = s_chief
            + ((s_major > 10) ? s_major : "0" + s_major)
            + "."
            + ((s_minor > 10) ? s_minor : "0" + s_minor)
            + ((s_patch > 10) ? s_patch : "0" + s_patch);

        vf = parseFloat(vf_str);
    } catch (e) {};

    return vf;
}

//===========================================================================//
function go(text)
{    

    if (!isIE && isWin) {
        if (is_plugin_installed() == false) {
            return;
        }
    }

    assign_plugin();        
}

function assign_plugin()
{
    if (isIE && isWin && !isOpera)
        pFish2= document.pFishObject;
    else if (isWin && !isOpera) {
        pFish2= get_ns_plugin();
    }
}

function get_ns_plugin()
{
    

    return document.pFishObject.nsIPagefishPublisher;    
}

//===========================================================================//
// function check_for_netscape_plugin(reqVer)
//  determines if the pagefish uploader netscape plug-in version reqVer is
//  installed
//  triggers xpi install if plug-in is not present or out of date
//  - this function is used only by the firefox/netscape code
function check_for_netscape_plugin(reqVer)
{
    

    var result = true;
    var mime  = navigator.mimeTypes[np_mime_type];
    var force_update = false;
    var update = false;
    var installed_version = 0;

    if (np_mime_type == "application/x-YOURCOMPANYpublisher-plugin")
        return false;

    try {
        // check for our mime type
        if(mime) {
            var plugin = mime.enabledPlugin;
            var desc = plugin.description;
            var tempArrayMajor = desc.split("Pagefish Uploader v");
            var version = tempArrayMajor[1];
            installed_version = dotted_version_string_to_float(version);
        } else {
            // no mime type, no plug-in: force update:
            force_update = true;
        }
    } catch(e) {
        //alert("caught exception, force_update=true");
        // something went wrong, try updating anyway...
        force_update = true;
    }

    if (force_update == false) {
        // we might already have the correct version,
        // make sure:
        var req_ver_float = dotted_version_string_to_float(reqVer);

        if (installed_version < req_ver_float)
            update = true;
    }

    if(update == true || force_update == true) {
        xpi={'pageFish Uploader Plugin':xpi_source};
        
        // make the firefox explanation div visible
        
        var ffDiv = document.getElementById('divFirefoxExp');
        var divWait = document.getElementById('divWait');
        
        if(ffDiv && divWait)
        {
            ffDiv.style.display = 'block';
            divWait.style.display = 'none';            
        }
        
        InstallTrigger.install(xpi,doneFn);
        result = false;
    }

    return result;
}

function is_plugin_installed()
{

    var mime;

    try {
        mime  = navigator.mimeTypes[np_mime_type];
    } catch (e) {
        return false;
    }

    if(mime)
    {
      	return true;
    } else {
      	return false;
    }
}

function WritePlugin()
{
    

    if(isOpera || isMac || !isWin)
    {
        alert("Unfortunately you are using an unsupported operating system or browser. This feature is only available in Windows (IE or FF). ");
    }
    else if (isIE && isWin )
    {

        var tags = '<object id="pFishObject" '
            + 'codebase="'
            + cab_source 
            + '#version='
            + cab_version 
            + '" '
            + 'name="pFishObject" '
            + 'align="center" '
            + 'classid="'
            + ie_class_id
            + '" '
            + 'width=200 '
            + 'height=25 '
            + 'VIEWASTEXT> '
            + '<div>' + getTranslationString('uploadernotinstalled') + '</div>'            
            +'<\/object>';
        document.write(tags);          
    }
    else if (isWin)
    {
        if (!firefox_needs_update) {
            if (check_for_netscape_plugin(xpi_version) == true)
            {
                var tags = '<object id="pFishObject" '
                    + 'name="pFishObject" '
                    + 'align="center" '
                    + 'type="'
                    + np_mime_type
                    + '" '
                    + 'width=200 '
                    + 'height=25 '
                    + 'VIEWASTEXT> '
                    +'<\/object>';
                document.write(tags);
            } 
        } 
    }
}

function doneFn(name, result)
{
    if(result == 0)
        javascript:location.reload(true);
}



function selectFiles()
{	
    var aid = document.getElementById('aid').value;
    var rid = document.getElementById('rid').value;

	pFish2.setHttpVar('uploadURL', document.getElementById('uploadURL').value );
	pFish2.setHttpVar('aid', document.getElementById('aid').value );
	pFish2.setHttpVar('rid', document.getElementById('rid').value );
	
	pFish2.setGUIVar('LeftRotateButtonMsg', getTranslationString("rotateleft") );
	pFish2.setGUIVar('RightRotateButtonMsg', getTranslationString("rotateright") );
	pFish2.setGUIVar('OpenFileDlgMsg', getTranslationString("openfiledialogtitle") );	
	pFish2.setGUIVar('BackupMsg', getTranslationString("filerotatebackupmsg") );
	
	pFish2.setGUIVar('NoFilesSelectedMsg', getTranslationString("nofilesselected") );	
	pFish2.setGUIVar('ProcessingMsg', getTranslationString("processingimages") );
	pFish2.setGUIVar('UploadingMsg', getTranslationString("uploadingimages") );
	pFish2.setGUIVar('FileTypeMsg', getTranslationString("imagefiles") );	
	
	pFish2.selectFiles();
}



function updateProgress()
{        
	document.getElementById('progress').innerHTML = pFish2.progressMessage;
}

function objectInitialized()
{
    ge('divWait').style.display = 'none';
	setTimeout("afterOneSelectFiles()", 50);
}

// somehow in object initialized method pFish2 variable is not set
// so we wait 50 miliseconds and call select files here
function afterOneSelectFiles()
{
    //window.alert('afterOneSelectFiles');
    selectFiles();
}


/*
function uploadComplete()
{
    alert("bitti!");
}
*/