/**
 *	@purpose:   handles the hiding/showing of the desktop
 *	@author:    robert.leurs@cooloxygen.com
 *	@version:   1.1 (improvements for handling in IE)
 *	            - replaced some standard jquery functions
 *	            - used a transparant background image on all involved elements 
 *	              (IE needs a background image/color the handle transitions correctly!)
 */ 

$(document).ready(function(){

    // 01. instantiate variables
    var toggleTrigger	= $("#toggle_hideables");	// the object that triggers the events
	var toggleObjects	= $(".hidable");			// the object that animated
    var waitTimer   	= 3000; 					// time to show the desktop after initial load (milliseconds)
    var effectTimer 	= 1000;						// how long the effect needs to be animated for (milliseconds)
	var fadePercentage  = 0.0;						// percentage to fade-to when animating (1.0 = 100% / 0.0 = 0%)
    var blinkCount      = 10                        // how many times should the toggle button blink
    var runIEmodus      = true                      // run in compatibility modus for IE true/false
    
    // 02. get variable from url
    var runIntro = getUrl()["intro"];
    
    // 03. do we need to run the intro yes/no
    if(runIntro == 'y'){
        // what's new.., IE doesn't support nice transitions with transparant PNG!!!
        if ($.browser.msie && runIEmodus){

            // 03.a. first hide all elements
            $(toggleObjects).css("display", "none");
            $('ul.spider').css("display", "none");  

            // 03.b. now set a timer and then show all elements
            setTimeout(
                function(){ 
                    // blink the toggler in the menu-bar
                    blinkToggler();
                    // show the spider
                    $('ul.spider').css("display", "block");
                    // fade-in the content
                    $(toggleObjects)
                        .stop()
                        .css("display", "block");
                }, 
                waitTimer
            );            
        } else {
            // 03.a. first hide all elements
            $(toggleObjects).fadeTo("fast", fadePercentage); // This sets the opacity to 100% on hover    
            $('ul.spider').hide("fast");
            
            // 03.b. now set a timer and then show all elements
            setTimeout(
                function(){ 
                    // blink the toggler in the menu-bar
                    blinkToggler();
                    // show the spider
                    $('ul.spider').show("slow");
                    // fade-in the content
                    $(toggleObjects)
                        .stop()
                        .fadeTo(effectTimer, 1.0); // Set the opacity to 100%
                }, 
                waitTimer
            );
        }
    }
    
    // 'blink' the toggler button x times
    function blinkToggler(){

        var blinkCounter = 1;
        var blinkUBound = blinkCount++; // we need to add 1 due to the while "<" condition
    
        // what's new.., IE doesn't support nice transitions with transparant PNG!!!
        if ($.browser.msie && runIEmodus){
            // argh!
            do {
                $(toggleTrigger).fadeTo(500, 0.3);
                $(toggleTrigger).fadeTo(500, 1.0);      
                blinkCounter++;
            } while (blinkCounter < blinkUBound);            
        } else {
            // proper browser; thus run effect
            do {
                $(toggleTrigger).fadeTo(500, 0.3);
                $(toggleTrigger).fadeTo(500, 1.0);
                blinkCounter++;
            } while (blinkCounter < blinkUBound);
        }
    }
    
    // toggle the hideables on mouse-hover
	$(toggleTrigger).hover(function(){
        hideObjects();
	},function(){
   		// reset objects on mouseout
        showObjects();
	});
    
    function hideObjects(){
        // what's new.., IE doesn't support nice transitions with transparant PNG!!!
        if ($.browser.msie && runIEmodus){
            $('ul.spider').css("display", "none");
            $(toggleObjects)
                .stop()
                .css("display", "none");
        } else {
            // set the opacity to value of variable "fadePercentage" on hover
            $(toggleObjects)
                .stop()
                .fadeTo(effectTimer, fadePercentage);
            $('ul.spider').hide("slow");
        }    
    }
    
    function showObjects(){
        // what's new.., IE doesn't support nice transitions with transparant PNG!!!
        if ($.browser.msie && runIEmodus){
            $('ul.spider').css("display", "block");
            $(toggleObjects)
                .stop()
                .css("display", "block");        
        } else {        
            $('ul.spider').show("slow");
            $(toggleObjects)
                .stop()
                .fadeTo(effectTimer, 1.0);
        }    
    }

/**
 *	@purpose:   replace some jquery functions for better handling in IE
 *	@version:   1.1 (improvements for handling in IE)
 */
    jQuery.fn.fadeIn = function(speed, callback) { 
        return this.animate({opacity: 'show'}, speed, function() { 
            if (jQuery.browser.msie)  
                this.style.removeAttribute('filter');  
            if (jQuery.isFunction(callback)) 
                callback();  
        }); 
    }; 
     
    jQuery.fn.fadeOut = function(speed, callback) { 
        return this.animate({opacity: 'hide'}, speed, function() { 
            if (jQuery.browser.msie)  
                this.style.removeAttribute('filter');  
            if (jQuery.isFunction(callback)) 
                callback();  
        }); 
    }; 
     
    jQuery.fn.fadeTo = function(speed,to,callback) { 
        return this.animate({opacity: to}, speed, function() { 
            if (to == 1 && jQuery.browser.msie)  
                this.style.removeAttribute('filter');  
            if (jQuery.isFunction(callback)) 
                callback();  
        }); 
    }; 
    
});