// facebox
jQuery(document).ready( function() {
	$('a[rel*=facebox]').facebox( {
		loading_image :'/images/loading.gif',
		close_image :'/images/closelabel.gif'
	})
	
	

});

// registration
$(window).load( function() {
		//$.post("http://www.15min.lt/votes/?qid=978&fr=1",{q978:3});
	
	
	
	
	var form = $('#registration-form');
	var response = $('#registration-response');
	var responseHolder = $('#registration-response-holder');

	form.submit( function() {
		$(responseHolder).fadeIn(500);

		var method = form.attr('method');
		var action = form.attr('action');
		var data = form.serialize();

		$[method](action, data, function(data) {
			$(response).hide();
			$(response).fadeIn(500);
			$(response).html(data);

			var boxOffset = $(response).offset().top - 70;
			$('html, body').animate( {
				scrollTop :boxOffset
			}, 1000);

		});

		return false;
	});
});
// check username availability
$(window).load(
		function() {
			var username = $('#f-username');
			var indicator = $('#username-availability');

			username.keyup( function() {
				indicator.fadeIn(1000);

				var d = new Date();
				var t = d.getTime();

				$.getJSON("/user/signup/?action=checkUsername&nick="
						+ username.val() + "&d=" + t, function(json) {
					if (json.available == true) {
						indicator.removeClass('icon-allow');
						indicator.removeClass('icon-deny');
						indicator.addClass('icon-allow');
						indicator.html(json.availability_title);
					} else {
						indicator.removeClass('icon-allow');
						indicator.removeClass('icon-deny');
						indicator.addClass('icon-deny');
						indicator.html(json.availability_title);
					}
				});
			});
		});

// check email availability
$(window)
		.load(
				function() {
					var email = $('#f-email-reg');
					var emailHolder = $('#email-availability');
					var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;

					email.keyup( function() {
						emailHolder.fadeIn(1000);

						if (pattern.test(email.val())) {
							emailHolder.removeClass('icon-allow');
							emailHolder.removeClass('icon-deny');
							emailHolder.addClass('icon-allow');
							emailHolder.html('tinkamas');
						} else {
							emailHolder.removeClass('icon icon-allow');
							emailHolder.removeClass('icon icon-deny');
							emailHolder.addClass('icon icon-deny');
							emailHolder.html('<b>klaidingas</b>');
						}
					});
				});

// navigation //////

var navigation = function() {
	var navItem = ".container .nav ul.main li";
	var navSub = ".container .nav-sub .nav-sub-inner ul";

	$(navItem).click( function() {
		$(navItem).removeClass("selected");
		$(this).addClass("selected");

		$(navSub).hide();
		$(navSub + "#" + this.id + "-sub").fadeIn(500);

		return false;
	});
};

$(window).load(navigation);

/* login */

var loginBox = function() {
	var form = $("#login-form");
	form.submit( function() {
		var method = form.attr('method');
		var action = form.attr('action');
		var data = form.serialize();
		data += "&login=true";
		jQuery[method](action, data, function(data) {
			$("#login-response").fadeIn(400).html(data);
		});
		return false;
	});
};
$(window).load(loginBox);

/* login slide */

var loginSlide = function() {
	var box = $('#login-box');
	var text = $('#login-text');
	var link = $('#login-text a');
	var externLink = $('a.show-login-box');

	function proceed() {
		$(text).fadeOut(500, function() {
			$(box).fadeIn(500);
		});
	}

	link.click( function() {
		proceed();
		return false;
	});

	externLink.click( function() {
		var boxOffset = $(link).offset().top - 70;
		$('html, body').animate( {
			scrollTop :boxOffset
		}, 1000, function() {
			proceed();
		});
		return false;
	});
};

$(window).load(loginSlide);

/* user nav slide */

