$(document).ready(function() {

	setLabel($('#search-1'), 'inline');
	$('#search-1').focus(function () {
		$(this).prev("label").css('display', 'none');
	}).blur(function () {
		setLabel($(this), 'inline');
	});

	$('#media a').click(function(){
		setTab($(this));
		return false;
	});

	//initSelect($('.select'));
	initCheckbox($('.checkbox'));
	initRadio($('.radio'));

	var ct = $('.l-content-top-holder');
    if(ct.length) $('.l-content-right').css({'margin-top' : ct.outerHeight(true) });


    var ar1 = $('.e-togglelist');
    ar1.each(function() {
        var el = $(this),
            h = $( this.getAttribute('href') || this.getAttribute('data-toggle') );
        el.bind('click', function() {
            if( h.first().hasClass('active') ) {
                h.add(el).removeClass('active');
                $('select').each(function(){
                    if( this.name == "arrFilter_pf[territory]" && is_region )
                        return;
                    $(this).val('');
                    $(this).ikSelect('reset');
                    $(this).ikSelect('redraw');
                });
                $('input').each(function(){
                    $(this).val('');
                });
            }
            else h.add(el).addClass('active');
            return false;
        });
    });

}).click(function() {
	$('.select').removeClass('active');
});


/*************************************/

function setLabel (nod, action) {
	nod.each(function(){
		var value = $(this).val();
		if (!value){
			$(this).prev('label').css('display', action);
		};
	});
};

/*************************************/

function initSelect(nod) {
	nod.each(function(i, v) {
		$(v).unbind().bind('click', function (event) {
			event.stopPropagation();
		});
		$(v).find('.select-input').unbind().bind('click', function (event) {
			nod.not($(v)).removeClass('active');
			$(v).toggleClass('active');
			//var e = $(v).find('.select-list');
			//e.css({'top' : '', 'bottom' : ''});
			//if(e.offset().top+e.outerHeight(true) > $(document).height()) e.css({'top' : 'auto', 'bottom' : '100%'});
			event.stopPropagation();
			return false;
		});
		$(v).find('input').unbind().bind('click', function () {
			$(v).toggleClass('active');
			setSelect($(v));
		});
	});
	setSelect(nod);
};
function setSelect(nod) {
	nod.each(function(i, v) {
		var value = $(v).find('input:checked').parent('label').text();
		if(value) $(v).find('.select-input').removeClass('empty').text(value);
		else {
			var e = $(v).find('.select-input');
			e.addClass('empty');
			if(e.data('text')) e.text(e.data('text'));
		};
	});
};

/*************************************/

function initCheckbox(nod) {
	nod.each(function(i, v) {
		$(v).find('input').unbind().bind('click', function () {
			setCheckbox($(v));
		});
		setCheckbox($(v));
	});
};
function setCheckbox(nod) {
	if(nod.find('input').is(':checked')) nod.addClass('active');
	else nod.removeClass('active');
};

/*************************************/

function initRadio(nod) {
	nod.each(function(i, v) {
		$(v).find('input').unbind().bind('click', function () {
			setRadio(nod);
		});
	});
	setRadio(nod);
};
function setRadio(nod) {
	nod.each(function(i, v) {
		if($(v).find('input').is(':checked')) $(v).addClass('active');
		else $(v).removeClass('active');
	});
};

/*************************************/

function setTab(nod) {
	var id = nod.attr('id');
	nod.addClass('active').siblings().removeClass('active');
	$('#'+id+'-body').addClass('active').siblings().removeClass('active');
    $(".l-content-holder").append('<img src="/bitrix/media/img/loading.gif" alt="" style="position: absolute; top: 23%; left: 50%;">');
    $.get(
        '/ajax/media.php',
        'action=getList&action_id='+id,
        function(response){
            $('.l-content-holder').html(response);
            $('audio,video').mediaelementplayer();
        }
    );
};

/*************************************/

$(function(){
    var wrapper = $( ".file_upload" );
    wrapper.each(function() {
        var inp = $(this).find( "input" ),
        btn = $(this).find( "button" ),
        lbl = $(this).find( "div" );

        btn.focus(function(){
            inp.focus()
        });
        // Crutches for the :focus style:
        inp.focus(function(){
            $(this).addClass( "focus" );
        }).blur(function(){
                $(this).removeClass( "focus" );
        });


        var file_api = ( window.File && window.FileReader && window.FileList && window.Blob ) ? true : false;

        inp.change(function(){
            var file_name;
            if( file_api && inp[ 0 ].files[ 0 ] )
                file_name = inp[ 0 ].files[ 0 ].name;
            else
                file_name = inp.val().replace( "C:\\fakepath\\", '' );

            if( ! file_name.length )
                return;

            if( lbl.is( ":visible" ) ){
                lbl.text( file_name );
                btn.text( "выберите файл" );
            }else
                btn.text( file_name );
        }).change();
    });

});

$( window ).resize(function(){
    $( ".file_upload input" ).triggerHandler( "change" );
});



