// utils
function basename(path) {
    return path.replace(/\\/g,'/').replace( /.*\//, '' );
}

function getSection(item, sec) {
    var href = $(item).attr('href');
    var section = $('title').html().replace(/ - /g,'/').replace('/Scholastic Australia', '');
    if (context == 'production') {
        pageTracker._trackEvent(section, sec, href);
    }
}

$(document).ready(function() {
    $('#channel-header').pngFix();
    // Add round corners
    $(".round").corner("round 7px");

    $('#tabs').tabs();

    // Attach superfish to navs.
    $('#nv').superfish();
    $('#menu').superfish();
    $('#nv-account').superfish();

    $('#nv-country > li.sub').hide();

    $('#nv-country').hoverIntent(
        function () {
            $('#nv-country > li.sub').slideDown();
        },
        function () {
            $('#nv-country > li.sub').slideUp();
        }
    );

    // Switch alt div colors.
    $(".alt:odd").css({
        'background-color': '#D2ECF9',
        'padding': '10px',
        'margin': '4px'
    });

    $(".alt:even").css({
        'background-color': '#BEDEF3',
        'padding': '10px',
        'margin': '4px'
    });

    // Overlay helper.
    $('.overlay').click(function() {
        // If a dialog is already open, close it.
        if ($('.dialog').length > 0) {
            $('.dialog').dialog('close');
        }

        // If not an image, set title to innerHTML of clicked element.
        var title = '';
        if ($(this).html().indexOf('img') == -1) {
            title = $(this).html();
        }
        // Get the name of the file used to fill
        // the dialog with content.
        var file = $(this).attr('rel');
        // Create the empty div to store the dialog content in.
        var $dialog = $('<div class="dialog"></div>')
        // Load the content into the div.
        .load(file)
        // Initialize the dialog.
	.dialog({
            autoOpen: false,
            resizable: false,
            height: 472,
            zIndex: 40000,
            //modal: true,
            width: 792,
            title: title,
            // Upon closing a dialog, destroy it!
            close: function() {
                if (context == 'production') {
                    if (file.indexOf('movies') != -1) {
                        pageTracker._trackEvent('movie-overlay', 'close', file);
                    } else {
                        pageTracker._trackEvent('content-overlay', 'close', file);
                    }
                }
                $('.dialog').remove();
            },
            open: function() {
                if (context == 'production') {
                    if (file.indexOf('movies') != -1) {
                        pageTracker._trackEvent('movie-overlay', 'open', file);
                    } else {
                        pageTracker._trackEvent('content-overlay', 'open', file);
                    }
                }
            }
        });
        // Open the new dialog.
        $dialog.dialog('open');
    });

    // zIndex IE fix.
    var zIndexNumber = 20000;
    $('div').each(function() {
        $(this).css('zIndex', zIndexNumber);
        zIndexNumber -= 10;
    });

    // Ensure all tiles that contain links
    // have the red arrow attached to there footer.
    $('.tile-body').each(function() {
        var content = $(this).html();
        content = content.toLowerCase().replace(/^\s+|\s+$/g, '').substr(0, 2);
        if (content  == '<a') {
            $(this).next().removeClass('tile-footer').addClass('tile-footer-arrow');
        }
    });

    // ensure certain links open in new windows.
    $('a[href^="http"]').attr('target','_blank');
    $('a[href^="https"]').attr('target','_blank');
    $('a[href$="pdf"]').attr('target','_blank');
    $('a[href$="doc"]').attr('target','_blank');
    $('a[href$="jpg"]').attr('target','_blank');
    $('area[href^="http"]').attr('target','_blank');
    $('area[href^="https"]').attr('target','_blank');
    $('area[href$="pdf"]').attr('target','_blank');
    $('area[href$="doc"]').attr('target','_blank');
    $('area[href$="jpg"]').attr('target','_blank');

    $('a[href$="pdf"]').click(function() {getSection(this, 'pdf');});
    $('a[href$="doc"]').click(function() {getSection(this, 'doc')});
    $('a[href$="xls"]').click(function() {getSection(this, 'xls')});

    // Create movie image overlays.
    $(".movie-hover").hover(
        function() {
            var $current = $(this).find("img");
            var pos = $current.position();
            var top = pos.top;
            var left = pos.left;
            var $img = $(
                '<img id="playbtn" src="/assets/img/140x90/play-button.png" />'
            ).css({
                'position': 'absolute',
                'top': top,
                'left': left,
                'z-index': '100000'
            })
            $current.before($img);
        },
        function() {
            $('#playbtn').remove();
        }
    );

    $('#nv-country').css('visibility','visible');

});
