window.event_selected=new Array();
if(document.location.pathname.substring(0, 4)=='/eng'){
	var site_language='en';
}
else var site_language='ru';

$(document).ready(function(){
	if($('.index_blocks').size() > 0) {
		set_index_blocks_sizes();
	}
	/* projects map */
	if ($('.project_map').size() > 0) {
		pr_map.init();
	}
	/* /projects map */
	if($('#switcher').size() > 0) {
		$('#switcher').bind('click', function() {
			$.ajaxSetup({async: false});
			if($('#switcher').hasClass("mmvb")) $('#switcher').toggleClass("rts");
			if($('#switcher').hasClass("mmvb_en")) $('#switcher').toggleClass("rts_en");
			if($('#switcher').hasClass("mmvb")||$('#switcher').hasClass("mmvb_en")){
				$.getJSON('/stock_data_daily.php?id=OPIN&period=-1M&stocks=1', function(data) {
					$('#val_type').html("RUR");
					window.stock_data=data;
					window.stock=1;
					var min_stock_data_value=0;
					var max_stock_data_value=0;
					$(window.stock_data).each(function(index, value){
						if(index==1) {
							min_stock_data_value=value[1];
							max_stock_data_value=value[1];
						}
						if(value[1]>max_stock_data_value) max_stock_data_value=value[1];
						else if(value[1]<min_stock_data_value) min_stock_data_value=value[1];
						lineoptions.vAxis.maxValue=max_stock_data_value+10;
						lineoptions.vAxis.minValue=min_stock_data_value-10
					});
					if($('#invest_graphs_vol').size()>0){
						$.getJSON('/micex_data.php?id=OPIN&period=-1M&type=volume', function(data) {
							window.volume_data=data;
							drawChart();
						});
					}
					else
						drawChart();
				});
			}
			if($('#switcher').hasClass("rts")||$('#switcher').hasClass("rts_en")){
				$.getJSON('/stock_data_daily.php?id=OPIN&period=-1M&stocks=2', function(data) {
					$('#val_type').html("$");
					window.stock_data=data;
					window.stock=2;
					var min_stock_data_value=0;
					var max_stock_data_value=0;
					$(window.stock_data).each(function(index, value){
						if(index==1) {
							min_stock_data_value=value[1];
							max_stock_data_value=value[1];
						}
						if(value[1]>max_stock_data_value) max_stock_data_value=value[1];
						else if(value[1]<min_stock_data_value) min_stock_data_value=value[1];
						lineoptions.vAxis.maxValue=max_stock_data_value+1;
						lineoptions.vAxis.minValue=min_stock_data_value-1;
					});
					if($('#invest_graphs_vol').size()>0){
						$.getJSON('/rts_data.php?id=OPIN&period=-1M&type=volume', function(data) {
							window.volume_data=data;
							drawChart();
						});
					}
					else
						drawChart();
				});
			}
			$.ajaxSetup({async: true});
		});
	}

	$('input[rel]').focus(function() {
		if($(this).val() == $(this).attr('rel')) {
			$(this).val('');
		}
	}).blur(function() {
		if($(this).val() == '') {
			$(this).val($(this).attr('rel'));
		}
	});

	if ($('.search_go').size()) {
		$('.search_go').bind('click', function() { form_submit('search_form'); });
	}

	if ($('.subscribe_go').size() > 0) {
		$('.subscribe_go').bind('click', function() {
			form_submit('subscribe_form');
		});
		$('.popup_bg, .message_popup .close').bind('click', function() {
			message_popup_hide();
		});
	}

	if ($('.graph_tabs').size()) {
		$('.graph_tabs a').bind('click', function() { change_graph_period($(this)); });
	}
	if ($('.contact_clear').size()) {
		$('.contact_clear').bind('click', function() { form_reset('contact_form'); });
	}
	if ($('.event_check').size() > 0) {
        $('.event_check a').bind('click', function () { event_check($(this)); });
    }
	if ($('.check').size()) {
		$('.check input[type=checkbox]').each(function() {
			if ($(this).attr('checked')) {
				$(this).parents('.check').removeClass('check_off').addClass('check_on');
			}
			else {
				$(this).parents('.check').removeClass('check_on').addClass('check_off');
			}
		}).bind('change', function() {
			if ($(this).attr('checked')) {
				$(this).parents('.check').removeClass('check_off').addClass('check_on');
			}
			else {
				$(this).parents('.check').removeClass('check_on').addClass('check_off');
			}
		});
		/* photo */
		if ($('a.photo_s').size() > 0) {
			photo_cur = -1;
			$('a.photo_s').each(function() {
				var img_src = $(this).find('img').attr('id');
				$('.photo_popup').append('<div class="photo_popup_img" style="background-image: url(' + img_src + ');"></div>');
			});

			$('a.photo_s').bind('click', function() {
				var index = $('a.photo_s').index($(this));
				photo_show(index);
				photo_popup_show();
			});
			$('.photo_popup .prev').bind('click', function() {
				var index = -1;
				if (photo_cur > 0)
					index = photo_cur - 1;
				else
					index = $('a.photo_s').size() - 1;
				photo_show(index);
			});
			$('.photo_popup .next').bind('click', function() {
				var index = -1;
				if (photo_cur < $('a.photo_s').size() - 1)
					index = photo_cur*1 + 1;
				else
					index = 0;
				photo_show(index);
			});
			$('.popup_bg, .photo_popup .close').bind('click', function() {
				photo_popup_hide();
			});
		}
		/* /photo */
	}

	if ($('.project_vitrina img').size() > 1) {
		$('.project_vitrina .prev').bind('click', function() { vitrina_prev(); });
		$('.project_vitrina .next').bind('click', function() { vitrina_next(); });
	}

	if ($('.switch').size()) {
		if(document.location=='http://opin.ru/investors/presentation/'||document.location=='http://www.opin.ru/investors/presentation/') switch_block($('.switch:eq(0)'));
		$('.switch').bind('click', function() { switch_block($(this)); });
	}

	if ($('.print').size()) {
		$('.print').bind('click', function() { print_page(); });
	}
});

