var LangList = {};
var Claster = {
	alertBox: function(title, text, class_name) {
		if (!$('#ajax_alert').attr('id')) {
			$('<div id="ajax_alert"></div>').appendTo(document.body).css("right",this.dialogRight());
		}
		$('<div class="item ' + class_name + '"><h3>' + title + '</h3><p>' + text + '</p></div>')
			.prependTo($('#ajax_alert'))
			.slideDown("slow")
			.fadeIn('slow')
			.animate({opacity: 0.8}, 3000)
			.fadeOut('slow', function() {$(this).remove();});
	},
	indikatorBox: function(state){
		if(state){
			if (!$('#ajax_indikator').attr('id')) {
				$('<div id="ajax_indikator">'+LangList.TLT_LOAD+'</div>').appendTo(document.body).css("right",this.dialogRight()+"px");
			}
		}else{
			if ($('#ajax_indikator').attr('id')) {
				$('#ajax_indikator').fadeOut('slow', function() {$(this).remove();});
			}
		}
	},
	dialogRight: function(){
		return ((document.body.offsetWidth - document.getElementById("root").offsetWidth)/2+10);

	},
	shadowText: function(obj){
	
		var obj = $(obj);
		obj.addClass('sh_box');
		var txt = obj.text();
		var itxt = '';
		for(var i = 1; i <= 5; i++){
			itxt += '<span class="sh_text_'+i+'">'+txt+'</span>'
		}
		obj.html(itxt);
	}
}




