var ie6 = false,
    imgDir = "/bitrix/templates/ccg_ru/i/";                                        //folder with controls' images

$(document).ready(function(){
    changeSelects();
    $("input[type='text']").each(function(){$(this).input_value()});
    $("input.button").controls('tabs_l.png', 'tabs_r.png');
    if (ie6) {
        $("#header .submenu").parent().each(function(){$(this).submenu()});
    }
});

// ----------------------------------------  hide/show text of textboxes
$.fn.input_value = function(val) {
    if (!val) {
        $(this).attr("value") ? val = $(this).attr("title") : val = "";
    };
    $(this)
        .focus(function(){
            if (this.value == val) {
                this.value = "";
            }
        })
        .blur(function(){
            if (this.value == "") {
                this.value = val;
            }
        });
}

//----------------------------------------  add pre and post images to textboxes and buttons
$.fn.controls = function (left_img_path, right_img_path) {
    $(this).before('<img alt="" src="'+imgDir+left_img_path+'"/>');
    $(this).after('<img alt="" src="'+imgDir+right_img_path+'"/>');
}

// ---------------------------------------- submenu control
$.fn.submenu = function() {
    this.mouseleave( function(){$(this).removeClass("over")} )
        .mouseenter( function(){
            var w = $(this).width(),
                t = $(this).offset().top + $(this).height(),
                l = $(this).offset().left - $("#header").offset().left - 201;
            if (w<150) w=150;
            $(this).find("div.submenu").css({width: w, top: t, left: l});
            $(this).addClass("over");
        } );

}

//������������� �����
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

//����������� ���������
var d = document;
var offsetfromcursorY=-50 // y offset of tooltip
var ie=d.all && !window.opera;
var ns6=d.getElementById && !d.all;
var tipobj,op;

function tooltip(el,txt) {
    tipobj=d.getElementById('tooltip');
    $("span", tipobj).text(txt);
    op = 0.1;
    tipobj.style.opacity = op;
    tipobj.style.visibility="visible";
    el.onmousemove=positiontip;
    appear();
}

function hide_info(el) {
    d.getElementById('tooltip').style.visibility='hidden';
    el.onmousemove='';
}

function ietruebody(){
return (d.compatMode && d.compatMode!="BackCompat")? d.documentElement : d.body
}

function positiontip(e) {
    var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
    var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
    var winwidth=ie? ietruebody().clientWidth : window.innerWidth-20
    var winheight=ie? ietruebody().clientHeight : window.innerHeight-20
    var offsetfromcursorX=-tipobj.offsetWidth/2 // x offset of tooltip

    var rightedge=ie? winwidth-event.clientX-offsetfromcursorX : winwidth-e.clientX-offsetfromcursorX;
    var bottomedge=ie? winheight-event.clientY-offsetfromcursorY : winheight-e.clientY-offsetfromcursorY;

    if (rightedge < tipobj.offsetWidth)  tipobj.style.left=curX-tipobj.offsetWidth-offsetfromcursorX+"px";
    else tipobj.style.left=curX+offsetfromcursorX+"px";

    if (bottomedge < tipobj.offsetHeight) tipobj.style.top=curY-tipobj.offsetHeight-offsetfromcursorY+"px"
    else tipobj.style.top=curY+offsetfromcursorY+"px";
}

function appear() {
    if(op < 1) {
        op += 0.1;
        tipobj.style.opacity = op;
        tipobj.style.filter = 'alpha(opacity='+op*100+')';
        t = setTimeout('appear()', 30);
    }
}
