var $akonda;

(function($) {

	var bindedFunctionStorage = [];
	
	function makeDump(message) {
		
		
	}
		
	function akonda() {
		
		var self = this;
		
		self.version = '0.1';
		
		self.getConf = function(name, type) {
			
			if ((type == 'local' || type == null) && window.localStorage) {
				
				var conf = localStorage[name];
			}
			else if (!window.localStorage) {
				
				
			}
			
			if ((type == 'session' || type == null) && window.sessionStorage) {
				
				var conf = sessionStorage[name];
			}
			else {
				
				
			}
			
			return conf;
		};
		
		self.setConf = function(name, value, type) {
			
			if (type == null)
				type = 'session';
			
			if (type == 'session' && window.xsessionStorage) {
				
				sessionStorage[name] = value;
			}
			else if (type == 'local' && window.xlocalStorage) {
				
				localStorage[name] = value;
			}
			else {
				
				var cookieName = (type == 'session') ? 'akonda_session_storage' : 'akonda_local_storage';
				var maxAge = (type == 'session') ? '' : ';max-age=' + (60*60*24*365);
				
				allCookies = document.cookie;
				var akondaConf = {};
				akondaConf[name] = value;
				var allCookies = allCookies.split(';');
				
				for (var i = 0; i < allCookies.length; i++) {
					
					var split = allCookies[i].split('=', 2);
					if (split[0] == cookieName) {
						
						akondaConf = $.extend(akondaConf, $.parseJSON(split[1]));
						
						break;
					}
				}
				
				document.cookie = cookieName + '=' + $.toJSON(akondaConf) + ';path=/' + maxAge;
			}
		};
		
		self.bindFunction = function(selector, name, func) {
			
			if (typeof(selector) != 'string')
				makeDump('Selector isn\'t valid');
			
			if (typeof(name) == 'function') {
				
				func = name;
				name = 'funcbind-' + bindedFunctionStorage.length;
			}
			
			bindedFunctionStorage.push({
				
				'selector': selector,
				'func': func,
				'name': name
			});
			
			return name;
		};
		
		self.unbindFunction = function(selector, name) {
			
			for (var i = 0; i < bindedFunctionStorage.length; i++) {
				
				if (bindedFunctionStorage[i].selector == selector) {
					
					if (typeof(name) == 'string' && bindedFunctionStorage[i].name == name) {
						
						bindedFunctionStorage[i] = null;
					}
				}
			}
		};
		
		self.doFunctionActions = function() {
			
			for (var i = 0; i < bindedFunctionStorage.length; i++) {
				
				$(bindedFunctionStorage[i].selector).each(bindedFunctionStorage[i].func);
			}
		};
		
		self.photoStack = function() {
			
			var selector = $(this).attr('data-stack-selector');
			if (selector == "")
				selector = 'div';
			
			var max = 5;
			var min = -5;
			
			var prefix = '';
	        if (window.opera)
	            prefix = '-o-';
	        else if(window.mozInnerScreenX)
	            prefix = '-moz-';
	        else
	            prefix = '-webkit-';

			$(this).find(selector).each(function() {
				
		        $(this).css(prefix + 'transform', 'rotate(' + (Math.floor(Math.random() * (max - min + 1)) + min) + 'deg)');
			});
		}
	}
	

	$akonda = new akonda();
	
	$(function() {
		
		$akonda.bindFunction('.akonda_photostack', $akonda.photoStack);
		$akonda.doFunctionActions();
	});

})(jQuery)

	/**
	 * Akonda Slideshow
	 */
	function akondaSlider(options) {
		
		options = $.extend({
			
			selector: '.akondaSlider',
			direction: 'vertical',
			devmode: false,
			sliderid: '',
			template: '.akondaSlider .akondaSliderTemplate',
			loadItemsUrl: ''
			
		}, options);
		
		
	
		var self = this;
		self.version = 0.1;
		self.loadDone = false;
		
		self.setup = function() {
			
			self.devmode = options.devmode && "console" in window ? true : false;
			
			if (self.devmode) {
				
				console.log('Slider: ', options.selector);
			}
			
			self.slider = $(options.selector).scrollable({
				
				api: true,
				vertical: options.direction == 'vertical' ? true : false,
				mousewheel: true,
				prev: '',
				next: '',
				items: '.slideshowSlider',
				onSeek: function() {
					
					//Check num of elements
					var numItems = this.getSize();
					var curItem = this.getIndex();
					var numPreload = 5;
					
					if (numItems - numPreload <= curItem) {
						
						//Load next 5 items
						self.loadItems(numItems, 5);
					}
				}
			});
			
			$(options.template).hide();
			
			if (self.slider.getSize() >= 25) {
				
				self.loadDone = true;
			}
		};
		
		self.loadItems = function(offset, limit) {
			
			$.post(options.loadItemsUrl.replace('%offset%', offset).replace('%limit%', limit), function() {
				
				if (data.file_listing && data.file_listing.length > 0) {
					
					for (var i = 0; i < data.file_listing; i++) {
						
						self.slider.addItem(self.createItem(data.file_listing[i]));
					}
				}
				else {
					
					self.loadDone = true;
				}
				
			}, 'json');
		};
		
		self.createItem = function(fileData) {
			
			var tmpl = $(options.template).html();
			
			for (var e in fileData) {
				
				tmpl = tmpl.replace('${' + e + '}', fileData[e]);
			}
			
			return $(tmpl);
		};
		
		self.insertItem = function(position, item) {
			
			if (position == 'prepend') {
				
				$(item).prependTo(self.slider.getItemWrap());
			}
			else if (position == 'append') {
				
				$(item).appendTo(self.slider.getItemWrap());
			}
			else {
				
				if (self.devmode) {
					
					console.warning('Sorry, add items on this position isn\'t implemented yet!', position);
				}
			}
		};
		
		this.setup();
		
		return this;
	}
	
