window.marketo = window.marketo || {};

// Misc functions, utilities
marketo = {
	log: function(){
		this.history = this.history || [];
		this.history.push(arguments);
		if(window.console){
			console.log( Array.prototype.slice.call(arguments) );
		}
	}
};

// Run on $.ready()
marketo.init = {
	foucFix: function () {
		$('.fouc-fix').removeClass('fouc-fix');
	},
	labels: function () {
		$('label').inFieldLabels();
	},
	vAlign: function () {
		$('.home .button-play').vAlign();
	},
	dialog: function () {
		$('.video-full').hide();
		$('.icon-large-video').click(function(){
			$('.video-group').dialog(
				{
					dialogClass: 'shadow-light',
					width: '926',
					title: 'Customer<br /><span class="pull-back">Testimonials</span>',
					closeText: 'X'
				}
			);
		});
	},
	qtip: function () {
		// Defaults for homepage icon popovers
		$.fn.qtip.styles.theme1 = {
			width: {'max': '500'},
			padding: '0  35px 15px',
			color: '#ffffff'
		};
		popoverDefaults =
		{
			position: {
			      corner: {
			         target: 'rightMiddle',
			         tooltip: 'center'
			      },
				adjust: {
					x: -50,
					y: 0
				}
			},
			style: {
				name: 'theme1',
				background: '#5a53a3',
				opacity: .95,
				title: {
					background: '#5a53a3',
					opacity: .95
				}
			},
			show: {
				delay: 0,
				when: {event: 'click'}
			},
			hide: 'unfocus'
		};
		$('.icon-large-revenue').qtip($.extend(popoverDefaults,
			{
				content: {
					text: 'Grow revenue faster by expanding, lead flow, optimizing sales and marketing investments and improving productivity with Revenue Performance Management (RPM). Combining marketing automation, lead nurturing and more, it’s a must-have solution with amazing bottom-line impact.<p class="more"><a href="http://www.marketo.com/about/revenue-performance-management/revenue-quotient.php">Learn More</a></p>',
					title: {
						text:'Achieve <br /><span class="pull-back">Success!</span>',
						button: ''
					}
				}
			})
		);
		$('.icon-large-quotes').qtip($.extend(popoverDefaults,
			{
				position: {
				      corner: {
				         target: 'topLeft',
				         tooltip: 'topRight'
				      },
					adjust: {
						x: 0,
						y: 25
					}
				},
				content: {
					text: '<div class="clearfix"><img class="left" style="margin-right: 10px;" src="_media/thumb_quote_ben_lamorte.png" width="85" height="115" alt="Thumb Quote Ben Lamorte">With Marketo, we now generate five times the number of leads at half the cost.”<br />BEN LAMORTE<br />Vice President of Sales<br />Alight</div><p class="more"><a href="/customers/success-snapshots.php">More Quotes</a></p>',
					title: {
						text:'Customer <br /><span class="pull-back">Quotes</span>',
						button: ''
					}
				}
			})
		);
		$('.icon-large-guide').qtip($.extend(popoverDefaults,
			{
				position: {
				      corner: {
				         target: 'rightMiddle',
				         tooltip: 'center'
				      },
					adjust: {
						x: -100,
						y: 0
					}
				},
				style: {
					name: 'theme1',
					background: '#00a5d9',
					opacity: .95,
					title: {
						background: '#00a5d9',
						opacity: .95
					}
				},
				content: {
					text: '<div class="clearfix"><img class="left" style="margin-right: 10px;" src="/_media/thumb_definitive_guide.png" width="85" height="115" alt="Thumb Definitive Guide">This groundbreaking 70 page workbook will teach you how to proactively speak the CxO language of forecasting and reporting. Earn marketing a place at the revenue table by using solid metrics that work.</div><p class="more"><a href="/b2b-marketing-resources/best-practices/the-definitive-guide-to-marketing-metrics-and-marketing-analytics.php">Learn More</a></p>',
					title: {
						text:'Speak the<br /><span class="pull-back">language!</span>',
						button: ''
					}
				}
			})
		);
		$('.icon-large-news').qtip($.extend(popoverDefaults,
			{
				position: {
				      corner: {
				         target: 'topMiddle',
				         tooltip: 'bottomMiddle'
				      },
					adjust: {
						x: 0,
						y: 0
					}
				},
				style: {
					name: 'theme1',
					background: '#00a5d9',
					opacity: .95,
					title: {
						background: '#00a5d9',
						opacity: .95
					}
				},
				content: {
					text: '<strong>Marketo Named CMI Magazine <br />Marketing Solution Winner</strong> <p class="no-margin">"Hot Darling" of marketing takes top place in CMR Market Leaders Awards 2001.</p> <strong>Marketo Announces Titanium <br />Sponsorship of Dream Force 2011</strong> <p class="no-margin">Marketo announced it will be a titanium sponsor of salesforce.com\'s Dreamforce 2011 conference, the world\'s largest cloud computing event.</p><p class="more"><a href="#">More News</a></p>',
					title: {
						text:'Marketo<br /><span class="pull-back">News</span>',
						button: ''
					}
				}
			})
		);

		// Defaults for social media popovers
		$.fn.qtip.styles.theme2 = {
			width: {'max': '500'},
			padding: '0  35px 15px',
			color: '#58595b',
			background: '#ffffff',
			title: {
				background: '#ffffff',
				color: '#58595b'
			}
		};
		popoverWhite =
		{
			style: {
				name: 'theme2',
				opacity: 1
			},
			show: 'click',
			solo: true,
			hide: 'unfocus',
			// api call to onShow to fix bugs in plugin (not changing opacity and not hiding other showing tooltips)
			api: {
				onShow: function() {
					var that = this.id;
					$(this).css({opacity: 1});
					$('.qtip').each(function() {
						if($(this).attr('qtip') != that) {
							$(this).qtip('hide');
						} else {
							$(this).css({opacity: 1});
						}
					});
				}
			}
		};
		$('.sidebar .facebook').qtip($.extend(popoverWhite,
			{
				content: {
					text: $('.follow-facebook'),
					title: {
						text:'<span class="pull-back">Like</span><br />Marketo',
						button: ''
					}
				},
				position: {
				      corner: {
				         target: 'leftMiddle',
				         tooltip: 'rightMiddle'
				      },
				      adjust: {
						x: -20,
						y: -40
				      }
				}
			})
		);
		$('.sidebar .twitter').qtip($.extend(popoverWhite,
			{
				content: {
					text: $('.follow-twitter'),
					title: {
						text:'<span class="pull-back">Follow</span><br />Marketo',
						button: ''
					}
				},
				position: {
				      corner: {
				         target: 'leftMiddle',
				         tooltip: 'rightMiddle'
				      },
				      adjust: {
						x: -58,
						y: -40
				      }
				}
			})
		);
		$('.sidebar .linkedin').qtip($.extend(popoverWhite,
			{
				content: {
					text: $('.follow-linkedin'),
					title: {
						text:'<span class="pull-back">Follow</span><br />Marketo',
						button: ''
					}
				},
				position: {
				      corner: {
				         target: 'leftMiddle',
				         tooltip: 'rightMiddle'
				      },
				      adjust: {
						x: -96,
						y: -40
				      }
				}
			})
		);
		$('.sidebar .googleplus').qtip($.extend(popoverWhite,
			{
				content: {
					text: $('.follow-googleplus'),
					title: {
						text:'<span class="pull-back">+1</span><br />Marketo',
						button: ''
					}
				},
				position: {
				      corner: {
				         target: 'leftMiddle',
				         tooltip: 'rightMiddle'
				      },
				      adjust: {
						x: -134,
						y: -40
				      }
				}
			})
		);
	},
	tabs: function () {
		$('.tabs').tabs();
		$('.footer-tabs').tabs({
			selected: -1,
			fx: { opacity: 'toggle' }
		});
	},
	lightbox: function () {
		$(".lightbox").colorbox();
		$(".lightbox-presentation").colorbox({
			iframe: true,
			innerWidth:560,
			innerHeight:370
		});
	},
	videos: function () {
		// Video thumbnails
		$('a.video').click(function(){
			// Grab video ID and generate youtube url
			var videoID = $(this).attr('id');
			var url = "http://www.youtube.com/embed/" + videoID;
			$('.video-full iframe').attr('src',url);
			// Toggle between thumbnails and full video
			$('.thumbnails').hide('fast', function(){
				$('.video-full').show('fast', function(){
					$('a.return').click(function(){
						$('.video-full').hide('fast');
						$('.thumbnails').show('fast');
					});
				});
			});
		});
	},
	stripes: function () {
		$("table.stripes tr:even").addClass("even");
	},
	main_nav: function () {
		var nav_open_check = false;
		// If homepage, push absolutely positioned content down by same amount
		if ($('body').hasClass('home')) {
			var subnavHeight = '406px';
		}
		$('.main-nav a').click(function(event){
			event.preventDefault();
			$('.ui-dialog').hide(); // Close video popovers if they're up

			var $this = $(this);
			var content = $('.nav-content.' + $this.attr('id'));
			var sidebar = $('.nav-content.' + $this.attr('id') + ' .sidebar');
			var links = $('.nav-content.' + $this.attr('id') + ' .main-nav-links');


			if ($('body').hasClass('nav-open') && !$this.hasClass('open')) {
				$('.nav-content').addClass('hide').removeAttr('style');
				content.removeClass('hide');
				links.show('slide', {direction: 'right'}, 400);
				sidebar.removeClass('hide');
			} else {
				if ($this.hasClass('open')) {
					content.slideUp('slow');
					$('body').removeClass('nav-open');
					if ($('body').hasClass('home')) {
						$('#parts').animate({top:'-='+subnavHeight},'slow');
						$('#footer').animate({bottom:'+='+subnavHeight},'slow');
						$('body').css('padding-bottom', '-='+subnavHeight);
					}
					nav_open_check = false;
				}
				else {
					nav_open_check = true;
					content.slideDown('slow');
					content.removeClass('hide');
					sidebar.removeClass('hide');
					$('body').addClass('nav-open');
					if ($('body').hasClass('home')) {
						$('#parts').animate({top:'+='+subnavHeight},'slow');
						$('#footer').animate({bottom:'-='+subnavHeight},'slow');
						$('body').css('padding-bottom', '+='+subnavHeight);
					}
				}
			}

			if ($this.hasClass('open')) {
				$this.removeClass('open');
			} else {
				$this.addClass('open');
				$('.open').not(this).removeClass('open');
			}
		});
		
		var mouse_inside_dropdown = false;
		
		$('body').mouseup(function() {
			if(! mouse_inside_dropdown && nav_open_check) {
				navClose(subnavHeight);
				nav_open_check = false;
			}
		});
		var timer;
		$('.main-nav li a').hover(function() {
			mouse_inside_dropdown = true;
			clearTimeout(timer);
		}, function() {
			mouse_inside_dropdown = false;
			if(nav_open_check) {
				timer = setTimeout(function() {
					navClose(subnavHeight);
					nav_open_check = false;
				}, 1000);
			}
		});
		$('.nav-content-container').hover(function() {
			mouse_inside_dropdown = true;
			clearTimeout(timer);
		}, function() {
			mouse_inside_dropdown = false;
			if(nav_open_check) {
				timer = setTimeout(function() {
					navClose(subnavHeight);
					nav_open_check = false;
				}, 1000);
			}
		});

		$('.nav-close').click(function(){
			navClose(subnavHeight);
			nav_open_check = false;
		});
	},
	accordion: function () {
		$('.accordion').accordion({active: 0});
	},
	categoryList: function () {
		$('.category-list').bind('change', function() {
			var selectedItem = $(this).val();
			if (selectedItem && selectedItem != '-1') {
				location.href = 'http://' + window.location.hostname + '/?cat=' + selectedItem;
			}
		});
	},
	blogSubscribe: function () {
		$('.js-blog-subscribe').on('change', function () {
			var subscribers = $('option:selected', this).data('subscribers');
			$('.js-subscriber-count').text(subscribers);
		});
	},
	share_count: function() {
		$('.js-share').each(function () {
			var url = $(this).data('share-url');
			var parent = $(this);
			var loc = window.location;

			var host;
			switch ( loc.hostname ) {
				case "blog.marketo":
				case "redesign.blog.marketo.com.dev":
					host = 'http://redesign.marketo.com.dev';
					break;
				case "blog.marketo.flickerbox.com":
				case "redesign.blog.marketo.flickerbox.com":
					host = 'http://redesign.marketo.flickerbox.com';
					break;
				case "beta.blog.marketo.com":
					host = 'http://beta.marketo.com';
					break;
				case "new.blog.marketo.com":
					host = 'http://new.www.marketo.com';
					break;
				default:
					host = 'http://www.marketo.com';
			}

			$.getJSON(host + '/api/share/count?url=' + encodeURIComponent(url) + '&callback=?', function (data) {
				$.each(data, function (key, value) {
					$('.js-share-' + key, parent).text(value);
				})
			})
		});
	},
	promo_display: function() {
		var promos = $('.resource-promo');
		//var promos = $(promos);
		var r      = Math.floor(Math.random() * promos.length);
		promos.slice(r, r+1).removeClass('hidden');
	},
	change_home_arcives: function() {
		if($('.home').length > 0) {$('#cat option[value="-1"]').text('By Category');}
	}
};

$(document).ready(function() {
	$.each(marketo.init,
		function(i,item) {
			item();
		}
	);
});

function navClose(subnavHeight) {
	var id = $('.open').attr('id');
	$('body').removeClass('nav-open');
	$('.open').removeClass('open');
	$('.nav-content.'+id).slideUp('slow');
	if ($('body').hasClass('home')) {
		$('#parts').animate({top:'-='+subnavHeight},'slow');
		$('#footer').animate({bottom:'+='+subnavHeight},'slow');
		$('body').css('padding-bottom','-='+subnavHeight);
	}
	return false;
}

