// Jquery ready scripts
(function($){
	onDocumentReady = function() {
		$('#menubar-content li').addClass('menubar-link')
			.children('a').addClass('menubar-href'); 
		$('#maincontent-content').children('div:last').addClass('last');
		$(".container").each(function(index, element) {
			ypos = findPosY(element);
			$(this).data("yoffset",ypos);
		});	
		
		$("#who-slider").easySlider({
			prevId: 		'who-slider-left',
			prevText: 		'&nbsp;',
			nextId: 		'who-slider-right',	
			nextText: 		'&nbsp;',
			auto: false,
			continuous: true,
		});
		
		$("#who-slider-left").children("a").addClass("arrow left-arrow");
		$("#who-slider-right").children("a").addClass("arrow right-arrow");
		
		$("#how-slider").easySlider({
			prevId: 		'how-slider-left',
			prevText: 		'&nbsp;',
			nextId: 		'how-slider-right',	
			nextText: 		'&nbsp;',
			auto: false,
			continuous: true,
		});
		
		$("#how-slider-left").children("a").addClass("arrow left-arrow");
		$("#how-slider-right").children("a").addClass("arrow right-arrow");
		
		$("#why-slider").easySlider({
			prevId: 		'why-slider-left',
			prevText: 		'&nbsp;',
			nextId: 		'why-slider-right',	
			nextText: 		'&nbsp;',
			auto: false,
			continuous: true,
		});
		
		$("#why-slider-left").children("a").addClass("arrow left-arrow");
		$("#why-slider-right").children("a").addClass("arrow right-arrow");
		
			
		$.ajax({
			type: "GET",
			url: "videos.xml",
			dataType:"xml",
			success: function(xml){
				$(xml).find('video').each(function(){
					var id = $(this).attr('id')
					var thumb = $(this).find('thumb').text()
					var divid = $(this).find('divid').text()
					var vid = $(this).find('id').text()
					var profile = $(this).find('profile').text()
					var expint = $(this).find('expint').text()
					var v = $(this).find('v').text()
					$('<li></li>')
						.html('<a id="video_'+id+'" href="#'+id+'" onClick="selectVideo('+id+');"><img src="thumbs/'+thumb+'"></a>')
						.appendTo("#video-roundabout ul");	
					$('a#video_'+id).data({'divid':divid,'thumb':thumb,'vid':vid,'v':v,'expint':expint,'videoid':id,'profile':profile});				
				});
			},
			complete: function(){
				$('#video-roundabout ul').roundabout({
					duration:1000, tilt: 0
				});
				$("#why").swipe({swipeStatus:swipeWhy, threshhold:10, allowPageScroll:"vertical",triggerOnTouchEnd:true});
				$("#how").swipe({swipeStatus:swipeHow, threshhold:10, allowPageScroll:"vertical",triggerOnTouchEnd:true});
				$("#who").swipe({swipeStatus:swipeWho, threshhold:10, allowPageScroll:"vertical",triggerOnTouchEnd:true});
				scrollToId('what');
				onMobileClick();
				$("#video-roundabout").swipe({swipeStatus:swipeStatus, threshhold:10, allowPageScroll:"vertical", triggerOnTouchEnd : true});    
				$("#video_1").click();					
			}
		});		
	};
	
	onDocumentScroll = function() {
		yoffset = (ie>0)?document.documentElement.scrollTop:window.pageYOffset;
		$(".container").each(function(key,value){
			ypos = $(this).data("yoffset");
			offset = yoffset - ypos;
			if(offset <= 400 && offset>=-100) {
				id = $(this).attr('id');
				setActiveLink(id);
			}
		});
	};
	
	onMobileScroll = function() {
		$('.fixed-content').fadeOut(0,'', function() {
			yoffset = (ie>0)?document.documentElement.scrollTop:window.pageYOffset;
			$("#menubar").css("top",yoffset);
			$('#mult-marketing').css("top",yoffset+61);
			wheight = $(window).height() + yoffset + 5;
			$("#footer").css("top",wheight);
		});
		$('.fixed-content').fadeIn('slow');
	};
	
})(jQuery);