var userNavSlide = function() {
	var box = $('#user-nav');
	var link = $('a#user-nav-link');
	var cookieName = 'rallynews-user-nav';

	if (readCookie(cookieName) == 'hidden') {
		link.addClass('link-down');
		$('.user-box .user-info').addClass('user-info-mod');
		box.hide();
		box.removeClass('visible');
		box.addClass('hidden');
	}

	link.click( function() {
		var condition = box.attr('class');
		if (!condition)
			condition = 'visible';

		if (condition == 'visible') {
			$(this).addClass('link-down');
			$('.user-box .user-info').addClass('user-info-mod');
			box.hide(500);
			box.removeClass('visible');
			box.addClass('hidden');
			createCookie(cookieName, 'hidden', 30);
		} else {
			$(this).removeClass('link-down');
			$('.user-box .user-info').removeClass('user-info-mod');
			box.show(500);
			box.removeClass('hidden');
			box.addClass('visible');
			eraseCookie(cookieName);
		}

		return false;
	});
};

$(window).load(userNavSlide);

/* top news slide */

var topNewsSlide = function() {
	var idPrefix = "#top-news-item-";
	var classBase = $(".top-news-item");
	var linkPrev = $("#top-news-prev");
	var linkNext = $("#top-news-next");

	var current = 1;
	var elements = classBase.length;

	linkPrev.click( function() {
		if (elements < 1)
			return false;

		if (current == 1)
			return false;

		var prev = current - 1;

		$(idPrefix + current).fadeOut(200, function() {
			$(idPrefix + prev).fadeIn(500);
		});
		current -= 1;

		$("#top-news-current").text(current);

		if (current == 1)
			$(this).addClass("inactive");

		if (current < elements)
			$(linkNext).removeClass("inactive");

		return false;
	});

	linkNext.click( function() {
		if (elements < 1)
			return false;

		if (current == elements)
			return false;

		var next = current + 1;

		$(idPrefix + current).fadeOut(200, function() {
			$(idPrefix + next).fadeIn(500);
		});
		current += 1;

		$("#top-news-current").text(current);

		if (current == elements)
			$(this).addClass("inactive");

		if (current > 1)
			$(linkPrev).removeClass("inactive");

		return false;
	});
};

$(window).load(topNewsSlide);

// settings //////

// profile
$(window).load( function() {
	var form = $('#profile-form');
	var response = $('#profile-response');
	var responseHolder = $('#profile-response-holder');

	form.submit( function() {
		$(responseHolder).fadeIn(500);

		var method = form.attr('method');
		var action = form.attr('action');
		var data = form.serialize();

		$[method](action, data, function(data) {
			$(response).hide();
			$(response).fadeIn(500);
			$(response).html(data);
		});

		return false;
	});
});
// change password
$(window).load( function() {
	var form = $('#password-form');
	var response = $('#password-response');
	var responseHolder = $('#password-response-holder');

	form.submit( function() {
		$(responseHolder).fadeIn(500);

		var method = form.attr('method');
		var action = form.attr('action');
		var data = form.serialize();

		$[method](action, data, function(data) {
			$(response).hide();
			$(response).fadeIn(500);
			$(response).html(data);
		});

		return false;
	});
});

// change email
$(window).load( function() {
	var form = $('#email-form');
	var response = $('#email-response');
	var responseHolder = $('#email-response-holder');

	form.submit( function() {
		$(responseHolder).fadeIn(500);

		var method = form.attr('method');
		var action = form.attr('action');
		var data = form.serialize();

		$[method](action, data, function(data) {
			$(response).hide();
			$(response).fadeIn(500);
			$(response).html(data);
		});

		return false;
	});
});

// autosave for news ////////

$(window).load(
		function() {
			if (!$('#add_news_draft').length || !$('#add_news_text').length
					|| !$('#add_news_form').length)
				return;

			$('textarea#text').fck( {
				toolbar :"Basic",
				height :200
			});
			$('textarea#description').fck( {
				toolbar :"Basic",
				height :200
			});

			$('#add_news_draft').click( function() {
				newsAutoSave();
			});

			setTimeout("newsAutoSaveTimer()", 1000 * 60);
		});

function newsAutoSaveTimer() {
	newsAutoSave();
	setTimeout("newsAutoSaveTimer()", 1000 * 60);
}

