jQuery.noConflict();

/*
  * TYPOGRAPHY
*/

Cufon.set('fontFamily', 'ColaborateLight');
Cufon.replace('h2, #main h3, h4, h5, h6, #slogan, .label', {
	hover: true
});

Cufon.set('fontFamily', 'Colaborate-Medium');
Cufon.replace('#main_home h3', {
	hover: true
});

jQuery(document).ready(function() {
						   
	var hash = window.location.hash.substr(1);
	var href = jQuery('#nav2 li a').each(function(){
		var href = jQuery(this).attr('href');
		if(hash==href.substr(0,href.length-5)){
			var toLoad = hash+'.html #content';
			jQuery('#content').load(toLoad)
		}											
	});

	jQuery('#nav2 li a').click(function(){
	jQuery("#nav2 li a").addClass("current").not(this).removeClass("current");
								  
		var toLoad = jQuery(this).attr('href')+' #content';
		jQuery('#content').hide('fast',loadContent);
		jQuery('#load').remove();
		jQuery('#box_two_third last').append('<span id="load">LOADING...</span>');
		jQuery('#load').fadeIn('normal');
		window.location.hash = jQuery(this).attr('href').substr(0,jQuery(this).attr('href').length-5);
		function loadContent() {
			jQuery('#content').load(toLoad,'',showNewContent())
		}
		function showNewContent() {
			jQuery('#content').show('normal',hideLoader());
		}
		function hideLoader() {
			jQuery('#load').fadeOut('normal');
		}
		return false;
		
	});

});

/*
  * WE CALL PK_ FUNCTIONS
*/

jQuery(document).ready(function(){	
	jQuery("#menu").pk_menu();

	/* GALLERIES */

	if(jQuery("#project_details").length == 0) {
		jQuery(".big_gallery").pk_gallery({
			photos: ".item",
			thumbs: ".gallery_navigation a",
			buttonNext: ".gallery_button_next",
			buttonPrev: ".gallery_button_prev",
			buttonPlayPause: ".button_play_pause",
			easing: "easeInOutQuad",
			speedIn: 400,
			speedOut: 600
		});
		jQuery(".small_gallery").pk_gallery({
			photos: ".item",
			thumbs: ".gallery_navigation a",
			buttonNext: ".gallery_button_next",
			buttonPrev: ".gallery_button_prev",
			buttonPlayPause: ".button_play_pause",
			easing: "easeInOutQuad",
			speedIn: 400,
			speedOut: 600
		});
	}

});

/*
  * PK_MENU ***
*/

