var scrolloffset = 0;
var scrollint = 0;
var totalphotos = 0;
var duraint = 400;
var clipInt;
var isIE6 = false;
var img;
var thumbslider;
var tabs;

$(document).ready(function(){
	isIE6 = (jQuery.browser.msie && jQuery.browser.version.charAt(0) == '6') ? true : false;
	img = $('.main_image');
	thumbslider = $('.thumb_slider');

	var $kids = $('.photo_thumb');
	if($kids.get(0))
	{
	   scrolloffset = $kids.get(0).offsetWidth;	
	}
	var totalphotos = $kids.length;
	var totalwidth = Math.max((totalphotos*scrolloffset), $('.gallery_thumbs').width());
	
	if($kids.length == 1) {
		//If we don't have more than one image to show hide the controls and exit the function
		$('.gallery_controls').hide();
		return;
	}
	
	thumbslider.width(totalwidth+((isIE6 == true) ? 3 : 0));
	
	if(isIE6 == true){
		//IE6 doesn't understand min-height so we have to calculate it on the fly
		var clipImg = function(){
			$(this).height('');
			var offset = parseInt($(this).css('margin-top')) + parseInt($(this).css('margin-bottom'));
			var container = ($('.gallery_main_view').height()-$('.photo_caption').height());
			if(($(this).height()+offset) > container) $(this).height(container-offset);
			$(this).css({visibility:''});
		};
		img.load(clipImg);
	}
	
	tabs = $('.gallery_tabs');
	if(tabs.length){
		var tabchildren = tabs.children('a').each(function(index){
			var tabclick = function(){
				var title_str = $(this).get(0).title.toLowerCase();
				$(this).addClass(title_str.substr(0,1)+'active');
				$(this).siblings('a').each(function(){$(this).removeClass($(this).get(0).title.substr(0,1).toLowerCase()+'active')})
				if(title_str == "video"){
					$('.gallery_controls').css({visibility:'hidden'});
					$('.gallery_main_view').addClass('videoview');
				}else if(title_str == "photos"){
					$('.gallery_controls').css({visibility:''});
					$('.gallery_main_view').removeClass('videoview');
				}
				return false;
			}
			$(this).click(tabclick);
			$(this).focus(function(){
				this.hideFocus=true;
			});
		});
	}
});
 
function moveRight(){
	var pLeft = thumbslider.position().left;
	thumbslider.stop();
	
	parentwidth = $('.gallery_thumbs').width();

	if(isIE6 && (pLeft == 0 && thumbslider.width() == parentwidth+3)) return false;

	newleft = Math.max(pLeft-scrolloffset, (thumbslider.width()-parentwidth)*-1);
	thumbslider.animate({left: newleft+"px"}, {easing:'linear',queue:false,duration:duraint} );
}
function moveLeft(){
	var pLeft = thumbslider.position().left;
	thumbslider.stop();
	
	newleft = Math.min(0, pLeft+scrolloffset);
	thumbslider.animate({left: newleft+"px"}, {easing:'linear',queue:false,duration:duraint} );
}
function showmyphoto(el) {
	if(isIE6 == true) img.css({visibility:'hidden'});
	$('.photo_caption').text(el.firstChild.alt);
	var newImageSrc = el.firstChild.src;
	img.get(0).src = newImageSrc;
}