/**
 * CCRF User Interface
 */ 
var CCRF = {

    /**
     * Navigation delay
     */         
    navigation_delay: 20, 

    /**
     * Init UI functions
     */         
    init: function () {
         CCRF.insertFlash(); 
         CCRF.navigation();
         CCRF.Researchers.init();
         CCRF.Fellows.init();
         CCRF.Tabs.init(); 
         CCRF.ShareThisPage.init();
         CCRF.preloadImages(["/images/bg/nav-current.jpg", "../images/bg/subnav-top.png", "/images/bg/subnav.jpg"]);
    },
    
    insertFlash: function () {
        if ($('#home-page-top').length) {
            var so = new SWFObject("../flash/home_page_top.swf", "intro", "960", "290", "7", "#ffffff");
            so.addParam("wmode", "transparent");
            so.addVariable("leftButtonURL", "/pioneering-research/what-we-fund/index.html");
            so.addVariable("rightButtonURL", "/stories-of-hope/kids-stories/index.html");
            so.write("home-page-top");
        }
    },
    
    /**
     * Navigation slider
     */         
    navigation: function () {
        $("#nav > li").each(function (i) {
            var counter = 0;
            var li = $(this);
            var container = li.find('.container');
            var slider = li.find('.slider');
            var additional = 6; 
            
            if ($.browser.msie) {
               additional = 0;
            }
            
            if ($.browser.safari) {
               additional = 0;
               container.css('margin-left', '1px');
            }

            var height = container.height() + li.height() + additional;
            container.css('margin-top', -(height));

            li.hoverIntent({
                sensitivity: 7,
                interval: CCRF.navigation_delay,
                over: expand,
                timeout: 0,
                out: collapse                
            });
            
            function expand () {
                counter++;
                li.addClass('hover');
                if (counter <= 1) {
                    container.addClass('display');
                    slider.SlideInDown(300);
                }
            }
            
            function collapse () {
                slider.SlideOutDown(200, function () {
                    li.removeClass('hover');
                    counter = 0;
                });
           }
        });
    },
    
    /**
     * Add stylesheet to document
     * @param {String} href Stylesheet url   
     * @param {String} media Stylesheet media   
     */               
    addStylesheet: function (href, media) {
        var head = document.getElementsByTagName("head")[0];
        var stylesheet = document.createElement('link');
        stylesheet.rel = 'stylesheet';
        stylesheet.type = 'text/css';
        stylesheet.media = media;
        stylesheet.href = href;
        head.appendChild(stylesheet);
    },
    
    /**
     * Preload images
     * @param {Array} images array with names of images   
     */               
    preloadImages: function (images) {
        for (var i = 0; i < images.length; i++) {
            var image = new Image();
            image.src = images[i];
        }
    }
}


/**
 * Researchers bios
 */
CCRF.Researchers = {
    
    /**
     * Time to open researcher's bio
     */         
    open_time: 250,
    
    /**
     * Time to close researcher's bio
     */         
    close_time: 1,
    
    /**
     * Time to scroll the page to researcher's name
     */         
    scroll_time: 500,
    
    /**
     * Init function
     */     
    init: function () {
        var researchers = $('.researcher');
        $('.researcher .expand a').each(function (i) {
            $(this).bind('click', function () {
                
                var link = $(this);
                var current = CCRF.Researchers.getCurrent(researchers);
                
                // Click on currently opened bio
                if (current === i) {
                    CCRF.Researchers.toggleExpandLink(link);
                    $(researchers[current]).find('.interview').slideUp(CCRF.Researchers.close_time, function () {
                        $(researchers[current]).removeClass('expanded');
                    });
                
                // Click on other bio than current          
                } else {
                    // Close current bio then open the other one
                    if (current !== -1) {
                        CCRF.Researchers.toggleExpandLink($(researchers[current]).find('.expand a')); // Toggle Expand link of current bio
                        $(researchers[current]).find('.interview').slideUp(CCRF.Researchers.close_time, function () {
                            $(researchers[current]).removeClass('expanded');
                            CCRF.Researchers.openAndScrollTo(researchers, i);
                        });
                        
                    // Nothing to close, open the bio    
                    } else {
                        CCRF.Researchers.openAndScrollTo(researchers, i);
                    }
                }      
                return false;
            });
        });
    },
    
    /**
     * Open bio and scroll page to researcher's name 
     */              
    openAndScrollTo: function (researchers, i) {
        
        $(researchers[i]).addClass('expanded');
        CCRF.Researchers.toggleExpandLink($(researchers[i]).find('.expand a'));
        
        $(researchers[i]).find('.interview').slideDown(CCRF.Researchers.open_time, function () {
            $(researchers[i]).find('img').ScrollTo(CCRF.Researchers.scroll_time);  
        });
    },
    
    /**
     * Toggle expand link
     */              
    toggleExpandLink: function (link) {
        $(link).parent().toggleClass('collapse');
        
        if ($(link).parent().is('.collapse')) {
            $(link).text('Close interview');
        } else {
            $(link).text('Read interview');
        }
    },
    
    /**
     * Get currently opened bio
     * Return bio index or -1 if no bio is opened     
     */              
    getCurrent: function (researchers) {
        var current = -1;
        for (var i = 0; i < researchers.length; i++ ) {
            if ($(researchers[i]).is('.expanded')) {
                current = i;
            }
        }
        return current;
    }
}


