$(document).ready(function(){

/*	$('ul#cat-list').treeview({
		animated: "fast",
		collapsed: false
	});*/
	
	$('#question-credit').hover(
		function () {
			$('#credit-info').show('fast');
		},
		function () {
			$('#credit-info').hide('fast');
		}
	);

	$('#question-pricemin').hover(
			function () {
				$('#pricemin-info').show('fast');
			},
			function () {
				$('#pricemin-info').hide('fast');
			}
	);

	$('#question-pricemax').hover(
			function () {
				$('#pricemax-info').show('fast');
			},
			function () {
				$('#pricemax-info').hide('fast');
			}
	);

	$('#question-interval').hover(
			function () {
				$('#interval-info').show('fast');
			},
			function () {
				$('#interval-info').hide('fast');
			}
	);


	$.preloadCssImages();

	$('form#register-form').validate({
		rules: {
			username: {
				required: true,
				minlength: 3
			},
			fullname: {
				required: true,
				minlength: 3
			},
			country: {
				required: true,
				min: 1
			},
			county: {
				required: false
			},
			postcode: {
				required: true,
				minlength: 4,
				min: 1000
			},
			city: {
				required: true
			},
			credits: {
				required: true,
				min: 1
			},
			uclass: {
				required: true,
				min: 1
			},
			password: {
				required: true
			},
			newsletter: {
				required: false
			},
			accept_rules: {
				required: true
			},
			address: {
				required: true
			},
			email: {
				required: true,
				email: true
			},
			phone: {
				required: true
			},
			new_password: {
				required: true,
				minlength: 6
			},
			new_pass_again: {
				required: true,
				equalTo: '#newpass'
			}
			
		},
		messages: {
			country: "Kérem válasszon ki egy országot!",
			uclass: 'Kérem válasszon ki egy csoportot!',
			new_pass_again: "Nem egyezik az előzőleg beírt jelszóval!"
		},
		errorElement: "label",
		errorClass: "error",
		highlight: function(element, errorClass) {
			$(element).addClass('req');
		},
		unhighlight: function(element, errorClass) {
			$(element).removeClass('req');
		}
	});

	$('form#ueditform').validate({
		rules: {
			fullname: {
				required: true,
				minlength: 3
			},
			country: {
				required: true,
				min: 1
			},
			county: {
				required: false
			},
			city: {
				required: true
			},
			postcode: {
				required: true
			},
			email: {
				required: true,
				email: true
			},
			address: {
				required: true
			},
			current_password: {
				required: true
			},
			new_pass_again: {
				equalTo: '#newpass'
			}
		},
		messages: {
			country: "Kérem válasszon ki egy országot!",
			new_pass_again: "Nem egyezik az előzőleg beírt jelszóval!"
		},
		errorElement: "label",
		errorClass: "error",
		highlight: function(element, errorClass) {
			$(element).addClass('req');
		},
		unhighlight: function(element, errorClass) {
			$(element).removeClass('req');
		}
	});

	$('form#recover-form').validate({
		rules: {
			email: {
				required: true,
				email: true
			}
		},

		errorElement: "label",
		errorClass: "error",
		highlight: function(element, errorClass) {
			$(element).addClass('req');
		},
		unhighlight: function(element, errorClass) {
			$(element).removeClass('req');
		}
	});

	$('form#invite-form').validate({
		rules: {
			invite_email: {
				required: true,
				email: true
			}
		},

		errorElement: "div",
		errorClass: "error",
		highlight: function(element, errorClass) {
			$(element).addClass('req');
		},
		unhighlight: function(element, errorClass) {
			$(element).removeClass('req');
		}
	});
	
	$('#auctions-hot').everyTime(2000, function(i) {
		var set = $('.timer'); //$('#auctions-hot').find('.timer');
		var post = new String;
		var tomb = new Array();
		set.each(function (i) {
			post += ',' + $(this).attr('id').substring(6);
			tomb[i] = $(this).attr('id').substring(6);
		});
		$.ajax({
			type: "POST",
			url: 'ajax.php?action=updateforever',
			data: {auction_id : post.substring(1)},
			dataType: 'json',
			success: function(data) {
				var set2 = $('.timer');
				set2.each(function (j) {
					id = tomb[j];
					$('div#timer-'+id).html(data[id].time_left);
					$('div#lastuser-'+id).html(data[id].last_bid_user_name);
					
					if ($('div#price-'+id).html().substring(3) != data[id].price_current)
					{
						//$('#user-credits').html(parseInt($('#user-credits').html()) - 1);
						$('div#price-'+id).css({background: 'red'});
						$('div#price-'+id).animate({backgroundColor: '#ebeae8'}, 'normal');

					}
					$('div#price-'+id).html('ft ' + data[id].price_current);
					
					if (data[id].status == 'archived')
					{
						if (data[id].winner == data.userid)
						{
							$('div#timer-'+id).css({background: 'red'});
							$('div#timer-'+id).animate({backgroundColor: '#ebeae8'}, 'normal');
							$('div#timer-'+id).html('Ön nyert!');
						}
						else
						{
							$('div#timer-'+id).css({background: 'red'});
							$('div#timer-'+id).animate({backgroundColor: '#ebeae8'}, 'normal');
							$('div#timer-'+id).html('A nyertes: ' + data[id].winner_name + '!');							
						}
					}
				});				
			}
		});
	});	

	$('#single-auction').everyTime(2000, function(i) {
		var id = $('#timer-div').attr('class').substring(6);
		$.ajax({
			type: "POST",
			url: 'ajax.php?action=update',
			data: {auction_id : id},
			dataType: 'json',
			success: function(data) {
				if (data == 'archived')
				{
					
				}
				else
				{
					$.ajax({
						type: "POST",
						url: 'ajax.php?action=updatebids',
						data: {auction_id : id},
						dataType: 'html',
						success: function(data) {
							$('div#bid-history').html(data);
						}
					});
					$('td#td-time-left').html(data[id].time_left);
					$('div#lastuser').html(data[id].last_bid_user_name);
					
					if ($('div#timer-div').html().substring(3) != data[id].price_current)
					{
						$('div#timer-div').css({background: 'red'});
						$('div#timer-div').animate({backgroundColor: '#ddd'}, 'normal');
	
					}
					if (data[id].status == 'archived')
					{
						/*if (data[id].winner == data.userid)
						{
							$('div#lastuser').css({background: 'red'});
							$('div#lastuser').animate({backgroundColor: '#ebeae8'}, 'normal');
							$('div#lastuser').html('Ön nyert!');
						}
						else
						{
							$('div#lastuser').css({background: 'red'});
							$('div#lastuser').animate({backgroundColor: '#ebeae8'}, 'normal');
							$('div#lastuser').html('A nyertes: ' + data[id].winner_name + '!');							
						}*/
						document.location.reload(true);
					}
					$('div#timer-div').html('ft ' + data[id].price_current);
				}
			}
		});
	});	

	$('#auction-list-div').everyTime(2000, function(i) {
		var set = $('#auction-list-div').find('.timer');
		var post = new String;
		var tomb = new Array();
		set.each(function (i) {
			post += ',' + $(this).attr('id').substring(6);
			tomb[i] = $(this).attr('id').substring(6);
		});
		$.ajax({
			type: "POST",
			url: 'ajax.php?action=updateforever',
			data: {auction_id : post.substring(1)},
			dataType: 'json',
			success: function(data) {
				var set2 = $('#auction-list-div').find('.timer');
				set2.each(function (j) {
					id = tomb[j];
					$('div#timer-'+id).html(data[id].time_left);
					$('div#lastuser-'+id).html(data[id].last_bid_user_name);
					
					if ($('div#price-'+id).html().substring(3) != data[id].price_current && $('div#price-'+id).html() != 'betöltés...')
					{
						//$('#user-credits').html($('#user-credits').html() - 1);
						$('div#price-'+id).css({background: 'red'});
						$('div#price-'+id).animate({backgroundColor: '#ddd'}, 'normal');

					}
					if (data[id].status == 'archived')
					{
						if (data[id].winner == data.userid)
						{
							$('div#lastuser-'+id).css({background: 'red'});
							$('div#lastuser-'+id).animate({backgroundColor: '#ebeae8'}, 'normal');
							$('div#lastuser-'+id).html('Ön nyert!');
						}
						else
						{
							$('div#lastuser-'+id).css({background: 'red'});
							$('div#lastuser-'+id).animate({backgroundColor: '#ebeae8'}, 'normal');
							$('div#lastuser-'+id).html('A nyertes: ' + data[id].winner_name + '!');							
						}
					}
					$('div#price-'+id).html('ft ' + data[id].price_current);
				});				
			}
		});
	});	


});

