/*window_resize - begin*/
var d = document;
var winIE = (navigator.userAgent.indexOf("Opera")==-1 && (d.getElementById &&  d.documentElement.behaviorUrns))  ? true : false;

function bodySize(){
	if(winIE && d.documentElement.clientWidth) {
		sObj = d.getElementsByTagName("body")[0].style;
		if(d.documentElement.clientWidth<1000) {
		   	sObj.width = "1000px";
		}
		else if(d.documentElement.clientWidth>1258) {
			sObj.width = "1258px";
		}
		else {
			sObj.width = "100%";
		}
	}
}

function init(){
	if(winIE) { bodySize(); }
}

if(winIE) { onresize = bodySize; }
/*window_resize - end*/

var highlightNav = function(idx){
	hoverHandler(null, $('#menu_top li:eq(' + (idx - 1) + ') a') );
};


var hoverHandler = function(e, obj, out){
	if (!out && $("#its_main_page").size() > 0) {
		obj.parents('li:eq(0)').addClass('active').removeClass('inactive').siblings().removeClass('active').addClass('inactive');
		var ind1 = $('#menu_top li').index($('#menu_top li.active') );
		$('.subnav').hide();
		$('.subnav:eq(' + ind1 + ')').show();
	}
	else {
/*		var relTarg = e.relatedTarget || e.toElement;
		if (relTarg && !$(relTarg).hasClass('crn_top') && !$(relTarg).is('#all') && relTarg.nodeName != 'LI') {
			var ind1 = $('#menu_top li').index($('#menu_top li.current') );
			$('.subnav').hide();
			$('.subnav:eq(' + ind1 + ')').show();
			$('#menu_top li:eq(' + ind1 + ')').addClass('active').removeClass('inactive').siblings().removeClass('active').addClass('inactive');
		};*/
	}
};

var fixPartnersLinkPos = function(){
	if ($('#mt_partners').length == 0) return; 
	if ($(window).width() - $('#mt_partners').offset().left >= 240){
		$('.top_submenu_partners.first').css({
			marginRight: $(window).width() - $('#mt_partners').offset().left  - 170 + 'px'
		});
	}
	else {
		$('.top_submenu_partners.first').css({
			marginRight: '70px'
		});
	};
};

/*er
 * Действия при готовности DOM
 */
