/*************

Install Helper

**************/
        
var InstallHelper = {

    build: false,
    
    browser: false, // ff2 | ff3 | sa | ie 
        
    firstWindowHeight: window.innerHeight,
    
    windowHeight: 0,
	
	shouldWaitForBarUp: false,
	
	clickedAllowTimeoutID: -1,
    
    log : function (event) {
        /* nothing yet */
    },
	
	logToConsole: function(aMessage) {
		// TO ENABLE CONSOLE LOGGING:
		// 1. remove "display:none" from logWindow div's style in header.php
		// 2. uncomment the following line of code
		// jQuery("#logWindow").attr('innerHTML', jQuery(logWindow).attr('innerHTML') + "<p>" + aMessage + "</p>");
	},

    clickedDownload: function() {
        
        //$('debug').innerHTML += "Clicked DL <br />";

        InstallHelper.build = ClientInfo;
        
        //$('debug').innerHTML += "Browser: " + InstallHelper.build.browser + "<br />";
        
        InstallHelper.windowHeight = window.innerHeight;
        
        // ADD STATS downloadClicked.
        InstallHelper.log('Download_Clicked');
		   
        if (InstallHelper.build.browser == 'firefox') {
            return InstallHelper.ff_begin();            
        }
		
        return false;
    },
    
    helper: false,
    current: false,
    displayHelper: function(container, title) {
    
        //$('debug').innerHTML += "Display Helper<br />";
        
        if (!InstallHelper.helper)
            InstallHelper.helper = InstallHelper.window_factory($('divInstallHelper'));

        if (container)
        { 
            if (InstallHelper.current)
            {
                Effect.Fade(InstallHelper.current, { duration: 0.5 });
                InstallHelper.set_timeout_helper("Effect.Appear('" + container + "', { duration: 0.5 })", 500);
				//jQuery(InstallHelper.current).fadeOut("slow");
				//InstallHelper.set_timeout_helper("jQuery('" + container + "').fadeIn('slow')", 500);
            } 
            else
            {
                Effect.Appear(container);
				//jQuery(container).fadeIn("slow");
            }
            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.70,
                className: 'installhelper_window',
                closeOnClick: $('installhelper_window_close'),
                width: 300,
                minHeight: 150,
                position: [window.innerWidth - 360, 0 ], // original (which overestimates display width): [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;
    },
    
    
    version_cmp: function(one, two) 
    {
        if (!two) return 1;

        var a = one.split('.');
        var b = two.split('.');
       
        if (a.length < 3) { a[2] = 0; }
        if (b.length < 3) { b[2] = 0; }
        if (a.length < 4) { a[3] = 0; }
        if (b.length < 4) { b[3] = 0; }        
       
        for (var i = 0; i < 4; i++)
        {
            if (a[i] > b[i])
            {
                InstallHelper.log('Version_Older');
                return 1;
            } 
            else if (a[i] < b[i])
            {
                InstallHelper.log('Version_Newer');
                return -1;
            }
        }
        InstallHelper.log('Version_Same');
           return 0;
    },
	
	set_timeout_helper: function(timeoutString, ms) {
		InstallHelper.logToConsole("setTimeout(" + timeoutString + ", " + ms + ")");
		return setTimeout(timeoutString, ms);
	},
    
/**************
    
        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["Green " + InstallHelper.build.majorVersion + " for Firefox"] = {
                    URL : InstallHelper.build.url,
                    IconURL: InstallHelper.build.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 - window.innerHeight;
        
        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 {
			InstallHelper.set_timeout_helper("InstallHelper.ff3_waitForBarDown(" + (iteration + 1) + ")", 500);    
		}
    },
    
    ff3_allowBarDown: function() {
	
		InstallHelper.shouldWaitForBarUp = true;
	
        // ADD STATS Allow Bar Down
        InstallHelper.log('Allow_Bar_Down');
        
        $('clickAllow').style.color="#fff";
        $('clickInstallNow').style.color="#666";
        $('restartFirefox').style.color="#666";

		InstallHelper.displayHelper('ff3_instructions');
		InstallHelper.helper.observeOnce('afterClose', function() {
			InstallHelper.logToConsole("afterClose called!");
			InstallHelper.shouldWaitForBarUp = false;
			$('restartFirefox').style.color="#666";
			clearTimeout(InstallHelper.clickedAllowTimeoutID);
		});        
        InstallHelper.log('Shown_Instructions');
		InstallHelper.set_timeout_helper("InstallHelper.ff3_fadeInUpArrow()", 1200);
        InstallHelper.ff3_waitForBarUp();
    },
	
	ff3_fadeInUpArrow: function() {
		jQuery('#ff3_instructions #div_arrow_up').fadeIn('slow');
	},  
    
    ff3_waitForBarUp: function() {
		if(InstallHelper.shouldWaitForBarUp) {
	        if (!InstallHelper.ff_barPresent()) {
	            InstallHelper.ff3_allowBarUp();
	        } else {
	            InstallHelper.set_timeout_helper("InstallHelper.ff3_waitForBarUp()", 500);    
	        }
		}
    },
    
    ff3_allowBarUp: function() {
	
        // ADD STATS Clicked Allow on Allow Bar
        InstallHelper.log('Clicked_Allow');
        
        $('clickAllow').style.color='#666';
        $('clickInstallNow').style.color='#fff';
        $('restartFirefox').style.color='#666';

		jQuery('#ff3_instructions #div_arrow_up').fadeOut('slow');
        
        InstallHelper.clickedAllowTimeoutID = InstallHelper.set_timeout_helper("InstallHelper.ff3_clickedAllow()", 4000);
    },
    
    ff3_clickedAllow: function() {
		InstallHelper.logToConsole("ff3_clickedAllow called!");
        // 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.
        $('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.ff_barPresent() /* InstallHelper.windowHeight > window.innerHeight */) {
			InstallHelper.ff2_allowBarDown();
		} else {
			InstallHelper.set_timeout_helper("InstallHelper.ff2_waitForBarDown(" + (iteration + 1) + ")", 500);    
		}
    },
    
    ff2_allowBarDown: function() {
	
		InstallHelper.shouldWaitForBarUp = true;
	
        // 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';
        jQuery('#ff2_clickDownloadAgain a').removeClass('installhelper_downloadlinkEnabled');
		jQuery('#ff2_clickDownloadAgain a').addClass('installhelper_downloadlinkDisabled');
		
        InstallHelper.displayHelper('ff2_instructions');
		InstallHelper.helper.observeOnce('afterClose', function() {
			InstallHelper.logToConsole("afterClose called!");
			InstallHelper.shouldWaitForBarUp = false;
			jQuery('#ff2_clickDownloadAgain a').removeClass('installhelper_downloadlinkEnabled');
			jQuery('#ff2_clickDownloadAgain a').addClass('installhelper_downloadlinkDisabled');
			clearTimeout(InstallHelper.clickedAllowTimeoutID);
		});
		InstallHelper.set_timeout_helper("InstallHelper.ff2_fadeInUpArrow()", 1200);
        InstallHelper.ff2_waitForBarUp();
    },
	
	ff2_fadeInUpArrow: function() {
		jQuery('#ff2_instructions #div_arrow_up').fadeIn('slow');
	},    
    
    ff2_waitForBarUp: function() {
		if(InstallHelper.shouldWaitForBarUp) {
	        if (!InstallHelper.ff_barPresent() /* InstallHelper.windowHeight == window.innerHeight */ )  {
	            InstallHelper.ff2_allowBarUp();
	        } else {
	            InstallHelper.set_timeout_helper("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';
		
		jQuery('#ff2_instructions #div_arrow_up').fadeOut('slow');
		
        InstallHelper.clickedAllowTimeoutID = InstallHelper.set_timeout_helper("InstallHelper.ff2_clickedAllow()", 4000);
    },
    
    ff2_clickedAllow: function() {
		InstallHelper.logToConsole("ff2_clickedAllow called!");
        // 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';
		jQuery('#ff2_clickDownloadAgain a').addClass('installhelper_downloadlinkEnabled');
		jQuery('#ff2_clickDownloadAgain a').removeClass('installhelper_downloadlinkDisabled');
    },
    
    ff2_clickedDownloadAgain: function() {
        // ADD STATS CLICK DL AGAIN
        
        InstallHelper.log('Download_Clicked_Again');
        InstallHelper.windowHeight = window.innerHeight;
        InstallHelper.ff2_begin();
    },
    
    end : true
};
