// JavaScript Document
var VisableTab = 'Videos';
var CurrentCat;
var CurrentFlashId = "";
var LoadingFunctions = "";
var Runitem = true;

function ChangeCategory(CmsCategoryId,CatType){
	Loading("ChangeCategory");
	CurrentCat = CmsCategoryId;
	if (CatType == 'Video'){
		ShowVideo = true;
	}else{
		ShowVideo = false;
	}
	if(Runitem){
	LoadVideo(CurrentCat,ShowVideo);
	LoadImage(CurrentCat,null);
	}
	
	DoneLoading("ChangeCategory");
	
}

/*

		Video Section
*/

//Load video based on what category we have chosen
function LoadVideo(TheCat,ShowVideo){
	
	Loading("LoadVideo");

	CurrentCat = TheCat;
	var theurl="moo/VideoSection.asp"
		+"?action=LoadVideo"
		+"&CurrentCat="+CurrentCat;
var req = new Request({url:theurl, 
		onSuccess: function(txt) {
			if (txt != ""){
				//get related for the flash page
				GetRelatedVideos(txt,ShowVideo);
				//get related for the images page
				GetRelatedVideos(null,false)
				GetRelatedPhotos(null,ShowVideo,1);
				GetRelatedPhotos(null,false,1);
				LoadImage(CurrentCat,null);
				GetRelatedPhotos(CurrentCat,true,1);
				LoadVideoPlayer(txt);
				
			}else{
				$("VideoHeader").setStyle('display');
				$('VideoDesctiption').setStyle('display','none');
				$("VideoHolder").setStyle('display','none');
				$("AboutThisVideoHeader").setStyle('display','none');
				$("RelatedPhotosToVideo").setStyle('display','none');
				$("RelatedPhotosHeaderVideo").setStyle('display','none');
				$("RelatedVideos").setStyle('display','none');
				$("RelatedVideosHeader").setStyle('display','none');
				$("RelatedVideosToPhotos").setStyle('display','none');
				$("RelatedVideosHeaderImages").setStyle('display','none');
				$("VideoHeader").set('html','There are no videos tied to this category.');
			}
				
			DoneLoading("LoadVideo");	
		},
		onFailure: function() {
			ShowError('Unable to load the requested video');
			DoneLoading("LoadVideo");
		}
		
		});
	
	req.send();
	
}

function LoadVideoPlayer(CmsVideoId){
	Loading("LoadVideoPlayer");
	var theurl="moo/VideoSection.asp"
		+"?action=LoadVideoPlayer"
		+"&CmsVideoId="+CmsVideoId;
		var req = new Request({url:theurl, 
				onSuccess: function(txt) {
					if (txt != ""){
						$("VideoHolder").setStyle('display');
						$("VideoHolder").set('html',txt);
						LoadVideoDetails(CmsVideoId);
					}	
					DoneLoading("LoadVideoPlayer");
				},
				onFailure: function() {
					ShowError('Unable to Show the video');
					DoneLoading("LoadVideoPlayer");
				}
			});
			
			req.send();
}

function LoadVideoDetails(CmsVideoId){
	Loading("LoadVideoDetails");
	
	var theurl="moo/VideoSection.asp"
		+"?action=LoadVideoDetails"
		+"&CmsVideoId="+CmsVideoId;
		var req = new Request({url:theurl, 
				onSuccess: function(txt) {
					if (txt != ""){
						var returnarray = txt.split(';,');
						$('VideoHeader').setStyle('display');
						$("AboutThisVideoHeader").setStyle('display');
						$('VideoDesctiption').setStyle('display');
						$('VideoHeader').set('html',returnarray[1]);
						$('VideoDesctiption').set('html',returnarray[0]);	
						DoneLoading("LoadVideoDetails");
					}	
				},
				onFailure: function() {
					ShowError('Unable to get the video info');
					DoneLoading("LoadVideoDetails");
				}
			});
			
			req.send();
			
	
}
/*

		End Video Section
*/



/*

		Universal Section
*/

//Get the related videos to the video that we are looking at. 
//If null then it will be all the videos in this category
function GetRelatedVideos(CmsVideoId,ShowVideo){
	Loading("GetRelatedVideos");
	var VideoElementArray = "RelatedVideos,RelatedVideosHeader".split(",");
	var ImageElementArray = "RelatedVideosToPhotos,RelatedVideosHeaderImages".split(",");

	//set the elements
	if (ShowVideo){
		var el1 = VideoElementArray[0];
		var el2 = VideoElementArray[1];
		var div1 = "RelatedVideosVideo";
		var div2 = "RelatedVideosVideo_inner";
		var div3 = "RelatedVideosVideo_items";
		var div4 = "RelatedVideosVideoo_element";
	}else{
		var el1 = ImageElementArray[0];
		var el2 = ImageElementArray[1];
		var div1 = "RelatedVideosImage";
		var div2 = "RelatedVideosImage_inner";
		var div3 = "RelatedVideosImage_items";
		var div4 = "RelatedVideosImage_element";
	}
	var theurl="moo/VideoSection.asp"
		+"?action=GetRelatedVideos"
		+"&CurrentCat="+CurrentCat
		+"&CmsVideoId="+CmsVideoId
		+ "&ShowVideo=" + ShowVideo;
		var req = new Request({url:theurl, 
				onSuccess: function(txt) {
					if(txt != ''){
						$(el1).setStyle('display');
						$(el2).setStyle('display');
						$(el1).set('html',txt);
						
						new SlideItMoo({
							overallContainer: div1,
							elementScrolled: div2,
							thumbsContainer: div3,		
							itemsVisible:2,
							itemsSelector: '.' + div4,
							itemWidth: 158,
							showControls:1});
					}else{
						$(el1).setStyle('display','none');
						$(el2).setStyle('display','none');
					
					}
					DoneLoading("GetRelatedVideos");
				},
				onFailure: function() {
					ShowError('Unable to get the related videos');
					DoneLoading("GetRelatedVideos");
				}
			});
			
			req.send();
	
	
}
//Show the error
//put it here so if we make better errors we can just change it here.
function ShowError(ErrorTxt){
	alert(ErrorTxt);
}

