
(function(jQuery,sr){
 
  // debouncing function from John Hann
  // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
  var debounce = function (func, threshold, execAsap) {
      var timeout;

      return function debounced () {
          var obj = this, args = arguments;
          function delayed () {
              if (!execAsap)
                  func.apply(obj, args);
              timeout = null; 
          };
 
          if (timeout)
              clearTimeout(timeout);
          else if (execAsap)
              func.apply(obj, args);
 
          timeout = setTimeout(delayed, threshold || 150);
      };
  }
	// smartresize 
	jQuery.fn[sr] = function(fn){  return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); };
 
})(jQuery,'smartresize');


jQuery.fn.drawBg = function () {
		bgImg=jQuery(this);
		

      // var imgwidth = bgImg.children("img").eq(0).width();
      // var imgheight = bgImg.children("img").eq(0).height();

      //var winwidth = bgImg.parents(window).width() - bgImg.parents("#top").height;7
      var offset = bgImg.offset();
      var winheight = bgImg.parents(window).innerHeight() - offset.top - 6;
 
      //var widthratio = winwidth / imgwidth;
      //var heightratio = winheight / imgheight;
 
      //var widthdiff = heightratio * imgwidth;
      //var heightdiff = widthratio * imgheight;
     

        bgImg.css({
          'height': winheight+'px'
        });

}


jQuery(document).ready(function(){

        jQuery("#slideimage").load(function(){
          jQuery(this).css({"display" : "inline", "visibility" : "visible"});
          jQuery(this).unbind("load");
        });
        setTimeout("startagain()",1000);
        startInterval();
        
});

var counter = 0;
function slideIntro() {
	var images = jQuery("#slideimages a").clone();
	var length = images.length;
	counter = counter+1;
	counter = counter%length;
	
	jQuery("#fullimg img").animate({opacity: 0.01},1400, function(){
		jQuery(this).attr("src", jQuery("img",images).eq(counter).attr("src"));
		jQuery("#fullimg").drawBg();
	});
	jQuery("#fullimg img").animate({opacity: 1},1400);

}

function startagain(){
	  jQuery("#fullimg").drawBg();
          jQuery("#slideimage").css("display", "inline");
          jQuery("#slideimage").unbind("load");
}

function startInterval(){
	jQuery("#fullimg").drawBg();
	setInterval( "slideIntro()", 7000);
}

