var iDidThisPhotos = new Array();
var iDidThisVideos = new Array();

function RateItem(itemType, itemID, positive, callBack)
{
	$.getJSON("ajax/Common.aspx", 
		{ 
			method: "RateBinary", 
			itemID: itemID, 
			itemTypeID: itemType, 
			positive: positive
		},
		function(data)
		{
		   callBack(data); 
		}
	);
	
	return false;
}

function PostComment(itemType, itemID, text, callBack)
{
	$.getJSON("ajax/Common.aspx", 
		{ 
			method: "PostComment", 
			itemID: itemID, 
			itemTypeID: itemType,
			comment: text
		},
		function(data)
		{
		   callBack(data); 
		}
	);
	
	return false;
}

function DisplayRating(ratings)
{
	var span = $(".thumbsRating .details span");
	span.eq(0).html(ratings.positiveRatingCount + " Yes");
	span.eq(1).html(ratings.negativeRatingCount + " No");
}

function DisplayComment(comment)
{
	//alert(comment.toSource());
	/*console.log(comment);*/
	if (comment.error) { 
		$('div.postComment').append("<div class='error-msg' style='clear: both'>"+comment.error+"</div>");
	} else {
		var content = '<li class="FLC"><a class="userAvatar avatarSmall" rel="nofollow" href="/members/'+comment.Username+'.html" title="'+comment.Username+'"><img alt="'+comment.Username+'" src="'+comment.UserAvatarURL+'" /></a><h3 class="Heading5a"><a rel="nofollow" href="/members/'+comment.Username+'.html" title="'+comment.Username+'">'+comment.Username+'</a> said</h3><div class="options"><div class="Stars"></div></div><div class="content"><span class="tick"></span><span>on '+comment.CreatedDate+'</span> '+comment.Text+'</div></li>';
		if ($(".box10Content .Comments").length > 0) {
			$(".commentList").prepend(content);
			$(".commentList li:first").hide().slideDown('normal');
		} else {
			$(".postComment").before('<div class="Comments"><ul class="commentList">'+content+'</ul></div>');
			$(".commentList li:first").hide().slideDown('normal');
		}
	}
}

//video switcher
(function($) {
	var currentIndex = 0;
	var videoCount = 0;
	var leftNav;
	var rightNav;
	var videoList;
	
	var updateNav = function() {
		leftNav.fadeTo("fast", (currentIndex <= 0)?0.3:1.0);
		rightNav.fadeTo("fast", (currentIndex == (videoCount -1))?0.3:1.0);
	}
	
	var changeVideo = function(clickedIndex) {
		if(clickedIndex < 0 || clickedIndex > videoCount -1) {
			return;
		}
		videoUrl = iDidThisVideos[clickedIndex];
		currentIndex = clickedIndex;
		swapVideo(videoUrl);
	}
	
	var swapVideo = function(videoUrl) {
		$("#currentVid .vid").fadeOut(function() {
			var h = 295;
			var padding = 50;
			$videoContainer = $(this);
			$videoContainer.find("object").remove();
			/*$videoContainer.html('<embed width="480" height="295" align="TL" flashvars="id='+iDidThisVideos[currentIndex]+'&amp;partnerId=3&amp;embedvars=http%3A%2F%2Flocal3.ehow.com%2Fembedvars.aspx%3FisEhow%3Dtrue%26show_related%3Dfalse%26from_url%3Dhttp%253a%252f%252fwww.ehow.com%252fhow-did_85908_cool-video-did.html" scale="noScale" allowfullscreen="true" wmode="transparent" menu="false" loop="false" allowscriptaccess="always" quality="high" bgcolor="#000000" name="VideoPlayer" id="VideoPlayer" src="/flash/player.swf" type="application/x-shockwave-flash"/>').hide();*/
			dlabs.objects.videoSWFObject = new SWFObject('/flash/player.swf', "VideoPlayer", "480", "295", "9", "CCCCCC");

			dlabs.objects.videoSWFObject.addParam("allowScriptAccess", "always");
			dlabs.objects.videoSWFObject.addParam("loop", "false");
			dlabs.objects.videoSWFObject.addParam("menu", "false");
			dlabs.objects.videoSWFObject.addParam("bgcolor","#000000");
			dlabs.objects.videoSWFObject.addParam("wmode", "transparent");
			dlabs.objects.videoSWFObject.addParam("allowFullScreen","true");
			dlabs.objects.videoSWFObject.addParam("scale","noScale");
			dlabs.objects.videoSWFObject.addParam("align","TL");

			dlabs.objects.videoSWFObject.addVariable("id", iDidThisVideos[currentIndex].toString());
			dlabs.objects.videoSWFObject.addVariable("partnerId", 3);
			dlabs.objects.videoSWFObject.addVariable("embedvars", encodeURIComponent(location.protocol + '//' + dlabs.settings.domain + '/embedvars.aspx?isEhow=true&show_related=false&from_url=' + '<%= HttpUtility.UrlEncode(Urls.ConvertToProductionUrl(Urls.SiteUrl + Request.RawUrl)) %>'));
			
			dlabs.objects.videoSWFObject.write("videoContainer");
			$videoContainer.hide();
			
			$videoContainer.animate({ height: h + padding }, "fast", function() {
				$videoContainer.find("object").fadeIn();
				updateNav();
				var videoCaption = videoList.eq(currentIndex).attr("title");
				if (videoCaption == "") {
					$("#videoCurrentCaption").slideUp('fast');
				} else {
					$("#videoCurrentCaption").html(videoCaption + "&nbsp;").slideDown('fast');
				}
			});
		});
	}
	
	$(function() {
		videoList = $("#videoThumbnailPic ul li img");
		videoCount = videoList.length;
		
		if($("#currentVideo .featured").length != 0) {
			currentIndex = -1;
		}
		
		videoList.bind("click", function() {
			var clickedIndex = videoList.index(this);
			changeVideo(clickedIndex);
		});
		
		leftNav = $("#currentVid .goLeft").bind("click", function() {
			changeVideo(currentIndex -1);
		});
		
		rightNav = $("#currentVid .goRight").bind("click", function() {
			changeVideo(currentIndex + 1);
		});
		updateNav();
		if($("#videoCurrentCaption").text() == "") {
			$("#videoCurrentCaption").hide();
		}
	});
})(jQuery);