function set_index_blocks_sizes() {
	var group_h = $('.group_bl').height();
	var news_h = $('.news_bl').height();
	var invest_h = $('.invest_bl').height();

	if (group_h + news_h - 255 > invest_h) {
		$('.invest_bl_b').height(group_h + news_h - 402);
		$('.news_bl').height($('.invest_bl').height() - group_h + 172);
	}
	else {
		$('.news_bl').height(invest_h - group_h + 172);
	}
	$('.index_blocks .subscr_bl').css('top', $('.invest_bl').height() + 170 + 'px');
}

function form_submit(name) {
	document.forms[name].submit();
}
function form_reset(name) {
	document.forms[name].reset();
}

function message_popup_hide() {
	$('.message_popup').hide();
	$('.popup_bg').hide();
}
function message_popup_show(text, unsubscribe) {
	if(!unsubscribe) unsubscribe=false;
	var doc_h = $(document).height();
	$('.popup_bg').height(doc_h).show();

	var win_h = $(window).height();
	var top = (win_h - $('.message_popup').height()) / 2 + $(document).scrollTop();
	$('.message_popup').css('top', top + 'px').show();
	$('.message_popup .message').html(text);
	if(unsubscribe)
		$('.message_popup .unsubscribe').html(unsubscribe);
}

function send_form(form){
	$.post($(form).attr("action"), $(form).serializeArray(), function(data){
		$("#form_container").html(data);
		message_popup_show($(data).find("#answer").html());
	});
}

function vitrina_prev() {
	var imgs = $('.project_vitrina img');
	var img_cur = $('.project_vitrina img:visible');
	var ind_cur = imgs.index(img_cur);
	var ind_max = imgs.size() - 1;

	var ind_prev;
	if (ind_cur == 0)
		ind_prev = ind_max;
	else
		ind_prev = ind_cur - 1;

	imgs.hide();
	imgs.eq(ind_prev).show();
}
function vitrina_next() {
	var imgs = $('.project_vitrina img');
	var img_cur = $('.project_vitrina img:visible');
	var ind_cur = imgs.index(img_cur);
	var ind_max = imgs.size() - 1;

	var ind_next;
	if (ind_cur == ind_max)
		ind_next = 0;
	else
		ind_next = ind_cur*1 + 1;

	imgs.hide();
	imgs.eq(ind_next).show();
}

