jQuery.fn.accordion = function(options) {
	var Accordion = {
		version: "1.0",
		initialize: function(containerEl, options){
			// option defaults
			this.options = jQuery.extend({
				tabSelector: "div#accordion div.tab",
				triggerSelector: "div#accordion div.trigger",
				drawerSelector: "div#accordion div.drawer",
				eventType: "click",
				defaultOpenTab: 0
			}, options || {});

			// accordion parts
			this.container = containerEl;
			this.tabs = jQuery(this.options.tabSelector);
			this.triggers = jQuery(this.options.triggerSelector);
			this.drawers = jQuery(this.options.drawerSelector);
			this.accordionId = this.container.attr("id");
			
			if (this.options.animator){
				this.animator = this.options.animator(this);
			}
			
			// set the state of all tabs to open
			this.tabs.each(function(i){
				this._open = true;
			});

			// check query string for default tab - NOT SUPPORTED BY JQUERY?
			// this.readQueryString();

			// quick close all tabs except default (no parallel event fired, therefore no animation)
			this.closeOtherTabs(this.tabs[this.options.defaultOpenTab], "quick");

			// add event handling
			var scope = this;
			this.triggers.bind(this.options.eventType, function(e){
				scope.getActivatedTab(e)
			});
		},
		/*readQueryString: function(){
			var originalDefaultOpenTab = this.options.defaultOpenTab;
			this.options.defaultOpenTab = window.location.search.toQueryParams()[this.accordionId];
			if(typeof(this.tabs[this.options.defaultOpenTab]) == "undefined"){
				this.options.defaultOpenTab	= originalDefaultOpenTab;
			}
		},*/
		openTab: function(tab){
			if(tab._open) { return; }
			jQuery(tab).removeClass("closed").addClass("open");
			tab._open = true;
			jQuery().trigger(this.accordionId+"_tab:opened", [tab]);
		},
		closeTab: function(tab){
			if(!tab._open) { return; }
			jQuery(tab).removeClass("open").addClass("closed");
			tab._open = false;
			jQuery().trigger(this.accordionId+"_tab:closed", [tab]);
		},
		closeOtherTabs: function(tabToOpen, type){
			// find all open tabs
			var currentlyOpenTabs = [];
			this.tabs.each(function(i){
				if(this._open){
					currentlyOpenTabs.push(this);
				}
			});
			
			// close each open tab
			jQuery(currentlyOpenTabs).each(function(i){
				jQuery(this).removeClass("open").addClass("closed");
				this._open = false;
			});

			// set active tab to open
			jQuery(tabToOpen).removeClass("closed").addClass("open");
			tabToOpen._open = true;

			// if type is quick, fire initialization event
			if (type == "quick"){
				jQuery().trigger(this.accordionId+"_accordion:initialized", [this.container, currentlyOpenTabs, tabToOpen]);

			// if type is not quick, fire parallel event
			} else {
				jQuery().trigger(this.accordionId+"_tab:parallel", [tabToOpen, currentlyOpenTabs]);
			}
		}
	}
	
	var AccordionController = jQuery.extend(Accordion, {
		getActivatedTab: function(e){
			var activatedTrigger = jQuery(e.target).closest(this.options.triggerSelector)[0];
			var activatedTab = this.tabs[jQuery.inArray(activatedTrigger, this.triggers)];
			this.manageActivatedTab(activatedTab);
		},
		manageActivatedTab: function(activatedTab){
			// if tab is closed, close other tabs and open it, if it is open, do nothing.
			if(!activatedTab._open){ 
				this.closeOtherTabs(activatedTab);
			}
		},
		getCurrentTab: function(){
			var openTab;
			this.tabs.each(function(i){
				if(this._open){
					openTab = this;
				}
			});
			return openTab;
		},
		getNextTab: function(){
			var currentTab = this.getCurrentTab();
			var currentIndex = jQuery.inArray(currentTab, this.tabs);
			if (currentIndex < this.tabs.length-1){
				return this.tabs[currentIndex+1];
			} else {
				return "Currently open tab is the last tab.";
			}
		},
		getPreviousTab: function(){
			var currentTab = this.getCurrentTab();
			var currentIndex = jQuery.inArray(currentTab, this.tabs);
			if (!currentIndex == 0){
				return this.tabs[currentIndex-1];;					
			} else {
				return "Currently open tab is the first tab.";
			}
		}	
	});
	
	if(options.type == "MultiTabAllClose") {
		jQuery.extend(AccordionController, {
			manageActivatedTab: function(activatedTab){
				// if tab is open, close it, if tab is closed, open it.
				activatedTab._open ? this.closeTab(activatedTab) : this.openTab(activatedTab);		
			},
			getCurrentTab: function(){
				return "MultiTab Controller does not support this function.";
			},
			getNextTab: function(){
				return "MultiTab Controller does not support this function.";
			},
			getPreviousTab: function(){
				return "MultiTab Controller does not support this function.";
			}
		});
	}
	
	if(options.type == "MultiTabAlwaysOpen") {
		jQuery.extend(AccordionController, {
			manageActivatedTab: function(activatedTab){
				// if the activated tab is open..
				if(activatedTab._open){
					// check to see if multiple tabs are open
					var openTabs = [];
					this.tabs.each(function(i){
						if(this._open){
							openTabs.push(this);
						}
					});
					// if multiple are open, close the tab
					if (openTabs.length > 1){
						this.closeTab(activatedTab);
					}
				// if the activated tab is closed, open it.
				} else {
					this.openTab(activatedTab)
				}	
			},
			getCurrentTab: function(){
				return "MultiTab Controller does not support this function.";
			},
			getNextTab: function(){
				return "MultiTab Controller does not support this function.";
			},
			getPreviousTab: function(){
				return "MultiTab Controller does not support this function.";
			}
		});
	}
	
	if(options.type == "OneTabAllClose") {
		jQuery.extend(AccordionController, {
			manageActivatedTab: function(activatedTab){
				// if tab is open, close it
				if(activatedTab._open){
					this.closeTab(activatedTab)

				// if tab is closed, close all other tabs and open it
				} else if(!activatedTab._open){
					var currentlyOpenTab = this.getCurrentTab();
					if(currentlyOpenTab){
						this.closeOtherTabs(activatedTab);
					} else {
						this.openTab(activatedTab);
					}
				}		
			}
		});		
	}

	return AccordionController.initialize(this, options);
}
jQuery.fn.accordionAnimation = function(options) {
	var AccordionAnimation = {
		initialize: function(options){
			// option defaults
			this.options = options;
			jQuery.extend({
				duration: 1000,
				transition: "swing"
			}, this.options || {});
		
			this.accordion = this.options.accordion;
			this.currentlyAnimating = false;
			
			// listen for custom events
			var scope = this;
		
			jQuery().bind(this.accordion.accordionId+"_accordion:initialized", function(e, caller, inactiveTabs, activeTab){
				scope.setupAnimations(e, caller, inactiveTabs, activeTab);
			});
			jQuery().bind(this.accordion.accordionId+"_tab:opened", function(e, tabToOpen){
				scope.openAnimation(e, tabToOpen);
			});
			jQuery().bind(this.accordion.accordionId+"_tab:closed", function(e, tabToClose){
				scope.closeAnimation(e, tabToClose);
			});
			jQuery().bind(this.accordion.accordionId+"_tab:parallel", function(e, tabToOpen, tabToClose){
				scope.parallelAnimation(e, tabToOpen, tabToClose);
			});	
		},
		setupAnimations: function(e, caller, inactiveTabs, activeTab){

			// get active and inactive tabs from event memo
			var activeTab = activeTab;
			var inactiveTabs = inactiveTabs;
		
			
			// set original drawer heights to "_height" property for use in animations
			jQuery(this.accordion.drawers).each(function(i){
				this._height = jQuery(this).height();
				jQuery(this).css({overflow: "hidden"});
			});	
		
			// set inactive tabs drawer height to 0px to appear closed and for smoothness in animation
			var scope = this;
		
			jQuery(inactiveTabs).each(function(i){
				jQuery(scope.accordion.drawers[i]).css({
					height: "0px"
				});
			});
		
			
			// set active tab drawer height to it's original height for smoothness in animation
			jQuery(this.accordion.drawers[jQuery.inArray(activeTab, this.accordion.tabs)]).css({
				height: this.accordion.drawers[jQuery.inArray(activeTab, this.accordion.tabs)]._height+"px"
			});
		
		},
		openAnimation: function(e, tabToOpen){
			var tabToOpen = tabToOpen;
			var drawerToOpen = this.accordion.drawers[jQuery.inArray(tabToOpen,this.accordion.tabs)];
		
			this.currentlyAnimating = true;
			var scope = this;
			jQuery(drawerToOpen).animate(
				{
					height: drawerToOpen._height+"px"
				},
				this.options.duration,
				this.options.transition,
				function(){
					scope.currentlyAnimating = false;
				}
			);
		},
		closeAnimation: function(e, tabToClose){
			var tabToClose = tabToClose;
			var drawerToClose = this.accordion.drawers[jQuery.inArray(tabToClose, this.accordion.tabs)];

			this.currentlyAnimating = true;
			var scope = this;
			jQuery(drawerToClose).animate(
				{
					height: "0px"
				},
				this.options.duration,
				this.options.transition,
				function(){
					$(this).css({overflow: "hidden"});
					scope.currentlyAnimating = false;
				}
			);
		},
		parallelAnimation: function(e, tabToOpen, tabToClose){
			var tabToOpen = tabToOpen;
			var tabToClose = tabToClose[0];
			var drawerToOpen = this.accordion.drawers[jQuery.inArray(tabToOpen, this.accordion.tabs)];
			var drawerToClose = this.accordion.drawers[jQuery.inArray(tabToClose, this.accordion.tabs)];

			this.currentlyAnimating = true;
			var scope = this;
		
			jQuery(drawerToClose).animate(
				{
					height: "0px",
					overflow: "hidden"
				},
				this.options.duration,
				this.options.transition,
				function(){
					$(this).css({overflow: "hidden"});
				}				
			);
			jQuery(drawerToOpen).animate(
				{
					height: drawerToOpen._height+"px"
				},
				this.options.duration,
				this.options.transition,
				function(){
					scope.currentlyAnimating = false;
				}
			);
		
		}
	}
	return AccordionAnimation.initialize(options);
}

