// JavaScript Document
function IsPath(path) {
	return php_self.indexOf(path) === 0;
}

jQuery.fn.delay = function(time, callback){
	jQuery.fx.step.delay = function(){};
	return this.animate({delay:1}, time, callback);
}

jQuery.fn.prevInLoop = function(selector) {
	var prev = this.prev(selector);
	return prev.length == 1 ? prev : $(selector + ':last');
}

jQuery.fn.nextInLoop = function(selector) {
	var nxt = this.next(selector);
	return nxt.length == 1 ? nxt : $(selector + ':first');
}

jQuery.fn.rotate = function(options) {
	var p = this;
	$(options.selector).each(function(i, e) {
		$(e).hover(function(ev, params) {
			if (!params) {
				params = [];
				params.bypass = false;
			}
			if (options.pauseOnMouseOver || params.bypass) {
				p.clearQueue().stop();
				if (options.loadbar) options.loadbar.stop(true, true);
			}
		}, function(ev, params) {
			if (!params) {
				params = [];
				params.bypass = false;
			}
			if (options.pauseOnMouseOver || params.bypass) {
				if (options.loadbar && !p.data('nodelay')) {
					options.loadbar
						.css('width', '0')
						.animate({
							width: options.loadbarEndWidth
						}, {
							duration: options.delay,
							easing: 'linear',
							complete: function() {
								$(this).css('width', '0');
							}
						});			
				}
				if (!p.data('prev')) p.data('prev', $(this).prevInLoop(options.selector));
				p.data('current', $(this));
				if (!p.data('next')) p.data('next', $(this).nextInLoop(options.selector));
			
				p.delay((p.data('nodelay')) ? 0 : options.delay, function() {
					p.data('busy', true);															
					p.data('current').css('opacity', '1').animate({
						opacity: '0'
					}, {
						duration: options.duration,
						complete: function() {
							if (options.currentComplete) options.currentComplete();
							$(this).css('zIndex', '0')
						}
					});
					p.data('next').animate({
						opacity: '1'
					}, {
						duration: options.duration,
						complete: function() {
							if (options.nextComplete) options.nextComplete();
							p.data('busy', false);
							p.data('prev', $(this).prevInLoop(options.selector));
							p.data('current', $(this));
							p.data('next', $(this).nextInLoop(options.selector));
							p.data('nodelay', false);
							if (!$.support.opacity) this.style.removeAttribute('filter');
							$(this).css('zIndex', '1');
							if (!p.data('paused')) $(this).trigger('mouseleave', {bypass: !options.pauseOnMouseOver});
						}
					});
				});
			}
		});
	})
	.filter(':first').trigger('mouseleave', {bypass: true});
}

jQuery.fn.ShowTooltip = function(tt) {
	tt.css({
		top: (this.offset().top - tt.height()) + 'px', 
		left: (this.offset().left + (this.width() / 2)) + 'px',
		zIndex: '1000'
	}).removeClass('hidden');
	if ($.support.opacity) {
		tt.stop().animate({
			opacity: '1', 
			top: '+=10'
		}, 'slow');	
	} else {
		tt.stop().animate({
			top: '+=10'
		}, 'slow');	
	}
}

jQuery.fn.HideTooltip = function(tt) {
	tt.css('zIndex', '999');
	if ($.support.opacity) {
		tt.stop(true, true).animate({
			opacity: '0', 
			top: this.offset().top - tt.height() + 'px'
		}, {
			duration: 'fast', 
			complete: function() {
				$(this).addClass('hidden')
			}
		});
	} else {
		tt.stop(true, true).animate({
			top: this.offset().top - tt.height() + 'px'
		}, {
			duration: 'fast', 
			complete: function() {
				$(this).addClass('hidden')
			}
		});	
	}
}

function SlideSlogan() {
	$('#slogan2')
		.wrap('<div id="slogan2_wrapper" />')
		.css({
			position: 'relative', 
			left: $('#slogan2').width() + 200 + 'px'
		})
		.animate({
			left: '0'
		}, {
			duration: 2000, 
			queue: false,
			easing: 'linear'
		});
}

