
jQuery.extend(jQuery.expr[':'], {
  containsIgnoreCase: "(a.textContent||a.innerText||jQuery(a).text()||'').toLowerCase().indexOf((m[3]||'').toLowerCase())>=0"
});

$(document).ready(function(){
	
	//nested lists in videos_all kills sorting
	$('#block-views-videos_all li li').each( function() {
			$(this).replaceWith("<span>" + $(this).text() + "</span>");
	});
	$('#block-views-videos_all ul ul').each( function() {
			$(this).replaceWith($(this).text());
	});
	
	
	// Feature videos setup for popup videos
	featureVideosSetup();
	// Feature style setup for Views
	featureViewsStyleSetup();
		
	
	
	//change Category or Live Search onload
	if($.cookie('category')) {
		
		if($.cookie('category') == 'search' && $.cookie('search') ) {
			$('#livesearch').val($.cookie('search'));
			liveSearch();
		} else {
		
	    categoryId = '#' + $.cookie('category');
		$('.categorylink').removeClass('active');
		
		if($(categoryId).text().length > 15) {
			$('#activecategory h2').text(($(categoryId).text()).substr(0,13) + '...');
		} else {
			$('#activecategory h2').text($(categoryId).text());
		}
		
		$('#block-views-videos_all li').hide();
		$("#block-views-videos_all li:containsIgnoreCase('" + $(categoryId).text() + "')").show();
		
		$(categoryId).addClass('active');
	
		}
	
		//change Sort onload
		if($.cookie('sort')) {
			sortId = $.cookie('sort');
			sortVideos(sortId);
			$('#activesort span').text($('#' + sortId).text());
			$('#sort-options li a').removeClass('active');
			$('#'+sortId).addClass('active');
		} else {
			$('#sortlatest').addClass('active');
		}
		
	} else {
		$('#newvideos').addClass('active');
		showNewVideos(10);
	}
	
	
	
	if($.cookie('hidecategories') == 1) {
		$('#categories').hide();
		videosListHeight();
	} else {
		$('#activecategory').addClass('active');
	}
	
	if($.cookie('hidesort') == 1) {
		$('#sort-options').hide();
		videosListHeight();
	} else {
		$('#activesort').addClass('active');		
	}

	
	
	
				   
	//Find active video					   
	$.scrollTo.defaults.axis = 'y';
	$('#block-views-videos_all ul').scrollTo($('#block-views-videos_all ul a.active'), 0);
	$('#block-views-videos_all ul').scrollTo('-=65px', 0);
						   
	
	//show/hide category and list options
	// REMOVED because cookie added $('#activecategory, #activesort').addClass('active');
	
	$('#activecategory, #categories a.close').click( function() {
		if($('#categories:hidden').length > 0) {
			//$('#categories').slideDown(1000);
			$('#categories').show();
			$('#activecategory').addClass('active');
			$.cookie('hidecategories', 0, { path: '/', expires: 1 });
		} else {
			//$('#categories').slideUp(1000);
			$('#categories').hide();
			$('#activecategory').removeClass('active');
			$.cookie('hidecategories', 1, { path: '/', expires: 1 });
		}
		videosListHeight()
		$(this).blur();
		return false;
	});
	$('#activesort, #sort-options a.close').click( function() {
		if($('#sort-options:hidden').length > 0) {
			//$('#sort-options').slideDown(1000);
			$('#sort-options').show();
			$('#activesort').addClass('active');
			$.cookie('hidesort', 0, { path: '/', expires: 1 });
		} else {
			//$('#sort-options').slideUp(1000);
			$('#sort-options').hide();
			$('#activesort').removeClass('active');
			$.cookie('hidesort', 1, { path: '/', expires: 1 });
		}
		videosListHeight()
		$(this).blur();
		return false;
	});
	
	$('#activecategory, #activesort').hover(
      function () {
        $(this).addClass('hover');
      }, 
      function () {
        $(this).removeClass('hover');
      }
    );
	
	
	
	//change category
	$('#categories a:not(#allvideos, #newvideos, .close)').click( function() {
		if($('#livesearch').val() != 'Live Search') {
		  $('#livesearch').val('Live Search');
		  $.cookie('search', null);
		}
		$('.categorylink').removeClass('active');
		
		if($(this).text().length > 15) {
			$('#activecategory h2').text(($(this).text()).substr(0,13) + '...');
		} else {
			$('#activecategory h2').text($(this).text());
		}
		
		$('#block-views-videos_all li').hide();
		
		$("#block-views-videos_all li:containsIgnoreCase('" + $(this).text() + "')").show();
		$("#block-views-videos_all li:containsIgnoreCase('" + $(this).text().replace(" ","") + "')").show();
		
		$.cookie('category', $(this).attr('id'), { path: '/', expires: 1 });
		
		$(this).addClass('active');							 
		$(this).blur();
		return false;
	});
	
	$('#allvideos').click( function() {
		if($('#livesearch').val() != 'Live Search') {
		  $('#livesearch').val('Live Search');
		  $.cookie('search', null);
		}
		$('.categorylink').removeClass('active');
		$('#activecategory h2').text('All ' + $('#block-views-videos_all li').length + ' videos');
		$('#block-views-videos_all li').show();	
		$(this).addClass('active');
		$.cookie('category', null);
		$(this).blur();
		return false;
	});
	
	$('#newvideos').click( function() {
		if($('#livesearch').val() != 'Live Search') {
		  $('#livesearch').val('Live Search');
		  $.cookie('search', null);
		}
		$('.categorylink').removeClass('active');
		$('#activecategory h2').text($(this).text());
		$('#activesort span').text('Latest');
		
		showNewVideos(10);
		
		$(this).addClass('active');
		$.cookie('category', null);
		$.cookie('sort', null);
			
		$(this).blur();
		return false;
	});
						   
	$('#livesearch').focus( function() {
		if($(this).val() == 'Live Search') {
		  $(this).val('');	
		}
	});
	$('#livesearch').keyup( function() {
		$('.categorylink').removeClass('active');
		$.cookie('category', null);
		$.cookie('sort', null);
		liveSearch();
	});
	$('#livesearch').blur( function() {
		$('.categorylink').removeClass('active');
		$.cookie('category', null);
		$.cookie('sort', null);
		liveSearch();
	});
						   
						   
	
	
	// Mouse over list items
	mouseOverListItems();




	
	// Attached Documents 
	$("#attachments td a[@href$='docx']").addClass("word-document"); // note @ has deprechiated
	$("#attachments td a[@href$='doc']").addClass("word-document");
	$("#attachments td a[@href$='pdf']").addClass("pdf-document");
	$("#attachments td a[@href$='jpg']").addClass("image-document");
	$("#attachments td a[@href$='gif']").addClass("image-document");
	$("#attachments td a[@href$='png']").addClass("image-document");
	
	
	
	// Swap Playing Video Tabs 
	$('#share-box, #link-box').hide();
	$('#information-tab').addClass('active');
	
	$('#videolinks a').click( function () {
		$('#information-box, #share-box, #link-box').hide();
		$('#videolinks a').removeClass('active');
		$(this).addClass('active');
 		activeTab = $(this).attr('id');
		activeBox = activeTab.substring(0, activeTab.length-3) + 'box';
		$('#'+activeBox).show();
    	$(this).blur();
    	return false;
    });

	
	// Swap Link Options 
	$('#link-textarea2, #link-textarea3').hide();
	$("#link-radio1").attr("checked", "checked"); 
	
	$('#link-box label').click( function() {
		$('#link-box textarea').hide();
		activeRadio = $('#link-box input:checked').attr('id');
		activeTextarea = 'link-textarea' + activeRadio.charAt(activeRadio.length-1);
		$('#'+activeTextarea).show();
	});
	
	// See example 
	$('#link-box #seeexample').click( function() {
		var examplePopup = '<div id="examplePopup">' + $('#link-textarea1').val() + '<a href="#" class="close">Close</a></div>';
		$('#link-box').prepend(examplePopup);	
		$('#examplePopup, #examplePopup a').click( function() {
			$('#examplePopup').remove();
			$(this).blur();
    		return false;
		});
		$(this).blur();
    	return false;
	});
	
	$('#link-box textarea').focus( function() {
		$(this).select();
	});
								
								
	



  // Sort
  $('#sort-options li a').click(function () {
		$('#activesort span').text($(this).text());
										  
		sortId = $(this).attr('id');							 

		sortVideos(sortId);
		
		$.cookie('sort', sortId, { path: '/', expires: 1 });
		
		$('#sort-options li a').removeClass('active');
		$(this).addClass('active');
		
		
        $(this).blur();
	    return false;
  });


});



