// News Section Sliders
(function($){

	$.fn.contentSlider = function(panel){
	
			//make entire list items clickable
			$("ul.items li:has(a)").click(function(){
					window.open($(this).find("a").attr("href")); 
				return false;
			});

			//scrollpane parts
			var scrollPane = $("div."+panel);
			var scrollContent = $('ul.items');
			var sliderLeftMargin = (scrollPane.width()-$('.slider').outerWidth()-3);
			var scrollValue = 100;
			
			if(panel=="sliderGallery"){
				if(scrollContent.height() < scrollPane.height()){
					$("div.slider").fadeOut(200,function(){
						$("ul.items").animate({"width":"374px"}, 200);
					});
				}else{
					$("ul.items").animate({"width":"360px"}, 200,function(){
						$("div.slider").fadeIn(200);
					});
				};
			};
			
			//build slider
			var scrollbar = $("div.slider").slider({
				min: 0,
				max: 100,
				value: scrollValue,
				animate:true,
				orientation: 'vertical',
				slide:function(e, ui){
					var difference = $(scrollContent).height()-$(scrollPane).height();
					var topValue = -((100-ui.value)*difference/100);
					if( scrollContent.height() > scrollPane.height() ){ 
						scrollContent.css('margin-top', topValue); 
					}else{ 
						scrollContent.css('margin-top', 0); 
					}
				}
			}).height(scrollPane.innerHeight()-8).css({"margin-top" : 3, "margin-left" : sliderLeftMargin});
			
			var sliderPadding = $(".ui-slider-handle").height()/2; 
			if($("div.ui-handle-helper-parent").length==0){
			$(".ui-slider-handle").wrap('<div class="ui-handle-helper-parent"></div>').parent().css({"margin-top":sliderPadding+1,"margin-bottom":sliderPadding-1}).height($('.slider').height()-$(".ui-slider-handle").height());
			};
			
			return this;
			
		};
		
})(jQuery);

(function($){

	$.fn.loadNews = function(){

// News Page Content Loading
	
	$('<div id="load">Loading...</div>').appendTo('.sliderGallery').hide();
	
	if(window.location.hash == '' || window.location.hash == 'NULL'){
		window.location.hash = 'in_the_news';
	};
	
	var hash = window.location.hash.substr(1);
	
    var href = $('#news .contentList li a').each(function(){  
        var href = $(this).attr('href'); 
		if(hash==href.substr(0,href.length-4)){  
            $(this).addClass("selectedNews"); 
        }else{
			$(this).removeClass("selectedNews");
		};
        if(hash==href.substr(0,href.length-4)){  
            var toLoad = hash+'.php .items li';  
            $('.items').load(toLoad, function(){$(this).contentSlider("sliderGallery");});
        }  
    });
  
    $('#news .contentList li a').click(function(){ 

		$('#news .contentList li a').each(function(){
			$(this).removeClass('selectedNews');
		});
		
		$(this).addClass("selectedNews");
		
		window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-4);
		
		var href = $(this).attr('href');
		if(href=="twitter.php"){
			var items = "#twitter_update_list li";
		}else{
			var items = ".items li";
		};
		var toLoad = href+' '+items;  
		var hash = window.location.hash.substr(1);

		$(items).fadeOut(200, showLoader());  
		
		function showLoader(){
			$('#load').fadeIn(200, function(){
				$('.items').load(toLoad, function(){
					$(this).css("margin-top",0);
					$(this).contentSlider("sliderGallery");
					$('#load').fadeOut(200);
				});
			});
		};
		
		return false;  
    }); 
	
	};
	
})(jQuery);


(function($){

	$.fn.productViewer = function(){

		var productName = $("a.viewIn360").attr("alt").toLowerCase().replace(/[\&*\s*]{1,3}/, "_"),
			imagePane = $('<div><div id="rotationDemoWrap"><img src="../../images/portfolio_360/'+productName+'/'+productName+'001.jpg" id="rotationDemo" width="800px" height="451px" /></div></div>').css("display", "none").appendTo("body"),
			allImages = [],
			num,
			i;

		(function(){
			for(i=1; i<201; i++){
				if(i < 10){
					num = "00"+i;
				}else if(i < 100){
					num = "0"+i;
				}else{
					num = i;
				};
				allImages.push("../../images/portfolio_360/"+productName+"/"+productName+num+".jpg");
			};
		})();
		
		$("a.viewIn360").fancybox();
		$("a.viewIn360").click(function(){ return false; });
		
		$('#rotationDemo').reel({
					images : allImages,
					opening : 2,
					speed : .1,
					wheelable : false,
					draggable : true
		});
			
	};
	
})(jQuery);