$(function(){
    function css_style_page(num_type_css){
        if(num_type_css == 12){
            $("body").addClass("smallfont");
            $("body").removeClass("averagefont");		
            $("body").removeClass("largefont");
            $(".b-font a.fs12").addClass("selected");
            $(".b-font a.fs12").siblings("a").removeClass("selected");
        }else if(num_type_css == 14){
            $("body").addClass("averagefont");
            $("body").removeClass("largefont");
            $("body").removeClass("smallfont");
            $(".b-font a.fs14").addClass("selected");
            $(".b-font a.fs14").siblings("a").removeClass("selected");
        }else if(num_type_css == 16){
            $("body").addClass("largefont");
            $("body").removeClass("smallfont");
            $("body").removeClass("averagefont");
            $(".b-font a.fs16").addClass("selected");
            $(".b-font a.fs16").siblings("a").removeClass("selected");
        }
    }

    if(getCookie("fspages")!="undefined"){
        val_type_css = getCookie("fspages");
        css_style_page(val_type_css);
    }

    $(".b-font a.fs12").click(function(){
		css_style_page(12);
        setCookie("fspages", "12", {path: "/"});
    });
    $(".b-font a.fs14").click(function(){
		css_style_page(14);
        setCookie("fspages", "14", {path: "/"});
    });
    $(".b-font a.fs16").click(function(){
		css_style_page(16);
        setCookie("fspages", "16", {path: "/"});
    });
});

$(window).load(function () {
    if(jQuery().ikSelect) {
	    $("select").each(function() {
	        if($(this).css("display")!=="none"){
	            $(this).ikSelect();
	        }
	    });
	};
    if(jQuery().datepicker) {
	    $( "#datepicker-1" ).datepicker();
	    $( "#datepicker-2" ).datepicker();
	};
});

$(function(){
	$("#begin").click(function(){
	    ('.cycle-slideshow').cycle('goto', 0);
    });

	var slidecount = $(".bp-main-holder a").length;
	$('.slider-nav-end').attr('data-cycle-arg', slidecount-1);

});

/*************************************/
function setEqualHeightCell(columns)
{
    columns.each(
        function()
        {
            var h=$(this).height();
            var columns_cell=$(this).find('.bs-title').not('.bs-group');
            var count=columns_cell.length;
            var h_cell=h/count;
            columns_cell.each(
                function()
                {
                    $(this).css({'height' : h_cell});
                }
            )
             }
             );
}

function setEqualHeight(columns)
{
var tallestcolumn = 0;
columns.each(
function()
{
currentHeight = $(this).height();
if(currentHeight > tallestcolumn)
{
tallestcolumn = currentHeight;
}
}
);
columns.height(tallestcolumn);
}
$(document).ready(function() {
setEqualHeightCell($(".bs-col5 > .bs-item"));
setEqualHeight($(".bs-col6 > .bs-item > .bs-title"));
setEqualHeight($(".bs-col5 > .bs-item > .bs-title"));
setEqualHeight($(".bs-col3 > .bs-item > .bs-title"));
setEqualHeight($(".bs-col6-sub > .bs-item > .bs-title"));
setEqualHeight($(".bs-col5-sub > .bs-item > .bs-title"));
setEqualHeight($(".bs-col3-sub > .bs-item > .bs-title"));
setEqualHeight($(".bs-col2-sub .bs-title"));
});
/*************************************/
//  options:
//  - expires
//  - path
//  - domain
//  - secure
function setCookie(name, value, options) {
  options = options || {};
  var expires = options.expires;
  if (typeof expires == "number" && expires) {
    var d = new Date();
    d.setTime(d.getTime() + expires*1000);
    expires = options.expires = d;
  }
  if (expires && expires.toUTCString) {
    options.expires = expires.toUTCString();
  }
  value = encodeURIComponent(value);
  var updatedCookie = name + "=" + value;
  for(var propName in options) {
    updatedCookie += "; " + propName;
    var propValue = options[propName];   
    if (propValue !== true) {
      updatedCookie += "=" + propValue;
     }
  }
  document.cookie = updatedCookie;
}

function getCookie(name) {
  var matches = document.cookie.match(new RegExp(
    "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
  ));
  return matches ? decodeURIComponent(matches[1]) : undefined;
}

function deleteCookie(name) {
  setCookie(name, "", { expires: -1 })
}

/*************************************/


/*************** Subscribe **********************/
function isValidEmail (email) {
    return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);
}

function subscribesubmit() {

    st = "";
    i = 0;

    $("#subscribe").find("input:checkbox").each(function () {
        if (i == 0) {
            st = st + $(this).prop('checked');
        }
        else
        {
            st = st + "|" + $(this).prop('checked');
        }
        i++;
    });

    sr = document.getElementById('subres');
    s = document.getElementById('subemail').value;
    mhash = $("#mhash").val();

    if ((s.length < 2) || !(isValidEmail(s))) {
        document.forms.subform.subemail.focus();
        st = 'Некорректный адрес электронной почты.';
        sr.innerHTML = st;
        return false;
    }

    $.ajax({
        url: '/ajax/subscribe.php?email=' + s + '&check=' + st + '&mhash=' + mhash,
        success: function (data) {
            $('#subres').html(data);
        }
    });

    return false;
}

/*************************************/