$(function() {
	var swap_text_boxes = [];
	//Store the default value for each box
	$('input[type=text][value].swaptextbox').each(function() {
		swap_text_boxes[$(this).attr('id')] = $(this).attr('value');
		});
		//Add focus and blur events to set or clear the value
		$('input[type=text][value].swaptextbox').bind('focus', function() {
		if($(this).val() == swap_text_boxes[$(this).attr('id')]) {
		  $(this).val('');
		}
		});
		$('input[type=text][value].swaptextbox').bind('blur', function() {
		if($(this).val() == '') {
		  $(this).val(swap_text_boxes[$(this).attr('id')]);
		}
	});
	

	$('.backtotop').click(function(){
		$('html, body').animate({scrollTop:0}, 'slow');
		return false;
	});
});


$(function () {
	$('.trigger').click(function() {
		return false;
	}).css("display", "inline");
	
	

	
  $('.bubbleInfo').each(function () {
    // options
    var distance = 10;
    var time = 100;
    var hideDelay = 500;

    var hideDelayTimer = null;

    // tracker
    var beingShown = false;
    var shown = false;
	

	
	
    
    var trigger = $('.trigger', this);
    var popup = $('.popup', this).css('opacity', 0).addClass("offscreen");

    // set the mouseover and mouseout on both element
    $([trigger.get(0), popup.get(0)]).mouseover(function () {
      // stops the hide event if we move from the trigger to the popup element
      if (hideDelayTimer) clearTimeout(hideDelayTimer);

      // don't trigger the animation again if we're being shown, or already visible
      if (beingShown || shown) {
        return;
      } else {
        beingShown = true;

        // reset position of popup box
        popup.css({
          top: -150,
          left: 125,
          display: 'block' // brings the popup back in to view
        })

        // (we're using chaining on the popup) now animate it's opacity and position
        .animate({
          top: '-=' + distance + 'px',
          opacity: 1
        }, time, 'swing', function() {
          // once the animation is complete, set the tracker variables
          beingShown = false;
          shown = true;
        });
      }
    }).mouseout(function () {
      // reset the timer if we get fired again - avoids double animations
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      
      // store the timer so that it can be cleared in the mouseover if required
      hideDelayTimer = setTimeout(function () {
        hideDelayTimer = null;
        popup.animate({
          top: '-=' + distance + 'px',
          opacity: 0
        }, time, 'swing', function () {
          // once the animate is complete, set the tracker variables
          shown = false;
          // hide the popup entirely after the effect (opacity alone doesn't do the job)
          popup.css('display', 'none');
        });
      }, hideDelay);
    });
  });
});





