$(document).ready(
		function() {

			/* ie6 home slide css fixes */
			if ($.browser.msie && $.browser.version < 7) {
				$("#homeslider ul>li>ul").addClass("homeslider_ul_li_ul");
				$("#homeslider>ul>li").addClass("homeslider_ul_li");
				$("#homeslider>ul>li>ul>li").addClass("homeslider_ul_li_ul_li");
				$("#homeslider .next").hover( function() {
					$(this).addClass("next_hover");
				}, function() {
					$(this).removeClass("next_hover");
				});
				$("#homeslider .previous").hover( function() {
					$(this).addClass("previous_hover");
				}, function() {
					$(this).removeClass("previous_hover");
				});
			}

			/* side menu toggler */
			function showSideMenuItem() {
				var $orgParent = $(this);
				if (!$orgParent.hasClass("current")) {
					var $sub = $("ul", $orgParent);
					$orgParent.addClass("open");
					$sub.stop(false, true).slideDown(500);
				}
			}
			function hideSideMenuItem() {
				var $orgParent = $(this);
				if (!$orgParent.hasClass("current")) {
					var $sub = $("ul", $orgParent);
					$orgParent.removeClass("open");
					$sub.stop(false, true).slideUp(500);
				}
			}
			$(".aside ul.list>li").hover(showSideMenuItem, hideSideMenuItem);

			/* portfolio toggler */
			$(".portfolio h2").click( function() {
				var $parentContainer = $(this).parent();
				var allItems = $(".portfolio>li");
				allItems.each( function() {
					var $this = $(this);
					var isClickedItem = $this[0] == $parentContainer[0];
					if (isClickedItem && !$this.hasClass("open")) {
						$parentContainer.slideUp(500, function() {
							$(this).removeClass("closed").addClass("open").slideDown(1000, function() {
								$parentContainer.scrollTo(-20, 1000);
							});
						});
						$(".images div", $parentContainer).cycle("resume");
					} else if ($this.hasClass("open")) {
						$this.slideUp(250, function() {
							$(this).removeClass("open").addClass("closed").slideDown(500, function() {
								if (isClickedItem) {
									$parentContainer.scrollTo(-20, 500);
								}
							});
						});
						$(".images div", $parentContainer).cycle("pause");
					}
				});

			});

			/* homepage slideshow */
			if ($("#homeslider").length > 0) {
				$("#homeslider>ul").cycle( {
					fx : "scrollHorz",
					timeout : 0,
					speed : 500,
					next : "#homeslider .next",
					prev : "#homeslider .previous",
					before : function(currSlideElement, nextSlideElement, options, forwardFlag) {
						/* pause current subslide, start next subslide */
						$(currSlideElement).find("ul").cycle("pause");
						$(nextSlideElement).find("ul").cycle("resume");
					}
				});

				/* init subslider and pause immediately except for firstslide */
				$("#homeslider>ul>li>ul").cycle( {
					timeout : 5000,
					next : ".nextsub",
					prev : ".previoussub",
					pause : true
				}).cycle("pause");
				$("#homeslider>ul>li.firstslide>ul").cycle("resume");
			}

			/* portfolio image 'slider' */
			if ($(".portfolio").length > 0) {

				$(".portfolio li .images").mousemove(
						function(event) {
							var $this = $(this);
							var relMouseX = event.clientX - $this.offset().left;
							var visibleDivMiddle = this.offsetWidth / 2;
							var scrollableDistance = this.scrollWidth - 2 * visibleDivMiddle;
							var distanceFromVisibleCenter = relMouseX - visibleDivMiddle;
							var scrollStatWidth = $(".scrollStat", $this).outerWidth(true);

							var newScrollPos = (scrollableDistance / 2)
									+ (distanceFromVisibleCenter * ((scrollableDistance / 2) / visibleDivMiddle));
							var scrollStatOffset = (visibleDivMiddle * 2 - scrollStatWidth)
									* (newScrollPos / scrollableDistance);

							$this.stop(true, false).animate( {
								scrollLeft : newScrollPos
							});
							$(".scrollStat", $this).css( {
								left : newScrollPos + scrollStatOffset
							});

						}).mouseenter( function() {
					var scrollStatWidth = this.offsetWidth * (this.offsetWidth / this.scrollWidth);
					$(".scrollStat", $(this)).width(scrollStatWidth).fadeTo(1000, 0.75);

				}).mouseleave( function() {
					$(".scrollStat", $(this)).fadeOut();
				});

			}

			/* contact location photo 'slider' */
			if ($(".contact").length > 0) {
				$(".contact .photos .large").cycle( {
					height : '173px',
					timeout : 0,
					fx : "fade",
					speed : 500,
					pager : ".contact .photos .thumbs",
					pagerAnchorBuilder : function(idx, slide) {
						return '<img src="' + slide.src + '"" />';
					},
					activePagerClass : 'current'
				});
			}

		});

jQuery.fn.extend( {
	scrollTo : function(offset, speed, easing) {
		return this.each( function() {
			var targetOffset = $(this).offset().top + offset || 0;
			$("html,body").animate( {
				scrollTop : targetOffset
			}, speed, easing);
		});
	}
});
