/**
 * @author Mirosław Bogacz
 * @version 0.3 20-03-2009 12:29
 */
/**
 * ================================================================================================================
 * @param {Object} e
 * @namespace keyPress
 * @version 0.2 20-03-2009 13:24
 * Pozwala pobrać KOD wcieśniętego klawisza klawiatury.
 * Należy użyć przy zdażeniu przekazując parametr funkcji.
 * =================================================================================================================
 */
function keyPress(e){
    this.key = (window.event) ? window.event.keyCode : e.which;
    return this.key;
}

/**
 * ================================================================================================================
 * @param {Object} e
 * @namespace eventElement
 * @version 0.2 20-03-2009 13:24
 * Pozwala uzyskac informacje na temat elemntu nad którym jest kursor.
 * Należy użyć przy zdażeniu przekazując parametr funkcji.
 * =================================================================================================================
 */
function eventElement(e){
    this.e = (e) ? e : ((window.event) ? event : null)
    if (this.e.target) {
        this.eElement = e.target;
    }
    else if (window.event.srcElement) {
        this.eElement = window.event.srcElement;
    }
    return this.eElement;
}

/**
 * ================================================================================================================
 * @param {Object} Event
 * @param {Object} Func
 * @namespace eventWindow
 * @version 0.3 23-03-2009 10:54
 * Obiekt pozwala wykonac funkcje po zdarzeniu.
 * ================================================================================================================
 */
function eventWindow(Event, Func){
    if (window.addEventListener) {
        window.addEventListener(Event, Func, true);
    }
    else if (window.attachEvent) {
        window.attachEvent('on' + Event, Func)
    }
}

/**
 * ================================================================================================================
 * @param {Object} obj
 * @namespace InputValue
 * @version 0.1 30-03-2009 10:36
 * Obiekt sprawdza czy w polu input o podanym id została zmieniona wartuość jeżeli tak to nic się nie dzieje,
 * jeżeli nie została zmieniona przywraca domyślną wartość po zajściu zdarzenia onblur.
 * ================================================================================================================
 */
function inputValue(obj){
    obj.Input = obj.Input;
    obj.InputArray = new Array();
    
    for (var i = 0; i <= obj.Input.length; i++) {
        if (obj.Input[i] && document.getElementById(obj.Input[i])) {
            obj.InputArray[i] = document.getElementById(obj.Input[i]);
            obj.InputArray[i].defaultValue = document.getElementById(obj.Input[i]).value;
            obj.InputArray[i].onfocus = function(){
                if (this.value == this.defaultValue) {
                    this.value = '';
                }
            }
            obj.InputArray[i].onblur = function(){
                if (this.value == '') {
                    this.value = this.defaultValue;
                }
            }
        }
    }
}

function element(_Element, _Event, _Func){
    if (_Element.substring(0, 1) == '#') {
        this.Element = document.getElementById(_Element.substring(1, _Element.length));
        if (this.Element.addEventListener) {
            this.Element.addEventListener(_Event, _Func, false);
        }
        else if (this.Element.attachEvent) {
            this.Element.attachEvent('on' + _Event, _Func)
        }
    }
    else if (_Element.substring(0, 1) == '.') {
    
        this.Element = new Array();
        this.ElementLicznik = 0;
        
        for (var i = 0; i <= document.getElementsByTagName('*').length; i++) {
            if (document.getElementsByTagName('*')[i]) {
                if (document.getElementsByTagName('*')[i].className == _Element.substring(1, _Element.length)) {
                    this.Element[this.ElementLicznik] = document.getElementsByTagName('*')[i];
                    
                    this.ElementLicznik++;
                }
            }
        }
        
        for (var j = 0; j <= this.Element.length; j++) {
            if (typeof(this.Element[j]) != 'undefined') {
                if (this.Element[j].addEventListener) {
                    this.Element[j].addEventListener(_Event, _Func, false);
                }
                else if (this.Element[j].attachEvent) {
                    this.Element[j].attachEvent('on' + _Event, _Func)
                }
            }
        }
    }
}