//thumblist photos
(function($) {
	var currentIndex = 0;
	var imageCount = 0;
	var leftNav;
	var rightNav;
	var imgList

	var updateNav = function() {
		leftNav.fadeTo("fast", (currentIndex <= 0)?0.3:1.0);
		rightNav.fadeTo("fast", (currentIndex == (imageCount - 1))?0.3:1.0);
	}

	var changePhoto = function(clickedIndex) {
		if (clickedIndex < 0 || clickedIndex > imageCount - 1)
			return
		var largePhotoUrl = iDidThisPhotos[clickedIndex];
		currentIndex = clickedIndex;
		swapPhoto(largePhotoUrl);
	}

	var swapPhoto = function(largePhotoUrl) {
		var img = new Image();
		$(img).load(function() {
			var h = img.height;
			var padding = 50;
			$("#currentPic .img").fadeOut(function() {
				var $imgContainer = $(this);
				$imgContainer.parent().parent().animate({ height: h + padding }, "fast", function() {
					$imgContainer.html(img).hide().fadeIn();
					updateNav();
					var largePhotoCaption = imgList.eq(currentIndex).attr("title");
					if (largePhotoCaption == "") {
						$("#currentCaption").slideUp('fast');
					} else {
						$("#currentCaption").html(largePhotoCaption + "&nbsp;").slideDown('fast');
					}
				});
			});
		});
		img.src = largePhotoUrl;

	};

	$(function() {
		imgList = $("#thumbnailPic ul li img");
		imageCount = imgList.length;

		if ($("#currentPic .featured").length != 0) {
			currentIndex = -1;
		}
	
		imgList.bind("click", function() {
			var clickedIndex = imgList.index(this);
			changePhoto(clickedIndex);
		});

		leftNav = $("#currentPic .goLeft").bind("click", function() {
			changePhoto(currentIndex - 1);
		});

		rightNav = $("#currentPic .goRight").bind("click", function() {
			changePhoto(currentIndex + 1);
		});
		updateNav();
		if ($("#currentCaption").text() == "") {
			$("#currentCaption").hide();
		}

		var resize = function($img) {
			var h = $img[0].height;
			var padding = 50;
			$img.parent().parent().parent().css("height", h + padding);
		}

		var currentImg = $("#currentPic img");
		if(currentImg.length > 0) {
			if (currentImg[0].complete) {
				resize(currentImg);
			} else {
				currentImg.load(function() {
					resize(currentImg);
				});
			}
		}
	});
}(jQuery));
d = {};
//photo view toggle
(function($) {
	var sliderView;
	var thumbView;
	$(function() {
		sliderView = $("#photos .sliderView");
		thumbView = $("#photos .thumbnailView");
		$(".changeView .slider").bind("click", function() {
			if (sliderView.css("display") == "none") {
				thumbView.fadeOut(function() {
					sliderView.fadeIn();
				});
			}
		});
		$(".changeView .thumbs").bind("click", function() {
			if (thumbView.css("display") == "none") {
				sliderView.fadeOut(function() {
					thumbView.fadeIn(function() {
						$("#thumbnailView img").centerImage();
					});
				});
			}
		});
	});
}(jQuery));

jQuery(function($) {
	(function(){
		var scrollWindowOptions = {
			itemList: $("#thumbnailPic").children(".thumbnailList"),
			marginWidth: 23,
			itemsPerView: 4,
			navLeft: $("#thumbnailPic .goLeft"),
			navRight: $("#thumbnailPic .goRight"),
			shadedOpacity: '.3'
		};
		
		dlabs.objects.scrollWindow = new scrollWindow(scrollWindowOptions);
		
		var scrollWindowOptions = {
			itemList: $("#videoThumbnailPic").children(".thumbnailList"),
			marginWidth: 23,
			itemsPerView: 4,
			navLeft: $("#videoThumbnailPic .goLeft"),
			navRight: $("#videoThumbnailPic .goRight"),
			shadedOpacity: '.3'
		};
		
		dlabs.objects.scrollWindow = new scrollWindow(scrollWindowOptions);
	})();

	$(".lightbox").lightbox({ fitToScreen: true });
	$("#readMoreLink").bind("click", function() {
		var href = $(this);
		var textElement = href.prev();
		var readmore = $("#readMore").html();
		href.remove();
		var txt = textElement.text();
		textElement.text(txt.substr(0, txt.length-3)+" ");
		textElement.append(readmore);
	});

	var tooltip = "An \"I Did This\" is a way to show a project in action.  If you're planning to do something, click <strong>I Did This</strong> on the right side of any article page to save the project to your drafts folder.  (If you're not a member, that's ok, go ahead and <strong>Register</strong> first, it's fast and free!)<br/><br/>As you're completing your project, be sure to take pictures along the way.  Then, just come back to the \"I Did This\" section of your profile when you're ready to share your story with everyone!";

	$('<a class="toolTip">What&rsquo;s&nbsp;this?</a>')
		.insertAfter('.DidThisButton')
		.tooltip(tooltip);
});
