// Mobius New Media - Global Site Scripts

// scrolling background image
var newleft = 0;
function scrollingBg() {
$('body').css({
	backgroundPosition: '0 ' + newleft + 'px'
});
newleft -= 1;
}

// toolip basic functionality
function setQTip(){
	
	$('a.tooltip').qtip({
	   show: 'mouseover',
	   hide: 'mouseout',
	   position: {
			  corner: {
				 target: 'bottomMiddle',
				 tooltip: 'topMiddle'
			  }
		   },
	   style: {
				  border: {
					 width: 3,
					 radius: 3,
					 color: '#b2cee5'
				  },
				  padding: 5, 
				  background: '#fff',
				  color: '#006ecc',
				  fontWeight: 'bold',
				  width: 150,
				  fontSize: '1.1em',
				  textAlign: 'center',
				  tip: true, // Give it a speech bubble tip with automatic corner detection
				  name: 'dark' // Style it according to the preset 'cream' style
			   }
	});	
}

// opacity rollovers
var onMouseOutOpacity = .9;
function setOpacity(){
$('.ul_work img, .ul_services img, #thumbs ul.thumbs li, #ul_work_sidebar .ul_work li, #ul_work_footer .ul_work li').opacityrollover({
	mouseOutOpacity:   onMouseOutOpacity,
	mouseOverOpacity:  1.0,
	fadeSpeed:         'fast',
	exemptionSelector: '.selected'
});
}

// Random Background Image for Work Section
var upperLimit = 10;
var imageRootName ="/images/randomBgs/body-";
var imageNumber = Math.floor(Math.random()*upperLimit);
var imageFullName= imageRootName + imageNumber + ".jpg";
function randomBgImg() {
	var theURL = "url(" + imageFullName + ")";
	//alert(theURL);
	$('#body_work #mainContentWrap, #body_blog #mainContentWrap').css( 'background-image' , theURL );
	return;
}

// project cycler function
function setProjectCycle() {
	
	// basic functionality options
	$('#projectSlider').cycle({
		fx:    'scrollVert',
		speed:  1000,
		timeout:  10000,
		pause:  1,
		pager: '#projectSliderNav',
		next: '#projectSliderNext',
		prev: '#projectSliderPrev',
		easeIn:  'easeInOutQuad', 
    	easeOut: 'easeInOutQuad'
 	});
	
	// slider navigations
	var sliderNavHeight = $('#projectSliderNav').height() + 36;
	$('#projectSliderNext').css('top', sliderNavHeight + 'px');
	if ( $('#projectSliderNav').children().size() == 1 ) {
		$('#projectSliderNav,#projectSliderPrev,#projectSliderNext').hide();
	}
	
	// navigation tooltips
	var navLinksSize = $('#projectSliderNav > a').size();
	var projectSlidesSize = $('#projectSlider > li').size();
	/*$('#projectSliderNav > a').each(function(){ // titles only
		var navLinkIndex = $('#projectSliderNav > a').index( $(this) ); // get the index of the link
		var matchingProjectSlide = $('#projectSlider > li').get( navLinkIndex ); // find the matching project slide
		var theTitleAttr = $(matchingProjectSlide).children('a').attr('title');
		$(this).attr('title',theTitleAttr).addClass('sidetooltip');
	});*/
	$('#projectSliderNav > a').each(function(){
		var navLinkIndex = $('#projectSliderNav > a').index( $(this) ); // get the index of the link
		var matchingProjectSlide = $('#projectSlider > li').get( navLinkIndex ); // find the matching project slide
		var theTitleAttr = $(matchingProjectSlide).children('a').attr('title');
		var theOrigImgSrc = $(matchingProjectSlide).children('a').children('img').attr('src');
		var theNewImgSrc = theOrigImgSrc.replace('medium', 'small');
		var navLinkId = $(this).attr('id','imgtooltip'+ navLinkIndex);
		//alert('my index is ' + navLinkIndex + ' and my img src is ' + theNewImgSrc);
		
		$(navLinkId).qtip({
		   content: '<img src="' + theNewImgSrc + '" alt="'+ theTitleAttr +'" />',
		   show: 'mouseover',
		   hide: 'mouseout',
		   position: {
				  corner: {
					 target: 'rightMiddle',
					 tooltip: 'leftMiddle'
				  }
			   },
		   style: {
					  border: {
						 width: 3,
						 radius: 3,
						 color: '#b2cee5'
					  },
					  padding: 0, 
					  width: 150,
					  height:90,
					  tip: true, 
					  name: 'dark' 
				   }
		});	
		
	});
	
}

// lightbox for images and video
function setLightbox() {
	$("a.lightbox").fancybox({
		'padding'		:	5,
		'speedIn'		:	600, 
		'speedOut'		:	300, 
		'overlayShow'	:	true,
		'overlayOpacity':	.7,
		'overlayColor' 	:	'#000',
		'titleShow' 	: true,
		'titlePosition'	: 'inside'
	});  
}

// truncate long card titles to fit
function truncateTitles() {
	$('.ul_work > li > h3 > a').truncate({
		width: '140',
		after: '&hellip;',
		center: false,
		addtitle: true // adds title attribute with original content
	});
}



