
/**!
 * @preserve Color animation jQuery-plugin
 * http://www.bitstorm.org/jquery/color-animation/
 * Copyright 2011 Edwin Martin <edwin@bitstorm.org>
 * Released under the MIT and GPL licenses.
 */

(function($) {
	
	jQuery.fn.center = function () {
	    this.css("position","absolute");
	    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
	    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	    return this;
	}
	
	/**
	 * Check whether the browser supports RGBA color mode.
	 *
	 * Author Mehdi Kabab <http://pioupioum.fr>
	 * @return {boolean} True if the browser support RGBA. False otherwise.
	 */
	function isRGBACapable() {
		var $script = $('script:first'),
				color = $script.css('color'),
				result = false;
		if (/^rgba/.test(color)) {
			result = true;
		} else {
			try {
				result = ( color != $script.css('color', 'rgba(0, 0, 0, 0.5)').css('color') );
				$script.css('color', color);
			} catch (e) {
			}
		}

		return result;
	}

	$.extend(true, $, {
		support: {
			'rgba': isRGBACapable()
		}
	});

	var properties = ['color', 'backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'outlineColor'];
	$.each(properties, function(i, property) {
		$.fx.step[property] = function(fx) {
			if (!fx.init) {
				fx.begin = parseColor($(fx.elem).css(property));
				fx.end = parseColor(fx.end);
				fx.init = true;
			}

			fx.elem.style[property] = calculateColor(fx.begin, fx.end, fx.pos);
		}
	});

	// borderColor doesn't fit in standard fx.step above.
	$.fx.step.borderColor = function(fx) {
		if (!fx.init) {
			fx.end = parseColor(fx.end);
		}
		var borders = properties.slice(2, 6); // All four border properties
		$.each(borders, function(i, property) {
			if (!fx.init) {
				fx[property] = {begin: parseColor($(fx.elem).css(property))};
			}

			fx.elem.style[property] = calculateColor(fx[property].begin, fx.end, fx.pos);
		});
		fx.init = true;
	}

	// Calculate an in-between color. Returns "#aabbcc"-like string.
	function calculateColor(begin, end, pos) {
		var color = 'rgb' + ($.support['rgba'] ? 'a' : '') + '('
				+ parseInt((begin[0] + pos * (end[0] - begin[0])), 10) + ','
				+ parseInt((begin[1] + pos * (end[1] - begin[1])), 10) + ','
				+ parseInt((begin[2] + pos * (end[2] - begin[2])), 10);
		if ($.support['rgba']) {
			color += ',' + (begin && end ? parseFloat(begin[3] + pos * (end[3] - begin[3])) : 1);
		}
		color += ')';
		return color;
	}

	// Parse an CSS-syntax color. Outputs an array [r, g, b]
	function parseColor(color) {
		var match, triplet;

		// Match #aabbcc
		if (match = /#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})/.exec(color)) {
			triplet = [parseInt(match[1], 16), parseInt(match[2], 16), parseInt(match[3], 16), 1];

			// Match #abc
		} else if (match = /#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])/.exec(color)) {
			triplet = [parseInt(match[1], 16) * 17, parseInt(match[2], 16) * 17, parseInt(match[3], 16) * 17, 1];

			// Match rgb(n, n, n)
		} else if (match = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color)) {
			triplet = [parseInt(match[1]), parseInt(match[2]), parseInt(match[3]), 1];

		} else if (match = /rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9\.]*)\s*\)/.exec(color)) {
			triplet = [parseInt(match[1], 10), parseInt(match[2], 10), parseInt(match[3], 10),parseFloat(match[4])];

			// No browser returns rgb(n%, n%, n%), so little reason to support this format.
		}
		return triplet;
	}
})(jQuery);

/**
 * @author Alexander Farkas
 * v. 1.22
 */


(function($) {
	if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
		var oldCurCSS = $.curCSS;
		$.curCSS = function(elem, name, force){
			if(name === 'background-position'){
				name = 'backgroundPosition';
			}
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}
	
	var oldAnim = $.fn.animate;
	$.fn.animate = function(prop){
		if('background-position' in prop){
			prop.backgroundPosition = prop['background-position'];
			delete prop['background-position'];
		}
		if('backgroundPosition' in prop){
			prop.backgroundPosition = '('+ prop.backgroundPosition;
		}
		return oldAnim.apply(this, arguments);
	};
	
	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}
	
	$.fx.step. backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			var start = $.curCSS(fx.elem,'backgroundPosition');
			if(!start){//FF2 no inline-style fallback
				start = '0px 0px';
			}
			
			start = toArray(start);
			fx.start = [start[0],start[2]];
			var end = toArray(fx.end);
			fx.end = [end[0],end[2]];
			
			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		//return;
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

	};
})(jQuery);

var myfont_webfont_test = false;
var woffEnabled = true;

$(function() {

	$('body').ezBgResize({ img : 'http://assets.vcard.pro/uploads/account/background/4e1a3bf890fdc644a7000005/bg.jpg' });
	
	
	var box; 
	$.box = box = {
			id: '#box'
		,	titleCss: '.container-header h1'
		, containerCss: '.container'
		, contentCss: '.container-content'
		, closeCss: 'a.close'
		, openMs: 100
		, closeMs: 100
		, html: '<div id="box"><div class="container"><div class="container-header"><a href="#" class="close"></a><h1></h1></div><div class="container-content"></div></div></div>'
		, init: function(){
				$('body').append(this.html);
				this.box 			 = $(this.id).hide();
				this.title 		 = this.box.find(this.titleCss);
				this.container = this.box.find(this.containerCss).hide();
				this.content 	 = this.box.find(this.contentCss);
				this.closeBtn	 = this.box.find(this.closeCss);
				this.events();
			}
		, events: function(){
			var _this = this;
			$(document).keyup(function(e){ if(e.keyCode == 27) { _this.close(); } });
			this.closeBtn.click(function(){ _this.close() });
		}
		, open: function(id){
				var _this = this;
				this.container.hide();
				this.box.fadeIn(this.openMs, function(){ _this.template(id) });
			}
		, template: function(name){
				var _this = this;
				this.title
					.html('')
					.html( $('.tpl-'+name+' h1').html() );
				this.content
					.html('')
					.html( $('.tpl-'+name).clone() )
					.find('.tpl').show().end()
					.parent().center().fadeIn().end()
					.find('form')
						.bind('ajax:beforeSend.rails', function(){ $(this).find('img').show(); })
						.bind('ajax:success', function(xhr, data, status) {
							if( !data.saved ) {
								var errors = JSON.parse(data.errors);
								$(this).find(':input').removeClass('error');
								for( var error in errors) {
									var field = $(this).find('input[name="message['+error+']"], textarea[name="message['+error+']"]');
									field.addClass('error');
								}
							} else {
								_this.title.html('');
								_this.content.html(data.message)
								_this.container.center();
							}
						})
						.bind('ajax:failure', function(xhr, status, error) { alert("Please try later."); })
						.bind('ajax:complete', function(){ $(this).find('img').hide(); });
			}
		, close: function(){
			 this.box.fadeOut(this.closeMs)
			}
	};
	
	$.box.init();

	$('#company a:not(.link, #logo-link), #user a')
		.live('click', function(e){
			var _self = $(this);
			switch(_self.attr('href')){
				case '#/team':
					$.box.open('team')
				break;
				case '#/contact/me':
					$.box.open('contact')
				break;
			}
			return false;
		});
});