$(document).ready(function(){
	$('h1').each(function(){
		if ($(this).height() > 30)
		{
			if ($.browser.msie && $.browser.version < 7)
			{
				$(this).css({"background-image":"url(/themes/pnleasing/img/siteimage/header_content_big.gif)", "background-repeat":"no-repeat"});
				$(this).parent().css({"background-image":"url(/themes/pnleasing/img/siteimage/header_content_big.gif)", "background-repeat":"no-repeat"});
			}
			else 
			{
				$(this).css({"background-image":"url(/themes/pnleasing/img/siteimage/header_content_big.png)", "background-repeat":"no-repeat"});
				$(this).parent().css({"background-image":"url(/themes/pnleasing/img/siteimage/header_content_big.png)", "background-repeat":"no-repeat"});
			}
		}
		else if ($.browser.msie && $.browser.version < 7)
		{
			$(this).css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, src='/themes/pnleasing/img/siteimage/header_content.png')"});
		}
	});
	if ($.browser.msie && $.browser.version > 6 && $.browser.version < 8)
	{
		$(".menu_wrapper > ul > li").each(function(){
			if ($(this).attr("name") == "submenu" && $(this).attr("id") == "curli") $(this).css({"background-image":"url(/themes/pnleasing/img/siteimage/bulletrsmie6.png)","background-repeat":"no-repeat"});
			else if ($(this).attr("name") == "submenu") $(this).css({"background-image":"url(/themes/pnleasing/img/siteimage/bulletsmie6.png)","background-repeat":"no-repeat"});
			else if ($(this).attr("id") == "curli") $(this).css({"background-image":"url(/themes/pnleasing/img/siteimage/bulletrie6.png)","background-repeat":"no-repeat"});
			if ($(this).attr("id") != "curli")
			{
				$(this).mouseover(function(){
					if ($(this).attr("name") != "submenu") $(this).css({"background-image":"url(/themes/pnleasing/img/siteimage/bulletrie6.png)","background-repeat":"no-repeat"});
					else $(this).css({"background-image":"url(/themes/pnleasing/img/siteimage/bulletrsmie6.png)","background-repeat":"no-repeat"});
				}).mouseout(function(){
					if ($(this).attr("name") != "submenu") $(this).css({"background-image":"url(/themes/pnleasing/img/siteimage/bulletie6.png)","background-repeat":"no-repeat"});
					else $(this).css({"background-image":"url(/themes/pnleasing/img/siteimage/bulletsmie6.png)","background-repeat":"no-repeat"});
				});
			}
		});
	}
	if ($.browser.msie && $.browser.version < 7)
	{
		$(".menu_wrapper > ul > li").each(function(){
			if ($(this).attr("name") == "submenu" && $(this).attr("id") == "curli") $(this).css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='/themes/pnleasing/img/siteimage/bulletrsmie6.png'"});
			else if ($(this).attr("name") == "submenu") $(this).css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='/themes/pnleasing/img/siteimage/bulletsmie6.png'"});
			else if ($(this).attr("id") == "curli") $(this).css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='/themes/pnleasing/img/siteimage/bulletrie6.png'"});
			if ($(this).attr("id") != "curli")
			{
				$(this).mouseover(function(){
					if ($(this).attr("name") != "submenu") $(this).css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='/themes/pnleasing/img/siteimage/bulletrie6.png'"});
					else $(this).css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='/themes/pnleasing/img/siteimage/bulletrsmie6.png'"});
				}).mouseout(function(){
					if ($(this).attr("name") != "submenu") $(this).css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='/themes/pnleasing/img/siteimage/bulletie6.png'"});
					else $(this).css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='/themes/pnleasing/img/siteimage/bulletsmie6.png'"});
				});
			}
		});
	}
/*	$("#top_flash_wrapper").css({"margin-left":document.documentElement.clientWidth/2 - $("#top_flash_wrapper").offset().left - 75});*/
	init();
	if ($.browser.msie && $.browser.version < 7) 
	{
		$('#all').css({'height':$('.index_columns').height() + 630});
		if ($("#logo img").attr("src").indexOf("logorus.png")>=0)
			$("#logo").css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, src='/themes/pnleasing/img/siteimage/logorus.png')","background-position":"left center","margin-top":"36px"});
		else
			$("#logo").css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, src='/themes/pnleasing/img/siteimage/logoeng.png')","background-position":"left center","margin-top":"36px"});

		$("#logo img").css({"visibility":"hidden","margin-top":"0"});
	}
	
	
	
	$('#menu_top li a').hover(function(e){
		hoverHandler(e, $(this) );
	}, function(e){
		hoverHandler(e, $(this), true);
	});
	if ($("table.index_columns").size() > 0)
	{
		$('.top_submenu').mouseout(function(e){
			var relTarg = e.relatedTarget || e.toElement;
			$(".active").attr("class","inactive");
			if (relTarg && !$(relTarg).hasClass('menu_wrapper') && !$(relTarg).is('#all') && !$(relTarg).hasClass('crn_top') && !$(relTarg).is('#center') && relTarg.nodeName != 'A' && relTarg.nodeName != 'UL' && relTarg.nodeName != 'H2' && relTarg.nodeName != 'LI') {
				var ind1 = $('#menu_top li').index($('#menu_top li.current') );
				//setTimeout(function(){$(".subnav").animate({"opacity": "hide"}, "slow");}, 2000);
				$('.subnav').hide();
				$('.subnav:eq(' + ind1 + ')').show();
				$('#menu_top li:eq(' + ind1 + ')').addClass('active').removeClass('inactive').siblings().removeClass('active').addClass('inactive');
			}
		});
	}
	
	$('.top_submenu').mouseover(function(){
		$(this).stop().animate({"opacity": "100%"}, "fast");
	});
	
	fixPartnersLinkPos();
	
	$('div.basic a.ec_control').toggle(function(){
		$(this).parents('li:eq(0)')
			.find('tr.extended').toggle().end()
			.find('div.basic a.ec_control').text('свернуть').end()
			.find('div.basic').find('span.dir').html('&uarr;');
		return false;
	}, function(){
		$(this).parents('li:eq(0)')
			.find('tr.extended').toggle().end()
			.find('div.basic a.ec_control').text('подробнее').end()
			.find('div.basic').find('span.dir').html('&darr;');
		return false;
	});
	
	$('tr.extended a.ec_control').click(function(){
		$(this).parents('li:eq(0)').find('div.basic a.ec_control').click();
		return false;
	});
	
	if ($.browser.msie) {
		$(".top_submenu").css({"bottom":"10px","width":"859px"});
		$("#all").css({"position":"relative","bottom":"5px"});
	}
	
	$('a').each(function(){
		if ($(this).attr('href')) {
			if ($(this).attr('href').replace($('base').attr('href'), '').match(/^#/) ) {
				$(this).click(function() {
					if ($.browser.opera) {
						location.hash = $(this).attr('href').replace($('base').attr('href'), '');
					}
					else {
						location.hash = $(this).attr('href').replace($('base').attr('href'), '');
					}
					return false;
				});
			}
		}
	});
	
	$('.js_hidden').hide();
	$('.js_shown').show();
	
	/* Валидация
	------------------------------------------------------------------------------------------------------------------- */
	/* дефолтные сообщения */
	$.validator.messages.required = "Это поле является обязательным для заполнения";
	$.validator.messages.email = "Некорректный формат адреса электронной почты";
	/* правила */
	
	$('form label.required input[name!=node_content_name], form label.required textarea').addClass('required');
	
	$('form').each(function () { 
		$(this).validate();
	});
	
	/*
	$('#form-create-faq').validate({
		rules:{
			node_content_question: 'required',
			node_content_fio: 'required'
		}
	});
	$('#form-create-feedback').validate({
		rules:{
			node_content_text: 'required',
			node_content_fio: 'required',
			node_content_email: {
				required: true,
				email: true
			},
			node_content_phone: 'required',
			node_content_wherefrom: 'required'
		}
	});
	$('#form-create-order').validate({
		rules:{
			node_content_price: 'required',
			node_content_prepay: 'required',
			node_content_company: 'required',
			node_content_fio: 'required',
			node_content_phone: 'required',
			node_content_email: 'required'
		}
	});
	*/
	/* ---------------------------------------------------------------------------------------------------------------- */
	
	
});


