$(document).ready( function() {
   
    // open external link in new tab/window
    $('a[rel="external"]').click( function() {
        this.target = "_blank";
    });
    
    //email replacement
   	$("span.mailto").each(function(){
   	  exp = $(this).text().search(/\((.*?)\)/) != -1 ? new RegExp(/(.*?) \((.*?)\)/) : new RegExp(/.*/);
   	  match = exp.exec($(this).text());
   	  addr = match[1] ? match[1].replace(/ at /,"@").replace(/ dot /g,".") : match[0].replace(/ at /,"@").replace(/ dot /g,".");
   	  emaillink = match[2] ? match[2] : addr;
   	  subject = $(this).attr('title') ? "?subject="+$(this).attr('title').replace(/ /g,"%20") : "";
       $(this).after('<a href="mailto:'+addr+subject+'">'+ emaillink + '</a>');
   		$(this).remove();
   	});
   	
   	//lavalamp
   	$("#home-page #home-nav, #the_marina-page #the_marina-nav, #things_to_do-page #things_to_do-nav, #accommodation-page #accommodation-nav, #functions-page #functions-nav, #events-page #events-nav, #links-page #links-nav, #employment-page #employment-nav, #contact-page #contact-nav").addClass('current');
    $("#navigation").lavaLamp({
      fx: "backout", 
      speed: 500
    });
    
    // sign up default value
    $("#newsletter_signup form input, #enquiry textarea, #enquiry form input").focus(function() {
    	if( this.value == this.defaultValue ) {
    		this.value = "";
    	}
    }).blur(function() {
    	if( !this.value.length ) {
    		this.value = this.defaultValue;
    	}
    });
    //validate no-default
    	jQuery.validator.addMethod("defaultInvalid", function(value, element) {
        return value != element.defaultValue;
      }, "This can't be left blank");
      $("#newsletter_signup form").validate();
      $("#enquiry form").validate();
        
      //featured business cycle
      $("#featured_businesses ul").cycle({
        fx: 'scrollHorz',
        timeout:7000,
        next:   'a#forward', 
        prev:   'a#back',
        pause: 1,
        speed: 1000,
				easing: 'backinout'
      });
      
      $("#latest_news ul").cycle({
        fx: 'scrollHorz',
        timeout:10000,
        pause: 1,
        speed: 2000,
				easing:  'backinout'
      });
      
      $("ul#banner").cycle();
      // click and hover functions for the specials
      $("ul#accommodation li, ul#functions li").click(function(){
      	 window.location=$(this).find("a").attr("href"); return false;
      }).hover(function(){
        $(this).find('h3').addClass('active');
        $(this).addClass('active');
        return false;
      },function(){
        $(this).find('h3').removeClass('active');
        $(this).removeClass('active');
      });

      $('.sf-menu').superfish({autoArrows: false});
      $('.colorbox').colorbox();
      $('.colorbox-frame').colorbox({width: "610px", height: "390px", iframe:true}); 

      $(".month ul.event:odd").addClass("odd");

      //EVENTS CALENDAR SHOW/HIDE
      /*$("ul.event a").click(function() {
        var id = $(this).parents("ul").attr("id").split("event")
        url = "/events/info/" + id[1]
        grabEvent(url)

        return false;
      });

      function grabEvent(event_url) {
        $.ajax({
         type: "GET",
         url: event_url,
         success: function(returnVal){
           $("div.selected-event").html(returnVal)
           $('.colorbox').colorbox()
         }
        });
      }*/
});