function OpacityAnimation(o){
    o.element = o.element;
    o.elementBox = o.elementBox;
    o.start = o.start;
    o.end = o.end;
    o.time = o.time;
    o.speed = o.speed;
    o._Status = 'Stop';
    o._Element = document.getElementById(o.element);
    o._Animation = false;
    o._AnimationStan = 0;
    o._ElementBox = document.getElementById(o.elementBox);
    
    o.Start = function(){
        o.AnimationStatus();
    };
    
    o.Animation = function(){
        if (o._Status == 'Up' && o._AnimationStan < 100) {
            o._AnimationStan += o.speed;
        }
        else if (o._Status == 'Down' && o._AnimationStan > 0) {
            o._AnimationStan -= o.speed;
        }
        else if (o._AnimationStan == 0) {
            clearInterval(o._Animation);
            o._ElementBox.style.display = 'none';
            o._Status = 'Stop';
        }
        
        if (document.all) {
            o._ElementBox.style.filter = "alpha(opacity:" + o._AnimationStan + ")";
        }
        else {
            o._ElementBox.style.opacity = o._AnimationStan / 100;
        }
        
    };
    
    o.AnimationStatus = function(){
        o._Element.onmouseover = function(){
            if (o._Status == 'Stop') {
                o._Animation = setInterval(o.Animation, o.time);
                o.BoxPosition();
            }
            o._ElementBox.style.display = 'block';
            o._Status = 'Up';
        };
        
        o._Element.onmouseout = function(){
            o._Status = 'Down';
        };
    };
    
    o.BoxPosition = function(){
        o._ElementBox.style.top = (o._Element.offsetTop - 30) + 'px';
        o._ElementBox.style.left = (o._Element.offsetLeft + o._Element.offsetWidth) + 'px';
    };
    
    if (o._Element) {
        o.Start();
    }
}

/**
 * @namespace RovelImg
 * @version 0.9
 * @see
 */
var RovelImg = {
    start: function(){
        var e = document.getElementsByTagName('img');
        for (var i = 0; i < e.length; i++) {
            if (e[i].className.match(/([RovelImg])/gi) && e[i].src.match(/(_off.)/gi)) {
                RovelImg.over(e[i]);
                RovelImg.out(e[i]);
            }
        }
    },
    over: function(e){
        e.onmouseover = function() {
			this.style.width = this.offsetWidth + 'px';
			this.style.height = this.offsetHeight + 'px';
			this.style.background = 'url(' + this.src + ')';
            this.src = this.src.replace('_off.', '_on.');
        }
    },
    out: function(e){
        e.onmouseout = function(){
            this.src = this.src.replace('_on.', '_off.');
        }
    }
};
/**
 * @author Mirosław Bogacz
 */
mAjaxHistory = function(){
    return false;
    this._install();
};

mAjaxHistory.prototype._install = function(){
    return false;
    setInterval('AjaxHistory._check()', 1000);
};

mAjaxHistory.prototype._set = function(_element){
    return false;
    window.mAjaxHistoryFrame.location.href = _element.href + _element.rel;
    
    this._currentAdress = _element.href + _element.rel;
    document.getElementById('Sprawdz').innerHTML = this._currentAdress;
    
};

mAjaxHistory.prototype._check = function(){
    return false;
    if (window.mAjaxHistoryFrame.location.href !== 'about:blank') {
        if (window.mAjaxHistoryFrame.location.href === this._currentAdress) {
        }
        else {
            this._currentAdress = window.mAjaxHistoryFrame.location.href;
            alert(window.mAjaxHistoryFrame.location.href)
            var File = this._currentAdress.substring(this._currentAdress.indexOf("#file:") + 6, this._currentAdress.indexOf("#parm:"));
            if (File == 'about') {
                File = 'ajax.php?tpl=a_index';
            }
            var Parm = this._currentAdress.substring(this._currentAdress.indexOf("#parm:") + 6, this._currentAdress.length);
            apInnerStronaProduktu('kontener', File, Parm, 'chwilę...');
        }
    }
};

