/*////////////////////////////////////////////////////////////*/
/*       XSHOCK JavaScript | (C) 2011 Matthias Schuetz        */
/*////////////////////////////////////////////////////////////*/

/*
 * jQuery EasIng v1.1.2 - http://gsgd.co.uk/sandbox/jquery.easIng.php
 *
 * Uses the built In easIng capabilities added In jQuery 1.1
 * to offer multiple easIng options
 *
 * Copyright (c) 2007 George Smith
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */
jQuery.extend( jQuery.easing, {
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	}
});

/**
 * @author Alexander Farkas
 * v. 1.02
 */
(function($) {
	$.extend($.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == "string") {
                var start = $.curCSS(fx.elem,"backgroundPosition");
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
            fx.elem.style.backgroundPosition = nowPosX[0]+" "+nowPosX[1];

           function toArray(strg){
               strg = strg.replace(/left|top/g,"0px");
               strg = strg.replace(/right|bottom/g,"100%");
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});
})(jQuery);

$(document).ready(function(){
	var bootAnimation = document.location.hash.toString();
	var galleryItemPositionId = 1;
	var galleryItemRowId = 1;
	var galleryTotalRows = 0;
	var galleryImages = new Array();
	var galleryLoadCount = 0;
	
	var thisPosition;
	var thisRow;
	var leftImage;
	var rightImage;
	var rowAbove;
	var rowBelow;

	$("#logo").hover(
		function(){
			$(this)
				.stop(true)
				.fadeTo(80, 0.6)
				.fadeTo(280, 1);
		},
		function(){
			$(this)
				.stop(true)
				.fadeTo(800, 0.8)
				.fadeTo(800, 1);
		}
	);
	
	// FULL PAGE
	if ($("body").attr("id") == "full") {
		$("#year-2005, #year-2004, #year-2003, #year-2002").show();
	}
	
	$("#btn-viewall").remove();
	
	// GALLERY
	$("#gallery a img").css("margin", 0);
	
	$("#gallery a").each(function(){
		mosaicSaturation($(this));
		
		$(this).click(function(){
			$(".pp_pic_holder, .pp_overlay, .ppt").remove();
		})
	});

	function blurGallery(innerBlur, outerBlur) {
		if (leftImage) blurGalleryItem(thisPosition - 1, thisRow, innerBlur);
		if (rightImage) blurGalleryItem(thisPosition + 1, thisRow, innerBlur);
		if (rowAbove) blurGalleryItem(thisPosition, thisRow - 1, innerBlur);
		if (rowBelow) blurGalleryItem(thisPosition, thisRow + 1, innerBlur);
		if (rowAbove && leftImage) blurGalleryItem(thisPosition - 1, thisRow - 1, outerBlur);
		if (rowAbove && rightImage) blurGalleryItem(thisPosition + 1, thisRow - 1, outerBlur);
		if (rowBelow && leftImage) blurGalleryItem(thisPosition - 1, thisRow + 1, outerBlur);
		if (rowBelow && rightImage) blurGalleryItem(thisPosition + 1, thisRow + 1, outerBlur);
	}
	
	function blurGalleryItem(itemPosition, itemRow, blurValue, originalItem) {
		var currentItem = $($("#gallery a[data-item-position='" + itemPosition + "'][data-item-row='" + itemRow + "']")).find("img[data-item-role!='blurImage']");

		if (blurValue != 1) {
			var currentItemClone = currentItem.clone();
			
			currentItemClone
				.css({
					margin: (blurValue == 1 ? 0 : "-109px 0 0 0"),
					opacity: 0,
					"z-index": 4
				})
				.attr("data-item-role", "blurImage")
				.appendTo(currentItem.parent())
				.stop()
				.clearQueue()
				.animate({ opacity: blurValue }, 150);
				
			if (originalItem) {
				currentItemClone.stop().clearQueue().animate({
							opacity: 1
						},
						80,
						function(){
							$("#gallery-image-hover").remove();
							
							if (!$.browser.msie || ($.browser.msie && parseInt($.browser.version, 10) != 8)) {
								$("<a />", {
									id: "gallery-image-hover",
									css: {
										width: 190,
										height: 109,
										background: "url('images/bg_viewfullimage.png') no-repeat 0 109px",
										"z-index": 5
									}
								})
								.prependTo(currentItem.parent())
								.animate({
										"background-position": "0 0"
									},
									400,
									"easeOutExpo"
								);
							}
						}
					);
			}
		} else {
			$("img[data-item-role='blurImage']").remove();
		}
	}
	
	function mosaicSaturation(obj) {
		if (galleryItemPositionId > 3) {
			galleryItemPositionId = 1;
			galleryItemRowId++;
		}

		obj.attr("data-item-position", galleryItemPositionId);
		obj.attr("data-item-row", galleryItemRowId);
		galleryItemPositionId++;

		obj.hover(
			function(){
				thisPosition = parseInt(obj.attr("data-item-position"));
				thisRow = parseInt(obj.attr("data-item-row"));
				leftImage = (thisPosition - 1 > 0);
				rightImage = (thisPosition + 1 < 4);
				rowAbove = (thisRow - 1 > 0);
				rowBelow = (thisRow + 1 < galleryTotalRows + 1);
				
				blurGalleryItem(thisPosition, thisRow, 0.2, 1);
				blurGallery(0.4, 0.2);
			},
			function(){
				obj.find("img").css("margin", 0);
				$("#gallery-image-hover").remove();
				blurGallery(1, 1);
			}
		);
		
		switch (galleryItemRowId) {
			case 6: $("#year-2005").fadeIn(300); break;
			case 8: $("#year-2004").fadeIn(300); break;
			case 10: $("#year-2003").fadeIn(300); break;
			case 12: $("#year-2002").fadeIn(300); break;
		}

		galleryTotalRows = galleryItemRowId;
	}

	// GALLERY LOADER
	galleryImages.push(["Tek7", "tek7.jpg"]);
	galleryImages.push(["The Warmth", "warmth.jpg"]);
	galleryImages.push(["Hypex", "hypex.jpg"]);
	galleryImages.push(["Stylism", "stylism.jpg"]);
	galleryImages.push(["Zyob", "zyob.jpg"]);
	galleryImages.push(["Betaform", "betaform.jpg"]);
	galleryImages.push(["Ctrl", "ctrl.jpg"]);
	galleryImages.push(["Lightflow", "lightflow.jpg"]);
	galleryImages.push(["Neon Sequence Remix", "neon_sequence_remix.jpg"]);
	galleryImages.push(["Neon Sequence", "neon_sequence.jpg"]);
	galleryImages.push(["Sync", "sync.jpg"]);
	galleryImages.push(["Wype", "wype.jpg"]);
	galleryImages.push(["Protophonic", "protophonic.jpg"]);
	galleryImages.push(["Sbtr", "sbtr.jpg"]);
	galleryImages.push(["Pyromance", "pyromance.jpg"]);
	galleryImages.push(["Fascination", "fascination.jpg"]);
	galleryImages.push(["Contraflection", "contraflection.jpg"]);
	galleryImages.push(["Ultratech", "ultratech.jpg"]);
	galleryImages.push(["Reinventing Creativity", "reinventing_creativity.jpg"]);
	galleryImages.push(["Consumption", "consumption.jpg"]);
	galleryImages.push(["Psytech", "psytech.jpg"]);
	galleryImages.push(["Deformed", "deformed.jpg"]);
	galleryImages.push(["Xenotron", "xenotron.jpg"]);
	galleryImages.push(["Respawn", "respawn.jpg"]);
	galleryImages.push(["Lost", "lost.jpg"]);
	galleryImages.push(["Endorfuture", "endorfuture.jpg"]);
	galleryImages.push(["Escape", "escape.jpg"]);
	galleryImages.push(["Vir2l Future", "vir2l_future.jpg"]);
	galleryImages.push(["Utopian Empire", "utopian_empire.jpg"]);
	galleryImages.push(["Obstructive", "obstructive.jpg"]);
	galleryImages.push(["xshock v8 Promo", "xshock_v8_promo.jpg"]);
	galleryImages.push(["Termination", "termination.jpg"]);
	galleryImages.push(["High Tek", "high_tek.jpg"]);
	galleryImages.push(["Constructure", "constructure.jpg"]);
	galleryImages.push(["Temptation", "temptation.jpg"]);
	galleryImages.push(["Tech Unit", "tech_unit.jpg"]);
	galleryImages.push(["Excessive Trauma", "excessive_trauma.jpg"]);
	galleryImages.push(["Retry", "retry.jpg"]);
	galleryImages.push(["Emotional Phase", "emotional_phase.jpg"]);
	galleryImages.push(["Compulsion", "compulsion.jpg"]);
	galleryImages.push(["Vortex", "vortex.jpg"]);
	galleryImages.push(["Out Of Reality", "out_of_reality.jpg"]);
	galleryImages.push(["Deterioration", "deterioration.jpg"]);
	galleryImages.push(["Dark Experience", "dark_experience.jpg"]);
	galleryImages.push(["Bacteria", "bacteria.jpg"]);
	galleryImages.push(["Exodus", "exodus.jpg"]);
	galleryImages.push(["Resistance", "resistance.jpg"]);
	galleryImages.push(["The Perfect Drug", "the_perfect_drug.jpg"]);
	galleryImages.push(["A Faint Moment Of Fortune", "a_faint_moment_of_fortune.jpg"]);
	galleryImages.push(["Subobjective", "subobjective.jpg"]);
	galleryImages.push(["Mutation", "mutation.jpg"]);
	galleryImages.push(["Ecstatic", "ecstatic.jpg"]);
	galleryImages.push(["Adrenaline", "adrenaline.jpg"]);
	galleryImages.push(["Lost In Silence", "lost_in_silence.jpg"]);
	galleryImages.push(["Hybridize", "hybridize.jpg"]);
	galleryImages.push(["Abandoned", "abandoned.jpg"]);
	galleryImages.push(["Neodym", "neodym.jpg"]);
	galleryImages.push(["Crash Complex", "crash_complex.jpg"]);
	galleryImages.push(["Precontrol", "precontrol.jpg"]);
	galleryImages.push(["Connecting", "connecting.jpg"]);
	galleryImages.push(["Lithium", "lithium.jpg"]);
	galleryImages.push(["Negative Logic", "negative_logic.jpg"]);
	galleryImages.push(["Monument", "monument.jpg"]);
	galleryImages.push(["Subconstruction", "subconstruction.jpg"]);
	galleryImages.push(["Vacuum", "vacuum.jpg"]);
	
	function loadGalleryImage() {
		var tempGalleryImage = new Image;
		tempGalleryImage.src = "data/thb/" + galleryImages[galleryLoadCount][1];
		attachEventListener(tempGalleryImage, "load", loadGalleryImageComplete);
	}
	
	function attachEventListener(obj, evt, func) {
		if (obj.addEventListener) {
			obj.addEventListener(evt, func, false);
		} else if (obj.attachEvent) {
			obj.attachEvent("on" + evt, func);
		}
	}

	function loadGalleryImageComplete(evt) {
		var newGalleryAjaxImage = $("<a />", {
				"href": "data/fullview/" + galleryImages[galleryLoadCount][1],
				"class": ((galleryLoadCount + 1) % 3 == 0 ? "image-right" : ""),
				"rel": "enlarge[gallery]",
				"html": '<span class="gallery-label">' + galleryImages[galleryLoadCount][0] + '</span><img src="data/thb/' + galleryImages[galleryLoadCount][1] + '" alt="' + galleryImages[galleryLoadCount][0] + '" />'
			})
			.click(function(){
				$(".pp_pic_holder, .pp_overlay, .ppt").remove();
			})
			.css("display", "none")
			.appendTo("#gallery")
			.fadeIn(300, function(){
				galleryLoadCount++;
				if (galleryLoadCount < galleryImages.length) loadGalleryImage();
			});

		setPrettyPhoto("a[rel='enlarge[gallery]']");
		mosaicSaturation(newGalleryAjaxImage);
		
		if (typeof $(".pp_overlay") != "undefined") {
			windowWidth = $(window).width();
			$(".pp_overlay").height($(document).height()).width(windowWidth);
		}
	}

	if ($("body").attr("id") == "dynamic") loadGalleryImage();

	// MENU INDICATOR
	$("#menu a").css("background", "none");
	
	$("<div>", {
		id: "mnu_indicator",
		"data-position": $(".mnu_active").attr("id"),
		css: {
			position: "relative",
			top: ($.browser.msie && $.browser.version.substr(0, 1) == 7 ? 0 : 26),
			left: 0,
			width: 37,
			height: 2,
			background: "#E41C33"
		}
	}).appendTo("#menu");
	
	hoverSlideIn($(".mnu_active").attr("id"), true);

	$("#mnu_work, #mnu_info, #mnu_contact").hover(
		function(){
			hoverSlideIn($(this).attr("id"));
		},
		function(){
			hoverSlideOut();
		}
	);

	function hoverSlideIn(mnuItem, noAnimation, customDelay) {
		var newWidth;
		var newLeft;
	
		switch (mnuItem) {
			case "mnu_work": newWidth = 37; newLeft = 0; break;
			case "mnu_info": newWidth = 31; newLeft = 416; break;
			case "mnu_contact": newWidth = 51; newLeft = 521; break;
		}

		if (!noAnimation) {
			$("#" + mnuItem).parent()
				.stop(true)
				.fadeTo(80, 0.6)
				.fadeTo(280, 1);
		
			$("#mnu_indicator")
				.stop()
				.animate({
						width: newWidth,
						left: newLeft
					},
					(customDelay ? customDelay : 1000),
					"easeOutExpo"
				)
				.css("overflow", "visible");
		} else {
			if (mnuItem == "mnu_work" && bootAnimation == "") {
				$("#mnu_indicator").css("width", 0);
				hoverSlideIn($(".mnu_active").attr("id"), false, 2200);
			} else {
				$("#mnu_indicator").css({
					width: newWidth,
					left: newLeft
				});
			}
		}
	}
	
	function hoverSlideOut() {
		var originalWidth;
		var originalLeft;
	
		switch ($("#mnu_indicator").attr("data-position")) {
			case "mnu_work": originalLeft = 0; originalWidth = 37; break;
			case "mnu_info": originalLeft = 417; originalWidth = 31; break;
			case "mnu_contact": originalLeft = 521; originalWidth = 51; break;
		}
	
		$("#mnu_indicator")
			.stop()
			.animate({
					width: originalWidth,
					left: originalLeft
				},
				1800,
				"easeOutExpo"
			)
			.css("overflow", "visible");
	}
	
	// SLIDER
	if ($("#slider").length >= 1) {
		$("#slider").easySlider({
			auto: true,
			continuous: true,
			speed: 700,
			pause: 3500
		});
		
		$("#slider-border").attr("href", $("#slider-content a:eq(1)").attr("href"));
		$("#slider-border").attr("name", $("#slider-content a:eq(1)").attr("name"));
		$("#slider-image-pixel").attr("alt", $("#slider-content a:eq(1) > img").attr("alt"));
	}
	
	$("#prevBtn, #nextBtn").hover(
		function(){
			$(this).css("opacity", 0.7);
		},
		function(){
			$(this).css("opacity", 1);
		}
	);
	
	$("#slider-border").hover(
		function(){
			$("#slider-border-hover").stop().clearQueue().animate({ "opacity": 1 }, 200);
		},
		function(){
			$("#slider-border-hover").stop().clearQueue().animate({ "opacity": 0 }, 200);
		}
	);
	
	$("#slider-border-hover").css({
		"display": "block",
		"opacity": 0
	});
	
	// PRETTYPHOTO
	prettySliderFix = false;

	function setPrettyPhoto(obj) {
		$(obj).prettyPhoto({
			animation_speed: "fast",
			slideshow: 3500,
			autoplay_slideshow: false,
			opacity: 0.6,
			show_title: true,
			allow_resize: true,
			default_width: 640,
			default_height: 480,
			theme: "dark_square",
			horizontal_padding: 0,
			keyboard_shortcuts: true,
			overlay_gallery: false,
			changepicturecallback: function(){
				if (!prettySliderFix && theRel == "enlarge[slider]") {
					prettySliderFix = true;
					pp_images.shift();
					pp_titles.shift();
					set_position = parseInt(theName.substr(-1) - 1);
				}

				$(".pp_download, .pp_close").fadeIn(100);
			},
			markup: '<div class="pp_pic_holder"> \
						<div class="ppt">&nbsp;</div> \
						<a class="pp_download" href="#">Download</a> \
						<a class="pp_close" href="#">Close</a> \
						<div class="pp_content_container"> \
								<div class="pp_content"> \
									<div class="pp_loaderIcon"></div> \
									<div class="pp_fade"> \
										<a href="#" class="pp_expand" title="Enlarge">Enlarge</a> \
										<div class="pp_hoverContainer"> \
											<a class="pp_next" href="#">Next</a> \
											<a class="pp_previous" href="#">Previous</a> \
										</div> \
										<div id="pp_full_res"></div> \
										<div class="pp_details"> \
										</div> \
									</div> \
								</div> \
							</div> \
						</div> \
					</div> \
					<div class="pp_overlay"></div>'
		});
	}
	
	setPrettyPhoto("a[rel='enlarge[slider]'], a[rel='enlarge[gallery]']");

	$("a[rel='enlarge[slider]'], a[rel='enlarge[gallery]']").click(function(){
		$(".pp_pic_holder, .pp_overlay, .ppt").remove();
		setPrettyPhoto("a[rel='enlarge[slider]'], a[rel='enlarge[gallery]']");
	});
});
