//==============================================================================
// Detects a redirect from iframe content which has been accessed directly
// Redirected page is include in the main page url
// Sets the relivant iframe content to the url accessed during redirect

function redirectedIframe() {

    var loc_var        = this.location.href,
//        regexp         = new RegExp('.*\\?frame(.)=(.*)'), 
        frame_ids      = ['topframe', 'bottomframe'],
//        regexp_matches = loc_var.match(regexp);
        regexp_matches = loc_var.match(/.*\\?frame(.)=(.*)/);
    if (regexp_matches) {
    
        document.getElementById(frame_ids[regexp_matches[1] - 1]).src = regexp_matches[2];

    }
    
    return true;
    
}

//==============================================================================
// Resets the loading image source, stops gif animation freezing during 
// javascript
function loading(divid) {

    var div_elm = document.getElementById(divid),
        elm,
        imgsrc;

    for (elm in div_elm) {
    
        if (elm.tagName === 'IMG') {
        
            imgsrc  = elm.src;
            elm.src = imgsrc;
        
        }
    
    }
    
    return true;

}

//==============================================================================
// Remove 'flookii loading' image once javascript has finished working on iframe
// table
function removeLoadingImg(divid) {

    var div_elm = document.getElementById(divid);
    
    div_elm.style.visibility = 'hidden';
    
    return true;

}

//==============================================================================
// Replace 'flookii loading' image while javascript is working on iframe
// table
function replaceLoadingImg(divid) {

    var div_elm = document.getElementById(divid);
    
    div_elm.style.visibility = 'visible';
    
    loading(divid);
    
    return true;

}

//==============================================================================
// Sets tabs to active 'orange' on top iframe load
function setActiveTab(e) {

    var iframe       = FLOOKII.target(e),
        iframe_title = FLOOKII.getIframeDocument(iframe).title,
        tabanchors   = FLOOKII.getElementsByTagPropertyValue('a', 'target', 'topframe'),
        targettab    = iframe_title.replace(/^.*?\s-\s(.*?)\s-\s.*$/i, "$1"),
        tabstate     = 'off',
        i,
        j,
        children;

    for (i = 0; i < tabanchors.length; i = i + 1) {

        if (tabanchors[i].title.toLowerCase() === targettab.toLowerCase()) {
            tabstate = 'on';
        }
        
        children = tabanchors[i].childNodes;
        
        for (j = 0; j < children.length; j = j + 1) {
            if (typeof children[j].tagName !== 'undefined' && children[j].tagName === 'IMG') {
                children[j].style.visibility = tabstate === 'on' ? 'visible' : 'hidden';
                tabstate = 'off';
            }            
        }
    }

    // Set homepage title to iframe1 title
    document.title = iframe_title;
    
    return true;
    
}

/*============================================================================*/
/* scrollToBottomFrame                                                        */
/* Scroll page to bottom frame when report selected                           */
/*============================================================================*/

function scrollToBottomFrame() { 

    // Get the current document scroll amount
    var from_top  = document.documentElement.scrollTop,
        scroll_to = 560,
        i;

    // Replace the loading graphic when a new report is clicked
    replaceLoadingImg('loading_iframe2');
    
    // Scroll from current position to y pixel scroll_to
    for (i = from_top; i <= scroll_to; i = i + 4) {         
        window.scroll(0, i);  
    }

    return true;

}           

//==============================================================================
// Calendar code                                                              
//==============================================================================
// Create paragraph element containing the current date                                                                       
function calendar() {
    var date_obj         = new Date(),
        day_of_month     = date_obj.getDate() < 10 ? '0' + date_obj.getDate() : date_obj.getDate(),
        month_of_year    = date_obj.getMonth(),
        year_of_calendar = date_obj.getYear() < 1000 ? date_obj.getYear() + 1900 : date_obj.getYear(),
        month_array      = ["January", "February", "March",
                            "April", "May", "June",
                            "July", "August", "September",
                            "October", "November", "December" ],
        div_element      = document.getElementById('calendar'),
        p_element        = document.createElement("p"),   
        text_node        = document.createTextNode(month_array[month_of_year] + " " + day_of_month + ", " + year_of_calendar);
         
    p_element.appendChild(text_node);
    div_element.appendChild(p_element);

    return true;

}
    