function BuildMenu() {
	$('#topnav > ul > li')
		.bind('focusin mouseover', function() {
			$(this).addClass('over');
		}).bind('focusout mouseout', function() {
			$(this).removeClass('over');
		});
}

var offsets = {'red': -240, 'green': -120, 'blue': 0, 'yellow': -360};
function UpdateTabState(tab) {
	var baseOffset = 0;
	for (var color in offsets) {
		if (tab.parent().hasClass(color)) {
			baseOffset = offsets[color];
			break;
		}
	}
	if (tab.hasClass('tab_closed'))	baseOffset -= 60;
	if (tab.hasClass('tab_hover')) baseOffset -= 30;
	tab.css('backgroundPosition', '0 ' + baseOffset + 'px');
}

jQuery.fn.TogglePanel = function(options) {
	var panelWrapper = this.next();
	var panel = panelWrapper.children('.panel_content');
	
	panelWrapper.stop().animate({height: this.hasClass('tab_closed') ? panel.height() : '10'}, options);
	options.complete = function() {
		UpdateTabState($(this).parent().prev());
	}
	panel.stop().animate({top: this.hasClass('tab_closed') ? '0' : ('-' + (panel.height() - 10))}, options);	
	this.toggleClass('tab_closed');
}

function BuildRightPanels() {
	$('.panel_content')
		.wrap('<div class="panel_content_wrapper" />')
		.css('position', 'relative');
	$('.panel_tab')
		.click(function() {			
			$(this).TogglePanel({
				duration: 1000, 
				queue: false, 
				easing: 'easeInOutQuad'
			});
		})
		.bind('mouseover mouseleave', function() {
			UpdateTabState($(this).toggleClass('tab_hover'));
		});
}

function BuildProductsCarousel() {
	var tts = $('#product_tooltips .tooltip')
		.css('position','absolute')
		.addClass('hidden');
	if ($.support.opacity) tts.css('opacity', '0');
	tts.parent().removeClass('hidden');
	
	var products = $('.product')
		.each(function(i, e) {
			$(e)
				.removeClass('hidden')
				.removeAttr('title')
				.wrap('<div />')
				.hover(function() {
					$(this).ShowTooltip(tts.eq(i));
				}, function() {
					$(this).HideTooltip(tts.eq(i));
				});
			});
			
	$('#products')
		.css({position: 'relative', width: (products.outerWidth() * products.length) + 'px'})
		.wrap('<div id="products_wrapper" />')
		.parent().css({position: 'relative', overflow: 'hidden'});
	
	var navPositions = ['left', 'right'];
	var pw = $('#products_wrapper');
	var maxOffset = products.outerWidth() * (products.length - 4);
	for (var i=0; i<navPositions.length; i++) {
		var p = navPositions[i];
		$('#products_nav-' + p)
			.insertAfter($('#products'))
			.wrap('<div />')
			.css({
				width: '8px',
				height: '10px',
				marginTop: ((pw.outerHeight() - 10) / 2) + 'px',
				marginLeft: '6px',
				background: 'url(images/nav/overlay-nav.png) no-repeat scroll left top',
				backgroundPosition: '0 ' + ((p == 'left') ? '0' : '-10px')
			})
			.parent()
				.attr('id', 'products_nav-' + p + '_wrapper')
				.css({
					width: '20px',
					height: pw.outerHeight() + 'px',
					position: 'absolute',
					left: ((p == 'left') ? 0 : (pw.outerWidth() - 20)) + 'px',
					background: 'url(images/nav/overlay-nav-bg.png) repeat-y scroll left top'
				})
				.hover(function() {
					$(this).css('backgroundPosition', 'right top');
					var ps = $('#products');
					var curLeft = Math.abs(ps.position().left);
					if (this.id == 'products_nav-left_wrapper') {
						ps.stop().animate({
							left: '0'
						}, {
							duration: curLeft * 4 , 
							easing: 'linear'
						});
					} else {
						ps.stop().animate({
							left: '-' + maxOffset
						}, {
							duration: (maxOffset - curLeft) * 4, 
							easing: 'linear'
						});						
					}
				}, function() {
					$(this).css('backgroundPosition', 'left top');
					$('#products').stop();
				});
	}
}

