/* Get In Touch Drawer Effect */

$(document).ready(function(){
	
	/**
	 * meta
	 */
	function getDevicePixelRatio() {
		if (window.devicePixelRatio == undefined) {
			return 1;
		}
		return window.devicePixelRatio;
	};
	
	// devices
	var iOS = navigator.userAgent.match(/(iPhone|iPod|iPad)/);
	var android = navigator.userAgent.match(/(Android)/);
	
	var isMobile = false;
	if (iOS != null) {
		isMobile = true;
	} else if (android != null) {
		isMobile = true;
	};
	if (isMobile) { $('body').addClass('mobile'); };
	
	if ((getDevicePixelRatio()==2) && iOS) {
		var contentStr = 'width=1000';
	} else if (iOS) {
		var contentStr = 'width=1000';
	}  else if (android) {
		var contentStr = 'width=1000';
	} else {
		var contentStr = 'width=1000';
	};
	
	$('#vpmeta').attr('content', contentStr);
	
	
	/**
	 * header dropdown
	 */
	$("header .show_hide").show().click(function(){
		$(this).parent().find('.hiddenContainer').slideToggle('fast');
   });
   
	
	
   /**
    * carousels
    */
   $('.carousel').each( function(i) {

   	var self = $(this);
   
		// shortcut to carousel
		var slides = $(this).find('.slides');
		var pager = $(this).find('.pager ul');
		
		// shortcuts
		var hasVideo = slides.children('.video').length > 0;
		
		//strip pager of existing content
		pager.empty();
		
		// get the slide and apply the video functionality if needed...
		if (hasVideo) {
			slides.children('.video').each(function(){
			
				// first, if it's mobile, remove the link and show the overlay...
				if (isMobile) {
			
					$(this).find('a.videolink').remove().end().find('a.close').remove();
				
				} else {
		
					// open the video layer
					$(this).find('a.videolink').click(function(){
						if (slides.children().size() > 1) {
							slides.cycle('pause');
						};
						$(this).hide().siblings('.overlay').show();
					});
				
					// close the video layer
					$(this).find('.overlay a.close').click(function(){
						$(this).parents('.overlay').hide().siblings('a.videolink').show();
						if (slides.children().size() > 1) {
							slides.cycle('resume');
						};
					});
			
				};	
				
			});
		};
			
		
		// only activate if more than one slide
		if (slides.children().length > 1) {
			
			// set update pager link - applies 'active' class as needed
			$.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) { 
				$(pager).find('li').removeClass('active')
					.filter('li:eq('+currSlideIndex+')').addClass('active'); 
			};
			
			// set unique carousel ID
			var carouselID = 'carousel' + i;
			
			// set the timeout based on video or not
			var timeout = (isMobile && hasVideo) ? 0 : 7500;
			
			// temp:
			if (!(isMobile && hasVideo)) {
			
				// create the carousel nav
				$(this).attr('id', carouselID);
				slides.cycle({
					fx: self.attr('fx'),
					prev: '#' + carouselID + ' .pagination a.prev',
					next: '#' + carouselID + ' .pagination a.next',
					pause: 0,
					delay: 9000,
					timeout: timeout,
					speed: 1500,
					pager: '#' + carouselID + ' .pager ul',
					activePagerClass: 'active',
					pagerAnchorBuilder: function(idx, slide) {
				
						var sel = '#' + carouselID + ' .pager ul li:eq('+idx+') a';
						if (!$(sel).length) {
							var pagerAnchor = self.find('.pager ul').append('<li><a href="javascript:void(null);"></a></li>');
						};
						return sel;
					
					},
					before: function(current, next, options, forwardFlag) {
					
						$(next)
							.find('.headline p').css('opacity','0')
								.filter(':even').css('left','-200px').end()
								.filter(':odd').css('left','30px').end()
							.end()
							.find('h3').css('opacity','0')
							.end()
							.find('a.button').css('opacity','0').css('top','5px');
					
						if (!isMobile) {
							if ($(next).hasClass('video')) {
								$(next).find('.overlay').hide().siblings('a.videolink').hide();
							};
						};
					
						slides.cycle('resume');
					},
					after: function(current, next, options, forwardFlag) {
					
						var paragraphs = $(next).find('.headline p');
						animateCarouselHeadline(next, 0, paragraphs.length);
					
						// show all
						if (!isMobile) {
							slides.children('.video').find('a.videolink').show();
						};
					
						slides.cycle('resume');
					}
				});
			
			} else {
				// remove pagination
				$(this).find('.pagination').remove();
			};
			
		} else {
			$(this).find('.pagination, .pager ul').remove();
		};
	
	});

	function animateCarouselHeadline(slide, count, max) {
		
		var elem = $(slide).find('.headline p:eq('+ count++ +')');
		elem.animate({
			opacity:1,
			left:'0px',
			duration:500*count
		});
			
		if (count < max) {
			var timeout = setTimeout(function(){ animateCarouselHeadline(slide, count, max) }, 100);
		} else {
			$(slide).find('h3').delay(500).fadeTo(1000, 1, function(){
				$(this).next().animate({
					opacity:1,
					top:'0px',
					duration:2000
				});
			});
		};
		
	};
	
	
	
	/**
    * load the careers XML
    */
   // load the XML
   var feedURL = ($('#careers').size() > 0) ? '/data/cats-long.xml' : '/data/cats-short.xml';
   
	$.ajax({
		type: "GET",
		url: feedURL,
		dataType: "xml",
		success: function(xml) {
		
			$('catsFeed', xml).each(function(){
				
				var catsdata = $(this);
				
				var topJobList = catsdata.find('topJobList');
				topJobList.children().each(function(index) {
					
					// create and append the footer link
					var label = $(this).attr('label');
					var link = $(this).find('link').text();
					var description = $(this).find('description').text();
					
					// careers page
					if ($('#careers').size() > 0) {
					
						var newRow = ((index % 2) == 0);
						if (newRow) {
							row = $('#topjobs').append('<div class="row"><div class="clearfix"></div></div>').find('div.row:last-child');
						};
						
						var entry = '<div class="col6"><h3><a href="'+ link +'" target="_blank">'+ label +'</a></h3><p>'+ description +'</p><a class="arrow" href="'+ link +'" target="_blank">See more details</a></div>';
						row.find('div.clearfix').before(entry);
						
					};
					
					// footer
					var li = '<li><a href="'+ link +'" target="_blank">'+ label +'</a></li>';
					$('footer ul.jobs').append(li);
					
				});
				
				// careers page
				if ($('#careers').size() > 0) {
				
					var categoryList = catsdata.find('categoryList');
					categoryList.children().each(function(index) {
				
						// loop through each category
						var category = $(this);
						var categoryLabel = $(this).attr('label');
						var jobs = $(this).children();
						
						// pair em up...
						var newCol = ((index % 2) == 0);
						if (newCol) {
							col = $('#alljobs').find('div.clearfix').before('<div class="col3"></div>').parent().find('div.col3').last();
						};
						
						// create the category heading and the ul...
						col.append('<h3>'+ categoryLabel +'</h3><ul class="bordered"></ul>');
						
						jobs.each(function(){
							var label = $(this).attr('label');
							var link = $(this).find('link').text();
							var entry = '<li><a href="'+ link +'" target="_blank">'+ label +'</a></li>';
							col.find('ul.bordered:last-child').append(entry);
						});
					});
				
				};
					
			});
		
		}
	});
	
	
});