//Get the related photos to the video that we are looking at. 
//If null then it will be all the videos in this category
function GetRelatedPhotos(CmsPhotosId,ShowVideo,curpage){
	
	Loading("GetRelatedPhotos");
	//set the elements

	if (ShowVideo  == false || ShowVideo == "false"){
		var el1 = "RelatedPhotosToVideo";
	}else{
		var el1 = "RelatedPhotos";
	}
	
	//alert(el1);
	var theurl="moo/VideoSection.asp"
		+"?action=GetRelatedPhotos"
		+"&CurrentCat="+CurrentCat
		+"&CmsPhotosId="+CmsPhotosId
		+"&curpage="+curpage
		+ "&ShowVideo=" + ShowVideo;
		
		var req = new Request({url:theurl, 
			onSuccess: function(txt) {
				if(txt != ''){
					$(el1).setStyle('display');
					$(el1).set('html',txt);

				
					
					
				}else{
					$(el1).setStyle('display','none');
				}
				DoneLoading("GetRelatedPhotos");
			},
			onFailure: function() {
				location.href = theurl;
				ShowError('Unable to get the related photos');
				DoneLoading("GetRelatedPhotos");
			}
		});
		
		req.send();
			
}


function LoadImage(CurrentCat,CmsPhotosId,idtoshow){
	//alert("CurrentCat " + CurrentCat + " CmsPhotosId " + CmsPhotosId + " idtoshow " + idtoshow )
	Loading("LoadImage");
	var theurl="moo/VideoSection.asp"
		+"?action=LoadImage"
		+"&CurrentCat="+CurrentCat
		+"&CmsPhotosId="+CmsPhotosId;
		var req = new Request({url:theurl, 
			onSuccess: function(txt) {
				if(txt != ''){
					var returnarray = txt.split(';,');
					$("PhotoHolder").setStyle('display');
					$("AboutThisPhotoDescription").setStyle('display');
					$("PhotoHeader").setStyle('display');
					$("PhotoDescription").setStyle('display');
					$("RelatedPhotos").setStyle('display');
					$("AboutThisVideoHeader").setStyle('display');
					$("PhotoHeader").set('html',returnarray[2]);
					$("PhotoDescription").set('html',returnarray[3]);
					$('LargePhoto').set('src','uploads/' + returnarray[1]);
					
				}else{
					$("PhotoHolder").setStyle('display','none');
					$("PhotoHeader").set('html','There are no photos in the category.');
					$("AboutThisPhotoDescription").setStyle('display','none');
					$("PhotoDescription").setStyle('display','none');
					$("RelatedPhotos").setStyle('display','none');
				}
				DoneLoading("LoadImage");
			},
			onFailure: function() {
				ShowError('Unable to load the photo.');
				DoneLoading("LoadImage");
			}
		});
		
		req.send();
	
	
}

function Loading(WhichFunction){
return;
 if (!$("TB_overlay") && LoadingFunctions == "") {
        new Element('iframe').setProperty('id', 'TB_HideSelect').injectInside(document.body);
        $('TB_HideSelect').setOpacity(0);
        new Element('div').setProperty('id', 'TB_overlay').injectInside(document.body);
        $('TB_overlay').setOpacity(0);
        TB_overlaySize();
        new Element('div').setProperty('id', 'TB_load').injectInside(document.body);
        $('TB_load').innerHTML = "<img src='/css/ajax-loader.gif' />";
        TB_load_position();
        $('TB_overlay').set('tween', {
            duration: 400
        });
        $('TB_overlay').tween('opacity', 0, 0.6);
 }
 
 LoadingFunctions += WhichFunction + ";";
	
}

function DoneLoading(WhichFunction){
	return;
	LoadingFunctions = LoadingFunctions.replace(WhichFunction+';','');
	if(LoadingFunctions == ""){
		$("TB_overlay").dispose();
		$('TB_HideSelect').dispose();
		$('TB_load').dispose();
	}
}

function TB_overlaySize(){
    // we have to set this to 0px before so we can reduce the size / width of the overflow onresize 
    $("TB_overlay").setStyles({
        "height": '0px',
        "width": '0px'
    });
    $("TB_HideSelect").setStyles({
        "height": '0px',
        "width": '0px'
    });
    $("TB_overlay").setStyles({
        "height": window.getScrollHeight() + 'px',
        "width": window.getScrollWidth() + 'px'
    });
    $("TB_HideSelect").setStyles({
        "height": window.getScrollHeight() + 'px',
        "width": window.getScrollWidth() + 'px'
    });
}

function TB_load_position(){
    if ($("TB_load")) {
        $("TB_load").setStyles({
            left: (window.getScrollLeft() + (window.getWidth() - 56) / 2) + 'px',
            top: (window.getScrollTop() + ((window.getHeight() - 210) / 2)) + 'px',
            display: "block"
        });
    }
}






