$.extend({
    includePath: '',
    include: function(file) {
        var files = typeof file == "string" ? [file]:file;
        for (var i = 0; i < files.length; i++) {
            var name = files[i].replace(/^\s|\s$/g, "");
            var att = name.split('.');
            var ext = att[att.length - 1].toLowerCase();
            var isCSS = ext == "css";
            var tag = isCSS ? "link" : "script";
            var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";
            var link = (isCSS ? "href" : "src") + "='" + $.includePath + name + "'";
            if ($(tag + "[" + link + "]").length == 0) document.write("<" + tag + attr + link + "></" + tag + ">");
        }
    }
});

$.include("/js/slides.min.jquery.js");
$.include("/js/jcarousellite.js");
$.include("/js/jquery.mousewheel.min.js");
$.include("/js/scrollTop.js");
$.include("/js/tabs.js");
$.include("/js/atooltip.min.jquery.js");
$.include("/js/jquery.prettyPhoto.js");
$.include("/js/jquery.ad-gallery.ljx.pack.js");

$.fn.clearForm = function(){
	return this.each(function(){
		$(".error", this).hide();
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == "form"){
			return $(":input", this).clearForm();
		}
		if (type == "text" || type == "email" || type == "password" || tag == "textarea"){
			this.value = "";
		}
		else if (type == "checkbox" || type == "radio"){
			this.checked = false;
		}
		else if (tag == "select"){
			this.selectedIndex = 0;
		}
	});
};

$.fn.setPlaceholder = function(){
	$(this).find("input, textarea").each(function(){
		$(this).attr("defaultValue", this.value);
		//$(this).css("font-style", "italic");
		$(this).bind({
			focus: function() {
				if (this.value == this.defaultValue) {
					this.value = "";
					//$(this).css("font-style", "normal");
				}
			},
			blur: function() {
				if (!this.value || (this.value == this.defaultValue)) {
					this.value = this.defaultValue;
					//$(this).css("font-style", "italic");
				}
			}
		});
	});
};

$.fn.setErrorCleaner = function(){
	return this.find("input, textarea, select").each(function(){
		$(this).bind({
			focus: function() {
				$("label.error[for=" + $(this).attr("id") + "]").hide();
			}
		});
	});
};

function setEqualHeight(columns) {
	maxHeight = 0;
	columns.each(function() {
		currentHeight = $(this).height();
		if(currentHeight > maxHeight) {
			maxHeight = currentHeight;
		};
	});
	columns.height(maxHeight);
};

