// Default JS for World Skills by Mark Boulton Design

$(function () {

    $('.tabbed').not('.tabbed:first').hide();

    $('.tabs a').bind('click', function () {

        $('.tabs a').removeClass('on');

        $(this).addClass('on')
			.parents('.tabs').parent().find('.tabbed').hide();

        $('#' + $(this).attr('rel')).show();

    });

    $('.sitemap li ul').hide();

    $('.sitemap .more').bind('click', function () {

        if ($(this).find('span').attr('class').match('down')) $(this).html('Hide All Teams <span class="up">up</span>')
																	.siblings('ul').show();

        else $(this).html('Show All Teams <span class="down">down</span>')
					.siblings('ul').hide();

    });

    $('#promonav a:first').addClass('on');

    $('#promonav a').bind('click', function () {

        $('#promonav a').removeClass('on');

        $(this).addClass('on')
			.parents('#promoarea').parent().find('.promo').hide();

        $('#promo' + $(this).attr('rel')).show();

    });

    $("a[rel=photos]").fancybox({
        'padding': 1,
        'cyclic': true,
        'centerOnScroll': true,
        'overlayOpacity': '0.7',
        'overlayColor': '#999',
        'titleFormat': function (title, currentArray, currentIndex, currentOpts) {
            return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
        }
    });

    $("a[rel=videos]").click(function () {
        $.fancybox({
            'padding': 1,
            'cyclic': true,
            'centerOnScroll': true,
            'overlayOpacity': '0.7',
            'overlayColor': '#999',
            'width': 640,
            'height': 385,
            'href': this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
            'type': 'swf',
            'swf': {
                'wmode': 'transparent',
                'allowfullscreen': 'true'
            }
        });
        return false;
    });

    $("#emailbutton").fancybox({
        'padding': 1,
        'cyclic': true,
        'centerOnScroll': true,
        'overlayOpacity': '0.7',
        'overlayColor': '#999'
    });

    $('input, textarea').bind('focus', function () { clearfield(this); }).bind('blur', function () { unclearfield(this); });

    $('#mobilenav').change(function () {
        window.location = $(this).val();
    });

    // Schedule JS

    $('#schedule input').bind('click', function () {

        $('#schedule tr').not('.day').removeClass('selected');
        $('#schedule :checked').parents('tr').addClass('selected');

    });

    $('#schedule tbody, #schedule thead tr + tr').hide();

    $('#schedule .tabletitle').each(function () {
        $(this).addClass('show').html('<span class="down">Down</span>View ' + $(this).html());
    });

    $('#schedule .tabletitle').click(function () {

        if ($(this).attr('class').match('show')) $(this).removeClass('show').html($(this).html().replace('<span class="down">Down</span>View ', '<span class="up">Up</span>')).parents('table').find('tbody, thead tr + tr').show();
        else $(this).addClass('show').html($(this).html().replace('<span class="up">Up</span>', '<span class="down">Down</span>View ')).parents('table').find('tbody, thead tr + tr').hide();

    });

    $('#topic').change(function () {

        if ($(this).val() != '') {
            $('#schedule table').not('#general').hide();
            $('#general .tabletitle').html($(this).val() + ' Events');
        }
        else {
            $('#schedule table').show();
            $('#general .tabletitle').html('General Events');
        }

    });

    // Map JS

    window.onload = maplinks;
    window.onresize = maplinks;

    $('area').bind('mouseover', function (e) { tip(e); }).each(function () {

        // add ocoords for calculating resized coordinates
        $(this).attr('ocoords', $(this).attr('coords'));

    });

    $('#legend a').click(function () {

        var t = $($(this).attr('href')), p = $(t).attr('coords').split(',');

        tip(false, t);

        window.scrollTo(p[0] - 100, p[1] - 100);

    });

});

tip = function (e, t) {

    var t = e.target || t,
	p = $(t).attr('coords').split(','), tid = 't' + $(t).attr('id'), d = '<div id="' + tid + '" class="tip" style="top: ' + (parseInt(p[1]) - 15) + 'px; left: ' + (p[0] - 10) + 'px">';

    if ($(t).attr('alt')) {

        if ($(t).attr('href')) {
            if ($(t).attr('href') != 'javascript:') d += '<a href="' + $(t).attr('href') + '"><span><strong>' + $(t).attr('id').replace(/[a-zA-z]+/, '') + '</strong> ' + $(t).attr('alt') + '</span>More info</a></div>';
            else d += '<span><strong>' + $(t).attr('id').replace(/[a-zA-z]+/, '') + '</strong> ' + $(t).attr('alt') + '</span></div>';
        }
        else d += '<span>' + $(t).attr('alt') + '</span></div>';

        if (e) {

            $(document).unbind('mouseover').bind('mouseover', function (e) {

                var e = $(e.target);

                if (!e.parents('#' + tid)[0] && e[0].id != tid && e[0].id != tid.replace('t', '')) $('#' + tid).remove();

            });

            if (e.preventDefault) e.preventDefault();
            else event.returnValue = false;

        }
        else {

            $(document).add('area').unbind('mouseover');

            setTimeout(function () {

                $('area').bind('mouseover', function (e) { tip(e); });

            }, 1000);

        }

        if (!$('#' + tid).length) $('#map').append(d);

    }

    $('.tip').not('#' + tid).remove();

};

maplinks = function () {

    $('area').each(function () {

        var c = $(this).attr('ocoords').split(','), x = c.length;

        while (x-- > 0) {

            if (x % 2 == 0) c[x] = Math.round(c[x] = (parseInt($('#map img').height()) / 982) * parseInt(c[x]));
            else c[x] = Math.round((parseInt($('#map img').width()) / 2000) * parseInt(c[x]));

        }

        $(this).attr('coords', c);

    });

};

clearfield = function (t) {

    if ($(t).val() == $(t).attr('title')) $(t).val('');

}

unclearfield = function (t) {

    if ($(t).val() == $(t).attr('title') || $(t).val() == '') $(t).val($(t).attr('title'));

}
