	/*
	* File: common.js
	*/

	$(document).ready(function ()
	{
		var inputs = $('input[type=text], textarea');
		inputs.each(function(i,e){e=$(e);
			var title = e.attr('title');
			if(title){
				e.focus(function(){if(e.val() == title) e.val('');});
				e.blur(function(){if(e.val() == '') e.val(title);});
			}
			e.blur();
		});

		$('select').each(function(){
			new StylizedSelect(this);
		});

		$("#request_demo_btn").click(submitRequestDemoForm);
		$("#view_more_events").click(viewMoreEvents);
	});

	function include(filename){
		var scripts = document.getElementsByTagName("script");
		for(var i in scripts) if(scripts[i].src == filename) return false;

    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', filename);
    html_doc.appendChild(js);
    return false;
	}//include

	function href(url, nw){
		if(nw) window.open(url, "_blank")
		else window.location.assign(url);
		return false;
	}//href

	function go(url, nw){
		if(nw) window.open(url, "_blank")
		else window.location.assign(url);
		return false;
	}//href

	function popup(url, w, h, name){
		if(!name) name = "popup_" + unique();
		var w = window.open(url, name, "location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no,width="+w+",height="+h);
		w.focus();
	}//popup

	function calendarOpen(i){
		openCalendar('',i.form.name || i.form.id,i.name,'date');
	}//calendarOpen

	function PopupOverlay(width, height){
		$('body .popup').remove();
		var This = this;
		This.elements = {};
		This.elements.popup = $('<div class="popup"><div class="overlay"></div><div class="content"><div></div></div><div class="close"></div></div>');
		This.elements.overlay = $('.overlay', This.elements.popup);
		This.elements.content = $('.content', This.elements.popup);
		This.elements.close = $('.close', This.elements.popup);
		This.html = function(html){
			html = $('<div>'+html+'</div>');
			var o = $('object[classid^="clsid"]', html);
			var flashvars = $('param[name=flashvars]', html).attr('value');
			if(o.length) o.get(0).wmode = 'transparent';
			$('object[type="application/x-shockwave-flash"], embed[type="application/x-shockwave-flash"]', html).attr('wmode', 'transparent');
			html = $(html.html());

			$('div:eq(0)', This.elements.content).html(html);

			var o = $('object[classid^="clsid"]', This.elements.content);
			if(flashvars && o.length) o.get(0).flashvars = flashvars;

			//This.resize();
			var w, h;
			w = parseInt(html.attr('width'));
			h = parseInt(html.attr('height'));
			if(w && h) This.size(w, h);
			else{
				w = parseInt(html.css('width'));
				h = parseInt(html.css('height'));
				if(w && h) This.size(w, h);
			}
			This.center();
		};
		This.load = function(url, data){
			$.ajax({
				url: url,
				data: data,
				type: data ? 'post' : 'get',
				success: This.html,
				error: function(){alert('Failed to load request.');}
			});
		}
		This.close = function(){
			This.elements.popup.remove();
			This = null;
		};
		This.position = function(x, y){
			This.elements.content.css({left: x, top: y});
			This.elements.close.css({left: x+This.elements.content.outerWidth()-18, top: y-18});
		};
		This.size = function(width, height){
			This.elements.content.css({width: width, height: height});
		};
		This.resize = function(){
			var content = $('div:eq(0)', This.elements.content);
			This.size(content.outerWidth(), content.outerHeight());
		};
		This.center = function(){
			This.position((windowWidth()-This.elements.content.outerWidth())/2, (windowHeight()-This.elements.content.outerHeight())/2);
		};
		This.elements.overlay.css({width: $(document).width(), height: $(document).height()});//.click(This.close);
		This.elements.close.click(This.close);
		if(width && height) This.size(width, height);
		This.center();
		$('body').append(This.elements.popup);
	}//PopupOverlay

	function windowWidth(){
		var r = 0;
	  if(typeof(window.innerWidth) == 'number') r = window.innerWidth;
	  else if(document.documentElement && document.documentElement.clientWidth) r = document.documentElement.clientWidth;
	 	else if(document.body && document.body.clientWidth) r = document.body.clientWidth;
	  return r;
	}//windowWidth

	function windowHeight(){
		var r = 0;
	  if(typeof(window.innerHeight) == 'number') r = window.innerHeight;
	  else if(document.documentElement && document.documentElement.clientHeight) r = document.documentElement.clientHeight;
	 	else if(document.body && document.body.clientHeight) r = document.body.clientHeight;
	  return r;
	}//windowHeight

	function checkPhone(e){
		var f = 'xxx-xxx-xxxx';
		var v = e.value;
		if(v.length){
			for(var i = 0; i < f.length; i++){
				if(i >= v.length) break;
				if(f[i] != 'x') v = v.substr(0, i)+f[i]+v.substr(i+(v[i].match(/\d/) ? 0 : 1));
				else if(!v[i].match(/\d/)) v = v.substr(0, i)+v.substr(1+i--);
			}
		}
		e.value = v.substr(0, f.length);
	}

	function submitRequestDemoForm(id)
	{
		var error = "";
		var frm = $('#request_demo_form');
		var name = $("[name=name]", frm).val();
		var company = $("[name=company]", frm).val();
		var email = $("[name=email]", frm).val();

		if (name == "" || name == "Name")	error += "Please enter your name.<br />";
		if (company == "" || company == "Company") error += "Please enter your company.<br />";
		if (email == "" || email == "Email") error += "Please enter your email.<br />";
		if (email != "" && email != "Email" && !validateEmail(email)) error += "Please enter a valid email address.<br />";

		if (error != "")
		{
			$("#rd_error").html(error);
		}
		else
		{
			$("#rd_error").html('');

			$.post("/ajax/request_demo/",
			{
				name: name,
				company: company,
				email: email
			},
			function(data)
  		{
				var tmp = data.split("_");
  			if (tmp[1] == "SUCCESS")
				{
					$("#rd_success").html("Your request was sent successfully! Thank you.");
					ThankYouPopup(tmp[0]);
					$("#request_demo_form").hide();
				}
				else
				{
					$("#rd_error").html(data);
				}
  		},"text");
		}

		return false;
	}//submitRequestDemoForm

	function submitRequestDemoForm2(id)
	{
		_gaq.push(['_trackPageview', '/request_demo/step2']);

		var error = "";
		var gettingInTouch = $("#getting_in_touch").val();
		var monthlyBudget = $("#monthly_budget").val();
		var description = $("#description").val();

		if (gettingInTouch == "")	error += 'Please select "' + "I'm getting in touch about" + '".<br />';
		if (monthlyBudget == "")	error += 'Please select "' + "Total Monthly Budget" + '".<br />';

		if (error != "")
		{
			$("#rdp_error").html(error);
		}
		else
		{
			$("#rdp_error").html('');

			$.post("/ajax/request_demo/",
			{
				action: "update_request_demo",
				getting_in_touch: gettingInTouch,
				monthly_budget: monthlyBudget,
				description: description,
				record_id: id
			},
			function(data)
  		{
  			if (data == "SUCCESS")
				{
					_gaq.push(['_trackEvent', 'formSubmission', 'submitted', 'homepageDemo']);

					ThankYouPopup2();
					$("#request_demo_form").hide();
				}
				else
				{
					$("#rdp_error").html(data);
				}
  		},"text");
		}

		return false;
	}//submitRequestDemoForm2

	function changePartnersCat(select)
	{
		$.post("/ajax/get_partners/",
		{
			partners_filter: select.val()
		},
		function(data)
		{
			if (data != "")
			{
				$("#partners_results").html(data);
			}
		},"text");

	}//changePartnersCat

	function changeResultsCat(select)
	{
		$.post("/ajax/get_results/",
		{
			cs_filter: select.val()
		},
		function(data)
		{
			if (data != "")
			{
				$("#results").html(data);
			}
		},"text");
	}//changeResultsCat

	function viewMoreEvents()
	{
		$("[id^=ie_]").each(function()
		{
			$(this).show();
		});

		$("#view_more_events").hide();

	}//viewMoreEvents

	function ThankYouPopup(recordID)
	{
		var popUpHTML = '';
		popUpHTML  = '<div id="thank_you_popup" style="height: 450px;">';
		popUpHTML += '	<div class="center AvenirHeavy default_color" style="font-size: 90px; line-height: 100%;">';
		popUpHTML += '		<div class="c h10"></div>';
		popUpHTML += '		Thanks!';
		popUpHTML += '	</div>';
		popUpHTML += '	<div class="AvenirLight fs18 lh24">We just need a few more things to best route your inquiry.</div>';
		popUpHTML += '	<div class="c h10"></div>';
		popUpHTML += '	<center>';
		popUpHTML += '		<table cellspacing="0" cellpadding="0">';
		popUpHTML += '			<tr><td><div id="rdp_error" class="fl i fs13" style="color: #932323; margin-bottom: 10px;"></div></td><tr>';
		popUpHTML += '			<tr>';
		popUpHTML += '				<td class="top left">';
		popUpHTML += '					<select id="getting_in_touch" name="getting_in_touch" class="fl" style="width: 250px;">';
		popUpHTML += "						<option value=''>I'm getting in touch about:</option>";
		popUpHTML += "						<option value='Setting up a meeting with your team'>Setting up a meeting with your team</option>";
		popUpHTML += "						<option value='Partnering with MediaMath'>Partnering with MediaMath</option>";
		popUpHTML += "						<option value='Learning about your product'>Learning about your product</option>";
		popUpHTML += "						<option value='Jobs at MediaMath'>Jobs at MediaMath</option>";
		popUpHTML += "						<option value='Request a Demo'>Request a Demo</option>";
		popUpHTML += "						<option value='Other'>Other</option>";
		popUpHTML += '					</select>';
		popUpHTML += '				</td>';
		popUpHTML += '				<td class="top left">';
		popUpHTML += '					<span class="required fs13" style="color: #679b41;">&nbsp;*</span>';
		popUpHTML += '				</td>';
		popUpHTML += '			</tr>';
		popUpHTML += '			<tr>';
		popUpHTML += '				<td class="top left">';
		popUpHTML += '					<div class="c h15"></div>';
		popUpHTML += '					<select id="monthly_budget" name="monthly_budget" class="fl" style="width: 250px;">';
		popUpHTML += "						<option value=''>Total Monthly Budget:</option>";
		popUpHTML += "						<option value='Under $50,000'>Under $50,000</option>";
		popUpHTML += "						<option value='$50,000 - $100,000'>$50,000 - $100,000</option>";
		popUpHTML += "						<option value='$100,000 - $500,000'>$100,000 - $500,000</option>";
		popUpHTML += "						<option value='$500,000 - $1M'>$500,000 - $1M</option>";
		popUpHTML += "						<option value='$1M+'>$1M+</option>";
		popUpHTML += '					</select>';
		popUpHTML += '				</td>';
		popUpHTML += '				<td class="top left">';
		popUpHTML += '					<div class="c h15"></div>';
		popUpHTML += '					<span class="required fs13" style="color: #679b41;">&nbsp;*</span>';
		popUpHTML += '				</td>';
		popUpHTML += '			</tr>';
		popUpHTML += '			<tr>';
		popUpHTML += '				<td class="top left">';
		popUpHTML += '					<div class="c h15"></div>';
		popUpHTML += '					<textarea class="text resizev" id="description" name="description" title="Description" style="width: 268px; height: 85px;"></textarea>';
		popUpHTML += '					<div class="c h20"></div>';
		popUpHTML += '					<input type="image" class="fr submit" src="/img/clear.gif" onclick="submitRequestDemoForm2(' + recordID + '); return false;" />';
		popUpHTML += '				</td>';
		popUpHTML += '				<td></td>';
		popUpHTML += '			</tr>';
		popUpHTML += '		</table>';
		popUpHTML += '	</center>';
		popUpHTML += '</div>';

		(new PopupOverlay(620, 450)).html(popUpHTML);

		$('#thank_you_popup select').each(function()
		{
			new StylizedSelect(this);
		});

	} //ThankYouPopup

	function ThankYouPopup2()
	{
		(new PopupOverlay(620, 300)).html("<script language='JavaScript1.1' src='http://pixel.mathtag.com/event/js?mt_id=110422&mt_adid=101341&v1=&v2=&v3=&s1=&s2=&s3='></script><http://pixel.mathtag.com/event/js?mt_id=110422&mt_adid=101341&v1=&v2=&v3=&s1=&s2=&s3=%27%3E%3C/script%3E>" + '<div class="center AvenirHeavy default_color" style="font-size:90px;line-height:100%;"><div class="c h10"></div>Thanks!<div class="c h10"></div><img width="75" height="48" src="/img/main/popup_logo.gif" alt="" title="" /><div class="c h10"></div></div>');
	}//ThankYouPopup2

	function validateEmail(email)
	{
		re=/.+\@.+\..+/;
		OK = re.exec(email);
		return (OK);
	}