(function($) {
	$.fn.pk_menu = function(options) {
		var defaults = {
			autoPosition: false,
			limitValue: "wrapper",
			easing: "easeOutQuint",
			speedIn: 400,
			speedOut: 100
		};
		
		var settings = $.extend({}, defaults, options);
		
		/**/
		
		return this.each(function () {
			var $root = $(this);
			var $mainmenu = $(">ul", this);
			var $headers = $mainmenu.find("ul").parent();
			var $limitValue = (settings.limitValue == "document") ? $(window).width() : 960;
            
			/**/
		
			$headers.each(function () {
				var $curobj = $(this);
				var $subul = $(this).find('ul:first');
				var $ul = $("ul", $curobj);
			
				$("ul ul", $root).css({"display": "none", "paddingTop": "0px"}).find("li:first").css({"marginTop": "15px"});
				$("ul ul ul", $root).find("li:first").css({"marginTop": "5px"});
			
				/**/
			
				function getProperty($li, $ul) {
					$li.dimensions = {
						w: $li.offsetWidth, 
						h: $li.offsetHeight, 
						subulw: $ul.outerWidth(), 
						subulh: $ul.outerHeight()
					}
					return $li.dimensions;
				}
			
				function showMenu ($element) {
					$element.css({visibility:'visible'}).slideDown(settings.speedIn);
				}
 
    			function hideMenu ($element, $current) {
    				$element.slideUp(settings.speedIn, function() {
    					$element.hide();
    				});
    				setTimeout(function() { 
        				$current.stop().animate({
        					borderBottomWidth: "5px"
        				}, settings.speedIn + 100, "easeInOutSine");
   				 	}, 100); 
    			}
			
				/**/
				$curobj.click(function() {
					var $targetul = $(this).find("ul:first");
					$targetul.hide();
				});
				$curobj.hoverIntent(function() {
					closeOptionsMenu();
					getProperty(this, $subul);
					
					if($(this).find("current")) {
						$(this).stop().animate({borderBottomWidth: "0px"}, 1, "easeInOutSine");
					}
					
					var $targetul = $(this).find("ul:first");
					var $offset = $(this).offset();
				
					if($curobj.parents("ul").length == 1) {
						$ul.css({visibility:'hidden'});
						this.firstLevel = true;
						$subul.css({top: 30 + "px"});
					} else {
						this.firstLevel = false;
						$subul.css({top: -5 + "px"});
					}
					
					if(this.firstLevel) {
						var menuleft = 0;
					} else {
						var menuleft = this.dimensions.w;
					}
					
					if(($offset.left + menuleft + this.dimensions.subulw) > $limitValue && !this.firstLevel) {
						menuleft = -(this.dimensions.w);
					}
				
					if(settings.autoPosition == true) {
						$targetul.css({left:menuleft + "px"});
					}
					showMenu($targetul);
				}, function() {
					var $targetul = $(this).find("ul:first");
					hideMenu($targetul, $(this));
				});
			});
		});
	};
})(jQuery);

/*
  * PK_OPTIONS_MENU ***
*/

function closeOptionsMenu() {
	jQuery("#options_wrapper div").filter(":visible").stop().slideUp(400, function() {
		jQuery("#options_wrapper").hide();
	});
}

(function($) {
	$.fn.pk_options_menu = function(options) {
		var defaults = {
			controls: "#options_menu li",
			easing: "easeOutExpo",
			speedIn: 400,
			speedOut: 100
		}
		
		var settings = $.extend({}, defaults, options);
		
		/**/
		
		return this.each(function() {
			var $root = $(this);
			var $menu = $(settings.controls);
			var $items = $("div", $root);
			var $new_item = null;
			
			$root.hide();
			
			$menu.each(function(i) {
				$(this).hoverIntent(function() {
					$new_item = $items.filter(":eq(" + i + ")");
					
					if($new_item.css("display") == "none") {
						if($root.css("display") == "none") {
							$root.show();
							$items.filter(":visible").slideUp(0);
							$new_item.slideDown(settings.speedIn);
						} else {
							$items.filter(":visible").slideUp(settings.speedOut, function() {
								$new_item.slideDown(settings.speedIn);
							});
						}
						$new_item.bind("mouseleave", function() {
							closeOptionsMenu();
						});
					}
				}, function(){});
			});
		});
	};
})(jQuery);


/*
  * PK_GALLERY ***
*/

