var Global = {
	getSpinner: function(color){
		var spinners = {
			orange: '<img src="/img/ui/loading-orange.gif" width="12" height="12" />',
			gray: '<img src="/img/ui/loading-gray.gif" width="12" height="12" />'
		}		
		return spinners[color];		
	}
};

$(document).ready(function() {
    $('input.fancy-label').field();
    $('textarea.fancy-label').field();
    $('div.fancy-tab').tab();
    $('input.fancy-field').input();
    $('input.fancy').field().input();
    $('input[type="checkbox"]').checkbox();

    $('textarea, input').fancyFocus();
	
	
	$('.unavailable').click(function() {
        alert('This feature is currently unavailable for our initial beta release. Please check back over the next few days as we further develop the site.');
        return false;
    });

    $('span.link').click(function() {
        if ($(this).attr('link') != '') {
            window.location = $(this).attr('link');
        }

        return false;
    });

    $('.help-popup .close-help-popup').click(function(){
        $(this).parent().fadeOut();
        return false;
    });

});

$.fn.fancyFocus = function(){
    return this.focus(function(){
        $(this).addClass('focus').parent().addClass('focus');
    }).blur(function(){
        $(this).addClass('focus').parent().removeClass('focus');
    })    
    return this;
};
 
$.fn.field = function() {
    $(this).siblings('label').addClass('fancy');
    $(this).css({color: '#999999'});

    return this.focus(function() {
        if ($(this).val() == this.defaultValue) {
            $(this).val('');
            $(this).css({color: '#333333'});
        }
    }).blur(function() {
        if (!$(this).val().length) {
            $(this).val(this.defaultValue);
            $(this).css({color: '#999999'});
        }
    });
    
    return this;
};

$.fn.input = function() {
    $(this).parent().addClass('fancy');
    return $(this).focus(function(){
        $(this).addClass('focus');
        $(this).parent().addClass('focus');
    }).blur(function(){
        $(this).removeClass('focus');
        $(this).parent().removeClass('focus');
    })
    return this;
};

$.fn.checkbox = function() {
    $(this).parent().addClass('fancy').children('label').prepend('<span></span>').end();
    return this.focus(function(){
        $(this).parent().addClass('focus');
    }).blur(function(){
        $(this).parent().removeClass('focus');
    }).click(function(){
        if($(this).is(':checked')){
            $(this).parent().addClass('checked');
        } else {
            $(this).parent().removeClass('checked');
        }
    });
    return this;
};

$.fn.tab = function() {
    var tab = $(this);

    tab.find('ul.tabs-nav li.tabs-item a').each(function() {
        tab.data($(this).text(), '');
    });

    tab.find('ul.tabs-nav li.tabs-item a').click(function() {
        if (tab.find('ul.tabs-nav li.tabs-item a img').length == 0) {
            var tab_anchor = $(this);
            var tab_text = tab_anchor.text();
            var tab_selector = tab_anchor.parent();
            var tab_container = tab_selector.parent().parent();

            tab_container.find('ul.tabs-nav li.tabs-item').removeClass('state-active');
            tab_anchor.html('<img src="/img/ui/loading-orange.gif" width="12" height="12" />');

            tab_container.find('div.tabs-panel').animate({height: '0px'}, 500, 'linear', function() {
                if (tab_container.data(tab_text) == '') {
                    $.ajax({
                        method: 'get',
                        url: tab_anchor.attr('href'),

                        success: function(html) {
                            LoadTab(html, tab_container, tab_anchor, tab_selector, tab_text);
                        }
                    });
                } else {
                    LoadTab(tab_container.data(tab_text), tab_container, tab_anchor, tab_selector, tab_text);
                }
            });
        }

        return false;
    });
}

function LoadTab(html, tab_container, tab_anchor, tab_selector, tab_text) {
    tab_container.data(tab_text, html);

    var height = tab_container.find('div.tabs-panel').html(html).css({height: 'auto'}).height();

    tab_container.find('div.tabs-panel').css({height: '0px'});        
    tab_container.find('div.tabs-panel').animate({height: height}, 250);

    tab_anchor.html(tab_text);
    tab_selector.addClass('state-active');
}

function parseXML(xml) {
    if (window.ActiveXObject && window.GetObject) {
        var dom = new ActiveXObject('Microsoft.XMLDOM');

        dom.loadXML(xml);

        return dom;
    }

    if (window.DOMParser) {
        return new DOMParser().parseFromString(xml, 'text/xml');
    } throw new Error('No XML parser available');
}

$.easing['easeInOutQuad'] = function (p, t, b, c, d) {
	if ((t/=d/2) < 1){
		return c/2*t*t + b;
	}
	return -c/2 * ((--t)*(t-2) - 1) + b;
};

jQuery.easing['bounceEaseOut'] = function(p, t, b, c, d) {
	if ((t/=d) < (1/2.75)) {
		return c*(7.5625*t*t) + b;
	} else if (t < (2/2.75)) {
		return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
	} else if (t < (2.5/2.75)) {
		return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
	} else {
		return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
	}
};

