function setcookie(name, value, expires, path, domain, secure) 
{   
  // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)  
  expires instanceof Date ? expires = expires.toGMTString() : typeof(expires) == 'number' && (expires = (new Date(+(new Date) + expires * 1e3)).toGMTString());  
  var r = [name + "=" + escape(value)], s, i;  
  for(i in s = {expires: expires, path: path, domain: domain})
  {
		s[i] && r.push(i + "=" + s[i]);  
  }  
  return secure && r.push("secure"), document.cookie = r.join(";"), true;  
}

function text_input_focus(obj)
{
	if ($(obj).hasClass('form-empty')) { $(obj).removeClass('form-empty'); obj.value=''}				
}
function text_input_blur(obj,type_str)
{
	if (obj.value=='') { $(obj).addClass('form-empty'); obj.value=type_str;}
}

$(document).ready(function(){

	// Main Menu for Investfunds UA
	var menuTimer;
	var menuItems = $('div.menu>ul>li:not(.menu-item-first)');
	var subMenuItems = $('div.post-header-body');
	menuItems.mouseenter(function() {
		menuItems.removeClass('menu-item-active');
		$(this).addClass('menu-item-active');
		subMenuItems.removeClass('sub-item-active');
		subMenuItems.filter('.'+$(this).attr('id')).addClass('sub-item-active');
	});
	$('div.menu').mouseleave(function() {
		menuTimer = window.setTimeout(function() {
			menuItems.removeClass('menu-item-active');
			$('div.menu li.default').addClass('menu-item-active');
			subMenuItems.removeClass('sub-item-active');
			$('div.post-header div.default').addClass('sub-item-active');
		}, 2000);
	});
	$('div.menu').mouseenter(function() {
		window.clearTimeout(menuTimer);
	});
	//

	$("#analitics-calendar1").datepicker({
		showOn: "button",
		buttonImage: "/images/icon-calendar.png",
		buttonImageOnly: true,
		onSelect: function(dateText, inst)
		{
			datepicker_redirect_analitics(dateText);
		}
	});

	$("#analitics-calendar2").datepicker({
		onSelect: function(dateText, inst)
		{
			datepicker_redirect_analitics(dateText);
		}
	});

	$("#news-calendar1").datepicker({
		showOn: "button",
		buttonImage: "/images/icon-calendar.png",
		buttonImageOnly: true,
		onSelect: function(dateText, inst)
		{
			datepicker_redirect_news(dateText);
		}
	});

	$("#news-calendar2").datepicker({
		onSelect: function(dateText, inst)
		{
			datepicker_redirect_news(dateText);
		}
	});

	$("#quotes-calendar").datepicker();
	$(".npf-indicators-calendar").datepicker();

	$('img.calendar-button').click(function(){
		$(this).prev().focus();
	});

	var showLinkActiveName = 'show-link-active';
	$("a.show-link").click(function(event){
		event.preventDefault();
		var link = $(this).attr('href');
		var allLinks = $("a[href='"+link+"']");
		if ($(this).hasClass(showLinkActiveName)) {
			allLinks.removeClass(showLinkActiveName);
			$(link).slideUp();
			$(link+'_etc').show();
		} else {
			allLinks.addClass(showLinkActiveName);
			$(link).slideDown();
			$(link+'_etc').hide();
		}
	});


	$("a.popup-layer-link")
	.unbind('click')
	.live('click', function(event) {
		event.preventDefault();
		$($(this).attr('href')).modal({
			opacity: 75,
			overlayCss: { backgroundColor: "#000" },
			overlayClose: true,
			escClose: true
		});
	});

});

function datepicker_redirect_analitics(dateText)
{
	var full_path=window.location.pathname;
	RegExp=/^(\/[^\/]+)(\/[^\/]+\/[^\/]+\/).*/;
	cur_path=RegExp.exec(full_path);
	if (cur_path!=null)
	{
		cur_prefix=cur_path[1];
		cur_path=cur_path[2];		
		if (cur_path=='/analitics/companies/' || cur_path=='/analitics/emitents/')
		{		
			RegExp=/^\/[^\/]+\/[^\/]+\/[^\/]+\/([^\/]+)\/.*/;
			cur_path2=RegExp.exec(full_path);
			if (cur_path2!=null)
			{
				cur_path2=cur_path2[1];
				RegExp=/[\d]{4}\-[\d]{1,2}\-[\d]{1,2}/;
				if (!RegExp.test(cur_path2))
				{
					cur_path=cur_path+cur_path2+'/';
				}
			}
		}
	}
	if (cur_path=='/analitics/today/' || cur_path==null){cur_path='/analitics/all/';}
	document.location=cur_prefix+cur_path+dateText.substring(6,10)+'-'+dateText.substring(3,5)+'-'+dateText.substring(0,2)+'/';		
}


function datepicker_redirect_news(dateText)
{
	var full_path=window.location.pathname;
	RegExp=/^(\/[^\/]+\/[^\/]+\/).*/;
	cur_path=RegExp.exec(full_path);
	if (cur_path!=null)
	{
		cur_path=cur_path[1];		
		if (cur_path=='/news/companies/' || cur_path=='/news/emitents/' || cur_path=='/news/tags/' || cur_path=='/news/npf/')
		{		
			RegExp=/^\/[^\/]+\/[^\/]+\/([^\/]+)\/.*/;
			cur_path2=RegExp.exec(full_path);
			if (cur_path2!=null)
			{
				cur_path2=cur_path2[1];
				RegExp=/[\d]{4}\-[\d]{1,2}\-[\d]{1,2}/;
				if (!RegExp.test(cur_path2))
				{
					cur_path=cur_path+cur_path2+'/';
				}
			}
		}
	}
	if (cur_path=='/news/today/' || cur_path==null){cur_path='/news/all/';}
	document.location=cur_path+dateText.substring(6,10)+'-'+dateText.substring(3,5)+'-'+dateText.substring(0,2)+'/';	
}

function set_html_text(obj,text1,text2)
{
	obj=$(obj);
	if(obj.html()==text1)
	{
		obj.html(text2);
	}
	else
	{
		obj.html(text1);
	}
}

/*
	$('.tabs-ajax li a').click(function(event){
		event.preventDefault();
		$(this).parent().addClass('tab-active').siblings().removeClass('tab-active');
		// Сюда добавить нужное действие при выборе нового таба
	});
*/
jQuery.fn.bookmarks = function() {
    $(this).each( function() {
        var self = this;
        $('.tabs a', this).click(function() {
            var hash = $('li.tab-active a', self).attr('hash');
            hash = hash.replace('#', '');
            $('#'+hash).addClass('cb-none');
            $('li.tab-active', self).removeClass('tab-active');

            hash = $(this).attr('hash');
            hash = hash.replace('#', '');
            $('#'+hash).removeClass('cb-none');
            $(this).parent('li').addClass('tab-active');
            return false;
        });

    });

    $(".tabs a[href='" + window.location.hash + "']", this).trigger('click');
}
