window.addEvent('domready', function(){
    auth();
    formInTest();
    formInAll();
    galleryRamka();

    if (!/MSIE (5\.5|6)/.test(navigator.userAgent)) {

    } else {
        ieW();
    }
});

if (!/MSIE (5\.5|6)/.test(navigator.userAgent)) {
   
} else {
    window.addEvent('resize', function(){
            ieW();
    });
}


function galleryRamka() {
    if ($('image')) {
        imgs = $('image').getElements('img');


        $each(imgs, function(el){
            //alert(el.src);
            el.setStyle('display', 'none');
            //alert(el.getParent().getParent().className);
            
            
            el.getParent().getParent().setStyle('background-image', 'url('+el.src+')');

        });
    }
    if ($('subcat')) {
	subimgs = $('subcat').getElements('div.im img');

	$each(subimgs, function(el){
            //alert(el.src);
            el.setStyle('display', 'none');
            //alert(el.getParent().getParent().className);
            
            
            el.getParent().getParent().setStyle('background-image', 'url('+el.src+')');

        });
    }
}



function ieW() {
    // for #head_container
    if (document.documentElement.clientWidth >= 1300) {
        $('head_container').setStyle('width', '1300px');
        $('content_container').setStyle('width', '1300px');
    } else {
        if (document.documentElement.clientWidth <= 1100) {
            $('head_container').setStyle('width', '1100px');
            $('content_container').setStyle('width', '1100px');
        } else {
            $('head_container').setStyle('width', 'auto');
            $('content_container').setStyle('width', 'auto');
        }
    }
}

function lmenuEn() {
    lmens = $('lmenu').getElements('li');


    $each(lmens, function(el){
        el.addEvents({
              'mouseenter': function(){
                  el.className = el.className + " uhover";
              },
              'mouseleave': function(){
                  el.className = el.className.replace(/uhover/, "");
              }
        });
    });

}

function callEn() {
    inps = $('callback').getElements('div.call_item input');


    $each(inps, function(inp){
        inp.addEvents({
              'focus': function(){
                  inp.getParent('div.call_item').id = 'focus_call';
              },
              'blur': function(){
                  inp.getParent('div.call_item').id = '';
              },
              'change': function(){
                  if ((trim(inp.value) !== "Ваше имя, Организация") && (trim(inp.value) !== "Ваш телефон") && (trim(inp.value) !== "") && (trim(inp.value) !== "")) {
                    inp.className = 'edited';
                  } else {
                    inp.className = '';
                  }
              }
        });
    });
}


function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


function auth() {
    if ($('auth')) {

        new Accordion(
            $$('.auth div.auth_txt'),
            $$('.auth div.auth_frm'),
            {
                duration: 300,
                opacity: false,
                alwaysHide: true,
                show: 1
            });

        /*
        frm = $('auth').getElement('div.auth_frm');
        var myFx = new Fx.Slide(frm).hide();
        $('auth').getElements('div.auth_txt').addEvents({
            'click' : function(){
                myFx.stop();
                myFx.slideIn();
            }
        });
        */

    }
}

//Функция валидации для формы в самом тесте
function formInTest() {
    if ($('testbox_testform')) {

    $('testbox_testform').getElements('div.tbox_item input').addEvents({
              
              'blur': function(){
                  formInTestCheck();
              },
              'change': function(){
                  formInTestCheck();
              },
              'keyup': function(){
                  formInTestCheck();
              },
              'keypress': function(){
                  formInTestCheck();
              },
              'keydown': function(){
                  formInTestCheck();
              }
        });
    }
}
//Не пустые ли поля
function formInTestCheck() {
    if ((trim($('tbox_g_name').value) == '')
        || (trim($('tbox_g_age').value) == '')
        || (trim($('tbox_g_phone').value) == '')
        || (trim($('tbox_g_email').value) == '')) {
        $('submit_test').disabled = true;

    } else {
        $('submit_test').disabled = false;

    }
}

//Функция валидации для формы на главной
function formInAll() {
    if ($('testbox')) {
        setactionInAll();
        testformInAll();
    }
}
//Не пустые ли значения обязательных полей формы на главной
function testformInAll() {
   
        $('tb_submit').addEvents({
              'click': function(){
                  if ((trim($('tb_name').value) == '')
                            || (trim($('tb_age').value) == '')
                            || (trim($('tb_phone').value) == '')
                            || (trim($('tb_email').value) == '')
                            || (trim($('tb_name').value) == 'Ваше имя')
                            || (trim($('tb_age').value) == 'Возраст, лет')
                            || (trim($('tb_phone').value) == 'Телефон')
                            || (trim($('tb_email').value) == 'Ваш e-mail')
                    ) {
                        alert('Пожалуйста, заполните все поля');
                    } else {
                        $('testbox').submit();
                    }
              }
        });
}


function setactionInAll() {

    $('tb_name').addEvents({
        'focus': function(){
            if (trim($('tb_name').value) == 'Ваше имя') {
                this.value = '';
            }
            this.setStyle('color', '#393432');
            this.setStyle('font-style', 'normal');
        },
        'blur': function(){
            if (trim($('tb_name').value) == '') {
                this.value = 'Ваше имя';
                this.setStyle('color', '#bdbdbd');
                this.setStyle('font-style', 'italic');
            }
        }
    });


    $('tb_age').addEvents({
        'focus': function(){
            if (trim($('tb_age').value) == 'Возраст, лет') {
                this.value = '';
            }
            this.setStyle('color', '#393432');
            this.setStyle('font-style', 'normal');
        },
        'blur': function(){
            if (trim($('tb_age').value) == '') {
                this.value = 'Возраст, лет';
                this.setStyle('color', '#bdbdbd');
                this.setStyle('font-style', 'italic');
            }
        }
    });


    $('tb_phone').addEvents({
        'focus': function(){
            if (trim($('tb_phone').value) == 'Телефон') {
                this.value = '';
            }
            this.setStyle('color', '#393432');
            this.setStyle('font-style', 'normal');
        },
        'blur': function(){
            if (trim($('tb_phone').value) == '') {
                this.value = 'Телефон';
                this.setStyle('color', '#bdbdbd');
                this.setStyle('font-style', 'italic');
            }
        }
    });

    $('tb_email').addEvents({
        'focus': function(){
            if (trim($('tb_email').value) == 'Ваш e-mail') {
                this.value = '';
            }
            this.setStyle('color', '#393432');
            this.setStyle('font-style', 'normal');
        },
        'blur': function(){
            if (trim($('tb_email').value) == '') {
                this.value = 'Ваш e-mail';
                this.setStyle('color', '#bdbdbd');
                this.setStyle('font-style', 'italic');
            }
        }
    });
}



function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}