 
    $(document).ready(function(){
	$('.wrapHeadBlock li.itemLang').dropDownFr();
	$('.wrapHeadBlock div.itemMore').dropDownFr();
	$('.wrapSearchForm div.itemMore').dropDownFr();
	$('#sText').typeAhead();
	$('.viewPopup').dropDownFr();
	$('.wrapNav li').dropMenuFr();
	$('.wrapPage').resizeColFr();
	$('.wrapLoginForm form').inputValueFr();
	$('.multiline:not(.researchTable)').tablesorter({widgets: ['zebra']});
	//two lines below are painting "zebra" on issue summary and issuer summary page
	$(".zebra tr.noneBorder").length % 2 != 0 ? $('.zebra tr:even').addClass("odd") : $('.zebra tr:odd').addClass("odd");
	$('#lsi_right tr:odd').addClass("odd"); 
	$("#researchIndex").researchIndex();
});

function ___serialize(data){

    if (typeof data.regions == "string")
        data.regions = data.regions.split(",");
    if (typeof data.roles == "string")
        data.roles = data.roles.split(",");

    var str = [];
    for(var i in data){
        str.push(i +"="+ encodeURI(data[i]));
    }
    return str.join("&");
}

$(function() {
	$(".treeview").treeview({
	collapsed: true,
	animated: "medium",
	control:"#sidetreecontrol",
	prerendered: true,
	persist: "location"
	});
})

$(function() {
    // --- Using the default options:
    $("#content .collapseHead, #content .wrapIssuesItemHead").toggler();
    // --- Other options:
    //$(".collapseHead, .wrapIssuesItemHead").toggler({method: "toggle", speed: 0});
    //$(".collapseHead, .wrapIssuesItemHead").toggler({method: "toggle"});
    //$(".collapseHead, .wrapIssuesItemHead").toggler({speed: "fast"});
    //$(".collapseHead, .wrapIssuesItemHead").toggler({method: "fadeToggle"});
    //$(".collapseHead, .wrapIssuesItemHead").toggler({method: "slideFadeToggle"});    
    $("#content").expandAll({trigger: "#content .collapseHead", ref: "div.subContent"});
});

$(document).ready(function(){
	$('.collapseItem').click(function(){
		$(this).parent().parent().parent().children('.collapseBlock, .endecaBreadcrumb').slideToggle("slow");
		$(this).toggleClass('expandItem');
	});
});

$(function () {
    var tabContainers = $('div.headTabs > div');
    tabContainers.hide().filter(':first').show();
    $('div.headTabs ul.tabNavigation a').click(function () {
        tabContainers.hide();
        tabContainers.filter(this.hash).show();
        $('div.headTabs ul.tabNavigation a').removeClass('selected');
        $(this).addClass('selected');
        return false;
    }).filter(':first').click();
});

$(function () {
    var tabContainers = $('div.subTabs > div');
    tabContainers.hide().filter(':first').show();
    $('div.subTabs ul.subTabsNavigation a').click(function () {
        tabContainers.hide();
        tabContainers.filter(this.hash).show();
        $('div.subTabs ul.subTabsNavigation a').removeClass('selected');
        $(this).addClass('selected');
        return false;
    }).filter(':first').click();
});