window.onload = function(){

    AjaxHistory = new mAjaxHistory();
}

/*########################################################################################################################################################################*/

function AjaxHistory(){
    this.History = document.location;
    //alert('||| Adres: ' + document.location + ' ||| History: ' + this.History + ' ||| HistoryContent: ' + this.HistoryContent.innerHTML);

}

function addHistory(adres){
    AjaxHistory.History = adres;
}

function AajaxHistorySp(){
    if (AjaxHistory.History != document.location) {
        //document.title = 'Wczytaj: ' + document.location;
        AjaxHistory.History = document.location;
        AjaxHistory.History = AjaxHistory.History.toString();
        var File = AjaxHistory.History.substring(AjaxHistory.History.indexOf("#file:") + 6, AjaxHistory.History.indexOf("#parm:"));
        if (File == 'http:') {
            File = 'ajax.php?tpl=a_index';
        }
        var Parm = AjaxHistory.History.substring(AjaxHistory.History.indexOf("#parm:") + 6, AjaxHistory.History.length);
        apInnerStronaProduktu('kontener', File, Parm, 'chwilę...');
        return false;
    }
    else {
        //document.title = AjaxHistory.History;
    }
}

function AajaxHistoryStart(){
    new AjaxHistory();
    addHistory(document.location)
    setInterval('AajaxHistorySp()', 200);
}

/* #################################################### */
eventWindow('load', function(){
    new element('.ListaZdjecie01', 'click', function(){
        element.style.border = '1px solid red';
    });
    AjaxHistory = new mAjaxHistory();
    RovelImg.start();
    new inputValue({
        Input: ['login', 'haslo']
    });
});


function Potwierdz(){
    var Usun = confirm("Potwierdz usunięcie konta")
    if (Usun) {
        apInnerSimple('kontener', 'ajax.php?tpl=koszyk', 'ajax=1&deleteKlient=1', 'usuwam konto...')
    }
}

function sklaujZdjecie(el, size){
    if (el.offsetWidth > size || el.offsetHeight > size) {
        if (el.offsetWidth > el.offsetHeight) {
            el.style.width = size + 'px';
        }
        else if (el.offsetWidth < el.offsetHeight) {
            el.style.height = size + 'px';
        }
        else {
        
            el.style.height = size + 'px';
            
        }
        
    }
    
}

function webDesktop(){
    var version = '0.1';
}

