$(document).ready(function() {
	/* thickbox adjustment for smaller screens */
	$('a.thickbox').click(function() {
		var winH = $(window).height();
		
		var theIDs = '#TB_window, #TB_iframeContent';
		
		//$(theID).css('top',  winH/2-$(theID).height()/2);
		//$(theID).css('left', winW/2-$(theID).width()/2);
		
		if ( winH < 620 ) {
			$(theIDs).css({'width': '574px', 'height': '400px'});
			$('#TB_window').css({'margin-top': '-200px'});
		}
	});
});

$.fn.sortVideos = (function() {

	$('p#sort-messaging').html('');
	var countRegionMatches = 0;
	
	var thisCat = $('#cat-sort').val();
	var thisRegion = $('#region-sort').val();
	
	$.cookie("catData", thisCat, { expires: 0 });
	$.cookie("regionData", thisRegion, { expires: 0 });
	
	if (thisCat != 'nada') {
		$('#video-list-gutter ul#testimonial-videos li').each(function(){
			if ($(this).attr('class').match(thisCat)) {
				$(this).removeClass('hidden');
			} else {
				$(this).addClass('hidden');
			}
		});
	}
	if (thisRegion != 'nada') {
		$('#video-list-gutter ul#testimonial-videos li').each(function(){
			if ($(this).attr('class').match(thisRegion)) {
				$(this).removeClass('hidden');
			} else {
				$(this).addClass('hidden');
			}
		});
	}
	if (thisCat != 'nada' && thisRegion != 'nada') {
		$('#video-list-gutter ul#testimonial-videos li').each(function(){
			if ($(this).attr('class').match(thisCat + ' ' + thisRegion)) {
				countRegionMatches++;
				$(this).removeClass('hidden');
			} else if ($(this).attr('class').match(thisCat + ' ' + thisCat + ' ' + thisRegion)) {
				countRegionMatches++;
				$(this).removeClass('hidden');
			} else {
				$(this).addClass('hidden');
			}
		});
		if (countRegionMatches == 0) {
			$('p#sort-messaging').css('display','block').html('There are no videos in the category and region you selected. Please select a different category or region.');
		}
	}
	if (thisCat == 'nada') {
		$("#cat-sort").val('option:first');
		$.cookie("catData", null, { expires: -10 }); 
	}
	if (thisRegion == 'nada') {
		$("#region-sort").val('option:first');
		$.cookie("regionData", null, { expires: -10 });
	}
	if (thisCat == 'nada' && thisRegion == 'nada') {
		$('#video-list-gutter ul li').each(function() { 
			$(this).removeClass('hidden');
		});
	}
	return false;
});

$.fn.videoTypes = (function() {
	
	var thisCat = $('#cat-sort').val();
	var thisRegion = $('#region-sort').val();
	var sortMessage = $('p#sort-messaging').html();
	
	var videoType = $('#video-cat-sort').val();
	
	//$('ul#commercial-videos').show();

	if (videoType == 'commercials') {
		$('tr#cat-tr, tr#region-tr, ul#testimonial-videos, p#sort-messaging').hide();
		$('ul#commercial-videos').show();
		window.location = '/floodsmart/pages/videos/one_in_four.jsp';
	}
	if (videoType == 'testimonials') {
		$("#cat-sort").val(thisCat);
		$("#region-sort").val(thisRegion);
		$('p#sort-messaging').show().html(sortMessage);
		$('ul#commercial-videos').hide();
		$('ul#testimonial-videos, tr#cat-tr, tr#region-tr').show();
		window.location = '/floodsmart/pages/videos/atlanta_flood_stories.jsp';
	}
});

$(window).load(function() {
	
	// set the temp filter cookies data
	var catCookie = ( $.cookie("catData") );
	var regionCookie = ( $.cookie("regionData") );
	
	if (catCookie != null) {
		$('#cat-sort').val(catCookie);
		$('#video-list-gutter ul#testimonial-videos li').each(function(){
			if ($(this).attr('class').match(catCookie)) {
				$(this).removeClass('hidden');
			} else {
				$(this).addClass('hidden');
			}
		});
	} else {
		$('#cat-sort').val('nada');
	}

	if (regionCookie != null) {
		$('#region-sort').val(regionCookie);
		$('#video-list-gutter ul#testimonial-videos li').each(function(){
			if ($(this).attr('class').match(regionCookie)) {
				$(this).removeClass('hidden');
			} else {
				$(this).addClass('hidden');
			}
		});
	} else {
		$('#region-sort').val('nada');
	}
	
	if (catCookie != null && regionCookie != null) {
		var countRegionMatches = 0;
		
		$('#video-list-gutter ul#testimonial-videos li').each(function(){
			if ($(this).attr('class').match(catCookie + ' ' + regionCookie)) {
				countRegionMatches++;
				$(this).removeClass('hidden');
			} else if ($(this).attr('class').match(catCookie + ' ' + catCookie + ' ' + regionCookie)) {
				countRegionMatches++;
				$(this).removeClass('hidden');
			} else {
				$(this).addClass('hidden');
			}
		});
	}
	
	// get current url
	var theURL = document.location.toString();
	
	// if current url is one of the 4 commercial videos maintain selection
	if (theURL.match('/floodsmart/pages/videos/stuck_in_the_rain.jsp')) { 
		$('tr#cat-tr, tr#region-tr, ul#testimonial-videos, p#sort-messaging').hide();
		$('ul#commercial-videos').show();
		$('#video-cat-sort').val('commercials');
	} else if (theURL.match('/floodsmart/pages/videos/covered.jsp')) { 
		$('tr#cat-tr, tr#region-tr, ul#testimonial-videos, p#sort-messaging').hide();
		$('ul#commercial-videos').show();
		$('#video-cat-sort').val('commercials');
	} else if (theURL.match('/floodsmart/pages/videos/one_in_four.jsp')) { 
		$('tr#cat-tr, tr#region-tr, ul#testimonial-videos, p#sort-messaging').hide();
		$('ul#commercial-videos').show();
		$('#video-cat-sort').val('commercials');
	} else if (theURL.match('/floodsmart/pages/videos/tailor_ad.jsp')) { 
		$('tr#cat-tr, tr#region-tr, ul#testimonial-videos, p#sort-messaging').hide();
		$('ul#commercial-videos').show();
		$('#video-cat-sort').val('commercials');
	};
	
	// basic hover event for css change
	$('#video-list-gutter ul li').mouseenter(function(){
		$(this).addClass('video-current');
		$('img',this).css('margin-top','-49px');
	}).mouseleave(function() {
		$(this).removeClass('video-current');
		$('img',this).css('margin-top','0');
		return false;
	});
	
	// category && region drop down filtering
	$("#region-sort, #cat-sort").change(function() {
		$().sortVideos();
	});
	
	// video type drop down filtering
	$('#video-cat-sort').change(function() {
		$().videoTypes();
	});
});
