/*!
 * osolabs.js
 * http://www.osolabs.com
 *
 * Copyright (c) 2009 Benjamin Chung
 */
 
 
 var header_rotate = 1;
 var reset_timer = false;
 var child_menu_timer = setInterval(hide_menu_children, 3000);
 function rotate_header() {
	 
	
	if (reset_timer == false) {
		if (header_rotate == 1) {
			header_switch('web_dev');
		}
		else if (header_rotate == 2) {
			header_switch('hosting');
		}
		else if (header_rotate == 3) {
			header_switch('vps');
		}
		else if (header_rotate == 4) {
			header_switch('dedicated-servers');
		}
		
		header_rotate++;
		if (header_rotate > 4) {
			header_rotate = 0;	
		}
	}

    var rotate = setTimeout("rotate_header()", 25000);	 
 }
 $(document).ready(function() {
		
		if ($(".header_menu").length == 0) {
			$("#order_now").css({"height":"168px"});
				
		}
		$("#order_now .order_now_cat .category").click(function() {
			$("#order_now .order_now_cat .category").removeClass("active");
			$(this).addClass('active');
			$("#order_now .order_now_plans").hide();
			
			$("#order_now .order_now_plans:eq(" + $("#order_now .order_now_cat .category").index($(this)) + ")").show();
			
			return false;
			
		});
		$("#order_now .order_now_btn img.show").click(function() {
			$('#order_now').animate({
				marginRight: '0px',
			  }, 200, function() {
				  return false;
			});
			$(this).hide();
			$("#order_now .order_now_btn img.hide").show();
			return false;
		});
		
		$("#order_now .order_now_btn img.hide").click(function() {
			$('#order_now').animate({
				marginRight: '-716px',
			  }, 200, function() {
				  return false;
			});
			$(this).hide();
			$("#order_now .order_now_btn img.show").show();
			return false;
		});
	  
	  	
	   $('.header_right .header_bg').addClass('web_dev');
	   $('.learn_more a').attr({'href':'/website-development'});
	   
	   $('.header_menu .web_dev').mouseover(function() { header_rotate = 2;});
	   $('.header_menu .hosting').mouseover(function() {reset_timer = true; header_rotate = 3;});
	   $('.header_menu .vps').mouseover(function() {reset_timer = true; header_rotate = 4;});
	   $('.header_menu .dedicated-servers').mouseover(function() {reset_timer = true; header_rotate = 1;});
	   
       $('.header_menu .web_dev').mouseout(function() {reset_timer = false; header_rotate = 2;});
	   $('.header_menu .hosting').mouseout(function() {reset_timer = false; header_rotate = 3;});
	   $('.header_menu .vps').mouseout(function() {reset_timer = false; header_rotate = 4;});
	   $('.header_menu .dedicated-servers').mouseout(function() {reset_timer = false; header_rotate = 1;});
	   
	   $('.header_menu .web_dev').hoverIntent(function() {reset_timer = true; header_switch('web_dev'); }, function() {});
	   $('.header_menu .hosting').hoverIntent(function() {reset_timer = true; header_switch('hosting'); }, function() {});
	   $('.header_menu .vps').hoverIntent(function() {reset_timer = true; header_switch('vps'); }, function() {});
	   $('.header_menu .dedicated-servers').hoverIntent(function() {reset_timer = true; header_switch('dedicated-servers'); }, function() {});
	   
       rotate_header();
	   
	   
	   $('.collapse a').click(function() { $('.accord').toggle(); 
											if ($('.collapse a').html() == '[+]' ) {
												$('.collapse a').html("[-]");
											}
											else {
												$('.collapse a').html("[+]");
											}
											
											
									   });
	   
	   
	     $('a.collapse_menu').click(function() { $('.header').toggle(); 
											if ($('a.collapse_menu').html() == '[+]' ) {
												$('a.collapse_menu').html("[-]");
											}
											else {
												$('a.collapse_menu').html("[+]");
											}
											
											
		});
		var alertcodes = new Array();
		alertcodes[0] = 'Thank you. You have successfully confirmed your email address.';
		if (alertmsg != '' && alertmsg != undefined) {
				alert(alertcodes[alertmsg]);   
			   
		}
		
		var html5 = false;
		
		if(html5 == true) {
			osolabs_html5();
			
		}
		$("#menu .wrapper ul li a:not(ul.children a)").mouseover(
			function() {
				var marginTop = "70px";
				
				if ($(this).html() == 'Website Hosting')
				{
					marginTop = "35px";	
				}
				
				clearInterval(child_menu_timer);
				$("#menu .wrapper ul li:not(.current_page_item) a:not(ul.children a)").css({"color" : "#333"});
				$("#menu .wrapper .children a").hide();
				
				if($(".children", $(this).parent()).length) {
					
					$(this).css({"color" : "#fff"});					
					var elem = this;
					$(".wrapper_bg").css({"border-top" : "1px #ccc solid"});
					$(".wrapper_bg").stop().animate({"marginTop": marginTop}, 100, function() {
						$(".children", $(elem).parent()).show();	
						$(".children a", $(elem).parent()).show();
					});
				}
				else {
					hide_menu_children();
					$(this).css({"color" : "#fff"});	
					$(".wrapper_bg").stop().animate({"marginTop": "0px"}, 250, function() {
						$(".children a", $(elem).parent()).show();
					});	 
					$(".wrapper_bg").css({"border-top" : "0px"});				
				}
				
			}
		);
		$(".accord_content").hover(function() {
			clearInterval(child_menu_timer);
			child_menu_timer = setInterval(hide_menu_children, 3000);
		});
		
		$("#menu .wrapper .children").mouseover(function() {
			clearInterval(child_menu_timer);
			child_menu_timer = setInterval(hide_menu_children, 3000);
		});
		$("#menu .wrapper .children").children().mouseover(function() {
			clearInterval(child_menu_timer);
			child_menu_timer = setInterval(hide_menu_children, 3000);
		});
		
		if ($("#menu .wrapper ul li.current_page_parent .children").length)
		{
			var marginTop = "70px";
			if ($("#menu .wrapper ul li.current_page_parent a").html() == 'Website Hosting')
			{
				marginTop = "35px";	
			}
			
			 $(".wrapper_bg").css({"margin-top": marginTop}, 50);
			 $("#menu .wrapper li.current_page_parent .children a").show();
		}
		if ($("#menu .wrapper ul li.current_page_parent .children").length) {
			
			var marginTop = "70px";
			if ($("#menu .wrapper ul li.current_page_parent a").html() == 'Website Hosting')
			{
				marginTop = "35px";	
			}
			
			$(".wrapper_bg").css({"border-top" : "1px #ccc solid"});
			$(".wrapper_bg").css({"margin-top": marginTop}, 50);
			$(".wrapper_bg").stop().animate({"marginTop": marginTop}, 100, function() {
				$(".children", $(this).parent()).show();
				$("#menu .wrapper ul li.current_page_parent .children a").show();
			});
			$("#menu .wrapper ul li.current_page_parent .children").show();		 
		}
		if ($("#menu .wrapper ul li.current_page_item .children").length) {

			var marginTop = "70px";
			if ($("#menu .wrapper ul li.current_page_item a").html() == 'Website Hosting')
			{
				marginTop = "35px";	
			}
			
			$(".wrapper_bg").css({"border-top" : "1px #ccc solid"});
			$(".wrapper_bg").css({"margin-top": marginTop}, 50);
			$(".wrapper_bg").stop().animate({"marginTop": marginTop}, 100, function() {
				$(".children", $(this).parent()).show();
				$("#menu .wrapper ul li.current_page_item .children a").show();
			});
			$("#menu .wrapper ul li.current_page_item .children").show();		 
		 }
		 
 
 });
 function hide_menu_children() {
	 $("#menu .wrapper ul li:not(.current_page_parent):not(.current_page_parent .current_page_item):not(.current_page_item) .children").hide();	
	 $("#menu .wrapper ul li:not(.current_page_item) a:not(ul.children a)").css({"color" : "#333"});
	 $("#menu .wrapper ul li.current_page_item a:not(ul.children a)").css({"color" : "#fff"});
	 $("#menu .wrapper ul li.current_page_parent a:not(ul.children a)").css({"color" : "#fff"});
	 $("#menu .wrapper ul .current_page_ancestor li a:not(ul.children a)").css({"color" : "#ccc"});
	 $("#menu .wrapper ul .current_page_ancestor li.current_page_item a:not(ul.children a)").css({"color" : "#fff"});
	 
	 if ( $("#menu .wrapper ul li.current_page_parent .children").length || $("#menu .wrapper ul li.current_page_item .children").length) {
		var marginTop = "70px";
		if ($("#menu .wrapper ul li.current_page_parent a").html() == 'Website Hosting')
		{
			marginTop = "35px";	
		}
		if ($("#menu .wrapper ul li.current_page_item a").html() == 'Website Hosting')
		{
			marginTop = "35px";	
		}
		$(".wrapper_bg").stop().animate({"marginTop": marginTop}, 100, function() {
			$(".wrapper_bg").css({"border-top" : "1px #ccc solid"});
			$("#menu .wrapper ul li.current_page_parent .children").show();	
			$("#menu .wrapper ul li.current_page_parent .children a").show();	
			$("#menu .wrapper ul li.current_page_item .children").show();
			$("#menu .wrapper ul li.current_page_item  a").show();	
		});
			 
	 }
	 else {
		$(".wrapper_bg").stop().animate({"marginTop": "0px"}, 250);	 
		$(".wrapper_bg").css({"border-top" : "0px"});
	 }
	

 }

 function header_switch(class_name) {
	 

	var left_header=$('.header_left');
		$('.web_dev').removeClass('active');
		$('.hosting').removeClass('active');
		$('.vps').removeClass('active');
		$('.dedicated-servers').removeClass('active');	
		$('.header_left .' + class_name).addClass('active');
	
	
	var right_header=$('.header_right .header_bg');
	var learn_more = $('.learn_more a');
	var learn_more_link = '';
	right_header.hide('slide', {direction: 'right'}, 'fast', function() {
		right_header.removeClass('web_dev');
		right_header.removeClass('hosting');
		right_header.removeClass('vps');
		right_header.removeClass('dedicated-servers');
		right_header.removeClass('.header_bg');
		right_header.addClass('.header_bg ' + class_name);
		
		
		if (class_name == 'web_dev') {
			learn_more_link = '/website-development';
		}
		else if (class_name == 'hosting') {
		
			learn_more_link = '/website-hosting';
		}
		else if (class_name == 'vps') {
		
			learn_more_link = '/vps';
		}
		else if (class_name == 'dedicated-servers') {
		
			learn_more_link = '/dedicated-servers';
		}
		else {
		
			learn_more_link = '/';
		}
		
		
		learn_more.attr({'href':learn_more_link});
		
		right_header.show('slide', {direction: 'right'}, 'fast');
	}).queue(function () {
	  $(this).dequeue();
	});

 
 }
 
 function blink_image_down(class_name) {
 
	$('.' +class_name).fadeOut('fast', function() {
	
		$('.' + class_name).fadeIn('fast')
	});

 }
 function osolabs_subscribe() {
	
	var name = jQuery.trim($(".subscribe_bottom input[name='name']").val());
	var email = jQuery.trim($(".subscribe_bottom  input[name='email']").val());
	
	if(name == '' || name == 'name') {
		
		alert("Please enter your name.");	
		$(".subscribe_bottom input[name='name']").val('');
		$(".subscribe_bottom input[name='name']").focus();
	}
	
	else if( (email == '' || email == 'email') || validemail(email) == false)   {
		
		alert("Please enter a valid email address.");	
		$(".subscribe_bottom input[name='email']").val('');
		$(".subscribe_bottom input[name='email']").focus();
	}
	else {
		var orig_background = $(".subscribe_bottom #submit_btn").css('background');
		
		$(".subscribe_bottom #submit_btn").css({'background': 'url(/images/ajax-loader.gif) no-repeat', 'width': '16px', 'height': '16px'});
		$(".subscribe_bottom #submit_btn").attr("disabled", "true"); 
		
		$.ajax({
		   type: "POST",
		   url: "http://www.osolabs.com/oso/subscribe/?",
		   data: "name=" + escape(name) + "&email=" + escape(email),
		   success: function(msg){
			 alert( msg );
			 $(".subscribe_bottom #submit_btn").css({'background': orig_background, 'width': '65px', 'height': '21px'});
			 $(".subscribe_bottom #submit_btn").removeAttr('disabled'); 
		   }
		 });
		
	}
	return false;
	 
 }
 function validemail(email) {
   var regex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(regex.test(email) == false) {
      return false;
   }
}

