var cBlockTasks = new Array();

function SetPrintCSS(isPrint)
{
   var link;

   if (document.getElementsByTagName)
      link = document.getElementsByTagName('link');
   else if (document.all)
      link = document.all.tags('link');
   else
      return;

   for (var index=0; index < link.length; index++)
   {
      if (link[index].title == 'print')
         link[index].disabled = !isPrint;
   }
}

dojo.addOnLoad(function() {
  dojo.require("dijit.Dialog");
  fromDialog = Array();
  dojo.query(".toDialog").forEach(function(node, index, arr) {
    var dlg_id = "fromDialog_" + index;
    fromDialog[index] = new dijit.Dialog({
      id: dlg_id,
      "class": "fromDialog tundra",
      title: dojo.attr(node, 'title'),
      content: "<iframe width='100%' height='98%' frameborder='0' src='" + dojo.attr(node, 'href') + "'></iframe>"
    });
    if(dojo.isIE) {
      node.attachEvent('onclick', function() {fromDialog[index].show(); return false;});
    } else {
      node.setAttribute('onclick', 'fromDialog[' + index + '].show(); return false;');
    }
  //  dojo.connect(dijit.byNode(node), "onclick", null, function() { fromDialog[index].show(); return false; });
  });
});

dojo.addOnLoad(function(){
              //
  var c = dojo.byId("actDt");
  if (c){dojo.connect(dijit.byNode(c),"onValueSelected",null,setActDt)};
  if ( dojo.isIE < 7 ) {
    var _c;/* = dojo.byId("photos");
    if ( _c )*/
      dojo.query('img', _c).forEach(fixpng);
  }
  enlarge();
  _setHeight();
  _setParaFormats();
  setPageTitle();
});

enlarge = function() {
  var n = 0;
  dojo.query("img.tobig").forEach(function(im){n++;});
  if ( n == 0 )
    return;
  dojo.require("dojox.image.LightboxNano2");

  var curWidth = 0;
  var curHeight = 0;
  var S = 25000;
  var Sc = 0;
  var width = 0;
  var height = 0;

  dojo.query("img.tobig").forEach(function(im){
    curWidth = im.width;
    curHeight = im.height;
    Sc = curWidth*curHeight;
    width = Math.sqrt(S/Sc)*curWidth;
    height = S/width;
    if(im.width > im.height) {
      im.setAttribute('width', '120px');
      im.setAttribute('height', 'auto');
      stl = 'width:120px;height:auto;';
    } else {
      im.setAttribute('width', 'auto');
      im.setAttribute('height', '120px');
      stl = 'width:auto;height:120px;';
    }
    //im.width = width;
    //im.height = height;
    if ( im.parentNode.tagName.toLowerCase()=="p" || im.parentNode.tagName.toLowerCase()=="font") {
      var alg = dojo.hasAttr(im, "align")?dojo.attr(im, "align"):"left";
      var _dd = dojo.create("div",{innerHTML:"<img onclick='dojo.query(\".dojoxEnlarge\")[0].title=this.title' src='"+im.src+"' title='"+(dojo.hasAttr(im,"title")?dojo.attr(im,"title"):"")+"' class='tobig' style='"+stl+"'/>",
                            style:{float:alg,margin:"10px"}},im.parentNode,"before");
      dojo.destroy(im);
      im = dojo.query("img", _dd)[0];
    }
    var nano = new dojox.image.LightboxNano({href:dojo.attr(im,"src"), preloadDelay:0}, im);
    n++;
  });
}

fixpng = function(element) {
    if ( (dojo.isIE > 0) /*&& (dojo.isIE < 7)*/ )
        var src;
        if (element.tagName=='IMG') {
            if (/\.png$/.test(element.src)) {
                src = element.src;
                element.src = "/imgs/blank.gif";
            }
        } else {
            src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i);
            if (src) {
                src = src[1];
                element.runtimeStyle.backgroundImage="none";
            }
        }
        if (src) element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='crop')";
}

adjustIfrH = function(frId) {
  var fr = dojo.byId(frId);
  if (fr.contentDocument && fr.contentDocument.body.offsetHeight) //ns6 syntax
    fr.height = fr.contentDocument.body.offsetHeight+16;
  else if (fr.Document && fr.Document.body.scrollHeight) //ie5+ syntax
    fr.height = fr.Document.body.scrollHeight;
}