webDesktop.prototype.Window = function(Window){
    Window._type = Window._type;
    Window._title = Window._title;
    Window._titleMenu = Window._titleMenu;
    Window._content = Window._content;
    Window._height = Window._height;
    Window._width = Window._width;
    
    Window.Content = {
        myDiv: document.createElement('div'),
        
        create: function(){
            document.body.appendChild(this.myDiv);
            this.myDiv.className = 'webDesktopWindow';
        },
        
        get: function(){
            return this.myDiv
        },
        
        content: {
            myDiv: document.createElement('div'),
            
            create: function(){
                Window.Content.get().appendChild(this.myDiv);
                this.myDiv.className = 'webDesktopWindowContent';
            },
            
            get: function(){
                return this.myDiv;
            },
            
            content: function(){
                this.get().style.width = Window._width + 'px';
                this.get().style.height = Window._height + 'px';
                this.get().innerHTML = Window._content;
            },
            
            start: function(){
                this.create();
                this.content();
            }
        },
        
        title: {
            myDiv: document.createElement('div'),
            
            create: function(){
                Window.Content.get().appendChild(this.myDiv);
                this.myDiv.className = 'webDesktopWindowTitle';
            },
            
            get: function(){
                return this.myDiv;
            },
            
            start: function(){
                this.create();
            },
            
            text: {
                myDiv: document.createElement('div'),
                
                create: function(){
                    Window.Content.title.get().appendChild(this.myDiv);
                    this.myDiv.className = 'webDesktopWindowTitleText';
                    this.myDiv.innerHTML = Window._title;
                },
                
                get: function(){
                    return this.myDiv;
                },
                
                start: function(){
                    this.create();
                }
            },
            
            menu: {
                myDiv: document.createElement('div'),
                
                create: function(){
                    Window.Content.title.get().appendChild(this.myDiv);
                    this.myDiv.className = 'webDesktopWindowTitleMenu';
                },
                
                get: function(){
                    return this.myDiv;
                },
                
                button: {
                    minimize: {
                        myImg: document.createElement('img'),
                        
                        create: function(){
                            Window.Content.title.menu.get().appendChild(this.myImg);
                            this.myImg.src = 'tpl_front/tpl/webDesktop/minimize.gif';
                            this.myImg.title = 'Minimalizuj';
                        },
                        
                        get: function(){
                            return this.myImg;
                        },
                        
                        fun: function(){
                            this.myImg.onclick = function(){
                                if (Window.Content.content.get().style.display != 'none') {
                                    Window.Content.content.get().style.display = 'none';
                                }
                            }
                        },
                        
                        start: function(){
                            this.create();
                            this.fun();
                        }
                    },
                    
                    maximize: {
                        myImg: document.createElement('img'),
                        
                        create: function(){
                            Window.Content.title.menu.get().appendChild(this.myImg);
                            this.myImg.src = 'tpl_front/tpl/webDesktop/maximize.gif';
                            this.myImg.title = 'Maksymalizuj';
                        },
                        
                        get: function(){
                            return this.myImg;
                        },
                        
                        fun: function(){
                            this.myImg.onclick = function(){
                                if (Window.Content.content.get().style.display == 'none') {
                                    Window.Content.content.get().style.display = 'block';
                                }
                                else if (Window.Content.content.get().style.display != 'none' && Window.Content.get().offsetWidth != $m.$document.WorkSpace('x') + 2) {
                                    ResetWidth = Window.Content.get().offsetWidth;
                                    ResetHeight = Window.Content.get().offsetHeight;
                                    Window.Content.get().style.left = $m.$document.Scroll('x') + 'px';
                                    Window.Content.get().style.top = $m.$document.Scroll('y') + 'px';
                                    Window.Content.get().style.width = $m.$document.WorkSpace('x') + 'px';
                                    Window.Content.get().style.height = $m.$document.WorkSpace('y') + 'px';
                                }
                                else if (Window.Content.get().offsetWidth != ResetWidth) {
                                    Window.Content.get().style.width = ResetWidth + 'px';
                                    Window.Content.get().style.height = ResetHeight + 'px';
                                    Window.Content.get().style.left = $m.$document.Scroll('x') + ($m.$document.WorkSpace('x') / 2) - (Window.Content.get().offsetWidth / 2) + 'px';
                                    Window.Content.get().style.top = $m.$document.Scroll('y') + ($m.$document.WorkSpace('y') / 2) - (Window.Content.get().offsetHeight / 2) + 'px';
                                }
                            }
                        },
                        
                        start: function(){
                            this.create();
                            this.fun();
                        }
                    },
                    
                    close: {
                        myImg: document.createElement('img'),
                        
                        create: function(){
                            Window.Content.title.menu.get().appendChild(this.myImg);
                            this.myImg.src = 'tpl_front/tpl/webDesktop/close.gif';
                            this.myImg.title = 'Zamknij okno';
                        },
                        
                        get: function(){
                            return this.myImg;
                        },
                        
                        fun: function(){
                            this.myImg.onclick = function(){
                                document.body.removeChild(Window.Content.get());
                                document.body.removeChild(document.getElementById('MASK2'));
                            }
                        },
                        
                        start: function(){
                            this.create();
                            this.fun();
                        }
                    },
                    
                    start: function(){
                        if (Window._titleMenu != undefined) {
                            if (Window._titleMenu[0] == 1) {
                                this.minimize.start();
                            }
                            if (Window._titleMenu[1] == 1) {
                                this.maximize.start();
                            }
                            if (Window._titleMenu[2] == 1) {
                                this.close.start();
                            }
                        }
                    }
                },
                
                start: function(){
                    this.create();
                }
            }
        },
        
        start: function(){
            this.create();
            this.title.start();
            this.title.text.start();
            this.title.menu.start();
            this.title.menu.button.start();
            this.content.start();
            this.get().style.left = $m.$document.Scroll('x') + ($m.$document.WorkSpace('x') / 2) - (this.get().offsetWidth / 2) + 'px';
            this.get().style.top = $m.$document.Scroll('y') + ($m.$document.WorkSpace('y') / 2) - (this.get().offsetHeight / 2) + 'px';
            MASK = document.createElement('div');
            MASK.id = 'MASK2';
            MASK.className = 'MASK2';
            document.body.appendChild(MASK);
            
            if (window.innerWidth || window.innerHeight) {
                if (!document.documentElement.scrollHeight < document.body.offsetHeight) {
                    MASK.style.width = document.body.offsetWidth + 'px';
                    MASK.style.height = (document.documentElement.scrollHeight - document.body.offsetHeight) + document.body.offsetHeight + 'px';
                    
                }
                else {
                    MASK.style.width = document.body.offsetWidth + 'px';
                    MASK.style.height = document.body.offsetHeight + 'px';
                }
            }
            else {
                if (document.documentElement.clientHeight < document.documentElement.scrollHeight) {
                    MASK.style.width = document.body.clientWidth + 'px';
                    MASK.style.height = (document.documentElement.scrollHeight - document.documentElement.clientHeight) + document.body.clientHeight + 'px';
                    
                }
                else {
                    MASK.style.width = document.body.clientWidth + 'px';
                    MASK.style.height = document.body.clientHeight + 'px';
                }
            }
			
		      $("#MASK2").animate({ 
		        opacity: 0.5
		      }, 1500 );
			  $(".webDesktopWindow").animate({ 
		        opacity: 1
		      }, 1500 );
        }
    }
    
    
    Window.Content.start();
    
};
Sugester = {}

