var current_overlay = '';
var hidden_field = '';
var random = 1;


jQuery(window).ready(function(){

	jQuery("#list-wide li .list-items ul li").hover(
		function (event) {
            
			if (jQuery(this).children('.thumb-info-next').length) {

				var next = jQuery(this).children('.thumb-info-next').attr('class');
				
				if (next.indexOf('thumb-info-hide') == -1) {
					
					jQuery(this).children('.thumb-info-next').addClass('thumb-info-hide');
                    jQuery(this).children('.thumb-info-next').addClass('thumb-next');
				}
				
				if (jQuery(this).children('.thumb-info-current').attr('class').indexOf('thumb-info-hide') == -1) {
						
                    return true;
                }
			}
			
			jQuery(this).children('a.thumb-info').stop(true, true).show();
			
			return false;
		},
		function(event) {


			if (jQuery(this).children('.thumb-info-next').length) {

				var next = jQuery(this).children('.thumb-info-next').attr('class');
							
				if (next.indexOf('thumb-next') != -1) {
				
					jQuery(this).children('.thumb-info-next').removeClass('thumb-info-hide');
                    jQuery(this).children('.thumb-info-next').removeClass('thumb-next');
				}
			}

			jQuery(this).children('a.thumb-info').stop(true, true).fadeOut('normal', 'linear');
		}
	);
    
    jQuery("#list-wide li .list-items ul li a").click(function(event) {
    
        var play_all = jQuery('.list-items').attr('data-play-all');

        if (play_all != '0' && undefined != play_all) {

            var video_id = jQuery(this).parent().attr('data-vid-id');
            jQuery('.list-items').attr('data-vid-current', video_id);

            jQuery('.thumb-info-current').addClass('thumb-info-hide');
            jQuery('.thumb-info-next').addClass('thumb-info-hide');
            jQuery('.thumb-info-next').removeClass('thumb-next');
            jQuery(this).stop(true, true).hide();
            setup_current('.list-items');
            return false;
        }
    });

	jQuery("#bio-videos .list-items ul li").hover(
		function (event) {
			jQuery(this).children('a.thumb-info').stop(true, true).show();
		},
		function(event) {
			jQuery(this).children('a.thumb-info').stop(true, true).fadeOut('normal', 'linear');
		}
	);

	jQuery("#featured-videos .list-items ul li").hover(

		function (event) {
			jQuery(this).children('a.thumb-info').stop(true, true).show();
		},
		function(event) {
			jQuery(this).children('a.thumb-info').stop(true, true).fadeOut('normal', 'linear');
		}

		//function (event) {
		//
		//	jQuery(this).children('a.thumb-info').fadeToggle('normal', 'linear');
		//	jQuery(this).parent().siblings('.monicker').css('z-index', 10000);
		//}
	);
	
	jQuery('#actual-blog .monicker').css('cursor', 'pointer');
	jQuery('#actual-blog .monicker').click(function(event) {
		window.location.href = '/';	
	});

	jQuery("#list-wide li .list-head").css({opacity: 0.7});
	jQuery(".thumb-info").css({opacity: 0.8});
	jQuery(".thumb-info-next").css({opacity: 0.7});
	jQuery(".thumb-info-current").css({opacity: 0.7});

	jQuery("#list-wide li .list-directors .director-details").css({opacity: 0.7});


	jQuery("#share a").hover(
		function(event) {
			
			var position = 0;
			
			switch(jQuery(this).attr('class')) {
				
				case 'mail':
					position = -119;
					break;
					
				case 'facebook':
					position = -119*2;
					break;
				
				case 'twitter':
					position = -119*3;
					break;
			}
			
			jQuery("#share").css({'background-position': '0 ' + position + 'px'});
		},
		function(event) {
			
			jQuery(this).parent().css('background-position', '0 0');
		}



	);

	jQuery(".share a.mail").fancybox({
		'scrolling'		: 'no',
		'titleShow'		: false,
		'overlayColor' : '#000000',
		'overlayOpacity': 0.8,
		'onClosed'		: function() {
		}
	});	

	jQuery('#send-form p img').click(function() {
		
		jQuery('#send-form').submit();
	});

	jQuery("#send-form").bind("submit", function() {
	
		jQuery.post('/ajax/send', jQuery(this).serialize(), function(data, textStatus) {
			if (data.code == 0) {
				
				alert(data.msg);
			}
			else {
				alert('Email sent.');
				jQuery.fancybox.close();
			}
		}, 'json');

		return false;
	});

	jQuery('#signup-button').click(function() {
		jQuery('#signup-form').submit();
		return false;
	});
	
	jQuery("#signup-form").bind("submit", function() {
	
		jQuery.post('/ajax/signup', jQuery(this).serialize(), function(data, textStatus) {
			if (data.code == 0) {

				alert(data.msg);
			}
			else {

				jQuery('#signup-form').parent().html('<div style="display: block; width: 100%; min-height: 10px; margin-top: 15px"><span style="font-family: GothamMedium; margin-top: 20px;">THANK YOU!</span><p style="clear:both; font-family: GothamBook; text-transform: none; width: 90%; margin-top: 15px">A confirmation email will be arriving soon.</p></div>');
			}
		}, 'json');

		return false;
	});

	
	jQuery(".poster").fancybox({
		'scrolling'		: 'no',
		'titleShow'		: false,
		'overlayColor' : '#000000',
		'overlayOpacity': 0.8,
		'onClosed'		: function() {
		}
	});	

	jQuery('.default-value').each(function() {
		 var default_value = this.value;
		 jQuery(this).focus(function() {
			  if(this.value == default_value) {
					this.value = '';
			  }
		 });
		 jQuery(this).blur(function() {
			  if(this.value == '') {
					this.value = default_value;
			  }
		 });
	});
	
	jQuery('#contact-form .submit').click(
	
		function(event) {

			jQuery.post('/ajax/email', jQuery('#contact-form').serialize(), function(data, textStatus) {
				if (data.code == 0) {
					
					alert(data.msg);
				}
				else {
					jQuery('#contact-form')[0].reset();
					alert('Thank you for your comments.');
				}
			}, 'json');
		}
	);

//	jQuery('.post-image img').css({width: '460'});
	jQuery('.post-image img').removeAttr('height');
	jQuery('.post-image a').hide();	
	jQuery('.post-image span').hide();

	jQuery('.list-head .list-head-actions a').click(
		
		function(event) {

			if (jQuery(this).attr('data-ident') == 'real-link') { return true;}
			var current_offset = jQuery(this).parent().attr('data-offset');
			var current_page = parseInt(jQuery(this).parent().children(".activ").attr('data-ident'));
			var current_video = jQuery(this).parent().attr('data-video-id');
			var page = jQuery(this).attr('data-ident');
			var color_hex = jQuery(this).parent().parent().css('background-color');
			var record_name = jQuery(this).parent().attr('data-record-name');
			var record_type = jQuery(this).parent().attr('data-record-type');

			if (page === 'next') {
				
				previous_node = jQuery(this).prev().attr('data-ident');
				page = (current_page + 1) >= previous_node ? previous_node : current_page + 1;
			}
			else if (page == 'prev') {
				
				page = current_page === 1 ? "1" : current_page - 1;
			}
			
			
			if (page == current_page) { return true; }

			jQuery(this).siblings('.activ').removeClass('activ');
			jQuery(this).parent().children('a').eq(page).addClass('activ');
		
			var list_items_node = jQuery(this).parent().parent().siblings('.list-items');
		
			var orient = page < current_page ? "" : "-";
			var width = (jQuery(this).parent().parent().css('width'));
			
			list_items_node.animate({marginLeft: orient + width}, function() {
				
				list_items_node.html('<p>Loading page ' + page +  ' ...</p>');
				list_items_node.animate({width: 'show'}, 'linear');
				list_items_node.css({'margin-left': '0px'});
				
			});
			
			++random;
			jQuery.post('/ajax/pager', {page: page, type: record_type, id: record_name, offset: current_offset, color: color_hex, video: current_video},
					 function(data, textStatus) {
						list_items_node.animate({opacity: 0}, 'linear', function() {
						
							list_items_node.html('<ul id="rnd"'+ random +'>' + data + '</ul>')
							
							width = 2 * parseInt(width);
							width = orient == '-' ? width : ('-'+width);
							width += 'px';
							
							list_items_node.css({'margin-left': width, 'opacity': 1.0});
							
							list_items_node.animate({marginLeft: '0px'}, 'fast', function() {

								jQuery(this).children('ul').children('li').hover(
									function (event) {
										jQuery(this).children('a.thumb-info').show();
									},
									function(event) {
										jQuery(this).children('a.thumb-info').fadeOut('normal', 'linear');
									}
								);

								jQuery(this).children('ul').children('ul').children('li').hover(
									function (event) {
										jQuery(this).children('a.thumb-info').show();
									},
									function(event) {
										jQuery(this).children('a.thumb-info').fadeOut('normal', 'linear');
									}
								);
							});
						}
						);
					 }
			);

			return false;
		}
		
	);
	
	jQuery('.widget_collapsarch .widget-title-container h3').html('NEWS ARCHIVE');


	jQuery('.addthis_toolbox').each(function() {
		
		jQuery(this).prependTo(jQuery(this).parent().next('.meta-bottom').children('li'));
		jQuery(this).css('width', 'auto');
		jQuery(this).css('float', 'left');
		jQuery(this).children('a').html('Share post');
    });
    
/*
function adjustPos(evt) {
            
    console.log(evt);
    
    jQuery('#at15s').css("position","absolute");
    jQuery('#at15s').css("top", ((jQuery(window).height() - jQuery('#at15s').outerHeight()) / 2) + jQuery(window).scrollTop() + "px");
    jQuery('#at15s').css("left", ((jQuery(window).width() - jQuery('#at15s').outerWidth()) / 2) + jQuery(window).scrollLeft() + "px");
 
    return false;
}
    
    if (typeof addthis != 'undefined') {
         addthis.addEventListener("addthis.menu.open", adjustPos);
    }
*/
	init_play_all();
	reset_player();
});


