(function($){
	$.fn.extend({
		//plugin name - animatemenu
		animateColumn: function(options) {
			$('html').css({ overflow: 'hidden' });
			/**/
			var defaults = {
				columnToggle: true,
				showSpecificColum: 0, /* 0 = no column open without click, a number greater than zero determines the number of column to show*/
				animationDelaySpecificColum: 0,
				speedIn: 1500,
				speedOut: 1500,
				columnsClass: 'home-column',
				commandButtonClass: 'button-column',
				wrapperClass: 'columns-wrapper',
				maxColumnHeight: 1000
			};

			/* override default options if this one isn't empty*/
			var options = $.extend(defaults, options);

			/**/
			var currentOpened;
			var marginSet = false;
			var currentClicked;
			var currentColumnHeight;
			var _options = options;
			var obj = $(this);
			var isiPad = navigator.userAgent.match(/iPad/i) != null;
			var isiPad2 = navigator.userAgent.match("CPU OS 5_0") != null;
           /**/
		   if (isiPad2){
			var posbottom = '3600px';
		   }else{
			var posbottom = '100%';
		   }
           theSpecificColumn = $('.'+_options.showSpecificColumClass); // useless
           cols = $('.'+_options.columnsClass);
           openBtns = $('.'+_options.commandButtonClass);
       
           //metto la relazione alla colonna e ne setto l'altezza alla stessa del contento
			cols.each(function (index,elem) {
	      	   $(elem).attr('rel', index).css({ bottom: posbottom, overflow: 'hidden'});
			   $(elem).wrapInner('<div id="inner_col'+index+'" />');
			   setTimeout(function () { $(elem).css({ visibility: 'visible' }); },50);
			});
           var setCols = function () {
	           cols.each(function (index,elem) {
				   var nHeight = $('.inner_wrapper').outerHeight(); // per scendere sotto ai pulsanti del menu
				   
				   var oHeight = $(elem).find('.container').outerHeight();
	        	   $(elem).css({ height: nHeight+'px' });
				   $(elem).children().css({ height: nHeight+'px' });
				 
				   if (nHeight >= oHeight) {
						$(elem).children().css({ height: oHeight+'px' });
						$(elem).find('.dragger').hide();
				   } else {
						$(elem).find('.dragger_container').css({ height: nHeight+'px' });
					    $(elem).find('.dragger').show();
		        	   	
                        if (!isiPad) {
						  	$(elem).children().mCustomScrollbar("vertical",400,"easeOutCirc",1.10,"auto","yes",'no',0);
					   }
					}
	           });
			}
			setCols();
			
	  	   $(window).bind('resize', setCols);

		   if (isiPad) {
		        $('.dragger').remove();
				$(window).bind('orientationchange', function () {
				    setTimeout(function () {
						cols.css({ height: ($('inner_wrapper').outerHeight()-10)+'px' });
						cols.children().css({ height: ($('inner_wrapper').outerHeight()-10)+'px' })//;.mCustomScrollbar("vertical",400,"easeOutCirc",1.00,"auto","yes",'no',0);
					},50);
			   });
		   }
			var fullscreenHeight = function () {
				var myHeight = 0;
				if(typeof(window.innerHeight) == 'number') {
					myHeight = window.innerHeight;
				}
				else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
					myHeight = document.documentElement.clientHeight;
				}
				else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
					myHeight = document.body.clientHeight;
				}
				return myHeight;
			}

			var myHeight = fullscreenHeight();

			if (isiPad) {
				$('body').css({ height: myHeight + 'px' });
				$(window).bind('orientationchange', function () {
					var myHeight = fullscreenHeight();
					var view_h = myHeight - $('#header').outerHeight() - $('#footer').outerHeight();
					$('body').css({ height: myHeight+'px' });
					$('html').css({ height: myHeight+'px' });
					$('.inner_wrapper').css({ height: view_h+'px' });
				});
			

           /*	aggiungo il contenitore della freccia
           		metto la relazione dello span alla colonna */
			
				openBtns.find('li a').each(function(index,elem){
					$(elem).parent().append('<span class="colomn arrow" rel="'+index+'"></span>');
				});
			}else{
				openBtns.find('li a').each(function(index,elem){
						$(this).attr({'class':'colomn', 'rel' : index});
				
				//	$(elem).parent().append('<span class="colomn arrow" rel="'+index+'"></span>');
				});
			}
			
			var closeCols = function () {
				$(cols).each(function (idx,col) {
					if ($(col).hasClass('opened')) {
						$(this).removeClass('opened');
						$(this).animate({ bottom: posbottom });
					}
				});
			};

			var openCol = function (colIndex) {

				$(cols).each(function (idx,col) {
					if (idx == colIndex) {
						if (isiPad) {
								
									$(col).addClass('opened');
									$(col).css({ bottom: '0px' });
									$(col).removeClass('selected');
						}else{
							$(this).addClass('opened');
							$(this).animate({ bottom: '0px' });
							$(this).removeClass('selected');
							}
						}
				});
			};
			if (isiPad) {
				openBtns.find('li span').click(function () {
					if ($(this).hasClass('selected')) {
						$(this).removeClass('selected');
						closeCols();
						return;
					} else {
						openBtns.find('li span').each(function(idx,ele){
							$(this).removeClass('selected');
						});
						$(this).addClass('selected');
						var colIndex = $(this).attr('rel');
						closeCols();
						openCol(colIndex);
					}
				});
			}else{
				openBtns.find('li a').mouseover(function () {
					if ($(this).hasClass('selected')) {
						$(this).removeClass('selected');
						closeCols();
						return;
					} else {
						openBtns.find('li a').each(function(idx,ele){
							$(this).removeClass('selected');
						});
						$(this).addClass('selected');
						var colIndex = $(this).attr('rel');
						closeCols();
						openCol(colIndex);
					}
				});
			}	
           
			//openCol(0);
			
		   	/*openBtns.find('li span').each(function(idx,ele){
				if (idx == 0)
					$(this).addClass('selected');
			});*/
			return $(this);
			
   		}
   });
})(jQuery);
