$(window).load(function() {

    if ($.browser.msie && parseInt($.browser.version) < 7) {
        window.setTimeout(function(){
          DD_belatedPNG.fix('.pngfix, li.sub_level_0, a, img');
        }, 1);
    }

    if ($('body.regional_tourism_awards #regional_tourism_award').length != 0) {
        window.setTimeout('init_masonry()', 0.2);
    }

    if ($("#tabs").length != 0) {
        window.setTimeout(function(){
          $("#tabs").tabs();
        }, 0.3);
    }
      
    

    window.setTimeout('init_navigation()', 0.4);

    //init_photos();

    if ($(document).find('#sidebar').length != 0) {
        window.setTimeout('init_sidebar()', 0.5);
    }

    if ($('body.programs-individual').length != 0) {
        window.setTimeout('init_programs_individual()', 0.6);
    }

    if ($('div.category_item').length != 0) {
        window.setTimeout('init_category_counter()', 0.7);
    }

    if ($('div.helpful_section').length != 0) {
        window.setTimeout('init_helpful_section()', 0.8);
    }

    if ($('body.events .JS_event_body').length != 0) {
        window.setTimeout('init_events_collapse()', 0.9);
    }

    window.setTimeout('init_header()', 0.5);
    window.setTimeout('init_sitemap()', 0.6);
    window.setTimeout('init_popups()', 0.7);
    
    if($('body.blog').length != 0){
      $('#nav_sub').find('li a').each(function(){
        if($(this).text() == "Industry Representation "){
          $(this).parent().addClass('current');
          $(this).parent().find('li a').each(function(){
            if($(this).text() == "From the Chief Executive "){
              $(this).parent().addClass('current');
            }
          });
        }
      });
    }

});

//CHECKED
function init_header() {
    /* RANDOMLY CHANGING HEADER IMAGE */
    /* This code picks a random number, out of the range specified, and adds a class to the body tag. */
    /* The CSS then uses the class name to change the image, and the header text image. */
    if ($(document).find('body.orangeheader').length != 0) {
        var numLow = 0;
        var numHigh = 5;
    } else if ($(document).find('body.greenheader').length != 0) {
        var numLow = 0;
        var numHigh = 6;
    } else {
        var numLow = 0;
        var numHigh = 12;
    }

    var adjustedHigh = (numHigh - numLow) + 1;
    var numRand = Math.floor(Math.random() * adjustedHigh) + numLow;

    $(document).find('body').addClass('image_' + numRand);
}

//CHECKED
function init_helpful_section() {
    var linkCount = 0;
    var totallinkHeight = 0;
    $('div.resource_body:first').find('li').each(function() {
        linkCount++;
        totallinkHeight += $(this).height();
    })
    var collinkHeight = totallinkHeight / 3;
    var rowlinkHeight = 0;
    var rowlinkHeight2 = 0;
    $('div.resource_body:first').find('li').each(function() {
        if (rowlinkHeight < collinkHeight) {
            rowlinkHeight += $(this).height();
            $(this).addClass('linkcolumn1');
        } else {
            if (rowlinkHeight2 < collinkHeight) {
                rowlinkHeight2 += $(this).height();
                $(this).addClass('linkcolumn2');
            } else {
                $(this).addClass('linkcolumn3');
            }
        }
    })
    var divHeight = collinkHeight + 30;
    var linkcol1 = $('div.resource_body:first').find('.linkcolumn1');
    var linkcol2 = $('div.resource_body:first').find('.linkcolumn2');
    var linkcol3 = $('div.resource_body:first').find('.linkcolumn3');
    $('div.resource_body:first').append('<div id="linkcolumns" style="clear: both; float: none; height: ' + divHeight + 'px;"></div>');
    $('#linkcolumns').append('<ul id="linkcol1" style="width: 210px; float: left;"></ul>');
    $('#linkcolumns').append('<ul id="linkcol2" style="width: 210px; float: left;"></ul>');
    $('#linkcolumns').append('<ul id="linkcol3" style="width: 210px; float: left;"></ul>');
    $('#linkcol1').append(linkcol1);
    $('#linkcol2').append(linkcol2);
    $('#linkcol3').append(linkcol3);
}