function BuildSlideShow() {
	$('#slideshow_loadbar').css({
		background: '#039',
		height: '2px',
		width: '0',
		margin: '5px 0'
	})
	var sn = $('#slideshow_nav');	
	sn.css({
		height: '21px',
		width: '100%',
		padding: '2px'
	});
	var p = $('#slideshow');
	var nav = {
		css: {
			width: '13px',
			height: '13px',
			padding: '4px',
			color: '#039',
			textAlign: 'center',
			fontSize: '14px',
			lineHeight: '12px',
			float: 'left',
			marginRight: '5px',
			background: 'url(images/nav/slideshow-nav.png) no-repeat scroll left top',
			cursor: 'pointer'
		},
		mouseover: function(ev, params) {
			if (!params) params = {bypass: false};
			if (p.data('current').data('index') != $(this).data('index') || params.bypass) {
				$(this).css({
					backgroundPosition: $(this).data('left') + 'px -21px',
					color: '#aac6ff'
				});	
			}
		},
		mouseleave: function(ev, params) {
			if (!params) params = {bypass: false};
			if (p.data('current').data('index') != $(this).data('index') || params.bypass) {	
				$(this).css({
					backgroundPosition: $(this).data('left') + 'px 0px',
					color: '#039'
				});	
			}
		}
	};
	//custom playback
	var navControls = {
		playpause: {
			left: '-42',
			click: function() {
				if (!p.data('busy')) {
					p.data('paused', !p.data('paused'));
					if (p.data('paused')) {
						$(this).data('left', '-21').mouseover()
						p.data('current').trigger('mouseover', {bypass: true});
					} else {
						$(this).data('left', '-42').mouseover()
						p.data('nodelay', true);
						p.data('current').trigger('mouseleave', {bypass: true});
					}
				}
			}
		},
		prev: {
			left: '-105',
			click: function() {
				if (!p.data('busy')) {
					p.data('current').trigger('mouseover', {bypass: true});
					p.data('next', p.data('prev'));
					p.data('nodelay', true);
					p.data('current').trigger('mouseleave', {bypass: true});
				}
			}
		},
		next: {
			left: '-126',
			click: function() {
				if (!p.data('busy')) {
					p.data('current').trigger('mouseover', {bypass: true});
					p.data('nodelay', true);
					p.data('current').trigger('mouseleave', {bypass: true});
				}
			}
		}
	}
	
	var cfg;
	for (var ctrl in navControls) {
		cfg = navControls[ctrl];
		$('<div />')
			.appendTo(sn)
			.css(nav.css)
			.css('backgroundPosition', cfg.left + 'px 0')
			.hover(nav.mouseover, nav.mouseleave)
			.click(cfg.click)
			.data('left', cfg.left);
	}
	
	$('.slideshow_content').each(function(i, e) {	
		$(e).css({
			position: 'absolute',
			opacity: (i == 0) ? '1' : '0',
			zIndex: (i == 0) ? '1' : '0'
		})
		.removeClass('hidden')
		.data('index', i);
		$('<div />')
			.attr('class', 'slideshow-nav-num')
			.appendTo(sn)
			.css(nav.css)
			.hover(nav.mouseover, nav.mouseleave)
			.click(function() {
				if (!p.data('busy')) {
					var nxt = $('.slideshow_content').eq(i);
					if (p.data('current').not(nxt).length) {
						p.data('current').trigger('mouseover', {bypass: true});
						p.data('next', nxt);
						p.data('nodelay', true);
						p.data('current').trigger('mouseleave', {bypass: true});
					}
				}
			})
			.text(i + 1)
			.data('left', '0')
			.data('index', i);
	}).parent().css('overflow', 'hidden')
	.end().wrapAll('<div />').parent().css({
		height: '350px',
		position: 'relative'
	});
	$('.slideshow-nav-num:first').css({
		backgroundPosition: '0px -21px',
		color: '#aac6ff'
	});
	$('#slideshow').rotate({
		selector: '.slideshow_content',
		delay: 5000,
		duration: 1000,
		loadbar: $('#slideshow_loadbar'),
		loadbarEndWidth: '100%',
		pauseOnMouseOver: false,
		currentComplete: function() {
			var i = p.data('current').data('index');
			$('.slideshow-nav-num').eq(i).trigger('mouseleave', {bypass: true});
		},
		nextComplete: function() {
			var i = p.data('next').data('index');
			$('.slideshow-nav-num').eq(i).trigger('mouseover', {bypass: true});
		}
	});
}