$(window).resize(function(){
	fixPartnersLinkPos();
});

/**
 * Функция предназначена для отладки. Shortcut console.log'a.
 * Записывает в консоль Firebug'a передаваемые в нее данные. 
 * 
 * @param {String} str - строка/массив для отображения в консоли
 */
function log(str) {
	window.console ? window.console.log(str) : $('body').prepend(str);
};

function add_favorite(a) { 
	if(document.all) 
		window.external.AddFavorite(document.location.href, document.title); 
	else if (typeof(opera)=="object") { 
		a.rel="sidebar"; 
		a.title=document.title; 
		a.url=document.location.href; 
		return true; 
	} else window.sidebar.addPanel(document.title,document.location.href,""); 
	return false; 
};

(function($){  
  
    $(function(){  
        var e = $(".scrollTop");  
        var speed = 500;  
  
        e.click(function(){  
            $("html:not(:animated)" +( !$.browser.opera ? ",body:not(:animated)" : "")).animate({ scrollTop: 0}, 500 );  
            return false; //важно!  
        });  
        //появление  
        function show_scrollTop(){  
            ( $(window).scrollTop()>300 ) ? e.fadeIn(600) : e.hide();  
        }  
        $(window).scroll( function(){show_scrollTop()} ); show_scrollTop();  
    });  
  
})(jQuery)
