﻿// dynamic menu state
var gv_mnu=null;
var gv_mnu_id=null;
var gv_hiding=false;
var gv_onhide_event=null;
var ucTimeout=null;


function _mnuHide() {
    if (gv_mnu) {
		gv_mnu.hide();
		if (gv_onhide_event) {gv_onhide_event();gv_onhide_event=null;}
		gv_mnu=null;
		hideSelects(true);		
	}
}
function _ucStopHide(){
	clearTimeout(ucTimeout);
}

function _mnuDisableEvent(e){
    if (e.stopPropagation) {
        e.stopPropagation();
    } else if (window.event) {
        window.event.cancelBubble = true;
    }
}

function eattach(obj, s_event, f) {
    if (obj.addEventListener)
        obj.addEventListener(s_event, f,false);
    else
        obj.attachEvent('on' + s_event, f,false);
}

function _mnuDisableBubble(obj, s_event) {
    if (obj.addEventListener)
        obj.addEventListener(s_event, _mnuDisableEvent,false);
    else
        obj.attachEvent('on' + s_event, _mnuDisableEvent,false);
}


function _mnuShow(object_id, cClassName, i_width, i_height, pos_x, pos_y, show_effect, onhide){
    var t=null;
    if(object_id!=gv_mnu_id)
    {
		_mnuHide();
    }
    if (!gv_hiding || object_id!=gv_mnu_id) {

		gv_mnu_id = object_id;
		gv_mnu = $(object_id);
		gv_mnu.className = cClassName;
		_mnuDisableBubble(gv_mnu, 'click');
		gv_mnu.style.top = pos_y + 'px';
		if (i_width) gv_mnu.style.width = i_width + 'px';
		if (i_height) gv_mnu.style.height = i_height + 'px';

		gv_onhide_event = onhide;

	    hideSelects(false);

		if(document.viewport.getWidth()<Element.getWidth(gv_mnu)+pos_x)
		{
			pos_x = document.viewport.getWidth()-gv_mnu.getWidth();
		}
	    gv_mnu.style.left = pos_x + 'px';
		show_effect(gv_mnu,{duration:0.3});
    }
}

function _hint_html(event, object_align, html_id, sPozition) {
	_hint(event, object_align, sPozition, $(html_id).innerHTML, null, 200);
}


function _hint(event, object_align, sPozition, sText, sstyle, iTimer, iHideTimer, iLeftCorrect, iTopCorrect) {
	if (iTimer==null) iTimer = 200;
	if (iHideTimer==null) iHideTimer = 200;
	var pos_el;
	if (event)
		pos_el = Event.element(event);
	else
		pos_el = object_align;
	eattach(pos_el, 'mouseout', function() {clearTimeout(ucTimeout);ucTimeout=setTimeout(_mnuHide, iHideTimer);});
	clearTimeout(ucTimeout);
	ucTimeout = setTimeout(function(){_hint_render(event, object_align, sPozition, sText, sstyle, iLeftCorrect, iTopCorrect)}, iTimer);
}