var StylizedSelect = function(e){
	var This = this;
	This.target = $(e);
	if(This.target.data('stylized')) return;
	This.target.data('stylized', true);
	This.options = $('option', This.target);
	This.elements = {
		select: $('<div class="select"></div>'),
		value: $('<div class="value"></div>'),
		options: $('<div class="options"></div>')
	};
	This.opened = false;
	This.open = function(){
		$('div.select').css('z-index', 0);
		This.elements.select.css('z-index', 1);
		$('.option', This.elements.options)
			.removeClass('selected')
			.eq(This.selectedIndex()).addClass('selected');
		This.opened = true;
		This.elements.options.show();
	};
	This.close = function(){
		This.opened = false;
		This.elements.options.hide();
	};
	This.selectedIndex = function(){
		return This.target.get(0).selectedIndex;
	};
	This.selectclick = function(){
		if(This.opened) This.close();
		else This.open();
		return false;
	};
	This.optionclick = function(event){
		var change = $(event.target).data('index') != This.selectedIndex();
		This.options.eq($(event.target).data('index')).attr('selected', 'selected');
		This.text();
		if(change) This.onchange();
	};
	This.optionmouseover = function(event){
		$('.option', This.elements.options).removeClass('selected');
		$(event.target).addClass('selected');
	};
	This.onchange = function(event){
		if(typeof This.target.get(0).onchange == 'function'){
			This.target.get(0).onchange(This);
		}
	};
	This.value = function(){
		return This.target.val();
	};
	This.text = function(){
		This.elements.value.text(This.options.eq(This.selectedIndex()).text());
	};
	This.target.css({position: 'absolute', visibility: 'hidden', left: 0, top: 0});
	This.elements.select.addClass(This.target.attr('class').replace(/\s+/g, ', '))
		.css('width', This.target.css('width'))
		.append(This.elements.value, This.elements.options)
		.click(This.selectclick);
	$(document).click(This.close);
	This.elements.value.css('width', This.target.css('width'));
	var i, newOption, option;
	for(i = 0; i < This.options.length; i++){
		option = This.options.eq(i);
		newOption = $('<div class="option"></div>')
			.text(option.text())
			.data('index', i)
			.click(This.optionclick)
			.mouseover(This.optionmouseover);
		This.elements.options.append(newOption);
	}
	var minW = $('<div class="c"></div>');
	minW.css('width', This.elements.select.innerWidth()+38);//8+30 padding
	This.elements.options.append(minW);
	This.text();
	This.target.before(This.elements.select);
	var div_select = $('div.select', This.target.parent());
	if(div_select.length > 1) div_select.each(function(i){$(this).css('z-index', div_select.length-i);});
}//StylizedSelect