$(document).ready(function() {
	
	// global site scripts
	// scrolling background image call				   	
	var timer = setInterval(scrollingBg, 100);
		
	// slideshow
	setProjectCycle();
	
	// tooltips for image thumbnails
	setQTip();
	
	// run opacity hover function
	setOpacity();
	
	// set lightbox
	setLightbox();
	
	// footer social media
	$('.socialWindow').hide();
	$('.socialMediaSprite > li').hoverIntent(function() {
		$(this).children('.socialWindow').slideDown();
	}, function() {
		$(this).children('.socialWindow').slideUp('');
	});
	
	// blog link animation
	$('#nav_blog').hoverIntent(function() {
		$(this).animate({ top: '-10px' }, 500, 'swing', function() {
			  //callback
		  });
	}, function() {
		$(this).animate({ top: '-40px' }, 300, 'swing', function() {
			  //callback
		  });
	});
	
	// footer rss feed
	$('#social_wordpress').rssfeed('http://mobiusnm.com/blog/?feed=rss2', {
		limit : 1,
		header : false,
		titletag : 'h5',
		content : false
	});
	
	if ( $('#body_home')[0] ) { // scripts for home page
	
		// blog feed on homepage
		$('#newsfeed').rssfeed('http://mobiusnm.com/blog/?feed=rss2', {
			limit : 4,
			header : false,
			titletag : 'h4',
			content : false
		});
	
	}
	
	if ( $('#body_work')[0] ) { // scripts for work pages
		
		// truncate card titles
		truncateTitles();
		
		// random bg image 
		randomBgImg();
		
	}
	
	if ( $('#body_about')[0] ) { // scripts for the about pages
		
		// show & hide sidebar content
		$(".hide").hide();
		$('h3.showHideNext').toggle(function(){
			$(this)
			.toggleClass('showing')
			.next('.hide').slideToggle('slow');
		}, function(){
			$(this)
			.toggleClass('showing')
			.next('.hide').slideToggle('slow');
		});
		
		// the team and partners show/hide content
		$('.ul_staff > li > h4').not('.ul_staff > li.simple > h4').each(function() {
			var theHeaderLink = '#' + $(this).attr('id');
			var segmentOriginalHeight = $(theHeaderLink).parent('li').height(); // get starting height
			var theTargetDivId = $(theHeaderLink).parent('li').attr('id'); // get ID of segment
			var myMatchingLink = '#teamLink-' + theTargetDivId; // get ID of matching sidebar link
			$('#' + theTargetDivId).css('height','70px'); // set short height to hide content initially
			$(theHeaderLink).click(function(){
				if ( $(theHeaderLink).hasClass('showing') ) {
					$(theHeaderLink).removeClass('showing'); // change arrow back
					$('#' + theTargetDivId).animate({ 'height' : '70px'}, 500, 'swing'); // hide content
				} else {
					$(theHeaderLink).addClass('showing'); // change arrow
					$('#' + theTargetDivId).animate({ 'height' : segmentOriginalHeight}, 500, 'swing'); // show content
				}
			});
			$(myMatchingLink).click(function(){
				$(theHeaderLink).addClass('showing'); // change arrow
				$('#' + theTargetDivId).animate({ 'height' : segmentOriginalHeight}, 500, 'swing'); // show content
			});
		});
	
	}
	
	if ( $('#body_contact')[0] ) { // scripts for the about pages
		
		// show & hide sidebar content
		$(".hide").hide();
		$('h3.showHideNext').toggle(function(){
			$(this)
			.toggleClass('showing')
			.next('.hide').slideToggle('slow');
		}, function(){
			$(this)
			.toggleClass('showing')
			.next('.hide').slideToggle('slow');
		});
		
		// the team and partners show/hide content
		$('.ul_staff > li > h4').not('.ul_staff > li.simple > h4').each(function() {
			var theHeaderLink = '#' + $(this).attr('id');
			var segmentOriginalHeight = $(theHeaderLink).parent('li').height(); // get starting height
			var theTargetDivId = $(theHeaderLink).parent('li').attr('id'); // get ID of segment
			var myMatchingLink = '#teamLink-' + theTargetDivId; // get ID of matching sidebar link
			$('#' + theTargetDivId).css('height','70px'); // set short height to hide content initially
			$(theHeaderLink).click(function(){
				if ( $(theHeaderLink).hasClass('showing') ) {
					$(theHeaderLink).removeClass('showing'); // change arrow back
					$('#' + theTargetDivId).animate({ 'height' : '70px'}, 500, 'swing'); // hide content
				} else {
					$(theHeaderLink).addClass('showing'); // change arrow
					$('#' + theTargetDivId).animate({ 'height' : segmentOriginalHeight}, 500, 'swing'); // show content
				}
			});
			$(myMatchingLink).click(function(){
				$(theHeaderLink).addClass('showing'); // change arrow
				$('#' + theTargetDivId).animate({ 'height' : segmentOriginalHeight}, 500, 'swing'); // show content
			});
		});
	
	}
	
	if ( $('#ul_work_footer')[0] ) { // if work list in footer exists, do this stuff
		
		footerWorkScroller();
		
	}
	
	if ( $('#body_contact')[0] ) {  // scripts for contact pages
	
		// form validation - visual application
		$("#contactForm").validate({
			rules: {
			name: "required",
			state: "required",
			email: {
						required: true,
						email: true
					},
			phone: "required",
			zip: {
						minlength: 5
					},
			project_details: "required"
			},
			messages: {
			name: "Please enter your name.",
			phone: "Please enter your phone number.",
			email: "Please enter your email address.",
			project_details: "Please enter your comments."
			}
		});		
	}
	
}); // close doc ready

// project listing scroller at bottom of page
function footerWorkScroller() {
$("#ul_work_footer").scrollable({ speed : 500, mousewheel: true });
	var footerScrollableApi = $("#ul_work_footer").data("scrollable");
	var theActiveProj = $( 'li.activeProject' );
	var activeProjIndex = $('.ul_work li').index( theActiveProj );
	footerScrollableApi.seekTo(theActiveProj, 700); // scroll to the active project on the work detail page
	$('#goToBeginning').click(function(){
		footerScrollableApi.begin(2000);
	});
}