function mouseOverListItems() {
	$('#block-views-videos_all li').hover(
      function () {
        $(this).addClass('hover');
      }, 
      function () {
        $(this).removeClass('hover');
      }
    );
	
	$('#block-views-videos_all li').click( function() {
		window.location.href= $(this).find('a:first').attr('href');
	});	
}



function videosListHeight() {
	
	$('#block-views-videos_all ul').css('height','10px'); // prevent jump
	
	categoriesHeight = $('#categories').height() + parseFloat($('#categories').css('padding-top')) + parseFloat($('#categories').css('padding-bottom')) + + parseFloat($('#categories').css('border-top-width')) + + parseFloat($('#categories').css('border-bottom-width'));
	
	sortHeight = $('#sort-options').height() + parseFloat($('#sort-options').css('padding-top')) + parseFloat($('#sort-options').css('padding-bottom')) + + parseFloat($('#sort-options').css('border-top-width')) + + parseFloat($('#sort-options').css('border-bottom-width'));
	
	maxListHeight = 414;	
	
	newListHeight = maxListHeight;
	
	if($('#activecategory.active').length > 0) {
		newListHeight = newListHeight - categoriesHeight;
	}
	if($('#activesort.active').length > 0) {
		newListHeight = newListHeight - sortHeight;
	}
	
	$('#block-views-videos_all ul').css('height',newListHeight + 'px');

}