var SliderVars = {};
function SliderAnimateCallback(){
	SliderVars.status++;
	if(SliderVars.status == SliderVars.length){
		SliderVars.slides.filter(':lt('+SliderVars.current+')').css('visibility', 'hidden');
		SliderVars.slides.filter(':gt('+SliderVars.current+')').css('visibility', 'hidden');
	}
}//SliderAnimateCallback
function SliderGo(p){
	p = (p + SliderVars.length) % SliderVars.length;
	SliderVars.nav.removeClass('active').filter(':eq('+p+')').addClass('active');
	if(SliderVars.status == SliderVars.length && p != SliderVars.current){
		SliderVars.previous = SliderVars.current;
		SliderVars.current = p;
		var count = Math.abs(SliderVars.current - SliderVars.previous);
		count = Math.min(count, SliderVars.length - count);
		var direction = (SliderVars.previous + count) % SliderVars.length == SliderVars.current ? 1 : -1;
		SliderVars.status = SliderVars.length - count - 1;
		for(var j = 0; j <= count; j++){
			var left = SliderVars.width*j*direction;
			SliderVars.slides.eq((SliderVars.length + SliderVars.previous + j*direction) % SliderVars.length)
			.css({left: left}).css('visibility', 'visible')
			.animate({left: left-direction*count*SliderVars.width}, SliderAnimateCallback);
		}
	}
	return false;
}//SliderGo
function Slider(container, nav){$(function(){
	SliderVars.container = $(container);
	SliderVars.items_container = $('.items', SliderVars.container);
	SliderVars.nav = $('.item', $(nav));
	SliderVars.width = SliderVars.items_container.width();
	SliderVars.current = 0;
	SliderVars.previous = 0;
	SliderVars.slides = $('.item', SliderVars.items_container);
	SliderVars.length = SliderVars.slides.length;
	if(SliderVars.length <= 1) return;
	SliderVars.items_container.css({overflow: 'hidden'});
	SliderVars.slides.css('position', 'absolute').each(function(i,e){e=$(e);e.css({top: 0, left: SliderVars.width*i});});
	SliderVars.status = SliderVars.length;
	$('.prev', SliderVars.container).click(function(){
		return SliderGo(SliderVars.current-1);
	});
	$('.next', SliderVars.container).click(function(){
		return SliderGo(SliderVars.current+1);
	});
	SliderVars.nav.each(function(i,e){$(e).click(function(){SliderGo(i);});});
	SliderGo(0);
});}//Slider


