// JavaScript Document
$( document ).ready( function(){
	
	
	var server 	= window.location.hostname;
	var url 	= '';
	var altTags = 'Assist-Card USA Travel Insurance';
	var todaysDate = new Date();			
	
	if(server == 'dev.zcom2.com' ){
		
		url = '/AssistCard/websites/2010-2.0/assist-cardusa_com/';
		
		setimagepath( url );
		
		
	}else{
		
		url = '/';
	}	
	

	//input mask 
	
	
	$( ".phone" ).mask( "(999) 999-9999" );
	$( ".agencycode" ).mask( "9999-999" );
	$( ".social" ).mask( "999-999-9999" );
	$( ".zip" ).mask( "99999" );
	
	
	//set dev image paths
	
	function setimagepath( url ){
		
		url = url.slice(0, -1)							   
	
		$( 'div#interior-banner div.content img' ).each( function( index ){
					  
		
 
		 	var src = $( this ).attr( 'src' );				  
			
			$( this ).attr( 'src' , url + src );
					  
		});
		
		return false;
	}

	//set the title of all links
					
	$( 'a' ).each( function( index ){
		
		var $this = $( this );
	
	
		if( $this.text() < 1 ){
			
			$this.attr( 'title' , altTags );	
			
		}else{ 
		   
			$this.attr( 'title' , $this.text() + ' | ' + altTags );
		}
		
		if( $this.children().is( 'img' )){
			
			var imageAlt = $this.find( 'img' ).attr( 'alt' );
			
			if( imageAlt.length < 1 ){
				
				$this.attr( 'title' , altTags );	
				
				
			}else{
				
				$this.attr( 'title' , imageAlt + ' | ' + altTags );
				
			}
			
		}	
	});


	//set up default meta info
	
	if( $( 'meta[ name="keywords" ]' ).attr( 'content' ) == ''  || $( 'meta[ name="description" ]' ).attr( 'content' ) == '' || $( 'title' ).text() == ''){
		
		var keywords 	= 'ASSIST-CARD, ASSIST-CARD Travel, ASSIST-CARD Insurance, Assist Travel Insurance, ASSIST-CARD Travel Insurance, Assist Travelers Insurance, Travelers Insurance, Travel Insurance, Travel Insurers';
		var desc		= 'ith products tailored to fit every traveler, ASSIST-CARD has you covered in over 107 countries.';		
		var title		= 'ASSIST-CARD | Travel Assistance - Travel Insurance';
		
		$( 'meta[ name="keywords" ]' ).attr( 'content' , keywords );
		$( 'meta[ name="description" ]' ).attr( 'content' , desc );
		
		document.title = title;	
	}


	//open all external links in a new tab
	
	$("a[href^='http']")
		.each( function( index ){
										
			var url = $( this )
						.attr( 'href')
							.replace( /^http\:\/\/(www\.)?/i , "" );

			pageTracker._trackPageview( '/external/' + url );
											
	});

	//set a general alt if none exists

	$( 'img' ).each( function( index ){
			
		var $this = $( this );
		
		if( $this.attr( 'alt' ).length < 1 ){
			
			$this.attr( 'alt' , altTags );
		
		}
	
					  
	});


	//tool tips
	
	$('a').tooltip({
		
		track: false,
		
		delay: 1000,
		
		showURL: false,
		
		showBody: " - ",
		
		fade: 250
	});

	$( 'li span' ).tooltip({
	
		track: false,
		
		delay: 200,
		
		showURL: false,
		
		showBody: " - ",
		
		fade: 250
				     
	});

	//image cache
	
	( function( $ ){
	
		var cache = [];
		
		// Arguments are image paths relative to the current page.
		
		$.preLoadImages = function() {

			var args_len = arguments.length;
    			
			for (var i = args_len; i--;) {
				
				var cacheImage = document.createElement( 'img' );
				cacheImage.src = arguments[ i ];
				cache.push( cacheImage );
    			}
		}
	
	})(jQuery)




	//our-services function

	$( 'ul#questions li' ).hover( function( event ){
				
		var $this = $( this );		
		var answerText;
		
		$this.addClass( 'question-hover' )
			.children( 'a' )
			.addClass( 'question-hover' );
		
		answerText = $( 'div#answers div' )
					.eq( $this.index() )
						.html();

	
		$( '.pull-quote' )
			.html( answerText )
				.contents()
					.fadeIn( 'slow' );
		
	},function(){
	
		$( this ).removeClass( 'question-hover' ).children( 'a' ).removeClass( 'question-hover' );
	
	});

// current client login page


	$( 'form#current-cleints-login' ).validate({
								 
			errorElement: 'p',
			
			onkeyup: false,
	
			onblur: false,
			
			focusCleanup: true,
			
			focusInvalid: false,
	
			rules:{
				
				policynumber:{
				
					required: true
					
				},
				dob:{
					
					required: true,
					date: true
					
				}
				
			},
			
			messages:{
				
				policynumber:{
					
					required: 'Your policy number is required.'
					
				},
				dob:{
					
					required: 'Your date of birth is required',
					date: 'A valid date is required'
					
				}
			}
	
	})


	//general calendar popup 
	
	$('.calendar').datepicker({

			showOn: 'button',
	
			buttonImage: url + 'assets/images/button/btn-calendar-icon.gif',
			
			buttonImageOnly: false,

			dateFormat: 'm/d/yy',
			
			minDate: todaysDate,
			
			defaultDate: "+1w",
			
			changeMonth: true,
			
			numberOfMonths: 2	

	});
	
	


	
	
	
// become an agent form 

	$("form#contact-form, form#corporate-form").validate({
		
		invalidHandler: function( e, validator ) {
			
			var errors = validator.numberOfInvalids();
			
			if ( errors ) {
				
				
				
				var message = errors == 1
					
					? 'You missed 1 field. It has been highlighted below'
					: 'You missed ' + errors + ' fields.  They have been highlighted below';
				
				$( "div.error span, div.contact-error span" ).html( message );
				
				$( "div.error, div.contact-error" ).show();
				
				if( $( this ).attr( 'id' ) == 'corporate-form' ){
	
					$( 'html, body' ).animate( { scrollTop: 500 } , 'slow' );
	
				}else{
					$( 'html, body' ).animate( { scrollTop: 100 } , 'slow' );
				
				}
			
			}else if( errors.length < 1 ) {
				
				//$( "div.error" ).hide();
			
			}
		}
	});


	$("form#agent-login").validate({

			errorElement: 'p',
			
			onkeyup: false,
	
			onblur: false,
			
			focusCleanup: true,
			
			focusInvalid: false,

			rules:{
			
				username:{
					
					required: true
					
				},
				password:{
					
					required: true
					
				}
			},
			messages:{
				
				username:{
					
					required: 'Your username is required.'
					
				},
				password:{
					
					required: 'Your password is required.'
					
				}
			
			}
		
	
	});


	$("form#alerts").validate({

			errorElement: 'p',
			
			onkeyup: false,
	
			onblur: false,
			
			focusCleanup: true,
			
			focusInvalid: false,
			
			messages:{
				
				email:{
					
					required:'Your email address is required.'
					
				}	
			},
			submitHandler: function( form ){
				
				$.post(
					 url + 'email-alerts/index.cfm', 
					 { 
					 	email: $( 'input#emailalert' ).val(),
						action: $( 'input[ name="action" ]' ).val(),
						page: $( 'input[ name="page" ]' ).val(),
						ip: $( 'input[ name="ip" ]' ).val()
					},
 					 function( data ){
					     
					     $( 'input#emailalert' ).val( $.trim( data ) );
				});	
				
				return true;
			}
		
	
	});


	//create product links
	
	$( 'body#products.international div.left h2' ).click( function(){
											   
		var $this = $( this );										   
	
		location.href = $this.attr( 'class' ) + '/';
	
	
	});
	
	
	
	//faqs
	$( 'body#faq div.sections a.top' ).click( function( event ){
								     
		event.preventDefault();
		
		$( 'html, body' ).animate( { scrollTop: 200 } , 'slow' );
	
	
		return false;						     
	}); 
	

	$( ".scroll" ).click( function( event ){

		event.preventDefault();

	
		var full_url = this.href;

	
		var parts = full_url.split( "#" );
		var trgt = parts[ 1 ];


		var target_offset = $( "#"+trgt ).offset();
		var target_top = target_offset.top;


		$( 'html, body' ).animate( { scrollTop: target_top }, 500) ;
	});



	//assistance vs insurance
	//$( 'div#insurance-container div.body-row div.col p' ).prepend( '&bull;&nbsp;' );


	//search
	$( 'img#search' ).toggle( function( event ){
						     
		event.preventDefault();					     
						
		$( 'div#search-form' ).slideDown( 'slow' );
		
	},function( event ){
		
		event.preventDefault();
	
		$( 'div#search-form' ).slideUp( 'slow' );


	});
	
	
	//div notice success boxes
	$( 'div.notice, div.success,div.error' ).delay( 2500 ).hide( 'slow' );



// become an agent form 

	$("form#become-agent-form").validate({
		

		rules:{
			
			licensenumber:{
				
				checkLicense:true			
			}
		},
		invalidHandler: function( e, validator ) {
			
			var errors = validator.numberOfInvalids();
			
			if ( errors ) {
				
				
				
				var message = errors == 1
					
					? 'You missed 1 field. It has been highlighted below'
					: 'You missed ' + errors + ' fields.  They have been highlighted below';
				
				$( "div.error span, div.contact-error span" ).html( message );
				
				$( "div.error, div.contact-error" ).show();
				
				if( $( this ).attr( 'id' ) == 'corporate-form' ){
	
					$( 'html, body' ).animate( { scrollTop: 500 } , 'slow' );
	
				}else{
					$( 'html, body' ).animate( { scrollTop: 100 } , 'slow' );
				
				}
			
			}
		}		
	});


	$.validator.addMethod( "checkLicense" , function( value ) {

				
		if( $( 'input[ name="license" ]:checked' ).val() == 'yes' && $( 'input[ name="licensenumber" ]' ).val() == ''){
			
			return false;
				
			
		}else{
			

			return true;	
		}

	});
	

	

});
