window.addEvent("domready", function(){
    
    var siteAccordion = new Fx.Accordion($$('.peffect_title'), $$('.peffect_text'), { display:-1, alwaysHide: true });
    
    var bla = new DFLTBgPattern({
        el: { type: 'img', src: 'img/bg.jpg' },
        position: { x: 'center', y: 'center' },
        fade: { tween: true, duration: 1000 }
    });
    bla.addEvent('ready',function(){
        if(Browser.firefox) { $$('*').setStyle('-moz-user-select','none'); }
        if(Browser.safari || Browser.chrome) { $$('*').setStyle('-webkit-user-select','none'); }
        if(Browser.ie){ document.body.addEvent("selectstart", function(){ return false; }); }
        if(Browser.opera){ document.body.addEvent("mousedown", function(){ return false; }); }
    });
    
});

var DFLTBgPattern = new Class({
    Implements: [Options, Events],
    options: {
        el: {
            type: '',
            src: '',
            size: { x: 0, y: 0 }
        },
        contentLayer: 'contentLayer',
        printLayer: 'printLayer',
        position: { x: 'right', y: 'bottom' },
        fade: { tween: false, duration: 2000 },
        overlayImg: 'img/clearBlack.gif',
        onReady: function(){
            if(Browser.ie6) { window.addEvent("scroll", function(){
                var sS = document.body.getScroll();
                this.bgDiv.setStyles({ 'top':sS.y });
            }.bind(this)); }

            window.addEvent("resize", function(){ this.resize(); }.bind(this));            
            
            if(this.options.fade.tween) {
                this.bgEl.set('tween', { duration: this.options.fade.duration });
                this.bgEl.tween('opacity', 1);
            } else {
                this.bgEl.setStyle('opacity',1);
            }
            
            this.resize();
        }
    },
    windowSize: {},
    layerSize: {},
    bgElSizeO: {},
    initialize: function(options){
        this.setOptions(options);
        document.id(this.options.contentLayer).setStyles({ 'position':'absolute', 'top': 0, 'left': 0, 'z-index': 2, 'width': '100%' });
        if(Browser.ie6) { document.body.setStyles({ overflow:'auto'}); }
        
        this.bgDiv = new Element('div', {
            styles: { 'overflow': 'hidden', 'position': 'relative', 'top': 0, 'left': 0, 'z-index': 0, 'display':'block', 'width': '100%' }
        });
        this.bgDivOver = new Element('div', {
            styles: { 'width': '100%', 'overflow': 'hidden', 'position': 'absolute', 'top': 0, 'left': 0, 'z-index': 1, 'display':'block', 'background':'url('+this.options.overlayImg+') top left repeat transparent'}
        });

        this.bgDiv.inject(document.id(this.options.printLayer));
        this.bgDivOver.inject(this.bgDiv,'after');
        switch(this.options.el.type) {
            case 'img':
                this.setupImg();
            break;
            case 'iframe':
                this.setupIframe();
            break;
            case 'video':
                this.setupVideo();
            break;
        }
    },
    setupImg: function(){
        this.img = new Image();
        this.img.onload = function() {
            this.bgElSizeO.x = this.img.width;
            this.bgElSizeO.y = this.img.height;
            this.bgEl = new Element('img', {
                src: this.options.el.src,
                styles: { display: 'block', position: 'fixed', 'z-index':0, opacity: 0 }
            });
            this.bgDiv.adopt(this.bgEl);
            this.setAnker();
            this.fireEvent('ready');
        }.bind(this);
        this.img.src = this.options.el.src;
    },
    setupIframe: function(){
        this.bgEl = new IFrame({
            src: this.options.el.src,
            frameborder: 0,
            styles: { width: this.options.el.size.x, height: this.options.el.size.y, position: 'fixed', 'z-index': 0 },
            events: {
                load: function(){
                    this.bgElSizeO.x = this.options.el.size.x;
                    this.bgElSizeO.y = this.options.el.size.y;
                    this.setAnker();
                    this.fireEvent('ready');
                }.bind(this)
            }
        });
        this.bgDiv.adopt(this.bgEl);
    },
    setupVideo: function(){
        this.bgEl = new Element('video', {
            src: this.options.el.src,
            loop: true,
            preload: true,
            autoplay: true,
            styles: { width: this.options.el.size.x, height: this.options.el.size.y, position: 'fixed', 'z-index': 0 }
        });
        this.bgDiv.adopt(this.bgEl);
        this.bgElSizeO.x = this.options.el.size.x;
        this.bgElSizeO.y = this.options.el.size.y;
        this.setAnker();
        this.fireEvent('ready');
    },
    setAnker: function(){
        switch(this.options.position.x) {
            case 'left':
                this.bgEl.setStyle('left',0);
            break;
            case 'right':
                this.bgEl.setStyle('right',0);
            break;
            case 'center':
                this.bgEl.setStyle('left',0);
            break;
        }
        switch(this.options.position.y) {
            case 'top':
                this.bgEl.setStyle('top',0);
            break;
            case 'bottom':
                this.bgEl.setStyle('bottom',0);
            break;
            case 'center':
                this.bgEl.setStyle('top',0);
            break;
        }
    },
    resize: function(){
        this.windowSize = window.getSize();
        this.bgDiv.setStyles({ 'height':this.windowSize.y });
        this.bgDivOver.setStyles({ 'height': this.windowSize.y });
        
        this.windowScrollSize = window.getScrollSize();

        this.bgDiv.setStyles({ 'height':this.windowScrollSize.y });
        this.bgDivOver.setStyles({ 'height': this.windowScrollSize.y });


        if(this.bgElSizeO.y>this.windowSize.y && this.bgElSizeO.x>this.windowScrollSize.x) {
            this.bgEl.setStyles({ 'height':this.bgElSizeO.y, 'width':this.bgElSizeO.x });
        } else {
            var vX = this.windowScrollSize.x/this.bgElSizeO.x;
            var vY = this.windowSize.y/this.bgElSizeO.y;
            
            if(vX >= vY){
                var ElSize = (vX*this.bgElSizeO.y).round(0);
                this.bgEl.setStyles({ 'height':ElSize, 'width':this.windowScrollSize.x });
            } else {
                var ElSize = (vY*this.bgElSizeO.x).round(0);
                this.bgEl.setStyles({ 'height':this.windowSize.y, 'width':ElSize });
            }
        }

        if(this.options.position.x=='center' || this.options.position.y=='center') {
            this.bgElSize = this.bgEl.getSize();
            if(this.options.position.x=='center'){
                var left = (this.windowScrollSize.x-this.bgElSize.x)/2;
                this.bgEl.setStyles({ 'left':left });
            }
            if(this.options.position.y=='center'){
                var top = (this.windowSize.y-this.bgElSize.y)/2;
                this.bgEl.setStyles({ 'top':top });
            }
            var top = (this.windowSize.y-this.bgElSize.y)/2;
            //console.log(this.bgElSize.x,this.windowScrollSize.x,left,'||',this.bgElSize.y,this.windowSize.y,top,'||',this.bgElSizeO.x,this.bgElSizeO.y);
        }
    }
});
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
