$(function() {
	var video_gallery_thumb_set_top = "-100px";
	var video_animate = true;
	var video_current_image = 0;
	var video_next_image = 0;
	var video_images = null;
	var video_in_animation = false;
	var video_element_height = $(window).height()-45;
	var video_element_width = $(window).height();
	var video_gallery_large = $("#video-gallery-large");

	//$("#video-gallery-large object").css("height", video_element_height+'px');
	$(window).resize(function() {
		var video_element_height = $(window).height()-45;
		var video_element_width = $(window).height();
		//$("#video-gallery-large object").css("height", video_element_height+'px');
	});
	$("#video-gallery-toggle-thumbs-close").click(function() {
		video_gallery_hide();
	});
	$("#video-gallery-toggle-thumbs").click(function() {
		var parent = $(this).parent("div");
		if(parent.css("top") == video_gallery_thumb_set_top) {
			video_gallery_show_thumbs();
		} else {
			video_gallery_hide_thumbs(video_gallery_thumb_set_top);
		}

	});
	$("#video-gallery-toggle-right").live('click', function() {
		video_change_image("right");
	});
	$("#video-gallery-toggle-left").live('click', function() {
		video_change_image("left");
	});


	$("#video-thumb-data-li li img").live('click' , function() {
		//$("#video-gallery-large img").attr("src", $(this).attr("src"));
                var data = $(this).attr("title");
                $("#video-gallery-large div").html("<!-- START OF THE PLAYER EMBEDDING TO COPY-PASTE -->" +

                                "<object id=\"player\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" name=\"player\" > "+
                                        "<param name=\"movie\" value=\"/player.swf\" /> "+
                                        "<param name=\"allowfullscreen\" value=\"true\" /> "+
                                        "<param name=\"allowscriptaccess\" value=\"always\" /> "+
                                        "<param name=\"flashvars\" value=\"file="+"/images/activities/videos/"+data+"&image=/preview.jpg\" /> "+
                                        "<embed "+
                                                "type=\"application/x-shockwave-flash\" "+
                                                "id=\"player2\" "+
                                                "name=\"player2\" "+
                                                "src=\"player.swf\" "+
                                                "width=\"328\" "+
                                                "height=\"200\" "+
                                                "allowscriptaccess=\"always\" "+
                                                "allowfullscreen=\"true\" "+
                                                "flashvars=\"file="+"/images/activities/videos/"+data+"&image=/preview.jpg\" "+
                                        "/> "+
                                "</object> "+
                                "<!-- END OF THE PLAYER EMBEDDING --> ");

		video_current_image = ($(this).attr("id"))-0;

		//video_gallery_hide_thumbs(video_gallery_thumb_set_top);
	});
	$(".video-gallery-open").click(function() {
		$("#video-gallery-thumbs").css("top", video_gallery_thumb_set_top);
		$("#video-gallery-toggle-thumbs").html('<img src="/images/gallery-open.gif">');

		var href = $(this).attr("href");
		$("#video-gallery").css("display", "block");
		$.ajax({
		  url: href,
		  success: function(data) {
		  	video_images = data;
		  	video_current_image = 0;
		  	var thumb_data = $("#video-gallery-thumb-data");
		  	thumb_data.html('<ul id="video-thumb-data-li"></ul>');
		  	var thumb_li   = $("#video-thumb-data-li");
		  	
		  	$.each(data, function(index, e) {
		  		
			 thumb_li.append('<li><img height="100" id="'+index+'" title="'+data[index]['file']+'" src="/images/none.jpg"></li>');
			});
			//$("#video-gallery-large img").attr("src", "/video_images/activities/video_images/"+data[0]['file']);
                        //$("#video-gallery-large div").html(data[0]['file']);
                        $("#video-gallery-large div").html("<!-- START OF THE PLAYER EMBEDDING TO COPY-PASTE -->" +

                                "<object id=\"player\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" name=\"player\" > "+
                                        "<param name=\"movie\" value=\"/player.swf\" /> "+
                                        "<param name=\"allowfullscreen\" value=\"true\" /> "+
                                        "<param name=\"allowscriptaccess\" value=\"always\" /> "+
                                        "<param name=\"flashvars\" value=\"file="+"/images/activities/videos/"+data[0]['file']+"&image=/preview.jpg\" /> "+
                                        "<embed "+
                                                "type=\"application/x-shockwave-flash\" "+
                                                "id=\"player2\" "+
                                                "name=\"player2\" "+
                                                "src=\"/player.swf\" "+
                                                "width=\"328\" "+
                                                "height=\"200\" "+
                                                "allowscriptaccess=\"always\" "+
                                                "allowfullscreen=\"true\" "+
                                                "flashvars=\"file="+"/images/activities/videos/"+data[0]['file']+"&image=/preview.jpg\" "+
                                        "/> "+
                                "</object> "+
                                "<!-- END OF THE PLAYER EMBEDDING --> ");
		  }
		});
	});

	function video_change_image(direction) {

		if(direction == 'left') {
			video_next_image = (video_current_image)-1;
			video_next_image = video_next_image
			if(video_next_image < 0) {
				video_next_image = (video_images.length-1);
			}

			video_current_image = video_next_image;
			if(video_animate) {
				if(!video_in_animation) {
					video_in_animation = true;
					video_gallery_large.animate({"left":"-"+(video_element_width+500)+"px"}, 400);
					video_gallery_large.queue(function() {
						//$("#video-gallery-large img").attr("src", "/video_images/activities/video_images/"+);
                                                //$("#video-gallery-large div").html(video_images[video_next_image]['file']);
                                                $("#video-gallery-large div").html("<!-- START OF THE PLAYER EMBEDDING TO COPY-PASTE -->" +

                                                    "<object id=\"player\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" name=\"player\" > "+
                                                            "<param name=\"movie\" value=\"/player.swf\" /> "+
                                                            "<param name=\"allowfullscreen\" value=\"true\" /> "+
                                                            "<param name=\"allowscriptaccess\" value=\"always\" /> "+
                                                            "<param name=\"flashvars\" value=\"file="+"/images/activities/videos/"+video_images[video_next_image]['file']+"&image=/preview.jpg\" /> "+
                                                            "<embed "+
                                                                    "type=\"application/x-shockwave-flash\" "+
                                                                    "id=\"player2\" "+
                                                                    "name=\"player2\" "+
                                                                    "src=\"/player.swf\" "+
                                                                    "width=\"328\" "+
                                                                    "height=\"200\" "+
                                                                    "allowscriptaccess=\"always\" "+
                                                                    "allowfullscreen=\"true\" "+
                                                                    "flashvars=\"file="+"/images/activities/videos/"+video_images[video_next_image]['file']+"&image=/preview.jpg\" "+
                                                            "/> "+
                                                    "</object> "+
                                                    "<!-- END OF THE PLAYER EMBEDDING --> ");
						$(this).dequeue();

						video_gallery_large.css("left", (video_element_width+500)+"px");
						video_gallery_large.animate({"left":"0px"}, 400);
						video_gallery_large.queue(function() {
							video_in_animation = false;
							$(this).dequeue();
						});
					});
				}
			} else {
				//$("#video-gallery-large img").attr("src", "/video_images/activities/video_images/"+);
                                

                                $("#video-gallery-large div").html("<!-- START OF THE PLAYER EMBEDDING TO COPY-PASTE -->" +

                                "<object id=\"player\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" name=\"player\" > "+
                                        "<param name=\"movie\" value=\"/player.swf\" /> "+
                                        "<param name=\"allowfullscreen\" value=\"true\" /> "+
                                        "<param name=\"allowscriptaccess\" value=\"always\" /> "+
                                        "<param name=\"flashvars\" value=\"file="+"/images/activities/videos/"+video_images[video_next_image]['file']+"&image=/preview.jpg\" /> "+
                                        "<embed "+
                                                "type=\"application/x-shockwave-flash\" "+
                                                "id=\"player2\" "+
                                                "name=\"player2\" "+
                                                "src=\"/player.swf\" "+
                                                "width=\"328\" "+
                                                "height=\"200\" "+
                                                "allowscriptaccess=\"always\" "+
                                                "allowfullscreen=\"true\" "+
                                                "flashvars=\"file="+"/images/activities/videos/"+video_images[video_next_image]['file']+"&image=/preview.jpg\" "+
                                        "/> "+
                                "</object> "+
                                "<!-- END OF THE PLAYER EMBEDDING --> ");
                                //video_images[video_next_image]['file']
			}
		} else {
			video_next_image = video_current_image+1;
			if(video_next_image >= video_images.length) {
				video_next_image = 0;
			}
			video_current_image = video_next_image;


			if(video_animate) {
				if(!video_in_animation) {
					video_in_animation = true;
					video_gallery_large.animate({"left":(video_element_width+500)+"px"}, 400);
					video_gallery_large.queue(function() {
                                                //$("#video-gallery-large div").html(video_images[video_next_image]['file']);
                                                $("#video-gallery-large div").html("<!-- START OF THE PLAYER EMBEDDING TO COPY-PASTE -->" +

                                                    "<object id=\"player\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" name=\"player\" > "+
                                                            "<param name=\"movie\" value=\"/player.swf\" /> "+
                                                            "<param name=\"allowfullscreen\" value=\"true\" /> "+
                                                            "<param name=\"allowscriptaccess\" value=\"always\" /> "+
                                                            "<param name=\"flashvars\" value=\"file="+"/images/activities/videos/"+video_images[video_next_image]['file']+"&image=/preview.jpg\" /> "+
                                                            "<embed "+
                                                                    "type=\"application/x-shockwave-flash\" "+
                                                                    "id=\"player2\" "+
                                                                    "name=\"player2\" "+
                                                                    "src=\"/player.swf\" "+
                                                                    "width=\"328\" "+
                                                                    "height=\"200\" "+
                                                                    "allowscriptaccess=\"always\" "+
                                                                    "allowfullscreen=\"true\" "+
                                                                    "flashvars=\"file="+"/images/activities/videos/"+video_images[video_next_image]['file']+"&image=/preview.jpg\" "+
                                                            "/> "+
                                                    "</object> "+
                                                    "<!-- END OF THE PLAYER EMBEDDING --> ");
						$(this).dequeue();

						video_gallery_large.css("left", "-"+(video_element_width+500)+"px");
						video_gallery_large.animate({"left":"0px"}, 400);
						video_gallery_large.queue(function() {
							video_in_animation = false;
							$(this).dequeue();
						});
					});
				}
			} else {
				//$("#video-gallery-large img").attr("src", "/video_images/activities/video_images/"+);
                                //$("#video-gallery-large div").html(video_images[video_next_image]['file']);
                                $("#video-gallery-large div").html("<!-- START OF THE PLAYER EMBEDDING TO COPY-PASTE -->" +

                                    "<object id=\"player\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" name=\"player\" > "+
                                            "<param name=\"movie\" value=\"/player.swf\" /> "+
                                            "<param name=\"allowfullscreen\" value=\"true\" /> "+
                                            "<param name=\"allowscriptaccess\" value=\"always\" /> "+
                                            "<param name=\"flashvars\" value=\"file="+"/images/activities/videos/"+video_images[video_next_image]['file']+"&image=/preview.jpg\" /> "+
                                            "<embed "+
                                                    "type=\"application/x-shockwave-flash\" "+
                                                    "id=\"player2\" "+
                                                    "name=\"player2\" "+
                                                    "src=\"/player.swf\" "+
                                                    "width=\"328\" "+
                                                    "height=\"200\" "+
                                                    "allowscriptaccess=\"always\" "+
                                                    "allowfullscreen=\"true\" "+
                                                    "flashvars=\"file="+"/images/activities/videos/"+video_images[video_next_image]['file']+"&image=/preview.jpg\" "+
                                            "/> "+
                                    "</object> "+
                                    "<!-- END OF THE PLAYER EMBEDDING --> ");
			}




		}

	}

});



function video_gallery_hide_thumbs(top) {
	$("#video-gallery-thumbs").animate({"top": top}, 300);
	$("#video-gallery-toggle-thumbs").html('<img src="/images/gallery-open.gif">');

}

function video_gallery_show_thumbs() {
	$("#video-gallery-thumbs").animate({"top":"0px"}, 300);
	$("#video-gallery-toggle-thumbs").html('<img src="/images/gallery-close.gif">');
}

function video_gallery_hide() {
	$("#video-gallery").css("display", "none");
}

