/* Theme Pack 3.1.1+ DO NOT modify */


/*** Panel ***/
CS.v3.Panel = Class.create(CS.v3.Base.prototype, {
	initialize: function(target) {
		var panel = CS.v3.getParentElementWithClass(target,'.csPanel');
		var heading = CS.v3.getParentElementWithClass(target,'.csPanelHeading');
		var content = heading.next('.csPanelContent') || heading.previous('.csPanelContent');
		panel.addClassName('csBlind');
		
		//effect type
		new CS.v3.ToggleDisplayEffect(target,panel,content,panel,'csPanelHide')
	}
});


/*** Overlay Panel ***/
CS.v3.Components.PanelPop = function(el,options) {new CS.v3.Config.PanelPop(el,options)};
CS.v3.Config.PanelPop = Class.create(CS.v3.Base.prototype, {
	initialize: function(el,options) {
	
		this.pops = $(el).select("div[class*='csOverlayContainer csHide']");
		this.bind();
	},
	bind: function() {
		this.pops.each(function(pop) {
			var dragHandle = CS.v3.down(pop,'> .csPanelHeading');
			this.resizeHandle = CS.v3.down(pop,'.csResizeHandle');
			new Draggable(pop, {handle: dragHandle});
			if (this.resizeHandle) {
				this.panel = CS.v3.down(pop,'.csPanel');
				this.contentScroll = CS.v3.down(pop,'.csPanelScroll');
				this.content = CS.v3.down(pop,'.csPanelContent');

				new Resizable(pop,{handle:this.resizeHandle});
				//new Resizable(this.contentScroll,{handle:this.resizeHandle});
				new Resizable(this.content,{handle:this.resizeHandle});
				//new Resizable(this.panel,{handle:this.resizeHandle});
				new Resizable(this.content,{constraint:'vertical', handle:this.resizeHandle});
			}
		}.bind(this));
	}
});


CS.v3.getToggleAttributes = Class.create(CS.v3.Base.prototype, {
	initialize: function(target,event,attr) {
		
		this.target = target
		
		// Origin of overlay launch, values are target or window
		this.overlayOrigin = target.readAttribute('data-csOverlayOrigin');
		
		// heading
		this.overlayHeadingContent = target.readAttribute('data-csOverlayHeading');
		
		// Lightbox effect, value is true
		this.lightbox = target.readAttribute('data-cslightbox');
		
		// set width on panel
		this.originalWidth = target.readAttribute('data-csSetPanelWidth');
		this.ContentOrigin = new CS.v3.updateContent(target);
		
		// set width on panel
		this.offsetY = 0;
		this.offsetX = 0;
		
		this.getOffsetY = target.readAttribute('data-csOverlayOriginYOffset');
		this.getOffsetX = target.readAttribute('data-csOverlayOriginXOffset');
		
		if (this.getOffsetY) {this.offsetY = this.getOffsetY;
		
			this.offsetY = parseInt(this.offsetY);
		}
		if (this.getOffsetY) {
		
			this.offsetX = this.getOffsetX;
			this.offsetX = parseInt(this.offsetX);
		
		}
		
		if (target.hasClassName('csOpacity')) {this.effect = 'opacity';}
		if (!target.hasClassName('csOpacity')) {this.effect = 'scaleMove';}
		
		this._getEndPosition(event);
	},
	_getEndPosition: function(event) {
	
		this.y = null;
		this.x = null;
		
		if (this.overlayOrigin == 'window') {
		
			var scrollYOffset = null;
			
			if (Prototype.Browser.IE) {
				
				var scrollYOffset = document.documentElement.scrollTop;
				var viewWidth = document.body.clientWidth;
			} else {
				
				var scrollYOffset = window.scrollY;
				var viewWidth = window.innerWidth;
			}
			
			this.y = 120 + scrollYOffset;
			this.x = (viewWidth - this.originalWidth) / 2 ;
		
			this.originX = 0; 
			this.originY = 0;
			this.mode = 'absolute';
		}
		
		if (this.overlayOrigin == 'target') {
		
			if (event.type == 'click' || event.type == 'mouse') {
				
				this.x = 0; 
				this.y = 0;
			}
			
			if (event.type == 'keydown') {this.x = 200; this.y = 200;}
			
			this.originX = event.pointerX() + (this.offsetX); 
			this.originY =  event.pointerY() + (this.offsetY);
			
			this.mode = 'relative';
		}
	}
});


