var postLoadImageArray = new Array();
var postLoadArrayComplete = false;

//load image data into associative array
function registerPostLoadImage(src, dom, groupId){
	if (!postLoadArrayComplete){
		var imageObject = new Object();
		imageObject["src"] = src;
		imageObject["domElement"] = dom;
		imageObject["groupId"] = groupId;
		
		postLoadImageArray.push(imageObject);
	}
};

function moveUp(id) {
	$('#'+id+' .move-block').css({top:'-125px'});	
}
function moveDown(id) {
	$('#'+id+' .move-block').css({top:'0px'});	
}

function loadImageGroup(id){
	for (var i=0; i<postLoadImageArray.length;i++){
		if (postLoadImageArray[i]["groupId"] == id){
			postLoadImageArray[i]["domElement"].src = postLoadImageArray[i]["src"];
		}
	}
};

$(document).ready(function() {
	$('body').bodyHeight();
	$('.va-js,').vAlign();
});

//Fixed header hack for Ipad/Iphone
if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1)) {
	function showNavBar() {
	   var scroll_y = $(window).scrollTop();
	   $("header").css({ top: scroll_y + "px" });
	 }
	 $(window).scroll( "scroll", showNavBar );
}

//Lightbox escape for all modals
$(document).keyup(function(e) {
  if (e.keyCode == 27) { $('.vimeo-lightbox-close,.speaker-lightbox-close,.contact-lightbox-close').click(); } 
});

//blank variable for hash tagging
var hash = '';

//when pushing articles to the array, this holds z index
var getCurrentIndex = function ( items ) {
  var y = ($(window).scrollTop()+ 62),
      maxCount = items.length,
      index = maxCount;
  for ( var i=0; i < maxCount; i++ ) {
    if ( items[i] -20 > y ) { 
      index = i;
      break;
    }
  }
  return index;
};

//Scrolling Funciton with easing
var scrollTo = function ( y, $article ) {
    $("html,body").stop().animate({scrollTop: y - 82}, {
     duration: 1200,
     easing: 'easeInOutQuart',
     queue: false,
     complete: function(){
        if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1)) {
			var scroll_y = $(window).scrollTop();
			$("header").css({ top: scroll_y + "px" });
			window.location.hash = hash;
		} 
	   if ( $article && ($.browser.msie  && parseInt($.browser.version) == 7) ) {
		//alert('ie7')
		//window.location.hash = hash;
       }
	  
	   else if ($article) {
   			window.location.hash = hash;
		}
	   return false;
     }
  });
};


$(function(){

  //setup variables for article management
  var articleY = [],
	  sectionid = $('.hash'),		
      $articles = $('.hashablearticle'),
      articleCount = $articles.length,
	  $mainNav = $('#main-nav'),
	  $mainNavItems = $mainNav.find('a');

	  //scrolling functions for articles and dynamic nav activation
	  $(window).scroll(function(){
		
		var cI = (getCurrentIndex(articleY)-1),
			hashI = (getCurrentIndex(articleY)),
			activeNav = $mainNavItems.filter('.activeNav'),
			temphash = activeNav.attr("href"),
			selectedI = $mainNavItems.index( activeNav );
		
		//Change the active menu with scroll	
		if ( cI != selectedI && cI != -1 ) {
		  activeNav.removeClass('activeNav');
		  $mainNavItems.eq( cI ).addClass('activeNav');
		}
					
	  });

	// get article offset
	$articles.each(function(){
	articleY.push($(this).offset().top);
	
});
	
	
//trigger window scroll activ nav is set
$(window).scroll();

// main nav article scrolling
$mainNavItems.click(function(){
	var i = $mainNavItems.index($(this)),
        $target =  $articles.eq(i),
		temphash = $(this).attr("href"),
        y = $target.offset().top - (0);	
		hash = temphash;
    	scrollTo( y, $target );
    return false;
  });
   
  
});



// Make whole event divs clickable on homepage //INTERNAL
$(".event-block").click(function(){
   window.location=$(this).find("a").attr("href"); return false;
});

//EXTERNAL
/*$(".purchase-tickets,#venue-map").click(function(){
	window.open=$(this).find("a").attr("href").attr("target", "_blank"); return false;
});*/
	
// Vertical Align funtion
$.fn.vAlign = function() {
	return this.each(function(i){
	$('.v-aligned').css('top', 0);
	$('.v-aligned-events').css('top', 0);
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = (ph - ah) / 2;
	$(this).css('margin-top', mh);
	});
};

