var is_opera =  navigator.userAgent.indexOf('Opera') > -1;
var is_safari = navigator.userAgent.indexOf('AppleWebKit/') > -1;
var is_chrome = navigator.userAgent.indexOf('Chrome') > -1;
var is_ipad = navigator.userAgent.toLowerCase().indexOf('ipad') > -1;
var is_firefox = navigator.userAgent.indexOf('Firefox') > -1;
var is_mac = navigator.platform.toLowerCase().indexOf('mac') > -1;
var is_linux = navigator.platform.toLowerCase().indexOf('linux') > -1;

var ie6 = false;
var ie7 = false;
var ie8 = false;

$(document).ready(function() {
	if(typeof article_vert_align == 'object') {
		article_vert_align.setup();
	}   
		
	$('#searchForm').submit(function() {
		document.location = "/search/node/" + $('#searchText').val();
		return false;
	});
		
	$('#searchForm2').submit(function() {
		document.location = "/search/node/" + $('#searchText2').val() + " type:news";
		return false;
	});	
	
	$('#searchForm3').submit(function() {
		document.location = "/search/node/" + $('#searchText3').val();
		return false;
	});		

	$('.frmContact').submit(function(){
		name = this.id;
		success = sendEmail(name);
		
		if (success) {
			$(this).each(function(){
				this.reset();
			});		
		}
		return false;
	});
});

var article_vert_align = {
	container: null,
	items: null,
    
	setup: function() {
		this.container = $('#featured-articles');
		if(!this.container.length) return;
        
		this.items = $('.article', this.container)
        
		var max_height = 0;

		$(this.items).map(function(i, el){
			var current_inner = $('.inner', el).first();
			var text_height = $(current_inner).innerHeight();
			if(text_height >  max_height) {
				max_height = text_height;
			}
		})
        
		$('#featured-articles .inner').css({
			'height':max_height
		});
	}
}

function renderVideo(video_id, flv_file, mp4_file, webm_file, ogv_file, jpg_file) {

	var ua = navigator.userAgent.toLowerCase();
    
	if(ua.indexOf('android') != -1 || ua.indexOf('ipad') != -1 || ua.indexOf('iphone') != -1 || ua.indexOf('ipod') != -1) {
        
		function html_5_video_string() {
			var video_string = '';
			video_string += '<video id="' + video_id + '" width="448" height="252" controls="controls" preload="none">';
			video_string += '<source type=\'video/webm; codecs="vp8, vorbis"\' src="' + webm_file + '" />';
			video_string += '<source type=\'video/ogg; codecs="theora, vorbis"\' src="' + ogv_file + '" />';
			video_string += '<source type="video/mp4" src="' + mp4_file + '" />';
			video_string += '</video>';
            
			return video_string;
		}
        
		$('#video-container').html(html_5_video_string('home'));

	} else {

		function flowplayer_string(file_prefix) {
			var video_string = '';
			video_string += '<a href="' + flv_file + '" id="video-home-content" style="display:block;width:448px;height:252px"><img src="' + jpg_file + '" alt="" /></a>';
			return video_string;
		}
        
		$('#video-container').html(flowplayer_string());

		flowplayer("video-home-content", "sites/all/themes/haverford/videos/flowplayer-3.2.7.swf",  {
			clip: {
				autoPlay: true, 
				autoBuffering: true
			},
			plugins: {
		//controls:  null
		}
		});
	}
    
}

var ajax_url = '/sites/all/themes/haverford/scripts/ajax_handler.php';	

function sendEmail(formName) {
	
	var email = $('#' + formName + ' #email').val();
	var recipient = $('input#recipient').val();
	var cc = $('#' + formName + ' #cc').val();	
	var fullname = $('#' + formName + ' #fullname').val();
	var subject = $('#' + formName + ' select option:selected').val();
	var comments = $('#' + formName + ' #comments').val();
	var phone = $('#' + formName + ' #phone').val();
	var validationFailed = false;
	
	if ($.trim(fullname) == "") {
		validationFailed = true;		
		$('#' + formName + ' #fullname').addClass('required');
		$('#' + formName + ' #fullname').removeClass('iptText');
	} else {
		$('#' + formName + ' #fullname').removeClass('required');
		$('#' + formName + ' #fullname').addClass('iptText');
	}
	
	if ($.trim(email) == "") {
		validationFailed = true;		
		$('#' + formName + ' #email').addClass('required');
		$('#' + formName + ' #email').removeClass('iptText');
	} else {
		$('#' + formName + ' #email').removeClass('required');
		$('#' + formName + ' #email').addClass('iptText');
	}

	if ($.trim(comments) == "") {
		validationFailed = true;		
		$('#' + formName + ' #comments').addClass('required');
		$('#' + formName + ' #comments').removeClass('iptText');
	} else {
		$('#' + formName + ' #comments').removeClass('required');
		$('#' + formName + ' #comments').addClass('iptText');
	}
	
	if (!validationFailed) {	
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;			
		if (!filter.test(email)) {
			validationFailed = true;		
			$('#' + formName + ' #email').addClass('required');
			$('#' + formName + ' #email').removeClass('iptText');
			alert('You must enter a valid email address');
			return false;				
		} else {
			$('#' + formName + ' #email').removeClass('required');
			$('#' + formName + ' #email').addClass('iptText');		
		}
	}
	
	if (!validationFailed && (phone.length > 0)) {
		var filter = /^\(?(\d{3})\)?[. ]?(\d{3})[. ]?(\d{4})$/;		
		if (!filter.test(phone)) {
			validationFailed = true;		
			$('#' + formName + ' #phone').addClass('required');
			$('#' + formName + ' #phone').removeClass('iptText');
			alert('You must enter a valid phone number');
			return false;				
		} else {
			$('#' + formName + ' #phone').removeClass('required');
			$('#' + formName + ' #phone').addClass('iptText');		
		}
	}
	
	if (validationFailed) {
		alert('Required fields must have a value.');
		return false;
	}
	
$.post(ajax_url
	,{
		"action":"sendemail"
		, "recipient":recipient
		, "cc":cc		
		, "sender":email
		, "fullname":fullname
		, "subject":subject
		, "comments":comments
		, "phone":phone
	}
	,function(data){
		$('.overlayContainer').hide();					
		alert(data);
	}
	,"text");
	
	return true;
}