/**
 * Fellows
 */
CCRF.Fellows = {
    
    /**
     * Time to open researcher's bio
     */         
    open_time: 250,
    
    /**
     * Time to close researcher's bio
     */         
    close_time: 1,
    
    /**
     * Time to scroll the page to researcher's name
     */         
    scroll_time: 500,
    
    /**
     * Init function
     */     
    init: function () {
        var fellows = $('.fellow');
        $('.fellow .expand a').each(function (i) {
            $(this).bind('click', function () {
                
                var link = $(this);
                var current = CCRF.Fellows.getCurrent(fellows);
                
                // Click on currently opened bio
                if (current === i) {
                    CCRF.Fellows.toggleExpandLink(link);
                    $(fellows[current]).find('.interview').slideUp(CCRF.Fellows.close_time, function () {
                        $(fellows[current]).removeClass('expanded');
                    });
                
                // Click on other bio than current          
                } else {
                    // Close current bio then open the other one
                    if (current !== -1) {
                        CCRF.Fellows.toggleExpandLink($(fellows[current]).find('.expand a')); // Toggle Expand link of current bio
                        $(fellows[current]).find('.interview').slideUp(CCRF.Fellows.close_time, function () {
                            $(fellows[current]).removeClass('expanded');
                            CCRF.Fellows.openAndScrollTo(fellows, i);
                        });
                        
                    // Nothing to close, open the bio    
                    } else {
                        CCRF.Fellows.openAndScrollTo(fellows, i);
                    }
                }      
                return false;
            });
        });
    },
    
    /**
     * Open bio and scroll page to researcher's name 
     */              
    openAndScrollTo: function (fellows, i) {
        
        $(fellows[i]).addClass('expanded');
        CCRF.Fellows.toggleExpandLink($(fellows[i]).find('.expand a'));
        
        $(fellows[i]).find('.interview').slideDown(CCRF.Fellows.open_time, function () {
            $(fellows[i]).find('h2').ScrollTo(CCRF.Fellows.scroll_time);  
        });
    },
    
    /**
     * Toggle expand link
     */              
    toggleExpandLink: function (link) {
        $(link).parent().toggleClass('collapse');
        
        if ($(link).parent().is('.collapse')) {
            $(link).text('Close');
        } else {
            $(link).text('Read More');
        }
    },
    
    /**
     * Get currently opened bio
     * Return bio index or -1 if no bio is opened     
     */              
    getCurrent: function (fellows) {
        var current = -1;
        for (var i = 0; i < fellows.length; i++ ) {
            if ($(fellows[i]).is('.expanded')) {
                current = i;
            }
        }
        return current;
    }
}


/**
* Tabs
*/  
CCRF.Tabs = {
    /**
     * Init Tabs
     */                 
    init: function () {
        var links = $('.tabs-nav a');
        var tabs = $('.tab');
        
        if (tabs.length !== 0) {
            $(tabs[0]).addClass('active-tab'); // Indicates active tab
        }
        
        links.each(function (i) {
            $(this).bind('click', function(e) {
                if (tabs[i] !== undefined) {
                    var current = CCRF.Tabs.getCurrent(tabs);
                    
                    CCRF.Tabs.deactivateAll(links);
                    $(this).addClass('active'); 
                    
                    $(tabs[current]).removeClass('active-tab'); 
                    $(tabs[i]).addClass('active-tab');
                    
                    $(tabs[current]).find('div').fadeOut(200, function () {
                        $(tabs[current]).removeClass('tab-display');  // Hide active tab   
                        $(tabs[i]).addClass('tab-display'); // Display active tab
                        $(tabs[i]).find('div').fadeIn(200);    
                    });
                }
                return false;
             });
         });
    },
     
    /**
     * Get index of active tab
     */
    getCurrent: function (tabs) {
        for (var i = 0; i < tabs.length; i++ ) {
            if ($(tabs[i]).is('.active-tab')) {
                return i;
            }
        }
    },
    
     /**
     * Deactivate all links
     */
    deactivateAll: function (links) {
        for (var i = 0; i < links.length; i++ ) {
            $(links[i]).removeClass('active');
        }
    }          
}    

CCRF.ShareThisPage = {
    /**
     * Init ShareThisPage
     */
    init: function () {
        // Get "Share This Page" links
        var links = $('.share a');
        // Get page URL
        var page = document.URL;        
        // Overwrite the default "href" value with the page-specific value
        for (var i = 0; i < links.length; i++) {
            links[i].href = "http://support.childrenscancer.org/site/TellAFriend?page=" + page;
        }
    }
}

CCRF.addStylesheet("/css/js.css", "screen, projection");
$(document).ready(function () {
    CCRF.init();
});
