// remap jQuery to $
(function($){

})(window.jQuery);





$(document).ready(function(){
	
	var FloatingMenuName = "#property_cats";  
	var FloatingMenuYlocation = null;

    //FloatingMenuYlocation = parseInt($(FloatingMenuName).css("top").substring(0,$(FloatingMenuName).css("top").indexOf("px")));
    $(window).scroll(function () {
        var FloatingMenuOffset = FloatingMenuYlocation+$(document).scrollTop()+"px";  
        $(FloatingMenuName).animate({top:FloatingMenuOffset},{duration:200,queue:false});  
    }); 






	var interval = 0;
	
	$('.fs_left').css({'opacity':'0.1'});
	$('.fs_right').css({'opacity':'0.1'});
	
	$(function() {
		var _totalCount = parseInt(jQuery('#_total').text());
		if (_totalCount>3) {
			$('.fs_right').css({'opacity':'1.0'});
		}
	});
	
	
	
	

	$('.fs_right').bind('click', function() {
		imageClickForward();
	});
	
	
	
	$('#property_cats').localScroll();
	
	// fix height on property thumbs
	getMaxHeight($(".property_container .fs_slide"));
	
	
	$('#property_thumb_container img').bind('click', function() {
		toggleThumb($(this).attr('id'));
	});
	





});





toggleThumb = function(xImage) {

	var _current = parseInt(jQuery('#_current').text());
	var _thumb = xImage.split('_');
	
	if (_current != _thumb[1]) {
		$('#property_main_image img').each(function(index) {
			if ($(this).css('display') == 'block') {
				$(this).fadeOut();
			}
			if ($(this).attr('id') == 'big_'+_thumb[1]) {
				jQuery('#_current').text(_thumb[1])
				$(this).fadeIn();
			}
	  	});
  	}
	
}

startHeaderRotation = function() {
  interval = setInterval('autoRotate()',5000);
}


imageClickForward = function() {

	if (!arguments[0]) {
		// Kill Auto ::
		clearInterval(interval);
	}

	$(".fs_right").unbind('click');	
	$(".fs_left").unbind('click');
	
	var _totalCount = parseInt(jQuery('#_total').text());
	var _current = parseInt(jQuery('#_current').text());
	var _nextImage = _current+1;	
	var _endslide = _nextImage+2;
	
	var _marginIs = parseInt($('.fs_container').css('margin-left'));
	var _setMargin = parseInt(_marginIs-267);
	
	$('.fs_container').animate({marginLeft:_setMargin+'px'}, 600, function() {
    	// Animation complete.
    	$('#_current').text(_nextImage);
    	if (_nextImage>1) {
			enableBackward();
		}
		if (_endslide == _totalCount) {
			disableForward();
		} else {
			enableForward();
		}
  	});

}

imageClickBackward = function() {

	clearInterval(interval);

	$(".fs_left").unbind('click');
	$(".fs_right").unbind('click');	

	var _totalCount = parseInt(jQuery('#_total').text());
	var _current = parseInt(jQuery('#_current').text());
	var _nextImage = _current-1;	
	var _marginIs = parseInt($('.fs_container').css('margin-left'));
	var _setMargin = parseInt(_marginIs+267);

	$('.fs_container').animate({marginLeft:_setMargin+'px'}, 600, function() {
    	// Animation complete.
    	$('#_current').text(_nextImage);
    	if (_nextImage==1) {
    		disableBackward();
    		enableForward();
		} else {
			enableBackward();
			enableForward();
		}
  	});

}

autoRotate = function() {

	$(".fs_left").unbind('click');
	$(".fs_right").unbind('click');

	var _totalCount = parseInt(jQuery('#_total').text());
	var _current = parseInt(jQuery('#_current').text());
	var _nextImage = _current-1;	
	var _endslide = _nextImage+3;
	
	if (_endslide == _totalCount) {
		imageRewind();
	} else {
		imageClickForward('auto');
	}
	
}

imageRewind = function() {
	disableBackward();
	$('.fs_container').animate({marginLeft:'0px'}, 600, function() {
    	// Animation complete.
    	$('#_current').text('1');
    	enableForward();
  	});

}

enableForward = function() {
	$('.fs_right').css({'opacity':'1.0'});
	$('.fs_right').bind('click', function() {
		imageClickForward();
	});
}
disableForward = function() {
	$(".fs_right").unbind('click');	
	$('.fs_right').css({'opacity':'0.1'});
}
enableBackward = function() {
	$('.fs_left').css({'opacity':'1.0'});
	$('.fs_left').bind('click', function() {
		imageClickBackward();
	});
}
disableBackward = function() {
	$(".fs_left").unbind('click');	
	$('.fs_left').css({'opacity':'0.1'});
}


getMaxHeight = function(group) {
   _maxHeight = 0;
   group.each(function() {
      _thisHeight = jQuery(this).height();
      if(_thisHeight > _maxHeight) {
         _maxHeight = _thisHeight;
      }
   });
   group.height(_maxHeight);
}