function osolabs_html5() {
	$("#menu .wrapper ul li.page_item").hover(function(){
			
	  var p = $(this);
	  var position = p.position();
	  
	  
	  $("#menu_bubble").stop(true, true).animate({'left': position.left});
	  $("#menu_bubble .bubble_icons li.icon").hide();	
	  
	  
	  
	  var show_flag = 0;
	  
	  if($("a", this).html() == "Website Development") {
		  $("#menu_bubble .bubble_icons li.icon.microsoft").show();
		  $("#menu_bubble .bubble_icons li.icon.mysql").show();
		  $("#menu_bubble .bubble_icons li.icon.php").show();	
		  $("#menu_bubble .bubble_icons li.icon.last").show();
		  show_flag = 1;								
	  };
	  
	  if($("a", this).html() == "Website Hosting") {
		  $("#menu_bubble .bubble_icons li.icon.parallels").show();
		  $("#menu_bubble .bubble_icons li.icon.php").show();
		  $("#menu_bubble .bubble_icons li.icon.mysql").show();	
		  $("#menu_bubble .bubble_icons li.icon.last").show();
		  show_flag = 1;								
	  };
	  
	  if($("a", this).html() == "VPS") {
		  $("#menu_bubble .bubble_icons li.icon.parallels").show();
		  $("#menu_bubble .bubble_icons li.icon.fedora").show();
		  $("#menu_bubble .bubble_icons li.icon.ubuntu").show();
		  $("#menu_bubble .bubble_icons li.icon.redhat").show();
		  $("#menu_bubble .bubble_icons li.icon.php").show();
		  $("#menu_bubble .bubble_icons li.icon.mysql").show();	
		  $("#menu_bubble .bubble_icons li.icon.last").show();
		  show_flag = 1;							
	  };
	  
	  if($("a", this).html() == "Dedicated Servers") {
		  $("#menu_bubble .bubble_icons li.icon.microsoft").show();
		  $("#menu_bubble .bubble_icons li.icon.parallels").show();
		  $("#menu_bubble .bubble_icons li.icon.fedora").show();
		  $("#menu_bubble .bubble_icons li.icon.ubuntu").show();
		  $("#menu_bubble .bubble_icons li.icon.redhat").show();
		  $("#menu_bubble .bubble_icons li.icon.php").show();
		  $("#menu_bubble .bubble_icons li.icon.mysql").show();
		  $("#menu_bubble .bubble_icons li.icon.last").show();
		  show_flag = 1;									
	  };
	  
	  if (show_flag == 1) {
		  $("#menu_bubble").fadeTo("fast",0.8);
		  $("#menu_bubble").show();
	  }

	  }, function() {
		  $("#menu_bubble").hide();
	  
  });
	
}
