/**
 * JavaScript Image and text cycle
 * Pull content from CMS and cycle image together with texts
 * @author Daniel Djurfelter (daniel.djurfelter at atrox.se)
 * @version 1.0
 */

var jsus = {
	slider : {
		// Cacheable cycle object 
		objCycle : null,
		
		/**
		 * Circle images along with texts of a project
		 * @method cycle.init()
		 * @param objOpts (object), the properties object
		 */
		init : function(objOpts){
			
			// Assign Cacheable object
			jsus.slider.objCycle = {
				texts : $(objOpts.texts),
				currIndex : 0,
				clicked : false
			};
			
			// Init the image cycle
			jQuery(objOpts.images).cycle({
				fx: 'fade',
				timeout: 4000,
				pager: objOpts.navigation,
				before : function(){
					if(!jsus.slider.objCycle.clicked && jsus.slider.objCycle.texts.length > 0){
						if(jsus.slider.objCycle.currIndex > jsus.slider.objCycle.texts.length-1){
							jsus.slider.objCycle.currIndex = 0;
						}
						
						jsus.slider.objCycle.texts.hide();
						$(jsus.slider.objCycle.texts[jsus.slider.objCycle.currIndex]).show();
						jsus.slider.objCycle.currIndex++;
					};
				}
			});
			
			// Make navigation trigger text shifts
			$(objOpts.navigation + " > a").click(function(){
				jsus.slider.objCycle.clicked = true;
				
				var arrNavs = $(this).parent().children();
				
				jsus.slider.objCycle.texts.hide();
				
				var index = 0;
				
				for(var i = 0; i < arrNavs.length; i++){
					if($(arrNavs[i]).hasClass("activeSlide")){
						$(jsus.slider.objCycle.texts[i]).show();
						jsus.slider.objCycle.currIndex = i+1;
						break;
					}
				}
				
				jsus.slider.objCycle.clicked = false;
			});
		}	
	}
};