(function($) {
	$.fn.pk_gallery = function(options) {
		var defaults = {
			photos: "",
			thumbs: "",
			buttonNext: "",
			buttonPrev: "",
			buttonPlayPause: "",
			buttonClose: "",
			/**/
			autoStart: true,
			firstClick: true,
			toogle: false,
			/**/
			timer: 5000,
			easing: "",
			speedIn: 400,
			speedOut: 200
		};
		
		var settings = $.extend({}, defaults, options);
		
		/**/
		
		return this.each(function() {
			var $root = $(this);
			var $items = $(settings.photos , $root);
			var $thumbs = $(settings.thumbs);
			var $totItems = $items.length;
			var $movies = [];
			var $status = ""; 
			var $index = 0; 
			var $interval = "";
			
			/**/
			
			function slideshow(index) {
				if(index){
					var id = index;
				} else {
					var id = 0;
				}
				$interval = setInterval(
					function() {
						play(id, "next");
					}, settings.timer
				);
			}
			
			function play(index, verse) {
				var id = index;
				if(verse == "next" || verse == undefined) {
					if(id < ($thumbs.length - 1)) {
						id++;
					} else {
						id = 0;
					}
				} else {
					if(id > 0) {
						id--;
					} else {
						id = ($thumbs.length - 1);
					}
				}
				$thumbs.filter(":eq("+ id +")").trigger("click", [true]);
			}
			
			function positionNavigation(next, prev, nav, gallery) {
				var left = (gallery.width() / 2) - (nav.width() / 2);
				var top = (gallery.height() / 2) - (next.height() / 2);
				
				next.css("margin-top", top);
				prev.css("margin-top", top);
				nav.css("margin-left", left);
			}
			
			function changeThumbs($current) {
				for(i = 0; i < $thumbs.length; i++) {
					$thumbs.filter(":eq("+ i +")").removeClass("current");
				}
				$current.addClass("current");
			}
			
			if(settings.autoStart == true) {
				$status = "play";
				$(settings.buttonPlayPause + " img").attr('src', 'images/skin/button_pause.png');
			} else {
				$status = "pause";
			}
			
			if($("body").find("#project_details").length > 0) {
				$thumbs.css("cursor", "pointer");
			}
			
			/**/

			$thumbs.each(function(i) {
				$movies[i] = $items.filter(":eq(" + i + ")").find(".movie").html();
				
				$(this).click(function() {
					var $new_item = $items.filter(":eq(" + i + ")");
					var $media = $items.filter(":eq(" + i + ")").find(".movie").length;
					var $new_height = $new_item.height();
					
					this.id = i;
					$index = this.id;
					
					if($interval) {
						clearInterval($interval);
					}
					if($media == 0 && $status == "play" && $totItems > 1) {
						slideshow(this.id);
					}
					
					if($new_item.css("display") == "none") {
						if($items.filter(":visible").find(".movie")) {
							$items.find(".movie").empty();
						}
						if($media > 0) {
							$new_item.find(".movie").html($movies[this.id]);
						}
						$items.filter(":visible").fadeOut(settings.speedOut, settings.easing);
						$new_item.fadeIn(settings.speedIn, settings.easing);
						
						if($("#project_details").length > 0) {
							var tot = $new_item.find(".item").length;
							var width = (15 * tot) + 80;
							$new_item.find(".gallery_navigation").css("width", width);
							if($("body").find(".portfolio_big_gallery").length > 0) {
								positionNavigation($new_item.find(".gallery_button_next"), $new_item.find(".gallery_button_prev"), $new_item.find(".gallery_navigation"), $new_item.find(".big_gallery"));
							}
							if($("body").find(".portfolio_small_gallery").length > 0) {
								positionNavigation($new_item.find(".gallery_button_next"), $new_item.find(".gallery_button_prev"), $new_item.find(".gallery_navigation"), $new_item.find(".small_gallery"));
							}
						}
					}
					
					if(settings.toogle == true) {
						$root.show().animate({
							"height" : $new_height 
						}, settings.speedIn, settings.easing, function() {
							$('#portfolio_navigation').slideDown(settings.speedIn);
						});
						$('html:not(:animated),body:not(:animated)').animate({ scrollTop: 0 }, settings.speedIn, settings.easing);
						$('.breadcrumb').animate({ "paddingTop": "0px"}, settings.speedIn, settings.easing);
					}
					
					changeThumbs($(this));
					
					return false;
				});
			});
			
			if($totItems > 1) {
				var width = (15 * $totItems) + 80;
				$root.find(".gallery_navigation").css("width", width);
				var left = ($root.width() / 2) - ($root.find(".gallery_navigation").width() / 2);
				var top = ($root.height() / 2) - ($(settings.buttonNext).height() / 2);
				
				/* NAVIGATION */
			
				$root.find(".gallery_navigation").show().css("margin-left", left);
				
				/* BUTTON PLAY/PAUSE */
				
				$(settings.buttonPlayPause, $(this)).click(function() {
					if($status == "play") {
						$status = "pause";
						clearInterval($interval);
						$("img", $(this)).attr('src', 'images/skin/button_play.png');
					} else {
						$status = "play";
						play($index, "next");
						$("img", $(this)).attr('src', 'images/skin/button_pause.png');
					}
				});
				
				/* BUTTONS NEXT / PREV / CLOSE */
				
				$(settings.buttonNext).show().css("margin-top", top).click(function() {
					play($index, "next");
				});
				$(settings.buttonPrev).show().css("margin-top", top).click(function() {
					play($index, "prev");
				});
			} else {
				$root.find(".gallery_navigation").hide();
			}
			
			/* BUTTON CLOSE */
			
			$(settings.buttonClose, $items).click(function() {
				closeProjects();
			});
			
			/* FIRST CLICK */
			
			if(settings.firstClick) {
				setTimeout(function() { 
        			$thumbs.filter(":eq(0)").trigger("click", [true]);
   				}, 500);
			}
		});
	}
})(jQuery);


