var flyout_options = {};

$(document).ready(function(){
	if (header_flash)
	{
		var so = new SWFObject(base_url + header_flash, "flash_arben", "100%", "315", "9", "#ffffff");
		so.addParam("scale", "noscale");
		so.addParam("wmode", "opaque");
		so.write("header_flash");
	}
	
	$("td.bg_menu a.on").each(function(){
		$(this).parent().next().remove();
		$(this).parent().prev().remove();
	});
	
	$("a.send").click(function() {
		$(this).parents("form").submit();
	});
	
	$("#gallery a").lightBox({
		overlayBgColor: "#FFF",
		overlayOpacity: 0.6,
		imageLoading: base_url + "img/loading.gif",
		imageBtnClose: base_url + "img/close.gif",
		imageBtnPrev: base_url + "img/prev.gif",
		imageBtnNext: base_url + "img/next.gif",
		containerResizeSpeed: 350,
		txtImage: 'Фотографии',
		txtOf: 'из'
	});
	
	flyout_options = {
		outSpeed: 300,
		loadingSrc: base_url+"img/loaders/loading.gif",
		outEase:"easeInCirc",
		inEase: "easeOutBounce",
		closeTip: "Щёлкните, чтобы закрыть изображение"
	};
	$("a.flyout").flyout(flyout_options);
	
	var auth_form = $('#auth_area form');
	if (auth_form.length)
	{
		var auth_error_container = auth_form.find(".error");
		auth_form.submit(function(){
			var form = $(this);
			
			auth_error_container.slideUp();
			
			$.post(
				"/ajax/auth/login",
				auth_form.serialize(),
				function (response) {
					if (response.status == "ok")
					{
						// Self redirect
						document.location.href = document.location.href;
					}
					else
					{
						auth_error_container.empty().text(response.error).slideDown();
						if (response.error_field)
						{
							form.find("input[name="+response.error_field+"]").focus();
						}
					}
				},
				"json"
			)
			
			return false;
		}).find("input[name=submit]").removeAttr("disabled");
		
		auth_form.find("input").bind("change keypress", function(){auth_error_container.slideUp();});
		auth_form.find("label.auto_label input")
			.focus(function(){
				$(this).parent().removeClass("label_mode");
			})
			.blur(function(){
				var input = $(this);
				if (input.val() == "")
				{
					input.parent().addClass("label_mode");
				}
				
			}).each(function(){
				$(this).triggerHandler("blur");
			})
			;
		auth_form.find("label.auto_label span").click(function(){
			$(this).parent().removeClass("label_mode")
				.find("input").focus();
		});
	}
	
	$("a.logout").click(function(){
		$.getJSON(
			"/ajax/auth/logout",
			{},
			function(response){
				if(response.status)
				{
					document.location.href = document.location.href;
				}
			}
		);
		return false;
	});
	
	var lightBoxOptions = {
		imageLoading:  base_url + 'img/lightbox/loading.gif',    // (string) Path and the name of the loading icon
		imageBtnPrev:  base_url + 'img/lightbox/prevlabel.gif',  // (string) Path and the name of the prev button image
		imageBtnNext:  base_url + 'img/lightbox/nextlabel.gif',  // (string) Path and the name of the next button image
		imageBtnClose: base_url + 'img/lightbox/closelabel.gif', // (string) Path and the name of the close btn
		imageBlank:    base_url + 'img/lightbox/blank.gif',      // (string) Path and the name of a blank image (one pixel)
		// Configuration related to texts in caption. For example: Image 2 of 8. You can alter either "Image" and "of" texts.
		txtImage:     'Изображение', // (string) Specify text "Image"
		txtOf:        'из'           // (string) Specify text "of"
	};
	
	$("a.gallery").lightBox(lightBoxOptions);
	
	$("#home_photos").each(function(){
		var $$items = $(this).find("ul > li");
		var count = $$items.length;
		var current = 0;
		var next = 0;
		if (count > 1)
		{
			window.setInterval(function(){
				next = current + 1;
				if (next >= count) next = 0;
				
				$$items.eq(current).find("img").fadeOut(2000);
				
				$$items.eq(next).find("img").hide().fadeIn(2000, function(){
					$$items.eq(current).removeClass("current");
					$$items.eq(next).addClass("current").removeClass("next");
					current = next;
				});
				$$items.eq(next).addClass("next");
			},5000);
		}
	});
});
