var Act = [];
//Path
Act.BasePath = '/';
Act.CartPath = '/';
Act.LoginPath = '/';
Act.LogoutPath = '/';
Act.VotePath = '/';
//Vars
	Act.Method = 'POST';
	Act.I = 'i';
	Act.Card = []
	Act.Card.regExp = [];
	//cart
	Act.Cart = [];
	Act.Cart.Sum = 0;
	Act.Cart.Count = 0;
	//user
	Act.User = [];
	Act.User.Loged = 0;
	Act.User.Fname = '';
	Act.User.Sname = '';
	Act.User.Login = '';
	Act.User.Password = '';
	//delivery
	Act.Delivery = [];
	Act.Delivery.Id = 0;
	Act.Delivery.Array = [];
	//messages
	Act.Message = [];
	Act.Message.Pattern = '';
	Act.Message.TimeOut = 6000;
	Act.Message.Counter = 1;
	Act.Message.Width = 0;
	//Vote
	Act.Vote = [];
	Act.Vote.Comments = [];
	Act.Vote.GoodId = 0;
//Functions
Act.Ajax = function(url,req,on_success,on_error) {
	$.ajax({
		type: this.Method,
		url: url,
		data: "async=1&"+req,
		dataType: "json",
		success: function(data) {
			//alert(data);
			if( data.result=="ok" ) {
				if( on_success ) {
					on_success( data );
				}
			} else {
				if( data.result=="error" ) {
					Act.Message.Show(data.message,'Error:');//$.jGrowl( data.message, { header:"Error" } );
				} else {
					Act.Message.Show("Unknown server answer",'Error:');//$.jGrowl( "Unknown server answer", { header:"Error" } );
				}
				if( on_error ) {
					on_error(data);
				}
			}
		},
		error: function(data) {
			Act.Message.Show('Connection error','Error:');
			if( on_error ) {
				on_error(data);
			}
		}
	});
}
Act.Delivery.Set = function(Id) {
	Id = parseInt(Id);
	if(this.Array[Id]!=undefined) {
		this.Id = Id;
		Act.Cart.withDelivery();
	}
}
Act.Cart.withDelivery = function() {
	var sum = parseFloat(this.Sum);
	var del = parseInt(Act.Delivery.Array[Act.Delivery.Id].price);
	$('#total_order_sum_new').text( Math.round((sum+del)*100)/100 );
}
Act.Cart.Update = function(sum,count) {
	this.Sum = parseFloat(sum);
	this.Count = parseInt(count);
	this.CartInfo();
}
Act.Cart.CartInfo = function() {
	$('#cart_count').text(this.Count);
	$('#cart_summ').text(this.Sum);
	Act.Cart.Empty();
}
Act.Cart.Empty = function() {
	if (this.Sum==0 || this.Count==0) {
		if($('#empty_msg').hasClass('i'))
			$('#empty_msg').removeClass('i');
		if(!$('#goods_in_cart').hasClass('i'))
			$('#goods_in_cart').addClass('i');
		if(!$('#payment_form').hasClass('i'))
			$('#payment_form').addClass('i');
	}
}
Act.User.LoginAction = function() {
	var Log = $('[name='+this.Login+']').val();
	var Pass = $('[name='+this.Password+']').val();
	var Req = this.Login+'='+Log+'&'+this.Password+'='+Pass;
	if(this.Loged)
		return false;
	if(Log=='' || Pass=='') {
		Act.Message.Show('Login and/or password does not set.','Attention:');
		return false;
	}
	Act.Ajax(
		Act.LoginPath,
		Req,
		function(data) {
			Act.Message.Show(data.message,'Message:');
			Act.User.UserInfo(1,data.user.fname,data.user.sname);
			$('[name='+Act.User.Login+']').val('');
			$('[name='+Act.User.Password+']').val('');
		}
	);
	return false;
}
Act.User.LogoutAction = function() {
	Act.Ajax(
		Act.LogoutPath,
		'',
		function(data) {
			Act.Message.Show(data.message,'Message:');
			Act.User.UserInfo(0,'','');
		}
	);
	return false;
}
Act.User.UserInfo = function(loged,fname,sname) {
	if(loged) {
		this.Loged = 1;
		this.Fname = fname;
		this.Sname = sname;
	} else {
		this.Loged = 0;
		this.Fname = '';
		this.Sname = '';
	}
	this.LogWindow();
}
Act.User.LogWindow = function() {
	if(this.Loged) {
		$('#user_fname_val').text('');
		if(this.Fname!=undefined)
			$('#user_fname_val').text(this.Fname);
		$('#user_sname_val').text('');
		if(this.Sname!=undefined)
			$('#user_sname_val').text(this.Sname);
		if($('#login_value').hasClass('i'))
			$('#login_value').removeClass('i');
		if(!$('#logout_value').hasClass('i'))
			$('#logout_value').addClass('i');
	} else {
		$('#user_fname_val').text('');
		$('#user_sname_val').text('');
		if(!$('#login_value').hasClass('i'))
			$('#login_value').addClass('i');
		if($('#logout_value').hasClass('i'))
			$('#logout_value').removeClass('i');
	}
}
Act.GoodRemove = function(sender,url,id) {
	$( sender ).addClass( "hidden" );
	this.Ajax(
		url,
		'rid='+id,
		function( data ) {
			$( sender ).removeClass( "hidden" );
			$( "#itemId_"+id ).remove();
			Act.Cart.Update(data.cartsumm,data.cartcount);
			Act.Cart.withDelivery();
			if(Act.Cart.Count) {
				Act.Message.Show('Good deleted.','Message:');
			} else {
				Act.Message.Show('Your cart is empty.','Message:');
			}
		},
		function( data ) {
			$( sender ).removeClass( "hidden" );
		}
	);
	return false;
}
Act.GoodAdd = function(sender, url) {
	$( sender ).addClass( "hidden" );
	this.Ajax(
		url,'',
		function( data ) {
			$( sender ).removeClass( "hidden" );
			Act.Cart.Update(data.cartsumm,data.cartcount);
		},
		function( data ) {
			$( sender ).removeClass( "hidden" );
		}
	);
	return false;
}

