(function($) {
	//Plugin methods
	var base;
	
	var s_options;
	var methods = {
		init : function(options) {
			base = this;
			
			$(".hide").css("opacity","0");
			base.panels_wrapper = $(base).find("ul.panels");
			base.panels = $(base.panels_wrapper).find("li.content_panel");
			base.menu = $(this).find("ul.menu");
			if($(this).hasClass("solutions")) {
				base.menu_items = $(".panel_button");
			} else {
				base.menu_items = $(".slider_menu");
			}
			//set overflow of container
			
			$(base).css({
				width : "940px",
				position : "relative",
				margin : "0 auto",
				overflow : "hidden"
			});

			base.cur_position = 0;
			//set wrappers width
			var screen_width = $(base).width();
			//set width of slider
			$(base.panels_wrapper).css({
				width : (940 * base.panels.length) + "px",
				position : "absolute",
				zIndex : "5"
			});
			//set panels width
			$(base.panels).css({
				width : 940 + "px"
			});

			//initialize menu on click
			if($(this).hasClass("solutions")) {
				$(base.menu_items).click(function() {
					//look for item id to slide to call slide function
					//methods.stopRotation();
					$(this).parent().find(".panel_button").removeClass("active");
					$(this).addClass("active");
					methods.slide($(this).find("input").val());
					// var parent = $(this).parent();
					// $(".menu").find(".current_page_item").removeClass("current_page_item");
					// $(parent).addClass("current_page_item");
					return false;
				});
			} else {
				$(base.menu_items).click(function() {
					//look for item id to slide to call slide function
					//methods.stopRotation();
					methods.slide($(this).attr("title"));
					var parent = $(this).parent();
					$(".menu").find(".current_page_item").removeClass("current_page_item");
					$(parent).addClass("current_page_item");
					return false;
				});
			}

			base.next = $(base).find(".next");
			base.prev = $(base).find(".prev");
			//bind next click
			$(base.next).click(function() {
				methods.stopRotation();
				methods.next();
			});
			//bind previous click
			$(base.prev).click(function() {
				methods.stopRotation();
				methods.previous();
			});
			//setup slidestep
			base.slide_step = 940;
			//bind resize
			// $(window).resize(function() {
				// methods.reInit();
			// });
			//auto_slide
			if($(base).hasClass("active")) {
				// base.interval = setInterval(function(){
				// methods.next();
				// },s_options.interval);
			}

			//check query string to see if its referencing a panel
			try {
				var p = getQueryVariable("panel");
				if(p != null) {
					if(p.length > 0) {
						if(p != "solutions") {
							$(".menu").find(".current_page_item").removeClass("current_page_item");
							$(".panel_" + p).addClass("current_page_item");
							methods.slide(p);
						}
					}
				}
			} catch(e) {

			}

		},
		reInit : function() {

			// $(base).css({
				// width : "100%",
				// overflow : "hidden"
			// });
			$(base).css({
				width : "940px",
				position : "relative",
				margin : "0 auto",
				overflow : "hidden"
			});

			$(base.panels_wrapper).css({
				width : (940 * base.panels.length) + "px",
				position : "absolute",
				zIndex : "5"
			});
			var screen_width = $(base).width();
			base.slide_step = 940;
			$(base.panels_wrapper).css({
				width : (940 * base.panels.length) + "px",
				position : "absolute",
				zIndex : "5"
			});
			//set panels width
			$(base.panels).css({
				width : 940 + "px"
			});

			methods.simpleSlideTo(base.cur_position);
			methods.stopRotation();
			// if($(base).hasClass("active"))
			// {
			// base.interval = setInterval(function(){
			// methods.next();
			// },s_options.interval);
			// }
		},
		slide : function(item_id) {
			//slide to given item_id
			
			base.previous_position = base.cur_position;
			base.cur_position = methods.getSlideNum(item_id);
			
			if(base.previous_position != base.cur_position) {
				if(base.cur_position != null) {
					//set new opacity to 0
					$(base.panels[base.cur_position]).css({
						opacity : 0,
						display:"block"
					});
					//animte next opacity to 1
					$(base.panels[base.cur_position]).animate({
						opacity : 1
					}, 2000, "easeInCirc");
					//slide wrapper
					$(base.panels_wrapper).animate({
						left : "-" + (base.cur_position * base.slide_step) + "px"
					}, 1400, "easeInOutCirc", function() {
						$(base.panels[base.previous_position]).css({
							opacity : 0
						});
					});
					//re"show" the panel
					$(base.panels[base.previous_position]).animate({
						opacity : 0
					}, 500, "easeOutCirc");

				}
			}
		},
		slideTo : function(pos) {
			//check if given position is withing the bounds of array
			if(pos < base.panels.length) {
				base.previous_position = base.cur_position;
				base.cur_position = pos

				if(base.cur_position != null) {
					//set new opacity to 0
					$(base.panels[base.cur_position]).css({
						opacity : 0,
						display:"block"
					});
					//animte next opacity to 1
					$(base.panels[base.cur_position]).animate({
						opacity : 1
					}, 1600, "easeInCirc");
					//slide wrapper
					$(base.panels_wrapper).animate({
						left : "-" + (base.cur_position * base.slide_step) + "px"
					}, 1400, "easeInOutCirc", function() {
						$(base.panels[base.previous_position]).css({
							opacity : "1"
						});
					});
					//re"show" the panel
					$(base.panels[base.previous_position]).animate({
						opacity : 0
					}, 500, "easeOutCirc");

				}
			}
		},
		simpleSlideTo : function(pos) {
			//check if given position is withing the bounds of array
			if(pos < base.panels.length) {
				base.previous_position = base.cur_position;
				base.cur_position = pos

				if(base.cur_position != null) {
					//set new opacity to 0
					//$(base.panels[base.cur_position]).css({opacity:0});
					//animte next opacity to 1
					//$(base.panels[base.cur_position]).animate({opacity:1},1600,"easeInCirc");
					//slide wrapper
					$(base.panels_wrapper).animate({
						left : "-" + (base.cur_position * base.slide_step) + "px"
					}, 1400, "easeInOutCirc", function() {
						//$(base.panels[base.previous_position]).css({opacity:"1"});
					});
					//re"show" the panel
					//$(base.panels[base.previous_position]).animate({
					//	opacity:0
					//},1400,"easeOutCirc");

				}
			}
		},
		getSlideNum : function(item_id) {
			for(var i = 0; i < base.panels.length; i++) {
				if($(base.panels[i]).attr("id") == item_id) {
					
					return i;
				}
			};

		},
		next : function() {
			$(".menu").find(".current_page_item").removeClass("current_page_item");

			if((base.panels.length - 1) == (base.cur_position)) {
				//do nothing or loop
				$(".panel_" + $(base.panels[0]).attr("id")).addClass("current_page_item");
				methods.slideTo(0);
			} else {
				$(".panel_" + $(base.panels[base.cur_position + 1]).attr("id")).addClass("current_page_item");
				methods.slideTo(base.cur_position + 1);
			}
		},
		previous : function() {
			$(".menu").find(".current_page_item").removeClass("current_page_item");
			if(base.cur_position == 0) {
				$(".panel_" + $(base.panels[base.panels.length - 1]).attr("id")).addClass("current_page_item");
				//do nothing or loop
				methods.slideTo((base.panels.length - 1));
			} else {
				methods.slideTo(base.cur_position - 1);
				$(".panel_" + $(base.panels[base.cur_position]).attr("id")).addClass("current_page_item");
			}
		},
		stopRotation : function() {
			clearInterval(base.interval);
		}
	}
	//plugin
	$.fn.jSlider = function(method, options) {

		var defaults = {
			duration : 300,
			type : "full",
			interval : 7000
		};
		s_options = $.extend({}, defaults, options);

		if(methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		} else if( typeof method === 'object' || !method) {
			return methods.init.apply(this, arguments);
		} else {
			$.error('Method ' + method + ' does not exist on jQuery.tooltip');
		}

	};
})(jQuery);