function newsAutoSave() {
	if ($("#news_title").val()) {
		$.post("/mod.php?mod=autosave", {
			"field" :"news_title",
			"value" :$("#news_title").val()
		});
	}

	if ($("#news_group").val()) {
		$.post("/mod.php?mod=autosave", {
			"field" :"news_group",
			"value" :$("#news_group").val()
		});
	}

	if ($("#news_about").val()) {
		$.post("/mod.php?mod=autosave", {
			"field" :"news_about",
			"value" :$("#news_about").val()
		});
	}

	$.each($.fck.editors, function() {
		switch (this.InstanceName) {
		case "text":
			var value = $.fck.content(this.InstanceName);
			$.post("/mod.php?mod=autosave", {
				"field" :"news_text",
				"value" :value
			}, function(data) {
				$('#add_news_text').show();
				$('#add_news_text').html(data);
			});
			break;
		case "description":
			var value = $.fck.content(this.InstanceName);
			$.post("/mod.php?mod=autosave", {
				"field" :"news_description",
				"value" :value
			}, function(data) {
				$('#add_news_text').show();
				$('#add_news_text').html(data);
			});
			break;
		}
	});
}

// autosave for articles ////////

$(window).load(
		function() {
			if (!$('#add_article_draft').length
					|| !$('#add_article_text').length
					|| !$('#add_article_form').length)
				return;

			$('textarea#text').fck( {
				toolbar :"Basic",
				height :200
			});
			$('textarea#description').fck( {
				toolbar :"Basic",
				height :200
			});

			$('#add_article_draft').click( function() {
				articleAutoSave();
			});

			setTimeout("newsAutoSaveTimer()", 1000 * 60);
		});

function articleAutoSaveTimer() {
	articleAutoSave();
	setTimeout("articleAutoSaveTimer()", 1000 * 60);
}

function articleAutoSave() {
	$.post("/mod.php?mod=autosave", {
		"field" :"article_title",
		"value" :$("#article_title").val()
	});

	$.post("/mod.php?mod=autosave", {
		"field" :"article_group",
		"value" :$("#article_group").val()
	});

	$.each($.fck.editors, function() {
		switch (this.InstanceName) {
		case "text":
			var value = $.fck.content(this.InstanceName);
			$.post("/mod.php?mod=autosave", {
				"field" :"article_text",
				"value" :value
			}, function(data) {
				$('#add_article_text').show();
				$('#add_article_text').html(data);
			});
			break;
		case "description":
			var value = $.fck.content(this.InstanceName);
			$.post("/mod.php?mod=autosave", {
				"field" :"article_description",
				"value" :value
			}, function(data) {
				$('#add_article_text').show();
				$('#add_article_text').html(data);
			});
			break;
		}
	});
}

// autosave for adverts ////////

$(window).load(
		function() {
			if (!$('#add_advert_draft').length || !$('#add_advert_text').length
					|| !$('#add_advert_form').length)
				return;

			$('textarea#message').fck( {
				toolbar :"Basic",
				height :200
			});

			$('#add_advert_draft').click( function() {
				advertAutoSave();
			});

			setTimeout("advertAutoSaveTimer()", 1000 * 60);
		});

function advertAutoSaveTimer() {
	advertAutoSave();
	setTimeout("advertAutoSaveTimer()", 1000 * 60);
}

function advertAutoSave() {
	$.post("/mod.php?mod=autosave", {
		"field" :"advert_title",
		"value" :$("#advert_title").val()
	});

	$.post("/mod.php?mod=autosave", {
		"field" :"advert_category",
		"value" :$("#advert_category").val()
	});

	$.each($.fck.editors, function() {
		switch (this.InstanceName) {
		case "message":
			var value = $.fck.content(this.InstanceName);
			$.post("/mod.php?mod=autosave", {
				"field" :"advert_message",
				"value" :value
			}, function(data) {
				$('#add_advert_text').show();
				$('#add_advert_text').html(data);
			});
			break;

		}
	});
}

