$j = jQuery.noConflict();
$j(document).ready(function(){
	$j('ul li:first-child').addClass('first');
	$j('ul li:last-child').addClass('last');
	
	$j('tr:last-child').addClass('last');
	
	$j('.widget:first-child').addClass('first');
	$j('.widget:last-child').addClass('last');
	
	$j('ul li ul').parent().addClass('nav_parent');
	
	$j('.button input[type=submit]').attr('value', '');

	// Navigation Title Tooltip
	$j('#nav_inner_cont ul li').children('a').attr('rel', 'tooltip');
	$j('ul.sub-menu li').children('a').removeAttr('rel');

	$j('#nav_inner_cont').prepend('<div class="tooltipBox"></div>');

	//Select all anchor tag with rel set to tooltip
    $j('a[rel=tooltip]').each(function(){
		var k = $j(this);
		if(k.attr('title')){
			var titlData = k.attr('title');
			k.attr('tip', titlData);
		}

		k.removeAttr('title');
		if(k.attr('tip')){
			k.unbind('mouseenter').mouseenter(function(){
				var l = k.attr('tip');
				var pos = k.parent().position();
	
	
				$j('.tooltipBox').stop();
				$j('.tooltipBox').html(l);		
				
				$j('.tooltipBox').css({
					'left': pos.left + 20,
					'display': 'block',
					'opacity': 1
				});
			});
	
			k.unbind('mouseout').mouseout(function(){				
				$j('.tooltipBox').animate({
					'opacity': 0
				},'slow',function(){
					$j('.tooltipBox').css({
						'display': 'none'
					});	
				});
			});
		}
	});





	// Overlay
	theOv = $j("a[rel=overlay]").each(function(i) {
		//var thetarget = $j(this).attr("href")
		$j(this).overlay({
			target : $j(this).attr("href"),
			left: 'center',
			top: 'center',
			expose: {
				color: '#111',
				loadSpeed: 200,
				opacity: 0.8
			},
			closeOnClick: true,
		api: true
		});
	}); 

	  // Set input values from its label
	  $j.fn.valFromLabel = function(){
	    var inputs = $j('.input', this);
	    inputs.each(function() {
	      var l = $j(this).siblings('label');
	      l.hide();
	      var v = l.html();
	      $j(this).val(v);
	    });
	  }

	  // Swap input values on focus
	  $j.fn.swapVals = function() {
	    var values = [];
	    var inputs = $j('.input', this);
	    inputs.each(function(i){
	      values[i] = $j(this).val();
	      $j(this)
	        .focus(function(){
	          if ($j(this).val() == values[i]) {
	            $j(this).val("");
	          }
	        })
	        .blur(function(){
	          var l = $j(this).siblings('label');
	          l.hide();
	          var v = l.html();
	          var theVal = $j.trim($j(this).val())
	          if ( theVal == "" || theVal == v ) {
	            $j(this).val(values[i]);
	            $j(this).removeClass('valid');
	            $j(this).addClass('error');
	          } else {
	            $j(this).removeClass('error');
	            $j(this).addClass('valid');
	          }
	        });
	    });
	  }
	
	  $j.fn.checkVal = function(){
	    var iForm = $j(this);
	    var result = null;
	    var reqF = $j('.req', iForm);
	    reqF.each(function() {
	      $j(this).blur(function(){
	        if ($j.trim($j(this).val()) == "") {
	          $j(this).addClass('error');
	          $j(this).removeClass('valid');
	        } else {
	          $j(this).removeClass('error');
	          $j(this).addClass('valid');
	        }
	      });
	    });
	    return result;
	  }
	
	  // Validate form
	  function validate(formData, jqForm, options){
	    send = null;
	    var theForm = jqForm[0];
	    var submit = $j('.submit', theForm);
	    var reqF = $j('.req', theForm);
	    //if (theForm.not('.swap')) {checkVal(theForm);}
	    reqF.each(function() {
	      if ( $j(this).hasClass('valid') ) {
	        send = true;
	        return true;
	      }
	      else {
	        $j(this).addClass('error');
	        alert('Please fill in all required fields!');
	        send = false;
	        return false;
	      }
	    });
	    if ( !send ) { return false; }
	    else { submit.attr('disabled', 'disabled'); alert('Thank You! We\'ll get back too as soon as possible'); }
	  }
	
	  $j.fn.checkReq = function(){
	    var theForm = $j(this);
	    var reqF = $j('.req', theForm);
	
	    reqF.each(function() {
	      if ( $j(this).hasClass('valid') ) {
	        send = true;
	        return true;
	      }
	      else {
	        $j(this).addClass('error');
	        alert('Please fill in all required fields!');
	        send = false;
	        return false;
	      }
	    });
	    if ( !send ) { return false; }
	  }

	$j('#thebook form').checkVal();
	$j('#thebook form').submit(function(){
	    return $j(this).checkReq();
	    theOv.close();
	 });

	cForm = $j('form.theForm');
	cForm.ajaxForm({
	  beforeSubmit: validate
	});

});
