/*
 * jQuery ResizeGalleryContents
 * @author www.designunderstood.com
 * @version 1.0.1
 * @date Feb, 2011
 * @category jQuery plugin
 * @copyright (c) 2011 (www.designunderstood.com)
 */
							
function getWindowHeight() {

	var win = $(window).height();

	return win; 
}
 
function GetContentHeight() {
	return getWindowHeight();
}

function GetContentWidth() {
	return $(window).width() + 1000;
}


/***************/
						
							
$(document).ready(function() {
//$(window).load(function () {
	
	///Load InfiniteCarousel2
	//$(window).load(function () {
		$(function(){
			$('#carouselbb').infiniteCarousel();
		});
	//});
	
						   
	ResizeGalleryContents();

	var window_resize_timeout;

	function window_resize_callback() {

		ResizeGalleryContents();
		thumbRowSet()

		$('#carouselbb').stop().scrollTo($('#carouselbb ul li').eq(idx));
	
	}; 

	$(window).resize(function () {
		clearTimeout(window_resize_timeout);
		window_resize_timeout = setTimeout(window_resize_callback, 55);
	});
	
$(document).ready(function() {
	// as images load, resize them to fit the gallery size
	var gallery_width = $('#carouselbb').width() + 0;
	var gallery_images = $('#carouselbb li img');
	gallery_images.hide();
	gallery_images.load(function() {
		$(this).data('size_ratio', $(this).width() / $(this).height());
		ResizeSliderImages($(this), gallery_width);
		$(this).show();
	});
});


	// Set Scrolling
	var idx = 0;
	if ( $('#carouselbb ul li').length ) {
		idx = $('#carouselbb ul li').index($('#carouselbb ul li'));
	};

	$('#carouselbb').scrollTo($('#carouselbb ul li').eq(idx));

	//$('#gallery .next-g').live('click', function() {
	$('#gallery').live('click', function() {
		idx++;
		if( idx == $('#carouselbb ul li').length) {
			$('#carouselbb').stop().scrollTo($('#carouselbb ul li'));
			idx = 0;
		} else {
			$('#carouselbb').scrollTo($('#carouselbb ul li').eq(idx), 500);
			return false;
		}
	});

	//$('#gallery .prev-g').live('click', function() {
	$('#gallery').live('click', function() {
		idx--;
		if( idx == -1 ) {
			idx = $('#carouselbb ul li').length - 1;
			$('#carouselbb').stop().scrollTo($('#carouselbb ul li').eq($('#carouselbb ul li').length - 1));
		} else {
			$('#carouselbb').scrollTo($('#carouselbb ul li').eq(idx), 500);
			return false;
		}
	});

		
	///end document ready function
});
					
jQuery.event.add(window, "load", ResizeGalleryContents);

jQuery.event.add(window, "load", thumbRowSet);
jQuery.event.add(window, "resize", thumbRowSet);


	
						 
function thumbRowSet() {			
		//// set position of thumb row     ///////#carouselbb li img
		var thumbhigh = $('#carouselbb li img').height() + 94;
		var thumrow = thumbhigh;
		$('.thumb-holder').css('top', $(this).scrollTop() + thumrow + "px"); 
		
		///add left padding
		var curImgWidth = $('#carouselbb li img').width();
		var tempSize = 1000;
		var curImgWidthCalc = (tempSize - curImgWidth);
		var DifimgWith = (curImgWidthCalc / 2);
		if (DifimgWith >= 0) { DifimgWith = DifimgWith; } else { DifimgWith = 0; }
		$('.thumb-insideholder').css('padding-left', DifimgWith + "px"); 
		
		//// set height of side arrows
		var arrowCol = thumrow - 120;
		$('.rightBut').height(arrowCol + "px"); 
		$('.leftBut').height(arrowCol + "px"); 
		
		////Nov 2012 update: set Width of side arrows
		var percentgap = ( (curImgWidth/100) * 20)
		var leftrightwidth = ((curImgWidth-percentgap)/2);
		$('.rightBut').css('width', leftrightwidth + "px"); 
		$('.leftBut').css('width', leftrightwidth + "px"); 
		////Nov 2012 update: set Pos of side arrows
		var leftrightpos = (DifimgWith + 20);
		$('.rightBut').css('right', leftrightpos + "px");
		$('.leftBut').css('left', leftrightpos + "px");
	};

function ResizeSliderImages(images, li_w) {
	
	/// create a space below the images for thumbnails
	if ($('#carouselbb li img').width() > 1000) {
		if ( $(window).height() > ($('#carouselbb li img').height() + 162) ) {
			li_h = $('#carouselbb li img').height();
			//alert ("1");
		} else { 
			li_h = $('#carouselbb li img').height() - 162;
			//alert ("2");
		};
	} else { 
		li_h = $(window).height() - 162;
		//alert ("3");
	};

	images.each(function() {
		var r = $(this).data('size_ratio');
		var w = li_w, h = li_h;
		
		if (!r) { return; };

		if ( r > 1) { // width > height
			h = w/r;
			if ( h > li_h ) {
				h = li_h;
				w = h*r;
			};
		} else {
			w = r*h;
		}
		$(this).width(w).height(h);

	});
}


function ResizeGalleryContents() {
	
	/// create a space below the images for thumbnails
	if ($('#carouselbb li img').width() > 1000) {
		if ( $(window).height() > ($('#carouselbb li img').height() + 162) ) {
			li_h = $('#carouselbb li img').height();
		} else { 
				li_h = $('#carouselbb li img').height() - 162;
		};
	} else { 
			li_h = $(window).height() - 162;
	};
	
	
	if ( $('#gallery').length ) {
		// set gallery and sub-nav size
		//$('#gallery').height(GetContentHeight()-30);
		$('#gallery').height(li_h);
		// set image and container size
		var li_w = $('#gallery').width()+0;
		var li_h = $('#gallery').height()+162; //set the #gallery back to full size (so it doesnt crop images)
		
		$('#carouselbb').height(li_h).width(li_w); //$(window).height() - 106);

		$('#carouselbb li').width(li_w).height(li_h); //.css('text-align', 'center');
			ResizeSliderImages($('#carouselbb li img'), li_w);

		$('#carouselbb ul').width(GetContentWidth() * $('#carouselbb img').length);
	} else {
		$('#carouselbb').height($(window).height() - 0);
	}

}




/*$(window).load(function() {
						
//// set position of thumb row     ///////#carouselbb li img
		var thumbhigh = $('#carouselbb li img').height() + 105;
		var thumrow = thumbhigh;
		$('.thumb-holder').css('top', $(this).scrollTop() + thumrow + "px"); 

	
});*/

