jQuery(document).ready(function() {	
	//open rel="external" in external window
	jQuery('a[href][rel*=external]').each(function(i){
		this.target = "_blank";
	});
	
	
	//cycle for the main page images
	if (jQuery('#topimg img').length > 1) {
		jQuery('#topimg').cycle({
			fx:					'fade',
			speed:				4500,
			next:				'#slide-next',
			prev:				'#slide-prev',
			pager:				'#home-thumbs ul',
			pause:				1,
			pagerAnchorBuilder:	function(idx, slide) {
		        return '<li><a href="#"><img src="' + slide.src + '" width="130px" height="75px"  /></a></li>'; 
		    }
		});
		//stop the slideshow when hovering
		jQuery('#home-thumbs').hover(function(){
				jQuery('#topimg').cycle('pause');
			}, 
			function(){
				jQuery('#topimg').cycle('resume');
			});
	}
		
	//open rel="external" in external window
	jQuery('a[href][rel*=external]').each(function(i){
		this.target = "_blank";
	});
	
   
	//create the slug if a slug and name field exist -- used on the people admin
	if(window.location.pathname == '/admin/people/people.cfm' && jQuery('#fldslug').length > 0 && jQuery('#fldname').length > 0){
		jQuery('#fldname').bind('keyup', function() {
			var regexed = jQuery.trim(jQuery('#fldname').val()).replace(/[^A-Za-z0-9 ]/g, '').replace(/ /g, '-').toLowerCase();
	    	jQuery('#fldslug').val(regexed);
	    });	
	}
	else{
		 //create the slug if a slug and title field exist
	    if (jQuery('#fldslug').length > 0 && jQuery('#fldtitle').length > 0) {
			jQuery('#fldtitle').bind('keyup', function() {
				var regexed = jQuery.trim(jQuery('#fldtitle').val()).replace(/[^A-Za-z0-9 ]/g, '').replace(/ /g, '-').toLowerCase();
		    	jQuery('#fldslug').val(regexed);
		    });
	    }
	}


    
    //make the AED "table" rows light up
    if (jQuery('ul#item_list').length > 0) {
    	jQuery('ul#item_list li').hover(function() {
    		jQuery(this).addClass('hover');
    	}, function() {
    		jQuery(this).removeClass('hover');
    	});
    }
	
	//run cycle on homepage realestate images ...scrollHorz
	if(jQuery('#listingNew').length > 0){
		jQuery('#listingNew').cycle({
			 fx:    'fade', 
    		 speed:  3500,
			 sync: 1,
			 cleartype:   0,
			 pause:  1,
			 prev: '#previous-realestate',
			 next: '#next-realestate'
		});		
		
	}	
	
	//time line pager
	if(jQuery('#history-cycle').length > 0){
		jQuery('#history-cycle').cycle({
			 fx:    'fade', 
			 timeout: 0,
			 pause:  1,
			 sync: 1,
			 cleartype:   0,
			 prev: '#previous-history',
			 next: '#next-history',
			 pager: '#timeline-cycle ul',
			 pagerAnchorBuilder: function(idx, slide) { 
       		 // return selector string for existing anchor 
       	 	 return '#timeline-cycle li:eq(' + idx + ') a'; 
    		} 

		});
		
		jQuery('#timeline-cycle').easySlider({
			//prevId: 'prevBtn',
			prevText: '',
			//nextId: 'nextBtn',	
			nextText: '',
			controlsShow: true,
			controlsFade: false,
			speed: 1800,
			pause: 2000
		});	
	}
	
	if (jQuery('.photoGalleryCategory').length > 0) {
		jQuery('.photoGalleryCategory a').fancybox({
		});
	}
	
	if (jQuery('input.default-value').length > 0) {
		jQuery("input.default-value").css("color", '#999');
		var default_values = new Array();
		jQuery("input.default-value").focus(function() {
			if (!default_values[this.id]) {
			  default_values[this.id] = this.value;
			}
			if (this.value == default_values[this.id]) {
			  this.value = '';
			  this.style.color = '#000';
			}
			jQuery(this).blur(function() {
			  if (this.value == '') {
			    this.style.color = '#999';
			    this.value = default_values[this.id];
			  }
			});
		});
	}
	
	//footer gallery fancy box
	if (jQuery('#footer-gallery').length > 0) {
		jQuery('#footer-gallery ul li a').fancybox({
		});
	}	
	
	jQuery('.IslandRealestate').click(function() {
		s=s_gi(s_account);
		s.linkTrackVars="events,prop14,eVar6";
		s.linkTrackEvents = "event10";
		s.events = "event10";
		s.prop14 = window.location.pathname;
		s.prop14 = s.prop14.toLowerCase();
		s.eVar6 = s.prop14;
		s.tl(this,'o','Search Island Real Estate');		 
	});
	
	//shootout for the contact form WOW
	if (jQuery('#smallComments')[0]) {
		jQuery('#smallComments').focus(function(){
			jQuery('#darkenBackground').css({opacity: "0.7"});
			jQuery('#darkenBackground').fadeIn('slow');
			jQuery('#smallContactContainer').hide('slow');
			jQuery('#bigContactContainer').show('scale', { origin: ['top', 'right'] });
			jQuery('#bigComments').focus();
		});
		
		//on submit validation.
		jQuery('#bigContact').submit(function(){
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			var flag=0;
			if(jQuery('#bigComments').val().length == 0){
				jQuery('#errorComments').css('color','#ff0000').html('This field required');
				jQuery('#errorComments').show();
				flag = 1;
			}
			else{
				jQuery('#errorComments').hide();
			}
			//radio btn switch
			if(jQuery('#bigContactStyleEmail').attr('checked') == true){
				if(jQuery('#bigEmail').val().length == 0){
					jQuery('#errorEmail').css('color','#ff0000').html('This field required');
					flag = 1;
				}
				else if(reg.test(jQuery('#bigEmail').val()) == false){
					jQuery('#errorEmail').css('color','#ff0000').html('Invalid Email Address');
					jQuery('#errorEmail').show();
					flag = 1;
				}
				else{
					jQuery('#errorEmail').hide();
				}
			}
			else {
				if(jQuery('#bigPhone').val().length == 0){
					jQuery('#errorPhone').css('color','#ff0000').html('This field required');
					flag = 1;
				}
				else{
					var stripped = jQuery('#bigPhone').val().replace(/[\(\)\.\-\ ]/g, '');
					if(isNaN(parseInt(stripped))){
						jQuery('#errorPhone').css('color','#ff0000').html('Invalid Phone Number');
						flag = 1;
					}
					else if(stripped.length != 10){
						jQuery('#errorPhone').css('color','#ff0000').html('Phone number to short.');
						jQuery('#errorPhone').show();
						flag = 1;
					}
					else{
						jQuery('#errorPhone').hide();
					}
				}
			}
			//error no submit
			if(flag == 1){
				return false;
			}
			//no error, submit to omniture
			else {
				s = s_gi(s_account);
				s.linkTrackVars = 'prop15,events';
				s.linkTrackEvents = 'event12';
				s.events = 'event12';
				s.prop15 = jQuery('input[name=bigContactStyle]').val();
				s.tl(this, 'o', 'popup contact submission');
			}
		});
		//end validation.
	}
	
	//add omniture success event to real estate exit link
	jQuery('.realestateexit').click(function() {
		s = s_gi(s_account);
		s.linkTrackVars = 'events';
		s.linkTrackEvents='event11';
		s.events = 'event11';
		s.tl(this, 'e', 'real estate search')
	});
	
	//close the big contact form $('#bigContactContainer').css('display')
	if (jQuery('#closeBigContact')[0]) {
		jQuery('#closeBigContact').click(function(){
			jQuery('#darkenBackground').fadeOut('slow');
			jQuery('#smallComments').val(jQuery('#bigComments').val());
			//jQuery('#bigContactContainer').hide('scale', {origin: ['top', 'right']});
			jQuery('#smallContactContainer').show('slow');
			jQuery('#bigContactContainer').hide('slow');
			//return false;
		});
	}
	
	//toggle email/phone in big contact form
	if (jQuery('#bigPhoneHolder')[0]) {
		jQuery('input[name=bigContactStyle]').click(function() {
			switch (jQuery(this).val()) {
				case 'phone':
					jQuery('#bigEmailHolder').hide();
					jQuery('#bigPhoneHolder').show();
				break;
				case 'email':
					jQuery('#bigPhoneHolder').hide();
					jQuery('#bigEmailHolder').show();
				break;
			}
		});
	}
	
	//add some class to an image aligned left
	if(jQuery('img[align=left]')[0]) {
		jQuery('img[align=left]').each(function(){ 
			var el = $(this);
			el.addClass('alignLeft');
			el.removeAttr('vspace');
			el.removeAttr('hspace');
			el.removeAttr('align');
		});
	}
	
	//add some class to an image aligned left
	if(jQuery('img[align=right]')[0]) {
		jQuery('img[align=right]').each(function(){ 
			var el = $(this);
			el.addClass('alignRight');
			el.removeAttr('vspace');
			el.removeAttr('hspace');
			el.removeAttr('align');
		});
	}
	
	//add an omniture success to the "view dataw island brochure" clicks
	if (jQuery('a[href=/about-dataw/brochure/]')[0]) {
		jQuery('a[href=/about-dataw/brochure/]').click(function() {
			s=s_gi(s_account);
			s.linkTrackVars = 'events';
			s.linkTrackEvents = 'event13';
			s.events = 'event13';
			s.tl(this, 'o', 'brochure click');
		});
	}
	
	//use class="fancybox" to make a fancybox show!
	if (jQuery('.fancybox')[0]) {
		jQuery('.fancybox').fancybox({
			padding: 0,
			frameWidth: 900,
			frameHeight: 600,
			hideOnContentClick: false
		});
	}
	
	//close fancy box when link is clicked
	if (jQuery('.continue')[0]) {
		jQuery('.continue').click(function(){
			jQuery.fn.fancybox.close();
			var el = jQuery(this);
			href = el.attr('href');
			console.log(href);
			window.parent.location.href = href;
		});
	}
}); //close doc.ready