﻿(function($j){

	Blind = {
		
		windowHeight: null,
		windowWidth: null,
		resize: false,
		blindClosed: false,
		
		blindOffset: 40,
		blindClosePos: null,
		
		blindHeight: null,
		closeHeight: null,
		
		blind: 'blind',
		blindControl: 'blind-control',
		closeBtn: 'close',
		openBtn: 'open',
		
		init: function(){
			//listen for when window dimensions have been set
			$j(this).bind('dimensionsSet', $j.proxy(this, 'addBlind'));
			$j('.' + this.blindControl, '.' + this.blind).bind('click', $j.proxy(this, 'toggleBlind'));
			//get window dimensions
			this.windowDimensions = this.getDimensions();
			
			$j(window).bind('resize', $j.proxy(this, 'resizeBlind'));
		},
		
		resizeBlind: function(evt){
			this.getDimensions('resize');
			if(this.blindClosed){
				var topPos = Math.abs(parseInt($j('.' + this.blind).css('top')));
				var finalPos = this.blindHeight - this.closeHeight;
				$j('.' + this.blind).css({top: '-' + this.blindClosePos + 'px'})
			}
			
		},
		
		getDimensions: function(evt){
			this.windowHeight = $j(window).height();
			this.windowWidth = $j(window).width();
			
			if(evt){
				this.resize = true;
			}
			
			$j(this).trigger('dimensionsSet');
		},
		
		addBlind: function(){
			//set blindHeight
			this.blindHeight = this.windowHeight - this.blindOffset;
			this.setBlindStartPos();
			this.getBlindClosePos();
		},
		
		getBlindClosePos: function(){
			this.closeHeight = $j('.' + this.blindControl).height();
			this.blindClosePos = (this.blindHeight - this.closeHeight) + 15;
		},
		
		setBlindStartPos: function(){
			$j('.' + this.blind).css({height: this.blindHeight + 'px'});
			if(!this.resize){
				$j('.' + this.blind).css({top: '-' + this.blindHeight + 'px'});
				this.openBlind();
			}
		},
		
		openBlind: function(){
			//add CSS3 to blind
			$j('.' + this.blind).animate({
				top: 0 + 'px',
				leaveTransforms: true
			}, 1000);
			this.blindClosed = false;
		},
		
		closeBlind: function(){
			
			$j('.' + this.blind).animate({
				top: '-' + this.blindClosePos + 'px',
				leaveTransforms: true
			});
			this.blindClosed = true;
			
		},
		
		toggleBlind: function(evt){
			if($j('.' + this.blindControl).hasClass(this.closeBtn)){
				$j('.' + this.blindControl).removeClass(this.closeBtn).addClass(this.openBtn);
				
				$j('.' + this.blindControl).html('Open');
				this.closeBlind()
			}else if($j('.' + this.blindControl).hasClass(this.openBtn)){
				$j('.' + this.blindControl).removeClass(this.openBtn).addClass(this.closeBtn);
				$j('.' + this.blindControl).html('Close to view current site');
				this.openBlind();
			}
			
		}
		
		
	};

})(jQuery);	




