//=====================================================
// Validation plugin
//=====================================================
(function( $ ){
  $.fn.validator = function( options ) {
    var defaultOptions = {
      'errorClass'	: 'error'
    };

	// determines if all fields are valid
	var valid = true;
    this.each(function() {
      // If options exist, lets merge them
      // with our defaultOptions
      var o = defaultOptions;
	  if ( options ) { 
        o = $.extend( defaultOptions, options );
      } 
	  
	  // test the value of each
	  if ($(this).val() == '' || $(this).val() == $(this).attr('placeholder')) {
		  $(this).addClass(o.errorClass);
		  valid = false;
	  } else {
		  $(this).removeClass(o.errorClass);
	  }
    });
	return valid;
  };
})( jQuery );

//=====================================================
//Tab effects
//=====================================================
var TabbedContent = {
      init: function() {      
            $(".tabNavBtn").click(function() {
				  var background = $(this).parent().find("#tabToolTip");
                  $(".tabNavBtn").removeAttr('style');
                  $(this).css({"background-position" : "0 -162px"});
				  
				  $(".selected").removeClass('selected');
				  $(this).addClass('selected');
				  
                  $(background).animate({
                        left: $(this).position()['left']
                        
                  }, {
                        duration: 300
                  });
                  
                  TabbedContent.slideContent($(this));
            });
			
            $("#tabSliderLeft").click(function() {
				var item = $('a.tabNavBtn.selected');
				var previous = item.prev("a.tabNavBtn");
				
				if (previous.attr('id') != item.attr('id') && previous.length > 0) {
					$("#tabToolTip").animate({
						left: previous.position()['left']
					}, {
						duration: 300
					});
				
					item.removeClass('selected');
					item.css('background-position', "0px 0px");
					
					previous.addClass('selected');
					previous.css('background-position', "0px -162px");

					TabbedContent.slideContent(previous);
				}
            });
			
            $("#tabSliderRight").click(function() {
				var item = $('a.selected');
				var next = item.next("a.tabNavBtn");
				
				if (next.attr('id') != item.attr('id') && next.length > 0) {
					$("#tabToolTip").animate({
						left: next.position()['left']
					}, {
						duration: 300
					});
				
					item.removeClass('selected');
					item.css('background-position', "0px 0px");
					
					next.addClass('selected');
					next.css('background-position', "0px -162px");
					
					TabbedContent.slideContent(next);
				}
            });
      },
      
      slideContent: function(obj) {
            
            var margin = $(obj).parent().parent().find("#slideContent").width();
            margin = margin * ($(obj).prevAll().size() - 1);
            margin = margin * -1;
            
            $(obj).parent().parent().find("#tabSlider").stop().animate({
                  marginLeft: margin + "px"
            }, {
                  duration: 300
            });
      }
};

