﻿/*!
* mcn.js
* ===========================================
* Copyright (c) 2011 Bauer Consumer Media Ltd
* -------------------------------------------
*/

(function ($) {
	var mcn = {
		page: {
			id: 0,
			title: document.title,
			type: 'Page',
			url: document.location.href
		},

		init: function () {
			$('body,#container').click(function (e) {
				mcn.subscribeBox.hide();
				mcn.topNavigation.hidePanels();
			});

			mcn.topNavigation.init();
			mcn.subscribeBox.init();
			mcn.facebook.init();
			mcn.twitter.init();

			$('.jqt_tooltip').tooltip({
				tip: '#jqt_tooltip',
				direction: 'down',
				offset: [125, 0]
			});
			$('.jqt_tooltip2').tooltip({
				tip: '#jqt_tooltip',
				direction: 'down',
				offset: [140, 0]
			});
			$('.bfsIcon_tooltip_trigger').tooltip({
				direction: 'down',
				offset: [125, 70]
			});

			$(document).bind('dragstart', function (e) {
				var o = $(e.target);
				if (o[0].tagName === 'IMG' || o.css('backgroundImage') !== '') { return false; }
			});
		},

		bikesForSale: {
			carouselInit: function (oCarousel) {
				if (oCarousel.list.children().length > 4) {
					oCarousel.container.hover(function () {
						oCarousel.stopAuto();
					}, function () {
						oCarousel.startAuto();
					});
				}
			}
		},

		facebook: {
			init: function () {
				if (window.FB && FB.Event) {
					if ($('.facebookLikeButton').length > 0 && mcn.page.id > 0) {
						FB.Event.subscribe('edge.create', function (response) {
							_gaq.push(['bauertracker._trackEvent', 'Facebook', mcn.page.type + ' - Like', mcn.page.url, mcn.page.id]);
						});
						FB.Event.subscribe('edge.remove', function (response) {
							_gaq.push(['bauertracker._trackEvent', 'Facebook', mcn.page.type + ' - Unlike', mcn.page.url, mcn.page.id]);
						});
					} else if ($('#facebookLikeBox').length > 0) {
						FB.Event.subscribe('edge.create', function (response) {
							_gaq.push(['bauertracker._trackEvent', 'Facebook', 'MCN Facebook Page - Like']);
						});
						FB.Event.subscribe('edge.remove', function (response) {
							_gaq.push(['bauertracker._trackEvent', 'Facebook', 'MCN Facebook Page - Unlike']);
						});
					}
				}
			}
		},

		subscribeBox: {
			init: function () {
				var box = $('#subscribeBox');
				if (box.length > 0) {
					$('#header .latestIssue').click(function (e) {
						e.stopPropagation();
						mcn.subscribeBox.show();
					}).hover(function () {
						$(this).addClass('hover');
						window.setTimeout(function () {
							if ($('#header .latestIssue').hasClass('hover')) {
								mcn.subscribeBox.show();
							}
						}, 500);
					}, function () {
						$(this).removeClass('hover');
					});

					box.click(function (e) {
						e.stopPropagation();
						mcn.subscribeBox.hide();
					}).bind('mouseleave', function () {
						window.setTimeout(mcn.subscribeBox.hide, 500);
					});
				}
			},

			hide: function () {
				var box = $('#subscribeBox');
				if (box.length > 0 && box.is(':visible')) {
					box.animate({
						height: 0,
						padding: 0,
						width: 0
					}, 'slow', function () {
						box.hide();
						$('#header .latestIssue').removeClass('hover');
						$('#container').removeClass('subsBoxActive');
					});
				}
			},

			show: function () {
				var box = $('#subscribeBox');
				if (box.length > 0 && !box.is(':visible')) {
					box.css({
						height: 0,
						padding: 0,
						width: 0
					}).show();
					box.animate({
						height: '500px',
						padding: '10px',
						width: '280px'
					}, 'slow');
					$('#container').addClass('subsBoxActive');
				}
			}
		},

		topNavigation: {
			init: function () {
				$('#topNavigation a.hasMenu').click(function (e) {
					e.preventDefault();
					e.stopPropagation();
					mcn.topNavigation.togglePanel(this, true);
				});
				$('#topNavigation a.hasMenu').hover( function (e) {
					e.stopPropagation();
					clearTimeout(mcn.topNavigation.hoverTimeout);	
					var linkClicked = this;
					mcn.topNavigation.hoverTimeout = setTimeout(function() {					
						mcn.topNavigation.togglePanel(linkClicked, false);							
					}, 250);
				}, function (e) {
					e.stopPropagation();
					clearTimeout(mcn.topNavigation.hoverTimeout);
					mcn.topNavigation.hoverTimeout = setTimeout(function() {
						mcn.topNavigation.hidePanels();
					}, 250);
				});

				$('#topNavigationPanels .navPanel').click(function (e) {
					e.stopPropagation();
				});
				$('#topNavigationPanels .navPanel').hover(
					function (e) {
						e.stopPropagation();
						clearTimeout(mcn.topNavigation.hoverTimeout);
					},
					function (e) {
						e.stopPropagation();
						clearTimeout(mcn.topNavigation.hoverTimeout);
						mcn.topNavigation.hoverTimeout = setTimeout(function() {
							mcn.topNavigation.hidePanels();
						}, 250);
					}
				);
			},

			hidePanels: function () {
				$('#topNavigationPanels .navPanel:visible').slideUp('fast');
				$('#topNavigation .arrow').fadeOut('fast');
				$('#topNavigation a.active').removeClass('active');
				$('#container').removeClass('topNavActive');
			},

			setSelectedPanel: function (panel) {
				panel = $(panel);
				$('#topNavigation ul li:eq(' + panel.index() + ') a').addClass('selected').siblings().removeClass('selected');
			},

			togglePanel: function (panelLink, isClick) {
				panelLink = $(panelLink);
				var panelItem = panelLink.parent();
				var selectedPanel = $('#topNavigationPanels .navPanel:eq(' + panelItem.index() + ')');

				if (selectedPanel.is(':visible')) {
					if (isClick) {
						var url = panelLink.attr('href');
						if (url !== window.location.href) {
							window.location.href = panelLink.attr('href');
						} else {
							selectedPanel.addClass('trans').slideUp('fast', function () {
								selectedPanel.removeClass('trans');
								$('#container').removeClass('topNavActive');
							});
							panelItem.find('.arrow').fadeOut('fast');
							panelLink.removeClass('active');
						}
					}
				} else {
					$('#topNavigation .arrow').hide();
					$('#topNavigation a.active').removeClass('active');
					selectedPanel.siblings(':visible').hide();
					selectedPanel.addClass('trans').slideDown('fast', function () {
						selectedPanel.removeClass('trans');
					});
					panelItem.find('.arrow').fadeIn('fast');
					panelLink.addClass('active');
					$('#container').addClass('topNavActive');
				}
			},

			hoverTimeout: null
		},

		twitter: {
			init: function () {
				var tweets = $('#recentTweets');
				if (tweets.length > 0) {
					tweets.find('.header a').click(function () {
						_gaq.push(['bauertracker._trackEvent', 'Twitter', 'Follow MCN']);
					});
				}
			},
			onLoadRecent: function () {
				var tweets = $('#recentTweets');
				if (tweets.length > 0) {
					tweets.find('.feed .tweet').click(function () {
						var t = $(this);
						_gaq.push(['bauertracker._trackEvent', 'Twitter', 'Recent Tweet - Click', t.find('a.user').text()]);
					});
				}
			},
			tweetClick: function () {
				if (mcn.page.id > 0) {
					_gaq.push(['bauertracker._trackEvent', 'Twitter', mcn.page.type + ' - Tweet', mcn.page.url, mcn.page.id]);
				}
			}
		}
	};
	window.mcn = mcn;
	$(document).ready(window.mcn.init);
})(jQuery);