var VideoSliderVars = {position: 0};
function VideoSliderGo(direction){
	var direction = direction == 'next' ? 1 : -1;
	var position = (VideoSliderVars.length + VideoSliderVars.position + direction) % VideoSliderVars.length;
	if(position > VideoSliderVars.length - VideoSliderVars.width/VideoSliderVars.slide_width) return;
	VideoSliderVars.position = position;
	VideoSliderVars.slides.each(function(i,e){e=$(e);
		e.css({left: VideoSliderVars.slide_width*(i-VideoSliderVars.position)});
	});
	return false;
}//VideoSliderGo
function VideoSlider(container){$(function(){
	VideoSliderVars.container = $(container);
	VideoSliderVars.items_container = $('.items', VideoSliderVars.container);
	VideoSliderVars.width = VideoSliderVars.items_container.width();
	VideoSliderVars.slides = $('.item', VideoSliderVars.items_container);
	VideoSliderVars.length = VideoSliderVars.slides.length;
	if(VideoSliderVars.length == 0) return;
	VideoSliderVars.slide_width = VideoSliderVars.slides.outerWidth();
	if(VideoSliderVars.slide_width*VideoSliderVars.length <= VideoSliderVars.width) return;
	VideoSliderVars.slides.css('position', 'absolute').each(function(i,e){e=$(e);e.css({top: 0, left: VideoSliderVars.slide_width*i});});
	$('.prev, .next', VideoSliderVars.container).css('visibility', 'visible').click(function(){
		VideoSliderGo(this.className);
		return false;
	});
});}//VideoSlider