$(document).ready(function() {
    
    $("#approach").css({'background-position' : '0 -162px'});
	//=====================================================
	// Content slider InitializerSlider
	//=====================================================
	TabbedContent.init();
	
	//=====================================================
	// drop down
	// Remove the class of child and grandchild
	// This removes the CSS 'fallback'
	//=====================================================
	$("#nav ul.child").removeClass("child");
	$("#nav ul.grandchild").removeClass("grandchild");
	// When a list item that contains an unordered list
	// is hovered on
	$("#nav li").has("ul").hover(function(){
		//Add a class of current and fade in the sub-menu
		$(this).addClass("current").children("ul").fadeIn();
		
	}, function() {
		// On mouse off remove the class of current
		// Stop any sub-menu animation and set its display to none
		$(this).removeClass("current").children("ul").stop(true, true).css("display", "none");
	});

	$('#nav li a').hover(function() {
		$("#switchViewBtnArea").css("z-index", "1");
	});
	$("#nav li ul").hover(function() {
		$("#switchViewBtnArea").css("z-index", "1");
	}, function() {
		$("#switchViewBtnArea").css("z-index", "9999");
	});
	//=====================================================
	// Lavalamp initializer
	//=====================================================
	$("#nav").lavaLamp({
        fx: "backout",
        speed: 700,
        init: offsetWidth = 85,
        click: function(event, menuItem) {
            return false;
        }
    });
	
	//=====================================================
	// Toggle social media div
	//=====================================================
	$("#socialMediaCollapseBtn").click(function () {
	  $("#toggle").slideToggle(700);
	  $('.socialMediaArrow').toggleClass('socialMediaActive');
	  return false;
	});
	
	//=====================================================
	//loginpanel animation
	//=====================================================
	$('#loginpanel').mouseenter(function(){
		
		$('#loginpanel').stop();
		$('#loginpanel').animate({width: 203}, 150, function(){
			$('#loginpanel').css({WebkitBorderRadius: 13, MozBorderRadius: 13, BorderRadius: 13, OBorderRadius: 13}).animate({height: 190});
		});
		window.clearTimeout(foldLogin);	
		
	});
	$('#loginpanel').mouseleave(function(){
	
		if(!$('#loginpanel input[name=username], #loginpanel input[name=password]').is(':focus'))
		{
			foldLogin = window.setTimeout(function(){
				$('#loginpanel').stop();	
				$('#loginpanel').animate({height: 28}, function(){
					$('#loginpanel').css({WebkitBorderRadius: 1000, MozBorderRadius: 1000, BorderRadius: 1000, OBorderRadius: 1000}).animate({width: 77}, 150);
				});
			}, 400);
		}
	});
	
	//=====================================================
	// Accordions
	//=====================================================
	$("#accordion").accordion({ 
		header: "h4",
		autoHeight: false,
		navigation: true,
		collapsible: true,
		active: false
	});
	
	$("#accordionQuote").accordion({ 
		header: "h4",
		autoHeight: false,
		navigation: true,
		collapsible: false,
		active: false
	});
	
	//=====================================================
	// Clear input fields on focus and fill with default values if empty on blur
	//=====================================================
	$('input[type=text], textarea').each(function() {
	    var default_value = this.value;
	    $(this).focus(function() {
	        if(this.value == default_value) {
	            this.value = '';
	        }
	    });
	    $(this).blur(function() {
	        if(this.value == '') {
	            this.value = default_value;
	        }
	    });
	});
	
	//=====================================================
	// Gallery image overlay
	//=====================================================
	console.log($('.bar2'));
	$('.bar2').mosaic({
		animation	:	'slide'		//fade or slide
	});
    
	
	//=====================================================
	// Show more buttons for hidden divs on Business Booster
	//=====================================================
	$('.seeMoreBtn').click(function(){
		var hiddenId = '#' + this.id + 'Div';
		$(hiddenId).toggle('slow');
	});
	// change innerHTML of seeMoreBtn upon click.
    $('.seeMoreBtn').click(function(){
        if($(this).hasClass('seeLess')){
            $(this).removeClass('seeLess');
            $(this).html("See more<div class='seeMoreBtnArrow'></div>");
        } else {
            $(this).addClass('seeLess');
            $(this).html("See less<div class='seeMoreBtnArrow'></div>");
        }
    });
		
	//=====================================================
	// Custom accordion unit activation for IT Quote builder
	//=====================================================
	$('.checkB').change(function(){
		var id = $(this).parents('li').find('div:first').attr('id');
		var hiddenId = '#' + id;
		$(hiddenId).toggle('slow');
	});
	
	//=====================================================
	// Expand selections in accordion on quote builder
	//=====================================================
	$('.quoteSelection').click(function(){
		var checker = '#' + $(this).find('input').attr('id');
		if($(checker).attr('checked')) {
			$(checker).attr('checked', false);
		} else {
			$(checker).attr('checked', true);
		}
		$('input[name=foo]').attr('checked');
		var selector = '#' + $(this).find('input').attr('id') + 'Div';
		console.log(selector);
		$(selector).toggle('slow');
	});
    
    //=====================================================
	// Hover effect for hosting Table
	//=====================================================
    $(".hostingHover").hover(
        function() {
            var currentId = $(this).attr('id');
            $("#hostingTable td.host" + currentId + "_l").css({'background' : '#fd9958', 'color' : '#fff'});
            $("#hostingTable td.btn" + currentId + "_d").css({'background' : '#ffcdad'});
            $("#hostingTable td.host" + currentId + "_d").css({'background' : '#ffcdad', 'color' : '#fff'});
            $("#hostingTable td.blue" + currentId + "_l").css({'background' : '#fd9958', 'color' : '#fff'});
            $("#hostingTable td a.sign" + currentId).addClass('activated');
        },
        function() {
            var currentId = $(this).attr('id');
            $("#hostingTable td.host" + currentId + "_l").css({'background' : '#F1E8E3', 'color' : '#EE6923'});
            $("#hostingTable td.btn" + currentId + "_d").css({'background' : 'none'});
            $("#hostingTable td.host" + currentId + "_d").css({'background' : '#fff', 'color' : '#EE6923'});
            $("#hostingTable td.blue" + currentId + "_l").css({'background' : '#1097ad', 'color' : '#fff'});
            $("#hostingTable td a.sign" + currentId).removeClass('activated');
        }
    );
		
	//=====================================================
	// Force Numeric only values on .numeric
	//=====================================================
	$(".numeric").keydown(function(event) {
		if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 13) {
			// do nothing
		} else {
			if (event.keyCode < 95) {
				if (event.keyCode < 48 || event.keyCode > 57 ) {
					event.preventDefault();
				}
			} else {
				if (event.keyCode < 96 || event.keyCode > 105 ) {
					event.preventDefault();
				}
			}
		}
	});

});

	//=====================================================
	// video Modal
	//=====================================================
	function Show() {
		$("#dialog:ui-dialog").dialog("destroy");
		$("#dialog-modal").dialog({
			height: 470,
			width: 510,
			modal: true
		});
	}