CS.v3.getPanelAttributes = Class.create(CS.v3.Base.prototype, {
	initialize: function(t,attr) {
		
		this.pop = attr.toggleContent[0];
		
		this.toggle = CS.v3.down(this.pop,'.csPanelToggle');
		this.content = CS.v3.down(this.pop,'.csPanelScroll');
		this.overlayHeading = CS.v3.down(this.pop,'.csPanelOverlayHeading');
		
		this.fader = $('csFader');
	}
});


CS.v3.PanelOverlay = Class.create(CS.v3.Base.prototype, {
	initialize: function(target,event,attr) {
		//this.log('initialize:init');
		var t = new CS.v3.getToggleAttributes(target,event);
		var p = new CS.v3.getPanelAttributes(t,attr);
		
		//var body = $$('body');
		//body[0].style.overflow = 'hidden';
		
		// add active state to target
		t.target.addClassName('csPopToggleActive');
		
		// IE 6 Cover
		if (CS.v3.Base.prototype.coverIE) {CS.v3.Base.prototype.coverIE(p.pop);}
		
		// Get AJAX Content
		if (t.target.hasClassName('csAjax')) {
		
		
			if (t.overlayHeadingContent) {
			
				p.overlayHeading.update(t.overlayHeadingContent);
			}

			new CS.v3.AjaxNav(t.target,p.content,{bind:false});

		// Get JSON Content
		} else {new CS.v3.getupDatePanelNodes(t,p);}
		
		//Display
		if (t.lightbox == 'true') {p.fader.toggleClassName('csHide');
			
			if (CS.v3.fixed) {CS.v3.fixed['fader']()}
		}
		
		// toggle class
		p.pop.toggleClassName('csHide');
		
		// set dimensions nad position
		p.pop.style.width = t.originalWidth + 'px';
		
		p.pop.style.left = t.originX + 'px'; 
		p.pop.style.top = t.originY + 'px';
		
		// store settings
		p.pop.writeAttribute({csOriginX: t.originX});
		p.pop.writeAttribute({csOriginY: t.originY});
		p.pop.writeAttribute({csOriginalWidth: t.originalWidth});
		p.pop.writeAttribute({csEffect: t.effect})
		
		// get Height Dimension
		this.dimen = p.pop.getDimensions();
		this.originalHeight = this.dimen.height;
		
		if (t.effect == 'scaleMove') {
			this._scaleMove(t,p);
		}
		
		// Opacity Effect
		if (t.effect == 'opacity') {
			this._opacity(t,p);
		}
		//this.log('initialize:exit');
	},
	_scaleMove: function(t,p) {
		
		// Set size
		p.pop.style.height = '5px'; 
		p.pop.style.width = '5px';
		
		//Move
		new Effect.Move(p.pop, {x: t.x, y: t.y, mode: t.mode, duration:.4});
		
		//Scale
		new Effect.Scale(p.pop, 100, {duration: .4, scaleFrom: 5, 
			scaleMode: {originalHeight: this.originalHeight, originalWidth: t.originalWidth}, 
			scaleContent: false, 
			
			beforeStart: function() {
				
				p.pop.style.overflow = 'hidden';
			}.bind(this),
			
			afterFinish: function() {
				p.pop.style.overflow = 'visible';
				p.pop.style.height = 'auto';
				p.toggle.focus();
			}.bind(this)
		});
		
		if (t.lightbox == 'true') {new Effect.Opacity(p.fader, {from: 0.0 , to: .5, duration:0.4});}
	},
	_opacity: function(t,p) {
	
		p.pop.style.left = t.x + 'px'; 
		p.pop.style.top = t.y + 'px';
		
		new Effect.Opacity(p.pop, {from: 0.0 , to: 1.0, duration:0.8,
				beforeStart: function() {
					p.pop.style.opacity = 0;
					p.pop.style.filter = 'alpha(opacity=0)';
				}.bind(this)
			});
		
		if (t.lightbox == 'true') {new Effect.Opacity(p.fader, {from: 0.0 , to: .5, duration:0.4});}
	},
	log: function (s) {
		CS.v3.Logger.log("PanelOverlay:" + s);
	}

});