function _hint_render(event, object_align, sPozition, sText, sstyle, iLeftCorrect, iTopCorrect){
	var card = document.getElementsByClassName('card_float');
		for (var i=0;i<card.length;i++) rm(card[i]);

	table = new Element('table',{'class':'hint'});		
	table.cellSpacing='0px';table.cellPadding='0px';	
	div = new Element('div',{'id':'globalhint', 'class':'card_float'})
	div.style.top='0px';
	div.style.left='0px';
	if (sstyle) div.setStyle(sstyle);
		ap(div,table);
		ap(document.body, div);	

	tbody = ap(table, new Element('tbody'));		
	tr = ap(tbody, new Element('tr',{'class':'t1'}));
		td = ap(tr, new Element('td',{'class':'l'}));td.fixpng();
		td = ap(tr, new Element('td',{'class':'c'}));td.fixpng();
		if (sPozition == 'bottom') ap(td, new Element('img',{'src':'/img/t.gif'})).fixpng();
		td = ap(tr, new Element('td',{'class':'r'}));td.fixpng();
	tr = ap(tbody, new Element('tr',{'class':'t2'}));
		td = ap(tr, new Element('td',{'class':'l'}));td.fixpng();
		td = ap(tr, new Element('td',{'class':'c'}));td.fixpng();
		td = ap(tr, new Element('td',{'class':'r'}));td.fixpng();
	tr = ap(tbody, new Element('tr',{'class':'t3'}));
		td = ap(tr, new Element('td',{'class':'l'}));td.fixpng();
		if (sPozition == 'right') ap(td, new Element('img',{'src':'/img/t.gif'})).fixpng();
		td = ap(tr, new Element('td',{'class':'c'}));td.fixpng();
			ap(td, new Element('span')).innerHTML = sText;
		td = ap(tr, new Element('td',{'class':'r'}));td.fixpng();
		if (sPozition == 'left') ap(td, new Element('img',{'src':'/img/t.gif'})).fixpng();		
	tr = ap(tbody, new Element('tr',{'class':'t4'}));
		td = ap(tr, new Element('td',{'class':'l'}));td.fixpng();
		td = ap(tr, new Element('td',{'class':'c'}));td.fixpng();
		td = ap(tr, new Element('td',{'class':'r'}));td.fixpng();
	tr = ap(tbody, new Element('tr',{'class':'t5'}));
		td = ap(tr, new Element('td',{'class':'l'}));td.fixpng();
		td = ap(tr, new Element('td',{'class':'c'}));td.fixpng();
		if (sPozition == 'top') ap(td, new Element('img',{'src':'/img/t.gif'})).fixpng();				
		td = ap(tr, new Element('td',{'class':'r'}));td.fixpng();


	var pos_el;
	if (event)
		pos_el = Event.element(event);
	else
		pos_el = object_align;
	if (object_align) pos_el = object_align;
	var xy = Position.page(pos_el);
	var dm = pos_el.getDimensions();		
	if (sPozition.substr(0,6) == 'bottom') {
		var	x = xy[0] + dm.width - table.getDimensions().width;
		var	y = xy[1] + dm.height - 3;
	}
	if (sPozition.substr(0,4) == 'left') {
		var	x = xy[0] - table.getDimensions().width + 3;
		var	y = xy[1] - dm.height / 2 - 5 ;
	}
	
	if (sPozition.substr(0,5) == 'right') {
		var	x = xy[0] + dm.width - 3 ;
		var	y = xy[1] - dm.height / 2 - 5 ;
	}	

	if (iLeftCorrect) x = x + iLeftCorrect;
	if (iTopCorrect) y = y + iTopCorrect;
	
	y += (document.body.scrollTop) ? document.body.scrollTop : document.documentElement.scrollTop;
	if (x < 0) {x = 10};
	if (y < 0) {y = 10};
	


	div.onmouseover = function(){_ucStopHide();};
	div.onmouseout = function(){ucTimeout=setTimeout(_mnuHide,400);};
	if (object_align) object_align.onmouseout = function(){ucTimeout=setTimeout(_mnuHide,400);};
	_mnuShow(div.id,'card_float',null,null,x,y, Effect.Appear, null);
}

function _cal_hide(){
	var card = document.getElementsByClassName('card_float');
		for (var i=0;i<card.length;i++) rm(card[i]);
}
function _cal_show(event, object_align, obj_id){
	_cal_hide();

	div = new Element('div',{'id':'globalhint', 'class':'card_float'})
	div.style.top='0px';
	div.style.left='0px';
	div.innerHTML = $(obj_id).innerHTML;
	ap(document.body, div);	

	var pos_el = object_align;
	var xy = Position.page(pos_el);
	var dm = pos_el.getDimensions();		
	var	x = xy[0] - div.getDimensions().width+10;
	var	y = xy[1] + 15;


	
	y += (document.body.scrollTop) ? document.body.scrollTop : document.documentElement.scrollTop;
	if (x < 0) {x = 10};
	if (y < 0) {y = 10};

	if (readCookie('cal-c-left')) x = parseInt(readCookie('cal-c-left'));
	if (readCookie('cal-c-top')) y = parseInt(readCookie('cal-c-top'));

		
	new Draggable('globalhint',
			{ onEnd : function(){
				createCookie('cal-c-left',Position.page($('globalhint'))[0],100);
				createCookie('cal-c-top',Position.page($('globalhint'))[1]+((document.body.scrollTop) ? document.body.scrollTop : document.documentElement.scrollTop),100);
				}}
		);
	_mnuShow(div.id,'card_float',null,null,x,y, Effect.Appear, null);
}

Prototype.Browser.Version = (navigator.userAgent.toLowerCase().match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1];
Prototype.FixPNG = { Pixel: '/img/pixel.gif' };
Element.addMethods({
  fixpng: (Prototype.Browser.IE && Prototype.Browser.Version < 7) ? function(element) {
    function fixPosition(element) { if(element.style.position != 'absolute') element.style.position == 'relative'; }
    function filter(src) { return "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod='scale',src='" + src + "')"; }
    var elementName = element.nodeName.toLowerCase();
    if(elementName == 'img' || elementName == 'input')
    {
      if(element.src && element.src.match(/.*\.png([?].*)?$/i)) 
      {
        element.setStyle(
        {
		  filter: filter(element.src),
          width: element.getWidth(),
          height: element.getHeight()
        });
        element.src = Prototype.FixPNG.Pixel;
        fixPosition(element);
      }
    } 
    {
      var image = element.currentStyle.backgroundImage;
      if (image.indexOf(".png")!=-1)
      {
        element.setStyle({
          filter: filter(image.split('url("')[1].split('")')[0]),
          backgroundImage: "none"
        });
        element.childElements().each(fixPosition);
      }
    }
  } : function(element) {return false;}
});  

Event.observe(window, 'load', function() {
  $$('.fixpng').each(function(element){ element.fixpng() });
}, false);