$(function() {
	jQuery.fn.bubblePopup = function(options) {
		var opts = $.extend({}, $.fn.bubblePopup.defaults, options);
		
		return this.each(function() {
			$this = $(this);
			var trigger = $(opts.triggerElement, $this);
			var popup = $(opts.popupElement, $this);
			var distance = opts.distance;
			var shown = opts.shown;
			var hideDelayTimer = opts.hideDelayTimer;
			var time = opts.time;
			var hideDelay = opts.hideDelay;
			var beingShown = opts.beingShown;
			var popupWidth = opts.popupWidth;
			var pageWrapper = $(opts.pageWrapper);
			jsActive();
			eventListeners();

			function jsActive() {
				popup.css({
					'opacity': 0,
					"width": popupWidth
				}).addClass("offscreen");
				trigger.css("display", "inline");
			};

			function eventListeners() {
				$([trigger.get(0), popup.get(0)]).bind("mouseover", __open);
				$([trigger.get(0), popup.get(0)]).bind("mouseout", __close);
			};
			
			function __open() {
				
				if (hideDelayTimer) clearTimeout(hideDelayTimer);
				// don't trigger the animation again if we're being shown, or already visible
				if (beingShown || shown) {
					return;
				} else {
					beingShown = true;
					var popupWidth = popup.width();
					var offset = trigger.offset().left+popupWidth;
					var wrapperWidth = pageWrapper.width();
					var triggerWidth = trigger.width();
				
					// If the wrapper
					if(offset < wrapperWidth) {
						var left = triggerWidth+20;
					} else {
						var left =  -(popupWidth+15);
					}
					
					// To fix IE6/7 z index issues
				
					// reset position of popup box
					popup.css({
						top: -150,
						left: left,
						display: 'block' // brings the popup back in to view
					});
					
					popup.animate({
						top: '-=' + distance + 'px',
						opacity: 1
					}, time, 'swing', function() {
					});
					beingShown = false;
					shown = true;
					
				}
			};
			
			function __close() {
				// reset the timer if we get fired again - avoids double animations
				
				if (hideDelayTimer) clearTimeout(hideDelayTimer);
				
				// store the timer so that it can be cleared in the mouseover if required
				hideDelayTimer = setTimeout(function () {
					hideDelayTimer = null;
					popup.animate({
						top: '-=' + distance + 'px',
						opacity: 0
					}, time, 'swing', function () {
						
						// once the animate is complete, set the tracker variables
						shown = false;
						
						// hide the popup entirely after the effect (opacity alone doesn't do the job)
						popup.css({
							display: 'none'
						});
					});
				}, hideDelay);
			};	
		});	
	};

	$.fn.bubblePopup.defaults = {
		popupElement: ".popup",
		triggerElement: ".trigger",
		distance: 10,
		time: 100,
		hideDelay: 500,
		beingShown: false,
		shown: false,
		hideDelayTimer: null,
		popupWidth: "350px",
		pageWrapper: "#main-wrapper"
	};

});

