function showHistories(year, month)
{
	$('div.history_content div.year-all').hide();
	$('div.history_content div.year-all div').hide();

	if ( year > 0 )
	{
		if ( month == '' )
		{
			$('div.history_content div.'+year+'-all').show();
			$('div.history_content div.'+year+'-all div').show();
		}
		else
		{
			$('div.history_content div.'+year+'-all').show();
			$('div.history_content div.'+year+'-all div.' + year + '-' + month).show();
		}
	}
}

$(function(){

	$('.uf-image').each(function(index){
		text = $(this).attr('alt');
		width = $(this).width();
		$(this).after('<div class="uf" style="width:'+width+'px"><div><div><div>'+text+'</div></div></div></div>');
	});

	$('div.content table th:first-child').css('background-position','-2px bottom');

	$('.content').find('.withsignature').each(function(){
		var thisWidth = $(this).width();
		var thisText = $(this).attr('alt');
		$(this).css('display','block');
		$('<div class="withsignatureafter" style="width:'+thisWidth+'px"><div class="withsignatureafter">'+thisText+'</div></div>').insertAfter( $(this) );
	});

	var sshare = $('.sshare');

	sshare.hover(
		function () {
			$(this).addClass('sshare_open');
		}, 
		function () {
			$(this).removeClass('sshare_open');
		}
	);

	var rss_menu = $('.f_rss_menu');
    $('div.f_rss a.f_rss_link').click(function(){
        if ( rss_menu.css('display') == 'none' )
        {
            rss_menu.fadeIn(200);
        }
        else
        {
            rss_menu.fadeOut(200);
        }
        return false;
	});

	$('div.f_rss_menu').mouseleave(function(){
		rss_menu.fadeOut(200);
	});




    $('.menu > ul > li > a').click(function(){

    	$('.menu > ul > li > a').removeClass('have_child_hover');
        $('.menu_dropdown').hide();

        var submenu = $(this).siblings('.menu_dropdown');

        if ( submenu.length > 0 )
        {

            if( submenu.css('display') == 'none' )
            {
                $(this).addClass('have_child_hover');
                submenu.show();
            }
            else
            {
                submenu.hide();
            }
            return false;
        }
    });

    $('.menu_dropdown > .menu_dropdown_inner > ul > li > a').mouseover(function(){

        $('.menu_dropdown > .menu_dropdown_inner > ul > li > a').removeClass('have_child_hover');
        $('.menu_dropdown ul ul').hide();

        var submenu = $(this).siblings('ul');

        if( submenu.css('display') == 'none' )
        {
            $(this).addClass('have_child_hover');
        	submenu.show();
        }

		if ( submenu.offset() )
			offset = submenu.offset().top;
		else
			offset = 0;

        var diff = offset + submenu.outerHeight() + 15 - submenu.parent('li').parent('ul').height() - 116;
        if ( diff > 0 )
        {
        	submenu.css('top','auto');
        	submenu.css('bottom','0');
			$('.for_node_14').css('top','0');
			$('.for_node_34').css('top','-13px');
			$('.for_node_1034').css('bottom','-32px');
        }
    });

    $('.outer').click(function(){
    	$('.menu > ul > li > a').removeClass('have_child_hover');
        $('.menu_dropdown').hide();
    });

    $('.menu').mouseleave( function() {
    	$('.menu > ul > li > a').removeClass('have_child_hover');
        $('.menu_dropdown').hide();
        //$('#overlay').css('display','none');
    });

    $('form.jqtransform').jqTransform({imgPath:'css/jquery/img/'});

/* mpanel 15.05.2011 SeRG!: перенес это в widget*/
/*
    var items = new Array();
    var prev_block = $('.mpanel_prev');
    var next_block = $('.mpanel_next');
    var current = 0;

    function setCurrent(current){
        $('.mpanel_list').find('a').each( function(i,item){
            if ( i == current )
            {
                $('.mpanel_list a').removeClass('current');
                $(this).addClass('current');
				
				$('.mpanel').fadeOut(200, function(){
					$('.mpanel').css('background-image','url(img/showcase' + (i+1) + 'a.jpg)');
					$('.mpanel .title').html( dataArray[i]['title'] );
					$('.mpanel .text p').html( dataArray[i]['text'] );
					$('.mpanel').fadeIn(200);
				});

            }
        });
    }

    var length = dataArray.length;

    var pagingTable = $("<table/>").attr('align','center').addClass('mpanel_list').appendTo('div.mpanel_content_body');
    var pagingTableTr = $("<tr/>").appendTo(pagingTable);

    for (var i=0; i<length; i++)
    {
        var pagingTableTd = $("<td/>").html('<a href="#'+(i+1)+'" class="current"><img src="img/blank.gif" alt="" /></a>').appendTo(pagingTableTr);
    }

    $('.mpanel_next').click( function(){
        if ( current + 1 == length )
            current = 0;
        else
            current = current + 1;

        setCurrent(current, length);
		return false;
    });

    $('.mpanel_prev').click( function(){
        if ( current - 1 < 0 )
            current = length - 1;
        else
            current = current - 1;

        setCurrent(current, length);
		return false;
    });

    $('.mpanel_list a').click( function(){
		href = $(this).attr('href').substring($(this).attr('href').lastIndexOf("/") + 1);
        href = href.replace('#','');
        current = href - 1;
        setCurrent(current);
        return false;
    });

    setCurrent(0);

*/

	$("#annual_report_slider").easySlider({
		prevText: 		'<img src="/upload/blank.gif" alt="" />',
		nextText: 		'<img src="/upload/blank.gif" alt="" />'
	});

	$("div.carousel").carousel({
		loop: true,
        autoSlide:true,
        autoSlideInterval:2600,
		pagination: true,
		effect: "fade",
			animSpeed: 600,
		nextBtn: '<img src="/upload/blank.gif" alt="" />',
		prevBtn: '<img src="/upload/blank.gif" alt="" />'
	});

	$('dl.tabs dt').click(function(){
		$(this)
			.siblings().removeClass('selected').end()
			.next('dd').andSelf().addClass('selected');
		return false;
	});

	$('dl.data dt').click(function(){
		$(this)
			.siblings().removeClass('selected').end()
			.next('dd').andSelf().addClass('selected');
		return false;
	});

/*
	var latest_item = 0;
	var latest_items = 0;

	var latest_text_height = 0;
	var latest_text_heightAll = 0;

	$('.latest_index .list .item .text').hover(function(){
		latest_item = $(this).parent('.item');
		latest_items = $(this).parent('.item').siblings('.item');

		latest_text_height = $(this).height();
		latest_text_heightAll = $(this).children().children('.date').innerHeight() + $(this).children().children('.title').innerHeight();

		latest_text_heightAll = (latest_text_heightAll>latest_text_height)?latest_text_heightAll:latest_text_height;

		//alert( latest_text_height + ', ' + latest_text_heightAll );

		latest_items.removeClass('item_selected');
		latest_items.children().css('z-index','1');
		
		latest_item.addClass('item_selected');
		$(this).css('z-index','2');

		$(this).animate({
			height: latest_text_heightAll
		}, 300);
	},
	function(){
		$(this).animate({
			height: latest_text_height
		}, 100);
	});
*/
	//var latest_items = $('.latest_index .list .item');

	$('.latest_index .list .item .text').hover(function(){
		latest_items = $(this).parent('.item').siblings('.item');
		latest_items.removeClass('item_selected');
		var height = $(this).find('.title').innerHeight();
		height = ( height > 36 ) ? height : 36;
		$(this).stop().animate({
			height: height + 12 + 'px'
		}, 200).css('z-index','2').parent('.item').addClass('item_selected');;
	},
	function(){
		$(this).stop().animate({
			height: 48
		}, 50, function(){
					$(this).css('z-index','1');
		});
	});

	$('.reports_page_block .list li').click(function(){
		$(this)
			.siblings().removeClass('selected').end()
			.andSelf().addClass('selected');
		return false;
	});

	$('#search_top_form input[type="text"]').focus(function() {
		$(this).removeClass("text_default");
		if ( this.value == this.defaultValue ){
			this.value = '';
		}
		else {
				this.select();
			}
	});
	$('#search_top_form input[type="text"]').blur(function() {
		$(this).addClass("text_default");
		if ( $.trim(this.value) == '' ){
			this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});

    $('div.supervisory_list div.accordion div.head:first').addClass('head_open').next('div.body').addClass('body_open');

    var urlParam = $.url.param("division");
    if( urlParam && typeof( urlParam ) != 'undefined' )
    {
        $('div.division_list div.accordion .division_' + urlParam)
            .addClass('head_open')
            .next('div.body').slideDown().addClass('body_open');
		$.scrollTo($('div.division_list div.accordion .division_' + urlParam));
    }

    $('div.accordion div.head').click(function(){
        if ( $(this).hasClass('head_open') )
        {
            $(this).removeClass('head_open');
            $(this).next('div.body').fadeOut(200, function(){ $(this).removeClass('body_open') });
        }
        else
        {
            $(this)
                .addClass('head_open')
                .next('div.body').fadeIn().addClass('body_open');
        }
        return false;
    });

    $('.help dl dt').click(function(){
        if ( $(this).hasClass('close') )
            $(this)
                .removeClass('close').andSelf().addClass('open').end()
                .next('dd').andSelf().removeClass('close').andSelf().addClass('open');
        else
            $(this)
                .removeClass('open').andSelf().addClass('close').end()
                .next('dd').andSelf().removeClass('open').andSelf().addClass('close');
        return false;
    });


    $('div.sitemap a.level').click( function(){
        var parent = $(this).parent('p').parent('li');
        if ( parent.children('.second').length > 0 )
        {
            if ( parent.hasClass('open') )
            {
                parent.children('.second').fadeOut(200, function(){
                    parent.removeClass('open');
                });
            }
            else
            {
                parent.children('.second').fadeIn(400, function(){
                    parent.addClass('open');
                });
            }
        }
        return false;
    });
    $('div.sitemap a.vinos').click( function(){
        var parent = $(this).parent('p').parent('li');
        if ( parent.children('.second').length > 0 )
        {
            if ( parent.hasClass('open') )
            {
                parent.children('.second').hide(400, function(){
                    parent.removeClass('open');
                });
            }
            else
            {
                parent.children('.second').show(800, function(){
                    parent.addClass('open');
                });
            }
        }
        return false;
    });
	$('div.sitemap div.second a').mouseover( function(){
		var description = $(this).parents('div.second').children('div').children('div').children('div').children('div').children('div.description');
		var description_inner = description.children('div.description_inner');
		description_inner.html( $(this).next('span.descr').html() );
		description.show();
	});
	$('div.sitemap div.second a').mouseout( function(){
		$('div.second div.description').hide( '' );
	});

	/*******************************************************
		history
	*/

	$('.history_years a').click(function(){
		history_year = $(this).attr('href').replace(/#/, '');
		
		showHistories(history_year,0);
		
		$('.history_years li').removeClass('item-active');
		$(this).parent('li').addClass('item-active');

		$('.history_months .list').hide();
		$('.history_months .list_of_'+history_year).show();
		
		return false;
	});
	
	$('.history_months a').click(function(){
		history_month = $(this).attr('href').replace(/#/, '');
		
		showHistories(history_year,history_month);
		
		$('.history_months li').removeClass('item-active');
		$(this).parent('li').addClass('item-active');
		
		return false;
	});

	first_year_in_list = $('.history_years .list li:first-child a');
	if ( first_year_in_list.length > 0 )
	{
		history_year = first_year_in_list.attr('href').replace(/#/, '');
		showHistories(history_year,0);
	}

	/*
		history
	*******************************************************/

	var sertificates_list_items = $('#sertificates .list .item');
	var sertificates_view_items = $('#sertificates .view .item');
	var sertificates_galleries = $('#sertificates .view .item .ad-gallery');

	sertificates_galleries.each(function(i){
		$('#ad-gallery-'+i).adGallery({
			loader_image: '/app/view/frontend/metinvest/pc/img/loader.gif',
			description_wrapper: $('#ad-description-'+i),
			slideshow: {
			   //autostart: true,
			   //speed: 5000
			},
			effect: 'fade',
			cycle: false,
			callbacks: {
				init: function() {
					$('.ad-gallery .ad-image a').fancybox();
				},
				afterImageVisible: function(){
					var image = this.current_image.html();
					var large_src = this.current_image.children('img').attr('src');
					var details = this.thumbs_wrapper.find('.ad-active').children('img').attr('longdesc');
					//alert(details);
					//var original_src = large_src.replace('large','original');
					//this.current_image.html('<a href="'+original_src+'">'+image+'</a>');
					this.current_image.html('<a href="'+details+'">'+image+'</a>');
					$('.ad-gallery .ad-image a').fancybox({
						width: '80%',
						height: '80%',
						autoDimensions: false
					});
				}
			}
		});
	});

	$('#sertificates_list a').click(function(){

		sertificates_list_items.removeClass('item-active');
		$(this).parent('li').addClass('item-active');

		sertificates_view_items.removeClass('item-active');
		$('#'+$(this).attr('href')).addClass('item-active');

		return false;
	});

});

function print_content(id) {
    $('#'+id).printElement({printMode:'popup'});
}