//==============================================================================
// Report Navigation Code
//==============================================================================
function navigationEvents(e) {

    var element      = FLOOKII.target(e),
        is_list_item = element.tagName === "LI" ? "Yes" : "No",
        is_anchor    = element.tagName === "A"  ? "Yes" : "No",
        list_element_id,
        list_element,
        element_class,
        element_height,
        action,
        icon,
        slidemenu;

    // Did the event occur on an anchor?
    if (is_anchor === "Yes") {
        scrollToBottomFrame();    
        return true;
    }
    //  Did the event occur on a non-list item (and non-anchor)
    //  Find the parent with the defined class
    else if (is_list_item === "No") {
        list_element_id = FLOOKII.getParentByClassName(element, 'subcategory', 3).id + "_list";
        list_element    = document.getElementById(list_element_id);
    }
    //  Did the event occur on a list item (and non-anchor) which had an id
    else if (is_list_item === "Yes" && element.childNode.tagName !== 'A') {
        list_element = document.getElementById(element.id + "_list");
    }
    // We are not interested
    else {
        return false;
    }    

    // If list item is defined change the class (show,hide) and expand/collapse icon
    if (list_element) {
        element_class  = list_element.className;
        element_height = '';
        action         = '';
        
        // Quickly show the element so we can get it's height then hide it again.
        if (element_class === 'hide') {
            list_element.className    = 'show';
            element_height            = FLOOKII.getComputedStyle(list_element, 'height').match(/(\d*)(px|.*)/)[1];
            list_element.className    = 'hide';
            list_element.style.height = '0px';
            list_element.className    = 'show';
            icon                      = document.getElementById(list_element.id.replace(/(.*\_).*/, "$1icon"));
            icon.innerHTML            = "[-]";
            action                    = "expand";
        }
        else {
            element_height = FLOOKII.getComputedStyle(list_element, 'height').match(/(\d*)(px|.*)/)[1];
            icon           = document.getElementById(list_element.id.replace(/(.*\_).*/, "$1icon"));
            icon.innerHTML = "[+]";
            action         = "collapse";
        }

        slidemenu = function () {
            var i      = 0,
                pixels = 5,
                delay  = 1,
                intervalref,
                slide = function () {
                    var icon, 
                        scrollto;
                        
                    if (action === "expand") {
                        if ((i + pixels) >= element_height) {
                            list_element.style.height = element_height + 'px';
                            clearInterval(intervalref);
                            list_element.style.height = '';
                            icon                      = document.getElementById(list_element.id.replace(/(.*\_).*/, "$1icon"));
                            icon.innerHTML            = "[-]";
                        } 
                        else {
                            i += pixels;
                            list_element.style.height = i + 'px';
                        }
                        
                        return true;
                    }
                    else if (action === "collapse") {
                        if ((i + pixels) >= element_height) {
                            clearInterval(intervalref);
                            list_element.style.height = '0px';
                            list_element.className    = 'hide';
                            list_element.style.height = '';
                            icon                      = document.getElementById(list_element.id.replace(/(.*\_).*/, "$1icon"));
                            icon.innerHTML            = "[+]";
                        } 
                        else {
                            i += pixels;
                            scrollto                  = element_height - i; 
                            list_element.style.height = scrollto + 'px';
                        } 
                        
                        return true;
                    }
                    
                    return false;
                
                };
                    
            intervalref = setInterval(slide, delay);
                 
        };
         
        slidemenu();
        
        return true;
        
    }
    
    return false;
    
}

//==============================================================================
// Amazon Ad Code
//==============================================================================
// Set recurring scroll for amazon ad
function amazon() {

    var element       = document.getElementById('amazon_sidebar_inner'),
        scroll_height = element.scrollHeight,
        size          = 224,
        total         = 0,
        delay         = 20 * 1000,
        scroll;
 
    scroll = function () {

        if (total < (scroll_height - size)) {
            total += size;
        }
        else {
            total = 0;
        }

        element.scrollTop = total; 
            
        return true;
               
    };
        
    setInterval(function () { scroll(); }, delay);
    
    return true;
    
}

//==============================================================================
// IFRAME2 Code
//==============================================================================
// Alternate table rows colors by assigning classes to 'tr' elements
function stripeTable() {

    var iframe        = FLOOKII.getIframeDocument(document.getElementById('bottomframe')),
        my_table_rows = iframe.getElementsByTagName("TR"),
        len           = my_table_rows.length,
        alternate     = FLOOKII.alternateClass("trnocolor", "trcolor"),
        i;
    
    for (i = 0; i < len; i = i + 1) {
        my_table_rows[i].className = alternate();
    } 

    removeLoadingImg("loading_iframe2");
    
    return true;

}

//==============================================================================
// Create event listeners
//==============================================================================
function setListeners() {

    var navigation = document.getElementById('navigation'), // report menu
        topframe   = document.getElementById('topframe');   // top iframe

    calendar(); // populate 'calendar' div
    amazon();   // initialise amazon rotating ads
    
    // Create event listeners on defined elements, manually fire events if needed
    FLOOKII.addEvent(navigation, 'mouseup', navigationEvents, false);
    FLOOKII.addEvent(topframe, 'load', setActiveTab, false);
    FLOOKII.fireEvent(topframe, 'HTMLEvents', 'load');

    redirectedIframe();
    
    return true;
    
}

//==============================================================================
// Create on load events
//==============================================================================
FLOOKII.addEvent(window, 'load', setListeners, false);







