(function($){
	$.fn.slider = function(options) {
		
		var defaults = {
			timer: false,
			randomize: false			
		};
		
		var opts = $.extend(defaults, options);
		
		return this.each(function() {
			
			/*	PARAMETERS	*/                    	
			var obj=  	$(this),
			o 			= $.meta ? $.extend({}, opts, $pxs_container.data()) : opts;
			
			obj.s_nav = $(".s_nav",obj);
			
			obj.s_slider =  $(".s_slider",obj);
			obj.slider_box = $(".slider_box",obj);
			obj.slider_box_w = obj.slider_box.css("width");
			obj.slider_box_length = obj.slider_box.length;
			
			$('.s_slider',obj).css("width",String(parseInt(obj.slider_box_w) * obj.slider_box.length + "px"));
			
			var deviceAgent = navigator.userAgent.toLowerCase();
			var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
			if (agentID) {
			 	o.randomize = false;
				o.timer = false;
			}
						
			obj.randomize = 0
			if(o.randomize){
				obj.randomize = Math.floor(Math.random()*obj.slider_box_length) 	
				$('.s_slider',obj).css("left",String("-" + String(parseInt(obj.slider_box_w) * obj.randomize)) + "px");
			}
			
			obj.s_nav_html =  "";
			
			var i=0;
			for (i=1;i<=obj.slider_box_length;i++)
			{
				obj.s_nav_html+= '<div class="s_nav_dot"></div>'; 
			}
			obj.s_nav.html(obj.s_nav_html);
			
			obj.s_nav_dot = $(".s_nav_dot",obj);
			
			obj.s_nav_dot_h = obj.s_nav_dot.eq(0).css("height");
			
			obj.s_nav_dot.eq(obj.randomize).css("background-position","0 -" + obj.s_nav_dot_h);
			//alert(String(parseInt(obj.s_nav_dot.eq(0).outerWidth("true")) * obj.slider_box_length + "px"))
			obj.s_nav.css("width", String(parseInt(obj.s_nav_dot.eq(0).outerWidth("true")) * obj.slider_box_length + "px"))

			
			
			if (!agentID) {
				obj.s_nav_dot.click(function(){
						_index = $(this).index();
						obj.slide(_index);
				})
			}
			
			obj.link_open_tab = $(".WORKbox",obj);
			
			obj._tab_open = false;
			obj.link_open_tab.click(function(){
				_ref_tab_open = $(this).attr("id");
				
				$(".WORKthumb").css("background-position","0 0");
				$("." + _ref_tab_open + "_thumb").css("background-position","0 -124px");
				
				$(".WHATPortfolioarea").each(function(index) {
					var this_what = $(this);
					if ($(".WHATPortfolioarea." + _ref_tab_open).css("display") == "none"){
						if($.browser.msie && (($.browser.version=="6.0") ||($.browser.version=="7.0"))) 
							$(".WHATPortfolioImagearea").css("display","none");
						if(this_what.css("display") == "block")
							this_what.slideUp("slow",function(){
							$(".WHATPortfolioarea." + _ref_tab_open).slideDown("slow", function(){
								if($.browser.msie && (($.browser.version=="6.0") ||($.browser.version=="7.0"))) 
									$(".WHATPortfolioImagearea").css("display","block");
								obj._tab_open = true;
							});												
						});
					}
				});
				
				if(obj._tab_open == false){
					if($.browser.msie && (($.browser.version=="6.0") ||($.browser.version=="7.0"))) 
						$(".WHATPortfolioImagearea").css("display","none")
					$(".WHATPortfolioarea." + _ref_tab_open).slideDown("slow",function(){
						if($.browser.msie && (($.browser.version=="6.0") ||($.browser.version=="7.0"))) 
							$(".WHATPortfolioImagearea").css("display","block");																					   	
						obj._tab_open = true;
						if($.browser.webkit){
							$('body')
							.stop()
							.animate({
								//get top-position of target-element and set it as scroll target
								scrollTop: ($("#WHATimagearea").offset().top - 20)
								//scrolldelay: 2 seconds
								}, {
									duration:100,
									complete: function() {}
							});
						}
						else {
							$('html')
							.stop()
							.animate({
								//get top-position of target-element and set it as scroll target
								scrollTop: ($("#WHATimagearea").offset().top - 20)
								//scrolldelay: 2 seconds
								}, {
									duration:100,
									complete: function() {}
							});
						}
					});		
				}
			});
			obj.slide = function(_index){
				if (o.timer)
					obj.stopTime();
				
				$('.s_slider',obj).animate({
					left: "-" + String(Number(_index * parseInt(obj.slider_box_w)) + "px")
				}, 1000, function() {
					obj.s_nav_dot.css("background-position","0 0px");
					obj.s_nav_dot.eq(_index).css("background-position","0 -" + obj.s_nav_dot_h);
					
					if (o.timer){
						obj.timer_count=_index;
						obj.setTimer();
					}
				});			
			}
			$(".w_btn a",obj).click(function(){
				bt_class_index = $(this).attr("class");
				if($.browser.webkit){
					$('body')
						.stop()
						.animate({
							//get top-position of target-element and set it as scroll target
							scrollTop: ($("a[name='WHAT_link']").offset().top - 20)
							//scrolldelay: 2 seconds
							}, {
								duration:2000,
								complete: function() {
									$("#WHATimagearea .s_nav .s_nav_dot").eq(0).click();
									$("#WHATimagearea ." + bt_class_index).children(".WORKtxtarea").children("a").click();
							}
						});
				}
				else {
						$('html')
						.stop()
						.animate({
							//get top-position of target-element and set it as scroll target
							scrollTop: ($("a[name='WHAT_link']").offset().top - 20)
							//scrolldelay: 2 seconds
							}, {
								duration:2000,
								complete: function() {
									$("#WHATimagearea .s_nav .s_nav_dot").eq(0).click();
									$("#WHATimagearea ." + bt_class_index).children(".WORKtxtarea").children("a").click();
							}
						});
				} 	
				
				
			});
			obj.setTimer = function(){
				obj.everyTime(10000,function(i) {
											
					//obj.timer_count+=1;
					if(obj.timer_count == obj.slider_box_length-1)
						obj.timer_count = 0;
					else	
						obj.timer_count+=1;		
					
					obj.slide(obj.timer_count);
				});	
			}	
			
			/*	swipe	*/
		
			obj.IMG_WIDTH = parseInt(obj.slider_box_w);
			obj.currentImg=0;
			obj.maxImages=obj.slider_box_length;
			obj.speed=500;
			obj.imgs;
			obj.swipeOptions=
			{
				triggerOnTouchEnd : true,	
				swipeStatus : obj.swipeStatus = function(event, phase, direction, distance)
				{
					//If we are moving before swipe, and we are going Lor R in X mode, or U or D in Y mode then drag.
					if( phase=="move" && (direction=="left" || direction=="right") )
					{
						obj.duration=0;
						
						if (direction == "left")
							obj.scrollImages((obj.IMG_WIDTH * obj.currentImg) + distance, obj.duration);
						
						else if (direction == "right")
							obj.scrollImages((obj.IMG_WIDTH * obj.currentImg) - distance, obj.duration);
						
					}
					
					else if ( phase == "cancel")
					{
						obj.scrollImages(obj.IMG_WIDTH * obj.currentImg, obj.speed);
					}
					
					else if ( phase =="end" )
					{
						if (direction == "right")
							obj.previousImage()
						else if (direction == "left")			
							obj.nextImage()
					}
				},
				allowPageScroll:"vertical",
				threshold:200
			}
			
				obj.imgs = obj.s_slider;
				obj.imgs.swipe(obj.swipeOptions);
			
			obj.previousImage = function()
			{
				obj.currentImg = Math.max(obj.currentImg-1, 0);
				obj.scrollImages( obj.IMG_WIDTH * obj.currentImg, obj.speed);
				obj.s_nav_dot.css("background-position","0 0px");
				obj.s_nav_dot.eq(obj.currentImg).css("background-position","0 -" + obj.s_nav_dot_h);
			}
		
			obj.nextImage = function()
			{
				obj.currentImg = Math.min(obj.currentImg+1, obj.maxImages-1);
				obj.scrollImages( obj.IMG_WIDTH * obj.currentImg, obj.speed);
				obj.s_nav_dot.css("background-position","0 0px");
				obj.s_nav_dot.eq(obj.currentImg).css("background-position","0 -" + obj.s_nav_dot_h);
			}
				
			/**
			* Manuallt update the position of the imgs on drag
			*/
			obj.scrollImages = function(distance, duration)
			{
				obj.imgs.css("-webkit-transition-duration", (duration/1000).toFixed(1) + "s");
				
				//inverse the number we set in the css
				var value = (distance<0 ? "" : "-") + Math.abs(distance).toString();
				
				obj.imgs.css("-webkit-transform", "translate3d("+value +"px,0px,0px)");
			}
			
		});	
	}
})(jQuery);
