if(typeof jQuery != 'undefined') {
	$(function() {
		$('body').addClass('jsenabled');
		var prodImgs = [
			// product 1 is already hard coded into the page
			'img/product2.png',
			'img/product3.png',
			'img/product4.png',
			'img/product5.png',
			'img/product6.png'
		];
		$.each(prodImgs, function() {
			$('<img src="'+this+'" alt="" />').appendTo('.thekno #gallery .main_image');
		});

		// product slideshow functionality
		if(jQuery().cycle) {
			$('#gallery .main_image *').wrapAll('<div class="slideshow"></div>');
			$('#gallery .main_image').append('<a class="prev"></a><a class="next"></a>');
			if(!(jQuery.support.cssFloat)) {var speed = 1;}
			else {var speed = 'normal';}
			
			var firstLoad = false;
			$('#gallery .slideshow').cycle({
				speed: speed,
				startingSlide: 0,
				timeout: 0,
				pager: '#gallery .thumbs',
				prev: '.prev', 
				next: '.next',
				before: function(currSlideElement, nextSlideElement, options, forwardFlag){
					if(options.nextSlide != 0 || options.currSlide != 0){
					//#main_product is found on the-kno.html and is the first image displayed in the image rotation
						if(firstLoad){
							$('#main_product').hide();
						}
					} else {
						$('#main_product').show();					
					}
					firstLoad = true; //We do this so on the initial load, the first image does not dissappear.
				},
				pagerAnchorBuilder: function(idx, slide) { 
					// return selector string for existing anchor 
					return '#gallery .thumbs :nth-child(' + (idx+1) + ')'; 
				}
			});
			// home page hero shot cycle
			//prepare stage by adding the necessary elements
			$('.homepage #main').children().wrapAll('<div class="text"></div>');
			$('.homepage #main').append('<div id="slideshow"><a class="prev"></a><a class="next"></a><div class="holder"></div></div>');
			$('#slideshow').append('<div id="product_area"></div>');

			var heroShots = [
				'img/lifestyle/1_kno_library2.jpg',
				'img/lifestyle/2_kno_dorm.jpg',
				'img/lifestyle/3_kno_pathway.jpg',
				'img/lifestyle/4_kno_library.jpg',
				'img/lifestyle/5_kno_classroom.jpg'
			];
			$.each(heroShots, function() {
				$('<img src="'+this+'" alt="" />').appendTo('.homepage #slideshow .holder');
			});
			$('.homepage #slideshow .holder').cycle({
				timeout: 5000,
				prev: '.prev', 
				next: '.next'
			});
		}				
		
		//Timer to perform the rotation on the-kno.html
		if($('.thekno #gallery .slideshow').length > 0) {
			setInterval(function(){
				$('.thekno #gallery .slideshow').cycle('next');
			}, 5000);
		}

		//On the-kno.html, allows display of the first image for rotation
		$('#main_product').show();	
		
		// home page product shot functionality
		if(!(jQuery().cycle)) {
			$('.homepage #main').append('<div id="slideshow"></div>');
		}

		$('#slideshow').append('<img class="product" src="'+$('.homepage .thumbs a:first-child').attr('href')+'" />');
		$('.homepage .thumbs a').click(function() {
			$('#product_area').show();
			
			$('.homepage #slideshow .holder').cycle('pause');
			var imgPath = $(this).attr('href');
			if(jQuery.support.cssFloat) {$('#slideshow > .product').hide().attr('src',imgPath).fadeIn('fast');}
			else {$('#slideshow > .product').attr('src',imgPath).show();}
			$('.homepage #slideshow a').hide();
			return false;
		});
		// dismiss product images on click anywhere and resume background slideshow
		if($('body').is('.homepage')) {
			$(document).click(function() {
				$('#slideshow > .product').hide();
				$('#product_area').hide();
				$('.homepage #slideshow .holder').cycle('resume');
				$('.homepage #slideshow a').show();
				
			});
			$('.homepage .product').click(function() {return false;});
		}
		// overlay functionality
		$('body.form fieldset').append('<div class="bottom" />');
		if(jQuery().overlay) {
			$('body').append('<div id="overlay"><div class="overlayContent"><div><iframe style="display: none;" id="iframe" scrolling="no" frameborder="0" src="iframe_back_block.html" /></div></div></div>');
			// overlay: STAY IN THE KNO
			$('#header ul li:last-child a').attr('rel','#overlay').overlay({
				mask: 'black',
				left: '50%',
				absolute: true,
				closeOnClick: false,
				onBeforeLoad: function() { 
	    		var overlay = this.getOverlay();
	    		var content = overlay.find('.overlayContent > div');
	    		content.find('iframe').attr('src',this.getTrigger().attr("href")).css('display','block').load(function() {
					var viewportHeight = $(window).height();
                    var overlayHeight = overlay.height();
    				/*overlay.css('position','fixed').animate({
    					top: (viewportHeight/2)-(overlayHeight/2)+'px'
    				});*/
    			});
	      		overlay.addClass('signup');

          },
	      onLoad: function() {
    			$(window).bind("resize", resizeWindow);
	    		var overlay = this.getOverlay();
					function resizeWindow() {
	    		var overlayHeight = overlay.outerHeight();
						var viewportHeight = $(window).height();
	    			overlay.css('top',(viewportHeight/2)-(overlayHeight/2)+'px');
					}
				},
				onClose: function() {
                    var overlay = this.getOverlay();
                    //overlay.removeClass().find('iframe').removeAttr('src'); //disabled to prevent issues with back being presseds
                    $('#iframe').attr("src", "iframe_back_block.html");
                    overlay.removeClass('signup');
                
				}
			});
			// overlay: JOIN THE BOARD
				// add a special class to developer join link for later use
			$('body.developers #main a[rel~=join]').addClass('dev');
			  // initialize overlay
			$('a[rel~=join]').addClass('join').attr('rel','#overlay');
			$('a.join').overlay({
				mask: 'black',
				left: '50%',
				absolute: true,
				closeOnClick: false,
				onBeforeLoad: function() { 
	    		var overlay = this.getOverlay();
	    		var trigger = this.getTrigger();
                var content = overlay.find(".overlayContent > div");
                content.find('iframe').attr('src',trigger.attr("href")).css('display','block').load(function() {
		      	//$('#overlay').find('#join').append('<div class="bottom"></div>');
					var viewportHeight = $(window).height();
	    			var overlayHeight = overlay.height();
	    			/*overlay.css({
	    				position: 'fixed',
	    				top: (viewportHeight/2)-(overlayHeight/2)+'px'
	    			});*/
                });
                overlay.addClass('join').append('<div class="bottom"></div>');
                if(trigger.is('.dev')) {overlay.addClass('dev');}
	      },
	      onLoad: function() {
    			$(window).bind("resize", resizeWindow);
	    		var overlay = this.getOverlay();
	    		var overlayHeight = overlay.outerHeight();
					function resizeWindow() {
						var viewportHeight = $(window).height();
	    			overlay.css('top',(viewportHeight/2)-(overlayHeight/2)+'px');
					} },
				onClose: function() {
                    $('#overlay > .bottom').remove();
                    var overlay = this.getOverlay();
                    //overlay.removeClass().find('iframe').removeAttr('src');
                    $('#iframe').attr("src", "iframe_back_block.html");
                    overlay.removeClass('join');
				}
			});

      // overlay: VIDEO
			$('a.video').attr('rel','#overlay');
			$('a.video').overlay({
				mask: 'black',
				left: '50%',
				absolute: true,
				closeOnClick: false,
				onBeforeLoad: function() { 
	    		var overlay = this.getOverlay();
	    		var trigger = this.getTrigger();
	        var content = overlay.find(".overlayContent > div");
	      	content.find('iframe').attr('src',trigger.attr("href")).css('display','block').load(function() {
				var viewportHeight = $(window).height();
	    		var overlayHeight = overlay.height();
	       	});
	      	overlay.addClass('video').append('<div class="bottom"></div>');
	    		if(trigger.is('.dev')) {overlay.addClass('dev');}
	      },
	      onLoad: function() {
    			$(window).bind("resize", resizeWindow);
                    var overlay = this.getOverlay();
                    var overlayHeight = overlay.outerHeight();
                    function resizeWindow() {
						var viewportHeight = $(window).height();
                        overlay.css('top',(viewportHeight/2)-(overlayHeight/2)+'px');
					}
				},
				onClose: function() {
		      $('#overlay > .bottom').remove();
	    		var overlay = this.getOverlay();
	    		//overlay.removeClass().find('iframe').removeAttr('src');
	    		$('#iframe').attr("src", "iframe_back_block.html");
			overlay.removeClass('video');
				}
			});
		}

		// faq highight functionality
		$('.faq #main ul li a').click(function() {
			var id = $(this).attr('href');
			$(id).css('background-color','#FFC').animate({ backgroundColor: "white" }, 1500);
		});
		
		// add new functions here
		$('#job-list li a').each(function(index) {
		  $(this).click(function(e) {
		  	// Act on the event
			$.ajax({
			  url: e.target.href,
			  type: 'get',
			  dataType: 'html',
			  success: function(data, textStatus, xhr){
					if (data === "") {
						$('#job-descriptions').html("<p>No content found.</p>");
					} else {
						$('#job-descriptions').html(data);	
					}
				}
			});
			
			e.preventDefault();
		  });
		});
	});
    
    function showVideo(elementId, videoId, width, height){ //Shows video depending on its user agent
            var agent=navigator.userAgent.toLowerCase();
            var is_iphone = (agent.indexOf('iphone') != '-1');
            var is_ipad = (agent.indexOf('ipad') != '-1');

            var youtubehtml = "";

            if (is_ipad) {
                youtubehtml = "<video src='http://www.vimeo.com/play_redirect?clip_id=" + videoId + "' controls='controls' width='" + width + "' height='" + height + "'></video>";
            } else {
                youtubehtml = '<object width="' + width + '" height="' + height +'"><param name="wmode" value="transparent"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=' + videoId + '&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=1&amp;color=00ADEF&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=' + videoId + '&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=1&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="' + width + '" height="' + height + '" wmode="transparent"></embed></object>';
            }
            
            $(elementId)[0].innerHTML = youtubehtml;
                    
    }
}
