/* Custom Afrihost Scripts */
/* Version 1.0 */
/* Copyright 2010 */
/* Coding by Tyler Sven Theron */


		$(document).ready(function() {
			
			//GENERAL VARIABLES
			var oldtext = "";
			
			//FADING BUTTON MOUSEOVERS
			//Adds hover spans to items with the class of .btnfade and does a fade-in animation
			   
			$('.btnfade').append('<span class="hover"></span>').each(function () {
				var $span = $('> span.hover', this).css('opacity', 0);
				$(this).hover(function () {
					$span.stop().fadeTo(300, 1);
				}, function () {
					$span.stop().fadeTo(300, 0);
				});
			});
			
			
			
			//SEARCHBOX
			//Triggers the searchbox to fade-in on focus and fade-out on blur
			//Also handles keyboard selection events
			   
			$('#searchbox').focus(function() {  
				$(this).addClass("active");
				if (this.value == this.defaultValue)
				{  
					this.value = '';  
				}  
				if(this.value != this.defaultValue)
				{this.select();}  
				$('#ajaxsearchresults').fadeIn(500);
			}); 
			
			$('#searchbox').blur(function() {  
				$(this).removeClass("active");  
				if ($.trim(this.value) == '')
				{  
					this.value = (this.defaultValue ? this.defaultValue : '');  
				}  
				$('#ajaxsearchresults').fadeOut(500);
			});
			
			$('#searchbox').keydown(function(event) 
			{
				var n = $("#asr_data a").length;
				var n = n-1;
				
				if (event.keyCode == '38') 
				//UP
				{
					 event.preventDefault();
					 var newindex = $('#asr_data').find('.active').index() - 1;
					 $('#asr_data a').removeClass('active');
					 $('#asr_data a').eq(newindex).addClass('active');
				}
				if (event.keyCode == '40') 
				//DOWN
				{
					 event.preventDefault();
					 var newindex = $('#asr_data').find('.active').index() + 1;
					 if (n < newindex)
					 {
						newindex = 0;
					 }
					 $('#asr_data a').removeClass('active');
					 $('#asr_data a').eq(newindex).addClass('active');
				   }
				if (event.keyCode == '13')
				//RETURN
				{
					event.preventDefault();
					if ($('#asr_data').find('.active').attr("id") == "btn_searchall")
					{
						$('#fullsearchbox').val($('#searchbox').val());
						$('#fullsearch').submit();
					}
					var curindex = $('#asr_data').find('.active').index();
					var pagetoload = $('#asr_data a').eq(curindex).attr("href");
					if (curindex > -1)
					{
						 document.location.href = pagetoload;
					} else {
						var curtext = $('#searchbox').attr("value").replace(/ /gi, "%20");
						if (curtext.search(/www./) >= 0)
						{	
							document.location.href = pagetoload = "/site/page/domain_check/" + curtext + "#results";
						} else if (curtext.search(/.co/) >= 0)
						{	
							document.location.href = pagetoload = "/site/page/domain_check/" + curtext + "#results";
						} else if (curtext.search(/.gov/) >= 0)
						{	
							document.location.href = pagetoload = "/site/page/domain_check/" + curtext + "#results";
						} else if (curtext.search(/.ac/) >= 0)
						{	
							document.location.href = pagetoload = "/site/page/domain_check/" + curtext + "#results";
						} else {
							if (curtext != "")
							{
								$('#fullsearchbox').val($('#searchbox').val());
								$('#fullsearch').submit();
							}
						}
					}
				}
			});
			
			$('#searchbox').keyup(function(event) 
			{
				var curtext = $('#searchbox').attr("value").replace(/ /gi, "%20");
				
					if (curtext != oldtext) {
						
						if (curtext.search(/www./) >= 0)
						{	
							var domainchecklink = "<a href='/site/page/domain_check/" + curtext + "#results'><strong>" + curtext + "</strong><br />Check to see if this domain is available</a>";
							$('#asr_data').html(domainchecklink);
						} else if (curtext.search(/.co/) >= 0)
						{	
							var domainchecklink = "<a href='/site/page/domain_check/" + curtext + "#results'><strong>" + curtext + "</strong><br />Check to see if this domain is available</a>";
							$('#asr_data').html(domainchecklink);
						} else if (curtext.search(/.gov/) >= 0)
						{	
							var domainchecklink = "<a href='/site/page/domain_check/" + curtext + "#results'><strong>" + curtext + "</strong><br />Check to see if this domain is available</a>";
							$('#asr_data').html(domainchecklink);
						} else if (curtext.search(/.ac/) >= 0)
						{	
							var domainchecklink = "<a href='/site/page/domain_check/" + curtext + "#results'><strong>" + curtext + "</strong><br />Check to see if this domain is available</a>";
							$('#asr_data').html(domainchecklink);
						} else {
						
							document.getElementById('asr_data').innerHTML = '<div class="loading"></div>';
							
							var ajaxsearchdata = $("#ajaxsearch").serialize();
							
							/* THIS IS BEING QUARANTINED UNTIL I CAN FIGURE OUT HOW TO CHANGE THE SEARCH RESULTS ON THE FLY WITH THE EXPRESSION 2.2 MODULE CHANGE
							var ajaxsearchdata = $("#ajaxsearch").serialize() + "&RES=5&RP=search/ajaxresults"; */
							
							$.ajax({
							type: 'POST',
							url: '/',
							data: ajaxsearchdata,
							success: function(data){
									if (data.search(/error/) >= 0) // Check to see if the word 'error' occurs in the returned data.
									{
										var $data = $(data);
										var err = "";
										$data.find('li').each(function() 
											{ // Check each instance of the <li> in the returned data and iterate through.
												err+= "\t" + $(this).text() + "\n";
											});
										$('#asr_data').html("<p>"+ err +"</p>");
									}
									
									else if (data.search(/not return/) >= 0)
									{
										$('#asr_data').html("<p>Your search did not yield any results. <br />Please try different keywords.</p>");
									}
									
									else if (data.search(/load frequency/) >= 0)
									{
										$('#asr_data').html("<p>You have exceeded the allowed page load frequency. Please try again in 30 seconds. <br />Thank you.</p>");
									}
									
									else
									
									{$('#asr_data').html(data);}
								}
							})
						
						}
						
						oldtext = curtext;
					}
				
				
			});
			
			$('#asr_data a').live('hover', function() 
			{
				$('#asr_data a').removeClass('active');
				$(this).addClass('active');
			});
			
			$('#btn_searchall').live('click', function() 
			{
				event.preventDefault();
				$('#fullsearchbox').val($('#searchbox').val());
				$('#fullsearch').submit();
			});
			
			
			
			//FIELD
			//Common field triggers
			   
			$('.field').focus(function() {  
				$(this).addClass("active");
				if (this.value == this.defaultValue)
				{  
					this.value = '';  
				}  
				if(this.value != this.defaultValue)
				{this.select();}  
			}); 
			$('.field').blur(function() {  
				$(this).removeClass("active");  
				if ($.trim(this.value) == '')
				{  
					this.value = (this.defaultValue ? this.defaultValue : '');  
				}  
			}); 
			
			
			
			//NAVIGATION SUBMENUS
			//Triggers the fade-in and fade-out effects for the submenu
				
			$('#nav > ul > li').mouseleave(function(){
				$(this).children(".subnav").stop(true, true).fadeOut(250);
			});
			$('#nav > ul > li').mouseenter(function(){
				$(this).children(".subnav").stop(true, true).fadeIn(500);
			});
			
			
			
			//HOMEPAGE SLIDER
			//Triggers the homepage slider to do it's thing
			//Also activates the sliding arrow effect on the panel's button
				
			$('#coda-slider-1').codaSlider({
				dynamicArrows: false,
				dynamicTabs: false,
				autoSlide: true,
				autoSlideInterval: 7000
			});
				
			$('a.panel .sliderbtn div').css( {opacity: 1,backgroundPosition: "-50px -8px"} );
			
			function sliderarrow() {
				  $('a:hover.panel .sliderbtn div').animate({opacity: 0.25,backgroundPosition:"(350px -8px)"}, 1000);
				  $('a:hover.panel .sliderbtn div').animate({opacity: 1,backgroundPosition:"(-50px -8px)"}, 0, sliderarrow);
			}
			
			$('a.panel').hover(function(){sliderarrow();});
			
			
			
			//PRODUCT TABLE
			//Adds classes to product table based on column and row positions
			//Animation Effects for column hovering
			
			$('.product_table.main tr').eq(0).addClass("tjs");
			$('.product_table thead tr td').eq(1).addClass("first");
			$('.product_table thead tr td:last').addClass("last");
			$('.product_table tr>td:last-child').addClass("last");
			$('.product_table tr>td:nth-child(1)').addClass("descriptor tjs");
			$('.product_table tr>td:nth-child(2)').addClass("product");
			$('.product_table tr>td:nth-child(3)').addClass("product");
			$('.product_table tr>td:nth-child(4)').addClass("product");
			$('.product_table tr>td:nth-child(5)').addClass("product");
			$('.product_table tr>td:nth-child(6)').addClass("product");
			$('.product_table tr:even').addClass("alt");
			
			(function($) {
				var current;
				function findColumn(element) {}
					
				$.fn.columnHoverClass = (function(className)
				{
					$('td', this).hover(function() {
						var index = $(this).parent().children().index(this) + 1;
						current = $(this).parent().parent().parent().find('tr > td:nth-child('+index+')');
						current.addClass(className);
						$('.product_table tfoot td.product.active div').stop().animate({backgroundPosition:"(0 0)"}, 350);
						$('.product_table thead td.product.active div').stop().animate({backgroundPosition:"(0 0)"}, 350);
				}, function() {
						$('.product_table tfoot td.product.active div').stop().animate({backgroundPosition:"(0 -70px)"}, 500);
						$('.product_table thead td.product.active div').stop().animate({backgroundPosition:"(0 70px)"}, 500);
						current.removeClass(className);
						$('.product div').removeClass('active');
						$('.prod_package').slideUp();
				});
				 return this;
				 });
			 })(jQuery);

			 $('.product_table.main').columnHoverClass('active');
			 
			 $(".product div").live('click', function() {
				$(this).toggleClass('active');
				$(this).parent().find('.prod_package').slideToggle();
			});
			 
			$(".prod_package a").click(function () {
				$(this).parent().parent().parent().find('div').toggleClass('active');
				$(this).parent().parent().parent().find('.prod_package').slideToggle();
				var newpackage = $(this).html();
				var bracketpos = newpackage.indexOf("(");
				if (bracketpos > 0)
					newpackage = newpackage.substring(0,bracketpos);
				if (jQuery.browser.msie)
					newpackage = $(this).html();
				var newprice = $(this).attr("title") + ' <span>' + $("#billfreq").attr("title") + '</span>';
				var newurl = $(this).attr("href").replace(/#/gi, "");
				var baseurl = $("#baseurl").attr("href");
				$(this).parent().parent().parent().find('div').replaceWith('<div>'+ newpackage +'<span class="button" title="Change Package"></span></div>');
				$("body").find('.price .product.active div').replaceWith('<div>'+ newprice +'</div>');
				$("body").find('tfoot .product.active div a').replaceWith('<a href="'+ baseurl + newurl +'" class="btn_signup btnpulse">Sign Up Now »</a>');
            	$('.product_table .product .button').stop(true, true).css('opacity', 1);
			});
			
			
			
			//MORE INFO ACCORDIANS
			//Controls the toggling of more info for product accordians on product tables
			if (!jQuery.browser.msie) {
				$('.moreinfo .product_table').slideToggle(100);
			} 
			
			$("a.trigger").click(function () {
				$(this).toggleClass('active');
				$(this).parent().children('.product_table').slideToggle(500);
			});
			
			
			
			//READ MORE FEEDBACK
			//Shows the feedback fields on products
			
			if (!jQuery.browser.msie) {
				$(".testimonial.t2").slideUp(100);
				$(".testimonial.t3").slideUp(100);
			}
			 
			if (jQuery.browser.msie) {
				$(".testimonial.t1 .btnfade").hide();
			}
			
			$("a#btn_test").click(function () {
				$(".testimonial.t1 .btnfade").slideUp(200);
				$(".testimonial").slideDown(500);
			});	
			
			
			
			//LEAVE PRODUCT FEEDBACK
			//Shows the feedback fields on products
			
			if (!jQuery.browser.msie) {$("#product_feedback").slideUp(100);}
			 
			if (jQuery.browser.msie) {
				$(".testimonial.t3 #btn_leavefeedback").hide();
			}
			
			$("a#btn_leavefeedback").click(function () {$("#product_feedback").slideDown(500);});	
			
			
			
			//NETWORK STATUS ACCORDIANS
			//Controls the toggling of the network status item's body content
			
			$(".netstatus_title").click(function () {
				$(this).toggleClass('active');
				var index = $(this).parent().children().index(this) + 1;
				$(this).parent().children().eq(index).slideToggle(500);
			});
			
			
			
			//DOMAIN CHECKER
			//Triggers the display of the clientzone login
			
			$("#btn_domcheck").click(function () {
				$('#domcheck_results').html("<div class='loading'>Searching for domain availability.<br />Please be patient. This can take up to a minute.</div>");
				var domaintocheck = $('#domaincheck').attr("value");
				var domcheckurl = 'http://www.afrihost.com/domainchecker.php?dom=' + domaintocheck;
				$('#domcheck_results').load(domcheckurl, function() {_typeface_js.renderDocument();});
			});
			
			$('#domaincheck').keydown(function(event) 
			{
				if (event.keyCode == '13')
				//RETURN
				{$("#btn_domcheck").click();}
			});
			
			
			//BLACKOUT AND POPUPS
			//Triggers the display of blackout and popups
			
			$(".close").click(function () {
				$(".pop").css({'-webkit-box-shadow': "none"});
				$(".pop").css({'-moz-box-shadow': "none"});
				$(".pop").slideUp("slow", function() {
					$('#blackout').animate({opacity: "0.0"}, 500, function() {$('#blackout').hide();});
				});
			}); 
			
			
			//CORPORATE REPOSITORY LOGIN
            //Triggers the display of the corporate repository login
            
            $("#crlogin").click(function () {
                $('#blackout').show();
                $('#blackout').animate({opacity: "0.85"}, 200, function() {
                    $(".login").slideDown("slow", function() {
                        $(".login").css({'-webkit-box-shadow': "0 0 100px #000"});
                        $(".login").css({'-moz-box-shadow': "0 0 100px #000"});
                      });
                  });
                
            });
            
            $('#btn_login').click(function() {
                $('#crloginform').submit();
                $('.login .replace').addClass('loading').html(" ");
            });
            
            $('#crloginform .password').click(function() {$(this).parent().submit();});

			$('#crloginform .password').keypress(function(e) {
				if(e.keyCode == 13) {$('#btn_login').click();}
			});
			
			
			//BOOKMARK PAGE SCROLL
            //Triggers window scroll
            
            $.localScroll();
		});