// CHECKED
function init_category_counter() {
    var catCount = 0;
    var totalHeight = 0;
    $('div.columnize').find('div.category_item').each(function() {
        $(this).find('.cat_body').hide();
        $(this).find('.cat_title').click(function() {
            $(this).parent().find('.cat_body').each(function() {
                if ($(this).parent().hasClass('expanded')) {
                    $(this).hide();
                    $(this).parent().removeClass('expanded');
                } else {
                    $(this).show();
                    $(this).parent().addClass('expanded');
                }
            })
        })
        catCount++;
        totalHeight += $(this).find('.cat_title').height();
    })
    var colHeight = totalHeight / 2;
    var rowHeight = 0;
    $('div.columnize').find('div.category_item').each(function() {
        if (rowHeight < colHeight) {
            rowHeight += $(this).find('.cat_title').height();
            $(this).addClass('column1');
        } else {
            $(this).addClass('column2');
        }
    })
    var col1 = $('div.columnize').find('.column1');
    var col2 = $('div.columnize').find('.column2');
    $('#col1').append(col1);
    $('#col2').append(col2);
}

function init_programs_individual() {
    $('body.programs-individual').find('#contents li a').each(function() {
        var text = $(this).text();
        if (text == 'Australian Tourism Accreditation') $(this).addClass('ata_sb');
        if (text == 'Ecotourism Certification') $(this).addClass('eco_sb');
        if (text == 'Climate Action Certification') $(this).addClass('cac_sb');
        if (text == 'Respecting Our Culture') $(this).addClass('roc_sb');
    });
    
    var href =  $('#regnowurl').attr('rel');
    
    $('#tabs .ui-tabs-nav').find('a').each(function() {
        var target = $(this).attr('href');
        if (target == '#tabs-3') {
            $(this).parent().after('<li><a href="' + href + '" target="_blank">Register Now</a></li>');
        }
    });
    
}

function init_masonry() {
    $('body.regional_tourism_awards #regional_tourism_award').masonry({
        columnWidth: 1
    });
}

function init_navigation() {
    /* DROP DOWN MENUS FROM HEADER NAVIGATION */
    /* Simply adds and removes a class, and changes the 'display' mode, on rollover. */
    $('#header #main-nav li').mouseenter(function() {
        $(this).addClass('rollover');
        $(this).find('ul').css('display', 'block');
    });
    $('#header #main-nav li').mouseleave(function() {
        $(this).removeClass('rollover');
        $(this).find('ul').css('display', 'none');
    });
}

// function init_photos() {
//     /* This is basic - uses default settings */
//     $(".photos a").fancybox();
//     /* Using custom settings */
//     $("a#inline").fancybox({
//         'hideOnContentClick': true
//     });
//     /* Apply fancybox to multiple items */
//     $("a.group").fancybox({
//         'transitionIn': 'elastic',
//         'transitionOut': 'elastic',
//         'speedIn': 600,
//         'speedOut': 200,
//         'overlayShow': false
//     });
//     $(document).find('.photo-gallery-holder').each(function() {
//         $(this).hide();
//     })
// 
//     $(document).find('.photo-gallery-holder').each(function() {
//         $('.albums a').click(function() {
//             $(this).fadeOut('slow');
//             var target = $(this).attr('rel');
//             $(document).find('.gallery-' + target).each(function() {
//                 $(this).fadeIn('slow');
//                 $(this).find('.show-album').click(function() {
//                     $(this).parent().parent().find('.albums a').fadeIn('slow');
//                     $(this).parent().hide();
//                 })
//             })
//         })
//     })
// }

function init_sidebar() {
    $('#sidebar #contents li ul').each(function() {
        $(this).parent().find('a:first').addClass('trigger');
        $(this).parent().addClass('haschildren');
    });

    $('#sidebar #contents li ul li.current').each(function() {
        $(this).parent().parent().addClass('current');
    });

    $('#contents li').mouseenter(function() {
        $(this).addClass('sidebar-open');
    });

    $('#contents li').mouseleave(function() {
        $(this).removeClass('sidebar-open');
    });

}

