var Surfacia = {};
Surfacia.WindowGridManager = new Class({
    options: {
        snapToGrid: false
    },
    presets: {
        CONTAINER_ID: 'container'
    },
    initialize: function(options) {
        //this.setOptions(options);
        window.addEvent('domready', this.doOnLoad.bind(this));
    },
    doOnLoad: function() {
        this.resizeNode = $(this.presets.CONTAINER_ID);
        widthFX = new Fx.Tween(this.resizeNode, {
        	onStart: function(){
        	  	if($('thumbnails') && (thumbSlide).open){
             		thumbMorph.start({
             			'opacity': 0	
             		});
				}									
			},
			link: 'ignore',
			property: 'width',
            duration: 500,
            transition: Fx.Transitions.Quart.easeOut,
            onComplete: function(){
             	if($('thumbnails') && (thumbSlide).open){
             		thumbMorph.start({
             			'opacity': 1	
             		});
				}
			
				if($('teaser')){	
					//alert(parseInt($('teaser').getStyle('width')));
					$('sm').setStyle('width',parseInt($('teaser').getStyle('width')));
					//alert(parseInt($('sm').getStyle('width')));
					expandSize = parseInt($('sm').getStyle('width'))-165-165-1;
					//alert(expandSize);
					slideMenu('sm',2);
				}
			}
        });
        
        widthSearchFX = new Fx.Morph('search', {
			link: 'ignore',
            duration: 500,
            transition: Fx.Transitions.Quart.easeOut
        });
		//this.resizeTimer = new Surfacia.Timer(0);
	    //this.resizeTimer.addEvent('onExpire', this.doGridResize.bind(this));
	    //window.addEvent('resize', this.resizeTimer.restart.bind(this.resizeTimer));
	    window.addEvent('resize',this.doGridResize.bind(this));
	    
	    this.doGridResize();
    },
    getGridWidth: function(state) {
        var offset = 0;
        var bw = window.getWidth();
        //alert(bw);
        bw = bw - offset;
        var gw = 0;
        
        if (bw < 980) {
            gw = 980;
        } else if (bw > 1280) {
            gw = 1280;
        } else {
            gw = bw;
        }
        
        
        if(state=='full'){
        	if(bw>gw){                               
        		return bw;
			}
         	return gw;	
		}
        
        //if($('portfolio_highlight')){
        	createCookie('container_width',(Math.floor(gw/70)*70)-70,1);
        	return (Math.floor(gw/70)*70)-70;
		/*} else {
			createCookie('container_width',gw-40,1);
			return gw-40;
		}   */
    },
    doGridResize: function(resize) {		
    	proceed = false;
        if(this.getGridWidth('resize')>0){
            var gw = this.getGridWidth('resize');
            //Conditions of resize, fixes IE multiple resize fires - DO NOT REMOVE
            if($('container').getStyle('width') != gw+'px' || resize==true){
            	proceed = true;
			}
			if(!$('thumbnails')){
			 	proceed = true;
			}

			if(proceed){
	            var full = this.getGridWidth('full');
	            widthFX.stop;
	            widthSearchFX.stop;
	            if(gw){
            		widthFX.start(gw);
				}
			}
        }
    },
    doInitPageLoad: function() {
        //document.write('<style type="text/css"> #' + this.presets.CONTAINER_ID + ' { width: ' + this.getGridWidth('init') + 'px;}</style>');
    }
});