$(document).ready(function(){
	
	Claster.shadowText('#copy');
	function catrBtnShow(){
		$('#cart_show').css({'display':'block','top':'165px','left':(Claster.dialogRight()-95)+'px'});
	}
	catrBtnShow();
	function catrKran(){
		$('#cart_kran').css({'display':'block','top':'64px','left':(Claster.dialogRight()-95)+'px'});
	}
	catrKran();
	$(window).resize(function(){catrBtnShow();catrKran();})
	$('#cart_show_btn').click(function (e) {
		e.preventDefault();
		Claster.indikatorBox(true);
		$.getJSON("/ajax/cart.php?lang="+curLang, function(json){

			if(json.state){
				//alert(json.toSource())
				Claster.indikatorBox(false);
				var str='<div id="cart_info">';
				str+='<div class="reqFild"><label for="fio">'+LangList.TLT_FIO+':</label> <input class="fld" type="text" id="fio" name="fio" value="'+json.fio+'"/> <sup>*</sup></div>';
				str+='<div><label for="firm">'+LangList.TLT_FIRM+':</label> <input class="fld" type="text" id="firm" name="firm" value="'+json.firm+'"/></div>';
				str+='<div class="reqFild"><label for="phone">'+LangList.TLT_PHONE+':</label> <input class="fld" type="text" id="phone" name="phone" value="'+json.phone+'"/> <sup>*</sup></div>';
				str+='<div><label for="email">'+LangList.TLT_EMAIL+':</label> <input class="fld" type="text" id="email" name="email" value="'+json.email+'"/></div>';
				str+='</div>'
				str+='<div id="cart_list_box"><table id="cart_list">';
				str+='<thead><tr><th><input id="sel_all" type="checkbox" /></th><th>'+LangList.TLT_NAME+'</th><th>'+LangList.TLT_COUNT+'</th></tr></thead>';
				str+='<tbody>';
				//var j=1;<th>'+LangList.TLT_NUMBER+'</th>
				for(var i in json.rows){
					str += '<tr>';
					str += '<td><input name="chb" type="checkbox" /><input name="id" type="hidden" value="'+json.rows[i].id+'" size="3" /></td>';
					//str += '<td>'+j+'</td>';
					str += '<td>'+json.rows[i].name+'</td>';
					str += '<td><input class="fld" name="count" type="text" value="'+json.rows[i].count+'" size="3" /></td>';
					str += '</tr>';
					//j++;
				}
				str += '</tbody></table></div>';
				$('<div id="cart"><h3>'+LangList.TLT_CART+'</h3>' + str + '<div id="cart_but"><div id="card_save" class="btn">'+LangList.TLT_SAVE+'</div><div id="card_item_del" class="btn">'+LangList.TLT_DEL+'</div><div id="card_send" class="btn">'+LangList.TLT_CART_SEND+'</div><div class="modalClose btn">'+LangList.TLT_CLOSE+'</div></div></div>').modal({
					onClose: modalClose,
					onOpen: modalOpen
				});
			}else{
				Claster.indikatorBox(false);
				Claster.alertBox(LangList.TLT_CART, LangList.TLT_CART_EMPTY, 'notice');
				return false;
			}
			function getFilds(){
				var flds_id = $('#cart_list input[name="id"]')
				var flds_count = $('#cart_list input[name="count"]')
				var ob = {};

				ob.fio = $('#fio').attr('value');
				ob.firm = $('#firm').attr('value');
				ob.phone = $('#phone').attr('value');
				ob.email = $('#email').attr('value');

				for(var i=0;i<flds_id.length;i++){


					ob[flds_id[i].value] = flds_count[i].value;
				}


				return ob;
			}
			function validFild(){
				var reqFild = $('#cart .reqFild input[type="text"]');
				reqFild.status = true;
				reqFild.each(function(){
					
					if(this.value==''){
						$(this).addClass("fld_err");
						reqFild.status = false;
					}
				
				})
		
				
				if(!reqFild.status){
					return false;
				}else{
					return true;
				}
			}
			function validCount(){
				var flds_count = $('#cart_list input[name="count"]')
				var state = true;
				for(var i=0;i<flds_count.length;i++){
					flds_count[i].state = true;
					if(isNaN(flds_count[i].value) || flds_count[i].value == ''){
						$(flds_count[i]).addClass("fld_err");
						flds_count[i].state = false
						state = false;
					}
					if(flds_count[i].state){
						flds_count[i].value = parseInt(flds_count[i].value);
					}

				}
				if(!state){
					return false;
				}
				return true;
			}
			if ($('#card_save').attr('id')){
				$('#card_save').click(function(){
					$('#cart .reqFild input[type="text"]').each(function(){	
							$(this).removeClass('fld_err');						
					})
					if(!validCount()){
						Claster.alertBox(LangList.TLT_ERROR, LangList.TLT_ERROR_MSG_2, 'error');
						return false;
					}
					Claster.indikatorBox(true);
					var obj = getFilds();
					obj.type = 'save';
					$.post("/ajax/cart_proc.php", obj,
						function(data){
							if(data.state){
								Claster.indikatorBox(false);
								Claster.alertBox(LangList.TLT_CART, LangList.TLT_CART_SAVE, 'notice');
							}else{
								Claster.indikatorBox(false);
								Claster.alertBox(LangList.TLT_ERROR, LangList.TLT_ERROR_MSG_1, 'error');
							}

					}, "json");
				})
			}
			if ($('#card_send').attr('id')){

				$('#card_send').click(function(){
					if(!validFild()||!validCount()){
						Claster.alertBox(LangList.TLT_ERROR, LangList.TLT_ERROR_MSG_2, 'error');
						return false;
					}
					Claster.indikatorBox(true);					
					var obj = getFilds()
					obj.type = 'send';
					obj.lang = curLang;
					//alert(obj.toSource())
					$.post("/ajax/cart_proc.php", obj,
						function(data){
							if(data.state){
								Claster.indikatorBox(false);
								//$.modal.close();
								myModalClose()
								Claster.alertBox(LangList.TLT_CART, LangList.TLT_CART_SENDM, 'notice');
							}else{
								Claster.indikatorBox(false);
								Claster.alertBox(LangList.TLT_ERROR, LangList.TLT_ERROR_MSG_1, 'error');
							}

					}, "json");




				})

			}
			if($('#sel_all').attr('id')){
				var card_send = $('#sel_all');
				card_send.state = false;
				card_send.click(function(){
					function Sel(sel){
						$('#cart_list input[name="chb"]').each(function(){
							$(this).attr('checked',sel);
						})
						card_send.state = sel;
					}
					if(card_send.state){
						Sel(false)
					}else{
						Sel(true)
					}
				})
			}
			if ($('#card_item_del').attr('id')){
				$('#card_item_del').click(function(){
					var chb = $('#cart_list input[name="chb"]');
					var status = false;
					chb.each(function(){
						if(this.checked){
							$(this).parent().parent().remove()
							if(!$('#cart_list input[name="chb"]').length){
								Claster.indikatorBox(true);								
								$.post("/ajax/cart_proc.php", {"type":"clear"},
									function(data){
										if(data.state){
											myModalClose();
											Claster.indikatorBox(false);
											//$.modal.close();
											
											Claster.alertBox(LangList.TLT_NOTISE, LangList.TLT_NOTISE_MSG_2, 'notice');	
										}else{
											Claster.indikatorBox(false);
											Claster.alertBox(LangList.TLT_ERROR, LangList.TLT_ERROR_MSG_1, 'error');
										}
			
								}, "json");								
								
							}
							
							status = true;							
						}
					})
					if(!status){
						Claster.alertBox(LangList.TLT_NOTISE, LangList.TLT_NOTISE_MSG_1, 'notice');	
					}
				})
			}
			$('#cart_list input[name="count"]').change(function(){
				if(!isNaN(this.value)&&this.value!=''){
					$(this).removeClass('fld_err');
				}
			})
			$('#cart .reqFild input[type="text"]').change(function(){
				if(this.value!=''){
					$(this).removeClass('fld_err');					
				}
			})
		});

	});


});