function switch_block(lnk) {
	var ind = $('.switch').index(lnk);
	var block = $('.switchable').eq(ind);
	switcher=$('.switch').eq(ind);
	if (block.css('display') == 'block') {
		if (lnk.parent().hasClass('title_line')) {
            lnk.parent().addClass('show').removeClass('hide');
        }
		else if($(switcher).hasClass("less1"))
			$(switcher).removeClass("less1").addClass("more1");
		else $(switcher).removeClass("less2").addClass("more2");
		$(switcher).text($(switcher).text().replace("Меньше", "Больше"));
		$(switcher).text($(switcher).text().replace("Скрыть", "Показать"));
		$(switcher).text($(switcher).text().replace("Collapse", "In Detail"));
		$(switcher).text($(switcher).text().replace("Less", "More"));
		block.hide();
	}
	else {
		if (lnk.parent().hasClass('title_line')) {
			$('.hide').removeClass('hide').addClass('show').next().css("display", "none");
            lnk.parent().addClass('hide').removeClass('show');
        }
		else if($(switcher).hasClass("more1"))
			$(switcher).removeClass("more1").addClass("less1");
		else $(switcher).removeClass("more2").addClass("less2");
		$(switcher).text($(switcher).text().replace("Больше", "Меньше"));
		$(switcher).text($(switcher).text().replace("Показать", "Скрыть"));
		$(switcher).text($(switcher).text().replace("In Detail", "Collapse"));
		$(switcher).text($(switcher).text().replace("More", "Less"));
		block.show();
	}
}

function print_page() {
	window.print();
}

function photo_popup_hide() {
	//photo_hide();
	$('.photo_popup').hide();
	$('.popup_bg').hide();
}
function photo_popup_show() {
	var doc_h = $(document).height();
	$('.popup_bg').height(doc_h).show();

	var win_h = $(window).height();
	var top = win_h / 2 - 270 + $(document).scrollTop();
	$('.photo_popup').css('top', top + 'px').show();
}
function photo_hide() {
	$('.photo_popup_img').hide();
}
function photo_show(index) {
	photo_hide();
	$('.photo_popup_img').eq(index).show();

	photo_cur = index;
}

/* projects map */
var pr_map = {
	timer: '',
	still_need_to_hide: '',
	cur_obj: '',
	init: function() {
		pr_map.still_need_to_hide = true;
		pr_map.cur_obj = 0;
		$('.pm_marker').bind('mouseover', function(){
			if ($('.pm_marker').index($(this)) != pr_map.cur_obj) {
				pr_map.still_need_to_hide = true;
				pr_map.obj_hide();
			}
			if ($('.pm_obj').eq(pr_map.cur_obj).css('display') == 'none') pr_map.obj_show($(this));
			pr_map.still_need_to_hide = false;
			pr_map.cur_obj = $('.pm_marker').index($(this));
		});
		$('.pm_marker').bind('mouseout', function(){
			pr_map.still_need_to_hide = true;
			setTimeout(function() { pr_map.hide_obj_if_needed(); }, 10);
		});
		$('.pm_obj').live('mouseover', function () {
			pr_map.still_need_to_hide = false;
		});
		$('.pm_obj').live('mouseout', function () {
			pr_map.still_need_to_hide = true;
			setTimeout(function() { pr_map.hide_obj_if_needed(); }, 10);
		});
	},
	hide_obj_if_needed: function() {
		if (pr_map.still_need_to_hide) {
			pr_map.obj_hide();
		}
	},
	obj_show: function(marker) {
		var ind = $('.pm_marker').index(marker);

		pr_map.timer = window.setTimeout(function() {
			pr_map.obj_move(ind);
			$('.pm_obj').eq(ind).show();
		}, 100);
	},
	obj_hide: function() {
		$('.pm_obj').hide();
		clearTimeout(pr_map.timer);
	},
	obj_move: function(ind) {
		var marker = $('.pm_marker').eq(ind);
		var obj = $('.pm_obj').eq(ind);

		var top = marker.position().top;
		var left = marker.position().left;
		var obj_top = (top - 140 > 0) ? (top - 140) : (top + 35);
		var obj_left = (left < 580) ? Math.max((left - 50), 0) : 530;
		obj.css({'top': obj_top + 'px', 'left': obj_left + 'px'});
	}
};
/* /projects map */


/*function event_check(lnk) {
    if (lnk.hasClass('checked')) {
        lnk.removeClass('checked');
        delete(window.event_selected[parseInt((lnk.attr('id')[4]))]);
    }
    else {
        lnk.addClass('checked');
        window.event_selected[parseInt((lnk.attr('id')[4]))]=parseInt((lnk.attr('id')[4]));
    }
    if($('.event_check .checked').length){
	    $('.event').hide();
	    for(event in window.event_selected){
	    	$('.event_color'+event).show();
	    }
	    $('.event_month').each(function (index, element){
	    	if (!$(element).find('.event:visible').length){
	    		$(element).hide();
	    	}
	    })
    }
    else {
    	$('.event').show();
	    $('.event_month').show();
    }
}*/