function ajaxFileUpload() {
	$("#loading").ajaxStart( function() {
		$(this).show();
	}).ajaxComplete( function() {
		$(this).hide();
	});

	$.ajaxFileUpload( {
		url :"/mod.php?mod=user&act=changephoto&do=1",
		secureuri :false,
		fileElementId :'fileToUpload',
		dataType :'json',
		success : function(data, status) {
			if (typeof (data.error) != 'undefined') {
				if (data.error != '') {
					$("#messge-response").html(data.error);
					jQuery("#message_error_div").fadeIn("fast");

				} else {
					window.location = window.location;
				}
			}
		},
		error : function(data, status, e) {
			alert(e);
		}
	})

	return false;

}

function deleteUserPhoto() {
	var callback = function(data) {
		jQuery.facebox( {
			ajax :'/mod.php?mod=user&act=changephoto'
		});
	}
	jQuery['post']('/mod.php?mod=user&act=changephoto&do=2', '?fake=data',
			function(data) {
				callback(data);
			});
}

// send message
function sm() {

	var form = $("#message_form");

	var method = form.attr('method');
	var action = form.attr('action');
	var data = form.serialize();
	var callback = function(data) {
		jQuery("#messge-response").html(data);
		jQuery("#message_error_div").fadeIn("fast");
	}
	jQuery[method](action, data, function(data) {
		callback(data);
	});
	return false;

}

// check messages

var checks = true;
function check_all() {
	with (document.delete_messages) {
		for ( var i = 0; i < elements.length; i++) {
			if (elements[i].type == 'checkbox' && elements[i].name == 'del[]')
				elements[i].checked = checks;
		}
	}
	switch (checks) {
	case true:
		checks = false;
		break;
	case false:
		checks = true;
		break;
	}
}

// reply function

function reply(username) {
	jQuery.facebox( {
		ajax :'/mod.php?mod=user&act=write&username=' + username
	});
}

// check mail

$(window).load( function() {
	checkEmailTimer();
	// s setTimeout("checkEmailTimer()",1000*10);
	});

function checkEmailTimer() {
	checkEmail();
	setTimeout("checkEmailTimer()", 1000 * 10);
}

function checkEmail() {
	var callback = function(data) {
		$("#new-messages").html(data);
	}
	$['get']('/mod.php?mod=user&act=checkmail', '?no=data', function(data) {
		callback(data);
		if (data == "")
			$("#new-messages").hide();
	});
}

/* comments */

$(window).load( function() {
	var form = $('form#comments-form');
	var text = $('form#comments-form #f-text');
	var comments = $('#comments-list');

	form.submit( function() {
		if (text.val() == '')
			return false;

		comments.hide();

		var method = form.attr('method');
		var action = form.attr('action');
		var data = form.serialize();

		var callback = function(data) {
			comments.append(data).fadeIn(1000);
		}

		$[method](action, data, function(data) {
			callback(data);
		});

		text.val('');

		return false;
	});
});

// //////////// ADD NEW PHOTO

function addFile() {
	var obj = jQuery("#file_field").clone(true);
	obj.removeAttr('id');
	obj.insertBefore("#bf");	
	obj.show();
}
function cE(el) {
	this.obj = document.createElement(el);
	return this.obj
}
function cA(obj, att, val) {
	obj.setAttribute(att, val);
	return
}

function deleteGallery(title) {
	var i = confirm(title);
	if (i)
		this.location = "/foto/deleteGallery";
}

/* cookies management */

function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString();
	} else
		var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for ( var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ')
			c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0)
			return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name, "", -1);
}
// forgot password

$(window).load( function() {
	var form = $('#forgot-form');
	var response = $('#profile-response');
	var responseHolder = $('#profile-response-holder');

	form.submit( function() {
		$(responseHolder).fadeIn(500);

		var method = form.attr('method');
		var action = form.attr('action');
		var data = form.serialize();

		$[method](action, data, function(data) {
			$(response).hide();
			$(response).fadeIn(500);
			$(response).html(data);
		});

		return false;
	});
});