$(document).ready(function() {
	/* Костыли для IE6 */
	if ($.browser.msie && (parseInt($.browser.version) <= 6)) {
		function setMaxSize(elems, maxWidth, maxHeight, centerX, centerY) {
			elems.each(function() {
				imgHeight = $(this).height();
				imgWidth = $(this).width();
				if ((maxHeight > 0) && (imgHeight > maxHeight)) {
					ratio = imgWidth / imgHeight;
					imgHeight = maxHeight;
					imgWidth = imgHeight * ratio;
				};
				if ((maxWidth > 0) && (imgWidth > maxWidth)) {
					ratio = imgHeight / imgWidth;
					imgWidth = maxWidth;
					imgHeight = imgWidth * ratio;
				};
				if (centerY && (imgHeight < maxHeight)) {
					$(this).css("margin-top", ((maxHeight - imgHeight) / 2) +"px");
				};
				if (centerX && (imgWidth < maxWidth)) {
					$(this).css("margin-left", ((maxWidth - imgWidth) / 2) +"px");
				};
				if (maxHeight > 0)
					$(this).height(imgHeight);
				if (maxWidth > 0)
					$(this).width(imgWidth);
			});
		};
		function setMaxWidth(elems, maxWidth, center) {
			elems.each(function() {
				imgWidth = $(this).width();
				if (imgWidth > maxWidth)
					$(this).width(maxWidth);
				if (center && (imgWidth < maxWidth))
					$(this).css("margin-left", ((maxWidth - imgWidth) / 2) +"px");
			});
		};
		function setMaxHeight(elems, maxHeight, center) {
			elems.each(function() {
				imgHeight = $(this).height();
				if (imgHeight > maxHeight)
					$(this).height(maxHeight);
				if (center && (imgHeight < maxHeight))
					$(this).css("margin-top", ((maxHeight - imgHeight) / 2) +"px");
			});
		};
		setMaxSize($("#partner-list ul li a img"), 160, 60, false, true);
		setMaxSize($(".shortnews img"), 250, 150, false, false);
		setMaxSize($("li.gallery-item .gallery-main"), 276, 200, false, false);
		setMaxWidth($(".tabs .tab-content"), 940, false);
		setMaxWidth($("footer nav"), 650, false);
		setMaxWidth($(".persons figure"), 95, false);
		setMaxSize($(".pride-list figure img"), 95, 190, false, false);

		/*$(".sldCaption, .filetype").supersleight({shim: "../img/transparent.gif"});*/

		try {
			document.execCommand("BackgroundImageCache", false, true);
		} catch(e) {}
	};


	$(".cool-menu ul").each(function() {
		$(this).parent("li").addClass("cool-sub");
	});

	if ($("#search-form").length) {
		$("#search-form").setPlaceholder()
	};

	$("#slides").slides({
		preload: true,
		preloadImage: "/img/loading.gif",
		play: 5000,
		pause: 2500,
		hoverPause: true,
		randomize: true,
		effect: "slide, fade",
		container: "sldContainer",
		next: "sldNext",
		prev: "sldPrev",
		paginationClass: "sldPagination",
		caption: "sldCaption",
		animationStart: function(current){
			$("#slides .sldCaption").animate({
				bottom: -30
			}, 100);
		},
		animationComplete: function(current){
			$("#slides .sldCaption").animate({
				bottom: 0
			}, 200);
		},
		slidesLoaded: function() {
			$("#slides .sldCaption").animate({
				bottom: 0
			}, 200);
		}
	});

	$(".hr a").click(
		function (e) {
			$("html, body").animate({scrollTop: "0px"}, 800);
			return false;
		}
	);

	$(".normalTip").aToolTip();  

	if ($("#contact-form").length) {
		$("#contact-form").setErrorCleaner()
	};

	$("a[rel^='prettyPhoto']").prettyPhoto({theme:"facebook"});

	// lightbox image
	$(".lightbox-image").append("<span></span>");
	$(".lightbox-image").hover(
		function(){
			$(this).find("img").stop().animate({opacity:0.5}, "normal")
		},
		function(){
			$(this).find("img").stop().animate({opacity:1}, "normal")
		}
	);

	$(".slideDown dt").click(function(){
		$(this).toggleClass("active").parent(".slideDown").find("dd").slideToggle();
	});

	$(".slideDownBasic .slideCaption").click(function(){
		$(this).toggleClass("active").parent(".slideDownBasic").find("dl").slideToggle();
	});

	$(".accordion dt").eq().addClass("active");
	$(".accordion dd").eq().show();
	$(".accordion dt").click(function(){
		$(this).next(".accordion dd")
			.slideToggle("slow")
			.siblings(".accordion dd:visible")
			.slideUp("slow")
			.toggleClass("active")
			.siblings(".accordion dt")
			.removeClass("active");
		return false;
	});

	$("#partner-list").jCarouselLite({
		btnNext: "#prtnext",
		btnPrev: "#prtprev",
		visible: 5,
		speed: 500,
		mouseWheel: true
	});


	// did not tested with old browsers
	if ($(".ad-gallery").length) {
		$(".ad-image-wrapper").css("display", "block");
		$(".ad-thumbs").css("height", "70px");
		$(".ad-gallery .ad-thumbs li").css("margin-bottom", "0px");
		var galleries = $(".ad-gallery").adGallery({
			loader_image: "/img/loader.gif",
			image_offset: 24,
			thumb_opacity: 0.7,
			animate_first_image: true,
			animation_speed: 750,
			effect: "slide-hori", //"slide-hori" or "slide-vert" or "fade" or "resize" or "none"
			enable_keyboard_move: false,
			slideshow: {
				enable: false,
				autostart: false,
				start_label: "Старт",
				stop_label: "Стоп",
				stop_on_scroll: true,
				countdown_prefix: "(",
				countdown_sufix: " сек)"
			}
		});
	};
});

/* wait for images to load */
$(window).load(function() {
	/*$("figure figcaption").each(function() {
		$(this).width($(this).parent("figure").find("img").width());
	});*/

	$("ul.galleries").each(function() {
		setEqualHeight($("li.gallery-item", this));
	});
});