$(function() {
	jQuery.fn.crossFade = function(options) {
		var opts = $.extend({}, $.fn.crossFade.defaults, options);
		
		return this.each(function() {
			$this = $(this);
			var buttonPrevious = $(opts.buttonPrevious);
			var buttonNext = $(opts.buttonNext);
			var viewport = $(opts.viewport);
			var items = viewport.children("ul").children("li");
			var index = opts.index;
			var duration = opts.duration;
			if(opts.counter == true) {
				var counterWrapper = $(opts.counterWrapper);
				counterActive();
			}
			jsActive();
			bindEventListeners();
						
			function counterActive() {
				counterWrapper.show();
				$(counterWrapper).children("span:last").text(items.length);
				setCurrentCounter();
			};
			
			function setCurrentCounter() {
				$(counterWrapper).children("span:first").text(index+1);
			};

			function jsActive() {
				buttonPrevious.show();
				buttonNext.show();
				var height = items.height();
				items.css({
					"position": "absolute"
				});
				items.parent().css("height", height);
			
				items.not(items[index]).css("opacity", 0);
				items.each(function(i) {
					$(this).css("zIndex",i+1);
				})
			};
	
			function bindEventListeners() {
				buttonPrevious.bind("click", __previousImage);
				buttonNext.bind("click", __nextImage);
			};
			
			function unbindEventListeners() {
				buttonPrevious.unbind("click", __previousImage);
				buttonNext.unbind("click", __nextImage);
			};
			
			function __nextImage() {
				var previousIndex = index;
		        index++;
		        if(index >= items.length) {
		            index = 0;
		        }
				showImage(previousIndex);
			};
			
			function __previousImage() {
				var previousIndex = index;
				index--;
				if(index <= -1) {
		            index = items.length - 1;
		        }
				showImage(previousIndex);
			};
			
			function showImage(previousIndex) {
				unbindEventListeners();
				setCurrentCounter();
				var previousZ = items.eq(previousIndex).css("zIndex");
				var currentZ = items.eq(index).css("zIndex");
				items.eq(index).animate({
					opacity: 1
				}, duration, function(){
					items.eq(previousIndex).css({
						opacity: 0
					});
					items.eq(index).css({
						zIndex: previousZ
					});
					items.eq(previousIndex).css({
						zIndex: currentZ
					});
					bindEventListeners();
				});
			};
		});	
	};

	$.fn.crossFade.defaults = {
		buttonPrevious: "#buttonPrevious",
		buttonNext: "#buttonNext",
		counterWrapper: "#counterWrapper",
		viewport: "#image-viewport",
		index: 0,
		counter: true,
		duration: 500
	};

});