/*
 * 	loopedSlider - jQuery plugin
 *	written by Nathan Searles	
 *	http://code.google.com/p/loopedslider/
 *
 *	Copyright (c) 2009 Nathan Searles (http://nathansearles.com/loopedslider/)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
(function($){
 	$.fn.extend({ 
 		loopedSlider: function(options) {
    		return this.each(function() {
				// set defaults
				var defaults = {
					container : 'container',
					slideClass : 'slide',
					pagination : 'pagination',
					navButtons : 'nav-buttons', 
					fadeSpeed : 400,
					slideSpeed : 1000,
					animateSpeed : 200,
					autoHeight : false,
					padding : 20,
					easing : 'easeOut',
					datafile : '/vkn/portfolio_data.html',
					imgdir : '/vkn/assets/snaps/',
					thumbprefix : 'thumb_',
					ext : '.jpg',
					multi : [
								['1200dreams','4stroke','8in8rate','dubell'],
								['gabriella','hsbc','imek_media','isend'],
								['lvintage','phaseII','prestigesecurity','cadlett']
							]
				};

				// set variables	
				var obj = $(this);
				var o = $.extend(defaults, options);
				var u = false;
				var w = obj.width(); 
				var h = obj.height();
				var f = $('.'+o.container, obj).find('div:first').attr('id');
				var l = $('.'+o.container, obj).find('div:last').attr('id');
				var page = 0;				
				var totalpages = o.multi.length;
		
				
				//$.preload([ 'red', 'blue', 'yellow' ], {
					//base:'images/colors/',
					//ext:'.jpg'
				//});
				// functions
				function setToActive(c) {
					var current = $(c).attr('id');
					$('a[href$="'+current+'"]', obj).addClass('active');
				}
				// applies style to divs
				$('.'+o.container, obj).find('div').css({ 'z-index': 0, opacity: 0 });
				
				// load first slide
				$('.'+o.container, obj).find('div:eq(0)').animate({ opacity: 1.0 }, o.fadeSpeed, function() {						
					$(this).css({ 'z-index': 80 });				
					$(this).addClass('current');
					if (o.autoHeight===true) {
						// gets height of new slide
						var newHeight = $(this, obj).height() + o.padding;
						$('.'+o.container, obj).animate({'height': newHeight}, o.animateSpeed, o.easing);
					}
					setToActive(this);
				});		
				// init
				$('.'+o.slideClass, obj).each(function(i){																
					$(this).css('background','url('+o.imgdir+ o.multi[page][i] + o.ext +') no-repeat');
					$(this).load(o.datafile +' #'+ o.multi[page][i]+' *');
				});	
				$('.'+o.pagination+' a', obj).each(function(i){
					$(this).html('');
					$(this).append('<img src="'+o.imgdir+ o.thumbprefix + o.multi[page][i] + o.ext +'" />');
				});
			
				// fade code
				$('.'+o.pagination, obj).find('a').click(function(){
					if(u===false  && ($(this).hasClass('active')===false)) {
						u = true;
						// removes active
						$('a', obj).removeClass('active');

						// fades out current slide
						$('.'+o.container, obj).find('div').animate({ opacity: 0 }, o.fadeSpeed, function() {					
							$(this).removeClass('current');
							$(this).css({ 'z-index': 0 });				
						});

						// setsup value for new slide
						var x = 0;
						var parentId = $(this).attr('href');
						var parentSplit = parentId.split('-');
						x = ((parentSplit[1]*1));
						
						if (o.autoHeight===true) {
							// gets height of new slide
							var newHeight = $('#'+o.slideClass+'-'+(x), obj).height() + o.padding;
							$('.'+o.container, obj).animate({'height': newHeight}, o.animateSpeed, o.easing);
						}
						
						// fades in new slide
						$('#'+o.slideClass+'-'+(x), obj).animate({ opacity: 1.0 }, o.fadeSpeed, function() {
							$(this).css({ 'z-index': 80 });
							$(this).addClass('current');
							u = false;
							setToActive(this);		
						});
					}
					return false;
				});
				
				// slide code
				$('.'+o.navButtons, obj).find('a').click(function(){
					if(u===false) {
						u = true;
						var loop = false;
						var fLoop = f;
						var lLoop = l;

						// removes active state
						$('a', obj).removeClass('active');

						// flips directions
						if ($(this).hasClass('next')) {
							var nextD = -w;
							var previousD = w;
							var direction = +1;
						}
						if ($(this).hasClass('previous')) {
							nextD = w;
							previousD = -w;
							direction = -1;
						}

						// setup the loop
						if ($('#'+fLoop, obj).hasClass('current')) {
							loop = 'first';
						}
						if (($('#'+lLoop, obj).hasClass('current'))) {
							loop = 'last';
						}

						// FIRST PAGE
						if ((loop==='first') && ($(this).hasClass('previous'))) {
							lLoop = lLoop.split('-');
							x = ((lLoop[1]*1));
							
							if(page == 0)
							{page = totalpages - 1;}
							else 
							{page = page - 1;}	
							
							$('.'+o.pagination+' a', obj).each(function(i){
								$(this).html('');
								$(this).append('<img src="'+o.imgdir+ o.thumbprefix + o.multi[page][i] + o.ext +'" />');
							});
							$('.'+o.slideClass, obj).each(function(i){
								if(i == 0)
								{}
								else 
								{								
									$(this).css('background','url('+o.imgdir+ o.multi[page][i] + o.ext +') no-repeat');
									$(this).load(o.datafile +' #'+ o.multi[page][i]+' *');
								}
								
							});
							
							//LAST PAGE					
						} else if ((loop==='last') && ($(this).hasClass('next'))) {
							fLoop = fLoop.split('-');
							x = ((fLoop[1]*1));
							
							if(page == totalpages - 1)
							{page = 0;}
							else 
							{page = page + 1;}
							$('.'+o.pagination+' a', obj).each(function(i){
								$(this).html('');
								$(this).append('<img src="'+o.imgdir+ o.thumbprefix + o.multi[page][i] + o.ext +'" />');
							});
							$('.'+o.slideClass, obj).each(function(i){
								if(i == 3)
								{}
							    else 
								{								
									$(this).css('background','url('+o.imgdir+ o.multi[page][i] + o.ext +') no-repeat');
									$(this).load(o.datafile +' #'+ o.multi[page][i]+' *');
								}
								
							});
							//alert('page '+page);
						} else {
							// setsup value for new slide
							var getCurrent = $('.'+o.container, obj).find('.current').attr('id');
							getCurrent = getCurrent.split('-');
							x = ((getCurrent[1]*1+direction));
						}
						
						
						// gets height of new slide
						if (o.autoHeight===true) {
							var newHeight = $('#'+o.slideClass+'-'+(x), obj).height() + o.padding;
							$('.'+o.container, obj).animate({'height': newHeight}, o.animateSpeed, o.easing);
						}
						
						// sets next slide to slide in position		
						$('#'+o.slideClass+'-'+(x), obj).css({ opacity: 1, left: previousD, 'z-index': 80 });
						$('#page_number').html(page+1);
						
						// slides in new slide					
						$('#'+o.slideClass+'-'+(x), obj).animate({ left: 0 }, o.slideSpeed, o.easing, function() {
							$(this).addClass('current');
							$(this).css({ opacity: 1 });
							u = false;	
							// Sets active state for pagination a
							setToActive(this);		
						});		
						
						// slides out current slide
						$('.'+o.container, obj).find('.current').animate({ 'left': nextD }, o.slideSpeed, o.easing, function() {					
							$(this).removeClass('current');
							$(this).css({ opacity: 0, left: 0, 'z-index': 0 });
							$('.'+o.slideClass, obj).each(function(i){																
									$(this).css('background','url('+o.imgdir+ o.multi[page][i] + o.ext +') no-repeat');
									$(this).load(o.datafile +' #'+ o.multi[page][i]+' *');
							});							
						});
					}
				return false;
				});
			});
    	}
	});
})(jQuery);