$(document).ready(function(e){
        var a;
        
        $('ul.jsTree').each(function(e){
                $(this).find('li').each(function(e){
                        if ($(this).html().indexOf('<ul') != -1)
                        {
                                $(this).bind('click',function(e){
                                        if ($(this).attr('class') == undefined) $(this).attr('class','');
                                        if ($(this).attr('class').indexOf('act') != -1)
                                        {
                                                $(this).attr('class','');
                                                $(this).find('a').attr('class','');
                                        } else {
                                                $(this).attr('class','act');
                                                $(this).find('a').attr('class','act');
                                                $(this).find('ul').find('a').attr('class','');
                                        }
                                        $(this).find('ul').toggle(100);
                                });
                                
                                a = $(this).find('a');
                                $(a[0]).attr('href','javascript:void()');
                        }
                });
                
                $(this).find('ul').css('display','none');
                $(this).find('li.act').each(function(){
                        $(this).click();
                        //$(this).attr('class',$(this).attr('class').split('act').join(''));
                });
                /*$(this).find('a.act').each(function(){
                        $(this).attr('class',$(this).attr('class').split('act').join(''));
                });*/
        });
        
        $('.offer').each(function(e){
                var width = $(this).find('div.left').outerWidth() + $(this).find('div.center').find('a').outerWidth() + $(this).find('div.right').outerWidth();
                $(this).css('width',width+'px')
        });
        
        
        jQuery(".radio").mousedown(function(){
                changeCheck($(this));
        });


        jQuery(".radio").each(function(){
                changeCheckStart(jQuery(this));
        });
        
        var tables = $('table.pinkCharacter'), rows, cells, th, i = 0, j = 0, k = 0;
        
        while (tables[i])
        {
                $(tables[i]).attr({
                        'cellspacing': 0,
                        'cellpadding': 0
                });
                
                j = 0;
                rows = $(tables[i]).find('tr');
                
                while (rows[j])
                {
                        if (j == 0)
                        {
                                k = 0;
                                cells = $(rows[j]).find('td');
                                $(cells[0]).attr('class',$(cells[0]).attr('class')+' lefttop');
                                while (cells[k])
                                {
                                        k++;
                                }
                                $(cells[k-1]).attr('class',$(cells[k-1]).attr('class')+' righttop');
                        } else {                        
                                if (j%2 == 0)
                                {
                                        $(rows[j]).find('td').each(function(){$(this).attr('class',$(this).attr('class')+' white')});
                                } else {
                                        $(rows[j]).find('td').each(function(){$(this).attr('class',$(this).attr('class')+' pink')});;
                                }
                        }
                        j++;
                }                

                k = 0;
                cells = $(rows[j-1]).find('td');
                $(cells[0]).attr('class',$(cells[0]).attr('class')+' leftbottom');
                while (cells[k])
                {
                        k++;
                }
                $(cells[k-1]).attr('class',$(cells[k-1]).attr('class')+' rightbottom');
                
                i++;
        }
        
        jquerySelect('select');
        
        var div = $('.searchform').find('div');
        i = 0;
        while (div[i])
        {
                $(div[i]).attr('class',$(div[i]).attr('class')+' sel'+(i+1));
                i++;
        }
        
        
        $('.orderTable').attr({
                'cellpadding': '0',
                'cellspacing': '0'
        })
        
        $('div.jquerySelect').each(function(){$(this).find('ul').bind('mouseout',function(){
                var elem = $(this);
                $('body').bind('mousemove',function(e){
                        var pos = offsetPosition(elem[0]);
                        var pageXY = mousePageXY(e);   
                        
                        if (!(pageXY.x >= pos[0] && pageXY.x <= pos[0]+elem.outerWidth() && pageXY.y >= pos[1] && pageXY.y <= elem.outerHeight()+pos[1]))
                        {
                                elem.css('display','none');
                                $('body').unbind('mousemove');
                        }
                });
        })});

        $('#recallDiv').mouseenter(function(){
                                $(this).animate({
                                        right: '-20px'
                                },{duration: 600, queue: false});
                        }).mouseleave(function(){
                                $(this).animate({
                                        right: '-255px'
                                },{duration: 600, queue: false});
                        });
});


function changeCheck(el)
{
        var el = el,
        input = el.find("input").eq(0);
        if(!input.attr("checked"))
        {
                el.css("background-position","0 0");	
                input.attr("checked", true);
        } else {
                el.css("background-position","0 -19px");	
                input.attr("checked", false)
        }
        return true;
}