// set the height of the body based on size of the browser frame
$.fn.bodyHeight = function() {
	return this.each(function(i){
	var ch = $('#content-container').height();
	var sh = $(window).height();
	var bHeight = (ch + sh) - 125;
	$(this).css('height', bHeight);
	$('#content-container').css('height', bHeight - 82);
	});
};

// Hover effect for Past Events
var pastEventsHover = {
	init: function() {
		var hoverTarget = $('.past-events-clk');
		hoverTarget.hover(
			function () {
				$(this).children().children('.past-events-info').show();
				}, 		
			function () {
				$(this).children().children('.past-events-info').hide();          
			}
		);	
	}
};
pastEventsHover.init();

//Funtion for event ticket click
/*$(function(){
	$(".event-slide").click(function(event){
		event.preventDefault();
		var TicketsTop = $('#general-pricing-img').offset().top;
		$("html,body").stop().animate( {scrollTop: TicketsTop - 82}, {
		 duration: 1200,
		 easing: 'easeInOutQuart'});
	});
});*/

// Past Events Lightbox Funtion
$(function(){

	$(".past-events-link").click(function(event){
		event.preventDefault();
		var id = $(this).attr('id'),
		targetPanel = $(".panel-wrapper").filter("#"+id),
		topValue =  $(window).scrollTop();
		$(".panel-wrapper").css("top", topValue + 80 );
		$(".lightbox").fadeIn(200);
		targetPanel.fadeIn(200);
		
		var initialSpkrFadeIn = 1000;
		var itemSpkrInterval = 5000;
		var fadeSpkrTime = 1000;
		var loopActive = true;
		var currentSpk = 0;
		var targetImgCanvas = targetPanel.find('.speaker-lightbox-right-img');
		var targetThumbCanvas = targetPanel.find('.speaker-lightbox-grid');
		var numberOfThumbs = targetThumbCanvas.children('.imgClick').length;
		var numberSpkrOfItems = targetImgCanvas.children('.rotating-item2').length;
							
			var SpkrRotator = {
				init: function() {

							targetImgCanvas.children('.rotating-item2').eq(currentSpk).stop().fadeTo("initialSpkrFadeIn", 1.0);
										
								var infiniteLoop = setInterval(function(){
								
									if (loopActive == true){
										
									targetImgCanvas.children('.rotating-item2').eq(currentSpk).stop().fadeTo("fadeSpkrTime", 0.0);
			
									if(currentSpk == numberSpkrOfItems -1){
										currentSpk = 0;
									}else{
										currentSpk++;
									}
								
									targetImgCanvas.children('.rotating-item2').eq(currentSpk).stop().fadeTo("fadeSpkrTime", 1.0);
									
									}
									else{
									   return;
									}	
									
								}, itemSpkrInterval);
				}
			};
			SpkrRotator.init();
			
			$(".speaker-lightbox-close").click(function(){
				$(".lightbox, .panel-wrapper").fadeOut("fast", function() {
					$(".iframe").remove();
					loopActive = false;
				});
			});

			$(function(){
				$(".imgClick").click(function(event){
					var thumbNo = $(this).index();
					targetImgCanvas.children(".rotating-item2").stop().fadeTo("fast", 0.0);
					targetImgCanvas.children('.rotating-item2').eq(thumbNo).stop().fadeTo("initialSpkrFadeIn", 1.0); 
					loopActive = false;
				});
			});
			
			var speakerGridHover = {
				init: function() {
					var hoverTarget = $('.speaker-lightbox-grid img,.speaker-lightbox-video-title img');
					hoverTarget.hover(
						function () {
							$(this).stop().fadeTo("fast", 0.8);
							}, 		
						function () {
							$(this).stop().fadeTo("fast", 1.0);          
						} 
					);	
				}
			};
			speakerGridHover.init();

	});
});

var homepageEventHover = {
	init: function() {
		
		var hoverTarget = $('.event-hover');
		hoverTarget.hover(
			function () {
				$(this).stop().fadeTo("fast", 0.8);
				}, 		
			function () {
				$(this).stop().fadeTo("fast", 1.0);          
			} 
		);	
	}
};