jQuery.fn.extend({ 
	ajaxSendForm: function() {
		$.ajax({
			type: "POST",
			url: $(this).attr('action'),
			data: $(this).serialize(),
			success: function(msg) {
				alert(msg);
			}
		});
		return false;
	},
	ajaxSendForm2: function(path, bool) {
		$.ajax({
			type: "POST",
			url: path + $(this).attr('action'),
			data: $(this).serialize(),
			success: function(msg) {
				if (bool == true)
				{
					document.location.reload(true);
				}
				alert(msg);
			}
		});
		return false;
	},
	ajaxSendFormRobot: function() {
		$.ajax({
			type: "POST",
			url: $(this).attr('action'),
			data: $(this).serialize(),
			success: function(msg) {
				if (msg != 'Success')
				{
					alert(msg);
				}
				else
				{
					alert('Licitrobot sikeresen aktiválva!');
					document.location.reload(true);
				}
			}
		});
		return false;
	},
	bidForAuction: function (divId) { 
		var id = divId;
		$.ajax({
			type: "POST",
			url: 'ajax.php?action=bid',
			data: {auction_id : id},
			dataType: 'json',
			success: function(data) {
				if (data == '403')
				{
					alert('A licitáláshoz regisztráció vagy bejelentkezés szükséges!');
				}
				else
				{
					$('#timer-'+id).html(data[id].time_left);
					$('#lastuser-'+id).html(data[id].last_bid_user_name);				
					if ($('#price-'+id).html().substring(3) != data[id].price_current)
					{
						$('#price-'+id).css({background: 'red'});
						$('#price-'+id).animate({backgroundColor: '#ebeae8'}, 'normal');
						$('#user-credits').html($('#user-credits').html().replace(' ', '') - 1);
						var szam = $('#user-credits').html();
						var vege = szam.substring(szam.length - 3, szam.length);
						$('#user-credits').html($('#user-credits').html().replace(vege, ' ' + vege));
					}
					$('#price-'+id).html('ft ' + data[id].price_current);
				}
			}
		});
	},
	bidForSingleAuction: function (divId) { 
		var id = divId;
		$.ajax({
			type: "POST",
			url: 'ajax.php?action=bid',
			data: {auction_id : id},
			dataType: 'json',
			success: function(data) {
				if (data == '403')
				{
					alert('A licitáláshoz regisztráció vagy bejelentkezés szükséges!');
				}
				else
				{
					$('td#td-time-left').html(data[id].time_left);
					$('div#lastuser').html(data[id].last_bid_user_name);				
					if ($('div#timer-div').html().substring(3) != data[id].price_current)
					{
						$('div#timer-div').css({background: 'red'});
						$('div#timer-div').animate({backgroundColor: '#ddd'}, 'normal');
						$('#user-credits').html($('#user-credits').html().replace(' ', '') - 1);
						var szam = $('#user-credits').html();
						var vege = szam.substring(szam.length - 3, szam.length);
						$('#user-credits').html($('#user-credits').html().replace(vege, ' ' + vege));
					}
					$('div#timer-div').html('ft ' + data[id].price_current);				
				}
			}
		});
	},
	bidForSingleAuctionOnList: function (divId) { 
		var id = divId;
		$.ajax({
			type: "POST",
			url: 'ajax.php?action=bid',
			data: {auction_id : id},
			dataType: 'json',
			success: function(data) {
				if (data == '403')
				{
					alert('A licitáláshoz regisztráció vagy bejelentkezés szükséges!');
				}
				else
				{
					$('#timer-'+id).html(data[id].time_left);
					$('#lastuser-'+id).html(data[id].last_bid_user_name);				
					if ($('#price-'+id).html().substring(3) != data[id].price_current)
					{
						$('#price-'+id).css({background: 'red'});
						$('#price-'+id).animate({backgroundColor: '#ddd'}, 'normal');
						$('#user-credits').html($('#user-credits').html().replace(' ', '') - 1);
						var szam = $('#user-credits').html();
						var vege = szam.substring(szam.length - 3, szam.length);
						$('#user-credits').html($('#user-credits').html().replace(vege, ' ' + vege));
					}
					$('#price-'+id).html('ft ' + data[id].price_current);
				}
			}
		});
	},
	viewPic: function (path) {
		$('#gallery-big').html('<img class="big-pic" src="' + path +'" alt="" />');
	},
	delRobot: function (id) {
		var divid = id;
		if (confirm('Biztosan törli a robotot?'))
		{
			$.ajax({
				type: "POST",
				url: 'ajax.php?action=delrobot',
				data: {auction_id : divid},
				dataType: 'html',
				success: function(data) {
					$('tr#robot-'+ divid).remove();
					alert(data);
				}
			});
		}
	},
	buyNow: function(id, userId, userName) {
		if (confirm('Biztosan megvásárolja azonnali áron a terméket?') == true)
		{
			$.ajax({
				type: "POST",
				url: 'ajax.php?action=buynow',
				data: { auction_id: id },
				success: function(msg) {
					alert(msg);
					document.location.reload(true);
				}
			});
		}
	},
	requestCredit: function(path) {
		if (confirm('Elküldi az igénylést?') == true)
		{
			$('#request-credit').ajaxSendForm2(path, true);
		}
	},
	undoCredit: function(id, path) {
		if (confirm('Visszavonja az igénylést?') == true)
		{
			$.ajax({
				type: "post",
				url: path + 'ajax.php?action=undocredit',
				data: { delitem: id},
				success: function(msg) {
					alert(msg);
				}
			});
			$('#delrow-'+id).remove();
		}
	},
	sendInvite: function() {
		if ($('#invite-form').valid() == true)
		{
			if (confirm('Biztosan elküldi a meghívót?') == true)
			{
				$.ajax({
					type: "POST",
					url: $(this).attr('action'),
					data: $(this).serialize(),
					success: function(msg) {
						alert(msg);
					}
				});
				return false;
			}
		}
	}
});