/*
 * www.huddle.net Javascript
 * (c) Huddle
 */

// Contact Forum Popups
var contactPopups = {

	init : function() {

		$('#tell-friend p input, #arrange-demo-box a, #promo-form p input').hover(
			function(){
				$('#tell-friend p input + span, #arrange-demo p input + span').addClass('hover');
			},
			function(){
				$('#tell-friend p input + span, #arrange-demo p input + span').removeClass('hover');
			}
		);

		$('#tf-link').bind('click', function(e) {
			e.preventDefault();
			e.stopPropagation();
			$('#tell-friend').toggle();
		});

		$('#demo-link').bind('click', function(e) {
			e.preventDefault();
			e.stopPropagation();
			$('#arrange-demo').toggle();
		});

		$('#promo-link').bind('click', function(e) {
			e.preventDefault();
			e.stopPropagation();
			$('#promo-form').toggle();
		});

		$('#arrange-demo, #promo-form, #tell-friend').hover(
			function(){
				$(this).show();
			},
			function(){
				$(this).hide();
			}
		);
	}

};

// Homepage Functions
var homePage = {

	init : function() {
		if ($('#news').length > 0) {
			//newsticker init
			$("#news").newsticker(8000);
		}

		if ($('#l_signin').length > 0) {
			//handle sign in input hover
			$('#l_signin').hover(
				function(){
					$('#l_signin + span').addClass('hover');
				},
				function(){
					$('#l_signin + span').removeClass('hover');
				}
			);
		}
	}
};

// Submenu dropdowns
var subMenus = {
	init : function() {
		$('.submenu-dd').hide();
		$('.submenu-toggle').bind('click', function(e) {
			$(this).next('.submenu-dd').slideToggle('normal');
		});
	}
};

// Tooltips
var toolTips = {
	init : function() {
		//tooltip init
		$('div.tooltip').hide();

		//handle tooltip hover
		$('span.trigger').hover(function(){
			$(this).css({ cursor:"pointer" });
			$(this).siblings('div').show();
		}, function(){
			$(this).siblings('div').hide();
		});

		$('.tooltip').hover(function(){
			$(this).show();
		}, function(){
			$(this).hide();
		});
	}
};

// Product Page Point Picker
var pointPickers = {

	init : function() {
        var points = $('div.bubble');
        var lists = $('div.bubble ul');
        var i = 0;
        var showList = function(el) {
            lists.hide();
            $('div.bubble h2').removeClass('selected');
            el.parent().children('ul').fadeIn(500);
            el.addClass('selected');
        };

        points.each(function() {
            $(this).children('h2').click(function() {
            }).mouseover(function() {
                showList($(this));
                $(this).addClass('hover');
            }).mouseout(function() {
                $(this).removeClass('hover');
            });
        });
    }
};

// Enterprise page slider
var slider = {
    init : function() {
        $('#slides > ul').cycle({
            next: '#slides .button-next',
            prev: '#slides .button-prev',
            timeout: 0,
            speed: 800,
            nowrap: true,
            prevNextClick: this.setButtonState
        });
    },

    setButtonState : function(isNext, zeroBasedSlideIndex, slideElement) {
        if($(slideElement).next().length) {
            $(this.next).removeClass('disabled');
        } else {
            $(this.next).addClass('disabled');
        }

        if($(slideElement).prev().length) {
            $(this.prev).removeClass('disabled');
        } else {
            $(this.prev).addClass('disabled');
        }
    }
};

$(document).ready(function() {
    $('body').addClass('js');

	if ($('#home').length > 0) { homePage.init(); }
	if ($('.submenu-dd').length > 0) { subMenus.init(); }
	if ($('#content.pricing, td span.gbp').length > 0) { pricingPage.init(); }
	if ($('.point-picker').length > 0) { pointPickers.init(); }
	if ($('#slides').length > 0) { slider.init(); }
	if ($('#tour').length > 0) { tourSlideshow.init(); }
	if ($('#home-slideshow').length > 0) { homeSlideshow.init(); }
	
	if($('#request-demo').length > 0) {
		var context = $('#request-demo');
		$('input.required', context).focus(function(e) {
			if($(this).val() == 'required') {
				$(this).val('').addClass('not-empty');
			}
		});
		
		$('input.required', context).blur(function(e) {
			if($(this).val() == '') {
				$(this).val('required').removeClass('not-empty');
			}
		});	 
	}
	
	toolTips.init();
	contactPopups.init();
});

// ****************************************************************************
// Plugins
// ****************************************************************************

/*
 *
 * Copyright (c) 2006/2007 Sam Collett (http://www.texotela.co.uk)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Version 2.0
 * Demo: http://www.texotela.co.uk/code/jquery/newsticker/
 *
 * $LastChangedDate$
 * $Rev$
 *
 */

(function($) {
/*
 * A basic news ticker.
 *
 * @name     newsticker (or newsTicker)
 * @param    delay      Delay (in milliseconds) between iterations. Default 4 seconds (4000ms)
 * @author   Sam Collett (http://www.texotela.co.uk)
 * @example  $("#news").newsticker(); // or $("#news").newsTicker(5000);
 *
 */
$.fn.newsTicker = $.fn.newsticker = function(delay)
{

	delay = delay || 5000;
	initTicker = function(el)
	{
		stopTicker(el);
		el.items = $("tr", el);
		// hide all items (except first one)
		el.items.not(":eq(0)").hide().end();
		// current item
		el.currentitem = 0;
		startTicker(el);
	};
	startTicker = function(el)
	{
		el.tickfn = setInterval(function() { doTick(el) }, delay)
	};
	stopTicker = function(el)
	{
		clearInterval(el.tickfn);
	};
	pauseTicker = function(el)
	{
		el.pause = true;
	};
	resumeTicker = function(el)
	{
		el.pause = false;
	};
	doTick = function(el)
	{
		// don't run if paused
		if(el.pause) return;
		// pause until animation has finished
		el.pause = true;
		// hide current item
		$(el.items[el.currentitem]).fadeOut("slow",
			function()
			{
				$(this).hide();
				// move to next item and show
				el.currentitem = ++el.currentitem % (el.items.size());
				$(el.items[el.currentitem]).fadeIn("slow",
					function()
					{
						el.pause = false;
					}
				);
			}
		);
	};
	this.each(
		function()
		{
			if(this.nodeName.toLowerCase()!= "table") return;
			initTicker(this);
		}
	)
	.addClass("newsticker")
	.hover(
		function()
		{
			// pause if hovered over
			pauseTicker(this);
		},
		function()
		{
			// resume when not hovered over
			resumeTicker(this);
		}
	);
	return this;
};

})(jQuery);
