$(document).ready(function(){
    $('.bask_link_item_add').click(function(){
        $(this).hide();
        $('#b_l_i_l_' + ($(this).attr('id_item'))).show();
        bask_change_item('add', $(this).attr('id_item'));
        return false;
    });
    $('.bask_link_item_del').click(function(){
        $(this).hide();
        bask_change_item('del', $(this).attr('id_item'));
        $('#b_l_i_l_' + ($(this).attr('id_item'))).show();
        return false;
    });
});

function bask_change_item(type, id) {
    $.getJSON(
        '/functions/_ajax_controller.php',
        {'action':'bask_' + type + '_item','id':id},
        function(data){check_bask_change_item(type, data, id);}
    );
    return false;
}

function check_bask_change_item(type, data, id) {
    $('.bask_link_item_loader').hide();
    if (data['error'].length != 0) {
        if (data['error'] == '0') {
            if (type == 'add') {
                if($('#b_l_i_d_' + data['id']).val() != undefined) {
                    $('#b_l_i_d_' + data['id']).show();
                }
            }
            if (type == 'del') {
                if($('#b_l_i_a_' + data['id']).val() != undefined) {
                    $('#b_l_i_a_' + data['id']).show();
                }
                if($('#item_' + data['id'] + '_bask').val() != undefined) {
                   $('#item_' + data['id'] + '_bask').remove();
                }
            }

            if (data['count_all'].length != 0) {
                $('#back_items_count_all').html(data['count_all']);
            }
        } else {
            if (type == 'add') {
                if($('#b_l_i_a_' + id).val() != undefined) {
                    $('#b_l_i_a_' + id).show();
                }
                alert('Ошибка: ' + data['error']);
            }
            if (type == 'del') {
                if($('#b_l_i_d_' + id).val() != undefined) {
                    $('#b_l_i_d_' + id).show();
                }
                alert('Ошибка: ' + data['error']);
            }
        }
    } else {
        alert('Ошибка добавления.');
    }
    return false;
}

function reloadCaptcha(){
    document.images["captcha_img"].src="/lib/captcha.php?rnd="+Math.round(Math.random(0)*1000);
}

stuHover = function() {
	var cssRule;
	var newSelector;
	for (var i = 0; i < document.styleSheets.length; i++)
		for (var x = 0; x < document.styleSheets[i].rules.length ; x++)
			{
			cssRule = document.styleSheets[i].rules[x];
			if (cssRule.selectorText.indexOf("LI:hover") >= 0)
			{
				 newSelector = cssRule.selectorText.replace(/LI:hover/gi, "LI.iehover");
				document.styleSheets[i].addRule(newSelector , cssRule.style.cssText);
			}
		}
	var getElm = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<getElm.length; i++) {
		getElm[i].onmouseover=function() {
			this.className+=" iehover";
		}
		getElm[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" iehover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", stuHover);
