

var SteamrSite = function() {
	var self = {
		cfg : {
			indexBannerTitles   : ['You\'re Protected', 'Obtain Immediate Support', 'Take Control'],
			indexBannerOffset   : [0, 271, 542],
			indexBannerLocation : ['#', '#', '#'],
			currentBanner : 0,
			indexBannerWaitTime : 6000,
			indexBannerTimeout : null,
			
			proactiveTimeout : 15
			
		}, 
		
		init : function() {
			self.initializeNavigation();
			self.initializeIndexBanners();
			self.initializePartnerList();
			self.initializeNewsTicker();
			self.initializeFormElements();
			self.initializeTooltips();
			self.initializeTopRight();
			self.fixKayakoProactive();
			self.initializeContactForms();
		},
		
		initializeNavigation : function() {
			$('#navigation ol li').each(function() {
				if ($(this).find('ul.subnav').length != 0) {
					$(this).mouseenter(function() {
						$('#navigation ol li').removeClass('selected');
						$('#subNavigation').empty();
						$(this).addClass('selected').find('ul.subnav').clone().appendTo($('#subNavigation'));
					});
				}
			});
			$('ul.subnav li:last-child').addClass('last');
		},
		
		initializeIndexBanners : function() {
			if ($('#mainBannerRotator') == null) return;
			
			$('<div>').attr({id: 'mainBannerRotatorFader'}).prependTo('#mainBannerRotator');
			
			$('<ol>').attr('id', 'bannerSelectorList').appendTo('#bannerSelector');
			for (i = 0; i < self.cfg.indexBannerTitles.length; i++) {
				$('#bannerSelectorList').append(
					$('<li>')
						.text(self.cfg.indexBannerTitles[i])
						.css('cursor', 'pointer')
						.attr('id', i)
						.click(function() {
							self._changeBannerImage($(this).attr('id'));
						})
				);
			}
			
			self._initializeRotateBanner();
			
		},
		
		_changeBannerImage : function(bannerIndex) {
			if ($('#mainBannerRotatorFader') == null) return;
			
			var bannerOffset = self.cfg.indexBannerOffset[bannerIndex];
			self.cfg.currentBanner = bannerIndex;
			
			$('#mainBannerRotatorFader')
				.stop()
				.css({
					opacity: '0',
					display: 'block',
					'background-position': '0px -' + bannerOffset + 'px'
				}).animate({
					opacity: '1'
				});
			
			$('#bannerSelector ol li').removeClass('selected');
			$('#bannerSelector ol li:eq(' + bannerIndex + ')').addClass('selected');
			
			setTimeout(function() {
				$('#mainBannerRotator').css({
					'background-position': '0px -' + bannerOffset + 'px'
				});
				$('#mainBannerRotatorFader').css({
					display : 'none'
				});
			}, 500);
		},
		
		_initializeRotateBanner : function() {
			self._changeBannerImage(self.cfg.currentBanner);
			
			self.indexBannerTimeout = setTimeout(function() {
				self.cfg.currentBanner = (self.cfg.currentBanner + 1) % self.cfg.indexBannerTitles.length;
				self._initializeRotateBanner();
			}, self.cfg.indexBannerWaitTime);
		},
		
		initializePartnerList : function() {
			$('.partnerList').stop().css({left: 0}).animate({left: -770}, 12500, 'linear');
			setTimeout(function() {
				self.initializePartnerList();
			}, 12500);
			// what works: 5000, 4300
		},
		
		initializeNewsTicker : function() {
			//if ( ! $.browser.msie || parseInt(jQuery.browser.version) != 7)
				$('ul#ticker').liScroll();
		},
		
		initializeFormElements : function() {
			$('input').add('textarea').focus(function() {
				$(this).addClass('focused');
			}).blur(function() {
				$(this).removeClass('focused');
			});
		},
		
		initializeTooltips : function() {
			$('a.tooltip').mouseenter(function(e) {
				$(this).append($('<div class="tooltipBox">').text($(this).attr('title')).css('top', e.pageY + 10).css('left', e.pageX + 20));
			}).mouseout(function() {
				$(this).children('div.tooltipBox').remove();
			}).mousemove(function(e) {
				$(this).children('div.tooltipBox').css('top', e.pageY + 10).css('left', e.pageX + 20);
			});
		},
		
		initializeTopRight : function() {
			$(window).scroll(function() {
				$("#topright").css({top: document.documentElement.scrollTop});
			});
		},

		fixKayakoProactive : function() {
			if ($('#proactivechatdiv') != null) {
				$('#proactivechatdiv').insertAfter('#topright');
			//	$('#proactivechatdiv').css('display', 'block');

				$(window).scroll(function() {
					$("#proactivechatdiv").css({top: document.documentElement.scrollTop + 300});
				});

				$('.cancel').click(function() {
					self.setCookie('cancelchat', 'true', null);
				});
				$('.chat').click(function() {
					self.setCookie('cancelchat', 'true', null);
				});

				self.fixKayakoProactivePosition();
				self.proactivelyDisplayKayako();
			}
		},

		proactivelyDisplayKayako : function() {
			if (self.getHostname() != "zenprotection.com" && self.getHostname() != "www.zenprotection.com" && self.getHostname() != "zenprotection.net" && self.getHostname() != "www.zenprotection.net") {
				return;
			}

			displayedBefore = self.getCookie('cancelchat');
			if (displayedBefore != "true") {
				setTimeout(function() {
					self.proactivelyDisplayTimeout();
				}, self.cfg.proactiveTimeout * 1000);
				//self.proactivelyDisplayTimeout();
			} else {
				self.setCookie('displayed', "true", null);
			}
		},
	
		proactivelyDisplayTimeout : function() {
			$("#proactivechatdiv").css('display', 'block');
		},

		fixKayakoProactivePosition : function() {
			var height = $('body').height() - 400;
			var width = $('body').width() - 400; 
			$('#proactivechatdiv').css({
					top: document.documentElement.scrollTop + 300,
					'left' : width / 2
			});

			setTimeout(function() {
				self.fixKayakoProactivePosition();
			}, 10000);
		},
	
		initializeContactForms : function() {
			$('input#submitContact').click(function() {
				$.post('/send.php', $('#contactForm').serialize(), function(data) {
					if (data == 'OK') {
					//	alert("Your message has been sent!");
						window.location = '/message_sent';
					} else {
						alert(data);
						$('#captcha_image').attr('src', 'securimage/securimage_show.php?r=' + Math.random());
						$('#captcha').attr('value', '');
					}
				});
				return false;
			});
		},

		getCookie : function(name) {
			if (document.cookie.length > 0) {
				start = document.cookie.indexOf(name + "=") + name.length + 1;
				end = document.cookie.indexOf(";", start);
				if (end == - 1) {
					end = document.cookie.length;
				}
				return unescape(document.cookie.substring(start, end));
			}
			return "";
		},


		setCookie : function(name, value, expire) {
			var expireDate = new Date();
			expireDate.setDate(expireDate.getDate() + expire);
			document.cookie = name + "=" + escape(value) + ((expire==null) ? "" : ";expires=" + expireDate.toUTCString());
		},

		getHostname : function() {
			return window.location.hostname;
		}

		

		
		
	};
	
	self.init.apply(self);
	return self;
};

