var myWidth = 0;
var myHeight = 0;

function ekoAlert(msg,title,x,y,img,czyButton){
getSize();
var widthParentAlert = x;
var heightParentAlert = y;
var body = document.getElementsByTagName('BODY')[0];
var pa;
if( pa = document.getElementById('ParentAlert')){
var paSadow = document.getElementById('ParentAlertSadow');
body.removeChild(pa);
body.removeChild(paSadow);}
var image = new Image();
image.src = "http://www.ekologia.pl/assets/images/bladAlert/img"+img+".png";
var divParentAlert = document.createElement('div');
divParentAlert.className = 'parentAlert';
divParentAlert.id = 'ParentAlert';
divParentAlert.style.width = widthParentAlert+'px';
divParentAlert.style.height = heightParentAlert+'px';
divParentAlert.style.top = (myHeight)-heightParentAlert/2+'px';
divParentAlert.style.left = (myWidth/2)-widthParentAlert/2+'px';
divParentAlert.style.zIndex = 2;
var divShadowAlert = document.createElement('div');
divShadowAlert.className = 'parentAlertSadow';
divShadowAlert.id = 'ParentAlertSadow';
divShadowAlert.style.width = widthParentAlert+'px';
divShadowAlert.style.height = heightParentAlert+'px';
divShadowAlert.style.top = (myHeight)-(heightParentAlert/2)+3+'px';
divShadowAlert.style.left = (myWidth/2)-(widthParentAlert/2)+3+'px';
divShadowAlert.style.zIndex = 1;
//TOP
var divTopAlert = document.createElement('div');
divTopAlert.id = 'topAlert';
divTopAlert.className = 'topAlert';
//var divTopLeftAlert = document.createElement('div');
//divTopLeftAlert.id = 'topLeftAlert';
//divTopLeftAlert.className = 'topLeftAlert';
var divTopCenterAlert = document.createElement('div');
divTopCenterAlert.style.width = widthParentAlert-2+'px';
divTopCenterAlert.id = 'topCenterAlert';
divTopCenterAlert.className = 'topCenterAlert';
//var divTopRightAlert = document.createElement('div');
//divTopRightAlert.id = 'topRightAlert';
//divTopRightAlert.className = 'topRightAlert';
//divTopAlert.appendChild(divTopLeftAlert);
divTopAlert.appendChild(divTopCenterAlert);
//divTopAlert.appendChild(divTopRightAlert);
//KONIEC TOP
//CENTER
var divCenterAlert = document.createElement('div');
divCenterAlert.id = 'centerAlert';
divCenterAlert.className = 'centerAlert';
//var divCenterLeftAlert = document.createElement('div');
//divCenterLeftAlert.id = 'centerLeftAlert';
//divCenterLeftAlert.className = 'centerLeftAlert';
//divCenterLeftAlert.style.height = heightParentAlert-26+'px';
var divCenterCenterAlert = document.createElement('div');
divCenterCenterAlert.style.width = widthParentAlert-2+'px';
divCenterCenterAlert.style.height = heightParentAlert-27+'px';
divCenterCenterAlert.id = 'centerCenterAlert';
divCenterCenterAlert.className = 'centerCenterAlert';
//var divCenterRightAlert = document.createElement('div');
//divCenterRightAlert.style.height = heightParentAlert-26+'px';
//divCenterRightAlert.id = 'centerRightAlert';
//divCenterRightAlert.className = 'centerRightAlert';
//divCenterAlert.appendChild(divCenterLeftAlert);
divCenterAlert.appendChild(divCenterCenterAlert);
//divCenterAlert.appendChild(divCenterRightAlert);
//KONIEC CENTER
//BOTTOM
//var divBottomAlert = document.createElement('div');
//divBottomAlert.id = 'bottomAlert';
//divBottomAlert.className = 'bottomAlert';
//var divBottomCenterAlert = document.createElement('div');
//divBottomCenterAlert.style.width = widthParentAlert-2+'px';
//divBottomCenterAlert.id = 'bottomCenterAlert';
//divBottomCenterAlert.className = 'bottomCenterAlert';
//divBottomAlert.appendChild(divBottomCenterAlert);
//KONIEC BOTTOM
var divClose = document.createElement('div');
divClose.id = 'closeAlert';
divClose.className = 'closeAlert';
divClose.style.left = widthParentAlert-30+'px';
divClose.onclick = 'zamknijAlert()';
if(window.addEventListener) divClose.addEventListener('click',zamknijAlert,false);
else divClose.attachEvent('onclick',zamknijAlert,false); 
divParentAlert.appendChild(divClose);
var divTitle = document.createElement('div');
divTitle.className = 'titleAlert';
var textTitle = document.createTextNode(title);
divTitle.appendChild(textTitle);
divTopAlert.appendChild(divTitle);

var divParentTresc = document.createElement('div');
divParentTresc.className = 'parentTrescAlert';
divParentTresc.style.width = widthParentAlert-10+'px';
divParentTresc.style.height = heightParentAlert-50+'px';


var divImage = document.createElement('div');
divImage.className = 'imageAlert';
var imgA = document.createElement('img');
imgA.src = image.src;
divImage.appendChild(imgA);

var divTresc = document.createElement('div');
divTresc.className = 'trescAlert';
divTresc.style.width = widthParentAlert-60+'px';
var textTresc = document.createTextNode(msg);
divTresc.appendChild(textTresc);


divParentTresc.appendChild(divImage);
divParentTresc.appendChild(divTresc);


divCenterAlert.appendChild(divParentTresc);

if(czyButton == 1){

var divButton = document.createElement('div');
divButton.className = 'divButton';
divButton.style.top = heightParentAlert-32+'px';
divButton.style.paddingLeft = '5px';

var buttonP = document.createElement('div');

buttonP.id = '1';
//buttonP.setAttribute('id','1');

var button_left = document.createElement('div');
var button_middle = document.createElement('div');
var textButton = document.createElement('div');
var button_right = document.createElement('div');

buttonP.className = 'button';
button_left.className = 'button_left';
button_middle.className = 'button_middle';
textButton.className = 'textButton';
button_right.className = 'button_right';

textButton.innerHTML = "TAK";
textButton.id = '1';

button_middle.appendChild(textButton);



buttonP.appendChild(button_left);
buttonP.appendChild(button_middle);
buttonP.appendChild(button_right);


var buttonP1 = document.createElement('div');

buttonP1.id = '0';

var button_left1 = document.createElement('div');
var button_middle1 = document.createElement('div');
var textButton1 = document.createElement('div');
var button_right1 = document.createElement('div');

buttonP1.className = 'button';
button_left1.className = 'button_left';
button_middle1.className = 'button_middle';
textButton1.className = 'textButton';
button_right1.className = 'button_right';

textButton1.innerHTML = "NIE";
textButton1.id = '0';

button_middle1.appendChild(textButton1);

buttonP1.appendChild(button_left1);
buttonP1.appendChild(button_middle1);
buttonP1.appendChild(button_right1);


if(window.addEventListener) buttonP.addEventListener('click',odbierzEkoAlert,false);
else buttonP.attachEvent('onclick',odbierzEkoAlert,false); 
 
if(window.addEventListener) buttonP1.addEventListener('click',odbierzEkoAlert,false);
else buttonP1.attachEvent('onclick',odbierzEkoAlert,false);  


divButton.appendChild(buttonP);
divButton.appendChild(buttonP1);
divParentAlert.appendChild(divButton);

};

divParentAlert.appendChild(divTopAlert);
divParentAlert.appendChild(divCenterAlert);
//divParentAlert.appendChild(divBottomAlert);
body.appendChild(divParentAlert);
body.appendChild(divShadowAlert);

//alert(body.innerHTML);


Drag.init(divTopAlert, divParentAlert,divShadowAlert)};