Sugester.Engine = function(setup){
    var Engine = {
        version: '0.1',
        url: 'http://nxs.pl/sugester/',
        
        Interface: {
            btnBar: {
                gfx: 'gfx/sugester-navigate-start.gif',
                html: document.createElement('img'),
                add: function(){
                    var that = this;
                    document.body.appendChild(that.get())
                    that.get().id = 'sugesternavigatestart'
                    that.get().onload = function(){
                        that.get().style.position = 'absolute';
                       
						$(document).ready(function() {
					    $(that.get()).css("position", "absolute");
						});
						$(that.get()).css("top", $(window).scrollTop() + (($m.$document.WorkSpace('y') / 2) - that.get().offsetHeight / 2) + "px");
						$(window).scroll(function() {
						    $(that.get()).css("top", $(window).scrollTop() + (($m.$document.WorkSpace('y') / 2) - that.get().offsetHeight / 2) + "px");
						});
                        that.get().style.left = $m.$document.WorkSpace('x') - that.get().offsetWidth + 'px';
                        that.get().style.cursor = 'pointer';
                    }
                    
                    that.get().src = Engine.url + that.gfx;
                    that.get().onclick = that.action;
                },
                get: function(){
                    var that = this;
                    return that.html;
                },
                action: function(){
                    setup.action();
                }
            }
        }
    
    };
    
    Engine.Interface.btnBar.add();
    return Engine;
};