/*** Get Overlay Content ***/
CS.v3.getupDatePanelNodes = Class.create(CS.v3.Base.prototype, {
	initialize: function(t,p) {
		//this.log('initialize:init');
		// Local Content
		var ar = t.ContentOrigin.InsertableNodes;
		var node;
		var localHeadingTest;
		var localContentTest;
		var localHeading;
		var localContent;
		var obj;
		var i = ar.length;
		//ar.each(function(node) {
		while(i--){
			node = ar[i];
			localHeadingTest = CS.v3.down(node,'.csPanelHeadingLocal');
			localContentTest = CS.v3.down(node,'.csPanelContentLocal');
			if (p.overlayHeading && localHeadingTest) {
				obj = CS.v3.down(node,'.csPanelHeadingLocal');
				localHeading = obj.innerHTML;
				p.overlayHeading.update(localHeading);
			}
			if (p.content && localContentTest) {
				obj = CS.v3.down(node,'.csPanelContentLocal');
				localContent = obj.innerHTML;
				p.content.update(localContent);
				new CS.v3.Config(p.content,{bind:'true', set:'true'});
			}
		}
		//})
		
		// Json - deprecated
		var ar2 = CS.v3.PopContent;
		//this.log('initialize:ar2=' + ar2);
		if (ar2) {
			var obj = ar2[t.targetContent];
			if (obj) {
				
				this.heading = obj.heading || null;
				this.content = obj.content || null;
				
				if (this.heading) {p.toggle.update(this.heading)}
				if (this.content) {p.content.update(this.content)}
			}
		}
		
		new CS.v3.zIndex();
		//this.log('initialize:exit');
	},
	log: function (s) {
		CS.v3.Logger.log("getupDatePanelNodes:" + s);
	}
});


/*** Close Pop Panel ***/

CS.v3.PanelPopClose = Class.create(CS.v3.Base.prototype, {
	initialize: function(el) {
		this.pop = CS.v3.getParentElementWithClass(el,'.csOverlayContainer');
		//var body = $$('body');
		//body[0].style.overflow = 'auto';
		
		// remove active state to target
		var popToggleActive = $$(".csPopToggleActive");
		popToggleActive.each(function(value,index) {
			var obj = popToggleActive[index];
			obj.focus();
			obj.removeClassName('csPopToggleActive');
		});
		
		this.x = this.pop.readAttribute('csOriginX');
		this.y = this.pop.readAttribute('csOriginY');
		this.effect = this.pop.readAttribute('csEffect');
		//this.log('initialize:effect=' + this.effect);
		this.originalHeight = this.pop.getHeight(); 
		this.originalWidth = this.pop.readAttribute('csOriginalWidth');
		
		this.fader = $('csFader');
		
		if (this.effect == 'scaleMove') {this._scaleMove();}
		if (this.effect == 'opacity') {this._opacity();}
	},
	_scaleMove: function() {
		
		if (!this.fader.hasClassName('csHide')) {new Effect.Opacity(this.fader, {from: .5 , to: .0})}
		
		new Effect.Move(this.pop, {x: this.x, y: this.y, mode: 'absolute', duration:.4,
		
			afterFinish: function() {
				if (!this.fader.hasClassName('csHide')) {
					this.fader.toggleClassName('csHide')
				}
				this.pop.addClassName('csHide');
			}.bind(this)
		});
		
		//Scale
		new Effect.Scale(this.pop, 5, {duration: .4, scaleFrom: 100, scaleMode: {originalHeight: this.originalHeight, 
			originalWidth: this.originalWidth}, scaleContent: false,
			
			beforeStart: function() {
				this.pop.style.overflow = 'hidden';
			}.bind(this),
			
			afterFinish: function() {this.pop.style.height = 'auto'}.bind(this)
		});
	},
	_opacity: function() {
		
		new Effect.Opacity(this.pop, {from: 1.0 , to: 0.0, duration:0.8,
			afterFinish: function() {
					if (!this.fader.hasClassName('csHide')) {this.fader.addClassName('csHide')}
					this.pop.addClassName('csHide');
					this.pop.style.opacity = 100;
					this.pop.style.filter = 'alpha(opacity=100)';
			}.bind(this)
		});
	},
	log: function (s) {
		CS.v3.Logger.log("PanelPopClose:" + s);
	}
});


