$(document).ready(function() {
	$("#solutions").hover(
		function(){
			$("#solutions_container").css("display","block");
		}, function(){
			$("#solutions_container").css("display","none");
	});
		
	// JS for the in page navigation / tabbed navigation
	var current_link;
	$(".arrow_content a").click(
	  function () {
			$(".selected").removeClass("selected");
			$(".selected_left_edge").removeClass("selected_left_edge");
			$(".selected_right_edge").removeClass("selected_right_edge");
			
			current_link = $(this).parent().parent().attr("id").substr(9);
			if (current_link == 1) {
				$(this).parent().prev().addClass("selected_left_edge");
			} else {
				$(this).parent().parent().prev().children(":last-child").prev().addClass("selected_left_edge");
			}
			$(this).parent().addClass("selected");
			$(this).parent().next().addClass("selected_right_edge");
	  }
	);
	
	$(".square_content a").click(
	  function () {
			$(".s_selected").removeClass("s_selected");
			$(this).parent().parent().addClass("s_selected");
	  }
	);
	
	$("#tab_nav ul li a").click(
		function() {
			$(".active_tab").removeClass("active_tab").addClass("inactive_tab");
			$current_link = $(this).parent().attr("id").substr(9);
			$last_link = ($("#tab_nav ul li").size() - 1)/2;
			if ($current_link != 1) {
				$(".active_left_edge").removeClass("active_left_edge").addClass("inactive_left_edge");
				if ($current_link == $last_link) {
					$(".inactive_right_edge").removeClass("inactive_right_edge").addClass("active_right_edge");
				} else {
					$(".active_right_edge").removeClass("active_right_edge").addClass("inactive_right_edge");
				}
			} else if ($current_link == 1) {
				$(".inactive_left_edge").removeClass("inactive_left_edge").addClass("active_left_edge");
			} 
			
			$(this).parent().removeClass("inactive_tab").addClass("active_tab");
		}
	);
	
	$landing_current_page = 1;
	if ($landing_current_page == 1) {
		$("#landing_prev_page").css("display","none");
		$(".landing_btn_next").html("LEARN MORE");
	}
		
	//subnav for main navigation
	var pathname = window.location.pathname;
	var current_page_section = pathname.substring(1,pathname.indexOf("/",1));
	if (pathname != "/") {
		$("#sub_nav_space").addClass("sub_nav_space");
		$("#sub_" + current_page_section).css("display","block");
		$("#sub_nav_bg").css("display","block");
	}
	
	
	$("#nav_main li a").hover(
		function () {
			$active_sub = $(this).parent().attr("id").substr(4);
			if ($active_sub != "") {
				$("#sub_" + current_page_section).css("display","none");
			}
			$("#sub_nav_bg").css("display","block");
			$("#sub_" + $active_sub).css("display","block");
		},
		function () {
			$(".header_sub_nav").hover(function() {
				if ($active_sub != "") {
					$("#sub_" + current_page_section).css("display","none");
				}
				$("#sub_" + $active_sub).css("display","block");
			}, function() {
				$(".header_sub_nav").css("display","none");
				$("#sub_" + current_page_section).css("display","block");
				if (pathname == "/") {
					$("#sub_nav_bg").css("display","none");
				}
			});
		}
	);
	
	
	// Ticker on Homepage
	$('.ticker').vTicker({
	   speed: 500,
	   pause: 5000,
	   showItems: 1,
	   animation: 'fade',
	   mousePause: true,
	   height: 0,
	   direction: 'up'
	});
	
	$current_ticker_section = 1;
	$current_section = 1;
	$interval = setInterval(function() {
		$current_ticker_section++;
		$(".ticker_active").removeClass("ticker_active");
		$(".ticker").css("display","none");
		$("#ticker_" + $current_ticker_section).css("display","block");
		$("#ticker_link_" + $current_ticker_section).addClass('ticker_active');
		if($current_ticker_section == 6){
			$current_ticker_section = 0;
		}
	}, 5000); 
	
	if($current_ticker_section == 1){
		$(".ticker").css("display","none");
		$("#ticker_" + $current_ticker_section).css("display","block");
	}
	
	$("#ticker_box_bottom_middle span").hover(
		function(){
			clearInterval($interval);
			$current_section = $(this).attr("id").substr(12);
			$(".ticker_active").removeClass("ticker_active");
			$(".ticker").css("display","none");
			$("#ticker_" + $current_section).css("display","block");
			$("#ticker_link_" + $current_section).addClass('ticker_active');
			$current_ticker_section = $current_section;
		}, function(){
			
		}
	);
	
	//Home Expand
	$(".hover_expand").hover(
		function(){
			$(this).children().stop(true, true).fadeIn();
		}, function(){
			$(this).children().stop(true, true).fadeOut();
	});
	
	//Ecosystem - Your Audience Expand
	$(".youraudience_stats").hover(
		function(){
			$(this).children(".youraudience_infograph").stop(true, true).fadeOut();
			$(this).children(".youraudience_text").stop(true, true).fadeIn();
		}, function(){
			$(this).children(".youraudience_text").stop(true, true).fadeOut();
			$(this).children(".youraudience_infograph").stop(true, true).fadeIn();
	});
});