if(jQuery) (function($){
	$.fn.dropDownFr = function(params){
		var conf = $.extend({
			headerBlock:'a', contentBlock:'ul, div', hoverClass:'itemHover'
		}, params);
		return this.each(function(){
			var o=$(this),c=conf,h=o.children(c.headerBlock),w=o.children(c.contentBlock);
			if(w.html() != null){
				w.css({display:'none'});
				mouse_is_inside=false;
				h.bind('click',function(){w.css({display:'block'}); h.addClass(c.hoverClass); return false});
				//h.bind('mouseleave',function(){w.css({display:'none'}); h.removeClass(c.hoverClass)});
				w.bind('mouseenter',function(){ mouse_is_inside=true; h.addClass(c.hoverClass)});
				w.bind('mouseleave',function(){ mouse_is_inside=false; });
				$("body").mouseup(function(){ 
			        if(! mouse_is_inside){w.css({display:'none'}); h.removeClass(c.hoverClass)};
			    });
			}
		});
	};
/*
	$.fn.tablesorter = function(params){
		$('.header').clik('table.multiline tr:nth-child(even)').addClass("even");
	};
*/
	$.fn.dropMenuFr = function(params){
		var conf = $.extend({
			headerBlock:'a', contentBlock:'ul', hoverClass:'itemHover', hoverSubClass:'itemSubHover'
		}, params);
		return this.each(function(){
			var o=$(this),c=conf,h=o.children(c.headerBlock),w=o.children(c.contentBlock);
			if(w.html() != null){
				w.css({display:'none'});
				o.bind('mouseenter',function(){w.css({display:'block'}); o.addClass(c.hoverSubClass)});
				o.bind('mouseleave',function(){w.css({display:'none'}); o.removeClass(c.hoverSubClass)});
			}else{
				o.hover(
					function(){o.addClass(c.hoverClass)},
					function(){o.removeClass(c.hoverClass)}
				)
			}
		});
	};
	
	$.fn.replaceWith = function(replacement) {
		return this.each(function(){
			element = $(this);
			$(this)
			.after(replacement).next()
			.attr('class', element.attr('class')).attr('id',element.attr('id'))
			.html(element.html())
			.prev().remove();
		});
	};
	
	$.fn.researchIndex = function(params){
		var conf = $.extend({
			listId:'researchIndex', listElement:'ul',listItemElement:'li'
		}, params);
		return this.each(function(){
			var list=$(this),
				c=conf,
				firstLink=list.children("li:first-child").children("a:first-child"),
				lastId=firstLink.attr("href");
			$(".reportContent div.sectionTab").hide();
			$(lastId).show();
			list.children("li").each(function() {
				$(this).append("<span>"+$(this).children("a:first-child").html()+"</span>");
				$(this).children("span").hide();
				$(this).click(function(e) {
					e.preventDefault(); 
					$(lastId).hide();
					$("a[href*="+lastId+"]").show();
					$("a[href*="+lastId+"]").siblings(':first').hide();
					pId=$(this).children("a:first-child").attr("href");
                    if($("#researchIndex").children(':first').children("a:first-child").attr("href") == pId)
                        $(".wrapLeftBar div.otherBlock,.wrapLeftBar div.financial,.wrapRightContent div.ratingsWrapper").show();
                    else 
                        $(".wrapLeftBar div.otherBlock,.wrapLeftBar div.financial,.wrapRightContent div.ratingsWrapper").hide();
					$(this).children("a").hide();
					$(this).children("span").show();
					$(pId).show();		
					lastId=pId;
				});
			});
            var unitBlocks = $(".reportContent").children("div:nth-child(2)").children(".unitBlock");
            var firstSectionId = $(".reportContent").children("div:nth-child(2)").attr('id');
            $(".reportContent").children("div:nth-child(2)").attr('id', "");
            $(".mainContent div.sectionTab").attr('id', firstSectionId);
            unitBlocks.each(function() {
                $(".mainContent div.sectionTab").append($(this));
            });

            if(list.children("li").length>4)
            {
                $(".reportContent div.sectionTab").css("margin-top","0px");
            }
			$("a[href*="+lastId+"]").hide();
			$("a[href*="+lastId+"]").siblings(':first').show();
		});
	};
	$.fn.typeAhead = function(params){
		var conf = $.extend({
			minLettersAmount:2,
			headerBlock:'a', contentBlock:'ul', hoverClass:'itemHover', hoverSubClass:'itemSubHover'
		}, params);
		return this.each(function(){
			var searchInput=$(this),
				c=conf,
				typeAheadBlock=$('.typeAhead'),
				typeAheadContent=$('.typeAhead .typeAheadContent'),
				typeAheadSummaryList=$('.typeAhead ul.taSummary'),
				typeAheadSearchList=$('.typeAhead ul.taSearch'),
				waitBlock=$('#taWaitBlock'),
				suggestTable=$('.typeAhead .typeAheadContent table');
				$('.typeAhead ul.taSummary li').width((searchInput.width()/2)-40);
				$('.typeAhead ul.taSearch li').width((searchInput.width()/2)-40);
				
				mouse_is_inside=false;
				isFocused=false;
				
				$("body").mouseup(function(){ 
			        if(! mouse_is_inside){typeAheadBlock.hide();}else{isFocused=true};
			    });
				typeAheadBlock.bind('mouseenter',function(){ mouse_is_inside=true;});
				typeAheadBlock.bind('mouseleave',function(){ mouse_is_inside=false;});
				searchInput.bind('mouseenter',function(){ mouse_is_inside=true;});
				searchInput.bind('mouseleave',function(){ mouse_is_inside=false;});
				waitBlock.bind('mouseover',function(){ mouse_is_inside=true;});
				waitBlock.bind('mouseleave',function(){ mouse_is_inside=false;});
				
				
				searchInput.focus(function(){isFocused=true});
				searchInput.blur(function(){isFocused=false});
				
				contentPaddingWidth=parseInt(typeAheadContent.css("padding-left"), 10) + parseInt(typeAheadContent.css("padding-right"), 10);
				contentBorderWidth=parseInt(typeAheadContent.css("borderLeftWidth"), 10) + parseInt(typeAheadContent.css("borderRightWidth"), 10)
				inputPaddingWidth=parseInt(searchInput.css("padding-left"), 10) + parseInt(searchInput.css("padding-right"), 10);
				inputBorderWidth=parseInt(searchInput.css("borderLeftWidth"), 10) + parseInt(searchInput.css("borderRightWidth"), 10)
				inputWidth=searchInput.width()+inputPaddingWidth+inputBorderWidth;
				
				jsonRequest=null;
				oldInputValue=searchInput.val();
				
				searchInput.keyup(function() {
					if(oldInputValue!=searchInput.val()){
						oldInputValue=searchInput.val();
						if($(this).val().length > c.minLettersAmount){
							if(typeAheadBlock.is(':visible')){
								waitBlock.width(inputWidth);
								waitBlock.height(typeAheadBlock.height());
								waitBlock.show();
							}						
							if(jsonRequest!=null){jsonRequest.abort()};
							var searchTerm = $(this).val();
							jsonRequest = $.ajax({
							    type: "POST",
							    url: context+"/"+langId+"/typeahead.json",
							   	dataType: "json",
							    data:___serialize({searchTerm: $(this).val()}),
							    success: function(data){
								  if(data!=null && data.typeAheadElements!=null && data.typeAheadElements.length==0){
									  typeAheadBlock.hide();
									  waitBlock.hide();
								  }else{
									  if(isFocused && data!=null && data.typeAheadElements!=null){
										  typeAheadSummaryList.children().remove();
										  typeAheadSearchList.children().remove();
										  $.each(data.typeAheadElements, function(i,item){
											  suggestName="";
											  if(item.localizedName==null){suggestName=item.name}else{suggestName=item.localizedName};
								        	  summaryLinkElement='<li><a href="'+context+"/"+langId+''+item.issuerSummaryPageUrl+'" title="'+suggestName+'" >'+suggestName+'</a></li>';
								        	  searchLinkElement='<li><a href="'+context+"/"+langId+'/search?'+item.searchByGroupIdUrl+'&searchType=TYPEAHEAD" title="'+suggestName+'" >'+suggestName+'</a></li>';
								        	  $(summaryLinkElement).appendTo(typeAheadSummaryList);
								        	  $(searchLinkElement).appendTo(typeAheadSearchList);
								          });
										  typeAheadBlock.show();
										  suggestTable.width(inputWidth-contentPaddingWidth-contentBorderWidth);
										  waitBlock.hide();
									  }
								  }
					        	}
							});
						}else{
							typeAheadBlock.hide();
							waitBlock.hide();
						}			
					}		
				});

		});
	};
	
	$.fn.inputValueFr = function(params){
		var conf = $.extend({
			textInput:'input:text, input:password'
		}, params);
		return this.each(function(){
			var o=$(this),f=this,c=conf,k=o.find(c.textInput);
			k.each(function(){
				var i=$(this),t,p=false,it;
				if(i.attr('type')=='password'){p=true}
				t=o.find('label[for='+i.attr('id')+']').text();
				if(t==null){t='Empty label'}
					if(p){
						it=document.createElement('input');
						it=$(it).addClass(i.attr('class')).attr('type','text');
						it.addClass ('lightColor');
						it.insertBefore(i);
						it.val(t);
						if(i.val()==''){
							i.css({display:'none'});
						}else{
							it.detach();
						}
						it.bind('focus', function(){
							it.detach(); i.css({display:'block'}).focus();
						});
						i.bind('blur', function(){
							if(i.val()==''){
								it.insertBefore(i); i.css({display:'none'}); it.val(t);;
							}
						});
						o.submit(function(){it.detach(); i.css({display:'block'})});
					}else{
						if(i.val()==''){i.val(t)}
						i.bind('focus', function(){
							if(i.val()==t){i.val('').removeClass('lightColor')}
						});
						i.bind('blur', function(){
							if(i.val()==''){i.val(t).addClass('lightColor')}
						})
						o.submit(function(){
							if((i.val()==t)||(i.val()=='')){i.val('')}
						});
					}
			})
		});
	};

	$.fn.resizeColFr = function(params){
		var conf = $.extend({
			leftBlock:'.stationAbout, .stationTopics', rightBlock:'.stationStory, .stationFeature'
		}, params);
		return this.each(function(){
			var o=$(this),c=conf,r=o.find(c.rightBlock),l=o.find(c.leftBlock),h=0,lh=0;
			l.each(function(){h+=$(this).height();});
			r.each(function(){if($(this).height()>h){h=$(this).height()}});
			lh=h;
			l.each(function(i){
				if(l.size()!=i+1){$(this).height($(this).height()); lh-=$(this).height();}
				else{$(this).height(lh)}
			});
			r.each(function(){$(this).height(h);})
		});
	};

	$.fn.dropDownDh1 = function(params){
		var conf = $.extend({
			bindAction:'click',
			activeClass:'itemActive'
		}, params);
		return this.each(function(){
			var o=$(this),c=conf,p=o.parent().eq(0),m=o.parent().children().eq(0);
			if(o.html() != null){
				o.css({visibility:'hidden',opacity:0});
				p.hoverIntent({
					timeout: 150,
					over: function(){
						if(c.bindAction == 'hover'){
							o.css('opacity', 0).css('visibility', 'visible').animate({opacity:1}, 100);
							p.addClass(c.activeClass);
						}
					},
					out: function(){
						o.animate({opacity:0}, 100, function(){
							o.css('visibility', 'hidden');
							p.children().eq(0).removeClass('active');
							p.removeClass(c.activeClass);
						});
					}
				});
				if(c.bindAction == 'click'){
					m.click(function(){
						if(o.css('visibility') == 'hidden') {
							o.css('opacity', 0).css('visibility', 'visible').animate({opacity:1}, 100);
							m.addClass(c.activeClass);
						} else {
							o.animate({opacity:0}, 100, function(){
								o.css('visibility', 'hidden');
								m.removeClass(c.activeClass);
							});
						}
						return false;
					});
				}
			}
		});
	};

	$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};
	})(jQuery)
