﻿var alert = function (html) {
    $('body').prepend('<div id="overlay" style="display:none"><div id="popup">' + html + '<input type="button" value="Close" class="close"/></div></div>');
    $('#overlay #popup input.close').live('click', function () {
        $('#overlay').fadeOut(500, function () {
            $(this).remove();
        });
    });
    $('#overlay').fadeIn(500);
}

$(function () {
    $(':input').attr('autocomplete', 'off');
    $(':input').live('focus', function () {
        var _this = $(this);
        if (_this.val() == _this.attr('title')) {
            _this.val('');
        }
    });
    $(':input').live('blur', function () {
        var _this = $(this);
        if (_this.val() == '') {
            _this.val(_this.attr('title'));
        }
    });
    $('table.data tr').each(function () {
        var row = $(this);
        row.find(':text').each(function () {
            var _this = $(this);
            if (_this.attr('title') && (_this.val() == _this.attr('title'))) {
                row.find(':input').css({ opacity: 0.5 });
            }
        });
    });

    $('table.data :text').live('keypress', function () {
        var _this = $(this);
        var tr = _this.parents('tr');
        var trans = true;
        tr.find(':text').each(function () {
            if ($(this).attr('title') && ($(this).val() != $(this).attr('title'))) {
                trans = false;
            }
        });
        if (trans) {
            tr.find(':input').css({ opacity: 0.5 });
        } else {
            tr.find(':input').css({ opacity: 1 });
        }
    });

    $('.msg:not(.stay)').delay(5000).animate({ opacity: 0 }, 1000, function () { $(this).slideUp(); });

    $('.confirm').click(function (e) {
        if (confirm('Are you sure you want to ' + $(this).attr('title'))) {
            window.location = $(this).attr('href');
        }
        return false;
    });
    $('div.admin-menu a').live('click', function () {
        var id = $(this).attr('href').replace('#', '');
        var type = $(this).parents('fieldset').attr('id');
        switch (id) {
            case 'pdf':
                break;
            case 'edit':
                $(this).parents('fieldset').find('form#volunteer-edit').slideDown();
                $(this).parents('fieldset').find('table[id^="volunteer-table"]').slideUp();
                break;
            case 'edit-results':
                $(this).parents('fieldset').find('form#results-edit').slideDown();
                $(this).parents('fieldset').find('#results-table').slideUp();
                break;
            case 'delete':
                $('#overlay #popup').hide();
                $('#overlay').addClass('loading').fadeIn();
                $.post(URL + '/edit-meet', { 'delete': 'true', 'id': ID }, function (result) {
                    if (result.status == 'success') {
                        window.location = URL + '/meets';
                    } else {
                        alert(result);
                    }
                }, 'json');
                break;
            case 'import':
                $('form#import_form input:file').click();
                $('form#import_form input:file').change(function () {
                    loading_overlay();
                    $(this).ready(function () {
                        $('form#import_form').submit();
                    });
                });
                break;
            default:
                break;
        }
    });
});
function loading_overlay() {
    $('#overlay #popup').hide();
    $('#overlay').addClass('loading').fadeIn();
}
function load_popup(title, data){
	$('#overlay').fadeIn();
	var url = null;
	var submitURL = null;
	var submitSuccess = null;
	switch(title){
	    case 'login':
	        url = templateURL + '/login.php?m=login';
	        submitURL = blogURL + '/login';
	        submitSuccess = function (result) {
	            closePopup();
	            window.location = blogURL + '/user/' + result.user_login + '/account';
	        };
	        break;
		case 'logout':
			url = blogURL+'/logout';
			submitSuccess = function(result){
				window.location = blogURL;
			}
			break;
		case 'register':
			url = templateURL+'/login.php?m=register';
			submitURL = blogURL+'/wp-login.php?action=register';
			submitSuccess = function(result){
				load_popup('options','registration');
			};
			break;
		case 'options':
			url = templateURL+'/login.php?m=options&username='+$('#user_login').val()+'&user_email='+$('#user_email').val()+'';
			submitURL = templateURL+'/login.php?update=true';
			submitSuccess = function(result){
				if(data = 'registration'){
					load_popup('login');
				}
				else {
					closePopup();
				}
			}
			break;
	}

	$.get(url, function (content) {
	    $('#overlay #popup').html(content);
	    $('#overlay #popup').css('margin-top', -(parseInt($('#overlay #popup').css('height').replace('px', '')) / 2));
	    $('#overlay #popup').fadeIn();
	    if (title == 'logout') {
	        $('#user_info').html('<a href="#login">Login</a>&nbsp;|&nbsp;<a href="#register">Register</a>');
	    }
	    $('#overlay #popup input#submit').click(function () {
	        $.post(submitURL, $(this).parents('form').serialize(), function (result) {
	            if (result.status == 'success') {
	                submitSuccess(result);
	            } else {
	                alert(result);
	            }
	        },'json');
	    });
	    $('#overlay #popup input#cancel').click(function () {
	        if (title == 'logout') {
	            submitSuccess(null);
	        } else {
	            closePopup();
	        }
	    });
	});
}
function closePopup(){
	$('#overlay #popup').fadeOut();
	$('#overlay #popup').html('');
	$('#overlay').fadeOut();
}
function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + '$' + num + '.' + cents);
}
