/*
 * www.Ayojak.com Javascript
 * (c) Ayojak
 */

// 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');
				}
			);
		}
	}
};

// Pricing Page Functions
var pricingPage = {

	init : function() {
		//currency init
		$('.gbp').hide();
		$('.euro').hide();

		var $currency = 'usd';

		//handle currency button clicks
		$('#gbp').click(function(){
			$currency = 'gbp';

			$('.usd').hide();
			$('.euro').hide();
			$('.gbp').show();

			$('#usd').css('background-position', '0 0');
			$('#euro').css('background-position', '0 0');
			$(this).css('background-position', '0 -400px');
		});
		$('#usd').click(function(){
			$currency = 'usd';

			$('.gbp').hide();
			$('.euro').hide();
			$('.usd').show();

			$('#gbp').css('background-position', '0 0');
			$('#euro').css('background-position', '0 0');
			$(this).css('background-position', '0 -400px');
		});
		$('#euro').click(function(){
			$currency = 'euro';

			$('.gbp').hide();
			$('.usd').hide();
			$('.euro').show();

			$('#gbp').css('background-position', '0 0');
			$('#usd').css('background-position', '0 0');
			$(this).css('background-position', '0 -400px');
		});

		//handle currency button hover
		$('#gbp').hover(function(){
			if($currency != 'gbp'){
				$(this).css('background-position', '0 -200px');
				$(this).css({ cursor:"pointer" });
			}
		}, function(){
			if($currency != 'gbp'){
				$(this).css('background-position', '0 0');
			}
		});
		$('#usd').hover(function(){
			if($currency != 'usd'){
				$(this).css('background-position', '0 -200px');
				$(this).css({ cursor:"pointer" });
			}
		}, function(){
			if($currency != 'usd'){
				$(this).css('background-position', '0 0');
			}
		});
		$('#euro').hover(function(){
			if($currency != 'euro'){
				$(this).css('background-position', '0 -200px');
				$(this).css({ cursor:"pointer" });
			}
		}, function(){
			if($currency != 'euro'){
				$(this).css('background-position', '0 0');
			}
		});
	}

};


// 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();
		});
	}
};

// Organize Event Point Picker
var pointPickers01 = {

	init : function() {
        var points = $('div.bubble01');
        var lists = $('div.bubble01 ul');
        var i = 0;
        var showList = function(el) {
            lists.hide();
            $('div.bubble01 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');
            });
        });
    }
};

// Manage Event Point Picker
var pointPickers02 = {

	init : function() {
        var points = $('div.bubble02');
        var lists = $('div.bubble02 ul');
        var i = 0;
        var showList = function(el) {
            lists.hide();
            $('div.bubble02 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');
            });
        });
    }
};

// Promote Event Point Picker
var pointPickers03 = {

	init : function() {
        var points = $('div.bubble03');
        var lists = $('div.bubble03 ul');
        var i = 0;
        var showList = function(el) {
            lists.hide();
            $('div.bubble03 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');
            });
        });
    }
};

// Event Services Point Picker
var pointPickers04 = {

	init : function() {
        var points = $('div.bubble04');
        var lists = $('div.bubble04 ul');
        var i = 0;
        var showList = function(el) {
            lists.hide();
            $('div.bubble04 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');
            });
        });
    }
};

// Event Services Point Picker
var pointPickers05 = {

	init : function() {
        var points = $('div.bubble05');
        var lists = $('div.bubble05 ul');
        var i = 0;
        var showList = function(el) {
            lists.hide();
            $('div.bubble05 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');
            });
        });
    }
};

// Event Services Point Picker
var pointPickers06 = {

	init : function() {
        var points = $('div.bubble06');
        var lists = $('div.bubble06 ul');
        var i = 0;
        var showList = function(el) {
            lists.hide();
            $('div.bubble06 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');
            });
        });
    }
};

// Event Services Point Picker
var pointPickers07 = {

	init : function() {
        var points = $('div.bubble07');
        var lists = $('div.bubble07 ul');
        var i = 0;
        var showList = function(el) {
            lists.hide();
            $('div.bubble07 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 ($('.gbp').length > 0) { pricingPage.init(); }
	if ($('.bubble-row01').length > 0) { pointPickers01.init(); }
	if ($('.bubble-row01').length > 0) { pointPickers02.init(); }
	if ($('.bubble-row01').length > 0) { pointPickers03.init(); }
	if ($('.bubble-row01').length > 0) { pointPickers04.init(); }
	if ($('.bubble-row01').length > 0) { pointPickers05.init(); }
	if ($('.bubble-row01').length > 0) { pointPickers06.init(); }
	if ($('.bubble-row01').length > 0) { pointPickers07.init(); }
    if ($('#slides').length > 0) { slider.init(); }

	toolTips.init();
	contactPopups.init();  
});

// ****************************************************************************
// Plugins
// ****************************************************************************

/*
 *
 * $LastChangedDate$
 * $Rev$
 *
 */

(function($) {
/*
 * A basic news ticker.
 *
 *
 */
$.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);