function init_play_all() {

	var info_holder = jQuery('.list-items');
	var play_all = jQuery(info_holder).attr('data-play-all');

	if (play_all != '0') {
		
		setup_next(info_holder);
	}	
}

function setup_current(info_holder) {
	    
	var up_next = jQuery(info_holder).attr('data-vid-current');
    var orig_vid = jQuery(info_holder).attr('data-orig-video');
    var id = 0;
        
	if (up_next != "") {
		
        load_vid(up_next);

        if (up_next == orig_vid) {

            jQuery(info_holder).attr('data-vid-current', '');
            id = jQuery(info_holder).attr('data-vid-cms');
            
            jQuery('.thumb-info-current').addClass('thumb-info-hide');
            jQuery('.thumb-info-next').addClass('thumb-info-hide');
        }
        else {

            var holder_class = "." + jQuery(info_holder).attr('class');

            jQuery('.thumb-info-current').addClass('thumb-info-hide');
            jQuery('.thumb-info-next').addClass('thumb-info-hide');

            var next_obj = jQuery(holder_class + ' li[data-vid-id="' + up_next + '"]');
	
            jQuery(next_obj).children('.thumb-info-current').removeClass('thumb-info-hide');	
            jQuery(next_obj).children('.thumb-info-next').addClass('thumb-info-hide');
        
            id = jQuery(next_obj).attr('data-id');

        }

        jQuery.post('/ajax/info/' + id, function(data) {
			
            jQuery('#video-info').html(data);
        }); 

        setup_next(info_holder);	        
	}
}

