/**
 * @author ec_stasis
 */
var validate = {};

validate.inti = function (el)
{
	var valid = validate.match(el, /^\d+$/);
	validate.classify(el, valid);
	return valid;
}

validate.floati = function (el)
{
	var valid = validate.match(el, /^\d+(\.\d(\d)?)?$/);
	validate.classify(el, valid);
	return valid;
}

validate.select = function (el)
{
	return $(el).val() !== '';
}


validate.match = function (el, reg)
{
	return $(el).val().match(reg) !== null;
}

validate.classify = function (el, valid)
{
	if (!valid)
	{
		$(el).addClass('error');
	}
	else
	{
		$(el).removeClass('error');
	}
}


function countdown(d, h, m, s)
{
	$('.countdown .cd-val').text("" + d +"d " + num_pad(h) +"h " + num_pad(m) +"m " + num_pad(s) +"s");

	s--;
	if (s < 0) 
	{
		s = 59;
		m--;
		if (m < 0)
		{
			m = 59;
			h--;
			if (h < 0)
			{
				h = 23;
				d--;
				if (d < 0)
				{
					d = 0;
					//stop
					return;
				}
			}
		}
	}

	setTimeout("countdown(" + [d, h, m, s].join(',') + ")", 1000); 
}

function num_pad(num)
{
	return str_pad(num, 2, '0', 'STR_PAD_LEFT');
}

var KCAL_FAT = 7000;

var calcCoef = function (age, sex)
{
	var coef;
	if (age <= 25) { coef = 1.1; }
	else if (age <= 40 ) { coef = 1.0; }
	else if (age <= 60)  { coef = 0.9; }
	else { coef = 0.8; }
	
	if (sex == 'f') { coef -= 0.1; }
	
	return coef;
}


jQuery.fn.loading = function ()
{
	return this.each(function () { $(this).html('<img class="loading" alt="loading..." src="/i/loading.gif" width="107" height="16" style="margin: 8px auto; display: block;"  />'); })
}

jQuery.fn.tip = function (contentSelector)
{
	//initialize tooltip
	return this.each(function (index) {

		var selector = this.rel.split('|')[0];
		
		var delta    = this.rel.split('|')[1];
		if (!delta) { delta = 'left'; }
		
		var content = $(selector).clone();
		content.attr('id', 'tip-gen-' + randomString(6));
		
		content.prepend('<a href="#" class="close">[x]</a>').find('a.close').data('trigger', this).click(function (event) { 
			event.preventDefault(); 
			$( $(this).data('trigger') ).mouseout();
		});
		
		$(this).removeBubbletip().bubbletip(content, { deltaDirection: delta, delayHide: 10 }); // bindHide : null, 
		$(this).click(function (event) { event.preventDefault(); $(this).mouseover(); })
		return;
		
		$(this).attr('title', $(contentSelector).html()).tooltip({
		    // tweak the position
		    offset: [10, 2],
	
		    // use the "slide" effect
		    effect: 'slide'
	
		// add dynamic plugin with optional configuration for bottom edge
		}).dynamic({ bottom: { direction: 'down', bounce: true } });
	});
}

function randomString(length) {
    var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.split('');
    
    if (! length) {
        length = Math.floor(Math.random() * chars.length);
    }
    
    var str = '';
    for (var i = 0; i < length; i++) {
        str += chars[Math.floor(Math.random() * chars.length)];
    }
    return str;
}

function str_pad (input, pad_length, pad_string, pad_type) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // + namespaced by: Michael White (http://getsprink.com)
    // +      input by: Marco van Oort
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: str_pad('Kevin van Zonneveld', 30, '-=', 'STR_PAD_LEFT');
    // *     returns 1: '-=-=-=-=-=-Kevin van Zonneveld'
    // *     example 2: str_pad('Kevin van Zonneveld', 30, '-', 'STR_PAD_BOTH');
    // *     returns 2: '------Kevin van Zonneveld-----'
    var half = '',
        pad_to_go;

    var str_pad_repeater = function (s, len) {
        var collect = '',
            i;

        while (collect.length < len) {
            collect += s;
        }
        collect = collect.substr(0, len);

        return collect;
    };

    input += '';
    pad_string = pad_string !== undefined ? pad_string : ' ';

    if (pad_type != 'STR_PAD_LEFT' && pad_type != 'STR_PAD_RIGHT' && pad_type != 'STR_PAD_BOTH') {
        pad_type = 'STR_PAD_RIGHT';
    }
    if ((pad_to_go = pad_length - input.length) > 0) {
        if (pad_type == 'STR_PAD_LEFT') {
            input = str_pad_repeater(pad_string, pad_to_go) + input;
        } else if (pad_type == 'STR_PAD_RIGHT') {
            input = input + str_pad_repeater(pad_string, pad_to_go);
        } else if (pad_type == 'STR_PAD_BOTH') {
            half = str_pad_repeater(pad_string, Math.ceil(pad_to_go / 2));
            input = half + input + half;
            input = input.substr(0, pad_length);
        }
    }

    return input;
}


function isScrolledIntoView(elem)
{
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}

function todayf()
{
	return new Date();
}

function yesterdayf()
{
	var yesterday = new Date();
	yesterday.setDate(yesterday.getDate() - 1);
	return yesterday;
}
