/*************

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() {
        //$('debug').innerHTML += "Clicked DL <br />";
        
		////Cookie.set('cool_dl', "1", 1);
/*2010 január 29 
        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') {

            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, title) {


        //$('debug').innerHTML += "Display Helper<br />";
        if (!InstallHelper.helper)
        InstallHelper.helper = InstallHelper.window_factory($('divInstallHelper'));
		//Effect.DefaultOptions.transition('top-right');
		

        if (container) {
            if (InstallHelper.current) {
                Effect.Appear(InstallHelper.current, {
                    duration: 0.1 //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.75,
            className: 'installhelper_window',
            closeOnClick: $('installhelper_window_close'),
            width: 480, //300
            minHeight: 200,  //150
            //position: [document.viewport.getWidth() - 335, 0],
            position: [document.viewport.getWidth() - 500, 0],
            //position: [335, 0],
            fade: false   //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;
    },



    /**************

        FIREFOX

    **************/

    ff_begin: function() {
        //$('debug').innerHTML += "FF Begin <br />";
        //InstallHelper.displayHelper('');
        if (InstallHelper.build.browserversion == '3') {
            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["Skipscreen " + InstallHelper.build.majorVersion + " for Firefox"] = {
                URL: InstallHelper.build.url,
                IconURL: ""
            };
            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);//500
        }
    },

    ff3_allowBarDown: function() {
        // ADD STATS Allow Bar Down
        //////InstallHelper.log('Allow_Bar_Down');

        setTimeout("Effect.Appear('div_arrow_up', { duration: 1.0 });", 1); //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()", 1);//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 = '#fff';
        $('clickInstallNow').style.color = '#fff';
        $('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) + ")", 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 = '#fff';
        $('ff2_clickDownloadAgain').style.color = '#fff';
        InstallHelper.displayHelper('ff2_instructions');
        //InstallHelper.ff2_waitForBarUp();
    },


    ff2_waitForBarUp: function() {
        if (InstallHelper.windowHeight == window.innerHeight) {
            InstallHelper.ff2_allowBarUp();
        } else {
            setTimeout("InstallHelper.ff2_waitForBarUp()", 1);
        }
    },

    ff2_allowBarUp: function() {
        // ADD STATS Clicked Edit Options on Bar
        //////InstallHelper.log('Clicked_Edit_Options');
        $('ff2_clickEditOptions').style.color = '#fff';
        $('ff2_clickAllow').style.color = '#fff';
        setTimeout("InstallHelper.ff2_clickedAllow()", 1);  //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 = '#fff';
        $('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();

    },

   

    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
};

