PATH_TO_BASE_DIR="http://valentrans.com/tools/src/js/base/1.0/";
//
BASE = {
	elmTop : 0,
	elmRight : 0,
	elmBottom : 0,
	elmLeft : 0,
	quickEdit : function (el, data) {
		$('.quick_edit_selected_element').each(function(){$(this).removeClass('quick_edit_selected_element');});
		$(el).addClass('quick_edit_selected_element');
		if (typeof(qeTO) != 'undefined') {
			clearTimeout(qeTO);
		}
		BASE.getElementOffset(el);
		if ($('#quick_edit').size() < 1) {
			$('body').prepend("<div id=quick_edit ></div>");
		}
		$('#quick_edit').html(data).css({
			display : 'block',
			opacity : 0.0,
			top : (BASE.elmBottom + 30) + 'px',
			left : dX + 'px'
		}).animate({
			opacity : 1.0,
			top : '-=20px'
		}, 200);
		// DRAGGABLE
		$('#quick_edit').draggable({
			start:function(){
				$(this).css({opacity:0.5});
			},
			stop:function(){
				$(this).css({opacity:1.0});
			}
		});
	},
	quickEditClose : function () {
		$('.quick_edit_selected_element').each(function(){$(this).removeClass('quick_edit_selected_element');});
		$('#quick_edit').animate({
			opacity : 0.0,
			top : '+=20px'
		},
			0,
			function () {
			$('#quick_edit').html('').hide();
		});
	},
	getElementOffset : function (el) {
		BASE.elmLeft = el.offset().left;
		BASE.elmTop = el.offset().top;
		BASE.elmBottom = el.offset().top + el.height();
		BASE.elmRight = el.offset().left + el.width();
	},
	tlt : function (msg, el, to) {
		to = to ? to : 0;
		if ($('#base_tlt').size() < 1) {
			$('body').prepend("<div id=base_tlt></div>");
		}
		BASE.getElementOffset(el);
		$('#base_tlt').css({
			display : 'block',
			top : (BASE.elmBottom + to) + 'px',
			left : dX + 'px'
		}).text(msg).animate({
			opacity : 1.0
		}, 200);
	},
	tltHide : function () {
		$('#base_tlt').css({
			opacity : 0.0
		}).hide();
	}
}
// POPUP
function myPopupEl(id) {
	var close = "<a class='popupclose pa o5' onclick='popupclose()'></a>";
	$('#mdiv_data').html(close + $('#' + id).html());
	$('#mdiv').show().animate({
		opacity : 1.0
	}, 400);
}
function myPopupElOuter(id,noclose) {
	close='';
	if(!noclose){
		var close = "<a class='popupclose pa o5' onclick='popupclose()'></a>";
	}
	else{
		$('#mdiv').attr('data-noclose',1);
	}
	$('#mdiv_data').html(close + $('#' + id).outerHTML());
	$('#mdiv').show().animate({
		opacity : 1.0
	}, 400);
}
function myPopup(data) {
	$('#mdiv_data').html(data);
	$('#mdiv').show().animate({
		opacity : 1.0
	}, 400);
}
// msg
function msg(text, color) {
	if ($('#msg').size() < 1) {
		$('body').prepend("<div id=msg></div>");
	}
	!color ? color = '#9c0' : color = color;
	$("#msg").html(text);
	$("#msg").css({
		border : "2px solid " + color,
		borderTop : '0px'
	});
	$("#msg").animate({
		top : "0px"
	}, "fast");
	setTimeout("msgHide()", 4000);
}
function msgHide() {
	$("#msg").animate({
		top : "-35px"
	}, 400);
}

