/*
 *
 *
 * Pomilioblumm.it Engine -> Pengine 0.4
 *
 *
 */
(function($){
    $.fn.pengine = function(options){
        /*
         * OPZIONI
         * opzioni standard per modificare
         * il comportamento del plugin
         */
        var $o = {
            'type': 'scroll',
            'auto': false,
            'interval': 5000,
            'speed': 500,
            'start': 0,
            'selector': 'li',
            'active': 'active',
            'menu': '.penginemenu',
            'nav': '#nav'
        }
        if (options)$.extend($o, options);
		/*
		 * VERIFICA
		 * Verifica dell'esistenza del soggetto
		 * se esiste proseguire, altrimenti nada
		 */
		if ($(this).length !== 0){
	        /*
	         * LIBRERIA
	         * variabili e funzioni di base
	         */
	        var $t = (this);
	        var $tp = (this).parent();
	        var $if = $t.find('>ul>' + $o.selector + ':first-child');
			/*funzioni*/
	        $.fn.navonoff = function(nav){
	            ($(this).scrollTop() == 0 || $(this).scrollTop() == null) ? $(nav).find('#penginecontroll li.up').addClass('off') : $(nav).find('#penginecontroll li.up').removeClass('off');
				(($(this).attr('scrollHeight') - $(this).height() - $(this).scrollTop()) == 0 || $(this).scrollTop() == null) ? $(nav).find('#penginecontroll li.down').addClass('off') : $(nav).find('#penginecontroll li.down').removeClass('off');
	        }
			$.fn.navonoffscr = function(nav){
				($(this).scrollLeft() == 0) ? $(nav).find('#penginecontroll li.left').addClass('off') : $(nav).find('#penginecontroll li.left').removeClass('off');
				($(this).attr('scrollWidth') - $(this).width() - $(this).scrollLeft()) == 0 ? $(nav).find('#penginecontroll li.right').addClass('off') : $(nav).find('#penginecontroll li.right').removeClass('off');
				($t.find('#pengineframe').scrollLeft()==0) ? $(nav).find('#penginecontroll li.back').addClass('off') : $(nav).find('#penginecontroll li.back').removeClass('off');
			}
			$.fn.navonoffsub = function(nav){
				($(this).scrollLeft() == 0) ? $(nav).find('#penginesubcontroll li.left').addClass('off') : $(nav).find('#penginesubcontroll li.left').removeClass('off');
				($(this).attr('scrollWidth') - $(this).width() - $(this).scrollLeft()) == 0 ? $(nav).find('#penginesubcontroll li.right').addClass('off') : $(nav).find('#penginesubcontroll li.right').removeClass('off');
				($(this).has('ul>li>div.pengineinfo').length==1) ? $(this).find('ul>li>div.pengineinfo').slideUp()+$(nav).find('#penginesubcontroll li.info').removeClass('off') : $(nav).find('#penginesubcontroll li.info').addClass('off');
			}
			$.fn.navigationbar = function(){
				$e='<ul id="penginecontroll"><li class="back"><a>back</a></li><li class="up"><a>Up</a></li><li class="down"><a>Down</a></li><li class="left"><a>Left</a></li><li class="right"><a>Right</a></li></ul><ul id="penginesubcontroll"><li class="back"><a>back</a></li><li class="up off"><a>Up</a></li><li class="down off"><a>Down</a></li><li class="left"><a>Left</a></li><li class="right"><a>Right</a></li><li class="info"><a>info</a></li></ul>';
				$(this).css({'position':'relative'}).append($e).slideDown();
				$('ul#penginecontroll,ul#penginesubcontroll').css({'position':'absolute','display':'none'});
				$('ul#penginecontroll').slideDown($o.speed);
			}
			/*
			 * libreria
			 */
			var $chn = $t.children().length;
			var box='#pengineframe>div>.' + $o.active + '>li';
			/*
			 * Azioni di base
			 */
			$t.children().css({'width':$t.width(),'height':$t.height(),'overflow':'hidden','float':'left'}).wrapAll('<div id="pengineframe" style="height:'+$t.height()+'px;width:'+$t.width()+'px;overflow:hidden;float:left;"><div class="penginecut" style="float:left;height:'+$t.height()+';width:'+$t.width()*$chn+'px;">');
			$(this).find('ul>li>div.pengineinfo').css('display','none');
	        /*
	         * TYPE:SCROLL
	         * navigazione manuale dei contenuti
	         * scelta di default
	         */
	        if ($o.type == 'scroll') {
				/*
				 * Azioni di base
				 */
				$('#pengineframe>div>ul>li>ul').wrap('<div class="penginesubcut" style="display:none;width:624px;height:'+$t.height()+'px;position:absolute;top:0;left:0;overflow:hidden;">');
				$(this).parent().append('<div id="nav">');
				$($o.nav).navigationbar();
				$t.find('#pengineframe').navonoffscr($o.nav);
				$t.find('#pengineframe>div>:first-child').addClass($o.active).navonoff($o.nav);
				/*
	             * Cliccando il riquadro
	             */
	            $(box+' a[rel=penginebutton]').live('click', function(){
					$('#pengineframe>div>.' + $o.active).removeClass('active');
					$($(this).attr('href')).addClass('active');
					var target = $($(this).attr('href')).position().left;
					//console.log(target);
					$t.find('#pengineframe').animate(
						{scrollLeft: '+=' + target},
						$o.speed,
						function(){$(this).navonoffscr($o.nav)}
					);
					return false;
	            })
				/*
	             * Cliccando il riquadro
	             */
				 $(box+'>.'+ $o.active +' a[rel=penginebuttonz]').live('click', function(){
					//$(box+'>.'+ $o.active)//.removeClass('active');
					//$($(this).attr('href')).addClass('active');
					var target = $($(this).attr('href')).position().left;
					//console.log(target);
					$t.find('.penginesubcut.'+$o.active).animate(
						{scrollLeft: '+=' + target},
						$o.speed,
						function(){$(this).navonoffsub($o.nav)}
					);
					return false;
	            })
				/*
	             * Cliccando il riquadro
	             */
	            $(box+'>a[rel=penginesubutton]').live('click', function(){
					var $ul=$(this).parent().find('>div.penginesubcut>ul');
					var $ulw=$t.width()*$ul.children().length;
					$ul.css({'width':$ulw});
					$(this).parent().find('>div.penginesubcut').addClass($o.active).slideDown($o.speed).scrollLeft(0).navonoffsub($o.nav);
	                $('ul#penginesubcontroll').addClass($o.active).slideDown($o.speed);
	            })
				/*
				 * Back
				 */
				 $($o.nav).find('#penginecontroll li.back:not(.off)').live('click', function(){
					//var $scroll = $(this).hasClass('left') ? '-' + $t.width() : '+' + $t.width() ;
					//var $go = $(this).hasClass('left') ? $t.find('#pengineframe>div>ul.'+$o.active).prev() : $t.find('#pengineframe>div>ul.'+$o.active).next();
					$t.find('#pengineframe').animate(
						{scrollLeft: '+=-' + $t.find('#pengineframe').scrollLeft()},
						$o.speed,
						function(){$(this).navonoffscr($o.nav)}
					);
					$t.find('#pengineframe>div>.'+$o.active).removeClass($o.active);
					$t.find('#pengineframe>div>:first-child').addClass($o.active).navonoff($o.nav);
				})
	            /*
	             * Cliccando il menu #nav UP e DOWN
	             */
	            $($o.nav).find('#penginecontroll li.up:not(.off),#penginecontroll li.down:not(.off)').live('click', function(){
	                var $hscroll = $(box+':first-child').height();
	                var $scroll = $(this).hasClass('up') ? '-' + $hscroll : '+' + $hscroll;
	                $('#pengineframe>div>.' + $o.active).animate(
						{scrollTop: '+=' + $scroll},
						$o.speed,
						function(){$(this).navonoff($o.nav)}
					);
	            })
				/*
				 * Cliccando il menu #nav LEFT RIGHT
				 */
	            $($o.nav).find('#penginecontroll li.left:not(.off),#penginecontroll li.right:not(.off)').live('click', function(){
					var $scroll = $(this).hasClass('left') ? '-' + $t.width() : '+' + $t.width() ;
					var $go = $(this).hasClass('left') ? $t.find('#pengineframe>div>.'+$o.active).prev() : $t.find('#pengineframe>div>ul.'+$o.active).next();
					$t.find('#pengineframe').animate(
						{scrollLeft: '+=' + $scroll},
						$o.speed,
						function(){$(this).navonoffscr($o.nav)}
					);
					$t.find('#pengineframe>div>.'+$o.active).removeClass($o.active);
					$go.addClass($o.active).navonoff($o.nav);
				})
				/*
				 * Cliccando il submenu #nav
				 */
	            $($o.nav).find('#penginesubcontroll li.left:not(.off),#penginesubcontroll li.right:not(.off)').live('click', function(){
					var $scroll = $(this).hasClass('left') ? '-' + $t.width() : '+' + $t.width() ;
					$('.penginesubcut.' + $o.active).animate(
						{scrollLeft: '+=' + $scroll},
						$o.speed, 
						function(){$(this).navonoffsub($o.nav)}
					);
				})
	            /*
	             * Cliccando su info mostra
	             * <div class="pengineinfo"></div>
	             */
	            $($o.nav).find('#penginesubcontroll li.info').live('click', function(){
	                $(box + '>div.penginesubcut.' + $o.active + ' ul>li>.pengineinfo').slideToggle($o.speed);
	            })
	            /*
	             * Chiudendo sottomenu
	             */
	            $($o.nav).find('#penginesubcontroll li.back').live('click', function(){
	                $('ul#penginesubcontroll,' + box + '>div.penginesubcut.' + $o.active).removeClass($o.active).slideUp($o.speed);
	            })
	        }
			/*
	         * TYPE:AUTO
	         * autoscroll
	         */
	        if($o.type=='auto'){
				var $if=$('.penginecut>:first-child').addClass($o.active);
				setInterval(function(){
					var $active=$t.find('.penginecut'+' .'+$o.active);
					var $go=$active.next().length ? $active.next() : $if;
					var target = $go.position().left;
					//console.log(target)
					$active.removeClass($o.active);
					$go.addClass($o.active);
					$t.find('#pengineframe').animate({scrollLeft:'+='+target},$o.speed);
					
				},$o.interval);
			}
	        /*
	         * ALTRE AZIONI DA DEFINIRE
	         */
    	}
	}
})(jQuery);