/* Email functions start */

//Grab data from email fields
function ef_email($type) {
	$email_id = "";
	$email_reply = "";
	//Grab all input id into $email_arr
	$inputs = $('#email_form input, #email_form textarea, #email_form select');
	var values = {};
	$email_arr = new Array;
	$inputs.each(function() {
		if ($(this).attr('id') && $(this).attr('id') != 'ef_submit') {
			$email_arr.push($(this).attr('id'));
		}
		if (($(this).attr('type') == 'checkbox') && ($email_arr[$email_arr.length-1] != $(this).parent().attr('id'))) {
			$email_arr.push($(this).parent().attr('id'));
		}
    });

	//Validate if all required fields filled
	$required = true;
	for (i = 0; i < $email_arr.length; i++) {
		if (($('#'+$email_arr[i]).attr('id').substr(0,6) == 'ef_r_e')) {
			$email_id = $('#'+$email_arr[i]).attr('id');
			$email_reply = $('#'+$email_arr[i]).val();
		}
		if ($('#'+$email_arr[i]).attr('id').substr(0,4) == 'ef_r') {
			if ($('#'+$email_arr[i]).attr('type') != '') {
				if (!$('#'+$email_arr[i]).val()) {
					$required = false;
					$('#'+$email_arr[i]).css("border","1px solid #f00");
				} else {
					$('#'+$email_arr[i]).css("border","1px solid #bbb");
				}
			} else {
				if ($("#"+$email_arr[i]+" :checked").size() == 0) {
					$required = false;
				}
			}
		}
	}
	if (!$required) {
		alert("Please enter the required fields!");
		return false;
	}

	//Concat all data to a string with items separated by :::
	$send_str = "";
	$sep = "|||";
	for (i = 0; i < $email_arr.length; i++) {
		if ($('#'+$email_arr[i]).attr('type') == '') {
			$check_type = "";
			$check_type_checked = $("#"+$email_arr[i]+" :checked");
			if ($check_type_checked.size() > 0) {
				$send_str += $('#'+$email_arr[i]).attr('name')+"::";
				$check_type = "";
				$check_type_checked.each(function() { 
					$check_type += $(this).val()+"::";
				});
				$check_type = $check_type.substr(0,$check_type.length-2);
			}
			$send_str += $check_type+$sep;
		} else {
			if (i == $email_arr.length - 1) {
				$sep = "";
			}
			$send_str += $('#'+$email_arr[i]).attr('name')+"::" + $('#'+$email_arr[i]).val()+$sep;
		}
	}

	$send_str = rawurlencode($send_str);
	
	if (!$('#'+$email_id).val()) {
		ef_send($type,$send_str);
	} else {
		$email = $('#'+$email_id).val();
		if (ef_emailValidate($email)) {
			ef_send($type,$send_str);
		} else {
			alert("Email address was not correct!");
			return false;
		}
	}
}

