(function($) {
	methods = {
		/* Animate the engineering nav */
		engineeringAnimation : function(hash) {
			
			/**
			 * Animate the lateral menu in ingegneria.html and hide/show the
			 * correct contents (not the specific ones, because that is done
			 * by displayCorrectContent()
			 * 
			 * @param topItem
			 *            the element in the lateral menu that has to be put on
			 *            top of the stack (while the others will be put at the
			 *            bottom of the stack)
			 * @param showContent
			 *            the content that must be shown, and the rest will be
			 *            hide.
			 * @param animate
			 *            true if an animation is required; false otherwise
			 */
			function moveLeftNav(topItem, showContent, animate) {
				var navItems = new $(
						'#ingegneria_nav div');
				var notTopItems = navItems.not(topItem);
				var contentItems = $('#telecomunicazioni_content, #industriale_content, #ambientale_content, #energie_content');
				var notCurrentContentItems = contentItems.not(showContent);

				if (animate) {
					navItems.animate({
						'margin-left' : '10px'
					}, 600).animate({
						'margin-left' : '40px'
					}, 600);

					setTimeout(function() {
						topItem.css('z-index', '2').find('a').addClass('top');
						notTopItems.css('z-index', '1').find('a').removeClass('top');
					}, 600);

					notCurrentContentItems.fadeOut(500);
					showContent.delay(600).fadeIn(600);
				} else {
					topItem.css('z-index', '2').find('a').addClass('top');
					notTopItems.css('z-index', '1').find('a').removeClass('top');

					contentItems.hide();
					showContent.find('> *').not('.sub_nav').hide();
				}
			}

			/**
			 * Hide/show the correct specific contents, doing animations or not, as required
			 * 
			 * @param content
			 *            the specific content to be shown
			 * @param instant
			 *            true if the animation in not required; false otherwise
			 */
			function displayCorrectContent(content, instant) {
				if (instant) {
					$('#telecomunicazioni_content > *, #industriale_content > *, #ambientale_content > *, #energie_content > *')
							.not('.sub_nav').hide();
				} else {
					$('#telecomunicazioni_content > *, #industriale_content > *, #ambientale_content > *, #energie_content > *')
							.not('.sub_nav').fadeOut(500);
				}

				if (content == 'sezione_telecomunicazioni') {
					if (instant) {
						$('#telecomunicazioni_content .main_content').show();
					} else {
						setTimeout(function() {
							$('#telecomunicazioni_content .main_content').show();
						}, 600);
					}
				} else if (content == 'sezione_industriale') {
					if (instant) {
						$('#industriale_content *').not('#industriale_content .top').show();
					} else {
						setTimeout(function() {
							$('#industriale_content *').not('#industriale_content .top').show();
						}, 600);
					}
				} else if (content == 'sezione_ambientale') {
					if (instant) {
						$('#ambientale_content *').not('#ambientale_content .top').show();
					} else {
						setTimeout(function() {
							$('#ambientale_content *').not('#ambientale_content .top').show();
						}, 600);
					}
				} else if (content == 'sezione_energie') {
					if (instant) {
						$('#energie_content .main_content').show();
					} else {
						setTimeout(function() {
							$('#energie_content .main_content').show();
						}, 600);
					}
				} else {
					var correctContent = $('#' + content + '_content');
					if (instant) {
						correctContent.show();
					} else {
						if (correctContent.parent(':visible').size()) {
							$('#' + content + '_content').delay(600)
									.fadeIn(600);
						} else {
							$('#' + content + '_content').fadeIn(600);
						}
					}
				}
			}

			var referrer = document.referrer;
			var previousHash = $(window).data("previousHash");
			var flag = true;
			/*
			 * If flag is true, than I'm trying to get a page while I am in
			 * ingegneria.html, so I have to perform the animations. Otherwise
			 * don't animate anything, just display the correct content
			 * instantly.
			 */
			if (referrer != $(window).data("referrer")) {
				$(window).data("referrer", referrer);
				flag = false;
			}
			if (hash == 'sezione_telecomunicazioni' || hash == 'progettazione'
					|| hash == 'aie' || hash == 'strutture') {
				if (flag) {
					if (previousHash != 'sezione_telecomunicazioni'
							&& previousHash != 'progettazione'
							&& previousHash != 'aie'
							&& previousHash != 'strutture') {
						moveLeftNav($('#telecomunicazioni div'),
								$('#telecomunicazioni_content'), true);
					}

					displayCorrectContent(hash, false);
					$('#telecomunicazioni_content').fadeIn(600);
				} else {
					moveLeftNav($('#telecomunicazioni div'),
							$('#telecomunicazioni_content'), false);

					displayCorrectContent(hash, true);
					$('#telecomunicazioni_content').show();
				}
			} else if (hash == 'sezione_industriale') {
				if (flag) {
					moveLeftNav($('#industriale div'),
							$('#industriale_content'), true);

					displayCorrectContent(hash, false);
				} else {
					moveLeftNav($('#industriale div'),
							$('#industriale_content'), false);

					displayCorrectContent(hash, true);
					$('#industriale_content').show();
				}
			} else if (hash == 'sezione_ambientale') {
				if (flag) {
					moveLeftNav($('#ambientale div'),
							$('#ambientale_content'), true);

					displayCorrectContent(hash, false);
				} else {
					moveLeftNav($('#ambientale div'),
							$('#ambientale_content'), false);

					displayCorrectContent(hash, true);
					$('#ambientale_content').show();
				}
			} else if (hash == 'sezione_energie' || hash == 'eolico'
					|| hash == 'solare') {
				if (flag) {

					if (previousHash != 'sezione_energie'
							&& previousHash != 'eolico'
							&& previousHash != 'solare') {
						moveLeftNav($('#energie div'),
								$('#energie_content'), true);
					}
					
					displayCorrectContent(hash, false);
					$('#energie_content').fadeIn(600);
				} else {
					moveLeftNav($('#energie div'), $('#energie_content'),
							false);

					displayCorrectContent(hash, true);
					$('#energie_content').show();
				}
			}
			/*
			 * If the hash contains not known strings, go to
			 * "sezione_telecomunicazioni" section by default
			 */
			else if (hash != '')
				location.hash = 'sezione_telecomunicazioni';

			$(window).data("previousHash", hash);
		},

		/* Here there are all the images that need to be preloaded */
		preload : function() {
			var img1 = new Image();

			img1.src = "../img/fancybox/fancy_loading.png";
		}
	};

	$.fn.iqtPlugin = function(method) {
		// Method calling logic
		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 on jQuery.iqtPlugin');
		}
	};
})(jQuery);