function BuildTopTabs() {
	$('#toptabs > ul > li')
		.bind('focusin mouseover', function() {
			$(this).addClass('over');
		}).bind('focusout mouseout', function() {
			$(this).removeClass('over');
		});
}
function BuildTestimonials() {
	var mh = 0;
	$('#testimonials_loadbar').css({
		background: '#039',
		height: '2px',
		width: '0', 
		margin: '5px 0'
	});
	$('.testimonial').each(function(i, e) {	
		$(e).css({
			position: 'absolute',
			opacity: (i == 0) ? '1' : '0',
			zIndex: (i == 0) ? '1' : '0'
		})
		.removeClass('hidden');
		if (i == 0 && !$.support.opacity) e.style.removeAttribute('filter');
		var h = $(e).height();
		if (h > mh) mh = h + ($.support.opacity ? 0 : 15);
	}).css('height', mh + 'px')
	.parent().css('overflow', 'hidden')
	.end().wrapAll('<div />').parent().css({
		position: 'relative',
		height: mh + 'px'
	});
	$('#testimonials').rotate({
		selector: '.testimonial',
		delay: 5000,
		duration: 750,
		loadbar: $('#testimonials_loadbar'),
		loadbarEndWidth: '100%',
		pauseOnMouseOver: true
	});
}

function BuildTwitter() {
	$('.tweet').tweet({
		username: 'WorldLynk',
		join_text: 'auto',
		avatar_size: 32,
		count: 5,
		auto_join_text_default: 'we said,',
		auto_join_text_ed: 'we',
		auto_join_text_ing: 'we were',
		auto_join_text_reply: 'we replied to',
		auto_join_text_url: 'we were checking out',
		loading_text: 'loading tweets...'
	});
}

$.fn.ScrollLeft = function() {
	var stripWidth = this.outerWidth(true)
	this.animate({
		left: '-' + stripWidth + 'px'
	}, {
		duration: stripWidth * 12,
		easing: 'linear',
		complete: function() {
			$(this).css('left', '750px').ScrollLeft();
		}
	});
}

function BuildScrollingText() {	
	$('.scrolling').each(function() {
		var stripWidth = 0;
		$(this).find('li').each(function() {
			stripWidth += $(this).outerWidth(true)
		})
		.parent().css('width',stripWidth + 'px')
		.ScrollLeft();
	})
}

function SimulateTextAreaValidation() {
	$('textarea')
		.focus(function() {
			$(this).css('backgroundColor', '#FFFFCC');
		})
		.blur(function() {
			$(this).css('backgroundColor', '#B8F5B1');
		});
}

$(document).ready(function() {
	if (IsPath('/index.php')) {												 
		SlideSlogan();	
		BuildSlideShow();
		BuildProductsCarousel();		
	}
	
	BuildScrollingText();
	BuildTopTabs();
	BuildMenu();
	BuildRightPanels();
	SimulateTextAreaValidation();
	
	if (!IsPath('/testimonials.php'))
		BuildTestimonials();
	if (IsPath('/employee/'))
		BuildTwitter();
		
	//greensite link
	$('#greensite').click(function() {
		window.open(
			'http://www.justhost.com/ecertified?domain=' + 'feefreecreditcardprocessing.com' + '',
			'JustHost',
			'toolbar = 0,' + 
			'scrollbars = 0,' +
			'location = 0,' +
			'statusbar = 0,' +
			'menubar = 0,' +
			'resizable = 0,' +
			'width = 570,' +
			'height = 680,' +
			'left = 300,' +
			'top = 200'
		);
	});
	
	$('.jsLink').attr({
		href: 'javascript:void(0)',
		target: ''
	});
	
	if (IsPath('/services/index.php')) {
		$('#leftnav a').click(function() {
			$('#leftnav a').removeClass('active');
			$(this).addClass('active');
			
			$('#page_title').removeClass('short').addClass('long');
			$('#page_title h3').text($(this).text());
		});
		
	}
});