/**
 * EV Main JavaScript
 *
 */

// establish EV namespace
var $EV = window.$EV || {};

/*
 * generalInit(): general EV website initialization
 *
 */
$EV.generalInit = function() {

	// toggle open and close of the log in dropdown menu
	jQuery("body").bind("click", function (e) {
		jQuery('.dropdown-toggle, .menu').parent("li").removeClass("open");
	});
	// Prevent clicks on the login block from closing the hover
	jQuery(".login-block, .dropdown-toggle").click(function(e) {
		e.stopPropagation();
		return false;
	});
	jQuery(".dropdown-toggle, .menu").hover(function (e) {
		jQuery(this).parent("li").addClass('open');
		jQuery("input.username").focus();
		return false;
	}); 
	// show tips in various places using standard bottom center and mostly used for images
	jQuery("[rel=tip]").tooltip({opacity: 0.8, delay: 200});
	// show tips in various places using top center placement and mostly used for text
	jQuery("[rel=tip-features]").tooltip({position: "top center", opacity: 0.9, delay: 0}); 
	// get topbars and set z-index after page load
	jQuery(".topbar").css('z-index', '800');
	jQuery(".topbar2").css('z-index', '700');
	// detach login dropdown menu from the DOM and set its z-index
	var loginDropDownMenu = jQuery(".menu-dropdown.login").css('z-index', '999');
	jQuery(loginDropDownMenu).detach();
	// append login dropdown menu back in the DOM in the correct place
	jQuery(".menu.dropdown.login").append(loginDropDownMenu);	
	// close dropdown after leaving the menu
	jQuery(".menu-dropdown, .features-menu").mouseleave(function() {
		console.log('out');
		var elem = jQuery(this);
		$EV.menuTimeout = setTimeout(function(){
			elem.parent("li").removeClass('open');
		},
		100
		)
	});
	jQuery(".menu-dropdown, .dropdown-toggle").mouseover(function() {
		clearTimeout($EV.menuTimeout);
	});
	// Select log in tab and update log in form
	jQuery(".login-tab").click(function() {
		jQuery(".login-tab").removeClass('active');	
		jQuery("input.username").focus();
	});
	jQuery(".login-tab.swft").click(function() {
		jQuery(this).addClass('active');
		jQuery(".toggle-label").html('Username');
		jQuery("#login-form").attr('action', swftBaseUrl + '/login/submit');	
	});
	jQuery(".login-tab.client-area").click(function() {
		jQuery(this).addClass('active');
		jQuery(".toggle-label").html('Email address');
		jQuery("#login-form").attr('action', whmcsBaseUrl + '/dologin.php');	
	});
	// Submit log in form
	jQuery(".login").click(function() {
		jQuery("#login-form").submit();
	});
	// navigate to feature on click of feature block
	jQuery(".alternate-text-img-grid").click(function() {
		var location = jQuery(this).attr('data');
		window.location = location;
	});
	
	// Because of the header bars, we need to adjust the scroll position on
	// anchor clicks
	jQuery("a").click(function(e) {
		var href = jQuery(this).attr("href");
		if (href != undefined && href.indexOf("#") > -1 && href.length > 1) {
			if ($EV.adjustScrollPos(href.substring(1))) {
				e.preventDefault();
			}
		}
	});
	
	// Adjust scroll on page load for anchors
	var url = window.location.toString();
	if (url.indexOf("#") > -1 && url.length > 1) {
		var href = url.substring(url.indexOf("#") + 1);
		// This is ugly but it works. We have to wait just a bit until after the page loads
		// so that the scroll will work correctly, otherwise this is fired before the default
		// page scroll happens
		setTimeout(function() {$EV.adjustScrollPos(href);}, 50);
	}
	
	// This code is called whenever the URL changes (when the back
	// button is hit, for example). It is used to maintain back 
	// history with SWFT tour
	jQuery.address.change(function(event) {
		var page = event.path.substring(1);
		if (page != "") {
			$EV.updateSelectedTour(page);
		} else {
			// If we didn't get a tour page ID and the tour is open, close it
			if ($EV.modalOpen) {
			    jQuery(".tour .close").click();
			}
		}
	});

}

/**	
 * adjustScrollPos(): Readjusts the scroll position to accomodate the header
 */
