﻿///  <reference path="../Shared/scripts/jquery-1.3.1-vsdoc.js" />
function log(l) { if (typeof console != "undefined" && typeof console.debug != "undefined") { console.log(l); } else { alert(l); } }

if ($.browser.msie && parseInt($.browser.version) <= 6) {
    $('html').addClass('IE6');
}

var content_OldTitle, content_OldBody;
var content_EditingID = '';

var currentPhoto = 0;
function onPhotoChange(value) {
    $('#Photo' + currentPhoto).addClass('TagCollapse')
    currentPhoto = value;
    $('#Photo' + currentPhoto).removeClass('TagCollapse');
}
function onAlbumChange(value) {
    aj_GetAlbum('GetAlbumData⌂' + value, '');
}
function aj_ReceiveAlbum(rValue) {
    $("#divPhotoContent").html(rValue);
    $('#Photo' + currentPhoto).removeClass('TagCollapse');
}
//-----------

//-- MS-AJAX STUFF ---------
function CallBackError(arg, context) {
    alert('An error has occurred.');
}
function aj_ReceiveOk(rValue) {
    if (rValue == "reload")
        window.location.href = window.location.href;
}
function aj_ReceiveData(rValue) {
    $("#TeamMessages").html(rValue.split('⌂')[0]);
    Content_allowAdd();
    if (rValue.split('⌂')[1] > 0) {
        Content_Edit(rValue.split('⌂')[1]);
        $('#Content_' + rValue.split('⌂')[1]).removeClass('Collapsed');
    }
}
//-----------
var editor, tAS;
function loadCKEDITOR(num) {
    CKEDITOR.basePath = '../ckeditor/';
    content_OldBody = $("#ContentBody" + num).html();
    $("#ContentBody" + num).html('');
    editor = CKEDITOR.appendTo('ContentBody' + num,{
    toolbar: 'ANETth'
    });
    editor.setData(content_OldBody);
}
function Content_AutoSave(num) {
    if (editor !== null && editor.checkDirty()) {
        aj_SendData('SaveContent⌂' + num + '⌂' + $("#EditTitle").val() + '⌂' + editor.getData(), '');
        editor.resetDirty();
    }
    if (editor !== null) {
        tAS = setTimeout("Content_AutoSave(" + num + ")", 120000);
    }
}
function Content_Edit(num) {
    loadCKEDITOR(num);
    content_OldTitle = $("#ContentTitle" + num).html()
    $('#ContentTitle' + num).html('<input id="EditTitle" type="text" maxlength="50" style="width:80%;font-size:larger;font-weight:bold;" value="' + content_OldTitle + '">'); //.bind('click', function(e) { e.stopPropagation() });
    $('#Content_' + num + ' span.ContentTools_inEditing').show();
    $('#Content_' + num + ' span.ContentTools_notEditing').hide();
    content_EditingID = num;
    setTimeout("Content_AutoSave(" + num + ")", 120000);
}
function Content_Save(num) {
    if (editor.checkDirty() || $("#EditTitle").val() != content_OldTitle)
        aj_SendData('SaveContent⌂' + num + '⌂' + $("#EditTitle").val() + '⌂' + editor.getData(), '');
    $("#ContentBody" + num).html(editor.getData());
    $("#ContentTitle" + num).html($("#EditTitle").val());
    $('#Content_' + num + ' span.ContentTools_inEditing').hide();
    $('#Content_' + num + ' span.ContentTools_notEditing').show();
    content_EditingID = '';
    editor.destroy();
    editor = null;
    clearTimeout(tAS);
}
function Content_Cancel(num) {
    aj_SendData('SaveContent⌂' + num + '⌂' + content_OldTitle + '⌂' + content_OldBody, '');
    $("#ContentBody" + num).html(content_OldBody);
    $("#ContentTitle" + num).html(content_OldTitle);
    $('#Content_' + num + ' span.ContentTools_inEditing').hide();
    $('#Content_' + num + ' span.ContentTools_notEditing').show();
    content_EditingID = '';
    editor.destroy();
    editor = null;
    clearTimeout(tAS);
}
function Content_allowAdd() {
    if ($('.MSG_Content').length > 10) {
        $('#ContentAdd_container').html('You have already added 10 Team Message boxes - you cannot add more.');
    }
    else {
        $('#ContentAdd_container').html('<a class="ContentAdd_click" href="#">Add a Team Message box above.</a> (You can add ' + (10 - $('.MSG_Content').length) + ' more).');
    }
}
function contentClicks() {
    $('span.ContentCancel_click').live('click', function() {
        Content_Cancel($(this).attr('id').substr(14));
    });
    $('span.ContentSave_click').live('click', function() {
        Content_Save($(this).attr('id').substr(12));
    });
    $('a.ContentEdit_click').live('click', function(e) {
        Content_Edit($(this).attr('id').substr(12));
        e.stopImmediatePropagation();
    });
    $('span.ContentUp_click').live('click', function() {
        aj_SendAndReceiveData('MoveContent⌂' + $(this).attr('id').substr(10) + '⌂-1', '');
    });
    $('span.ContentDn_click').live('click', function() {
        aj_SendAndReceiveData('MoveContent⌂' + $(this).attr('id').substr(10) + '⌂1', '');
    });
    $('a.ContentEnable_click').live('click', function() {
        aj_SendAndReceiveData('ToggleVisibility⌂' + $(this).attr('id').substr(14), '');
    });
    $('a.ContentDisabl_click').live('click', function() {
        aj_SendAndReceiveData('ToggleVisibility⌂' + $(this).attr('id').substr(14), '');
    });
    $('span.ContentDelete_click').live('click', function() {
        var x = window.confirm("Are you sure you want to permanently delete this content? \n \nWARNING: Cannot be undone! \n \nNote that you can use the 'Disable' function to hide your content from viewers instead of deleting it.")
        if (x) {
            aj_SendData('DeleteContent⌂' + $(this).attr('id').substr(14), '');
            clearTimeout(tAS);
        }
    });
}
function CSFtabs() {
    $('span.tabHeader').live('click', function() {
        var showing = $('#tab_' + $(this).text()).show();
        $('div.tab').not(showing).hide();
        $(this).addClass('selectedTab');
        $('span.tabHeader').not($(this)).removeClass('selectedTab');
    });
}
function gotoCal() {
    var calAnchor = document.location.toString().split('#S=')[1];
    if ($('#S' + calAnchor).length > 0) {
        $('table.CalSeason:visible').addClass('CollapsedCal');
        $('#S' + calAnchor).removeClass('CollapsedCal');
        updateSelSeason(calAnchor);
    }
}
function updateSelSeason(SeasonSelected) {
    $('#CalYears li').removeClass('cySelected');
    if (SeasonSelected.substr(0, 1) == 1) {
        $('ul.I').children(':contains(' + SeasonSelected.substr(2, 4) + ')').addClass('cySelected');
    }
    else {
        $('ul.O').children(':contains(' + SeasonSelected.substr(1, 4) + ')').addClass('cySelected');
    }
    document.location.hash = "S=" + SeasonSelected;
};
function Events_Expander() {
    $('div.ContentHeaderExpander.click').live('click', function() {
        var $ContentBlock = $(this).parent();
        if (content_EditingID == '') {
            if ($ContentBlock.hasClass('AccLocked')) {
                $ContentBlock.toggleClass('Collapsed');
                $ContentBlock.siblings('.AccLocked').addClass('Collapsed');
            }
            else {
                $ContentBlock.toggleClass('Collapsed');
            }
        }
    });
    $('span.EditHeader .pD').live('click', function(e) {
        e.preventDefault();
    });
    if (!(typeof html_Editing === "undefined")) {
        contentClicks();
    }
}
function Events_Calendar() {
    $('a.nextSeason_Click').live('click', function(e) {
        $(this).parents('.CalSeason').addClass('CollapsedCal').prev().removeClass('CollapsedCal');
        updateSelSeason($(this).parents('table.CalSeason').prev().attr('id').substr(1, 5));
        e.preventDefault();
    });
    $('a.prevSeason_Click').live('click', function(e) {
    $   (this).parents('table.CalSeason').addClass('CollapsedCal').next().removeClass('CollapsedCal');
        updateSelSeason($(this).parents('table.CalSeason').next().attr('id').substr(1, 5));
        e.preventDefault();
    });
    $('#Calendar table tr > td').live('click', function(e) {
        var $CalTarget = $(e.target);
        if ($CalTarget.hasClass('MeetLink')) {
            var $ThisMeetHeader = $CalTarget.parents('tr').toggleClass('CollapsedMH');
            var $ThisMeetInfo = $CalTarget.parents('tr').next('tr.MeetInfo').toggleClass('CollapsedMI');
            $('tr.MH').not($ThisMeetHeader).addClass('CollapsedMH');
            $('tr.MeetInfo').not($ThisMeetInfo).addClass('CollapsedMI');
            e.preventDefault();
        }
    });
    $('#CalYears li').live('click', function() {
        $('table.CalSeason').addClass('CollapsedCal')
        $('#CalYears li').removeClass('cySelected');
        if ($(this).parent().hasClass('O')) {
            $('#S' + $(this).text()).removeClass('CollapsedCal');
            updateSelSeason($(this).text());
        }
        else {
            $('#S1' + $(this).text()).removeClass('CollapsedCal');
            updateSelSeason('1' + $(this).text());
        }
    });
    $('a.MoreYears_click').live('click', function(e) {
        var left = $(this).position().left + 10;
        var top = $(this).position().top;
        $(this).next().next().css({ left: left, top: top }).animate({ opacity: 'toggle', height: 'toggle' }, 'fast');
        e.preventDefault();
    });
    $('div.MoreYears').hover(function() { }, function() { $(this).animate({ opacity: 'hide', height: 'hide' }, 'fast') })
    $('#ShowHideAllCal').toggle(function(e) {
        e.preventDefault();
        $('table.CalSeason').removeClass('CollapsedCal');
    }, function(e) {
        e.preventDefault();
        $('table.CalSeason').addClass('CollapsedCal');
        $('table.CalSeason:eq(0)').removeClass('CollapsedCal');
    });
};
function WindowSizer() {
    if ($(window).width() < 771) {
        $('#RightColumn').addClass('small')
    }
    else {
        $('#RightColumn').removeClass('small');
    }
};
function Events_Other() {
    $(window).resize(function() {
        WindowSizer();
    });
    if ($.browser.msie) {
        if (parseInt($.browser.version) <= 6) {
            $('#CalYears li').hover(
                    function() {
                        $(this).addClass('Hover');
                    },
                    function() {
                        $(this).removeClass('Hover');
                    });
            $('span.EditHeader').hover(
                    function() {
                        $(this).addClass('Hover');
                    },
                    function() {
                        $(this).removeClass('Hover');
                    });
        }
    }
    $('a.Map_click').live('click', function(e) {
        var left = $(this).position().left + 10;
        var top = $(this).position().top;
        $('#G_saddr').val(OrigZipCode);
        $('#G_daddr').val($(this).attr('title'));
        $('#GetZipGoogleMap').css({ left: left, top: top }).insertAfter($(this)).show()
        e.preventDefault();
    });
    $('#GetZipGoogleMap').hover(function() { }, function() { $(this).hide() });
    Content_allowAdd();
    $('.ContentAdd_click').live('click', function(e) {
        aj_SendAndReceiveData('AddContent⌂109⌂Click to Edit⌂ ', '');
        e.preventDefault();
    });
};
function showPrepSports() {    
    if (html_ShowStore) {
        $('#PrepSportsBlock .ContentInner').html('<iframe id="iframePrepSportswear" src="../Partner/PrepSportswear/Show.aspx?SchoolID=' + html_IDSchool + '&Sport=XC' + '" frameborder="0" height="200" width="370"></iframe>');
        $('#PrepSportsBlock').show();
    }
}
function PageHelp() {
    $('#click_PageHelp').click(function() {
        $('#MoreHelpOptions').hide();
        $('#body_PageHelp').animate({ width: 'toggle' }, 'fast');
    });
    $('#click_MoreHelpOptions').click(function() {
        $('#MoreHelpOptions').slideToggle('fast');
    });
}
$('#tab_Calendar').ready(function() {
    Events_Calendar();
    $('table.CalSeason:first a.nextSeason_Click, table.CalSeason:last a.prevSeason_Click').unbind('click');
});

$(function() {
    WindowSizer();
    gotoCal();
    Events_Expander();
    Events_Other();
    CSFtabs();
    PageHelp()
    setTimeout("showPrepSports();$.getScript('../Shared/scripts/jquery.thickbox-compressed.js');$('<link>').appendTo($('head')).attr({ rel: 'stylesheet', type: 'text/css', href: '../Shared/styles/jquery.thickbox.css' });", 100);
});