function videoPlayer(code){
	var conteiner = $('#video_player');
	code = $('<div>'+code+'</div>');
	var o = $('object[classid^="clsid"]', code);
	var flashvars = $('param[name=flashvars]', code).attr('value');
	if(o.length) o.get(0).wmode = 'transparent';
	$('object[type="application/x-shockwave-flash"], embed[type="application/x-shockwave-flash"]', code).attr('wmode', 'transparent');
	conteiner.html(code.html());
	var o = $('object[classid^="clsid"]', conteiner);
	if(flashvars && o.length) o.get(0).flashvars = flashvars;
}//video_player

function homepageMedia(i){
	$('table.homepageMedia').hide().eq(i-1).show();
	$('a.homepageMedia').removeClass('active').eq(i-1).addClass('active');
}//homepageMedia

function homepageMediaPlayer(code){
	(new PopupOverlay()).html(code);
}//homepageMediaPlayer


/*
 * NEWS_TICKERS
 */
var newsTickers;
var newsTickerCurrent = 0;
var newsTickerTimeout;
var newsTickerWidth;
function newsTickersInit(){
	newsTickerWidth = $('#nt_items').innerWidth();
	newsTickers = $('#news_ticker .nt_item');
	newsTickers.css({position: 'absolute'});
	newsTickers.each(function(i,e){e=$(e);
		e.data('scrollWidth', e.outerWidth() - newsTickerWidth);
	});
	hideNewsTickers();
	currentNewsTicker().show().css({opacity: 1});
	scrollNewsTicker();
}
function currentNewsTicker(){
	return newsTickers.filter(':eq('+newsTickerCurrent+')');
}
function setNewsTickerTimeout(){
	if(newsTickerTimeout) clearTimeout(newsTickerTimeout);
	newsTickerTimeout = setTimeout(nextNewsTicker, 3000);
}
var hideNewsTickerCount = 0;
function hideNewsTicker(){
	hideNewsTickerCount = 0;
	newsTickers.filter(':not(:eq('+newsTickerCurrent+'))').animate({opacity: 0}, 600, function(){
		hideNewsTickerCount++;
		if(hideNewsTickerCount == newsTickers.length - 1) showNewsTicker();
	});
}
function hideNewsTickers(){
	newsTickers.hide().css({opacity: 0, position: 'absolute', left: 0});
}
function showNewsTicker(){
	hideNewsTickers();
	currentNewsTicker().show().animate({opacity: 1}, 600, scrollNewsTicker);
}
function scrollNewsTicker(){
	var l = currentNewsTicker().data('scrollWidth');
	if(l > 0) currentNewsTicker().animate({left: -l}, 4000, setNewsTickerTimeout);
	else setNewsTickerTimeout();
}
function nextNewsTicker(){
	newsTickerCurrent = (newsTickerCurrent + 1) % newsTickers.length;
	hideNewsTicker();
}
$(newsTickersInit);
/*
 * NEWS_TICKERS
 */