$(document)
		.ready(
				function() {
					var mobile_view = false;
					if($('.main_menu_1 li').css('display') == 'block') {
						mobile_view = true;
					}

					if ($('body').attr('id') == 'ingegneria' && !mobile_view) {
						
						$(".sub_gallery a").fancybox({
							'transitionIn'	:	'elastic',
							'transitionOut'	:	'elastic'
						});

						/* Apply CSS rules to the "ingegneria" page */
						$('#ingegneria_nav').show();
						$('#content').css('margin-left', '260px');
						$('.sub_gallery li').css({
							'float' : 'left',
							'display' : 'block'
						});
						$(
								'#ambientale_content, #industriale_content, #energie_content > .sub_nav')
								.css({
									'margin-top' : '0',
									'padding-top' : '0',
									'border-top' : '0'
								});
						$('#telecomunicazioni a').attr('href',
								'#sezione_telecomunicazioni');
						$('#industriale a')
								.attr('href', '#sezione_industriale');
						$('#ambientale a').attr('href', '#sezione_ambientale');
						$('#energie a').attr('href', '#sezione_energie');
						$('#ingegneria_nav').css({
							'position' : 'absolute',
							'margin-top' : '90px'
						});
						$('#ingegneria_nav a').css('margin', '0');
						$('.top').css('display', 'none');

						/* CarouFredSel start */
						/* Add buttons to move the galleries */
						$(
								'#telecomunicazioni_sub_gallery, #srb_sub_gallery, #gallerie_sub_gallery, #aie_sub_gallery, #strutture_sub_gallery, #industriale_sub_gallery, #ambientale_sub_gallery, #eolico_sub_gallery, #solare_sub_gallery')
								.each(
										function(index) {
											/*
											 * Create buttons to scroll the
											 * carouFredSel galleries
											 */
											var id = $(this).find('> h2')
													.text();
											var prevButton = $('<button class="prev '
													+ id + '"></button>');
											$(this).find('> h2').after(
													prevButton);

											var nextButton = $('<button class="next '
													+ id + '"></button>');
											$(this).find('.sub_gallery').after(
													nextButton);

											$(this)
													.find('.sub_gallery ul')
													.carouFredSel(
															{
																circular : false,
																infinite : false,
																auto : {
																	play : false
																},
																items : {
																	width : 132
																},
																prev : {
																	button : "button[class='prev "
																			+ id
																			+ "']"
																},
																next : {
																	button : "button[class='next "
																			+ id
																			+ "']"
																}
															});
										});
						/* CarouFredSel end */

					}

					/* Start the browser history listening */
					$.history.init(function(hash) {
						/*
						 * When engineering page is accessed, go directly to the
						 * "sezione_telecomunicazioni" section by default (if no
						 * other section is specified)
						 */
						if ($('body').attr('id') == 'ingegneria' && !mobile_view) {

							if (hash == "") {
								// initialize the page
								location.hash = 'sezione_telecomunicazioni';
							} else {
								// restore the state from hash
								$(document).iqtPlugin('engineeringAnimation',
										hash);
							}
						} else if ($('body').attr('id') == 'who'
								&& hash != "") {
							$(document).iqtPlugin('staffAnimation', hash);
						}
					}, {
						unescape : ",/"
					});

					/*
					 * Be sure to have the second level menu always on the upper
					 * overlay
					 */
					$('.main_menu_1 .ingegneria').mouseenter(function() {
						var subMenu = $(this).find('ul');
						if(subMenu.size()) {
							subMenu.slideDown('fast');
							
							/* Hack for IE7- */
							if($('.main_menu_1 li').css('display') == 'inline') {
								$('.main_menu_1 li').not('.ingegneria, .ingegneria li').css({
									'position' : 'relative',
									'top' : '-9px'
								});
							}
						}
					});

					/* Hack for IE7- */
					$('.main_menu_1 .ingegneria').mouseleave(function() {
						var subMenu = $(this).find('ul');
						if(subMenu.size()) {
							$(this).find('ul').hide();
							if($('.main_menu_1 li').css('display') == 'inline') {
								$('.main_menu_1 li').not('.ingegneria, .ingegneria li').css({
									'position' : 'static'
								});
							}
						}
					});
					
					if($('body').attr('id') == 'contact_us' && !mobile_view) {
						$('#map').empty().append(
								'<iframe width="640" height="480" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.it/maps?f=q&amp;source=s_q&amp;hl=it&amp;geocode=&amp;q=iqt+consulting+ingegneria&amp;aq=&amp;sll=41.442726,12.392578&amp;sspn=28.324854,67.631836&amp;ie=UTF8&amp;hq=iqt+consulting+ingegneria&amp;hnear=&amp;cid=871420930115026474&amp;ll=45.056304,11.787386&amp;spn=0.029103,0.054932&amp;z=14&amp;iwloc=A&amp;output=embed">Cartina geografica che mostra dove si trova la sede di IQT Consulting S.r.l.</iframe><a href="http://maps.google.it/maps?f=q&amp;source=embed&amp;hl=it&amp;geocode=&amp;q=iqt+consulting+ingegneria&amp;aq=&amp;sll=41.442726,12.392578&amp;sspn=28.324854,67.631836&amp;ie=UTF8&amp;hq=iqt+consulting+ingegneria&amp;hnear=&amp;cid=871420930115026474&amp;ll=45.056304,11.787386&amp;spn=0.029103,0.054932&amp;z=14&amp;iwloc=A">Visualizzazione ingrandita della mappa</a>');
					}

					/* Slider in the homepage start */
					var slider = $(".slider");
					if (slider.size() && !mobile_view) {
						/*
						 * Put the images in the slider if and only if js is enabled
						 * (otherwise they would be useless)
						 */
						slider = slider.find('ul');
						slider.append(
								'<li class="sliderImage"><img src="img/slides/Immagine2.jpg" /> <span>Impianto\
								Fotovoltaico - 198,90 kW<br /> <br />Fratta Polesine (Rovigo)\
								(2010)</span></li>\
								<li class="sliderImage"><img src="img/slides/Immagine3.jpg" /> <span>Sito\
								Vodafone<br /> <br />Commenda Est (Rovigo) (2008)</span></li>\
								<li class="sliderImage"><img src="img/slides/Immagine4.jpg" /> <span>Sito\
								Vodafone<br /> <br />Passo di Costalunga - Vigo di Fassa\
								(Trento) (2001)</span></li>\
								<li class="sliderImage"><img src="img/slides/Immagine5.jpg" /> <span>Impianto\
						Vodafone<br /> <br />Povegliano (Treviso)</span></li>');
						slider.find('span').not(':first').css('margin-top', '400px');
						slider.carouFredSel(
								{
									onCreate : function() {
										$(this).find(':first span').animate({
											'margin-top' : '0' 
										}, 500);
									},
									width : 640,
									height : 400,
									auto : {
										pauseDuration : 6000,
										pauseOnHover : true
									},
									items : {
										visible : 1,
										width : 640,
										height : 400
									},
									scroll : {
										duration : 1000,
										items : 1,
										fx : 'crossfade',
										onBefore : function(oldItems, newItems, newSizes, duration) {
											oldItems.find('span').animate({
												'margin-top' : '400px' 
											}, 500);
										},
										onAfter : function(oldItems, newItems, newSizes) {
											newItems.find('span').animate({
												'margin-top' : '0' 
											}, 500);
										}
									}
								});
					}
					/* Slider in the homepage end */
					
					/* Architettura e design start */
					if($('body').attr('id') == 'arc_e_des' && !mobile_view) {
						$("#arc_e_des_gallery a").fancybox({
							'transitionIn'	:	'elastic',
							'transitionOut'	:	'elastic'
						});
					}
					/* Architettura e design end */
					
					/* Sociale start */
					if($('body').attr('id') == 'home' && !mobile_view) {
						$("#sociale_content a").fancybox({
							'transitionIn'	:	'elastic',
							'transitionOut'	:	'elastic'
						});
					}
					/* Sociale end */
					
					$(document).iqtPlugin('preload');
					
					if(document.getElementById("hideAll"))
						document.getElementById("hideAll").style.display = "none";
				});
