jQuery.noConflict();

if (typeof  cfeu_corp == 'undefined' || !cfeu_corp  ){ var cfeu_corp = {}; };
if(typeof console == 'undefined'){ console = { log : function(){} } };
/*
 * 
 */
(function(jQuery) {
	jQuery.fn.extend({
				isChildOf : function(filter_string) {
					var parents = jQuery(this).parents().get();
					for (j = 0; j < parents.length; j++) {
						if (jQuery(parents[j]).is(filter_string)) {
							return true;
						}
					}
					return false;
				}
			});
})(jQuery);
/*
 * 
 */
cfeu_corp.merge = function(){
	var o=arguments[0], a = arguments,p;
	for(var i=1, l=a.length; i<l; i++){
		for(p in a[i]){
			o[p] = a[i][p];
		}
	}
	return o;
};

cfeu_corp.initialize = function(o, selector, options){
	var debug = false;
	jQuery(selector).each(
		function(){
			if(debug){ console.log("initialize");console.log(this); }
			var c = new o( this, options );
			if (typeof c.controllerName != 'undefined'){ this[c.controllerName] = c; }
		}
	);
}

;cfeu_corp.FloatContainer = function(options){ 
	this.options = {
		left:0,
		top:0,
		iframe:false
	};
	jQuery.extend(this.options, options);
	this.create();
}