function changeCheckStart(el)
{
        var el = el,
        input = el.find("input").eq(0);
        if(input.attr("checked")) {
                el.css("background-position","0 0");	
        }
        return true;
}

function dump(array,depth,ret)
{
        if ((!depth) || depth == 'undefined')
                depth = 0;
        var depth_str = '';
        for (i = 0; i<=parseInt(depth)*5; i++)
        {
                depth_str = depth_str+' ';
        }
        var str = '';
        for(key in array)
        {
                if ( array[key] instanceof Object )
                {
                        str = str+depth_str+key+' : {'+"\r\n"+dump(array[key],parseInt(depth)+1,'no')+depth_str+'}'+"\r\n";
                } else {
                        if ( array[key] instanceof Array )
                        {
                                str = str+depth_str+key+' : {'+"\r\n"+dump(array[key],parseInt(depth)+1,'no')+depth_str+'}'+"\r\n";
                        } else {
                                str = str+depth_str+key+' : '+array[key]+"\r\n";
                        }
                }
        }
        if (ret != 'no')
                alert(str);
        else
                return str;
}

function offsetPosition(element) 
{
        var offsetLeft = 0, offsetTop = 0;
        do 
        {
                offsetLeft += element.offsetLeft;
                offsetTop  += element.offsetTop;
        } while (element = element.offsetParent);
        return [offsetLeft, offsetTop];
}

function mousePageXY(e)
{
        var x = 0, y = 0;

        if (!e) e = window.event;

        if (e.pageX || e.pageY)
        {
                x = e.pageX;
                y = e.pageY;
        } else if (e.clientX || e.clientY) {
                x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
                y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
        }

        return {"x":x, "y":y};
}

function set_cookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}


function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function getBrowserInfo() { 
        var t,v = undefined; 

        if (window.chrome) t = 'Chrome'; 
        else if (window.opera) t = 'Opera'; 
        else if (document.all) { 
                t = 'IE'; 
                var nv = navigator.appVersion; 
                var s = nv.indexOf('MSIE')+5; 
                v = nv.substring(s,s+1); 
        } 
        else if (navigator.appName) t = 'Netscape'; 

        return {type:t,version:v}; 
} 
 
function bookmark(a){ 
        var url = window.document.location; 
        var title = window.document.title; 
        var b = getBrowserInfo(); 

        if (b.type == 'IE' && 8 >= b.version && b.version >= 4) 
                window.external.AddFavorite(url,title); 
        else if (b.type == 'Opera') { 
                a.href = url; 
                a.rel = "sidebar"; 
                a.title = url+','+title; 
                return true; 
        } 
        else if (b.type == "Netscape") window.sidebar.addPanel(title,url,""); 
        else alert("Нажмите CTRL-D, чтобы добавить страницу в закладки."); 
        return false; 
} 

function bannerLoad(id_rubric,id,path)
{
        $.ajax({
                url: '/!ajax/ajax.load.banner.php',
                type: 'post',
                data: ({
                        id_rubric: id_rubric,
                        id: id,
                        'act': path
                }),
                success: function(msg)
                {
                        msg  = msg.replace(/<\/?[^>]+>/gi, '');
                        var preview = msg.substr(0,msg.indexOf('|'));
                        var flash = msg.substr(msg.indexOf('|')+1,msg.indexOf('^')-msg.indexOf('|')-1);
                        var id_i = msg.substr(msg.indexOf('^')+1, msg.indexOf('*')- msg.indexOf('^')-1);
                        var id_r = msg.substr(msg.indexOf('*')+1, msg.length);
                        
                        $('.banner').find('.subleft').find('img').attr('src',''+preview+'?id='+Math.random()+'');
                        $('.banner').find('.subright').find('img').attr('src',''+preview+'?id='+Math.random()+'');
                        $('.banner').find('object').find('param').each(function(){
                                if ($(this).attr('name') == 'movie')
                                        $(this).attr('value',flash);
                        });
                        $('.banner').find('embed').attr('src',flash);
                        $('.banner').find('.left').attr('onclick','bannerLoad('+id_r+','+id_i+',"prev");');
                        $('.banner').find('.right').attr('onclick','bannerLoad('+id_r+','+id_i+',"next");');  
                }
        })
}

function recall(phone)
{
        testAjaxField('#recall','PHONE_RUS_MOSCOW',null,null,null,function(msg){
                alert('Спасибо за заявку! В ближайшее время с Вами свяжется наш менеджер!');
                ajax(
                        {type:'post',url:'/!ajax/ajax.send.mail.php'},
                        function(){},
                        {phone:phone}
                );
        });
};


