
//this function is called when date is selected in calendar
function onDateChange(sender, selectedDate) {
	var month = selectedDate.getMonth();
	var day = selectedDate.getDate() - 1;
	var year = selectedDate.getFullYear() - 1999;
	
	document.getElementById("selMonth").options[month].selected = true;
	document.getElementById("selDay").options[day].selected = true;
	document.getElementById("selYear").options[year].selected = true;
}

//called when one of the comboboxes is changed
function selectDate() {
	var month = document.getElementById("selMonth").selectedIndex;
	var day = document.getElementById("selDay").selectedIndex;
	var year = document.getElementById("selYear").selectedIndex;
	
	var selectedDate = new Date(year + 1999, month, day + 1);
	
	calDate.setDate(selectedDate, selectedDate);
}

function fctConfirmAction(strParam) {              
    var strConfirm
    
    switch (strParam) {
    case "DeleteEvent": 
    strConfirm = "This action will delete this event permanently. Are you sure you want to do this?";
    break;
    case "DeleteDocument": 
    strConfirm = "This action will delete this document permanently. Are you sure you want to do this?";
    break;
    case "DeleteSubCategory": 
    strConfirm = "This action will delete this sub-category permanently. No events will be lost. Are you sure you want to do this?";
    break;
    case "DeleteInvitation": 
    strConfirm = "This action will delete this invitation permanently. Are you sure you want to do this?";
    break;
    case "RejectInvitation": 
    strConfirm = "You are about to reject this invitation. Once rejected, this invitation will be deleted permanently. Are you sure you want to do this?";
    break;
    case "RemoveFriend": 
    strConfirm = "You are about to remove a friend from your friend list. Once removed, he or she won't be able to access your account and vice-versa. Are you sure you want to do this?";
    break;
    case "leave": 
    strConfirm = "You are about to leave this website. Are you sure you want to do this?";
    break;
    default: strConfirm = "Are you sure you want to do this?";
    }
    return confirm(strConfirm);
}

function slUpload() {
    $.get('/mlhoe-ajax.aspx', { 'function': 'LastDocType' }, function (data) {
        $.query = $.query.load(location.href);
        location.href = "/manager.aspx?strFunction=DisplayEvent&lngEventId=" + $.query.get('lngEventId') + "&strDocType=" + data;
    });
}

function clickSwitch(linker,switchbox)
{
    box = document.getElementById(switchbox);
    link = document.getElementById(linker);
    box.style.display=box.style.display=='none'?'':'none';
}

function transcodeCheck(elid) {
    // Check if the transcode has finished.
    $.get('/mlhoe-ajax.aspx', {
        'function': 'CheckDocReady',
        'DocId': $('#' + elid).attr('rel')
    }, function (data) {
        if (data == 0) {
            // Get the correct display for the event
            $.get('/mlhoe-ajax.aspx', {
                'function': 'GetDocDisplay', 'DocID': $('#' + elid).attr('rel')
            }, function (data) {
                $('#' + elid).replaceWith(data);
            });


            //            $this = $('#' + elid);

            //            // Generate the URL for the thumbnail
            //            urlImg = '/mlhoe-draw-thumbnail.aspx?strFileName=thumb-video-';
            //            urlImg += $this.parent().attr('rel') + '-' + $this.attr('rel') + '.jpg'

            //            // Update the img src and add the link around it.
            //            $this.find('img').attr('src', urlImg).addClass('pic-event-thumb').wrap('<a href="manager.aspx?strFunction=DisplayEvent&lngEventId=' + $this.parent().attr('rel') + '&strDocType=TVideo&lngDocumentId=' + $this.attr('rel') + '" title="" />');
            //            

        } else {
            // Only try if it belongs to the user.
            if (data != -99) {
                setTimeout('transcodeCheck("' + elid + '")', 15000);
            }
        }
    });
}

var pageUnsaved = false;
var pageSortable;

$(function () {
    // Check for "transcoding" records
    $('div.EVvideo').each(function () {
        if ($(this).find('a').size() == 0) {
            tempid = "VidTemp" + $(this).attr('rel');
            $(this).attr('id',tempid);
            setTimeout('transcodeCheck("'+tempid+'")', 15000);
        }
    });

    // Changes and Colorbox handler for images
    $('.EVimage img.event-thumb').parent().each(function () {
        $this = $(this);

        // Change the link for colorbox if JavaScript is enabled.
        $.query = $.query.load($this.attr('href'));
        //mlhoe.nocturnmedia.com/mlhoe-gallery-ajax.aspx?function=CBImage&lngEventId=3584&strDocType=TImage&lngDocumentId=5531

        $this.attr('href', '/mlhoe-gallery-ajax.aspx?function=CBImage&lngEventId=' + $.query.get('lngEventId') + '&strDocType=TImage&lngDocumentId=' + $this.parent().parent().attr("rel"));

        // Make the other "View" link work properly.
        $this.parent().parent().find('a.EVview').click(function (evt) {
            evt.preventDefault();
            $(this).parent().parent().parent().find('a.cb').click();
        });
    });

    $.ajaxSetup({ error: function (req, stat, err) { alert(stat); } });

    // If there are items with .cb, set up the colorbox gallery.
    if ($('.cb').size()) {
        $('.cb').colorbox({ current: "{current} of {total}", height: "552px", title: function () {
            // <a href='/display-original-pictures.aspx?lngDocumentId=" + dsRow.Item("intDocumentId").ToString + "' target='_blank'>Display Original Picture</a>
            $.query = $.query.load($(this).attr("href"));
            return '<a href="/display-original-pictures.aspx?lngDocumentId=' + $.query.get("lngDocumentId") + '" target="_blank">Display Original Picture</a>';
        }
        });
    }
    $('.cb_gonext').live('click', function (evt) {
        evt.preventDefault();
        $.fn.colorbox.next();
    });

    // Check for hidden notices that apply only when there is JavaScript
    if ($('.js_only').size()) {
        $('.js_only').attr("style", "");
    }

    // Sortable handler for images & videos
    if ($('.EVsort').size()) {
        $('.EVsort').sortable({ stop: function (event, ui) {
            var sortList = "";
            $('.EVsort .EVbox').each(function () { sortList += $(this).attr('rel') + ","; });
            $.get('/mlhoe-ajax.aspx', { 'function': 'DocSort',
                'EventId': $('.EVsort').attr('rel'),
                'SortList': sortList.substring(0, sortList.length - 1)
            });
        }
        });

        // prevent the items from being selected (so you can drag them)
        $('.EVsort').disableSelection();

        // Reverse order link handler
        $('#sortable_reverse').click(function (evt) {
            evt.preventDefault();
            // Disable the sorting temporarily
            $('.EVsort').sortable("disable");

            // Set the ID
            $('.EVsort .EVbox').each(function () { $(this).attr("id", "EVsort_" + $(this).attr("rel")) });

            // Get the order
            var sort_order;
            sort_order = $('.EVsort').sortable("toArray");
            sort_order.reverse();

            // Move the blocks
            $.each(sort_order, function (key, val) {
                $("#" + this).detach().appendTo('.EVsort');
            });

            $('.EVsort').sortable("enable");
            $('.EVsort').sortable("refresh");

            // Save off the reordered list
            var sortList = "";
            $('.EVsort .EVbox').each(function () { sortList += $(this).attr('rel') + ","; });
            $.get('/mlhoe-ajax.aspx', { 'function': 'DocSort',
                'EventId': $('.EVsort').attr('rel'),
                'SortList': sortList.substring(0, sortList.length - 1)
            });
        });

    }

});