cfeu_corp.FloatContainer.prototype = {
	create:function(){
		this.box = jQuery('<div />').addClass('cf_container');
		this.body = jQuery('<div />').addClass('cf_container_body');
		this.box.append(this.body);
	},
	add: function(content) {
		this.body.append(content);
	},
	display: function() {
		if(!this.box) return;
		
		this.box.attr('style', 'visibility: hidden');
		jQuery(document.body).append(this.box);
		
		var x = this.options.left;
		var y = this.options.top - jQuery(this.box).outerHeight();
		
		if(jQuery.browser.msie) {
			this.box.attr('style', 'visibility: visible');
			this.box.css('position','absolute');
			this.box.css('left', x +'px');
			this.box.css('top', y + 'px');
			this.box.css('z-index', '1000');
		} else {
			this.box.attr('style','position:absolute;left:'+x+'px;top:'+y+'px;z-index:1000'); //Use setProperty for Opera
		}

		if (jQuery.browser.msie && (jQuery.browser.version == 6) || this.options.iframe) {
			
			var p = this.box.position();
			var h = this.box.height() + 'px';
			var l = p.left + 'px';
			var w = this.box.width() + 'px';
			var t = y + 'px'; // x ???
			this.iframe = jQuery('<iframe />').css( {height: h, left: l, position: 'absolute', top: t, width: w, zIndex: 998, border:'none'} );
			if(jQuery.browser.msie) {
				this.iframe.css( {filter : 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)' });
			}
			jQuery(document.body).append(this.iframe);
		}
		
		jQuery(document).bind("mousedown", this ,this._remove );
	},
	
	_remove: function(event) {
		var target = jQuery(event.target);
		var clickOutsideContainer = target.closest(".cf_container").length == 0;
		if(typeof arguments[0].data == "object" && clickOutsideContainer ){
			var container = arguments[0].data;
			container.remove(container);
		}
	},
	
	remove: function(container) {
		if(typeof container.box == "object") {
			container.box.css('display','none'); // for Opera
			container.box.remove();
 			if(container.iframe){ container.iframe.remove(); }
			jQuery(document).unbind('mousedown', container._remove);
		}
	}
};

cfeu_corp.Overlayer = function(elm,options){
	this.elm = elm;
	this.init(options);
}

cfeu_corp.Overlayer.prototype = {
	options : {},		
	init: function(options){
		cfeu_corp.merge(this.options, options);
		jQuery(this.elm).bind("click", this, this._clickHandler);
		jQuery('#overlayBase ' + this.options.overlayer + ' .close').click(this._hide);
		jQuery('#overlayBase ' + this.options.overlayer + ' .popupFooter .nav a').click(this._hide);
		if(typeof this.options.closeKey != 'undefined'){
			jQuery(document).bind("keypress",this,  
				function(e){ 
					 var code = (e.keyCode ? e.keyCode : e.which);
					 var o = e.data;
					 if(code == o.options.closeKey) { 
						 o._hide(e);
					 }
				} 
			);
		}
	},
	_clickHandler: function(e){
		e.preventDefault();
		var o = e.data;
		o._displayBlock(o.options.overlayer);
	},
	_displayBlock: function(blockClass){
		jQuery("html").addClass("jsPop");
		jQuery('#overlayBase').css('display','block');
		var arrChildren = jQuery('#overlayBase').children();
		arrChildren.filter(blockClass).css('display','block').focus();
		arrChildren.filter("#overlayBG").css('display','block');
	},
	_hide: function(e){
		e.preventDefault();
		jQuery('#overlayBase').css('display','none');
		var arrChildren = jQuery('#overlayBase').children();
		jQuery(arrChildren).each(function(){
			jQuery(this).css('display','none');
		});
		jQuery("html").removeClass("jsPop");		
	}
};
cfeu_corp.Carousel = function(elm, options){
	this.controllerName = "_carousel";
	this.elm = jQuery(elm);
	this.options = jQuery.extend(
	{
		animate: false,
		animateRunning: false,
		autoAnimate: false, 
		duration: 4, 
		frequency: 4,
		activeNav:false // set nth nav active in nav, not used in prev/next nav
	}, options );
	this.init();
};
cfeu_corp.Carousel.prototype = {
	init: function(){
	 	this.autoAnimateRunning = false;
	    this.sections = this.elm.find('.carContent ol li').each( function(index, section) {	section._index = index; });
		this.ctrlNav = this.elm.find('.carNav ul li').each( function(index, section) {	section._index = index; });
		
		var a = this.ctrlNav.find('a');
		for ( var i = 0; i < a.length; i++) {
			jQuery(a[i]).bind("click", this, this.pager );
		}
		
		if(this.options.activeNav){
			var activeNav = this.ctrlNav.filter(".active");
			//
			// if there is an active in nav, match nth item in sections (second nav is active but first section is visible on init)
			//
			if (activeNav.length == 0) {
				this.ctrlNav.eq(0).addClass("active");
				
				this.sections.eq(index).show();
				this.sections.not(":first").hide();

				this.sections.children(":first").find('a').children().css('z-index','2');
				this.sections.children(":first").css('z-index','2');
				
			} else {
				
				var index = activeNav.get(0)._index;
				this.sections.eq(index).show();
				this.sections.not(":eq(" + index + ")").hide();
				
				this.sections.hide();
				this.sections.filter(":eq(" + index + ")").show();

				this.sections.children(":eq(" + index + ")").children().css('z-index','3');
				this.sections.children(":eq(" + index + ")").css('z-index','3');
				
				this.sections.eq(index).addClass("active");
				//this.sections.not(":eq(" + index + ")").removeClass("first");
			}
			this.ctrlNav.hover( function(){jQuery(this).addClass("onHover");} , function(){jQuery(this).removeClass("onHover");} );
		} else {
			var index = this.sections.filter(":first").eq(0).attr("_index");
			this.sections.eq(index).addClass("active");
			this.sections.not(":eq(" + index + ")").hide();	
		}
		if(this.options.autoAnimate) {
			// this.elm.hover(this.stop,this.start);
			this.elm.find(".carContent").hover(this.stop,this.start);
			this.start();
		}	
	},
	/**
	 * 
	 */
	 pager: function(e){
		e.preventDefault();
		var o = e.data;
		if(o.autoAnimateRunning){o.stop()};
		o._pager(jQuery(e.target).closest("li").get(0)._index);
	},
	/**
	 * 
	 */
	_pager: function(index){
		var oldIndex = this.ctrlNav.filter(".active").get(0)._index;
		
		 // navigation present ?
		 if(this.options.activeNav){
			 var an = this.ctrlNav.eq(index);
			 var ao = this.ctrlNav.filter(".active");
			 if(ao.get(0)._index == index) {return;}
		 }
		 // animation callback function
		 var c = this;
		 var fn = function(){
			 // navigation present ?
			 // current or old index
			 var cIndex = (c.options.activeNav) ? ao.get(0)._index : c.sections.filter(".active").eq(0).attr("_index"); 
			 if (c.options.activeNav){
				 c.sections.eq(cIndex).find('a').children().hide();
				 c.sections.eq(cIndex).hide();				 
				c.sections.eq(cIndex).find('a').children().css('z-index','4');
			 	c.sections.eq(cIndex).css('z-index','1');
			 	c.sections.eq(index).find('a').children().css('z-index','8');
				c.sections.eq(index).css('z-index','8');
			 } else {
				 c.sections.eq(index).find('a').children().show();
				 c.sections.eq(index).show();
				 c.sections.eq(cIndex).find('a').children().hide();
				 c.sections.eq(cIndex).hide();				 
			 }
			 
			 c.animateRunning = false;
		 };
		 
		 var fs = function(){
			 c.sections.eq(index).find('a').children().show();
			 c.sections.eq(index).show();
			 c.sections.eq(index).find('a').children().css('z-index','1');
			 c.sections.eq(index).css('z-index','1');
		 };
		 // start animation
		 if(this.options.animate){
			 if(!this.animateRunning){
				 this.animateRunning = true;
				 fs();
				 if(c.options.activeNav){
					 ao.removeClass("active");
					 an.addClass("active");
				 }
				 this.sections.eq(ao.get(0)._index).find('a img').fadeOut(this.options.duration * 1000, fn);
				 this.sections.eq(ao.get(0)._index).find('a span').fadeOut(this.options.duration * 1000);
			 }
		 }else{
			 if(this.options.activeNav){
				 ao.removeClass("active");
				 an.addClass("active");
			 }			 
			 this.sections.eq(oldIndex).removeClass("active");
			 this.sections.eq(oldIndex).hide();
			 this.sections.eq(index).addClass("active");
			 this.sections.eq(index).show();
		 }
	},
	/**
	 * 
	 */
	next: function(o){
		var obj = (typeof o != "undefined") ? jQuery(o).get(0)._carousel : this;

		if(obj.options.activeNav){
			// pager nav
			var ao = obj.ctrlNav.filter(".active");
			var nextIndex = 0;
			if(ao.length == 1){
				var cIndex = ao.get(0)._index;
				nextIndex = (obj.sections.length - 1 == cIndex) ? 0 : cIndex + 1;
			}
		} else {
			// prev next nav
			var cIndex = obj.sections.filter(".active").eq(0).attr("_index");
			nextIndex = (obj.sections.length - 1 == cIndex) ? 0 : cIndex + 1;
		}
		obj._pager(nextIndex);
	},
	previous: function(o){
		var obj = (typeof o != "undefined") ? jQuery(o).get(0).carousel : this;
		if(obj.options.activeNav){
			// pager nav
			var ao = obj.ctrlNav.filter(".active");
			var nextIndex = 0;
			if(ao.length == 1){
				var cIndex = ao.get(0)._index;
				nextIndex = (cIndex === 0)? obj.sections.length - 1 : cIndex - 1;
			}
		} else {
			// prev next nav
			var cIndex = obj.sections.filter(".active").eq(0).attr("_index");
			nextIndex = (cIndex == 0)? obj.sections.length - 1 : cIndex - 1;
		}	
		obj._pager(nextIndex);
	},
	/**
	 * 
	 */
	stop: function(e)
	{
		if(typeof e != "undefined"){
			var c = jQuery(e.currentTarget).parents('.carousel').get(0)._carousel;
			clearInterval(c.timer);
		} else {
			clearInterval(this.timer);
			this.options.autoAnimate = false;
		}
	},
	/**
	 * 
	 */
	start: function(e)
	{
		if(typeof e != "undefined"){
			var t = jQuery(e.currentTarget).parents('.carousel').get(0)._carousel;
			if(t.options.autoAnimate){
				t._periodicallyUpdate(t.elm, t.options.frequency * 1000);
			}
		} else {
			this._periodicallyUpdate(this.elm, this.options.frequency * 1000);	
		}
	},
	/**
	 * 
	 */
	_periodicallyUpdate: function(o,t)
	{ 
		this.autoAnimateRunning = true;
		var nextItem = function(o){
			cfeu_corp.Carousel.prototype.next(jQuery(o));
		};
		this.timer = window.setInterval(function(){nextItem(o)},t);
	}
};;cfeu_corp.FieldHelp = function(elm,options){
	this.controllerName = "_fieldHelp"; // cfeu_corp.initialize will add cfeu_corp.FieldHelp object to elm HTML element
	this.options = {
			closeText : "close",
			closeButton : false
	};
	cfeu_corp.merge(this.options, options);
	this.obj = jQuery(elm);
	this.initialize();
}

cfeu_corp.FieldHelp.prototype = {
	initialize: function() {
		this.obj.parent().get(0)._fieldHelp = this;
		this.obj.click( this._clickHandler );
	},
	_clickHandler: function(event) {
		//
		event.preventDefault();
		//
		var target = jQuery(event.target);
		var obj = target.parent();
		var fieldHelp = obj.get(0)._fieldHelp;
		//
		var box = jQuery('<div />').addClass('block help');
		var header = jQuery('<h3 />').addClass('title'); 
		var body = jQuery('<div />').addClass('content');
		var elmId = obj.attr('id');
		
		if(document.getElementById(elmId+"-body") == null || document.getElementById( elmId +"-head") == null){ return; }
		
		box.append(header);
		box.append(body);
		//
		if(fieldHelp.options.closeButton == true){
			var closeButton = jQuery('<span />').addClass('close');
			var closeButtonText = jQuery('<span />').text( fieldHelp.options.closeText );
			closeButton.append(closeButtonText);
			closeButton.get(0)._fieldHelp = fieldHelp;
			header.append(closeButton);
			closeButton.click( fieldHelp.close );
		}
		// HEAD
		var htmlHead = document.getElementById( elmId +"-head").innerHTML;
		if(htmlHead) {
			header.append( jQuery('<span />').append(htmlHead) );
		}
		// BODY
 		var htmlBody = document.getElementById(elmId+"-body").innerHTML;
		if(htmlBody) {
			body.append( jQuery('<p />').append(htmlBody) );
			body.find("a.anchor").remove();
			var arrow = jQuery('<div />').addClass('arrow');
			body.append(arrow);					
		}
		//
		var height = jQuery(this).height();
 		var left = jQuery(this).offset().left ;
		var top = jQuery(this).offset().top - height * 0.5 + 3;
		var options = { left: left, top :top  };
		//
		fieldHelp.container = new cfeu_corp.FloatContainer(options);
 		fieldHelp.container.add(box);
 		fieldHelp.container.display();
	},

	close: function(event) {
		event.preventDefault();
		var fieldHelp = jQuery(event.target).closest(".close").get(0)._fieldHelp;
		if(fieldHelp && fieldHelp.container){
			fieldHelp.container.remove(fieldHelp.container);
		};
	}
};

;cfeu_corp.ActiveLabel = function(elm,options){
	this.options = {
			container : ".control"
	};
	cfeu_corp.merge(this.options, options);
	this.obj = jQuery(elm);
	this.initialize();
};

cfeu_corp.ActiveLabel.prototype = {
	initialize: function() {
		var l = "label[for='" + this.obj.attr('id') + "']";
		var lbl = (typeof this.options.container != 'undefined') ? this.obj.closest(this.options.container).find(l) : jQuery(l) ;
		//
		if(lbl.length == 1 ) {
			var o = this.obj;
			var f = function(){ ( o.val().length < 1)? lbl.show() : lbl.hide(); };
			this.obj.focus(function(){lbl.hide();});
			this.obj.blur(f);
			f();
		}
	}
};cfeu_corp.Subordinate = function(elm,options){
	this.controllerName = "_subordinate"; // cfeu_corp.initialize will add cfeu_corp.Subordinate object to elm HTML element
	this.elm = elm;
	this.options = { 
		master: "" // relative to .control [example "input:first"]
	};
	cfeu_corp.merge(this.options, options);
	this.init();
}

cfeu_corp.Subordinate.prototype = {
	init: function(){
		var m = jQuery(this.elm).closest(".control").find(this.options.master);
		if(m.length > 0){
			(m.get(0).checked == true) ? jQuery(this.elm).show() : jQuery(this.elm).hide();
			m.bind("click", this, this._changeHandler);
		}
	},
	_changeHandler: function(e){
		var o = e.data;
		var t = jQuery(e.target);
		(t.get(0).checked == true) ? jQuery(o.elm).show() : jQuery(o.elm).hide();
	}
};jQuery(document).ready(function(){
	/*
	 *  overlay
	 */
	cfeu_corp.initialize(cfeu_corp.Overlayer, "#showPopFlash", {overlayer: "#popFlash", closeKey:"27"});// flash overlay
	cfeu_corp.initialize(cfeu_corp.Overlayer, '.downloadList .jpeg, .downloadList .tiff, .downloadList .doc, .downloadList .gif, .downloadList .pdf, .downloadList .png, .downloadList .ppt, .downloadList .xls', {overlayer: "#popDownload"});// download overlay

	
	/*
	 * subordinates 
	 */
	cfeu_corp.initialize(cfeu_corp.Subordinate, '.control .subordinate', { master: "input:first" });
	cfeu_corp.initialize(cfeu_corp.ActiveLabel, '.control .subordinate input');
	/*
	 * fieldhelp
	 */
	cfeu_corp.initialize(cfeu_corp.FieldHelp, ".fieldHelp");
	/*
	 * carousel
	 */
	cfeu_corp.initialize(cfeu_corp.Carousel, ".carousel", {autoAnimate: true, frequency: 4, activeNav:true });
});
