/*************

Install Helper

@require cookie.js
@require statlogger.js
@require prototype.js, livepipeui, scriptaculous, cookie


**************/

var InstallHelper = {

    build: false,

    browser: false,
    // ff2 | ff3 | sa | ie
    firstWindowHeight: window.innerHeight,

    windowHeight: 0,

    log: function(event) {
        StatLoggerNew.logEvent('newih_' + event);
    },
    
    onload: function() {
      
        InstallHelper.build = InstallerBuild;

        if (InstallHelper.build.browser == 'ie') {
            InstallHelper.ie_onload();
        } else if (InstallHelper.build.browser == 'ch') {
            InstallHelper.ch_onload();
        }
    },

    clickedDownload: function(pagetype) {
        //$('debug').innerHTML += "Clicked DL <br />";
        //check to see if download is from cooliris
        Cookie.set('cool_dl', "1", .004);
        if(pagetype == 1) Cookie.set('campaign_page', "1", 1);
        //alert(InstallHelper.build);
        if(!InstallHelper.build){
          InstallHelper.build = InstallerBuild;
          //alert(InstallHelper.build.url);
        }
        if (!InstallHelper.build.url) {
            return InstallHelper.browser_not_supported();
        }

        //$('debug').innerHTML += "Browser: " + InstallHelper.build.browser + "<br />";
        InstallHelper.windowHeight = window.innerHeight;

        // ADD STATS downloadClicked.
        InstallHelper.log('Download_Clicked');
        
        if (InstallHelper.build.browser == 'ff' || InstallHelper.build.browser == 'ge') {

            return InstallHelper.ff_begin();

        } else if (InstallHelper.build.browser == 'ie') {
            return InstallHelper.ie_MSI_download();

        } else if (InstallHelper.build.browser == 'sa') {
            return InstallHelper.sa_begin();
        } else if (InstallHelper.build.browser == 'ch') {
            return InstallHelper.ch_begin();
        }
        return false;

    },

    browser_not_supported: function() {

        //$('debug').innerHTML += "Browser Not Supported! <br />";
        InstallHelper.displayHelper('na_not_supported');
        return false;
    },

    helper: false,
    current: false,
    displayHelper: function(container, hideEasy) {


        //$('debug').innerHTML += "Display Helper<br />";
        if (!InstallHelper.helper)
        InstallHelper.helper = InstallHelper.window_factory($('divInstallHelper'));

        if (container) {
            if (hideEasy) {
                $('installing_is_easy').style.visibility='hidden';
            }
            
            if (InstallHelper.current) {
                Effect.Appear(InstallHelper.current, {
                    duration: 0.5
                });
            } else {
                Effect.Appear(container);
            }
            InstallHelper.current = container;
        }

        if (!InstallHelper.helper.isOpen) {
            InstallHelper.helper.open();
        }

        //$('debug').innerHTML += Object.toJSON(InstallHelper.helper);
    },

    // LivePipe UI, Ryan Johnson
    // styled examples use the window factory for a shared set of behavior
    window_factory: function(container) {
        var w = new Control.Modal(container, Object.extend({
            overlayOpacity: 0.90,
            className: 'installhelper_window',
            closeOnClick: $('installhelper_window_close'),
            width: 300,
            minHeight: 150,
            position: [document.viewport.getWidth() - 335, 0],
            fade: true

        },
        {}));

        if (InstallHelper.build.browser != 'ie' || InstallHelper.build.browserversion != '6')
        {
            Control.Overlay.container.setStyle({
                position: 'fixed',
                left: '0px',
                top: '0px',
                right: '0px',
                bottom: '0px'
            });

            $('divInstallHelper').style.position = 'fixed';
        }
        return w;
    },


    /**************

        SAFARI

    **************/

    sa_begin: function() {
        return true;
    },

    /**************

        FIREFOX

    **************/

    ff_begin: function() {
        //$('debug').innerHTML += "FF Begin <br />";
        //InstallHelper.displayHelper('');
        if (InstallHelper.build.browserversion == '3' || InstallHelper.build.browser == 'ge') {
            return InstallHelper.ff3_begin();

        } else if (InstallHelper.build.browserversion == '2') {
            return InstallHelper.ff2_begin();
        }
    },

    ff_install_try: false,
    ff_tryInstall: function()
    {
        var result = false;
        try {
            var params = new Array();
            params["Cooliris " + InstallHelper.build.majorVersion + " for Firefox"] = {
                URL: InstallHelper.build.url,
                IconURL: "http://www.cooliris.com/shared/static/cooliris/cooliris_32.png"
            };
            result = InstallTrigger.install(params);
        } catch(e) {}
        InstallHelper.ff_install_try = result;
        return result;
    },

    ff_barPresent: function() {
        if (InstallHelper.windowHeight > window.innerHeight)
        return true;

        var diff = InstallHelper.firstWindowHeight - InstallHelper.windowHeight;

        return (diff != 0 && (diff < 35 || diff > -35));

    },

    /*************
        FF3
    *************/

    ff3_begin: function() {
        // ADD STATS Beginning Installation
        //$('debug').innerHTML += "FF3 Begin <br />";
        InstallHelper.ff3_waitForBarDown(1);
        return InstallHelper.ff_tryInstall();

    },

    ff3_waitForBarDown: function(iteration) {

        if (iteration > 5) {
            // ADD STATS 'Already Allowed'
            InstallHelper.ff3_allowBarUp();
            // Redundant, no? well yes, its cause if you refresh with bar down, then the other check doesnt work.
        } else if (InstallHelper.ff_barPresent()) {
            InstallHelper.ff3_allowBarDown();

        } else {
            setTimeout("InstallHelper.ff3_waitForBarDown(" + (iteration + 1) + ")", 500);
        }
    },

    ff3_allowBarDown: function() {
        // ADD STATS Allow Bar Down
        InstallHelper.log('Allow_Bar_Down');

        setTimeout("Effect.Appear('div_arrow_up', { duration: 1.0 });", 1000);
        InstallHelper.displayHelper('ff3_instructions');
      

        InstallHelper.log('Shown_Instructions');
        InstallHelper.ff3_waitForBarUp();
    },


    ff3_waitForBarUp: function() {
        if (InstallHelper.windowHeight == window.innerHeight) {
            InstallHelper.ff3_allowBarUp();
        } else {
            setTimeout("InstallHelper.ff3_waitForBarUp()", 500);
        }
    },

    ff3_allowBarUp: function() {
        // ADD STATS Clicked Allow on Allow Bar
        InstallHelper.log('Clicked_Allow');

        //Effect.Fade('ff3_dialog_installnow', { duration: 1.0 });
        Effect.Fade('div_arrow_up', {
            duration: 0.5
        });
        $('clickAllow').style.color = '#666';
        $('clickInstallNow').style.color = '#666';
        $('restartFirefox').style.color = '#fff';

    },

    /*************
        FF2
    *************/
    ff2_begin: function() {
        // ADD STATS Beginning Installation
        //$('debug').innerHTML += "FF2 Begin <br />";
        InstallHelper.ff2_waitForBarDown(1);
        InstallHelper.ff_tryInstall();
        return false;
    },

    ff2_waitForBarDown: function(iteration) {
        if (iteration > 5) {
            // ADD STATS 'Already Allowed'
            // InstallHelper.ff2_allowBarDown(); // Redundant, no? well yes, its cause if you refresh with bar down, then the other check doesnt work.
            } else if (InstallHelper.windowHeight > window.innerHeight) {
            InstallHelper.ff2_allowBarDown();

        } else {
            setTimeout("InstallHelper.ff2_waitForBarDown(" + (iteration + 1) + ")", 500);
        }
    },

    ff2_allowBarDown: function() {
        // ADD STATS Allow Bar Down
        InstallHelper.log('Edit_Options_Bar_Down');

        $('ff2_clickEditOptions').style.color = '#fff';
        $('ff2_clickAllow').style.color = '#666';
        $('ff2_clickDownloadAgain').style.color = '#666';
        InstallHelper.displayHelper('ff2_instructions');
        InstallHelper.ff2_waitForBarUp();
    },


    ff2_waitForBarUp: function() {
        if (InstallHelper.windowHeight == window.innerHeight) {
            InstallHelper.ff2_allowBarUp();
        } else {
            setTimeout("InstallHelper.ff2_waitForBarUp()", 500);
        }
    },

    ff2_allowBarUp: function() {
        // ADD STATS Clicked Edit Options on Bar
        InstallHelper.log('Clicked_Edit_Options');
        $('ff2_clickEditOptions').style.color = '#666';
        $('ff2_clickAllow').style.color = '#fff';
        setTimeout("InstallHelper.ff2_clickedAllow()", 4000);
    },

    ff2_clickedAllow: function() {
        // This cannot actually be detected, so we dont log a stat.
        // It is just estimated via time. It's only a nice color change anyway.
        $('ff2_clickAllow').style.color = '#666';
        $('ff2_clickDownloadAgain').style.color = '#fff';
    },

    ff2_clickedDownloadAgain: function() {
        // ADD STATS CLICK DL AGAIN
        InstallHelper.log('Download_Clicked_Again');

        InstallHelper.windowHeight = window.innerHeight;
        InstallHelper.ff2_begin();

    },

    /****************

    INTERNET EXPLORER

    ****************/

    ie_onload: function() {
        InstallHelper.build = InstallerBuild;
    },

    /* MSI Functions */

    ie_MSI_download: function() {
        InstallHelper.log('MSI_Download');
        InstallHelper.displayHelper('ie_install');
        /* hack for hitachi */
        if(piclens.getConfigProperty('sys.partner-id') == '3de1eec42628'){
          window.location = InstallHelper.build.alt_url;
        }else{
          window.location = InstallHelper.build.full;
        }
        return false;
    },

    /****************

    Google Chrome

    ****************/
    
    ch_onload: function(){
        var chromeVersionArr = navigator.userAgent.match(/Chrome\/([\d\.]+)/),
            chromeVersion = chromeVersionArr && chromeVersionArr[1];
        InstallHelper.chromeVersionSupported = InstallHelper.matchesRequired(chromeVersion, InstallerBuild.minBrowserVersion);
        
        if (!InstallHelper.chromeVersionSupported) {
            var anchors = document.getElementsByTagName('a'),
                index = anchors.length,
                a;
            
            while (--index) {
                a = anchors[index];
                if (a.href == InstallHelper.build.url){
                    a.href = "#";
                }
            }
            
        }
    },


    ch_begin: function() {
        if (InstallHelper.chromeVersionSupported) {
            InstallHelper.log('Chrome_Download');
            window.location = InstallHelper.build.url;
            return false;
        } else {
            InstallHelper.displayHelper('ch_update_required', 'hideEasy');
            return false;
        }
    },

    matchesRequired: function(version, required) {
        if (!required) return true;

        var a = version.split('.');
        var b = required.split('.');

        for (var i = 0; i < 4; i++)
        {
            var left = parseInt(a[i] || 0, 10),
            right = parseInt(b[i] || 0, 10);

            if (left > right)
            {
                //InstallHelper.log('Version_Older');
                return true;
            }
            else if (left < right)
            {
                //InstallHelper.log('Version_Newer');
                return false;
            }
        }
        //InstallHelper.log('Version_Same');
        return true;
    },

    end: true
};