//Execute sending
function ef_send ($type,$send_str) {
	$("#ef_submit").val("Sending...");
	$.post("/email/",{type:$type,reply:$email_reply,send_str:$send_str},function(data){$("#email_form").text(data);}, "html");
}

//Email format validation
function ef_emailValidate(email) {
  var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
  return pattern.test(email);
}
/* Email functions end */

function rawurlencode(str) {
	return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A');
}

function clearInput(input) {
	$(input).val("");
}


// switch to current page for in page navs
function switchToPage($this, $type) {
	$("div[id^='nav_link_page_']").css("display","none");
	if ($type == "arrow") {
		$active_link = $($this).parent().parent().attr("id");
	} else if ($type == "square") {
		$active_link = $($this).parent().parent().parent().attr("id");
	} else if ($type == "tab") {
		$active_link = $($this).parent().attr("id");
	}
	$current_page_number = parseInt($active_link.substr(9));
	$("#nav_link_page_" + $current_page_number).fadeIn('slow');
	
	$landing_current_page = $current_page_number;	
	if ($landing_current_page == 5) {
		$(".landing_btn_next").css("display","none");
		return false;
	}
	checkButtons();
}

function switchPage($direction) {
	$('#nav_link_page_' + $landing_current_page).css('display','none');
	if ($direction == "next") {
		$landing_current_page = $landing_current_page + 1;
		checkButtons();
	} else if ($direction == "prev") {
		$landing_current_page = $landing_current_page - 1;
	}
	
	$landing_next_page = $landing_current_page;
	$('#nav_link_page_' + $landing_next_page).fadeIn('slow');
	
	$('.selected').removeClass('selected');
	
	if ($direction == "next") {
		$('.selected_left_edge').removeClass('selected_left_edge');
		$('.selected_right_edge').addClass('selected_left_edge').removeClass('selected_right_edge');	
	} else if ($direction == "prev") {
		$('.selected_right_edge').removeClass('selected_right_edge');
		$('.selected_left_edge').addClass('selected_right_edge').removeClass('selected_left_edge');
	}
	
	$('#nav_link_' + $landing_next_page + ' .arrow_content').addClass('selected');	
	$('#nav_link_' + $landing_next_page + ' div:last-child').prev().addClass('selected_right_edge');
	
	if ($direction == "prev") {
		if ($landing_current_page == 1) {
			$('#nav_link_1 div:first-child').addClass('selected_left_edge');
		} else {
			$temp = $landing_next_page - 1;	
			$('#nav_link_' + $temp + ' div:last-child').prev().addClass('selected_left_edge');	
		}
		checkButtons();
	}
}

function checkButtons() {
	if ($landing_current_page == 1) {
		$(".landing_btn_next").html("LEARN MORE");
		$("#landing_prev_page").css("display","none");
		$(".landing_btn_next").css("display","block");
	}
	else if ($landing_current_page == 4) {
		$(".landing_btn_next").html("CONTINUE");
		$("#landing_prev_page").css("display","block");
		$(".landing_btn_next").css("display","block");
	} else if ($landing_current_page == 5) {
		$(".landing_btn_next").css("display","none");
	} else {
		$(".landing_btn_next").html("CONTINUE");
		$("#landing_prev_page").css("display","block");
		$(".landing_btn_next").css("display","block");
	}
}

//Ticker
function ticker_nav(direction) {
	clearInterval($interval);
	$(".ticker_active").removeClass("ticker_active");
	if (direction == "next") {
		$current_ticker_section++;
		if ($current_ticker_section == 7){
			$current_ticker_section = 1;
		}
	} else if (direction == "prev") {
		if($current_ticker_section != 0){
			$current_ticker_section--;
		}
		if ($current_ticker_section == 0){
			$current_ticker_section = 6;
		}
	}
	$(".ticker").css("display","none");
	$("#ticker_" + $current_ticker_section).css("display","block");
	$("#ticker_link_" + $current_ticker_section).addClass('ticker_active');
	$current_section = $current_ticker_section;
}