auth = function(){
  dijit.byId("dlgAuth").show();
}
setActDt = function(dt){
  var cpAct=dojo.byId("actsContent");
  var _cpAct=null;
  var _toDay=function(){
    var _d  = dojo.byId('day_' + dt.getDate() + '_' + (dt.getMonth()+1));
    if (_d) {dijit.scrollIntoView(_d);_setHeight();return true;}
    else return false;
  }
  if ( cpAct ) {
    if ( !_toDay() ) {
        _cpAct = dijit.byNode(cpAct);
        dojo.attr(_cpAct, 'href', '/ap/?yr='+dt.getFullYear() + '&mn=' + (dt.getMonth()+1));
    }
  } else {
    var ctx = dojo.byId('innerContMain');
    dojo.style(ctx, "overflow", "hidden");
    ctx.innerHTML = "";
    var plan = {href:'/zdyn/ap?yr=' + dt.getFullYear() + '&mn=' + (dt.getMonth() + 1),
                                          title:'Мероприятия ЗСК',
                                          id:'actsContent',
                                          loadingMessage:"<div class='dijitContentPaneLoading'>Загрузка Плана основных мероприятий ЗСК</div>",
                                          refreshOnShow:false,
                                          layoutAlign:'client',
                                          region:'center'};
    _cpAct = new dijit.layout.ContentPane(plan, ctx);
    dojo.addClass(_cpAct.domNode, 'tundra');
    dojo.connect(_cpAct, 'onLoad', null, _toDay);
    _cpAct.startup();
  }
 // dijit.byId('actsContent').onLoad = function () {_setHeight()/*setPageTitle()*/};
}

function setPageTitle() {
  var pageTitle = dojo.byId('pageTitle');
  if(pageTitle != undefined) {
    var hElems = document.getElementsByTagName("h1");
    if(hElems != undefined && hElems[0] != undefined) {
      pageTitle.innerHTML = hElems[0].innerHTML;
      hElems[0].parentNode.removeChild(hElems[0]);
    }
  }
}

function _setHeight(){
    var w = dojo.byId("wrap");
    var cL, cR;
    var max;
    if (w) {
    try {
            cL = dojo.byId("contLeft");
            cR = dojo.byId("contRight");
            max =  Math.max(dojo.contentBox(cL).h,Math.max(dojo.contentBox(w).h,dojo.contentBox(cR).h));
            dojo.style(cL, "height",max);
	    dojo.style(cR, "height", max);
    }catch(e){}
    }
}

function _setParaFormats(){
    var _m = dojo.byId("contMain");
    if (!_m) return;
    var n=0;
    dojo.query("p", _m).forEach(function(_p){
        if (dojo.hasAttr(_p,"align")) {
            _p.style.textAlign = dojo.attr(_p,"align");
//            dojo.removeAttr(_p,"align");
            n++;
        }
    });
//    console.debug(n+' para found');
}           ///setParaFormats

function openLeftMenu(event) {             //открываем меню слева
  var v = (event.target || event.srcElement).nextSibling;
  while(v.className != 'sub')
    v = v.nextSibling;
  if(v.style.display == 'block')
    v.style.display = 'none';
  else
    v.style.display = 'block'
}

function sortDeps(sortType) {       //сортировка депутатов
  var arr;
  var dep;
  var sortList = null;
  var alpha = dojo.byId('alphaList');
  var list = dojo.byId('depList');
  var cmlst = dojo.byId('commList');
  var frclst = dojo.byId('fracList');
  if(alpha != undefined)
    alpha.style.display = 'none';
  if(list != undefined)
    list.style.display = 'none';
  if(cmlst != undefined || frclst != undefined || alpha != undefined) {
    if(sortType == "comm") {               //comities list
      frclst.style.display = 'none';
      cmlst.style.display = '';
      sortList = dojo.query('#commList ul');
    }
    if(sortType == "frac") {               //fraction list
      frclst.style.display = '';
      cmlst.style.display = 'none';
      sortList = dojo.query('#fracList ul');
    }
    if(sortType == "alpha") {                  //full list
      frclst.style.display = 'none';
      cmlst.style.display = 'none';
      alpha.style.display = '';
      list.style.display = '';
      return;
    }
    if(sortList == null) {			//sortList а не sortType!!!	для сортировки по алфавиту
      list.style.display = '';
      alpha.style.display = '';
      var sorted = (sortType != ''?dojo.query('#depList li[alpha=' + sortType + ']'):dojo.query('#depList li'));
      arr = dojo.query('#depList li');
      for(var a in arr) {
        if(arr[a] != undefined && arr[a].style != undefined)
          arr[a].style.display = 'none';
      }
      for(var a in sorted) {
        if(sorted[a] != undefined && sorted[a].style != undefined)
          sorted[a].style.display='';
      }
      return;
    }
  }

  arr = dojo.query('#depList li');
  if(!arr || !sortList) {
    return;
  }
  var container = new Object();
  for(var cn in sortList) {
    var node = sortList[cn];
    if(node.childNodes && node.childNodes.length > 3) {
      return;
    }
    container[node.id] = node;
  }
  if(arr != undefined) {
    for(var i in arr) {
      dep = arr[i];
      if(dep.style) dep.style.display="";
      if(dep != undefined && dojo.hasAttr(dep, sortType)) {
        var sortVal = dojo.attr(dep, sortType);
        if(dojo.byId(sortVal)) {
          container[sortVal].appendChild(dep.cloneNode(true));
        }
      }
    }
  }
}

function setOnlineStat(setId) {
  var args = {
    url: "/php_scripts/online_stat.php",
    load: function(data) {
      dojo.byId(setId).innerHTML = data;
    }
  }
  dojo.xhrGet(args);
}
