jQuery(function ($) {
	$('#sort_submit, #actual-content .video-metrics .video:not(:first)').hide();
	$('.channel-video-list .video-post:even').addClass('even-video-post');

	$('.btn-login').click(function () {
		$('#header .holder').hide();
		$('.login').show();
		return false;
	});
	
	
	$('.btn-close').click(function () {
		$('.login').hide();
		$('#header .holder').show();
		return false;
	});
	
	$('#head .nav ul li a').hover(function () {
		$(this).addClass('hover');
		return false;
	}, function () {
		$(this).removeClass('hover');
		return false;
	});
	
	$('#navigation li:last, #footer-nav li:last, #footer-cat-nav li:last').addClass('last');
	
	$('.video-thumb img').reflect({
		'height': 30,
		'opacity': 0.2
	});

	$('#content .testimonial:last').addClass('last-testimonial');
	
	$('#uploadform .client_info').hide();
	$('#video_add_client_info').change(function() {
		if ($(this).is(':checked') != '') {
			$('#uploadform .client_info').show();
		} else {
			$('#uploadform .client_info').hide();
		}
	});
	
	$('#user-videos').change(function() {
		$('#video-' + $(this).find('option:selected').val()).show().siblings().hide();
	});
	
	$('#uploadform').submit(function() {
		var valid = true;
		var errors = '';
		
		var desc = $(this).find('.desc');
		
		$(this).find('.required').each(function() {
			if ($(this).val() == '') {
				if (valid) $(this).focus();
				
				valid = false;
				errors += 'The "' + $(this).attr('title') + '" field is required\n';
			}
		});
		
		if (!valid && errors != '') {
			alert(errors + ' ');
		}
		
		return valid;
	});
	
	$('#get_quote_form .service-details').hide();
	$('#get_quote_form .section h4').click(function() {
		$(this).toggleClass('expanded').siblings('.service-details').slideToggle(300);
	});
	
	$('.video-thumb a').hover(
		function() {
			$(this).addClass('hover')
		},
		function() {
			$(this).removeClass('hover');
		}
	);
	
	if ($.browser.msie && ($.browser.version.substring(0,1) == '6')) {
		DD_belatedPNG.fix('#head .nav ul li a span, .video, #logo a, img');
	}
	
	$('.header-links .login-link, .btn-login').click(function() {
		$('#header_dropdown').show().find('.' + $(this).attr('rel')).slideDown(250);
		return false;
	});
	
	$('#header_dropdown .content a.slide-up').click(function() {
		$('#header_dropdown').slideUp(250, function() {
			$(this).hide().find('.content > div').hide();
		});
		return false;
	});
	
	$('#registration_form, #mchd_contact_form, #get_quote_form, .client-contact-form').submit(function() {
		var valid = true;
		var errors = '';
		
		$(this).find('.required').each(function() {
			if ($(this).val() == '' || $(this).val() == $(this).attr('title')) {
				$(this).addClass('field-error');
				errors += 'The "' + $(this).attr('title') + '" field is required\n';
				if (valid) {
					$(this).focus();
				}
				valid = false;
			} else {
				$(this).removeClass('field-error');
			}
		});
		
		$(this).find('.email').each(function() {
			var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
			
			if (!pattern.test($(this).val())) {
				$(this).addClass('field-error');
				errors += 'The email address is invalid\n';
				if (valid) {
					$(this).focus();
				}
				valid = false;
			} else {
				$(this).removeClass('field-error');
			}
		});
		
		$(this).find('.pass_confirm').each(function() {
			if ($(this).val() != $('#registration_form #password').val()) {
				$(this).addClass('field-error');
				errors += 'Password mismatch\n';
				if (valid) {
					$(this).focus();
				}
				valid = false;
			} else {
				$(this).removeClass('field-error');
			}
		});
		
		if ($(this).find('#agree-to-terms').length && !$(this).find('#agree-to-terms:checked').length) {
			errors += 'You must agree to the terms of use\n';
			valid = false;
		}
		
		if (!valid) {
			alert(errors + ' ');
		}
		
		return valid;
	});
	
	$('#first-name').focus();
	$('#sort_by, #videos_filter').change(function() {
		$(this).parents('form').submit();
	});
	
	$('.blink').each(function() {
		$(this).focus(function() {
			if ($(this).val() == $(this).attr('title')) {
				$(this).val('');
			}
		});
		
		$(this).blur(function() {
			if ($(this).val() == '') {
				$(this).val($(this).attr('title'));
			}
		});
	});
	
	$('textarea.embed-code').click(function() {
		$(this).select();
	}).each(function() {
		var psid = new Date().getTime();
		var eScript = $(this).data('script');
		var textarea = $(this);
		$(this).before('<label for="width-' + psid + '">Width:</label><input id="width-' + psid + '" class="width" value="' + $(this).data('width') + '" /><br />' +
					   '<label for="height-' + psid + '">Height:</label><input id="height-' + psid + '" class="height" value="' + $(this).data('height') + '" />' +
					   '<label for="autoplay-' + psid + '">Autoplay:</label><input id="autoplay-' + psid + '" class="autoplay" type="checkbox" checked />');
		
		$(this).siblings('input').change(function() {
			if ($(this).hasClass('width')) {
				var eWidth = $(this).val();
				$(this).siblings('.height').val(parseInt(eWidth*0.5625)); // 16:9
			} else if ($(this).hasClass('height')) {
				var eHeight = $(this).val();
				$(this).siblings('.width').val(parseInt(eHeight*1.7778)); // 16:9
			}
			
			var video = $(this).parent();
			var eWidth = video.find('.width').val();
			var eHeight = video.find('.height').val();
			var eAutoPlay = video.find('.autoplay').is(':checked');
			
			$.post(window.location.href, { request: 'embedcode', script: eScript, width: eWidth, height: eHeight, autoplay: eAutoPlay }, function(data, status, XHR) {
				textarea.val('');
				setTimeout(function() {
					textarea.val(data);
				}, 50);
			});
		});
	});
	
	$('.addons .thumbnail-link').colorbox({
		inline: true
	});
	
	// Client subsite tabs
	(function() {
		var boxes = $('#main .client-site-container .boxes .box');
		var tabs = $('#main .client-site-container .navigation a[href^="#"]');
		var currentBox = boxes.first();
		
		boxes.each(function() {
			$(this).height($(this).height());
		}).filter(':not(:first)').hide();
		
		tabs.click(function() {
			var target = $($(this).attr('href'));
			if (target[0] != currentBox[0]) {
				boxes.slideUp();
				currentBox = target.slideDown();
			}
			
			return false;
		});
	})();
	
	// City-list select redirect
	$('#city-list').change(function() {
		var url = $(this).find('option:selected').val();
		window.location = url;
	});
});