function getSize(){

//alert(window.pageXOffset);

//alert(getYOffset()-window.innerHeight);
//alert(getYOffset());

 if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = parseInt(window.innerWidth);
    //myHeight = window.innerHeight+ getYOffset()+300;
    myHeight = parseInt(getYOffset()+(window.innerHeight/2));
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = parseInt(document.documentElement.clientWidth);
    //myHeight = document.documentElement.clientHeight+ getYOffset()+300;
    myHeight = parseInt(getYOffset()+(document.documentElement.clientHeight/2));
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = parseInt(document.body.clientWidth);
    //myHeight = document.body.clientHeight+ getYOffset()+300;
    myHeight =  parseInt(getYOffset()+(document.body.clientHeight/2));
    }
    };


function getYOffset() {
    var pageY;
    if(typeof(window.pageYOffset)=='number') pageY=window.pageYOffset;
    else pageY=document.documentElement.scrollTop;
return pageY;
};


function zamknijAlert(){
	var body = document.getElementsByTagName('BODY')[0];
	var pa;
	if( pa = document.getElementById('ParentAlert')){
	var paSadow = document.getElementById('ParentAlertSadow');
	body.removeChild(pa);
	body.removeChild(paSadow);}
};



var Drag = {
obj : null,
objTlo : null, 
init : function(o, oRoot,tlo, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper){
o.onmousedown	= Drag.start;
o.hmode			= bSwapHorzRef ? false : true ;
o.vmode			= bSwapVertRef ? false : true ;
o.root = oRoot && oRoot != null ? oRoot : o ;
	if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
	if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
	if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
	if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";
o.minX	= typeof minX != 'undefined' ? minX : null;
o.minY	= typeof minY != 'undefined' ? minY : null;
o.maxX	= typeof maxX != 'undefined' ? maxX : null;
o.maxY	= typeof maxY != 'undefined' ? maxY : null;
o.xMapper = fXMapper ? fXMapper : null;
o.yMapper = fYMapper ? fYMapper : null;
o.root.onDragStart	= new Function();
o.root.onDragEnd	= new Function();
o.root.onDrag		= new Function();
},
start : function(e){
var o = Drag.obj = this;
Drag.objTlo = document.getElementById('ParentAlertSadow');
e = Drag.fixE(e);
var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
o.root.onDragStart(x, y);
o.lastMouseX	= e.clientX;
o.lastMouseY	= e.clientY;
	if (o.hmode) {
		if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
		if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
	} else {
		if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
		if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;}
	if (o.vmode) {
		if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
		if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
	} else {
		if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
		if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;}
document.onmousemove	= Drag.drag;
document.onmouseup		= Drag.end;
	return false;
},
drag : function(e){
e = Drag.fixE(e);
var o = Drag.obj;
var ey	= e.clientY;
var ex	= e.clientX;
var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
var nx, ny;
if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);
nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));
if (o.xMapper)		nx = o.xMapper(y);
else if (o.yMapper)	ny = o.yMapper(x);    
Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
Drag.obj.lastMouseX	= ex;
Drag.obj.lastMouseY	= ey;
Drag.objTlo.style[o.hmode ? "left" : "right"] = nx+ 3 + "px";
Drag.objTlo.style[o.vmode ? "top" : "bottom"] = ny + 3 + "px";
Drag.obj.root.onDrag(nx, ny);
	return false;
},
end : function(){
document.onmousemove = null;
document.onmouseup   = null;
Drag.obj.root.onDragEnd(parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
Drag.obj = null;
},
fixE : function(e){
if (typeof e == 'undefined') e = window.event;
if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
	return e;
}
};
