Array.prototype.in_array = function(p_val)
{
	for(var i = 0, l = this.length; i < l; i++)
	{
		if(this[i] == p_val)
		{
			return true;
		}
	}
	return false;
}

jQuery.cookie = function (key, value, options) {

	// key and at least value given, set cookie...
	if (arguments.length > 1 && String(value) !== "[object Object]") {
		options = jQuery.extend({}, options);

		if (value === null || value === undefined) {
			options.expires = -1;
		}

		if (typeof options.expires === 'number') {
			var days = options.expires, t = options.expires = new Date();
			t.setDate(t.getDate() + days);
		}

		value = String(value);

		return (document.cookie = [
			encodeURIComponent(key), '=',
			options.raw ? value : encodeURIComponent(value),
			options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
			options.path ? '; path=' + options.path : '',
			options.domain ? '; domain=' + options.domain : '',
			options.secure ? '; secure' : ''
		].join(''));
	}

	// key and possibly options given, get cookie...
	options = value || {};
	var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
	return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};



$(function(){
		
	$(".b-weather__link").attr('target','_blank');
	$("#besttoday_informer a").attr('target','_blank');
	
	$("#login-btn").click(function(){
		$("#login-cont").toggle();
		return false;
	});
	
	$("input").each(function(){
		var placeholder = $(this).attr("placeholder");
		if(placeholder){
			$(this).blur(function(){
				if($(this).val() == ''){
					$(this).val(placeholder);
				}
			}).focus(function(){
				if($(this).val() == placeholder){
					$(this).val('');
				}
			});
		}
	});	
	
	if($(".iiblock").length)
	{
		var newTop = 0,
			scroll = $(".iiblock").find(".scroll"),
			block = $(".iiblock").find(".block").eq(0);
			previewHeight = ( parseInt(block.height()) + parseInt(block.css("paddingTop")) + parseInt(block.css("paddingBottom")) + parseInt(block.css("borderTopWidth")) + parseInt(block.css("borderBottomWidth")) + ( parseInt(block.css("marginTop")) > parseInt(block.css("marginBottom")) ? parseInt(block.css("marginTop")) : parseInt(block.css("marginBottom")) ) ) * 2;
		
		$(".iiblock .btn").click(function(){
			var scrollTop = parseInt(scroll.css("top"));
			
			if(scroll.data("blocked") == "yes") return false;
			scroll.data("blocked","yes");
			
			scrollTop = isNaN(scrollTop) ? 0 : scrollTop;
			
			scroll.css("position","absolute");
			var scrollHeight = scroll.height();
			scroll.css("position","relative");

			if($(this).attr("href") == "#top"){
				newTop = scrollTop + previewHeight;
				if(newTop > 0) newTop = 0;
			}else{
				newTop = scrollTop - previewHeight;
				if(newTop < (scrollHeight * -1) + previewHeight) newTop = (scrollHeight * -1) + previewHeight;
			}

			scroll.animate({top: newTop + "px"}, function(){
				scroll.data("blocked","no");
			});
			
			return false;
		});
	}
	
	if($("#nomer").length)
	{
		var nomer_tabs = $("#nomer .tab"),
			nomer_imgs = $("#nomer .tabimg"),
			nomer_a = $("#nomer .menu-ul a");
		
		nomer_a.click(function(){
			if($(this).hasClass("active")) return false;
			var index = $(this).parent().index();
			nomer_a.removeClass("active");
			$(this).addClass("active");
			
			nomer_tabs.filter(":visible").fadeOut(function(){
				nomer_tabs.eq(index).fadeIn();
			});
			
			nomer_imgs.filter(":visible").fadeOut(function(){
				nomer_imgs.eq(index).fadeIn();
			});
	
			return false;
		});
	}
	
	
	if($(".tabs-block"))
	{
		$(".tabs-block").each(function(){
			$(this).find(".tab").each(function(i,e){
				if(i != 0) $(this).hide();
			});
		});
		$("a[rel=tab]").click(function(){
			$(this).siblings().removeClass("active");
			$(this).addClass("active");
			$(this).parents(".tabs-block").find(".tab").hide();
			$(this).parents(".tabs-block").find(".tab").eq($(this).index()).show();
			
			return false;
		});
	}
	
	
	if($(".fotogallery").length)
	{
		var album = false;
		if($(".photo-album-view").length)
		{
			album = true;
		}
		
		if(album)
		{
			$(".fotogallery .link-prev").click(function(){
				$(".photo-album-view ins.select").prev().find('a').click();
				return false;
			});
			
			$(".fotogallery .link-next").click(function(){
				$(".photo-album-view ins.select").next().find('a').click();
				return false;
			});
		}
		
		$(".fotogallery .preview a").click(function(){
			if(album)
			{
				$(".photo-album-view ins").removeClass('select');
				$(this).parents("ins").addClass('select');
				
				$(this).parents(".fotogallery").find(".view").attr("src", $(this).attr("fullimg") );
				$(".photo-album-view .descimg").html( $(this).attr("title") );
				
				$(".photo-album-view .item-num").html($(".photo-album-view ins.select").index() + 1);
			}
			else
			{
				$(this).parents(".fotogallery").find(".view").attr("src", $(this).attr("href") );
			}
			
			return false;
		});
		
		var scroll = $(".fotogallery .scroll"),
			previewWidth = $(".fotogallery .preview").width();
		
		$(".fotogallery .btn").click(function(){
			var newLeft = 0,
				scrollLeft = parseInt(scroll.css("left"));
			
			if(scroll.data("blocked") == "yes") return false;
			scroll.data("blocked","yes");
			
			scrollLeft = isNaN(scrollLeft) ? 0 : scrollLeft;
			
			scroll.css("position","absolute");
			var scrollWidth = scroll.width();
			scroll.css("position","relative");

			if($(this).attr("href") == "#left"){
				newLeft = scrollLeft + previewWidth;
				if(newLeft > 0) newLeft = 0;
			}else{
				newLeft = scrollLeft - previewWidth;
				if(newLeft < (scrollWidth * -1) + previewWidth) newLeft = (scrollWidth * -1) + previewWidth;
			}
			
			scroll.animate({left: newLeft + "px"}, function(){
				scroll.data("blocked","no");
			});
			
			return false;
		});
	}
	
	(function(){
		var newblocks = $(".newblock");
		if(newblocks.length)
		{
			function remember(){
				var arr = [];
				newblocks.has(".block:visible").each(function(){
					arr.push($(this).attr("id"));
				});
				
				jQuery.cookie('newblocks',arr.join(','),{expires:365,path:'/'});
			}
			
			var blocks = jQuery.cookie('newblocks');
			if(blocks)
			{
				blocks = blocks.split(',');
				
				newblocks.each(function(){
					if(blocks && blocks.in_array($(this).attr("id"))){
						$(this).find(".ahide").show();
						$(this).find(".block").show();
					}else{
						$(this).find(".ashow").show();
						$(this).find(".block").hide();
					}
				});
			}
			else
			{
				newblocks.each(function(i,e){
					if(i < 4){
						$(this).find(".ahide").show();
					}else{
						$(this).find(".ashow").show();
						$(this).find(".block").hide();
					}
				});
			}
			
			newblocks.find(".ashow").click(function(){
				$(this).hide();
				$(this).siblings(".ahide").show();
				$(this).parents(".newblock").children(".block").show();
				remember();
				return false;
			});
			newblocks.find(".ahide").click(function(){
				$(this).hide();
				$(this).siblings(".ashow").show();
				$(this).parents(".newblock").children(".block").hide();
				remember();
				return false;
			});
		}
	})();
	
	
	(function(){
		var archive = $(".archive"),
			loading = archive.find('.loading');
		
		if(archive.length)
		{
			archive.find(".need").change(function(){
				var year  = archive.find("select[name=year] option:selected").val(),
					month = archive.find("select[name=month] option:selected").val();
				
				loading.show();
				
				$.ajax({
					url: '/udata/custom/getarchivenomers/'+year+'/'+month+'/',
					success: function(xml){
						var text = $(xml).find('udata').text() || '';
						archive.find("select[name=nomer]").html(text);
						
						archive.removeData('pdf');
						archive.removeData('rar');
						
						actionUpdate();
					},
					dataType: 'xml',
					complete: function(){
						loading.hide();
					}
				});
			});
			
			archive.find("select[name=nomer]").change(function(){
				loading.show();
				
				$.ajax({
					url: '/uobject/'+$(this).find("option:selected").val()+'/',
					success: function(xml){
						var pdf = $(xml).find('property#376 value').text() || '',
							rar = $(xml).find('property#377 value').text() || '';
						
						archive.data('pdf', pdf);
						archive.data('rar', rar);
						
						actionUpdate();
					},
					dataType: 'xml',
					complete: function(){
						loading.hide();
					}
				});
			});
			
			archive.find("input[type=radio]").change(function(){
				actionUpdate();
			});
			
			function actionUpdate()
			{
				archive.find("input[type=submit]").attr("disabled","disabled");
				
				var key = $("#archive-form").find("input[type=radio]:checked").val();
				if(key)
				{
					var data = archive.data(key);
					if(data)
					{
						$("#archive-form").attr('action', data);
						archive.find("input[type=submit]").removeAttr("disabled");
					}
				}
			}
			
			archive.find("select[name=year] option:first").attr("selected","selected");
			archive.find(".need").trigger('change');
		}
	})();
	
	(function(){
		var archivecal = $(".archivecal");
		
		if(archivecal.length)
		{
			archivecal.delegate(".month a", "click", function(){
				$.ajax({
					url: '/udata/custom/getarchivecal/'+$(this).attr('href')+'/',
					success: function(xml){
						var text = $(xml).find('udata').text() || '';
						archivecal.find(".block").html(text);
					},
					dataType: 'xml'
				});
				
				return false;
			});
		}
	})();
	
	$("a[rel=lastmag]").fancybox();
})
