﻿var cpId = 'AdminCP';
var __hf='input[name=__cvVars]';
var _imgs={down:['arr_d', '../images/arr_d.gif', 20], right:['arr_r', '../images/arr_r.gif']}

_cv = function(){}

tooltip=function(){
	var id = 'tt';
	var top = 3;
	var left = 3;
	var maxw = 300;
	var speed = 10;
	var timer = 20;
	var endalpha = 95;
	var alpha = 0;
	var tt,t,c,b,h;
	var ie = document.all ? true : false;
	return{
		show:function(title,help,w){
			if(tt == null){
				tt = document.createElement('div');
				$(tt).attr('id',id);
				$(tt).css('visibility','hidden');
				t = document.createElement('div');
				$(t).attr('id',id + 'top');
				c = document.createElement('div');
				$(c).attr('id',id + 'cont');
				if(title!='') $(tt).append(t);
				$(tt).append(c);
				$(document.body).append(tt);
				$(tt).css('opacity','0');
				$(tt).css('filter','alpha(opacity=0)');
				document.onmousemove = this.pos;
			}
			$(tt).css('display','block');
			t.innerHTML = title;
			c.innerHTML = help;
			$(tt).css('width',w ? w + 'px' : 'auto');
			if(!w && ie){
				$(t).css('display','none');
				$(tt).css('width',$(tt).innerWidth());
				$(t).css('display','block');
			}
			if($(tt).innerWidth() > maxw){$(tt).css('width',maxw + 'px')}
			h = parseInt($(tt).innerHeight()) + top;
			clearInterval(tt.timer);
			tt.timer = setInterval(function(){tooltip.fade(1)},timer);
		},
		showAlt:function(text){
		    this.show('',text);
		},
		pos:function(e){
			var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
			var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
			$(tt).css('top',(u - h) + 'px');
			$(tt).css('left',(l + left-100) + 'px');
		},
		fade:function(d){
			var a = alpha;
			if((a != endalpha && d == 1) || (a != 0 && d == -1)){
				var i = speed;
				if(endalpha - a < speed && d == 1){
					i = endalpha - a;
				}else if(alpha < speed && d == -1){
					i = a;
				}
				alpha = a + (i * d);
				$(tt).css('visibility','visible');
				$(tt).css('opacity',alpha * .01);
				$(tt).css('filter','alpha(opacity=' + alpha + ')');
			}else{
				clearInterval(tt.timer);
				if(d == -1){$(tt).css('display','none')}
			}
		},
		hide:function(){
		   if(tt != undefined){
			   clearInterval(tt.timer);
			   tt.timer = setInterval(function(){tooltip.fade(-1)},timer);
		   }
		}
	};
}();
_cv.setVar=function(key,value){
    var v = this.getVars();
    var sVals='';
    v[key] = value;
    var s;
    for (s in v)
        sVals += '~|~' + s + '~!~' + v[s];
    var p=0;
    if(sVals.length>0) p=3;
    $(__hf).val(sVals.substr(p));
}
_cv.getVar=function(key){
   if (this.getVars()[key] != null)
   {
	 return this.getVars()[key];
   }
   return '';
}
_cv.getVars=function(){
   var vars = new Array();
   temp = $(__hf).val().split('~|~');
   for(i=0;i<temp.length;i++){
     aryItem=temp[i].split('~!~');
     if (aryItem.length == 2)
	    vars[aryItem[0]] = aryItem[1];
   }  
   return vars;
}
showMessage = function(container,type,title,text){
    if(type==undefined && cv_message!=undefined){
        type=cv_message.type;
        title=cv_message.title;
        text=cv_message.text;
    }
    o=$('#'+container);
    if(o.length>0){
       o.html('<div class="ph t'+type+'"><div class="icon"></div>'+text+'</div>').fadeIn(500);
    }  
}

hideMessage = function(container){
    o=$('#'+container);   
    if(o.length>0){
       o.fadeOut(500);
    }
}
_cv.hilightWord = function(blockId, words, className){
    str=$('#'+blockId).html();
    lowerStr=str.toLowerCase(); 
    result="";
    for(i=0;i<words.length;i++){
        word=words[i].toLowerCase();
        result="";
        while(lowerStr.indexOf(word)>=0 && word.length>2){
          index=lowerStr.indexOf(word);
          lowerStr=lowerStr.substr(index+word.length);
          result+=str.substr(0,index)+"<span class='"+className+"'>"+str.substr(index,word.length)+"</span>";
          str=str.substr(index+word.length)
        }
        result+=str;
        str=result;
        lowerStr=result.toLowerCase(); 
    }
    $('#'+blockId).html(result);
}

function userSessionReset(){
   $.ajax({
       type: "POST",
       url: "Default.aspx/ResetUserSession",
       data: "{}",
       contentType: "application/json; charset=utf-8",
       success: callBack,
       dataType: "json",
       error: ajaxCallFailed
   });
}
function callBack(r){

}
function ajaxCallFailed(res, error) {
    //alert(error);
}
$(document).ready(function()
{
  jmenu.buildmenu("tnav", _imgs);
  setInterval('userSessionReset()',60000);
  
  fixHeight();
  setInterval("fixHeight()",2000);
});
function fixHeight(){
  var cnt_maxh=0;
  var hreset=false;
  $('.cnt_box').each(function(){
    if(cnt_maxh!=0 && cnt_maxh!=this.offsetHeight){
        hreset=true;
    }
    if(this.offsetHeight>cnt_maxh) cnt_maxh=this.offsetHeight;
  })
  cnt_maxh=(cnt_maxh%2!=0)?cnt_maxh+1:cnt_maxh;
  if(hreset){
    $('.cnt_box .ph').css("height",cnt_maxh+"px");
  }
}
