﻿
/* Collapsing regions */

function toggleCollapsedRegion(el, command) {
    switch (command) {
        case 'collapse':
            if ($(el).hasClass('open')) {
                $(el).toggleClass('open');
                $(el).find('.content').animate({ height: 'toggle' }, 900, 'easeInOutExpo');
                $(el).find('.head .status').html('+');
            }
            break;
        case 'expand':
            if (!$(el).hasClass('open')) {
                $(el).toggleClass('open');
                $(el).find('.content').animate({ height: 'toggle' }, 900, 'easeInOutExpo');

                $(el).find('.head .status').html('-');
            }
            break;
        case 'expand-scroll':
            scrollDelay = 0;
            if (!$(el).hasClass('open')) {
                scrollDelay = 1400;
                $(el).toggleClass('open');
                $(el).find('.content').animate({ height: 'toggle' }, 900, 'easeInOutExpo');

                $(el).find('.head .status').html('-');
            }
            scrollToEl = el;
            setTimeout('$(document).scrollTo(scrollToEl, 1200, { offset:{ top:-10, left:0 }});', scrollDelay);
            break;
        default:
            $(el).toggleClass('open');
            $(el).find('.content').animate({ height: 'toggle' }, 900, 'easeInOutExpo');

            if ($(el).hasClass('open')) {
                $(el).find('.head .status').html('-');
                if (command == 'scroll') {
                    scrollDelay = 500;
                    scrollToEl = el;
                    setTimeout('$(document).scrollTo(scrollToEl, 1200, { offset:{ top:-10, left:0 }});', scrollDelay);
                }
            }
            else {
                $(el).find('.head .status').html('+');
            }
    }
}
function toggleAllCollapsedRegions(el, command) {
    $('.collapse').each(function () {
        toggleCollapsedRegion(this, command);
    });
}

$(document).ready(function () {
    //Add collapse auxilliary controls
    $('.expandIndex a').click(function (event) {
        event.preventDefault();
        var collapseIndex = parseInt($(this)[0].id.split('_')[1]) - 1;
        toggleCollapsedRegion($('.collapse')[collapseIndex], 'expand-scroll');
    });
    $('.expandAll').click(function (event) {
        event.preventDefault();
        var expandAllLink = $(this).find('a');
        var command = '';
        if (expandAllLink.html() == 'Expand All') {
            command = 'expand';
            expandAllLink.html('Collapse All');
        }
        else {
            command = 'collapse';
            expandAllLink.html('Expand All');
        }
        toggleAllCollapsedRegions(this, command);

    });

    //initialise collapsable content regions
    $('.collapse').each(function () {
        //Add styling markup
        var corners = '<span class="left-end"></span>'
                + '<span class="right-end"></span>';
        var status = '<span class="status">+</span>';
        $(this).find('.head').prepend(corners).append(status);
        $(this).find('.content').wrapInner('<div class="inner" />');
    });
    $('.collapse .content').hide();
    $('.collapse .head').click(function () {
        toggleCollapsedRegion($(this).parents('.collapse')[0], 'scroll');
    });

    //Setup scrolling on next, prev, top buttons
    $('.top').click(function (event) {
        event.preventDefault();
        $(document).scrollTo(0, 1200);
    });
});

/* End Collapsing Regions */

/* Error style */

(function($) {
    $.fn.errorStyle = function(message) {
        var errorText = message || this.html();
        var errorID = this[0].id;
        var textID = '';
        if (errorID) errorID = 'id="' + errorID + '"';
        if (errorID) textID = 'id="' + errorID + '-text"';
        var styleError = "<div " + errorID + " class=\"ui-state-error ui-corner-all\" style=\"padding: 0 .7em;\">";
            styleError += "<p><span class=\"ui-icon ui-icon-alert\" style=\"float: left; margin-right: .3em;\">";
            styleError += "</span><strong>Alert: </strong>";
            styleError += errorText;
            styleError += "</p></div>";
			this.replaceWith(styleError);
    }
})(jQuery);

/* End Error Style */

/* Login */

function post_to_url(path, params, method) { 
    method = method || "post"; // Set method to post by default, if not specified. 
 
    // The rest of this code assumes you are not using a library. 
    // It can be made less wordy if you use one. 
    var form = document.createElement("form"); 
    form.setAttribute("method", method); 
    form.setAttribute("action", path); 
 
    for (var key in params) { 
        var hiddenField = document.createElement("input"); 
        hiddenField.setAttribute("type", "hidden"); 
        hiddenField.setAttribute("name", key); 
        hiddenField.setAttribute("value", params[key]); 
 
        form.appendChild(hiddenField); 
    } 
 
    document.body.appendChild(form);    // Not entirely sure if this is necessary 
    form.submit(); 
}
