/**
 * 
 * Family Bike Gallery
 * 
 */

var familyNextOff, familyNextOn, familyPrevOff, familyPrevOn;
var familyThumbnailCount, familyCurrentPhoto, familyThumbnailDisplayMax;
var familySlideDistance, familyCurrentSlidePosition, familyAnimationInterval, familyPhotoGalleryThumbTitleTopOffset;
var isInferior = (navigator.appName.indexOf('Microsoft') >= 0) && (parseFloat(navigator.appVersion.substring(navigator.appVersion.indexOf('MSIE')+5)) < 8);
var nextOff, nextOn, prevOff, prevOn;
var thumbnailCount, currentPhoto, thumbnailDisplayMax;
var slideDistance, currentSlidePosition, animationInterval, photoGalleryThumbTitleTopOffset;
var hideLargePhoto;

$(document).ready(function() {
	createYouTubeVids();
	$('#twr_modal_window').jqm(); 

});
function closeVideo(){
	
	$('#twr_modal_window').jqmHide(); 
	$('#pics').cycle('resume');
	startSlideshow();
	
}
function watchVideo( videoUrl ){
		stopSlideshow();
		$('#pics').cycle('pause');
		var html = '<object width="400" height="325">'+
  			'<param name="movie" value="http://www.youtube.com/v/u1zgFlCw8Aw&rel=1&color1=0x2b405b&color2=0x6b8ab6&border=1&fs=1"></param>'+
  			'<param name="allowFullScreen" value="true"></param>'+
		  	'<embed id="ytplayer" src="'+ videoUrl+'"'+
    		'type="application/x-shockwave-flash"'+
    		'width="400" height="325" '+
    		'allowfullscreen="true"></embed>'+
			'</object>';
		$('#darkenVideo').empty();
		$(html).appendTo('#darkenVideo');
		//loadVideo(this.href);
		//return false;
		$('#twr_modal_window').jqm({
 
    		modal: true
   
      
    	});

		$('#twr_modal_window').jqmShow(); 
}
function familySlideThumbnail(x) {
	
	// do we need sliding at all?
	if (familyThumbnailCount <= familyThumbnailDisplayMax) {
		return;
	}
	
	if (x == 1) {
		// next
		// is this button active?
		if ($("#familyGalleryNext img").attr("src") == familyNextOn) {
			familyCurrentSlidePosition = familyCurrentSlidePosition + familySlideDistance;
			$("#familyGalleryThumbs").animate({right: familyCurrentSlidePosition}, familyAnimationInterval);
			familyCurrentPhoto++;
			familyUpdateNextPrevious();
		}
		return false;
		
	} else {
		// previous
		if ($("#familyGalleryPrev img").attr("src") == familyPrevOn) {
			familyCurrentSlidePosition = familyCurrentSlidePosition - familySlideDistance;
			$("#familyGalleryThumbs").animate({right: familyCurrentSlidePosition}, familyAnimationInterval);
			familyCurrentPhoto--;
			familyUpdateNextPrevious();
		}
		return false;
	}
}