function setup_next(info_holder) {
	
	var current = jQuery(info_holder).attr('data-vid-current');
	var holder_class = "." + jQuery(info_holder).attr('class');
        
	if (current == "") {
		
		var next = jQuery(holder_class + " ul:first-child li:first-child");
		var vid_id = jQuery(next).attr('data-vid-id');
                
		jQuery(info_holder).attr('data-vid-current', vid_id);		
        jQuery(next).children('.thumb-info-next').removeClass('thumb-info-hide');
		jQuery(next).children('.thumb-info-next').attr('class', 'thumb-info-next');
	}
	else {
		
		var next = jQuery(holder_class + ' li[data-vid-id="' + current + '"]').next();
            
		if (next.length == 0) {

			var next_parent = jQuery(holder_class + ' li[data-vid-id="' + current + '"]').parent().next();
                        
			if (next_parent.length && jQuery(next_parent).children('li').length) {

				next = jQuery(next_parent).children('li:first-child');
			}
            else {
                                
                orig_video = jQuery(holder_class).attr('data-orig-video');
                jQuery(info_holder).attr('data-vid-current', orig_video);
            }
		}

		if (next.length) {
			
			var vid_id = jQuery(next).attr('data-vid-id');
			jQuery(info_holder).attr('data-vid-current', vid_id);
			jQuery(next).children('.thumb-info-next').removeClass('thumb-info-hide');
		}
		else {
			
		}
	}
}


function ready(player_id) {

	// Keep a reference to Froogaloop for this player
				
	var container = document.getElementById(player_id).parentNode.parentNode,
		 froogaloop = $f(player_id);

	function setupEventListeners() {
		
		// function onPlayProgress() {
		//
		//	 froogaloop.addEvent('playProgress', function(data) {
		//               
		//	 });
		// }

		 function onFinish() {
			 
			 froogaloop.addEvent('finish', function(data) {

               froogaloop.removeEvent('finish');
               setup_current(jQuery('.list-items'));
			 });
		 }

		 onFinish();
	}

	setupEventListeners();
}


function reset_player() {

  var vimeoPlayers = document.querySelectorAll('iframe'),
		player;
		
  for (var i = 0, length = vimeoPlayers.length; i < length; i++) {
		player = vimeoPlayers[i];
		$f(player).addEvent('ready', ready);
  }	
}

function load_vid(id) {

	var obj = jQuery('#player_1');
	var src = "http://player.vimeo.com/video/" + id +"?title=0&byline=0&portrait=0&autoplay=1&api=1&player_id=player_1";
    
    obj.attr('src', src);
	reset_player();
}

jQuery(window).load(function() {

	jQuery('.post-image span').each(function() {

		jQuery(this).html('image <strong>1</strong>/' + jQuery(this).siblings('.post-image-slide').children('img').length);
	});
		
	jQuery('.post-image-slide').each(function() {
	
		var p = this.parentNode;
		
		jQuery(this).cycle({
			fx:     'fade',
			speed:  'fast',
			timeout: 0,
			next:   jQuery('a.next', p),
			prev:   jQuery('a.prev', p),
			containerResize: 1,
			after: function(curr, next, opts) {

				var caption = 'image ' + (opts.currSlide + 1) + '/' + opts.slideCount;
				jQuery(this).parent().siblings('span').html(caption);
				jQuery(this).parent().css("height", jQuery(this).css('height'));
			}
		});
	});

	jQuery('.post-image p').hide();
	jQuery('.post-image a').show();
	jQuery('.post-image span').show();
});


      
