monthNames=["January","February","March","April","May","June","July","August","September","October","November","December"];

jQuery(function(){
  $.fn.extend({ 
	   fadeInOutHover: function() { 
		  return this.fadeTo("fast", .5).css('cursor','pointer').hover(function(){$(this).fadeTo("fast", 1);},function(){$(this).fadeTo("fast", .5);});
				  },
	  fadeOutInHover: function() { 
		  return this.fadeTo("fast", 1).css('cursor','pointer').hover(function(){$(this).fadeTo("fast", .5);},function(){$(this).fadeTo("fast", 1);});
				  }
  });
});

function cycle(full,thumb){
    $(full).cycle({
        speed: 3000,
        timeout:  3000,
        pager: thumb,
        pagerEvent: 'mouseover',
		//pause:'#topnav a[rel=sexylightbox]',
		pauseOnPagerHover: true,
		before:function(){//$('a#SLB-CloseButton').length?$(full).cycle('pause'):$(full).cycle('resume');
		},
		after: function(){ //$('a#SLB-CloseButton').length?$(full).cycle('pause'):cycle(full,thumb); 

		},
		pagerAnchorBuilder: function(idx, slide) { 
        //return '<a href="#"><img src="' + slide.src.replace('/full/','/thumbs/') + '" /></a>'; 
        //return '<a href="#"><img src="images/tiny_off.jpg" /></a>'; 
		return '<a href="#"><div class="ib">&nbsp;</div></a>';
    	}
    }).hover(
		function(){$(this).cycle('pause')
		},
		function(){$(this).cycle('resume')
		}
	).find('.ib').each(function(i){
		//(new Image()).src=this.src.replace('/full/','/thumbs/'); 
	})
	
}

function cycle_ready(full,thumb){

  var off=$(full).offset();
  var loading=$('<div id=loading/>').css({position:'absolute',zIndex:1000,left:off.left,top:off.top}).prependTo('body').hide().text('loading...');
  var first_slide = $('img:first',full);
  if(is_image_loaded(first_slide.get(0))){
	  loading.hide();
	  cycle(full,thumb);
  } else {
	  first_slide.load(function(e){
		  loading.hide();
		  $(full).show();
		  cycle(full,thumb);
	  })
	  loading.show();
	  $(full).hide();
  }
  
}
var is_image_loaded = function(img) {
    return (!img.complete || (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) )?false:true;// IE || others
}


bookingDate=function() {
	var today = new Date();
	return todayStr = (today.getMonth()+1) + "/" + (today.getDate()+1) + "/" + today.getFullYear();
}


String.prototype.isValidEmail=function(){
	re= new RegExp("^([a-z0-9])+([._])?([a-z0-9])+@([a-z0-9])+\.([a-z0-9]){2,6}$","ig");
	return this.match(re)==null?false:true;
}

isValidForm=function($form){
	var $field=$form.find('select,input:text,input:password,textarea');
	var error=[];
	var elem=[];
	var index=0;
	$field.each(function(i,o){
			$(o).val($.trim($(o).val()));
			if($(o).val()=='' || ($(o).attr('name')=='email' && !$(o).val().isValidEmail()) ){
				error[index]=true;
				elem[index]=o;
				index+=1;
			}
			else{
				//error[i]=false;
			}
	});
	if(typeof elem[0]!='undefined'){
		$field.removeClass('sexyFocus');
		$(elem[0]).addClass('sexyFocus').focus();
		return false;
		}
	else {
		//return true;
	}
}

function focus_blur(sel){
	$(sel).each(function(i,o){
		$(o).focus(function(){if(this.value==this.defaultValue) this.value='';})
			.blur(function(){this.value=($.trim(this.value)=='')?this.defaultValue:$.trim(this.value);});
	 });
}


sexyAlert=function(str){
	var btn='input#BoxAlertBtnOk';
	Sexy.alert('<h1>'+str+'</h1>');
	$('select').css({visibility:'visible'});
	$$(btn).DoSexy();
	window.setTimeout(function(){$(btn).click();$('.sexyFocus').focus()},3000);
}


formSubmit=function(ref){
		var $status=$('span#status').hide();
		 $form=$(ref);
		 var $focusBtn=$form.find('input,textarea');
		 $form.submit(function(){
			 //$status.show().text('sending...');
			 if(isValidForm($form)==false) {
				 sexyAlert('Please enter '+$('input.sexyFocus').attr('name').replace(/_/ig,' '));
				 $('input.sexyFocus').focus();
				 return false;
			   }
			 $.ajax({
					url:$form.attr('action'),
					data:$form.serialize(),
					type:'POST',
					//beforeSend:function(){return isValidForm($form);},
					success:function(d){
						sexyAlert(d);
						$form[0].reset();
						}
					});
			 return false;
			 })
}

StylizedCalendar=function(){
	$CalendarBlock=$('.CalendarBlock');
	$CalendarBlock.click(function(){
							  var $datepicker=$(this).find('input');
							  var $datesetter=$(this).find('a');
							  $datepicker.datepicker({dateFormat: 'mm/dd/yy',
													 onSelect: function(dateText, inst) { 
													 			var arr= dateText.split("/");
																var mm = arr[0];
																var dd = arr[1];
																//alert($(this).datepicker('getDate'));
																/* reza: don't need this anymore
													 			$datesetter.html('<div class=CalendarMonth>'+monthNames[mm-1]+'</div>'
																				+'<div class=CalendarDate>'+dd+'</div>'); */
													 			} 
													 }
													 );
							  $datepicker.focus();
							  });	
}


onImageOver=function(ref){
	$(ref).hover(
		function(){
			var oSrc=$(this).attr('src');
			var nSrc=oSrc.replace(/\.(png|jpg|gif)$/i,'_over.$1'); 
			$(this).attr('src',nSrc)
			},
		function(){
			var oSrc=$(this).attr('src');
			var nSrc=oSrc.replace(/_over(?=\.(png|jpg|gif))/i,'');
			$(this).attr('src',nSrc);
			}
		)
}