// Set Active Links

function setActiveLink(id){
	$('.menubar-href').removeClass('active');
	$('#menubar-'+id).addClass('active');
}

function selectVideo(id){
	var divid = $('a#video_'+id).data('divid');
	var v = $('a#video_'+id).data('v');
	var profile = $('a#video_'+id).data('profile');
	var expint = $('a#video_'+id).data('expint');
	var vid = $('a#video_'+id).data('vid');	
	
	$("div#video-video").children("div").replaceWith('<div id="'+divid+'-wrap"  class="evp-video-wrap"></div>');
	$.getScript('http://ahd.evplayer.com/framework.php?div_id='+divid+'&id='+vid+'&v='+v+'&profile='+profile, function() {
		_evpInit(expint);
	});
}

// Swipe Functions
function swipeStatus(event, phase, direction, distance) {
	if( phase=="move" && (direction=="left" || direction=="right") ) {		
		if (direction == "right")
			$('#video-roundabout ul').roundabout_animateToNextChild()
				.delay('1000').children('li.roundabout-in-focus a').click();
		else if (direction == "left")
			$('#video-roundabout ul').roundabout_animateToPreviousChild()
				.delay('1000').children('li.roundabout-in-focus a').click();
	}	
}

function swipeWhy(event, phase, direction, distance) {
	if( phase=="move" && (direction=="left" || direction=="right") ) {		
		if (direction == "right")
			$('#why-slider-left a').click();
		else if (direction == "left")
			$('#why-slider-right a').click();
	}	
}

function swipeHow(event, phase, direction, distance) {
	if( phase=="move" && (direction=="left" || direction=="right") ) {		
		if (direction == "right")
			$('#how-slider-left a').click();
		else if (direction == "left")
			$('#how-slider-right a').click();
	}	
}

function swipeWho(event, phase, direction, distance) {
	if( phase=="move" && (direction=="left" || direction=="right") ) {		
		if (direction == "right")
			$('#who-slider-left a').click();
		else if (direction == "left")
			$('#who-slider-right a').click();
	}	
}

function moreClick(id) {
	$('#'+id+'-slider-right a').click();
}

// Lightbox effect

function lightboxin(id){
	$('#fade').fadeTo('fast',0.8,function(){
		$('#'+id).fadeIn('fast');
		$('body').css('overflow','hidden');
	});
}

function lightboxout() {
	$('#fade').fadeOut();
	$('.white_content').fadeOut();
	$('lightboximg').css('z-index','0');
	$('body').css('overflow','auto');
}


// DETECT IE VERSION
var ie=0;
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
ie=new Number(RegExp.$1)} // capture x.x portion and store as a number

// Scroller Functions

function scrollToId(id) {
		
		scrolling=true;
		
		start = (ie>0)?document.documentElement.scrollTop:window.pageYOffset;
		obj = document.getElementById(id);
		finish = findPosY(obj);
		correct = mobileCorrect();
		finish = correct + finish;
		if (start > finish) {
        scrollUp(start,finish); 
        } else {
        scrollDown(start,finish) 
        }
	setActiveLink(id);
	onMobileClick();
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
	{
		curtop += obj.y;
	}
	curtop -= 150;
	return curtop;
}

function scrollDown(start,finish) {
        if (start < finish - .5) {				
                window.scroll(0,start);  
				start += (finish-start)/7;  
				start2=start; finish2=finish;
                setTimeout('scrollDown(start2,finish2)', 1);	
        }else{
        window.scroll(0,finish)
        scrolling=false;;
        }
}

function scrollUp(start,finish) {
        if (start > finish + .5) {				
                window.scroll(0,start);  
				start += (finish-start)/7;  
				start2=start; finish2=finish;
                setTimeout('scrollUp(start2,finish2)', 1);	
        }else{
        window.scroll(0,finish);
        scrolling=false;
        }
}