function familyUpdateNextPrevious() {
	if ((familyThumbnailCount - familyThumbnailDisplayMax) >= familyCurrentPhoto) {
		$("#familyGalleryNext img").attr("src", familyNextOn);
	} else {
		$("#familyGalleryNext img").attr("src", familyNextOff);
	}
	
	if (familyCurrentPhoto > 1) {
		$("#familyGalleryPrev img").attr("src", familyPrevOn);
	} else {
		$("#familyGalleryPrev img").attr("src", familyPrevOff);
	}
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
function showCarouselHover( divId ){
	
	$('#'+divId).show();

}
function hideCarouselHover( divId ){
	$('#'+divId).hide();
}
function createYouTubeVids(){
	
	var count = 0;
	$.getJSON("http://gdata.youtube.com/feeds/api/playlists/8F182B030A1312ED?v=2&alt=json-in-script&callback=?",
        function(data){
          var feed = data.feed;
		  var entries = feed.entry || [];
			
		  for (var i = 0; i < 40; i++) {
		  	var entry = entries[i];
			
		  	if (entry != undefined) {
				
				var title = entry.title.$t;
				var description = entry.media$group.media$description.$t;
				var image = entry.media$group.media$thumbnail[0].url;
				var imageHeight = entry.media$group.media$thumbnail[0].height;
				var imageWidth = entry.media$group.media$thumbnail[0].width;
				if (entry.media$group.media$content[0].url != undefined) {
					$('<div class="bikeInfo"><div class="bikeImageName"><a onmouseout="hideCarouselHover(\'carousel_hover' + i + '\')" onmouseover="showCarouselHover(\'carousel_hover' + i + '\')" class="image" href="javascript: watchVideo(\'' + entry.media$group.media$content[0].url + '\')" ><img width="' + imageWidth + '"height="' + imageHeight + '" src="' + image + '" /></a></div></div>').appendTo('#familyGalleryThumbs');
					$('<div id="carousel_hover' + i + '" class="twr_carousel_popup"><div class="twr_carousel_popup_inner"><h5>' + title + '</h5><p>' + description + '</div></div>').appendTo('#familyGalleryHovers');
					
					count++;
				}
			}
			
		  }
		  familyThumbnailCount = count;
		 
		 turnOn();

        });
		
	
}
function turnOn(){

	familyCurrentSlidePosition = 0;
	familyCurrentPhoto = 1;
	// activate next button with correct state
	if (familyThumbnailCount > familyThumbnailDisplayMax) {
		$("#familyGalleryNext img").attr("src", familyNextOn); 
	} else {
		$("#familyGalleryNext img").attr("src", familyNextOff);
	}
	
	// activate off state of previous button
	$("#familyGalleryPrev img").attr("src", familyPrevOff);
	
	// add onclick functionality
	$("#familyGalleryPrev").click(function() { familySlideThumbnail(-1); $(this).blur(); return false; });
	$("#familyGalleryNext").click(function() { familySlideThumbnail(1); $(this).blur(); return false; });

	// add mouseover/out to bike images
//	$("div.bikeImageName a").hover(function(){
//		currentElementId = $(this).attr("id");
//		hoverContents = $(this).attr("rev");
//		$("#familyGalleryHoverInner").html(hoverContents); // inject contents
//		var anchorPosition = findPos(this); // find anchor position
//		var wrapperPosition = findPos(document.getElementById('wrapper'));
//		$("#familyGalleryHover").show();
//		
//		if (document.getElementById('familyGalleryHover')) {
//			var hoverBoxHeight = document.getElementById('familyGalleryHover').offsetHeight; // find height of box
//
//			// show to the left, or to the right?
//			//if (anchorPosition[0] - wrapperPosition[0] >= 700) {
//				//document.getElementById('familyGalleryHover').style.left = anchorPosition[0] - 150 + 'px';
//				//$("familyGalleryHover").css("left", anchorPosition[0] - 100);
//				//$("#familyGalleryHoverBottomShadow").removeClass("left");
//				//$("#familyGalleryHoverBottomShadow").addClass("right");
//			//} else {
//				document.getElementById('familyGalleryHover').style.left = anchorPosition[0] -450 + 'px';
//				$("#familyGalleryHoverBottomShadow").removeClass("right");
//				$("#familyGalleryHoverBottomShadow").addClass("left");
//			//}
//			
//			if (isInferior) {
//				document.getElementById('familyGalleryHover').style.left = anchorPosition[0] - 290 + 'px';
//				document.getElementById('familyGalleryHover').style.top = '-185px';
//			} else {
//				document.getElementById('familyGalleryHover').style.top = '-185px';
//			}
//			
//		}
//
//	},function(){
//		$("#familyGalleryHover").hide();
//	});

}
function darkenScreen() {
	$("#darkenDiv").show();
	$("#darkenContents").show();
	
	var theBody = document.getElementById("body");
	$("#darkenDiv").css("height", theBody.scrollHeight + 135);

	// create hit area for closing darkened area
	$("#darkenDiv").click(function() {
		clearDarkenScreen();
	});
	return false;
}

function clearDarkenScreen() {
	$("#darkenContents").hide();
	$("#darkenDiv").hide();
	return false;
}

function loadVideo(url) {
	return;
}