//Messages
Act.Message.Show = function(Msg, Title, Link) {
	this.ReSize();
	var message = this.Pattern;
	var Counter = this.Counter++;
	if(Link && Link.link && Link.title)
		Link = "<a class='btn' href='"+Link.link+"'><span>"+Link.title+"</span></a>";
	else
		Link = '';
	if(Title!=undefined && Title!='')
		Title = '<h4>'+Title+'</h4>';
	else
		Title = '';
	if(Msg==undefined || Msg=='')
		return false;
	Msg = '<p>'+Msg+'</p>';
	message = message.replace( /%MSG_TITLE%/, Title );
	message = message.replace( /%MSG_NUMBER%/, Counter );
	message = message.replace( /%MSG_TEXT%/, Msg );
	message = message.replace( /%MSG_LINK%/, Link );
	$('#msg_place').append("<div id='msg_place_"+Counter+"'></div>");
	$('#msg_place_'+Counter).append(message);
	setTimeout(
		function(){
			Act.Message.Close(Counter);
		},
		this.TimeOut
	);
	return false;
}
Act.Message.Close = function(Counter) {
	$('#msg_place_'+Counter).empty();
	return false;
}
Act.Message.LoadPattern = function() {
	Act.Message.Pattern = $('#msg_pattern').html();
}
Act.Message.ReSize = function() {
	var Width = parseInt($(window).width());
	if(Width!=this.Width) {
		this.Width = Width;
		Width = (Width-1000)/2;
		if(Width<0)
			Width = 0;
		$('#msg_place').css('right',Width);
	}
}