$(function() {
	var steamrSite = new SteamrSite();
});









jQuery.fn.liScroll = function(settings) {
		settings = jQuery.extend({
		travelocity: 0.07
		}, settings);		
		return this.each(function(){
				var $strip = jQuery(this);
				$strip.addClass("newsticker")
				var stripWidth = 0;
				var $mask = $strip.wrap("<div class='mask'></div>");
				var $tickercontainer = $strip.parent().wrap("<div class='tickercontainer'></div>");								
				var containerWidth = $strip.parent().parent().width();	//a.k.a. 'mask' width 	
				$strip.find("li").each(function(i){
				stripWidth += jQuery(this, i).width();
				});
				$strip.width(stripWidth);			
				var totalTravel = stripWidth+containerWidth;
				var defTiming = totalTravel/settings.travelocity;	// thanks to Scott Waye		
				function scrollnews(spazio, tempo){
				$strip.animate({left: '-='+ spazio}, tempo, "linear", function(){$strip.css("left", containerWidth); scrollnews(totalTravel, defTiming);});
				}
				scrollnews(totalTravel, defTiming);				
				$strip.hover(function(){
				//jQuery(this).stop();
				},
				function(){
				var offset = jQuery(this).offset();
				var residualSpace = offset.left + stripWidth;
				var residualTime = residualSpace/settings.travelocity;
				scrollnews(residualSpace, residualTime);
				});			
		});	
};

