	$(document).ready(function() { 
	
		$('#content').masonry({
			columnWidth: 90,
			animate: true
		});
		
		$(".collection-grid-item").hover(
		  function () {
			$(this).children(".collection-grid-item-overlay").fadeIn("fast");
		  },
		  function () {
			$(this).children(".collection-grid-item-overlay").fadeOut("slow");
		  }
		);
		
		$(".collection-grid-item-large").hover(
		  function () {
			$(this).children(".collection-grid-item-large-overlay").fadeIn("fast");
		  },
		  function () {
			$(this).children(".collection-grid-item-large-overlay").fadeOut("slow");
		  }
		);	

		$(".news-item").hover(
		  function () {
			$(this).children(".news-item-overlay").fadeIn("fast");
		  },
		  function () {
			$(this).children(".news-item-overlay").fadeOut("slow");
		  }
		);		

		$("li.parent").hoverIntent({    
			 over: makeTall_subs,
			 timeout: 500,
			 out: makeShort_subs
		});	
		
		$("ul.subnav li").hoverIntent({    
			 over: makeTall_subs,
			 timeout: 500,
			 out: makeShort_subs
		});		

		$(window).resize(function() {
			mapResize();
		});	

        $('#product-washes-inner a').click(function(){
           $('#collection-product-alpha img.product-image').remove();
           $('#collection-product-alpha img#loading').show();
            var href = $(this).attr('href')
            var img = new Image();
			$(img).addClass('product-image');
            $(img).load(function(){
				$('#collection-product-alpha').append(img);
                $('#collection-product-alpha img#loading').hide();
            }).attr('src',href);
            return false;
        });		
		
		$('.enlarge_video').click(function() {
			$(".video-holder-view").hide();
			$(".video-holder-preview").show();
			$(this).parent().parent().hide();
			$(this).parent().parent().siblings(".video-holder-view").slideToggle();
		});		
		
		$("#twitter").getTwitter({
			userName: "insightfiftyone",
			numTweets: 5,
			loaderText: "Loading tweets...",
			slideIn: true,
			slideDuration: 750,
			showHeading: false,
			headingText: "Latest Tweets",
			showProfileLink: false,
			showTimestamp: true
		});	

		$("#Form_HemiForm").change(function(){
			$(this).submit();
		});
		
		$("#offices_select").change(function(){
			$("#offices_header").html($("#offices_select option:selected").text());
			$.getJSON("contact/getOffices/?country="+$(this).val(),function(data){
				$("#offices_locations").html(data.Description);				
			})
		});	

		$("#distro_select").change(function(){
			$("#distro_header").html($("#distro_select option:selected").text());
			$.getJSON("contact/getDistro/?country="+$(this).val(),function(data){
				$("#distro_locations").html(data.Description);				
			})
		});			

		$("#jobs_select").change(function(){
			$("#jobs_header").html($("#jobs_select option:selected").text());
			$.getJSON("contact/getJobs/?country="+$(this).val(),function(data){
				$("#jobs_locations").html(data.Description);				
			})
		});

		$('.news-gallery-wrap-images').each(function(){
					
			var $this = $(this);
			
			$this.cycle({
				speed:  'slow', 
				timeout: 0, 			
				next: $this.siblings('.news-gallery-controls').find('.next'),
				prev: $this.siblings('.news-gallery-controls').find('.prev'),
				after:   onAfter
			});
			
			function onAfter(curr,next,opts) {
				var text = (opts.currSlide + 1);
				$(this).parent().siblings('.news-gallery-controls').children('.news-gallery-controls-beta').find('span.current-pos').html(text);
			}			
			
		});


		$('.news-gallery-sml-wrap-images').each(function() {
		
			var $this = $(this);
			
			$this.cycle({
				speed:  'slow', 
				timeout: 0, 			
				next: $this.siblings('.news-gallery-sml-controls').find('.next'),
				prev: $this.siblings('.news-gallery-sml-controls').find('.prev'),
				after: onAftersml
			});
			
			function onAftersml(curr,next,opts) {
				var text = (opts.currSlide + 1);
				$(this).parent().siblings('.news-gallery-sml-controls').children('.news-gallery-sml-controls-beta').find('span.current-pos').html(text);
			}	
					
		});

		/* MODAL */
		
		var closeModal = function(hash) {
		
			var $modalWindow = $(hash.w);

			$("iframe#jqmContent").attr('src','');
			$("iframe#jqmContent").empty();
			
			$modalWindow.fadeOut('5000', function() {
				hash.o.remove();

				if (hash.refreshAfterClose === 'true') {
					window.location.href = document.location.href;
				}
			});
			
			$("#content").css({ 'padding-bottom' : '50px'}); 
			
		};
	
		var openInFrame = function(hash)
		{
			$("iframe#jqmContent").html('');
							
			var $trigger = $(hash.t);
			var $modalWindow = $(hash.w);
			var $modalContainer = $('iframe', $modalWindow);
			var myUrl = $trigger.attr('href');
			var myTitle = $trigger.attr('title');
			$modalContainer.html('').attr('src', myUrl);
			$('#jqmTitleText').text(myTitle);
			myUrl = (myUrl.lastIndexOf("#") > -1) ? myUrl.slice(0, myUrl.lastIndexOf("#")) : myUrl;
			var queryString = (myUrl.indexOf("?") > -1) ? myUrl.substr(myUrl.indexOf("?") + 1) : null;

			if (queryString != null && typeof queryString != 'undefined') {
				var queryVarsArray = queryString.split("&");
				for (var i = 0; i < queryVarsArray.length; i++) {
					if (escape(unescape(queryVarsArray[i].split("=")[0])) == 'jqmRefresh') {
						hash.refreshAfterClose = queryVarsArray[i].split("=")[1]
					} else {
						hash.refreshAfterClose = false;
					}
				}
			}
			else {
				$modalWindow.jqmShow();
			}
				
			if ($($trigger).hasClass('Portrait')) {
			
				$('.jqmWindow,.jqmContent,#jqmContent,#jqmTitle').animate({
					width: 225
				}, 500, function() {
					$modalWindow.show();
				});
			}
			
			if ($($trigger).hasClass('Landscape')) {
				$('.jqmWindow,.jqmContent,#jqmContent,#jqmTitle').animate({
					width: 525
				}, 500, function() {
					$modalWindow.show();
				});				
			}			
		}

		$('a.modal').click(function(){		
			
			var elemOffset = $(this).parent().position();
								
			$('#modalWindow').removeAttr("style")

			$('#modalWindow').css('top',elemOffset.top);
			var parentWidth = $(this).parent().width();
		  
			if ($(this).hasClass('Portrait')) {
			   if (elemOffset.left > 900) {
					$('#modalWindow').css('right',1090 - elemOffset.left - parentWidth - 12);				
			   }
			   else {
					 $('#modalWindow').css('left',elemOffset.left + 12);
			   }
			} else {
			   if (elemOffset.left > 600) {
					$('#modalWindow').css('right',1090 - elemOffset.left - parentWidth - 12);				
			   }
			   else {
					 $('#modalWindow').css('left',elemOffset.left + 12);
			   }
			}			

		  $.scrollTo(elemOffset.top,500);
		   
		   $("#content").css({ 'padding-bottom' : '400px'}); 
        });			
		
		$('#modalWindow').jqm({
			overlay: 1,
			modal: false,
			trigger: 'a.modal',
			target: '#jqmContent',
			onHide: closeModal,
			onShow: openInFrame
		});	
		
		mapResize();
		
	});

	function triggerThis(element){
		$.scrollTo(element,500, {offset: {top:-100} });
		$(element).children('a').trigger('click');
	}
	
	function mapResize() {
		var windowHeight = $(window).height();
		$("#map_canvas,#map-wrap").height(windowHeight - 180);	
		
	}

	function makeTall_subs() {
		$(this).children("ul").slideDown("fast");
	}
	
	function makeShort_subs() {
		$(this).children("ul").slideUp("fast");
	}
	

