/*
 * MYVAN JS Functions
 * by TR
 */

/* Define our scope */
var myvan = myvan || {};
myvan.site = myvan.site || {};

 
//INIT
$(document).ready(function() {	
	myvan.site.main.init();
});

//This is the scope of the functions described here
myvan.site.main = {
	init: function() {
		myvan.site.main.sethandlers();
	},
	sethandlers: function() {	
		/* slide up/down excerpt text on mouse over of each box */
		$('.box.thumb')
			.mouseenter(myvan.site.main.manualDetailsSlide)
			.mouseleave(myvan.site.main.manualDetailsSlide);
		/* simulate :hover on headline, when the box is entered, but not on the excerpt...*/
		$('.box.thumb img, .headline, .action-title, .hoverme').mouseenter(function(){
			if(!$(this).parent().find('.headline').hasClass('ishovered'))
				$(this).parent().find('.headline').addClass('ishovered');
		}).mouseleave(function(){
			if($(this).parent().find('.headline').hasClass('ishovered'))
				$(this).parent().find('.headline').removeClass('ishovered');
		});
		$('.fulltext').mouseenter(function(){
			$('.headline').removeClass('ishovered');
		})
		/* in the active box load the video on click of each element */
		$('.video.active .wp-post-image, .video.active .clickme, .video.active .headline, .video.active .headline a, .action-title')
			.click(myvan.site.main.clickloadVideo);
		  
		/* switch the active element on click */
		$('.headline, .headline a, .wp-post-image')
			.click(myvan.site.main.switch_active_element);
		
		$('.fulltext').click(myvan.site.main.readmore);
		
		/* just in case ...*/
		$('.social_next','.social_prev','.choose_program a','.clock a').click(function(e){e.preventDefault();});

	},
	manualDetailsSlide: function(e) {
		/* read rel-attribute of trigger */
		var this_rel = $(this).find('.slide_trigger.video a').attr('rel');
		/* select corresponding excerpt element */
		var detailslide = $('.fulltext[rel='+this_rel+']');
		
		/* on mouse enter slide excerpt element up */
		if(e.type == "mouseenter")
			myvan.site.main.detailSlideUp(detailslide);
		/* on mouse leave slide excerpt element down */
		if(e.type == "mouseleave")
			myvan.site.main.detailSlideDown(detailslide);
	},
	detailSlideDown: function(obj) {
		/* function to slide down excerpt element */
		obj
			.stop()
			.animate(
				{
					'height':'0px' /* animation */
				},
				myvan.site.config.overlays.togglespeed, /* speed */
				function(){ /* callback function when animation has finished */
					var parent_box = obj
										.closest('.box');
					parent_box
						.find('.slide_trigger')
						.removeClass('down');	/* change the bg image of the trigger element */
					obj
						.css('display', 'none'); /* hide excerpt element */
				}
			);
	},
	detailSlideUp: function(obj) {
		/* function to slide up excerpt element */
		obj
			.css('display', 'block') /* show excerpt element */
			.stop()
			.animate({
					'height':'64px'	/* animation */
				},
				myvan.site.config.overlays.togglespeed, /* speed */
				function(){
					var parent_box = obj
										.closest('.box');
					parent_box
						.find('.slide_trigger')
						.addClass('down');	/* change bg image of trigger element */
				}
			);	
	},
	readmore: function() {
		/* Text articles have a hidden readmore link, so just go ahead and call any a.readmore we find. */
		if($(this).hasClass('fulltext')) {
			location.href = $(this).find('a.readmore').attr('href');
		}
	},
	switch_active_element: function(e) {
		/* 
		 * on click of video trigger, resize the active box to normal size and resize the triggered box to active-size 
		 * then load the loop containing the player
		 */
		e.preventDefault();
			
		$('.myvan-likebox')
			.css('opacity', '0.0');
		
		$('.currentlyblack').removeClass('currentlyblack');
		
		if(!$(this).closest('.box').hasClass('video')) {
			location.href = $(this).closest('.box').find('.deeplink a').attr('href');
			return false;
		}else {
		
		if($(this).closest('.box').hasClass('active')) return false;
		
			var active_box = $('.box.article.active'); /* currently active box */
			var inactive_box = $(this).closest('div.box.article'); /* box where we triggered the switch element event */

			if(!inactive_box.hasClass('active')) {
				/* remove player from active div */
				
				if($('.curpostoelaborate').length) {
					// if you find this div, the player is playing
					
					if($('.curpostoelaborate').html() != 'finished') {
						ET_Event.videoStop($('.curpostoelaborate').attr('rel'));
						
						var current_time = Math.round(jwplayer('jwplayer-'+active_box.attr('rel')).getPosition());
						
						ET_Event.eventStart('Video-incomplete', $('.curpostoelaborate').attr('rel'), current_time+'s', '');
						
						
						/* the video hasn't finished, yet */
						
					}
				
				}
				//console.log($('.curpos').html());
				$('#jwplayer-'+active_box.attr('rel')+'_wrapper').remove(); 
				
			
				
				var active_box_c_rel = active_box.find('.clickme').attr('rel');
				var active_box_d_rel = '';
				
				if(active_box_c_rel == undefined) {
					active_box_c_rel = '';
					active_box_d_rel = active_box.find('.deeplink').attr('rel');				
				}
				var sep = (document.location.search.indexOf('?')>=0)?'&':'?';
			
				active_box.load(active_box_c_rel + active_box_d_rel + sep + myvan.site.config.videos.trigger.box, 
					function(){
						active_box.find('.clickme').parent().unbind('click').click(myvan.site.main.switch_active_element);
						active_box.find('.wp-post-image').unbind('click').click(myvan.site.main.switch_active_element);
						active_box.find('img, .headline, .action-title, .hoverme')
						.mouseenter(function(){
							if(!$(this).parent().find('.headline').hasClass('ishovered'))
								$(this).parent().find('.headline').addClass('ishovered');
							
						}).mouseleave(function(){
							if($(this).parent().find('.headline').hasClass('ishovered'))
								$(this).parent().find('.headline').removeClass('ishovered');
						});
					}
				);
				
				/* remove loadVideo Event and add switch element event */
				active_box.find('.clickme').unbind('click').click(myvan.site.main.switch_active_element);
				active_box.find('.wp-post-image').unbind('click').click(myvan.site.main.switch_active_element);
				
				/* remove class active of old element */
				active_box.removeClass('active').addClass('currentlyblack'); 
				
				/* make the parent element of the clicked div active */
				
				/* remove swicth element event and add load video Event */
				var inactive_box_c_rel = inactive_box.find('.clickme').attr('rel');
				
				if(inactive_box_c_rel == undefined) {
					inactive_box_c_rel = '';
					inactive_box_d_rel = inactive_box.find('.deeplink').attr('rel');				
				}
				
				
				
				inactive_box.addClass('active');
				inactive_box.find('img').css('display', 'none');
				/*inactive_box.load(inactive_box_c_rel + sep + myvan.site.config.videos.trigger.box + '&thumb=large', 
						function(){
					$('.video.active .wp-post-image, .video.active .clickme, .video.active .headline, .video.active .headline a')
						.click(myvan.site.main.clickloadVideo);
					$('.video.active .fulltext').click(myvan.site.main.readmore);
				}
				);
				*/
				
				myvan.site.main.autoloadVideo(inactive_box);
				//active_box.removeClass('currentlyblack');
				/* call for isotope */
				myvan.site.general.relayout();	
			}
		}
	},
	autoloadVideo: function(obj) {	
		var article_url = $(obj).find('.clickme').attr('rel');
		/* load the article with a parameter, so only the video is rendered (see single.php) */
		var sep = (document.location.search.indexOf('?')>=0)?'&':'?';
		$(obj).load(article_url + sep + myvan.site.config.videos.trigger.video);
	},
	clickloadVideo: function() {
		var parent_box = $(this).closest('.box.article.active');
		var article_url = parent_box.find('.clickme').attr('rel');
		/* load the article with a parameter, so only the video is rendered (see single.php) */
		var sep = (document.location.search.indexOf('?')>=0)?'&':'?';
		parent_box.load(article_url + sep + myvan.site.config.videos.trigger.video);		
	}
};