var scrollto;
var footerstate = false;

function init_sitemap() {
    /* SITEMAP */
    /* Hides the sitemap on load */
    $('#footer #sitemap').hide();

    /* Sets the click event handlers for the footer header and the sitemap nav link */
    $('#footer h1').click(footer);
    $('#sitemap-nav').click(footerOpen);

    $('#footer h1').mouseenter(function() {
        $(this).css('color', 'white');
    });

    $('#footer h1').mouseleave(function() {
        $(this).css('color', '#333');
    });

    /* Sets up variables to use in the opening/closing */
    

    /* First function is called by clicking the footer header, checks if the sitemap is open or closed. */
    /* If it is open it will close it. If it is closed it will call footerOpen, which is shared by the Sitemap link in the header. */
    /* This way the header link can only open the sitemap, not close it. */
}

function footer() {
    if (footerstate == true) {
        footerstate = false;
        $('#footer #sitemap').css('display', 'none');
    } else {
        footerOpen();
    }
}

function footerOpen() {
    scrollto = $(document).height();
    footerstate = true;
    $('#footer #sitemap').show();
    $(document).scrollTop(scrollto)
}

function init_popups() {
    /* LOG-IN POPUP */
    $('#header #login-nav-link').click(function() {
        init_popup_redirect();
        $('#login-bg').fadeIn('slow');
        $('#login-box').fadeIn('slow');
    })
    $('div.newsletter_left a span, div.newsletter_content a span, li.signup a').click(function() {
        $('#login-bg').fadeIn('slow');
        $('#subscribe-box').fadeIn('slow');
    })
    var currentsignupvalue = '';
    $('#subscribe-box .newsletter_field').focus(function() {
        if ($(this).attr('rel') == $(this).attr('value')) {
            currentsignupvalue = $(this).attr('value');
            $(this).attr('value', '');
        }
    })
    $('#subscribe-box .newsletter_field').blur(function() {
        if ($(this).attr('value') == '') $(this).attr('value', $(this).attr('rel'));
    })
    $('#top-nav li.social a.rss').click(function() {
        $('#login-bg').fadeIn('slow');
        $('#rss-box').fadeIn('slow');
    })
    $('#login-bg, a.close_pop').click(function() {
        $('#login-bg').fadeOut('slow');
        $('#login-box').hide();
        $('#subscribe-box').hide();
        $('#rss-box').hide();
    })
}

function init_events_collapse() {
    $('body.events .trigger').each(function() {
        var state = "collapsed";
        $(this).click(function() {
            if (state == "collapsed") {
                state = "expanded";
                $(this).addClass(state);
                $(this).parent().parent().find('.JS_event_summary').hide();
                $(this).parent().parent().find('.JS_event_body').show();
            } else {
                state = "collapsed";
                $(this).removeClass("expanded");
                $(this).parent().parent().find('.JS_event_summary').show();
                $(this).parent().parent().find('.JS_event_body').hide();
            }
        });
    });
    var events_url = window.location.href;
    var split_events_url = events_url.split('#');
    split_events_url.splice(0, 1);
		$(document).find('a#'+split_events_url).parent().find('.JS_event_summary').hide();
		$(document).find('a#'+split_events_url).parent().find('.JS_event_body').show();
}

function init_popup_redirect() {
    var url = window.location.href;
    var spliturl = url.split('/');
    spliturl.splice(0, 1);
    spliturl.splice(0, 1);
    spliturl.splice(0, 1);
    spliturl.splice(0, 1);
    var finishedurl = "";
    for (var i = 0; i < spliturl.length - 1; i++) {
        finishedurl += spliturl[i] + "/";
    }
    if (finishedurl == "") finishedurl = "/";
    $('#login-box .hiddenFields input').each(function() {
        var ret = $(this).attr('name');
        if (ret == "RET") {
            $(this).attr('value', url);
        }
    })

}
