﻿
// copyright playface limited 2010.
// common jquery based javascript routines

$(window).load(function() {

});

// global document ready - include generally used routines
$(document).ready(function(){
    
    // render our datepicker ui widget
    $('.datepicker').datepicker({ dateFormat: 'dd/mm/yy' });
    
    // expander for team member invites
    $("a.invite-teamperson-members-toggle").click(function() {
        // odd clicks - expand, and set content to be shown
        if($("#itmc-" + $(this).attr("teamid")).is(':hidden')) {
            // show
            $("#itmc-" + $(this).attr("teamid")).show('blind', { direction: 'vertical' }, 1000);
            $(this).html('Hide Team Members');
        } else {
            // hide 
            $("#itmc-" + $(this).attr("teamid")).hide('blind', { direction: 'vertical' }, 1000);
            $(this).html('Show Team Members');
        }
        // make sure link is not followed
        return false;
    });

    // click action to submit a form
    $('#invite-form-submit').click(function() { $('#invite-form').submit(); });

    // code for handling invitiation popups
    $("#invites-cont a").click(function() {
        
        // destroy any existing dialog to get round IE issues
        $("#dialog_modal_popup").dialog("destroy");
        
        // setup our modal popup container
        $("#dialog_modal_popup").dialog({
            autoOpen: false,
            draggable: false,
            modal: true,
            position: ['center', 125],
            resizable: false,
            title: 'Invitiations',
            width: 390
        });
        
        // open dialog box and show loading
        $('#dialog_loading').show();
        $('#dialog_modal_popup').dialog('open');
        
        // load remote content in to popup, will over-write loading
        $('#dialog_modal_popup').load('/invitation/index', function() {
            
        });
        //prevent the browser to follow the link
        return false;
    });

    
    // invite everyone in team
    $('input.invite-teamperson-members-state').change(function () {
        if ($(this).attr("checked")) {
            // check all checkboxes
            $("#itmc-" + $(this).attr("teamid") + " input[type='checkbox']").attr('checked', true);
            // also show the people if the object is currently hidden
            $("#itmc-" + $(this).attr("teamid") +":hidden").show('blind', { direction: 'vertical' }, 1000);
            $(this).parent().find('a.invite-teamperson-members-toggle').html('Hide Team Members');
        } else {
            // uncheck all checkboxes
            $("#itmc-" + $(this).attr("teamid") + " input[type='checkbox']").attr('checked', false);
        }
    });
});

// Jcrop event handlers to input data in to a form
function jcropShowCoords(c)
{
    $('#cropbox-x').val(c.x);
    $('#cropbox-y').val(c.y);
    $('#cropbox-w').val(c.w);
    $('#cropbox-h').val(c.h);
};

// jQuery spesific functions
// image pre-loading
(function($) {
    var cache = [];
    $.preLoadImages = function() {
        var args_len = arguments.length;
        for (var i = args_len; i--;) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }
})(jQuery);

// Playface functions
function pfShowError(message) {
    // show an error dialog, first purge dialog
    $("#dialog_modal_popup").dialog("destroy");
    // render error
    $("#dialog_modal_popup").attr('title', 'Error');
    $("#dialog_modal_popup").text('Unable to complete request: ' + message);
    // create popup
    $("#dialog_modal_popup").dialog({
        draggable: false,
        modal: true,
        position: 'center',
        resizable: false,
        width: 390,
        buttons: {
            "Close": function() {
                // close dialog and ignore request
                $(this).dialog('close');
            }
        }
    });
}