$(function(){

$(".speaker-clk").click(function(){
		var id = $(this).parent().attr('id'),
		offset = $(this).offset(),
		targetPanel = $(".panel-wrapper").filter("#"+id+'-popup'),
		topValue =  $(window).scrollTop();
		$(".panel-wrapper").css("top", topValue + 80 );
		$(".lightbox").fadeIn(200);
		targetPanel.fadeIn(200);		

		var initialSpkrFadeIn = 1000;
		var itemSpkrInterval = 5000;
		var fadeSpkrTime = 1000;
		var loopActive = true;
		var currentSpk = 0;
		var targetImgCanvas = targetPanel.find('.speaker-lightbox-right-img');
		var targetThumbCanvas = targetPanel.find('.speaker-lightbox-grid');
		var numberOfThumbs = targetThumbCanvas.children('.imgClick').length;
		var numberSpkrOfItems = targetImgCanvas.children('.rotating-item2').length;
							
			var SpkrRotator = {
				init: function() {
	
							targetImgCanvas.children('.rotating-item2').eq(currentSpk).stop().fadeTo("initialSpkrFadeIn", 1.0);
								
								var infiniteLoop = setInterval(function(){
								
									if (loopActive == true){
										
									targetImgCanvas.children('.rotating-item2').eq(currentSpk).stop().fadeTo("fadeSpkrTime", 0.0);
			
									if(currentSpk == numberSpkrOfItems -1){
										currentSpk = 0;
									}else{
										currentSpk++;
									}
								
									targetImgCanvas.children('.rotating-item2').eq(currentSpk).stop().fadeTo("fadeSpkrTime", 1.0);
									
									}
									else{
									   return;
									}	
									
								}, itemSpkrInterval);
				}
			};
			SpkrRotator.init();
			
			
			$(".speaker-lightbox-close").click(function(){
				$(".lightbox, .panel-wrapper").fadeOut("fast", function() {
					$(".iframe").remove();
					loopActive = false;
				});
			});
			
			$(function(){
				$(".imgClick").click(function(event){
					var thumbNo = $(this).index();
					targetImgCanvas.children(".rotating-item2").stop().fadeTo("fast", 0.0);
					targetImgCanvas.children('.rotating-item2').eq(thumbNo).stop().fadeTo("initialSpkrFadeIn", 1.0); 
					loopActive = false;
				});
			});
			
			
			var speakerGridHover = {
				init: function() {
					var hoverTarget = $('.speaker-lightbox-grid img,.speaker-lightbox-video-title img');
					hoverTarget.hover(
						function () {
							//alert('hey')
							$(this).stop().fadeTo("fast", 0.8);
							}, 		
						function () {
							$(this).stop().fadeTo("fast", 1.0);          
						}
						  
					);	
				}
			};
			speakerGridHover.init();
						
			
			var vimeoModal = {
				init: function() {
					var video = $(".speaker-lightbox-video-title");
					video.click(
						function (event) {
							event.preventDefault();
							$('.rotating-item2').hide();
							$(".iframe").remove();
							loopActive = false;
							vimeoLink=$(this).find("a").attr("href");
							$('<iframe src="' + vimeoLink + '" width="500" height="375" frameborder="0" class="iframe"></iframe>').appendTo('.speaker-lightbox-right-img');
						}  
					);	
				}
			};
			vimeoModal.init();	
	});

});

// Videos Lightbox Funtion
$(function(){
	$(".videos-clk").click(function(event){
		//event.preventDefault();
		$('.speaker-lightbox-close').click();
		topValue =  $(window).scrollTop();
		$(".vimeo-lightbox-wrapper").css("top", topValue + 200 );
		$(".lightbox").fadeIn(200);
		$(".vimeo-lightbox").fadeIn(200);
		vimeoLink = $(this).find('a').attr("href");
		$('<iframe src="' + vimeoLink + '" width="625" height="350" frameborder="0" class="iframe"></iframe>').appendTo('.vimeo-lightbox-content');
		$(".vimeo-lightbox-close").click(function(){
			$(".vimeo-lightbox,.lightbox").fadeOut("fast", function() {
				$(".iframe").remove();	
			});
		});
		
	});
});


// Contact Lightbox
$(function(){

	$("#send-message").click(function(){
	$(".lightbox").fadeIn(200);
	
	
	offset = $(this).offset(),
	topValue =  $(window).scrollTop();
	
	$(".contact-wrapper").css("top", topValue - 10 );
	$(".contact-wrapper").fadeIn(200);
		
    });

	$(".contact-lightbox-close").click(function(){
		$(".lightbox,.contact-wrapper").fadeOut("fast", function() {
			
   		});
    });
});


