//GENERIC POPUPWINDOW CLASS
var GenericPopupWindow = Class.create();
GenericPopupWindow.default_width = 600;
GenericPopupWindow.default_height = 500;
GenericPopupWindow.scrolling = false;
GenericPopupWindow.prototype = {
	initialize: function( el ){
		this.el = el;
		this.winCount = 0;
		this.width = ( this.el.hasAttribute('win_width') ? this.el.attributes['win_width'].value : GenericPopupWindow.default_width );
		this.height = ( this.el.hasAttribute('win_height') ? this.el.attributes['win_height'].value : GenericPopupWindow.default_height );
		this.scrolling = ( this.el.hasAttribute('scrolling') ? this.el.attributes['scrolling'].value : GenericPopupWindow.scrolling );
		Event.observe( this.el, 'click', this.clicked.bindAsEventListener( this ) );
	},
	clicked: function( event ){
		if(this.scrolling == '1'){
			window.open(this.el.href, "win"+this.winCount++, "width="+this.width+",height="+this.height+",resizable=1,scrollbars=1");
		} else {
			window.open(this.el.href, "win"+this.winCount++, "width="+this.width+",height="+this.height+",resizable=1");
		}
		try{
			Event.stop( event );
		}catch( e ){
			return false;
		}
	}
}
//Fancy Login
var fancyLogin = Class.create({
	initialize: function(triggerID,formID) {
		this.trigger = $(triggerID);
		this.formID = $(formID);
		this.initEventHandlers();
	},
	initEventHandlers: function() {
		this.trigger.select('a')[0].observe('click',this.handleToggle.bind(this));
		$('fancyCloseBtn').observe('click',this.handleToggle.bind(this));
	},
	handleToggle: function(e) {
		Event.stop(e);
		Effect.toggle(this.formID, 'appear',{duration:.4});
	}	
});
//Tabs Class
var tabsManager = Class.create({
	initialize: function(element,options) {
		if(element != undefined){
			this.element = $(element);
			this.options = Object.extend({
				tabClass: 'viewTrigger',
				containerClass: 'contentBox',
				activeTabClass: 'activeBtn',
				triggerEvent: 'click' 
			}, options || {});
			this._tabs = this.element.select('.' + this.options.tabClass);
			this._containers = this.element.select('.' + this.options.containerClass);
			if(this._tabs.length > 0 && this._containers.length > 0){
				this.initEventHandlers();
			}
		}
	},
	initEventHandlers: function() {
		this.openFirst();
		this._tabs.each(function(elem){
			elem.setStyle({cursor:'pointer'});
			elem.observe(this.options.triggerEvent,this.handleToggle.bind(this))
		}.bind(this));
	},
	openFirst: function() {
		this._tabs[0].addClassName(this.options.activeTabClass);
		this._containers[0].show();
	},
	openTab: function(num) {
		this.closeAllBoxes();
		this.removeActiveClasses();
		this._tabs[num].addClassName(this.options.activeTabClass);
		this._containers[num].show();
	},
	closeAllBoxes: function() {
		this._containers.each(function(elem){elem.hide();});
	},
	removeActiveClasses: function() {
		this._tabs.each(function(elem){elem.removeClassName( this.options.activeTabClass )}.bind(this));
	},
	handleToggle: function(e) {
		var ele = Event.element(e);
		this.closeAllBoxes();
		this.removeActiveClasses();
		try{
          ele.addClassName(this.options.activeTabClass);
        }catch(e){}
        try{
            $(ele.readAttribute('rel')).show();
        }catch(e){}
	}	
});
var viewSwap = Class.create({
	initialize: function(element) {
		this.element = $(element);
		this._buttons = this.element.select('.viewTrigger');
		this._containers = this.element.select('.contentBox');
		this.initEventHandlers();
	},
	initEventHandlers: function() {
		this._buttons.each(function(elem){
			elem.observe('click',this.handleClick.bind(this));
		}.bind(this));
	},
	handleClick: function(e) {
        alert ('shit is happening');
		this.closeElements();
		var ele = Event.element(e);
		try{
			ele.up('.viewTrigger').addClassName('activeBtn');
		}catch(e){
			//console.log(e)
		}
		try{
			$(ele.up('.viewTrigger').readAttribute('rel')).show();
		}catch(e){
			//console.log(e);
		}
	},
	closeElements: function() {
		this._containers.each(function(elem){
			elem.hide();
		});
		this._buttons.each(function(elem){
			elem.removeClassName('activeBtn');
		});	
	}
});
var checkboxManager = Class.create({
	initialize: function(element) {
		this.element = element;
		this.boxes = this.element.select('input');
		this.allbox = this.element.select('.checkbox_all')[0];
		this.initEventHandlers();
	},
	initEventHandlers: function() {
		this.boxes.each(function(elem){elem.observe('click',this.handleClick.bind(this))}.bind(this));
	},
	handleClick: function(e) {
		var ele = Event.element(e);
		if(ele.hasClassName('checkbox_all')) {
			this.boxes.each(function(elem){elem.checked = false});
			this.allbox.checked = true;
		}else{
			this.allbox.checked = false;
		}
	}
});
var jo_tooltip = Class.create({
	initialize: function(_useclass,options) {
		this.options = Object.extend({
			openDelay:8,
			skinID: 'jo_tooltipCont',
			tipContentID: 'jo_tooltipDynamicContent',
			stemID: 'jo_tooltip_stem',
			tipTopID: 'jo_tooltipTop',
			tipInnerID: 'jo_tooltipInnerContainer',
			tipBottomID: 'jo_tooltipBottom',
			onOpen: Prototype.emptyFunction,
			effect:false,
			onEvent:'mouseover',
			offEvent:'mouseout',
			useStem:false,
			stemHeight: 0,
			stemBuffer: 0,
			offset:{x:0,y:0},
			hook:{tipPosition:'middleRight'},
			stemClasses: ['tl','tr','br','bl']
		}, options || {});
		this.hook = this.options.hook.tipPosition;
		this.timer = null;
		this.tipIDExtended = null;
		this._stemID = null
		this._top = null;
		this._left = null;
		this._dynamicContentID = null;
		this._viewportDimensions = null;
		this._elementViewport = null;
		this._viewportScroll = null;
		this.isOpen = false;
		this.buildTip();
		var tooltip = $$('.' + _useclass);
		tooltip.each(function(elem){
			elem.observe(this.options.onEvent,this.handleOpenEvent.bind(this))
			elem.observe(this.options.offEvent,this.handleCloseEvent.bind(this))
		}.bind(this));
	},
	buildTip: function(){
		var lw = document.createElement('div');
		var bodyElement = document.getElementsByTagName('body')[0];
		var skin = 
			'<div id="' + this.options.skinID + '">'
				+	'<div id="' + this.options.tipInnerID + '">'
					+	'<div id="' + this.options.tipTopID + '">&nbsp;</div>'
					+ 	'<div id="' + this.options.tipContentID + '">&nbsp;</div>'
					+	'<div id="' + this.options.tipBottomID + '">&nbsp;</div>'
		if(this.options.useStem == true){
			skin += '<div id="' + this.options.stemID + '" class="' + this.options.stemClasses[3] + '">&nbsp;</div>';
		}
		skin += 	'</div>'	
			+'</div>';
		lw.innerHTML = skin;
		bodyElement.appendChild(lw);
		this.tipIDExtended = $(this.options.skinID);
		this._dynamicContentID = $(this.options.tipContentID);
		if(this.options.useStem == true){
			this._stemID = $(this.options.stemID);
		}
		try{
			bodyElement.observe('click',this.handleCloseEvent.bind(this));
			bodyElement.observe('mouseover',this.handleCloseEvent.bind(this));
		}catch(e){
			document.body.onclick = function(e){
				this.handleCloseEvent(e);
			}.bind(this);
			document.body.mouseover = function(e){
				this.handleCloseEvent(e);
			}.bind(this);
		}
	},
	makeCalculations: function(tip,target){
		var _max_y = this._viewportDimensions.height + this._viewportScroll.top;
		var _max_x = this._viewportDimensions.width + this._viewportScroll.left;
		this.stemClassReset();
		var bumpup = false;
		var bumpside = false;
		var oldheight = null;
		var stemTop = null;
		switch(this.hook){
			case 'topLeft': 
				this._top = this.options.offset.y + ( ( -1 * ( tip.height - target.height ) ) + this._elementViewport.top + this._viewportScroll.top );
				this._left = this.options.offset.x + (-1 * tip.width) + this._elementViewport.left + this._viewportScroll.left;
				if( this._left < 0){
					this._left = this.options.offset.x + (target.width) + this._elementViewport.left + this._viewportScroll.left;
				}
				break;
			case 'topRight':
				this._top = this.options.offset.y + ( ( -1 * ( tip.height - target.height ) ) + this._elementViewport.top + this._viewportScroll.top );
				this._left = this.options.offset.x + target.width + this._elementViewport.left + this._viewportScroll.left;
				if( ( this._left + tip.width ) > _max_x){
					this._left = this._elementViewport.left + this._viewportScroll.left + ( -1 * tip.width ) - this.options.offset.x;
				}
				break;
			case 'bottomLeft':
				this._top = this.options.offset.y + target.height + this._elementViewport.top + this._viewportScroll.top;
				this._left = this.options.offset.x + (-1 * tip.width) + this._elementViewport.left + this._viewportScroll.left;
				if( this._left < 0){
					this._left = this.options.offset.x + (target.width) + this._elementViewport.left + this._viewportScroll.left;
				}
				if( ( this._top + tip.height ) > _max_y){
					this._top = _max_y - tip.height;
				}
				break;
			case 'bottomRight':
				this._top = this.options.offset.y + (-1 * target.height) + this._elementViewport.top + this._viewportScroll.top;
				this._left = this.options.offset.x + target.width + this._elementViewport.left + this._viewportScroll.left;
				if( ( this._left + tip.width ) > _max_x){
					this._left = this._elementViewport.left + this._viewportScroll.left + ( -1 * tip.width ) - this.options.offset.x;
				}
				if( ( this._top + tip.height ) > _max_y){
					this._top = _max_y - tip.height;
				}
				break;
			case 'middleRight':
				this._top = this.options.offset.y + ( target.height + ( -1 * ( tip.height / 2 ) ) + this._elementViewport.top + this._viewportScroll.top );
				this._left = this.options.offset.x + target.width + this._elementViewport.left + this._viewportScroll.left;
				if( ( this._top + tip.height ) > _max_y){
					oldheight = this._top;
					this._top = _max_y - tip.height;
					bumpup = true;
				}
				if( ( this._left + tip.width ) > _max_x){
					this._left = this._elementViewport.left + this._viewportScroll.left + ( -1 * tip.width ) - this.options.offset.x + 14;
					bumpside = true;
				}
				if(this.options.useStem == true){
					if(bumpup == true && bumpside == false){
						stemTop = (tip.height / 2) + (oldheight - this._top) - (this.options.stemHeight / 2);
						if(stemTop > tip.height - this.options.stemBuffer - (this.options.stemHeight / 2)){
							stemTop = tip.height - this.options.stemBuffer - (this.options.stemHeight / 2);
						}
						this.positionStem( stemTop , 3);
					}else if(bumpup == false && bumpside == true){
						this.positionStem((tip.height / 2) - (this.options.stemHeight / 2) , 2);
					}else if(bumpup ==true && bumpside == true){
						stemTop = (tip.height / 2) + (oldheight - this._top) - (this.options.stemHeight / 2);
						if(stemTop > tip.height - this.options.stemBuffer - (this.options.stemHeight / 2)){
							stemTop = tip.height - this.options.stemBuffer - (this.options.stemHeight / 2);
						}
						this.positionStem(stemTop , 2);
					}else{
						this.positionStem((tip.height / 2) - (this.options.stemHeight / 2) , 3);
					}
				}
				break;
			case 'middleLeft':
				this._top = this.options.offset.y + ( target.height + ( -1 * ( tip.height / 2 ) ) + this._elementViewport.top + this._viewportScroll.top );
				this._left = this.options.offset.x + (-1 * tip.width) + this._elementViewport.left + this._viewportScroll.left;
				if( ( this._top + tip.height ) > _max_y){
					this._top = _max_y - tip.height;
				}
				if( this._left < 0){
					this._left = this.options.offset.x + (target.width) + this._elementViewport.left + this._viewportScroll.left;
				}
				break;
		}
	},
	stemClassReset: function(){
		if(this.options.useStem == true){
			this.options.stemClasses.each(function(elem){
				this._stemID.removeClassName(elem);
			}.bind(this));
		}
		return;
	},
	positionStem: function(theY,theclass){
		this._stemID.addClassName(this.options.stemClasses[theclass]);
		this._stemID.setStyle({
			top: theY + 'px'
		});
		return;
	},
	getWindow: function(element){
		this._viewportDimensions = document.viewport.getDimensions();
		this._elementViewport = element.viewportOffset();
		this._viewportScroll = document.viewport.getScrollOffsets();		
	},
	handleOpenEvent: function(e){
		var elem = Event.element(e);
		if(this.options.onEvent == 'click'){
			Event.stop(e);
		}
		if(this.timer != null){
			clearTimeout(this.timer);
			this.timer = null;
		}
		this.timer = setTimeout(function(){
			this.timer = null;
			if(this.isOpen == false){
				this.isOpen = true;
				var tipDim = this.tipIDExtended.getDimensions();
				var triggerDim = elem.getDimensions();			
				this.getWindow(elem);		
				this.makeCalculations(tipDim,triggerDim);
				this.tipIDExtended.setStyle({
					top:this._top + 'px',
					left:this._left + 'px',
					display:'block'
				});
				this.options.onOpen(elem,this._dynamicContentID);		
			}
		}.bind(this),800);
	},
	handleCloseEvent: function(e){
		if(this.options.offEvent == 'click'){
			Event.stop(e);
		}
		if(this.isOpen == true || this.tipIDExtended.visible()){
			this.isOpen = false;
			this.tipIDExtended.hide();
		}
	}
});

Event.observe(window, 'load', function() {
	$$( "a.PopupWin" ).each(function(el){new GenericPopupWindow(el);});
});