
	$(document).ready(function()
	{
		$(".trokit_slider").trokitSlider();
		
		$("input.viewproject").each(function()
		{
			var $this = $(this);
			var href = $(this).attr("data-href");
			
			if( href )
			{
				$this.click(function(ev)
				{
					ev.preventDefault();
					window.location.href = href;
					
					return false;
				});
			}
		});
		
		var bg = $(".trokit_slider").css("backgroundImage").replace(/url\(|\)/ig, '');
		
		if( bg )
		{
			tsAutoRotator();
			
		}
		else
		{
			tsAutoRotator();
		}
		
		// Slide Change - Arrows
		$(window).keydown(function(e)
		{
			if( e.keyCode == 37 || e.keyCode == 39 )
			{
				if( e.keyCode == 37 )
				{
					$(".trokit_navigation .prev a").trigger('click');
				}
				else
				{
					$(".trokit_navigation .next a").trigger('click');
				}
			}
		});
	});
	
	// Auto Rotator
	var ts_autorotator;
	var ts_autorotator_timeout = 5000;
	var ts_autorotator_disabled = false;
	
	function tsAutoRotator()
	{
		$(".trokit_navigation a").click(function(ev)
		{
			ts_autorotator_disabled = true;
		});
		
		ts_autorotator = window.setInterval(function()
		{
			if( !ts_autorotator_disabled )
			{
				$(".trokit_slider").trokitNextSlide();
			}
		}, ts_autorotator_timeout);
	}
	
	// Trokit Slider - Created by Arlind Nushi @ Trokit Agency Creative - www.trokit.com
	(function($){
	
		var busy = false;
		
		var easingIn = 'easeInOutExpo';
		var easingOut = 'easeInOutExpo';
		
		var easingIn = 'easeInOutQuart';
		var easingOut = 'easeInOutQuart';
		
		var min_time = 800;
		var max_time = 1600;
		
		var ts;
		var bg_mask;
		var slides = [];
		
		var current_index = 0;
		var slides_total = 0;
		
		function getSlide(index)
		{
			var index = index % slides_total;
			return slides[index];
		}
		
		function randomFromTo(from, to)
		{
			return Math.floor(Math.random() * (to - from + 1) + from);
		}
		
		function changeSlide(_out, _in)
		{
			if( busy || slides.length < 2 )
			{
				return false;
			}
			
			var body = $("body");
			busy = true;
			
			var current_slide = getSlide(_out);
			var next_slide = getSlide(_in);
			
			var turn = 1;
			var win_ts_gap_size = parseInt($(window).width() / 2 - ts.width() / 2);
			
			// Container Height
			var current_height = current_slide.height();
			var next_height = next_slide.height();
			
			// Get Predefined Next Slide Height
			var default_current_height = parseInt(current_slide.attr("data-slider-height"), 10);
			if( default_current_height )
				current_height = default_current_height;	
			
			// Get Predefined Next Slide Height
			var default_next_height = parseInt(next_slide.attr("data-slider-height"), 10);
			if( default_next_height )
				next_height = default_next_height;
			
			// Change Height of Container
			ts.stop().animate({height: next_height}, 500, 'easeInBack');
			
			// Positioning
			var go_left = current_slide.width() + parseInt(ts.width() / 2 - current_slide.width() / 2, 10);
			
			
			// Start Change
			next_slide.show();			
			
			/* Left to the Right */
			
			if( _out > _in )
			{
				current_slide.css({top: -next_slide.height(), position: 'relative'});
				
				// Current Slide Elements - OUT
				var cs_elements = current_slide.find('*:not(.xc)');
				cs_elements.css({position: 'relative'});
				
				cs_elements.each(function(i){
					var $this = $(this);
					
					var time_to_animate = randomFromTo(min_time, max_time);
					var move_left = ts.width() + win_ts_gap_size + $this.get(0).offsetWidth;
					
					$this.stop().animate({left: turn*move_left}, time_to_animate, easingIn);
				});
				
				// Next Slide Elements - IN
				var ns_elements = next_slide.find('*:not(.xc)');
				ns_elements.css({position: 'relative'});
				
				ns_elements.each(function(i){
									
					var $this = $(this);
					
					var time_to_animate = randomFromTo(min_time, max_time);
					var move_left = ts.width() + win_ts_gap_size + $this.get(0).offsetWidth;
					
					$this.css({left: -move_left});
					
					$this.stop().animate({left: 0}, time_to_animate, easingOut);
				});
				
				// Clear
				setTimeout(function(){
					current_slide.hide();
					
					cs_elements.css({left: 0});
					next_slide.css({top: 0}).show();
					current_slide.css({top: 0})
					
					busy = false;
				}, max_time);
				
				return false;
			}
			
			/* Right to the Left */	
			
			next_slide.css({
				position: "relative",
				top: -(turn == -1 ? 0 : current_slide.outerHeight()),
				left: 0
			});
			
			
			// Current Slide Elements - OUT
			var cs_elements = current_slide.find('*:not(.xc)');
			cs_elements.css({position: 'relative'});
			
			cs_elements.each(function(i){
				var $this = $(this);
				
				var time_to_animate = randomFromTo(min_time, max_time);
				var move_left = ts.width() + win_ts_gap_size + $this.get(0).offsetWidth;
				
				$this.stop().animate({left: -turn*move_left}, time_to_animate, easingIn);
			});
			
			// Next Slide Elements - IN
			var ns_elements = next_slide.find('*:not(.xc)');
			ns_elements.css({position: 'relative'});
			
			ns_elements.each(function(i){
								
				var $this = $(this);
				
				var time_to_animate = randomFromTo(min_time, max_time);
				var move_left = ts.width() + win_ts_gap_size + $this.width();
				
				$this.css({left: turn*move_left});
				
				$this.stop().animate({left: 0}, time_to_animate, easingOut);
			});
			
			// Clear
			setTimeout(function(){
				current_slide.hide();
				
				cs_elements.css({left: 0});
				next_slide.css({top: 0}).show();
				current_slide.css({top: 0})
				
				//ts.height("auto");
				busy = false;
			}, max_time);
			
		}
		
		$.fn.extend({
			trokitSlider: function(options){
				
				return this.each(function(){
					
					ts = $(this);
					
					ts.css({position: 'relative'}).children().each(function(i){
						
						var slide = $(this);
						slide.trokitPrepareSlide();
						slides.push(slide);
						
					}).hide();
					
					
					slides_total = slides.length;
					
					// Add Navigation
					if( slides_total > 1 )
					{
						$(ts).trokitSliderNavigation();
					}
					
					// First Slide Options
					var first_slide = getSlide(0);
					first_slide.show();
					
					var default_height = parseInt( first_slide.attr("data-slider-height"), 10);
					var background = first_slide.attr('data-bg-color') +  " url(" + first_slide.attr("data-bg-img") + ")";
					
					ts.css("background", background);
					
					if( default_height )
					{
						ts.height(default_height);
					}
					
					// Background Mask
					bg_mask = $('<div class="trokit_bg_mask"></div>');
					first_slide.before(bg_mask);
					
					bg_mask.css({
						position: 'absolute',
						top: 0,
						left: 0,
						width: $(window).width(),
						height: ts.height()
					});
					
					$(window).resize(function(){
						bg_mask.width( $(this).width() );
					});
				});
			},
			
			trokitNextSlide: function(){	
				
				if( busy )
					return false;
					
				var to_go_index = current_index;
				
				current_index++;
				current_index = current_index % slides_total;
				
				changeSlide(to_go_index, current_index);
				
			},
			
			trokitPrevSlide: function(){
			
				if( busy )
					return false;
					
				var to_go_index = current_index;
					
				current_index--;
				
				if( current_index < 0 )
					current_index = slides_total - 1;
				
				changeSlide(to_go_index, current_index);
			},
			
			trokitPrepareSlide: function(){
			
				var $this = $(this);
				
				var text = $this.find('.text');
				var image = $this.find('.image');
				
				var container_height = $this.outerHeight();
				var text_height = text.outerHeight();
				
				var default_height = $this.attr("data-slider-height");
				var text_color = $this.attr('data-text-color');
				var text_shadow = $this.attr('data-text-shadow');
				
				if( text_color )
				{
					text.find('p,h1,span').css("color", text_color);
				}
				
				if( text_shadow )
				{
					text.find('p,h1,span').css("text-shadow", text_shadow);
				}
				
				if( default_height )
				{
					text.css({marginTop: parseInt(default_height/2 - text_height/2)});
					
					if( image.length > 0 )
					{
						var img_height = image.height();
						image.css({marginTop: parseInt(default_height/2 - img_height/2)});						
					}
					return;
				}
				
				if( container_height > text_height )
				{
					text.css({marginTop: parseInt(container_height/2 - text_height/2)});
				}
			},
			
			trokitSliderNavigation: function()
			{
				var nav_list = $('<ul class="trokit_navigation"></ul>');
				
				var prev = $('<li class="prev"><a href="#">Prev</a></li>');
				var next = $('<li class="next"><a href="#">Next</a></li>');
				
				prev.click(function(ev){
					ev.preventDefault();
					
					$(ts).trokitPrevSlide();
				});
				
				next.click(function(ev){
					ev.preventDefault();
					
					$(ts).trokitNextSlide();
				});
				
				nav_list.append(prev);
				nav_list.append(next);
				
				ts.append(nav_list);
				
				nav_list.show().fadeTo(0, 0);
				
				ts.hover(
				function(){
					nav_list.stop().fadeTo(500, 1, 'easeInSine');
				},
				function(){
					nav_list.stop().fadeTo(500, 0, 'easeInSine');
				});
			}
			
		});
	})(jQuery);