//Sponsored by animation 
$(window).load(function() {	

	var InfiniteRotator = 
	{
		init: function()
		{
			
			var initialFadeIn = 1000;
			var itemInterval = 7000;
			var fadeTime = 1500;
			var numberOfItems = $('.rotating-item').length;
			var currentItem = 0;

			$('.rotating-item').eq(currentItem).fadeIn(initialFadeIn);
		
			var infiniteLoop = setInterval(function(){
				$('.rotating-item').eq(currentItem).fadeOut(fadeTime);

				if(currentItem == numberOfItems -1){
					currentItem = 0;
				}else{
					currentItem++;
				}
				$('.rotating-item').eq(currentItem).fadeIn(fadeTime);

			}, itemInterval);	
		}	
	};

	InfiniteRotator.init();
	
});


//********VIMEO LIGHTBOXES FOR THE NEWS SECTION**************
//***********************************************************
var vimeoLightbox = {
	init: function() {
		var video = $("a[rel^='vimeo']");
		video.click(
			function (event) {
				event.preventDefault();
				$('.speaker-lightbox-close').click();
				topValue =  $(window).scrollTop();
				$(".vimeo-lightbox-wrapper").css("top", topValue + 200 );
				$(".lightbox").fadeIn(200);
				$(".vimeo-lightbox").fadeIn(200);
				vimeoLink = $(this).attr("href");
				$('<iframe src="' + vimeoLink + '" width="625" height="350" frameborder="0" class="iframe"></iframe>').appendTo('.vimeo-lightbox-content');
				$(".vimeo-lightbox-close").click(function(){
					$(".vimeo-lightbox,.lightbox").fadeOut("fast", function() {
						$(".iframe").remove();	
					});
				});
			}  
		);	
	}
};

//********VIDEO HOVER**************
//*********************************
var videoHover = {
	init: function() {
		var hoverTarget = $('.videos-clk');
		hoverTarget.hover(
			function () {
				$(this).children().children('.videos-info').show();
				}, 		
			function () {
				$(this).children().children('.videos-info').hide();          
			}
		);	
	}
};


//********DETAIL GRID HOVER********
//*********************************
var speakerHover = {
	init: function() {
		var hoverTarget = $('.speaker-clk');
		hoverTarget.hover(
			function () {
				$(this).children().children('.speaker-info').show();
				}, 		
			function () {
				$(this).children().children('.speaker-info').hide();          
			}
			  
		);	
	}
};


//********NEWS SLIDER********
//***************************
var newsSlider = {
	init: function() {
		var currentPosition = 0;
		var slideWidth = 875;
		var slides = $('.slide');
		var numberOfSlides = slides.length;
	
		// Wrap all .slides with #slideInner div
		slides.wrapAll('<div id="slideInner"></div>').css({
		 'float' : 'left',
		 'width' : slideWidth
		});
	
		// Set #slideInner width equal to total width of all slides
		$('#slideInner').css('width', slideWidth * numberOfSlides);
	
		// Insert controls in the DOM
		$('#nav-dock-wrapper')
		.append('<span class="control notSelected" id="leftControl"></span>')
		.append('<span class="control notSelected" id="rightControl"></span>');
		
		//Place news navigation in correct position
		var newsTop = $('#slideshow').offset().top;
		
		//nav dock is the navigation for the news
		$("#nav-dock").css("margin-top",newsTop+"px");
		
		// fade control states for arrows
		$('#rightControl').fadeTo("fast", 0.2);
		$('#leftControl').fadeTo("fast", 0.2);
		
		// setup hover states for arrows
		$('#rightControl').hover(
			function () {
				if ( currentPosition >= 0 && currentPosition < (numberOfSlides-1) ) {
					$(this).addClass('selectedArrow');
					$(this).stop().fadeTo("fast", 1.0);
				}
				else {
					$(this).removeClass('selectedArrow');
				}
			},
			function () {
				$(this).stop().fadeTo("fast", 0.2);	
			}
		);
			
		$('#leftControl').hover(
			function () {
				if ( currentPosition > 0 && currentPosition <= (numberOfSlides-1) ) {
					$(this).addClass('selectedArrow');
					$(this).stop().fadeTo("fast", 1.0);
				}
				else {
					$(this).removeClass('selectedArrow');
				}
			}, 
			function () {
				$(this).stop().fadeTo("fast", 0.2);
			}
		);
	
		$('#rightControl').click(function() {	  
			if ( currentPosition >= 0 && currentPosition < (numberOfSlides-1) ) {
				currentPosition = currentPosition +1
				$('#slideInner').animate({'marginLeft' : slideWidth*(-currentPosition)});
			}
		});
			
		$('#leftControl').click(function() {
			if ( currentPosition > 0 && currentPosition <= (numberOfSlides-1) ) {
				currentPosition = currentPosition -1
				$('#slideInner').animate({'marginLeft' : slideWidth*(-currentPosition)});
			}
		});
	}
};