/*
  * PK_IMPROVEMENTS ***
*/

(function($) {
	$.fn.pk_improvements = function(options) {
		var defaults = {
			easing: "easeOutQuint",
			speedIn: "fast",
			speedOut: "fast"
		};
	
		var settings = $.extend({}, defaults, options);
		
		return this.each(function () {
			var $root = $(this);
			
			/* PRETTYPHOTO --- theme: light_rounded / dark_rounded / light_square / dark_square */
			
			if($("body").find("a[rel^='prettyPhoto']").length > 1) {
				$("a[rel^='prettyPhoto']").prettyPhoto({
					"default_width": 820,
					"default_height": 500,
					"opacity": 0.80,
					"theme": 'light_square'	
				});
			}
		});
	}
})(jQuery);

(function($) {
	$.fn.hoverIntent = function(f,g) {
		// default configuration options
		var cfg = {
			sensitivity: 7,
			interval: 100,
			timeout: 200
		};
		// override configuration options with user supplied object
		cfg = $.extend(cfg, g ? { over: f, out: g } : f );

		// instantiate variables
		// cX, cY = current X and Y position of mouse, updated by mousemove event
		// pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
		var cX, cY, pX, pY;

		// A private function for getting mouse position
		var track = function(ev) {
			cX = ev.pageX;
			cY = ev.pageY;
		};

		// A private function for comparing current and previous mouse position
		var compare = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			// compare mouse positions to see if they've crossed the threshold
			if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
				$(ob).unbind("mousemove",track);
				// set hoverIntent state to true (so mouseOut can be called)
				ob.hoverIntent_s = 1;
				return cfg.over.apply(ob,[ev]);
			} else {
				// set previous coordinates for next time
				pX = cX; pY = cY;
				// use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
				ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
			}
		};

		// A private function for delaying the mouseOut function
		var delay = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			ob.hoverIntent_s = 0;
			return cfg.out.apply(ob,[ev]);
		};

		// A private function for handling mouse 'hovering'
		var handleHover = function(e) {
			// next three lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
			var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
			while ( p && p != this ) { try { p = p.parentNode; } catch(e) { p = this; } }
			if ( p == this ) { return false; }

			// copy objects to be passed into t (required for event object to be passed in IE)
			var ev = jQuery.extend({},e);
			var ob = this;

			// cancel hoverIntent timer if it exists
			if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }

			// else e.type == "onmouseover"
			if (e.type == "mouseover") {
				// set "previous" X and Y position based on initial entry point
				pX = ev.pageX; pY = ev.pageY;
				// update "current" X and Y position based on mousemove
				$(ob).bind("mousemove",track);
				// start polling interval (self-calling timeout) to compare mouse coordinates over time
				if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}

			// else e.type == "onmouseout"
			} else {
				// unbind expensive mousemove event
				$(ob).unbind("mousemove",track);
				// if hoverIntent state is true, then call the mouseOut function after the specified delay
				if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
			}
		};

		// bind the function to the two event listeners
		return this.mouseover(handleHover).mouseout(handleHover);
	};
})(jQuery);
