/*
 * -- grayscale.js --
 * Copyright (C) James Padolsey (http://james.padolsey.com)
 *
 */

var grayscale = (function(){
    
    var config = {
            colorProps: ['color','backgroundColor','borderBottomColor','borderTopColor','borderLeftColor','borderRightColor','backgroundImage'],
            externalImageHandler : {
                /* Grayscaling externally hosted images does not work
                   - Use these functions to handle those images as you so desire */
                /* Out of convenience these functions are also used for browsers
                   like Chrome that do not support CanvasContext.getImageData */
                init : function(el, src) {
                    if (el.nodeName.toLowerCase() === 'img') {
                        // Is IMG element...
                    } else {
                        // Is background-image element:
                        // Default - remove background images
                        data(el).backgroundImageSRC = src;
                        el.style.backgroundImage = '';
                    }
                },
                reset : function(el) {
                    if (el.nodeName.toLowerCase() === 'img') {
                        // Is IMG element...
                    } else {
                        // Is background-image element:
                        el.style.backgroundImage = 'url(' + (data(el).backgroundImageSRC || '') + ')';
                    }
                }
            }
        },
        log = function(){
            try { window.console.log.apply(console, arguments); }
            catch(e) {};
        },
        isExternal = function(url) {
            // Checks whether URL is external: 'CanvasContext.getImageData'
            // only works if the image is on the current domain.
            return (new RegExp('https?://(?!' + window.location.hostname + ')')).test(url);
        },
        data = (function(){
            
            var cache = [0],
            expando = 'data' + (+new Date());
            
            return function(elem) {
                var cacheIndex = elem[expando],
                    nextCacheIndex = cache.length;
                if(!cacheIndex) {
                    cacheIndex = elem[expando] = nextCacheIndex;
                    cache[cacheIndex] = {};
                }
                return cache[cacheIndex];
            };
            
        })(),
        desatIMG = function(img, prepare, realEl) {
            
            // realEl is only set when img is temp (for BG images)
            
            var canvas = document.createElement('canvas'),
                context = canvas.getContext('2d'),
                height = img.naturalHeight || img.offsetHeight || img.height,
                width = img.naturalWidth || img.offsetWidth || img.width,
                imgData;
                
            canvas.height = height;
            canvas.width = width;
            context.drawImage(img, 0, 0);
            try {
                imgData = context.getImageData(0, 0, width, height);
            } catch(e) {}
            
            if (prepare) {
                desatIMG.preparing = true;
                // Slowly recurse through pixels for prep,
                // :: only occurs on grayscale.prepare()
                var y = 0;
                (function(){
                    
                    if (!desatIMG.preparing) { return; }
                    
                    if (y === height) {
                        // Finished!
                        context.putImageData(imgData, 0, 0, 0, 0, width, height);
                        realEl ? (data(realEl).BGdataURL = canvas.toDataURL())
                               : (data(img).dataURL = canvas.toDataURL())
                    }
                    
                    for (var x = 0; x < width; x++) {
                        var i = (y * width + x) * 4;
                        // Apply Monoschrome level across all channels:
                        imgData.data[i] = imgData.data[i+1] = imgData.data[i+2] =
                        RGBtoGRAYSCALE(imgData.data[i], imgData.data[i+1], imgData.data[i+2]);
                    }
                    
                    y++;
                    setTimeout(arguments.callee, 0);
                    
                })();
                return;
            } else {
                // If desatIMG was called without 'prepare' flag
                // then cancel recursion and proceed with force! (below)
                desatIMG.preparing = false;
            }
            
            for (var y = 0; y < height; y++) {
                for (var x = 0; x < width; x++) {
                    var i = (y * width + x) * 4;
                    // Apply Monoschrome level across all channels:
                    imgData.data[i] = imgData.data[i+1] = imgData.data[i+2] =
                    RGBtoGRAYSCALE(imgData.data[i], imgData.data[i+1], imgData.data[i+2]);
                }
            }
            
            context.putImageData(imgData, 0, 0, 0, 0, width, height);
            return canvas;
        
        },
        getStyle = function(el, prop) {
            var style = document.defaultView && document.defaultView.getComputedStyle ? 
                        document.defaultView.getComputedStyle(el, null)[prop]
                        : el.currentStyle[prop];
            // If format is #FFFFFF: (convert to RGB)
            if (style && /^#[A-F0-9]/i.test(style)) {
                    var hex = style.match(/[A-F0-9]{2}/ig);
                    style = 'rgb(' + parseInt(hex[0], 16) + ','
                                   + parseInt(hex[1], 16) + ','
                                   + parseInt(hex[2], 16) + ')';
            }
            return style;
        },
        RGBtoGRAYSCALE = function(r,g,b) {
            // Returns single monochrome figure:
            return parseInt( (0.2125 * r) + (0.7154 * g) + (0.0721 * b), 10 );
        },
        getAllNodes = function(context) {
            var all = Array.prototype.slice.call(context.getElementsByTagName('*'));
            all.unshift(context);
            return all;
        };
        
    var init = function(context) {
        
        // Handle if a DOM collection is passed instead of a single el:
        if (context && context[0] && context.length && context[0].nodeName) {
            // Is a DOM collection:
            var allContexts = Array.prototype.slice.call(context),
                cIndex = -1, cLen = allContexts.length;
            while (++cIndex<cLen) { init.call(this, allContexts[cIndex]); }
            return;
        }
        
        context = context || document.documentElement;
        
        if (!document.createElement('canvas').getContext) {
            context.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)';
            context.style.zoom = 1;
            return;
        }
        
        var all = getAllNodes(context),
            i = -1, len = all.length;
            
        while (++i<len) {
            var cur = all[i];
            
            if (cur.nodeName.toLowerCase() === 'img') {
                var src = cur.getAttribute('src');
                if(!src) { continue; }
                if (isExternal(src)) {
                    config.externalImageHandler.init(cur, src);
                } else {
                    data(cur).realSRC = src;
                    try {
                        // Within try statement just encase there's no support....
                        cur.src = data(cur).dataURL || desatIMG(cur).toDataURL();
                    } catch(e) { config.externalImageHandler.init(cur, src); }
                }
                
            } else {
                for (var pIndex = 0, pLen = config.colorProps.length; pIndex < pLen; pIndex++) {
                    var prop = config.colorProps[pIndex],
                    style = getStyle(cur, prop);
                    if (!style) {continue;}
                    if (cur.style[prop]) {
                        data(cur)[prop] = style;
                    }
                    // RGB color:
                    if (style.substring(0,4) === 'rgb(') {
                        var monoRGB = RGBtoGRAYSCALE.apply(null, style.match(/\d+/g));
                        cur.style[prop] = style = 'rgb(' + monoRGB + ',' + monoRGB + ',' + monoRGB + ')';
                        continue;
                    }
                    // Background Image:
                    if (style.indexOf('url(') > -1) {
                        var urlPatt = /\(['"]?(.+?)['"]?\)/,
                            url = style.match(urlPatt)[1];
                        if (isExternal(url)) {
                            config.externalImageHandler.init(cur, url);
                            data(cur).externalBG = true;
                            continue;
                        }
                        // data(cur).BGdataURL refers to caches URL (from preparation)
                        try {
                            var imgSRC = data(cur).BGdataURL || (function(){
                                    var temp = document.createElement('img');
                                    temp.src = url;
                                    return desatIMG(temp).toDataURL();
                                })();
                            
                            cur.style[prop] = style.replace(urlPatt, function(_, url){
                                return '(' + imgSRC + ')';
                            });
                        } catch(e) { config.externalImageHandler.init(cur, url); }
                    }
                }
            }
        }
        
    };
    
    init.reset = function(context) {
        // Handle if a DOM collection is passed instead of a single el:
        if (context && context[0] && context.length && context[0].nodeName) {
            // Is a DOM collection:
            var allContexts = Array.prototype.slice.call(context),
                cIndex = -1, cLen = allContexts.length;
            while (++cIndex<cLen) { init.reset.call(this, allContexts[cIndex]); }
            return;
        }
        context = context || document.documentElement;
        if (!document.createElement('canvas').getContext) {
            context.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(grayscale=0)';
            return;
        }
        var all = getAllNodes(context),
            i = -1, len = all.length;
        while (++i<len) {
            var cur = all[i];
            if (cur.nodeName.toLowerCase() === 'img') {
                var src = cur.getAttribute('src');
                if (isExternal(src)) {
                    config.externalImageHandler.reset(cur, src);
                }
                cur.src = data(cur).realSRC || src;
            } else {
                for (var pIndex = 0, pLen = config.colorProps.length; pIndex < pLen; pIndex++) {
                    if (data(cur).externalBG) {
                        config.externalImageHandler.reset(cur);
                    }
                    var prop = config.colorProps[pIndex];
                    cur.style[prop] = data(cur)[prop] || '';
                }
            }
        }
    };
    
    init.prepare = function(context) {
        
        // Handle if a DOM collection is passed instead of a single el:
        if (context && context[0] && context.length && context[0].nodeName) {
            // Is a DOM collection:
            var allContexts = Array.prototype.slice.call(context),
                cIndex = -1, cLen = allContexts.length;
            while (++cIndex<cLen) { init.prepare.call(null, allContexts[cIndex]); }
            return;
        }
        
        // Slowly recurses through all elements
        // so as not to lock up on the user.
        
        context = context || document.documentElement;
        
        if (!document.createElement('canvas').getContext) { return; }
        
        var all = getAllNodes(context),
            i = -1, len = all.length;
        
        while (++i<len) {
            var cur = all[i];
            if (data(cur).skip) { return; }
            if (cur.nodeName.toLowerCase() === 'img') {
                if (cur.getAttribute('src') && !isExternal(cur.src)) {
                    desatIMG(cur, true);
                }
                
            } else {
                var style = getStyle(cur, 'backgroundImage');
                if (style.indexOf('url(') > -1) {
                    var urlPatt = /\(['"]?(.+?)['"]?\)/,
                        url = style.match(urlPatt)[1];
                    if (!isExternal(url)) {
                        var temp = document.createElement('img');
                        temp.src = url;
                        desatIMG(temp, true, cur);
                    }
                }
            }
        }
    };
    
    return init;

})();

function ble()
{
  if(cur > last)
    cur=0;
  document.getElementById("a1").src = "http://www.europsl.pl/" + phs[cur][0];
  document.getElementById("a2").innerHTML = "<a href=http://wiadomosci.europsl.pl/" + phs[cur][2] + ">" + phs[cur][1] + "</a>";
  document.getElementById("a3").href = "http://wiadomosci.europsl.pl/" + phs[cur][2];
  cur++;
  autocontrolvar=setTimeout("ble()",14000)
}
function ble2()
{
  if(cur2 > last2)
    cur2=0;
  document.getElementById("a12").src =  phs2[cur2][0];
  document.getElementById("a22").innerHTML = "<a href="+ phs2[cur2][2] + ">" + phs2[cur2][1] + "</a>";
  document.getElementById("a32").href = phs2[cur2][2];
  cur2++;
  autocontrolvar=setTimeout("ble2()",8000)
}
function ble3()
{
  if(cur3 > last3)
    cur3=0;
  document.getElementById("a13").src = "http://www.europsl.pl/" + phs3[cur3][0];
  document.getElementById("a23").innerHTML = "<a href=http://wiadomosci.europsl.pl/" + phs3[cur3][2] + ">" + phs3[cur3][1] + "</a>";
  document.getElementById("a33").href = "http://wiadomosci.europsl.pl/" + phs3[cur3][2];
  cur3++;
  autocontrolvar=setTimeout("ble3()",12000)
}
function ble4()
{
  if(cur4 > last4)
    cur4=0;
  document.getElementById("a14").src =  "http://europsl.pl/" + phs4[cur4][0];
  document.getElementById("a24").innerHTML = "<a href=http://wiadomosci.europsl.pl/"+ phs4[cur4][2] + ">" + phs4[cur4][1] + "</a>";
  document.getElementById("a34").href = "http://wiadomosci.europsl.pl/" +  phs4[cur4][2];
  cur4++;
  autocontrolvar1=setTimeout("ble4()",10000)
}
function ble5()
{
  if(cur5 > last5)
    cur5=0;
  document.getElementById("a15").src =  "http://europsl.pl/" + phs5[cur5][0];
  document.getElementById("a25").innerHTML = "<a href=http://wiadomosci.europsl.pl/"+ phs5[cur5][2] + ">" + phs5[cur5][1] + "</a>";
  document.getElementById("a35").href = "http://wiadomosci.europsl.pl/" +  phs5[cur5][2];
  cur5++;
  autocontrolvar=setTimeout("ble5()",8000)
}
function photoSubmit()
{
  showLoadingStatus();
  //alert('aaa');
 // return StartUpload();
}
function showLoadingStatus()
{
  document.getElementById('loadingStatus').style.display = '';
  document.getElementById('loadingSubmit').style.display = 'none';
}
function Myconfirm(Str)
{
	return confirm(Str);
}
function ratesOn(rate)
{
	for(i=1;i<=5;i++)
		document.getElementById(('rate'+i)).src='pix/gwiazda_off.gif';
	for(i=1;i<=rate;i++)
		document.getElementById(('rate'+i)).src='pix/gwiazda_on.gif';

}
function Gfx(pic,x,y)
{
    var s='<html><head><TITLE>www.europsl.pl</TITLE><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2"></head>\n';
     s=s+'<body BGCOLOR="#ffffff" MARGINWIDTH=0 MARGINHEIGHT=0 TOPMARGIN=0 LEFTMARGIN=0>\n';
    s=s+'<A HREF="javascript:close()"><IMG ALT="Close..." BORDER=0 SRC="'+pic+'"></A>\n';
    s=s+'</body></html>';

    var f = null;
    f = window.open('','','width='+x+',height='+y+',left=50,top=20,resizable=1,directories=0,location=0,menubar=0,scrollbars=0,status=0,toolbar=0');
    if(f != null) {
        if(f.opener == null) {
          f.opener = self
        }
        f.document.clear();
        f.document.write(s);
        f.document.close();
    }
}
function ePrint(url)
{
	f = window.open(url,'','width=800,height=600,left=50,top=20,resizable=1,directories=0,location=0,menubar=0,scrollbars=0,status=0,toolbar=0');

}
function radarInfo(url)
{
	f = window.open(url,'','width=300,height=200,left=50,top=20,resizable=1,directories=0,location=0,menubar=0,scrollbars=0,status=0,toolbar=0');

}
function showHide(id)
{
  if(document.getElementById(id).style.display == "")
    document.getElementById(id).style.display = "none";
  else
    document.getElementById(id).style.display = "";
}
function hide(id)
{
  document.getElementById(id).style.display = "none";
}
function show(id)
{
    document.getElementById(id).style.display = "";
}
function fieldValid(field,warning)
{
	if(field.value == '')
	{
		alert(warning);
		field.focus();
		return false;
	}
	else
		return true;
}
function abuseSend(site,type,id)
{
  document.location.href='http://www.europsl.pl/'+site+','+type+','+id+'.html';
}