function showNewVideos(numberOfVideos) {
	if(!numberOfVideos) {
		numberOfVideos == 15;	
	}
	$('#block-views-videos_all li').show();
	
	if($('#sortlatest.active').length == 0 ) { 
		$('#sort-options li a').removeClass('active');
		$('#sortlatest').addClass('active');
		sortVideos('sortlatest');
	}
	
	
	i=1;
	$('#block-views-videos_all li').each(function() {
		if(i > numberOfVideos) {
			$(this).hide();
		}
		i++;
	});
}






function sortVideos(sortId) {
	$('#block-views-videos_all ul').hide();
	
	
	
	 howToSort = sortId;
	 
	 
	 // If hidden part 1/2
	 $('#block-views-videos_all li:hidden').append('<span class="hidden"></span>');
	 
	 // if A-Z copy the title to the beginning
     if(howToSort == 'sortaz' || howToSort == 'sortza') {
	   $('#block-views-videos_all li').each(function() {
			videoTitle = $(this).find('.view-data-node-title').text();
			videoTitle = videoTitle.toLowerCase();
			if(videoTitle.indexOf('the ') == 0) {
			  videoTitle = videoTitle.substring(4, videoTitle.length);
			}
			$(this).prepend( '<a class="tmp">' + videoTitle + '</a>' );
		});
	 }
	 
	 // if Latest copy the date to the beginning
     if(howToSort == 'sortlatest' || howToSort == 'sortoldest') {
	   $('#block-views-videos_all li').each(function() {
			videoDate = $(this).find('.view-data-created').text();
			$(this).prepend( '<a class="tmp">' + videoDate + '</a>' );
		});
	 }
	 
	 
	 var sortArray = [];
     $("#block-views-videos_all li").each(function() {
		 sortArray.push($(this).html()) 
	  });
	 
     sortArray.sort();
	 
	 if(howToSort == 'sortza' || howToSort == 'sortlatest') {
		 sortArray.reverse();
	 }
	 
	 
	 var output = "";
	 for(i=0; i < sortArray.length; i++) {
	   output += '<li>' + sortArray[i] + '</li>' + "\n\n";
	 }
	 $('#block-views-videos_all ul').html('<div>' + output + '</div>');
	 
	 
	 // remove A-Z sort info  before fadingIn
	 $('#block-views-videos_all a.tmp').remove();
	 
	 // If hidden part 2/2
	 $('#block-views-videos_all li:has(span.hidden)').hide();
	 $('#block-views-videos_all .hidden').remove();
	 
     $('#block-views-videos_all ul').fadeIn('slow');
	 
	 mouseOverListItems();
	 
}



function liveSearch() {
	$('#block-views-videos_all li').removeClass('active');

	$.cookie('category', 'search', { path: '/', expires: 1 });
	$.cookie('search', $('#livesearch').val(), { path: '/', expires: 1 });

	$('#activecategory h2').text('Search Results');
	$('#block-views-videos_all li').show(); // reset
	$('#channels a').removeClass('active');
	
	searchTextArray = $('#livesearch').val().split(" "); // if more than one word search for each word
	for(i=0; i< searchTextArray.length ;i++) {
		$("#block-views-videos_all li:not(:containsIgnoreCase('" + searchTextArray[i] + "'))").hide(); // hide non-matches
	}
	
}




// FEATURE javascript includes VALE and POPUP

function featureVideosSetup() {
	
	/* Close popup and vale */
	$('#vale, #close-popup').click( function() {
      $('#vale, #popup').hide();
	  $('#popup-inner-inner').html('');
	  $('#popup').removeClass('image');
	  $(this).blur();
	  return false;
    });
	
    $('.feature #content-area .video').each( function() {
		videoNodeUrl = $(this).find('a').attr('href');
		$(this).html($('#block-views-videos_all a[href='+videoNodeUrl+']:first').parent().parent().html()); // load details from list
    });
	
	videoboxSetup();
}

function videoboxSetup() {
  // Show that the .video is click area
  $('.feature #content-area .video').css('cursor','pointer');
	
  // Load 'video'  
    $('.feature #content-area .video').click( function() {
      showVale();
	  videoNodeUrl = $(this).find('a').attr('href');
      $("#popup-inner-inner").load(videoNodeUrl + ' #flashplayer', '', $('#popup').fadeIn("slow"));
	  $(this).blur();
	  return false;
    });
	
}

function showVale() {
      $('#vale').css('opacity',0.01);
	  $('#vale').show();
	  $('#vale').fadeTo("slow", 0.66);
}

function featureViewsStyleSetup() {
  $('.view-item-Features').each( function() {
	styleArray = $(this).find('.view-data-node-data-field-style-settings-field-style-settings-value').text().split(",");
	$(this).find('.view-data-node-title a').css('background-color', styleArray[0]);
	$(this).find('.view-data-node-view a').css('color', styleArray[1]);
	$(this).find('.view-data-node-title a').css('color', styleArray[2]);
  });
}