/*--- tabs function ---*/
function initTabs(){
	$('ul.menu').each(function(){
		var btn_h = $(this);
		var _btn = $(this).find('a.tab');
		var _a = _btn.index(_btn.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		_btn.removeClass('active').eq(_a).addClass('active');
		_btn.each(function(_i){
			this._box = this.href.substr(this.href.indexOf("#") + 1);
			if(this._box){
				this._box = $('#'+this._box);
				if(_i == _a) this._box.show();
				else this._box.hide();
			}
			this.onclick = function(){
				changeTab(_i);
				return false;
			}
		});
		function changeTab(_ind){
			if(_ind != _a){
				if(_btn.get(_a)._box) _btn.get(_a)._box.hide();
				if(_btn.get(_ind)._box) _btn.get(_ind)._box.show();
				_btn.eq(_a).removeClass('active');
				_btn.eq(_ind).addClass('active');
				_a = _ind;
			}
		}
	});
}
/*--- language select ---*/
function initLang(){
	var _hold = $('#language');
	if(_hold.length){
		var _list = _hold.find('li:not(".active") a');
		_list.mouseenter(function(){
			$(this).animate({right:0}, {queue:false, duration:200});
		}).mouseleave(function(){
			$(this).animate({right:-30}, {queue:false, duration:200});
		});
	}
}
/*--- drag tickets ---*/
function dragTickets(){
	var _z = 110;
	var c_t = $('div.dropzone-inf').offset().top - $('#wrapper').offset().top; //282
	var c_l = $('div.dropzone-inf').offset().left - $('#wrapper').offset().left; //73
	$('#dropzone1').css({top:c_t, left:c_l});
	$('#dropzone2').css({top:c_t, left:c_l});
	$('#paper-clip').css({top:c_t+42, left:c_l+46})
	var _url = window.location.href;
	$('div.draggable').each(function(){
		var _el = $(this);
		var _t = _el.position().top;
		var _l = _el.position().left;
		var t_t, t_l;
		var n_t, n_l;
		var _l1, _l2;
		var _i = -1;
		_z+=3;
		if(_el.attr('id').indexOf('contact-info') != -1){
			n_t = c_t + 48;
			n_l = c_l + 10;
			_i = 1;
			if(_url.indexOf('box1') != -1) _el.css({ zIndex: _z, top:n_t, left:n_l});
		}
		else if(_el.attr('id').indexOf('my-photo') != -1){
			n_t = c_t + 34;
			n_l = c_l + 1;
			_i = 2
			if(_url.indexOf('box2') != -1) _el.css({ zIndex: _z, top:n_t, left:n_l});
		}
		else{
			n_t = c_t + 46;
			n_l = c_l - 2;
			_i = 3;
			if(_url.indexOf('box3') != -1) _el.css({ zIndex: _z, top:n_t, left:n_l});
		}
		_el.draggable({
			start: function(){
				_el.css({zIndex:_z+=3});
			},
			stop:function(event, ui){
				t_t = _el.position().top;
				t_l = _el.position().left;
				_l1 = Math.sqrt((_t - t_t)*(_t - t_t) + (_l - t_l)*(_l - t_l));
				_l2 = Math.sqrt((n_t - t_t)*(n_t - t_t) + (n_l - t_l)*(n_l - t_l));
				if(_l1 < _l2){
					_el.stop().animate({top:_t, left: _l}, 400);
				}
				else{
					_el.stop().animate({top:n_t, left:n_l}, 400, function(){
						if(this.getElementsByTagName('a')[0]){
							var t_url = '?box' + _i + '=true' + ((_url.indexOf('box1') != -1)?('&box1=true'):('')) + ((_url.indexOf('box2') != -1)?('&box2=true'):('')) + ((_url.indexOf('box3') != -1)?('&box3=true'):(''));
							window.location = this.getElementsByTagName('a')[0].href + t_url;
						}
					});
				}
			}
		});
	});
}
/*---- clear inputs function ---*/
function clearInputs(){
	$('input:text, input:password, textarea').each(function(){
		if(!this.val) this.val = this.value;
		this.onfocus = function(){
			if(this.value == this.val) this.value = '';
		}
		this.onblur = function(){
			if(this.value == '') this.value = this.val;
		}
	});
}

$(document).ready(function(){
	initTabs();
	initLang();
	dragTickets();
	clearInputs();
}); 