/*!
 * jQuery GoGallery v1.0
 * http://review.yourifteam.com/dev/plugins/gogallery/
 *
 * Copyright 2011, Steven Crocker
 * Released under the MIT, BSD, and GPL Licenses.
 *
 * Date: TIMESTAMP      
 */
 
(function($){
	var methods = {
		init : function(options) {
			return this.each(function(){
				$(this).data('settings', {
					left: 'auto',
					top: 'auto',
					interval: 5000,
					speed: 500,
					timer: false,
					active: false,
					gallery: false,
					overlay: false,
					caption: false,
					thumbs: false
				});
				var me = $(this);
				var $$ = me.data('settings'), active=true;
				if(options) $.extend($$,options);
				if($$['overlay']) {
					var img = new Image();
					$(img).load(function(){
						$('<div style="position:absolute;left:0px;top:0px;width:'+this.width+'px;height:'+this.height+'px;z-index:2;background:url('+this.src+');" class="goOverlay" />').appendTo(me);
					}).attr({src:$$['overlay']});
				}
				for(key in $$['gallery']) {
					var gallery = $$['gallery'][key];
					for(var i=0;i<gallery.length;i++) {
						if(typeof gallery[i].begin != 'undefined') {
							var begin = gallery[i].begin, end = gallery[i].end;
							delete gallery[i].begin; delete gallery[i].end;
							var template = $.extend({},gallery[i]), shash = template.src.replace(/[^#]*(#*).*/,'$1'), chash = template.caption.replace(/[^#]*(#*).*/,'$1');
							for(var j=0;j<1+(end-begin);j++) {
								var update=$.extend({},template);
								update.src=update.src.replace(/([^#]*)#*(.*)/,'$1'+(((((j+begin)+'').length-shash.length)>0)?(j+begin):((shash.replace(/#/g,'0') + (j+begin)).slice(-shash.length)))+'$2');
								update.caption=update.caption.replace(/([^#]*)#*(.*)/,'$1'+(((((j+begin)+'').length-chash.length)>0)?(j+begin):((chash.replace(/#/g,'0') + (j+begin)).slice(-chash.length)))+'$2');
								gallery.splice(i+j,Math.max(0,1-j),update);
							}
						}
					}
					var img = new Image();
					$(img).load(function(){
						$('<div style="position:absolute;left:0px;top:0px;width:'+this.width+'px;height:'+this.height+'px;z-index:0;" class="goGallery go'+$(this).data('key')+'"><img class="gomain" style="position:absolute;left:0px;top:0px;width:100%;height:100%;border:none;" src="'+this.src+'"/><img class="goqueue" style="position:absolute;left:0px;top:0px;width:100%;height:100%;border:none;opacity:0;filter:alpha(opacity=0);" src="'+this.src+'"/></div>').css({display:$(this).data('active')?'block':'none'}).data({current:0,count:$(this).data('count')}).prependTo(me);
						if($$['caption']) { $($$['caption']).html(gallery[0].caption); }
						if($$['thumbs']) $($$['thumbs']).find('div:eq(0)').addClass('active');
					}).data({active:active,key:key,count:gallery.length}).attr({src:gallery[0].src});
					if(active) $$['active']=key;
					active = false;
				}
				$$['timer'] = methods['timer'].apply(me,Array($$['interval'],function(){ methods['next'].apply(me);}));
			});
		},
		load : function (frame) {
			var me = $(this);
			var $$ = me.data('settings');
			var active = me.find('.go'+$$['active']);
			var img = new Image();
			var f = frame;
			var frame = $$['gallery'][$$['active']][frame];
			if($$['caption']) { $($$['caption']).html(frame.caption); }
			$(img).load(function(){
				var queue = active.find('> .goqueue');
				var main = active.find('> .gomain');
				main.attr({src:queue.attr('src')});
				queue.stop().css({opacity:0});
				queue.attr({src:this.src}).stop().animate({opacity:1},$$['speed'],function(){
					main.attr({src:this.src});
					queue.stop().animate({opacity:0},$$['interval']/2);
				});
				if($$['thumbs']) $($$['thumbs']).find('div:eq('+f+')').addClass('active').siblings().removeClass('active');
			}).attr({src:frame.src});
			if($$['timer']) $$['timer'].start();
		},
		stop : function() {
			var me = $(this);
			var $$ = me.data('settings');
			if($$['timer']) $$['timer'].stop();
		},
		start : function() {
			var me = $(this);
			var $$ = me.data('settings');
			if($$['timer']) $$['timer'].start();
		},
		goto : function (frame) {
			var me = $(this);
			var $$ = me.data('settings');
			var active = me.find('> .go'+$$['active']);
			active.data('current',frame-1);
			methods['load'].apply(me,Array(active.data('current'),0));
		},
		prev : function () {
			var me = $(this);
			var $$ = me.data('settings');
			var active = me.find('> .go'+$$['active']);
			active.data('current',active.data('current')-1);
			if(active.data('current')<0) active.data('current',active.data('count')-1);
			methods['load'].apply(me,Array(active.data('current'),0));
		},
		next : function () {
			var me = $(this);
			var $$ = me.data('settings');
			var active = me.find('> .go'+$$['active']);
			active.data('current',active.data('current')+1);
			if(active.data('current')==active.data('count')) active.data('current',0);
			methods['load'].apply(me,Array(active.data('current'),0));
		},
		prevgallery : function () {
			var me = $(this);
			var $$ = me.data('settings');
			alert('prev gallery');
		},
		nextgallery : function () {
			var me = $(this);
			var $$ = me.data('settings');
			var active = me.find('> .go'+$$['active']);
//			active.next
//			if(active.next('.goGallery').hasClass('goGallery')) {
//				active =  
//			}
//			alert(active.next().next().html());
		},
		timer : function (val, cb) {
			var val = val || 100;
			_timer = function(val, cb) {
				var timer = this;	
				timer.val = val;
				timer.stop = function () { clearInterval(timer.id); };
				timer.internalCallback = function () { cb(timer); };
				timer.start = function (val) { if (timer.id) clearInterval(timer.id); this.id = setInterval(this.internalCallback, timer.val); };
				timer.id = setInterval(timer.internalCallback, timer.val);
			}
			return new _timer(val, cb);
		}
	};
	$.fn.gogallery = function(method) {
		if (methods[method]) { return methods[method].apply(this,Array.prototype.slice.call(arguments,1)); }
		else if (typeof method==='object'||!method){ return methods.init.apply(this,arguments); }
		else { $.error('Method '+method+' does not exist!'); }
	};
})( jQuery );



function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}
