(function($) {	
	jQuery.fn.qlgallery = function(options)
	{
		settings = jQuery.extend({
			speed: 600,
			reveal: 600,
			easing: false
		}, options);

		return this.each(
			function(){
				if($(this).attr("id") && $(this).length == 1){					
					var images = $(this).children(),
					parent = $(this),
					pid = $(this).attr("id"),
					galWidth = $(parent).width(),
					galHeight = $(parent).height(),
					viewLength = (images.length * galWidth);

					var qlGal = {
						controls: [
							'<a class="back '+pid+'" href="#">- Prev</a>',
							'<a class="next '+pid+'" href="#">Next -</a>'
							//'<a class="full '+pid+'" href="#">Full Size</a>'
						],
						init: function(){
							try{
								$.each(qlGal.controls, function(i,x){
									$(parent).append(x);
								});
								
								$(parent).after('<div class="capt '+pid+'"><span></span></div>');

								images.wrapAll('<div class="viewer '+pid+'" />');
								$('.viewer.'+pid).css({'width': viewLength+'px'});
								
								var cap = $(images).first().attr('alt');
								
								if(cap !== ""){
									$('div.capt.'+pid+' span').text(cap);
	                                $('div.capt.'+pid).fadeIn(settings.speed);
	                            } else {
									$('div.capt.'+pid+' span').text(cap);
									$('div.capt.'+pid).fadeOut(settings.speed);
								}
								
								$('a.back.'+pid).bind('click', qlGal.next);
								$('a.next.'+pid).bind('click', qlGal.next);
								$('a.full.'+pid).bind('click', qlGal.full);
								$('#'+pid+' img').bind('click', qlGal.auto);
							} catch(e){
								var msg = 'There was an error initializing the jquery.qlgallery plugin.\n'+e;
								if(window.console){
									console.debug(msg);
								}else{
									alert(msg);
								}
							}

						}, 
						next: function(){
							$(this).unbind('click');
							$(this).bind('click', function(){return false;});
                            
							var diff = qlGal.diff(),
							object = $(this),
                            speed = (settings.speed / 2),
                            offset, direction, forward, back;
                            
                            if($(this).hasClass('back')){
								if(diff.position === 0){
									offset = (viewLength - galWidth);
								} else {
									offset = (diff.position + galWidth);
								}
                                
                                if(diff.rel !== viewLength){
								    back = {'left': offset+'px'};
							    }else{
								    back = {'left': (galWidth - viewLength)+'px'};
								}
							    
							    direction = back;
							} else {
							    offset = (diff.position - galWidth);
							    if(diff.rel !== galWidth){
									forward = {'left':(diff.position - galWidth )+'px'};
								} else {
									forward = {'left':0+'px'};
								}
						
							    direction = forward;
							} 
                            $('div.capt.'+pid).fadeOut(settings.speed);
							$('.viewer.'+pid).animate(
								direction, 
								settings.speed, 
								settings.easing, 
								function(){
									qlGal.setHeight(offset);
									$(this).unbind('click');
									$(object).bind('click', qlGal.next);
									
								}
							);
							
							return false;
						},
						full: function(){
							var diff = qlGal.diff(),
							offset = Math.abs(diff.position),
							getHeight = qlGal.getHeight(offset),
							currentHeight = $(parent).height(),
							imgHeight = getHeight.current,
							fullHeight = getHeight.full;
							
							if(imgHeight == galHeight){
								$('.full.'+pid).animate({"text-indent": '-4px'}, 50)
									.animate({"text-indent": '1px'}, 80)
									.animate({"text-indent": '-4px'}, 50)
									.animate({"text-indent": '1px'}, 80)
									.animate({"text-indent": '-4px'}, 50)
									.animate({"text-indent": '0px'}, 30);
							} else {

								var direction;
								if(imgHeight == fullHeight && currentHeight == galHeight) {
									direction = [{'height': fullHeight+'px'}, "Minimize"];
								}else if (imgHeight < fullHeight && currentHeight !== imgHeight) {
									direction = [{'height': imgHeight+'px'},"Minimize"];
								}else if (imgHeight < fullHeight && currentHeight == imgHeight){
									direction = [{'height': galHeight+'px'},"Full Size"];
								}else {
									direction = [{'height': galHeight+'px'},"Full Size"];
								}
								
								$(parent).animate(
									direction[0], 
									settings.reveal, 
									settings.easing, 
									function(){
										$('.full.'+pid).text(direction[1]);
									}
								);
							}
							
							return false;
						},
						diff: function(){
							
							var left = $('.viewer.'+pid).position().left;
							
							var rel = Math.floor(left + viewLength);
							
							if(jQuery.browser.version == "8.0"){
								if(left < -1){
									left = left - 1;
								}
								
								rel = Math.floor(left + viewLength);

							}
							
							var obj = {
								position: left,
								rel: rel
							};
							
							return obj;
						},
						setHeight: function(offset){
							var getHeight = qlGal.getHeight(offset),
							currentHeight = getHeight.current,
							fullHeight = getHeight.full,
							cap = $(getHeight.img).attr('alt'),
							speed = (settings.speed / 2);  

							$(parent).animate(
								{'height': currentHeight+'px'}, 
								settings.reveal, 
								settings.easing, 
								function(){
									$('.full.'+pid).text('Minimize');
								}
							);
							
							if(cap !== ""){
                                $('div.capt.'+pid+' span').text(cap);
								$('div.capt.'+pid).fadeIn(speed);
                            } else {
								$('div.capt.'+pid+' span').text(cap);
								$('div.capt.'+pid).fadeOut(speed);
							}
						},
						getHeight: function(offset){
							var imgWidth = $('.viewer.'+pid+' img:nth-child(1)').width(),
							posOff = Math.abs(offset),
							curOff = (posOff / imgWidth)+1;

							if(curOff > images.length){
								curOff = 1;
							}

							var curImage = $('.viewer.'+pid+' img:nth-child('+curOff+')'),
							curHeight = $(curImage).height(),
							fullHeight = $('.viewer.'+pid).height();

							var obj = {
								full : fullHeight,
								current : curHeight,
								img : curImage,
								off : curOff,
								width : imgWidth
							};

							return obj;
						},
						auto : function(){
						    $('a.next.'+pid).trigger('click');
						    return false;
						}
					};
					return qlGal.init();
				} else {
					var msg = 'There was an error loading the jquery.qlgallery plugin.';
					if(window.console){
						console.log(msg);
					}else{
						alert(msg);
					}
				}
			});
	};
	jQuery.fn.qlscroll = function(){
		
		this.each(function(){
			
			$(this).click(function(){
				
				var name = $(this).attr('href').split('#'),
				off = $("a[name='"+name[1]+"']").offset().top,
				windowHeight = $(window).height(),
				posOff = ($('html,body').height() - off);
				
				if(jQuery.browser.version == "7.0" || jQuery.browser.version == "6.0"){
					$('html,body').animate(
						{'scrollTop': off-60 }, "slow"
					);
				} else {
					if(posOff < windowHeight){
						$('html,body').animate(
							{'scrollTop': off }, "slow"
						);
					} else {
						$('html,body').animate(
							{'scrollTop': off-47 }
						);
					}
				}

				return false;
			});
		});
		
	};
})(jQuery);