$EV.adjustScrollPos = function(name) {
	var anchor = jQuery("a[name='" + name + "']");
	if (anchor.length > 0) {
		var anchorPos = jQuery(anchor).offset().top;
		jQuery(document).scrollTop(anchorPos - 120);
		return true;
	} else {
		return false;
	}
}

/*
 * tourInit(): initialize the product tour
 *
 */
$EV.tourInit = function() {

	$EV.modalOpen = false;

	// the product tour
	jQuery(".tour-trigger").overlay({

		// the overlay mask
		mask: {
			color: '#000',
			loadSpeed: 100,
			opacity: .5
		},
		left: "2%",
		top: "2%",
		bottom: "2%",
		fixed: true,
		onLoad: function(e) {
			$EV.modalOpen = true;
			var windowHeight = jQuery(window).height();
			jQuery("html").css({height: windowHeight, overflow: 'hidden' })
		},
		onClose: function(e) {
			$EV.modalOpen = false;
			jQuery("html").css({ height: 'auto', overflow: 'auto' })
		}
	});
	
	jQuery(window).resize(function(e) {
		if ($EV.modalOpen) {
			var windowHeight = jQuery(window).height();
			jQuery("html").css({height: windowHeight});
		}
	});
	
	// Position all of the tooltip dots
	jQuery(".tour-product-image").hide();
	jQuery(".tour-product-image").first().show();
	
	jQuery(".dot-radius").each(function() {
		var posX = jQuery(this).attr("x");
		var posY = jQuery(this).attr("y");
		jQuery(this).css({'top': posY + "px", 'left': posX + "px"});
	})
	
	jQuery(".dot-radius").mouseover(function() {
		var message = jQuery(this).attr("data");
		jQuery('.info-details').html('<strong>' + 'Tip: ' + '</strong>' + message);
	});
	jQuery(".dot-radius").mouseout(function() {
		var defaultText = jQuery(".tour-product-image:visible .default-text").html();
		jQuery(".info-details").html(defaultText);
	});
	
	// Go back one screen when the previous button is clicked. If at the beginning, loop to the end.
	jQuery(".tour-nav .previous").click(function(e) {
		e.preventDefault();
		var prevId = jQuery(".tour-product-image:visible").prev().attr("id");
		
		jQuery(".tour-product-image").hide();
		if (prevId == undefined) {
			prevId = jQuery(".tour-product-image").last().attr("id");
		}
		// When the path is changed, the tour will load the new path ID because of the .address plugin
		jQuery.address.path(prevId);
	});
	
	// Go forward one screen when the next button is clicked. If at the end, loop to the beginning.
	jQuery(".tour-nav .next").click(function(e) {
		e.preventDefault();
		var nextId = jQuery(".tour-product-image:visible").next().attr("id");
		
		if (nextId == undefined) {
			nextId = jQuery(".tour-product-image").first().attr("id");
		}
		jQuery.address.path(nextId);
	});
	
	// Click event for the tour selection buttons
	jQuery(".tour-nav .item").click(function(e) {
		e.preventDefault();
		var clickId = jQuery(this).attr("data");
		jQuery.address.path(clickId);
	})
	
	// Click event on the small screenshots on the homepage
	jQuery(".tour-trigger").click(function(e) {
		var clickId = jQuery(this).attr("data");
		jQuery.address.path(clickId);
	})
	
	// Force modal to close on page wrap click
	jQuery('.page-wrap').click(function() {
		if ($EV.modalOpen) {
		    jQuery(".tour .close").click();
		}
	})
}



/**
 * updateSelectedTour(): Called when a new tour image is selected. This updates
 * the interface to show the tour and update controls
 * @param tourId: ID of the selected tour image
 */
$EV.updateSelectedTour = function(tourId) {
	// Hide all images, show the correct one.
	jQuery(".tour-product-image").hide();
	jQuery(".tour-product-image-wrap").scrollTop();
	jQuery("#" + tourId).show();
	
	// Reset the active class on the tour selectors
	jQuery(".tour-nav .item").removeClass("active");
	jQuery(".tour-nav .item[data=" + tourId + "]").addClass("active");
	
	// Replace the default text
	jQuery(".info-details").html(jQuery("#" + tourId + " .default-text").html());
}

jQuery(document).ready( function(){
 	$EV.generalInit();
 	$EV.tourInit();
});