$(document).ready( function(){

	// iOS Hover Event Class Fix
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
		$(".navPortfolio, .navContact").click(function(){  
			$(this).find("ul").toggle();
		});
	};
	
	// Facilities video popout
	$("a.facVideo").fancybox();
	
	// Hover Intent
	(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){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;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

// Facilities List Popup Animations
	
	$("#facilities ul.contentList li").click(function(){
		return false;
	});
	
	$("#facilities ul.contentList li").hoverIntent(function(e){
	
		var currentItem = $(this).attr("rel");
		var currentPlace = $("ul.fac li."+currentItem);
		var placeLocation = $(currentPlace).position();
		var placeHeight = ($(currentPlace).height())/2;
		var placeWidth = ($(currentPlace).width())/2;
		var title = $(currentPlace).text();
		var href = $(currentPlace).attr("class");

		$('<div id="facTooltip"><b>'+ title +'</b><img src="../images/' + href + '.jpg" alt="" /><div id="facTooltipTail" class="tailLeft"></div></div>').css("top", placeLocation.top - 85 + placeHeight).css("left", placeLocation.left - 70 + placeWidth + 120).appendTo("div.facilitiesRightCol").hide().fadeIn(400);
	
	}, function() {
		$("#facTooltip").remove();
	});

// Team Popup Animations
	
	$(".teamPage li").hover(function(e) {
		
		var quote = $(this).text();
		var name = $("img", this).attr("alt");
		var title = $("img", this).attr("rel");
		
		$('<div id="teamTooltip"><b>'+ name +'</b><br/>'+ title +'<br/><br/><i>'+ quote +'</i></div>').css("top", e.pageY - 160).css("left", e.pageX + offset).appendTo("body").hide().fadeIn(400);
	}, function() {
		$("#teamTooltip").remove();
	});
	
	$(".teamPage li").mousemove(function(e) {
		var windowWidth = $(window).width();
		var tooltipWidth = $("#teamTooltip").width();
		
		if(windowWidth - e.pageX <= tooltipWidth){
			$("#teamTooltip").css("top", e.pageY - 40).css("left", e.pageX - 230);
		}else{
			$("#teamTooltip").css("top", e.pageY - 40).css("left", e.pageX + offset);
		};
	});
	
// Leadership Popup Animations

	var offset = 20;
	
	$(".leadT li").hover(function(e) {
		
		var title = $(this).text();
		
		$('<div id="leadTooltip"><b>'+ title +'</b></div>').css("top", e.pageY - 160).css("left", e.pageX + offset).appendTo("body").hide().fadeIn(400);
	}, function() {
		$("#leadTooltip").remove();
	});
	
	$(".leadT li").mousemove(function(e) {
		var windowWidth = $(window).width();
		var tooltipWidth = $("#leadTooltip").width();
		
		if(windowWidth - e.pageX <= tooltipWidth){
			$("#leadTooltip").css("top", e.pageY - 40).css("left", e.pageX - 130);
		}else{
			$("#leadTooltip").css("top", e.pageY - 40).css("left", e.pageX + offset);
		};
	});

// Facilities Mouse Follow Animations
	
	$(".fac li").click(function(){
		return false;
	});
	
	$(".fac li").hover(function(e) {
		
		var href = $(this).attr("class");
		var title = $(this).text();
		
		$('<div id="facTooltip"><b>'+ title +'</b><img src="../images/' + href + '.jpg" alt="" /><div id="facTooltipTail"></div></div>').css("top", e.pageY - 60).css("left", e.pageX + offset).appendTo("body").hide().fadeIn(400);
	}, function() {
		$("#facTooltip").remove();
	});
	
	$(".fac li").mousemove(function(e) {
		var windowWidth = $(window).width();
		var tooltipWidth = $("#facTooltip").width();
		
		if(windowWidth - e.pageX <= tooltipWidth + 100){
			$("#facTooltipTail").removeClass("tailLeft").addClass("tailRight");
			$("#facTooltip").css("top", e.pageY - 80).css("left", e.pageX - 220);
		}else{
			$("#facTooltip").css("top", e.pageY - 80).css("left", e.pageX + 110);
			$("#facTooltipTail").removeClass("tailRight").addClass("tailLeft");
		};
	});

	$('#imageSlider').imageSlider({ hideCrumbs : false, prevBtn : $("#sliderControlsWrap a.prevImg"), nextBtn : $("#sliderControlsWrap a.nextImg"), loop : true, animationTimeout: 3000 });
	$('.welcomeSlider').imageSlider({ loop : true, crumbs : false, animationTimeout: 3000 });
	
// Portfolio Bubbles
/*
	var bubble = $('<div class="portfolioBubble"><p>Previous Portfolio Item</p></div>');

	$("#portfolio_prev").parent().append(bubble).find(".portfolioBubble").css("opacity", 0);
	
	$("#portfolio_prev, #portfolio_next").hover(function(){
	
		$(".portfolioBubble").css({"top" : $(this).position().top - 50, "left" : $(this).position().left - $(".portfolioBubble").width() / 2}).text($(this).attr("title")).stop().animate({ opacity : 1}, 300);
		
	}, function(){
	
		$(".portfolioBubble").stop().animate({ opacity : 0 }, 300);
		
	});
*/
	
}); 