//VOTE
Act.Vote.SetMark = function(GoodId, Mark) {
	GoodId = parseInt(GoodId);
	Mark = parseInt(Mark);
	if(!Act.User.Loged) {
		Act.Message.Show('You need login for this action','Message:');
		return false;
	}
	if(!GoodId) {
		Act.Message.Show('Error','Message:');
		return false;
	}
	if(Mark<=0) {
		Act.Message.Show('Error','Message:');
		return false;
	}
	if(Mark>5)
		Mark = 5;
	Act.Ajax(
		Act.VotePath,
		'act=mark&good_id='+GoodId+'&mark='+Mark,
		function(data) {
			if(data.value)
				$('#mark_value').text(data.value);
			Act.Message.Show(data.message, 'Message:');
		}
	);
	return false;
}
Act.Vote.GetGoodTitle = function(GoodId) {
	var title = '';
	Act.Ajax(
		Act.VotePath,
		'act=title&good_id='+GoodId,
		function(data) {
			title = data.value;
		}
	);
	//alert(title);
	return title;
}
Act.Vote.OpenComments = function(GoodId) {
	Act.Vote.ShowComments(GoodId);
	//var title = this.GetGoodTitle(GoodId);
	if(this.Comments[GoodId]==undefined)
		this.getComment(GoodId);
	return false;
}
Act.Vote.getComment = function(GoodId) {
	if(!$('#more_comments_'+GoodId).hasClass('i'))
		$('#more_comments_'+GoodId).addClass('i');
	Act.Vote.ShowComments(GoodId);
	//var title = this.GetGoodTitle(GoodId);
	if(this.Comments[GoodId]==undefined) {
		this.Comments[GoodId] = [];
		this.Comments[GoodId].Count = 0;
		this.Comments[GoodId].Array = [];
		this.Comments[GoodId].Stop = 0;
		this.Comments[GoodId].Empty = 1;
	}
	if(this.Comments[GoodId].Stop) {
		return false;
	}
	Act.Ajax(
		Act.VotePath,
		'act=getComment&good_id='+GoodId+'&num='+Act.Vote.Comments[GoodId].Count,
		function(data) {
			if(data.value) {
				if(data.value.length<5) 
					Act.Vote.Comments[GoodId].Stop = 1;
				if(data.value.lenght>0 && data.value.lenght!=undefined)
					Act.Vote.Comments[GoodId].Array[Act.Vote.Comments[GoodId].Count] = data.value;
				Act.Vote.Comments[GoodId].Count++;
				Act.Vote.InsertComments(data.value, GoodId);
				if(!Act.Vote.Comments[GoodId].Stop) {
					if($('#more_comments_'+GoodId).hasClass('i'))
						$('#more_comments_'+GoodId).removeClass('i');
				}
			}
			/*
			if((data.value.lenght==0 || data.value.lenght==undefined) && (Act.Vote.Comments[GoodId].Array.lenght==0 || Act.Vote.Comments[GoodId].Array.lenght==undefined)) {
				$('#comments_ph_'+GoodId).empty();
				$('#comments_ph_'+GoodId).append('<p>No comments found</p>');
			}
			*/
		},
		function(data) {
			Act.Vote.CloseComments(GoodId);
			if($('#more_comments_'+GoodId).hasClass('i'))
				$('#more_comments_'+GoodId).removeClass('i');
		}
	);
	return false;
}
Act.Vote.noComments = function(GoodId) {
	if(this.Comments[GoodId].Empty) {
		if($('#no_comments_'+GoodId).hasClass('i'))
			$('#no_comments_'+GoodId).removeClass('i');
	} else {
		if(!$('#no_comments_'+GoodId).hasClass('i'))
			$('#no_comments_'+GoodId).addClass('i');
	}
	return false;
}
Act.Vote.InsertComments = function(Array,GoodId) {
	var Comm = [];
	var Str = '';
	for(var Key in Array) {
		if(this.Comments[GoodId].Empty)
			this.Comments[GoodId].Empty = 0;
		if(Array[Key].date_only!=undefined && Array[Key].comment!=undefined && Array[Key].comment!='') {
			Str = '';
			Str = '<p class="comment"><span class="date">'+Array[Key].date_only+'</span><br/><br/><span class="comment_text">'+Array[Key].comment+'</span></p>';
			Comm.push(Str);
		}
	}
	this.noComments(GoodId);
	$('#comments_ph_'+GoodId).append(Comm.join(''));
}
Act.Vote.ShowCommentForm = function(GoodId) {
	$('#comment_text').val('');
	if(!Act.User.Loged) {
		Act.Message.Show('You need login for this action','Message:');
		return false;
	}

	this.GoodId = parseInt(GoodId);
	if($('.comment_add').hasClass('i'))
		$('.comment_add').removeClass('i');
	$('.comment_good_id').val(this.GoodId);
	this.CommentFormSize();
	return false;
}
Act.Vote.CommentFormSize = function() {
	//height
	var height = parseInt($('.comm_wind_b').height());
	var wh = parseInt($(window).height());
	var top = (wh - (height+40))/2;
	if(top<0) {
		$(window).scroll(0,0);
		$('.comment_add').css('position','absolute');
		top = 0;
	} else {
		$('.comment_add').css('position','fixed');
	}
	$('.comm_wind_a').css('top',top);
	//width
	var width = parseInt($('.comm_wind_b').width());
	var ww = parseInt($(window).width());
	var right = (ww - (width+40))/2;
	if(right<0) {
		$('.comm_wind_a').css('left',0);
		$('.comm_wind_b').css('left',0);
		$('.comment_add').css('position','absolute');
	} else {
		$('.comm_wind_a').css('left','50%');
		$('.comm_wind_b').css('left','-230px');
		$('.comment_add').css('position','fixed');
	}
}
Act.Vote.CloseCommentForm = function() {
	if(!$('.comment_add').hasClass('i'))
		$('.comment_add').addClass('i');
	$('#comment_text').val('');
	return false;
}
Act.Vote.addComment = function() {
	var GoodId = parseInt(this.GoodId);
	if($('.comment_good_id').val()==this.GoodId) {
		var text = $('#comment_text').val();
		if(text==undefined || text=='') {
			Act.Message.Show('Please set comment','Message:');
			return false;
		}
		text = text.replace(/'/,'');
		text = text.replace(/"/,'');
		text = text.replace(/</,'');
		text = text.replace(/>/,'');
		text = text.replace(/\//,'');
		text = text.replace(/\\/,'');
		Act.Ajax(
			Act.VotePath,
			'act=addComment&good_id='+GoodId+'&comm='+$.URLEncode(text),
			function(data) {
				Act.Message.Show(data.message, 'Message:');
				Act.Vote.CloseCommentForm();
			},
			function(data) {
				Act.Vote.CloseCommentForm();
			}
		);
	}
	return false;
}
Act.Vote.ShowComments = function(GoodId) {
	this.CloseComments(GoodId);
	if($('#good_comments_'+GoodId).hasClass('i'))
		$('#good_comments_'+GoodId).removeClass('i');
}
Act.Vote.CloseComments = function() {
	for(var GoodId in Act.Vote.Comments) {
	if(!$('#good_comments_'+GoodId).hasClass('i'))
		$('#good_comments_'+GoodId).addClass('i');
	}
	return false;
}

$(window).resize(function() {
	Act.Message.ReSize();
	Act.Vote.CommentFormSize();
});
$( function() {
	Act.Cart.CartInfo();
	Act.Delivery.Id = parseInt(Act.Delivery.Id);
	Act.UserLoged = parseInt(Act.UserLoged);
	Act.Message.LoadPattern();
});
