
var lastaction;
var refreshing = 5*1000; // 60 * 1000 = 1 Minute (1000 = millisekunden, 60 = sekunden)
var refreshId=0;

$(document).ready(function() {
   $('a.showBoxy').each(function() {
       var title = $(this).attr("title");
       $(this).boxy({
            closable:true,
            title:title,
            clickToFront: true
        });
   });


   datepicker_init();

   
   $('form.enquireform > input.submit').click(function(e) {
      e.stopPropagation();
      if (_formValidation($(this).parent())==true) { return false; }
      $(this).parent().submit();
   });
   
   lastaction = new Date().getTime();
	if ($('.galleryimages')) {
		$('.galleryimages a:last').addClass("last");
	}
	
	var imgnav = new image_navigation();
     imgnav.init();
     
    $('#badgeshower').click(function() {
    	$('#offers').toggle();
    	return false;
    });
    
    $('.offerpreview').click(function() {
    	var me = $(this);
    	$('#offer_full').slideUp(function() {
    		$('#offer_full').html($('.html',$(me).parent()).html()).slideDown();
    	});
    	$('.offerpreview').removeClass("active");
    	$(this).addClass("active");
    	return false;
    });
     
});

var image_navigation = function() {
   var me = this;
   this.init = function() {   		
   		$('.galleryimages a:first').addClass("active");
   		$('.galleryimages a').click(function() {
   			setRefreshTimeout();
   			if (!$(this).hasClass("active")) {
   				//$('div.loader').show();
   				
   				$('.galleryimages a').removeClass("active");
   				$(this).addClass('active');
   				
   				var src_new = $(this).attr("href");
                changePicture(src_new);
   			}
   			return false;
   		});
   		setRefreshTimeout();
   }
}
function changePicture(src_new) {
	$('#the_big_image').fadeOut(100,function() {
        var img_new = new Image();
        $(img_new)
        .hide()
        .load(function() {
          	$('#the_big_image').attr("src",src_new);
          	$('#the_big_image').fadeIn(1200,function() {
          		$('#the_big_image2').attr("src",src_new);
          	});
        })
        .attr("src",src_new);
    });
}
function nextImage() {
	var actImage = $('.galleryimages a.active');
	$(actImage).removeClass("active");
	if ($(actImage).hasClass("last")) {
		$('.galleryimages a:first').click();
	} else {
		$(actImage).next().click();
	}
	setRefreshTimeout();
}

function setRefreshTimeout(first) {
	if (refreshId!=0) {
		clearTimeout(refreshId);
	}
	if (first=="1") {
		refreshId = setTimeout("nextImage()",0);
	} else {
		refreshId = setTimeout("nextImage()",refreshing);
	}
}

function datepicker_init() {
   // datepickers in contact-form:
   $('input.datepicker').each(function() {
       $(this).datepicker({showButtonPanel: false, dateFormat: "yy-mm-dd"});
   });
}

function _formValidation(form) {
    var ret = false;
    $(".required",$(form)).each(function() {
        if ($(this).val()=="") {
            $(this).css("background","#eee").css("border","2px solid #f00");
           ret = true;
        }
    });
    return ret;
}