// PIE
function findPie(){
	$('.pie_chart').each(function(){
		var size=typeof($(this).attr('data-size'))!='undefined'?$(this).attr('data-size'):100;
		var colors=['#69b','#fd5','#e88','#9e7','#e9d'];
		data=$(this).attr('data-values').split(';');
		var values=[],texts=[];
		var n=0;
		for(var i in data){
			if(data[i]!=''){
				var arr=data[i].split(':');
				values.push(arr[0]);
				texts.push("<div class=color_example_div><div class='color_example fl' style='background:"+colors[n]+";'></div>"+arr[1]+"<span class='fr'>["+arr[0]+"]</span></div>");
				n++;
			}
		}
		chart_class=$(this).hasClass('chart_div_big')?'chart_div chart_div_big':'chart_div';
		$(this).wrap("<div class='"+chart_class+"'/>");
		$(this).html(values.join()).parent('.chart_div').prepend("<h1>"+$(this).attr('data-header')+"</h1>").append("<div>"+texts.join("\n")+"</div><br clear=both />");
		$(this).sparkline(
			'html',
			{
				type: 'pie',
				width: size,
				height: size,
				sliceColors: colors
			}
		);
	});
}
$(function () {
	// outerhtml
	jQuery.fn.outerHTML = function() {return jQuery("<p>").append(this.clone()).html();}
	// CSS
	$('head').prepend("\n<!-- added from valentrans.com -->\n<script src='"+PATH_TO_BASE_DIR+"jquery-ui.js'></script>\n<link rel='stylesheet' href='"+PATH_TO_BASE_DIR+"base.css'/>\n");
	
	
	
	// base_bar
	$('.base_bar').each(function(){
		$(this).addClass('graygradient pr')
			.attr('data-current',0)
			.append("<div class='pa mcrbtn mcrbtn_left'></div><div class='pa mcrbtn mcrbtn_right'></div><div class='fill redgradient pr' style='width:1%'><span class='pa'>0</span></div>");
	});
	$('.base_bar .mcrbtn').live({
		click: function(){
			var bar=$(this).parent('.base_bar');
			var l=parseInt($(bar).attr('data-levels'));
			var c=parseInt($(bar).attr('data-current'));
			c=c+1<=l?c+1:0;
			$(bar).attr('data-current',c);
			var w=(100/l)*c;
			$(bar).find('.fill').animate({
				width: w+"%"
			},100);
		}
	});
	//
	
	
	
	
	findPie();
	// X Y
	$(document).mousemove(function (e) {
		dX = e.pageX;
		dY = e.pageY;
		$('#base_tlt').css({left:dX+'px'});
	});
	// TABS
	TABflag = false;
	$('.tabs .tab').live({
		click : function () {
			if (!TABflag) {
				TABflag = true;
				tab = $(this);
				$(this).parent('.tabs').find('.tab_open').attr('class', 'tab');
				$(this).parent('.tabs').parent('div').find('.tab_content_open').animate({
					opacity : 0.0
				},
					200,
					function () {
					$(tab).parent('.tabs').parent('div').find('.tab_content_open').attr('class', 'tab_content').hide();
					$(tab).parent('.tabs').parent('div').find('.tab_content[name=' + $(tab).attr('name') + ']').css({
						opacity : 0.0,
						display : 'block'
					}).animate({
						opacity : 1.0
					},
						200,
						function () {
						$(tab).parent('.tabs').parent('div').find('.tab_content[name=' + $(tab).attr('name') + ']')
						.attr('class', 'tab_content tab_content_open')
						.show();
						TABflag = false;
					});
				});
				$(this).attr('class', 'tab tab_open');
			}
		}
	});
	$('.tab_content').each(function(){
		if($(this).text().toString().length<40){
			$(this).append("<div style='padding:17px 30px 10px 30px;margin:10px;border:1px dashed #ddd;'><h1>Ничего нет</h1></div>");
		}
	});
	
	
	// POPUP
	$('body').append("<div id=mdiv style=''><table align=center valign=middle><tr><td valign=middle align=center ><div id=mdiv_data class=pr ></div></td></tr></table></div><script>function popupclose(){$('#mdiv').animate({opacity:0.0},400);setTimeout(function(){$('#mdiv').hide();},400);}</script>");
	$('#mdiv').click(function () {
		if($(this).attr('data-noclose')!=1){
			var idata = false;
			$('#mdiv_data').find('input[type=text],input[type=password],textarea').each(function () {
				if ($(this).val() !== '') {
					idata = true;
				}
			});
			if (idata) {
				if (confirm('Закрыть?')) {
					$('#mdiv').animate({
						opacity : 0.0
					}, 400);
					setTimeout(function () {
						$('#mdiv').hide();
					}, 400);
				}
			}
			else {
				$('#mdiv').animate({
					opacity : 0.0
				}, 400);
				setTimeout(function () {
					$('#mdiv').hide();
				}, 400);
			}
		}
	});
	$('#mdiv #mdiv_data').click(function (event) {
		event.stopPropagation();
	});
	
	
	// TABS
	// data-tlt
	$('*').live({
		mouseover : function () {
			if (typeof($(this).attr('data-tlt')) != 'undefined') {
				if (typeof(bthTO) != 'undefined') {
					clearTimeout(bthTO);
				}
				BASE.tlt($(this).attr('data-tlt'), $(this));
			}
		},
		mouseout : function () {
			if (typeof($(this).attr('data-tlt')) != 'undefined') {
				bthTO = setTimeout(function () {
						BASE.tltHide();
					}, 50);
			}
		}
	});
	$('#base_tlt').live({
		mouseover : function () {
			if (typeof(bthTO) != 'undefined') {
				clearTimeout(bthTO);
			}
		},
		mouseout : function () {
			bthTO = setTimeout(function () {
					BASE.tltHide();
				}, 50);
		},
	});
	// 
	
	//
	$('body').click(function () {
		if ($('#quick_edit').css('display') != 'none' && $('#quick_edit').size() > 0) {
			qeTO = setTimeout(function () {
					BASE.quickEditClose();
				}, 50);
		}
	});
	$('#quick_edit').live({
		click : function () {
			if (typeof(qeTO) != 'undefined') {
				clearTimeout(qeTO);
			}
		}
	});
	
	// base_form
	// allow_empty -> для необязательных
	$('.base_form_send').live({
		click: function(){
			var flag=true;
			var name=$(this).attr('name');
			$('form[name='+name+']').find('input[type=text],textarea').each(function(){
				if($(this).attr('title')!='allow_empty'){
					if($(this).val()==''){
						flag=false;
						$(this).css({boxShadow:'0px 0px 3px #c02428',border:'1px solid #d3474a'});
					}
					else{
						$(this).css({boxShadow:'0px 0px 3px #68a824',border:'1px solid #81c735'});
					}
				}
			});
			if(flag){
				$.post(
					PATH_TO_BASE_DIR+"/php/send_form/",
					$('form[name='+name+']').serialize(),
					function(data){
						msg(data);
					}
				);
			}
			else{
				msg('проверьте все поля');
			}
			return false;
		}
	});
	
});