CS.v3.PopContent = {
	content1: {
		heading:'Heading 1',
		content:'<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do' + 
		'eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>'
	}
}



/*** Confirmation Panel ***/
CS.v3.Components.Confirmation = function(el,options) {new CS.v3.Config.Confirmation(el,options); };
CS.v3.Config.Confirmation = Class.create(CS.v3.Base.prototype, {
	initialize: function(el,options){
		this.allSourceButtons = $(el).select("input[class*='csConfirmationButton']","button[class*='csConfirmationButton']");
		this.allTargetButtons = $(el).select("input[class*='csConfirmationSubmit']","button[class*='csConfirmationSubmit']");
		this.el=el;
		//this.bind();
		if (options.bind == 'true') {this.bind(); }
	},
	bind: function(){
		var el = this.el;
		var tag = CS.v3.I18N.getTagName(el);
		if(tag=='BODY'){
			this.allSourceButtons = $(el).select("input[class*='csConfirmationButton']","button[class*='csConfirmationButton']");
			this.allTargetButtons = $(el).select("input[class*='csConfirmationSubmit']","button[class*='csConfirmationSubmit']");
			var btnid = this.bindSource();
			this.bindTarget(btnid);
		}
	},
	bindSource: function(){
		var rc = '';
		try{
			var ar = this.allSourceButtons;
			if(ar){
				var i = ar.length;
				var button;
				var str;
				while(i--){
					button = ar[i];
					str = CS.v3.Confirmation.setupSourceButton(button);
					var b = CS.v3.I18N.isEmpty(str);
					if(!b){
						rc = str;
					}
				}
			}
		}
		catch(e){
			this.log('bindSource:e=' + e);
		}
		return rc;
	},
	bindTarget: function(btnid){
		try{
			var ar = this.allTargetButtons;
			if(ar){
				var i = ar.length;
				var button;
				while(i--){
					button = ar[i];
					CS.v3.Confirmation.setupTargetButton(button,btnid);
				}
			}
		}
		catch(e){
			this.log('bindTarget:e=' + e);
		}
	}
});
CS.v3.Confirmation = {
	setupSourceButton: function(el){
		var rc = '';
		// when JS is enabled (e.g. we are in this code, change the 'type=submit' to 'type=button' to prevent accidental submissions!
		try{
			var formEl = CS.v3.getParentElement(el,'FORM');//el.up('FORM');
			if(formEl){
				CS.v3.I18N.updateAttribute(el,'TYPE','button'); // force to use JS! (should they build no-js version);
				var btnid = Element.identify(el);//CS.v3.I18N.assertId(el,'confirmsource');
				this.setCustomAttributeFormSubmit(el,btnid);
				rc = btnid;
				Event.observe(el,'click', function(event){ CS.v3.Confirmation.confirmationSourceButtonClickedEV(event); });
				this.setupSourceForm(formEl);
			}
		}
		catch(e){
			this.log('setupSourceButton:e=' + e);
		}
		return rc;
	},
	setupSourceForm: function(el){
		if(el){
			var ar = this.getSourceControls(el);
			if(ar){
				var i = ar.length;
				var control;
				while(i--){
					control = ar[i];
					this.setupSourceControl(control);
				}
			}
		}
	},
	setupTargetButton: function(el,btnid){
		var myid = Element.identify(el);//CS.v3.I18N.assertId(el,'confirmtarget');
		CS.v3.I18N.updateAttribute(el,'TYPE','button'); // must use JS! (this is in the overlay, and is relocated outside the form in the DOM!)
		this.setCustomAttributeFormSubmit(el, btnid);
		Event.observe(el,'click', function(event){ CS.v3.Confirmation.confirmationTargetButtonClickedEV(event); });
		CS.v3.I18N.updateAttribute(el,'onclick','return CS.v3.Confirmation.confirmationTargetButtonClickedEV(event);');
	},
	getSourceContainer: function(el){
		var body = $$('body');
		var container = body[0];// $('csContainer');
		return container;
	},
	getSourceControls: function(el){
		var container = this.getSourceContainer(el);
		var rc = container.select('.csConfirmationSource');
		return rc;
	},
	getTargets: function(el){
		var container = this.getSourceContainer(el);
		var rc = container.select('DIV.csConfirmationTarget');
		return rc;
	},
	setupSourceControl: function(el){
		if(el){
			Event.observe(el,'change', CS.v3.Confirmation.confirmationControlChangeEV);
			var tag = CS.v3.I18N.getTagName(el);
			if(tag=='INPUT'){
				var type = CS.v3.I18N.getType(el);
				if(type=='RADIO'){
					Event.observe(el,'click', CS.v3.Confirmation.confirmationControlChangeEV);
				}
			}
		}
	},
	confirmationControlChangeEV: function(event){
		var el = $(Event.element(event));
		CS.v3.Confirmation.confirmationUpdate(el);
	},
	confirmationSourceButtonClickedEV: function(event){
		try{
			var buttonEl = $(Event.element(event));
			var ar = this.getSourceControls(el);
			var i = ar.length;
			var el;
			while(i--){
				el = ar[i];
				this.confirmationUpdate(el);
			}
		}
		catch(e){
			this.log('confirmationSourceButtonClickedEV:e=' + e);
		}
	},
	confirmationTargetButtonClickedEV: function(event){
		var rc = false;
		try{
			var el = $(Event.element(event));
			var btnid = this.getCustomAttributeFormSubmit(el);
			var btnEl = $(btnid);
			var jssubmitnam = this.getCustomAttributeSubmitButtonName(btnEl);
			var jssubmitval = this.getCustomAttributeSubmitButtonValue(btnEl);
			var myformEl = CS.v3.getParentElement(btnEl,'FORM');//btnEl.up('FORM');
			if(myformEl){
				CS.v3.I18N.updateAttribute(btnEl,'value',jssubmitval);
				myformEl.insert({bottom:'<input type=\"hidden\" name=\"' + jssubmitnam + '\" value=\"' + jssubmitval + '\" />'});
				myformEl.submit();
			} else {
				CS.v3.I18N.updateAttribute(btnEl,'TYPE','submit'); // put it back to the original state
			}
		}
		catch(e){
			this.log('confirmationTargetButtonClickedEV:e=' + e);
		}
		return rc;
	},
	confirmationUpdate: function(el){
		var sourceIndex = this.getCustomAttributeConfirmationIndex(el);
		var sourceValue = CS.v3.I18N.getValue(el);
		this.updateTargetValue(el, sourceValue, sourceIndex);
	},
	updateTargetValue: function(el, sourceValue, sourceIndex){
		try{
			var arTarget = this.getTargets(el);
			var i = arTarget.length;
			var obj;
			var targetIndex;
			while(i--){
				obj = arTarget[i];
				targetIndex = this.getCustomAttributeConfirmationIndex(obj);
				if(sourceIndex==targetIndex){
					CS.v3.I18N.setValue(obj,sourceValue);
					//break;//NOTE: we could break here, but want to allow for multiple instances of the same target ID (in rare cases)
				}
			}
		}
		catch(e){
			this.log('updateTargetValue:e=' + e);
		}
	},
	getCustomAttributeConfirmationIndex: function(el){
		var def = '';
		var rc = CS.v3.I18N.getDataAttribute(el,'data-csConfirmationIndex',def);// HTML5 
		return rc;
	},
	setCustomAttributeFormSubmit: function(el,val){
		CS.v3.I18N.setDataAttribute(el,'data-csConfirmationFormSubmit',val);// HTML5
	},
	getCustomAttributeFormSubmit: function(el){
		var def = '';
		var rc = CS.v3.I18N.getDataAttribute(el,'data-csConfirmationFormSubmit',def);// HTML5 
		return rc;
	},
	getCustomAttributeSubmitButtonName: function(el){
		var def = el.readAttribute('name');
		var rc = CS.v3.I18N.getDataAttribute(el,'data-csConfirmationSubmitButtonName','');// HTML5 
		return rc;
	},
	getCustomAttributeSubmitButtonValue: function(el){
		var def = CS.v3.I18N.getValue(el);
		var rc = CS.v3.I18N.getDataAttribute(el,'data-csConfirmationSubmitButtonValue','');// HTML5 
		return rc;
	},
	log: function (s) {
		CS.v3.Logger.log("Confirmation:" + s);
	}
};
