var loadedImageWidth = 0,
	loadedImageHeight = 0,
	scrollTop = 0,
	lightboxActive = false,
	imageRatio = 0,
	documentWidth = 0,
	documentHeight = 0,
	newImageWidth = 0,
	newImageHeight = 0,
	clickWrapShowing = false,
	contentTarget,
	contentType,
	imageSource,
	thumbCounter = 0,
	imageCounter = 0,
	viewableThumbs = 4,
	thumbWidth = 124,
	totalWidth = 0,
	offset = 125,
	movement = 0,
	currentPosition = 0,
	currentImage = 1,
	previousImage,
	flash_media_player,
	overImageDescription = false,
	clickedThumbTracker,
	nextClickedWhileActive = false,
	huluPlaying = false,
	imgCounter = 0,
	killSlideshow = false,
	overlaySlideshowPlaying = false,
	flash_media_player;
	youtube_playing = false;

/*************************************
		HULU INTEGRATION STUFF 
***************************************/

function loadHuluContent(theFile) {
	var PageLoader = {
		init: function() {
			NewSite.adComponent.show();
			var qs = NewSite.getQueryString();
			var contentId = qs.contentId || qs.contentid || qs.CONTENTID;
			NewSite.videoPlayerComponent.playVideo(theFile);	
		}
	};
  if (NewSite.readyEventFired()) {
	PageLoader.init();
  }else {
	NewSite.addListener("newsiteReady", PageLoader, "init");
  }
}

/*************************************
PLAY HULU VIDEO ON INITIAL PAGE LOAD
**************************************/

function play_hulu_video(){
	huluPlaying = true;
	var theEmbedCoded = $('.thumb_item_link:first').attr('rel');
	$("#adComponent").show();
	$("#episodes_ad300x250").hide();
	$("#hulu_brand_bar").fadeIn(150);
	$('#hulu_brand_bar a').attr('href',"http://www.hulu.com/watch?content_id="+theEmbedCoded);
	$(document).ready(function(){
		$(".hulu_player").show();
		$(".hulu_player").css({'zIndex': '1'});
		$(".hulu_player embed").attr('width', '600').attr('height', '360').attr('wmode', 'opaque').attr('bgcolor',"#000000");
		$(".hulu_player object").attr('width','600').attr('height','360');
		$("#media_content_holder").css({'opacity':'0'});
		loadHuluContent(theEmbedCoded);	
	});
}

/****************************************
		FUNCTION TO LOAD IMAGES
****************************************/
$.fn.image = function(src,f){
	return this.each(function(){
		$("#media_center_loader").fadeIn(200);
		$("#overlay_content").remove();
		$("<img id='overlay_content' alt='Overlay' style='display:none'/>").appendTo(this).each(function(){
			this.src = src;
			this.onload = f;
		});
	});
};

/*CHECK TO SEE IF SLIDESHOW IS PLAYING*/
function slide_show_run_checker(){
	if(slideshowPlaying === true){
		clearTimeout(l);
		slideshowTimeCount = 0; slideshowPlaying == false; clickedSlideshow = 1;
	};
};
/****************************************
		FLASH PLAYER FUNCTIONS
****************************************/
//IMAGE CLICKED FUNCTION
var image_click = function(params) {
	if(slideshowPlaying == true){
		clearTimeout(l);
		slideshowTimeCount = 0;
		slideshowPlaying = false;
	}
	if(params.content_type == 'image')
	{
		activateLightbox(params.full_size_preview,params.item_title,params.download_path);
	}else{
		//alert("Full Screen Clicked " + params.content_path); 	
	}
}
//FULL SCREEN BUTTON CLICKED
var full_screen_clicked = function(params) {
 	if(slideshowPlaying == true){
		clearTimeout(l);
		slideshowTimeCount = 0;
		slideshowPlaying = false;
	}
	if(params.content_type == 'image')
	{
		activateLightbox(params.full_size_preview,params.item_title,params.download_path);
	}else{
		//alert("Full Screen Clicked " + params.content_path); 	
	}
}			
//HD BUTTON CLICKED		
var hd_clicked = function(params) {
	//alert("HD Clicked " + params.content_path); 
}	
//ON VIDEO COMPLETE 								
var video_complete = function(params) {
	if(params.download_path == 'ad' && params.item_id == 'video'){
		play_regular_vid_init();
	}else if(params.download_path == 'ad' && params.item_id == 'hulu'){
		play_hulu_video();
	};
	vidAnalyticsTracking(params.content_type + '=finished',params.item_title,params.content_path);
}	
//LOAD MEDIA INTO PLAYER FUNCTION
var load_media = function(contentPath,contentType,previewImage,itemId,itemTitle,theAutoplay,hd,downloadFile){
	if(downloadFile == ""){downloadFile = null;}
	var params = {
		content_path: contentPath,
		content_type: contentType,
		full_size_preview: previewImage,
		item_id: itemId,
		download_path: downloadFile,
		item_title: itemTitle,
		autoplay:true,
		hd_disabled:hd,
		share_in_flash:false
	}
	theLoadingMachine = document.getElementById('flash_media_player');
	theLoadingMachine.load_media(params);
	vidAnalyticsTracking(contentType + '=loaded',itemTitle,contentPath);
}
////////////////////////////////////////
///////////////////////////////////////
/*GET IMAGE FOR SLIDESHOW*/
function get_slideshow_img(imgLoc){							
	whichStillsNext = stillIds[imgLoc];
	var target = "/dynamic/getObjects.php?1=" + whichStillsNext;
	$.getJSON(target, function(data){
		$.each(data.items, function(i,item){
			downloaderFile = item.fullImage;
			if(downloaderFile == ""){downloaderFile = null};				
			var params = {content_path: item.previewImage, content_type: 'image',full_size_preview: item.largeImage, item_id: whichStillsNext, download_path: downloaderFile, item_title:item.name, autoplay:false, hd_enabled:false, share_in_flash:false};
			document.getElementById('flash_media_player').load_media(params);
		});
	});
};

/*GET NEXT IMAGE IN FLASH*/
function get_ss_image(params,nextBack){
	slide_show_run_checker();
	if(nextBackFlashClicker == false){ 
		nextBackFlashClicker = true;
		if(nextBack == 'next'){
			var stillNext = ($.inArray(params.item_id, stillIds) + 1);
		}else{
			var stillNext = ($.inArray(params.item_id, stillIds) - 1);
		}
		get_slideshow_img(stillNext);
		nextBackFlashClicker = false;	
	};		
};

/*IMAGES/FLASH NEXT BTN*/
function next_clicked(params){ get_ss_image(params,'next');};

/*IMAGES/FLASH PREV BTN*/
function prev_clicked(params){	get_ss_image(params,'back');};
		
/*DECIDES WHETHER TO PLAY OR PAUSE THE SLIDESHOW*/
function slideshow_clicked(params) {
	if(clickedSlideshow == 1){
		slideshow_play(params);
		clickedSlideshow = 2;
	}else{
		clearTimeout(l); 
		slideshowTimeCount = 0; 
		slideshowPlaying = false; 
		clickedSlideshow = 1;
	};
};

/*PLAYS THE SLIDESHOW*/
function slideshow_play(params) {
	slideshowPlaying = true;
	if(slideshowTimeCount == 0){
		stillTracker = params.item_id;
		slideshowPosition = $.inArray(params.item_id, stillIds);
		slideshowTimeCount++;
		t = setTimeout("slideshow_play()",10);				
	}else{
		slideshowPosition = slideshowPosition + 1;
		get_slideshow_img(slideshowPosition);
		l = setTimeout("slideshow_play()",6400);
	};
};
	
/*THUMBNAIL AJAX/JSON LOADER*/
function thumbnail_ajax_loader(target,loader){

	$("#thumb_container_area").wrapInner($('<div id="thumbWrapper"></div>'));
	$.getJSON(target,function(data){
		$.each(data.items, function(i,item){
			if(item.contentType == 'Still'){
				$('#thumbWrapper').append('<a href="'+item.previewImage+'" id="'+idItems[i-1]+'" full="'+item.fullImage+'" rel="'+item.largeImage+'" type="'+item.contentType.toLowerCase()+'" class="thumb_item_link media_center_image" title="'+ item.name+'" onclick="return false"><div class="thumb_item" style="float:left"><div class="media_type_overlay" style="position:absolute;display:none"><img src="/images/image-overlay.png" width="113px" height="70px"></div><img src="'+ item.thumb +'" alt="'+ item.id+ '"  width="113" height="70" /></div></a>');						
				$('.thumb_item').animate({'opacity':'0'},0);
			}else if(item.contentType == 'Hulu')
			{
				$('#thumbWrapper').append('<a href="'+item.name+'" data-preview="'+item.previewImage+'" id="'+idItems[i-1]+'" title="'+item.name+'" type="'+item.contentType.toLowerCase()+'" class="thumb_item_link media_center_image" rel="'+escape(item.contentPath)+'" onclick="return false"><div class="thumb_item" style="float:left"><div class="media_type_overlay" style="position:absolute;display:none"><img src="/images/video-overlay.png" width="113px" height="70px"></div><img src="'+ item.thumb +'" alt="'+ item.id+ '"  width="113" height="70" /></div></a>');
				$('.thumb_item').animate({'opacity':'0'},0);
			}else{
				$('#thumbWrapper').append('<a href="'+item.contentPath+'" id="'+idItems[i-1]+'" rel="'+item.previewImage+'" type="'+item.contentType.toLowerCase()+'" class="thumb_item_link media_center_image" title="'+ item.name+'" onclick="return false"><div class="thumb_item" style="float:left"><div class="media_type_overlay" style="position:absolute;display:none"><img src="/images/video-overlay.png" width="113px" height="70px"></div><img src="'+ item.thumb +'" alt="'+ item.id+ '"  width="113" height="70" /></div></a>');						
				$('.thumb_item').animate({'opacity':'0'},0);
			};
		});
		if(loader === false){
				$('.thumb_item').animate({'opacity':'1'},1).fadeIn();
				$('.media_type_overlay').fadeIn();
		}else{
			$('#loading').fadeOut(100,function(){
				$('.thumb_item').animate({'opacity':'1'},150).fadeIn();
				$('.media_type_overlay').fadeIn();
				nextBackClicker = false;
			});
		};
	});
};



/*GET THE THUMBNAILS - BUTTON ACTION*/
function get_thumbnails(marginOffset){
	nextBackClicker = true;			
	var whereAt = mediaClickCounter * offset;
	var target ="/dynamic/getObjects.php?";	
	for(i=0;i<4;i++)
	{ 
		if(ids[whereAt] == undefined){
			i=4;
		}else{
			idItems.shift();
			idItems.push(ids[whereAt]);
			whereAt++;
		}
	}					
	for(j = 0; j <= (idItems.length - 1); j++)
	{
		target = target+ (j+1) +'='+idItems[j]+'&';
	}
	target = target.substr(0,target.length-1);
	$('.media_type_overlay').hide(1);	
	$("#thumbWrapper").animate({marginLeft:marginOffset,'opacity':'0'},200,function(){
		$(this).remove();
		$('#thumb_container_area').append('<div id="loading" style="display:none;margin:25px 0 0 180px"><img height="8" src="/images/loade_img.gif" alt="Loading..."/></div>');
		$("#loading").fadeIn(300,function(){
			thumbnail_ajax_loader(target,true);
		});
	});
};

/*GET NEXT SET OF THUMBS*/
function nextThumbs(){
	if(nextBackClicker == false && mediaClickCounter <= Math.ceil((ids.length/4)-2)){	
		mediaClickCounter++;	
		get_thumbnails('-500px');			
	}else{
		return false;
	};
};

/*GET PREV SET OF THUMBS*/
function backThumbs(){
	if(nextBackClicker == false && mediaClickCounter > 0){		
		mediaClickCounter--;								
		get_thumbnails('500px');
	}else{
		return false;
	};
};


//PLAY REGULAR VIDEO IN MEDIA PLAYER ON INITIAL PAGE LOAD//
function play_regular_vid_init(){
	huluPlaying = false;
	$(".hulu_player").hide();
	$("#media_content_holder").css({'display':'block'});
	var contentPath = $('.thumb_item_link:first').attr('href');
	var contentsType = $('.thumb_item_link:first').attr('type');
	var previewImage = $('.thumb_item_link:first').attr('rel');
	var itemId = $('.thumb_item_link:first').attr('id');
	var itemTitle = $('.thumb_item_link:first').attr('title');
	var downloadFile = $('.thumb_item_link:first').attr('full');
	var autoplay = false;
	var hd_enabled = true;
	if (contentsType == 'still') {contentsType = 'image';};
	load_media(contentPath,contentsType,previewImage,itemId,itemTitle,autoplay,hd_enabled,downloadFile);
};


/*********************************
		LIGHT BOX ACTIVATION
**********************************/
function activateLightbox(fullSizeUrl,overlayTitle,downloadSize){    //Start Activating the lightbox
	lightboxActive = true;
	bigImageSource = fullSizeUrl;                   //if there is class 'media_center_image' launch lightbox
	
	$(document).bind('keydown', 'left', function(){back_image();});
	$(document).bind('keydown', 'right', function(){ next_image();});
	$(document).bind('keydown', 'esc', function(){fade_out_overlay();});
	$("#overlay_content_holder").css({'display': 'block','opacity':'1','filter':'alpha(opacity=100)','marginTop':$(window).scrollTop()})
	$("#media_center_overlay_bg").css({'display':'block','height':$(document.body).height()}).animate({'opacity': '.8'	},500)
	$("#overlay_image_description").fadeIn();
	$('.overlay_description_text').text(overlayTitle);
	$('#accept_btn').attr('href',downloadSize);					
	//Load the Content, this may to be reworked
	$("#media_center_overlay_content").image(bigImageSource, function(){
		scrollTop = $(window).scrollTop();

		loadedImageWidth = $("#overlay_content").width();
		loadedImageHeight = $("#overlay_content").height();
		imageRatio = loadedImageWidth/loadedImageHeight;
		documentWidth = Math.floor(($(window).width()) * .85);
		documentHeight = Math.floor(($(window).height()) * .85);
		
		if(imageRatio > 1)
		{
			newImageWidth = documentWidth;
			newImageHeight = documentWidth/imageRatio;
		}else{
			newImageHeight = documentHeight;
			newImageWidth = documentHeight * imageRatio;
		}
		
		//This centers the and animates the width 
		$("#overlay_image_description").css({'textAlign':'center','width':newImageWidth,'left':-(newImageWidth/2),'top':($(window).scrollTop())+(newImageHeight-75)});
		$("#overlay_content_holder").animate({width:newImageWidth,left:-(newImageWidth/2),height:newImageHeight},function(){
			$("#media_center_loader").fadeOut(200, function(){
				$("#overlay_content").css({'display':'block','width':newImageWidth,'height':newImageHeight});
			})
		});
	})
	
}

/***********************************************
		FIND NEXT IMAGE WHILE IN LIGHTBOX
************************************************/
function next_image(){
	killSlideshow = true;
	if (nextClickedWhileActive == false) {
		//alert(clickedThumbTracker);
		for (i = 0; i < (stillIds.length - 1); i++) {
			if (clickedThumbTracker == stillIds[i]) {
				//alert('we got it its at position:' + i +'and the next thumb is: ' +stillIds[i + 1]);
				currentImage = i;
			}
		}
	}	
	nextClickedWhileActive = true;
	var target ="/dynamic/getObjects.php?1=";								
	whichStillsNext = stillIds[currentImage + 1];
	//alert(currentImage)
	currentImage = currentImage + 1;
	var target = target + whichStillsNext;
	$.getJSON(target,
	 function(data){
		$.each(data.items, function(i,item){
			nextImageFile = item.largeImage;
			var itemTitle = item.name;
			
			var downloadImage = item.fullImage;
			
			$("#accept_btn").attr('href',downloadImage);
			$("#media_center_overlay_content").image(nextImageFile, function(){
				loadedImageWidth = $("#overlay_content").width();
				loadedImageHeight = $("#overlay_content").height();
				imageRatio = loadedImageWidth/loadedImageHeight;
				documentWidth = Math.floor(($(window).width()) * .85);
				documentHeight = Math.floor(($(window).height()) * .85);
				
				if(imageRatio > 1)
				{
					newImageWidth = documentWidth;
					newImageHeight = documentWidth/imageRatio;
				}else{
					newImageHeight = documentHeight;
					newImageWidth = documentHeight * imageRatio;
				}
				
				//This centers the and animates the width 
				$("#overlay_image_description").css({'width':newImageWidth,'left':-(newImageWidth/2),'top':($(window).scrollTop())+(newImageHeight-75)});
				$("#overlay_content_holder").animate({width:newImageWidth,left:-(newImageWidth/2),height:newImageHeight},function(){
					$("#media_center_loader").fadeOut(200, function(){
						$('.overlay_description_text').text(itemTitle);
						$("#overlay_content").css({'display':'block','width':newImageWidth,'height':newImageHeight});
					})
				});
			})
		});
	})
}

/***********************************************
		FIND PREVIOUS IMAGE WHILE IN LIGHTBOX
************************************************/
function back_image(){
	killSlideshow = true;
	if (nextClickedWhileActive == false) {
		//alert(clickedThumbTracker);
		for (i = 0; i < (stillIds.length - 1); i++) {
			if (clickedThumbTracker == stillIds[i]) {
				
				currentImage = i;
			}
		}
	}
	nextClickedWhileActive = true;
	var target ="/dynamic/getObjects.php?1=";								
	whichStillsNext = stillIds[currentImage - 1];
	//alert(currentImage)
	currentImage = currentImage - 1;
	var target = target + whichStillsNext;
	$.getJSON(target,
	 function(data){
		$.each(data.items, function(i,item){
			nextImageFile = item.largeImage;
			var itemTitle = item.name;
			var downloadImage = item.fullImage;
			$("#accept_btn").attr('href',downloadImage);
			$("#media_center_overlay_content").image(nextImageFile, function(){
				loadedImageWidth = $("#overlay_content").width();
				loadedImageHeight = $("#overlay_content").height();
				imageRatio = loadedImageWidth/loadedImageHeight;
				documentWidth = Math.floor(($(window).width()) * .85);
				documentHeight = Math.floor(($(window).height()) * .85);
				
				if(imageRatio > 1)
				{
					newImageWidth = documentWidth;
					newImageHeight = documentWidth/imageRatio;
				}else{
					newImageHeight = documentHeight;
					newImageWidth = documentHeight * imageRatio;
				}
				
				//This centers the and animates the width 
				$("#overlay_image_description").css({'width':newImageWidth,'left':-(newImageWidth/2),'top':($(window).scrollTop())+(newImageHeight-75)});
				$("#overlay_content_holder").animate({width:newImageWidth,left:-(newImageWidth/2),height:newImageHeight},function(){
					$("#media_center_loader").fadeOut(200, function(){
						$('.overlay_description_text').text(itemTitle);
						$("#overlay_content").css({'display':'block','width':newImageWidth,'height':newImageHeight});
					})
				});
			})
		});
	})
}

/**************************************
		LIGHTBOX DEACTIVATION
***************************************/
function fade_out_overlay(){
	killSlideshow = true;
	nextClickedWhileActive = false;
	lightboxActive = false;
	$('#media_center_overlay_bg').fadeOut();
	$("#overlay_content_holder").fadeOut();
	$('#overlay_content').fadeOut();
	$("#overlay_image_description").fadeOut();
	$(document).unbind('keydown', 'esc');
	$(document).unbind('keydown', 'left');
	$(document).unbind('keydown', 'right');
	if(clickWrapShowing == true){
		$('#clickwrap_license').fadeOut();
		clickWrapShowing = false;
	}
	
}	

$(document).ready(function(){		
						   
	function loadHuluContent2(theFile) {
		var PageLoader = {
			init: function() {
				NewSite.adComponent.show();
				var qs = NewSite.getQueryString();
				var contentId = qs.contentId || qs.contentid || qs.CONTENTID;
				NewSite.videoPlayerComponent.playVideo(theFile);
			}
		};
		if (NewSite.readyEventFired()){
			PageLoader.init();
		}else {
			NewSite.addListener("newsiteReady", PageLoader, "init");
		};
	};


/*******************************************
		LIGHTBOX SLIDESHOW FUNCTIONS
********************************************/
	
	//FUNCTION THAT RUNS THE SLIDESHOW
	function overlaySlideShow(){
		if (killSlideshow == true) {
			clearTimeout(slideshowTimer);
			overlaySlideshowPlaying = false;
		}
		else 
		{
			if (nextClickedWhileActive == false) {
				for (i = 0; i < (stillIds.length - 1); i++) {
					if (clickedThumbTracker == stillIds[i]) {
						currentImage = i;
					}
				}
			}
			nextClickedWhileActive = true;
			var target = "/dynamic/getObjects.php?1=";
			whichStillsNext = stillIds[currentImage + 1];
			//alert(currentImage)
			currentImage = currentImage + 1;
			var target = target + whichStillsNext;
			$.getJSON(target, function(data){
				$.each(data.items, function(i, item){
					nextImageFile = item.largeImage;
					titleImage = item.name;
					$("#media_center_overlay_content").image(nextImageFile, function(){
						loadedImageWidth = $("#overlay_content").width();
						loadedImageHeight = $("#overlay_content").height();
						imageRatio = loadedImageWidth / loadedImageHeight;
						documentWidth = Math.floor(($(window).width()) * .85);
						documentHeight = Math.floor(($(window).height()) * .85);
						
						if (imageRatio > 1) {
							newImageWidth = documentWidth;
							newImageHeight = documentWidth / imageRatio;
						}
						else {
							newImageHeight = documentHeight;
							newImageWidth = documentHeight * imageRatio;
						}
						
						//This centers the and animates the width 
						$("#overlay_image_description").css({
							'width': newImageWidth,
							'left': -(newImageWidth / 2),
							'top': ($(window).scrollTop()) + (newImageHeight - 75)
						});
						$("#overlay_content_holder").animate({
							width: newImageWidth,
							left: -(newImageWidth / 2),
							height: newImageHeight
						}, function(){
							$("#media_center_loader").fadeOut(200, function(){
								$("#overlay_content").css({
									'display': 'block',
									'width': newImageWidth,
									'height': newImageHeight
								});
							})
							$(".overlay_description_text").text(titleImage);
						});
					})
				});
			})
			
			var slideshowTimer = setTimeout(function(){
				overlaySlideShow();
				//alert('playing still');
			}, 7000)
		}
	}
	
	//WHEN SLIDE SHOW BUTTON IS CLICKED
	$(".overlay_slideshow_btn").click(function(){
		if(overlaySlideshowPlaying == false){
			killSlideshow = false;
			overlaySlideShow();
			overlaySlideshowPlaying = true;
			$(this).text('PAUSE SLIDESHOW');
		}else{
			killSlideshow = true;
			$(this).text('PLAY SLIDESHOW');
			overlaySlideshowPlaying = false;
		};
			
	});
/********************************************
		LIGHTBOX BUTTON FUNCTIONS
*********************************************/
	//WHEN FULL SIZE IS CLICKED IN LIGHTBOX SLIDESHOW
	$(".overlay_full_size").click(function(){
		killSLideshow = true;
		clickWrapShowing = true;
		$('#clickwrap_license').fadeIn();
	});
	//WHEN  DECLINE BUTTON IS CLICKED IN LIGHTBOX 
	$('#decline_btn').click(function(){
		$('#clickwrap_license').fadeOut();	
		clickWrapShowing = false;
	});
	//WHEN ACCEPT BUTTON IS CLICKED IN LIGHTBOX
	$('#accept_btn').click(function(){
		if($('#img_checkbox').attr('checked')){
			$('#license_check_tooltip').fadeOut()	
			$('#clickwrap_license').fadeOut();		
			clickWrapShowing = false;
			window.open($(this).attr('href'),'FullImage','toolbar=no,location=no,menubar=no,copyhistory=no,status=nodirectories=no') 
		}else{
			$('#license_check_tooltip').fadeIn()	
		}
		return false;
	});
	
	//KEEPS LIGHTBOX OVERLAY IN CENTER OF SCREEN WHILE SCROLLING
	$(window).scroll(function(){
		if(lightboxActive == true)
		{
			scrollTop = $(window).scrollTop();
			$("#overlay_content_holder").css({'marginTop':scrollTop})
			$("#overlay_image_description").css({'top':($(window).scrollTop())+(newImageHeight-75)});
		};
	});
	
	//LIGHTBOX RESIZING FUNCTION
	$(window).resize(function(){
		if(lightboxActive == true)
		{
			documentWidth = Math.floor(($(window).width()) * .85);
			documentHeight = Math.floor(($(window).height()) * .85);
			
			if(imageRatio > 1)
			{
				newImageWidth = documentWidth;
				newImageHeight = documentWidth/imageRatio;
			}else{
				newImageHeight = documentHeight;
				newImageWidth = documentHeight * imageRatio;
			};
			$("#overlay_image_description").css({'width':newImageWidth,'left':-(newImageWidth/2),'top':($(window).scrollTop())+(newImageHeight-75)},200);
			$("#overlay_content_holder").css({'width':newImageWidth,'left':-(newImageWidth/2),'height':newImageHeight},200);
			$("#overlay_content").css({'width':newImageWidth,'height':newImageHeight},200);
		};
	});
	
	//LIGHTBOX CLOSE BUTTON ON CLICK
	$("#media_center_overlay_close").click(function(){
		fade_out_overlay();
		return false;
	});
	//CLOSE LIGHTBOX IF BACKGROUND IS CLICKED
	$("#media_center_overlay_bg").click(function(){
		fade_out_overlay();
	});
	//IMAGE COUNTER?
	$(".thumb_item_link").each(function(){
		if ($(this).attr('type') == 'image') {
			imageCounter++;
			$(this).attr({ id : "image" + imageCounter});
		};
	});
	//LIGHTBOX BACK BUTTON ON CLICK
	$("a #overlay_back_btn").click(function(){back_image();});
	//LIGHTBOX NEXT BUTTON ON CLICK
	$("a #overlay_next_btn").click(function(){next_image();});
	

	//SHOW HIDE LIGHTBOX CONTORLS ON IMAGE HOVER
	$("#media_center_overlay_bg").hover(
		function(){
			$("#overlay_image_description").animate({height:'100px',opacity:0},200)
			$("#overlay_next_btn").animate({width:'0px'},200)
			$("#overlay_back_btn").animate({width:'0px'},200)
			$("#media_center_overlay_close").animate({height:'0px'},200)
		}
		,function(){
			if (lightboxActive == true) {			
				$("#overlay_image_description").animate({height: '100px',opacity: .8}, 200)
				$("#overlay_next_btn").animate({height:'100px',width: '80px'}, 200)
				$("#overlay_back_btn").animate({height: '100px',width: '80px'}, 200)
				$("#media_center_overlay_close").animate({width:'80px',height: '35px'}, 200)
			};
		}
	);

	/************************************
	*		 THUMBNAIL STUFF            *
	************************************/
	
	//WHEN MEDIA PLAYER THUMBNAIL IS CLICKED
	$(".thumb_item_link").live("click",function(){
		if(slideshowPlaying == true){
			clearTimeout(l);
			slideshowTimeCount = 0;
			slideshowPlaying = false;
		};

		theEmbededCode = $(this).attr('rel');
		if ($(this).attr("type") == 'hulu') 
		{
			document.getElementById('flash_media_player').stop_media();
			$("#hulu_brand_bar").fadeIn(150);
			if (huluPlaying == true) {
				$('#hulu_brand_bar a').attr('href',"http://www.hulu.com/watch?content_id="+theEmbededCode);
				loadHuluContent2(theEmbededCode);
				$("#episodes_ad300x250").hide();
				$("#adComponent").show();					
			}else{
				delayer = setTimeout(
					function(){
						loadHuluContent2(theEmbededCode);
						$(".hulu_player").fadeIn(150);
						$("#episodes_ad300x250").hide();
						$("#adComponent").show();	
					},600)
				$("#media_content_holder").css({'opacity':'0'});
				huluPlaying = true;
				$('#hulu_brand_bar a').attr('href',"http://www.hulu.com/watch?content_id="+theEmbededCode);				
			};
			//alert($(this).attr('href'));
			
			vidAnalyticsTracking('hulu',$(this).attr('href'),$(this).attr('rel'));
		}else if($(this).attr("type") == 'youtube'){
			if(youtube_playing == true){
				setTimeout(function(){
					loadNewVideo(theEmbededCode,0)},1000);	
			}else{
				youtube_playing = true;
				document.getElementById('flash_media_player').stop_media();
				$('#flash_media_player').css({'opacity':'0'});
				$('#youtube_player').show(1,function(){
					setTimeout(function(){
					loadNewVideo(theEmbededCode,0)},1000);										   
				});
			};
		}else{			
			$("#adComponent").hide();
			$("#episodes_ad300x250").show();
			$("#hulu_brand_bar").fadeOut(150);
			if(huluPlaying == true){
				huluPlaying = false;					
			};
			$(".hulu_player").fadeOut(300,function(){
			   $("#media_content_holder").css({'opacity':'1'});
			   loadHuluContent2('0');
			});	
			if(youtube_playing == true){
				stop();
				youtube_playing = false;
				$('#flash_media_player').css({'opacity':'1'});
				$('#youtube_player').hide(1);
			}
			var contentsType = $(this).attr('type');
			if (contentsType == 'still') {
				contentsType = 'image';
			};
			var contentPath = $(this).attr('href');			
			var previewImage = $(this).attr('rel');
			var downloadSize = $(this).attr('full');
			var itemId = $(this).attr('id');
			var itemTitle = $(this).attr('title');
			autoplay = true;
			hdEnabled = true;
			clickedThumbTracker = itemId;
			var timer = setTimeout(function(){					
				load_media(contentPath,contentsType,previewImage,itemId,itemTitle,autoplay,hdEnabled,downloadSize);
			},500);
		};
	})

	/*********************************
	 *      MEDIA CENTER OVERLAY     *
	 ********************************/
	//initial setting stuff for overlay
	$("#overlay_content_holder").css({'display':'none','top':'25px','marginLeft':'50%','position':'absolute','zIndex':'600','background':'black','alpha':'1','filter':'alpha(opacity=100)','width':'0px'});
	$("#media_center_overlay_bg").css({'display':'none','top':'0px','left':'0','width':'100%','height':'100%','background':'black','position':'absolute','zIndex':'590','opacity':'0','filter':'alpha(opacity=0)'});
	$("#overlay_image_description").css({'display':'none','height':'100px','marginLeft':'50%','zIndex':'620','width':'0','opacity':'0','filter':'alpha(opacity=0)'});
	$("#overlay_back_btn").css({'width':'0','height':'32px'});
	$("#overlay_next_btn").css({'width':'0','height':'32px'});
	$("#media_center_overlay_close").css({'height':'0px','width':'33px','zIndex':'650','right':'0'});


	 /****************************
	 *     TOOL TIP STUFF		 *
	 ****************************/
	 //FUNCTION FOR SHOWING TOOLTIPS
	function tooltipOver(thumbnail){	
		if ($(thumbnail).parent('.thumb_item').parent('.thumb_item_link').attr('type') == 'still') {
			$(thumbnail).parent('div').prepend("<div class='media_tooltip' style='display:none'><img src='/images/media-center/tool_image.gif' alt='View This Image' /><br />" + $(thumbnail).parent('.thumb_item').parent('.thumb_item_link').attr('title') + "<br /><div id='tool_tip_arrow'><img src='/images/media-center/tool_tip_arrow.png' alt=' ' /></div></div>");
		}else if ($(thumbnail).parent('.thumb_item').parent('.thumb_item_link').attr('type') == 'video') {
			$(thumbnail).parent('div').prepend("<div class='media_tooltip' style='display:none'><img src='/images/view-vid-tooltip.gif' alt='View This Video' /><br>" +$(thumbnail).parent('.thumb_item').parent('.thumb_item_link').attr('title') + "<br><div id='tool_tip_arrow'><img src='/images/media-center/tool_tip_arrow.png' alt=' ' /></div></div>");	
		}
		else if ($(thumbnail).parent('.thumb_item').parent('.thumb_item_link').attr('type') == 'audio') {
			$(thumbnail).children('div').prepend("<div class='media_tooltip' style='display:none'><b>Click to Here</b><br>" + $(thumbnail).parent('.thumb_item').parent('.thumb_item_link').attr('title') + "<br><div id='tool_tip_arrow'><img src='/images/media-center/tool_tip_arrow.png' alt=' ' /></div></div>");	
		}else if ($(thumbnail).parent('.thumb_item').parent('.thumb_item_link').attr('type') == 'hulu') {
			$(thumbnail).parent('div').prepend("<div class='media_tooltip' style='display:none'><img src='/images/view-vid-tooltip.gif' alt='View This Video'/><br />" + $(thumbnail).parent('.thumb_item').parent('.thumb_item_link').attr('title') + "<br /><div id='tool_tip_arrow'><img src='/images/media-center/tool_tip_arrow.png' alt=' ' /></div></div>");	
		}else if ($(thumbnail).parent('.thumb_item').parent('.thumb_item_link').attr('type') == 'youtube') {
			$(thumbnail).parent('div').prepend("<div class='media_tooltip' style='display:none'><img src='/images/view-vid-tooltip.gif' alt='View This Video'/><br />" + $(thumbnail).parent('.thumb_item').parent('.thumb_item_link').attr('title') + "<br /><div id='tool_tip_arrow'><img src='/images/media-center/tool_tip_arrow.png' alt=' ' /></div></div>");	
		};
		$(".media_tooltip").css({'opacity': '0'})		;
		$(".media_tooltip").show().animate({bottom:'72px',opacity:'.98'},180).css({'textDecoration':'none','lineHeight':'17px'});
		$(".thumb_item_link").css({'textDecoration':'none'});
	}
	
	//FUNCTION FOR HIDING TOOLTIPS
	function tooltipOut(){
		$(".media_tooltip").css({'zIndex':'900'}).animate({bottom:'80px',opacity:'0'},150,function(){$(this).remove();});
	};
	//RUN SHOW TOOLTIP FUNCTION WHEN MOUSE OVER A MEDIA PLAYER THUMBNAIL
	$('.media_type_overlay').live('mouseover',function(){tooltipOver($(this));});
	//RUN HIDE TOOLTIP FUNCTION WHEN MOUSE OVER A MEDIA PLAYER THUMBNAIL
	$('.media_type_overlay').live('mouseout',function(){tooltipOut($(this));});
});

/*******************************************************************/

var offset = 4;
var mediaClickCounter = 0;

var share_clicked = function(params) {
		var embedType;
		var embedId;
		var brokenstring=params.item_id.split(":"); 
		embedId = brokenstring[1];
		if(brokenstring[0] == "Still")
			{embedType = 'embed_img';}
		else if(brokenstring[0] == "Video")
			{embedType = 'embed_vid';}
		
		$("#embedText").replaceWith('<textarea id="embedText" style="display:none;"><div style="width:150px;font-family:arial,verdana,helvetica;font-size:10pt"><p>Come check out what I found over at stargate.mgm.com</p><a class="embed_text_link" href="'+theEmbedLink+embedType + '/'+embedId+'/index.html">'+params.item_title+'</a><div></textarea>');
		$("#embed_stuff_area").replaceWith('<div id="embed_stuff_area" style="position:absolute;z-index:5000;margin:-335px 0 0 100px"><div id="divWildfirePost"></div></div>');
		$("#embed_stuff_area").append("<script>  var pconf={defaultContent:'embedText',UIConfig:'<config><display showDesktop=\"false\" showEmail=\"true\" useTransitions=\"true\" showBookmark=\"true\" codeBoxHeight=\"auto\" showCloseButton=\"true\"  ></display><body><background frame-color=\"#BFBFBF\" background-color=\"#FFFFFF\" gradient-color-begin=\"#ffffff\" gradient-color-end=\"#F4F4F4\" corner-roundness=\"4;4;4;4\"></background><controls color=\"#202020\" corner-roundness=\"4;4;4;4\" gradient-color-begin=\"#EAEAEA\" gradient-color-end=\"#F4F4F4\" bold=\"false\"><snbuttons type=\"textUnder\" frame-color=\"#D5D5D5\" background-color=\"#fafafa\" over-frame-color=\"#60BFFF\" over-background-color=\"#ebebeb\" color=\"#808080\" gradient-color-begin=\"#FFFFFF\" gradient-color-end=\"d4d6d7\" size=\"10\" bold=\"false\" down-frame-color=\"#60BFFF\" down-gradient-color-begin=\"#6DDADA\" over-gradient-color-end=\"#6DDADA\" down-gradient-color-end=\"#F4F4F4\" over-color=\"#52A4DA\" down-color=\"#52A4DA\" over-bold=\"fals\"><more frame-color=\"#A4DBFF\" over-frame-color=\"#A4DBFF\" gradient-color-begin=\"#F4F4F4\" gradient-color-end=\"#BBE4FF\" over-gradient-color-begin=\"#A4DBFF\" over-gradient-color-end=\"#F4F4F4\"></more><previous frame-color=\"#BBE4FF\" over-frame-color=\"#A4DBFF\" gradient-color-begin=\"#FFFFFF\" gradient-color-end=\"#A4DBFF\" over-gradient-color-begin=\"#A4DBFF\" over-gradient-color-end=\"#F4F4F4\"></previous></snbuttons><textboxes frame-color=\"#CACACA\" color=\"#757575\" gradient-color-begin=\"#ffffff\" bold=\"false\"><codeboxes color=\"#757575\" frame-color=\"#DFDFDF\" background-color=\"#FFFFFF\" gradient-color-begin=\"#ffffff\" gradient-color-end=\"#FFFFFF\" size=\"10\"></codeboxes><inputs frame-color=\"#CACACA\" color=\"#757575\" gradient-color-begin=\"#F4F4F4\" gradient-color-end=\"#ffffff\"></inputs><dropdowns list-item-over-color=\"#52A4DA\" frame-color=\"#CACACA\"></dropdowns></textboxes><buttons frame-color=\"#CACACA\" gradient-color-begin=\"#F4F4F4\" gradient-color-end=\"#CACACA\" color=\"#000000\" bold=\"false\" over-frame-color=\"#60BFFF\" over-gradient-color-begin=\"#BBE4FF\" down-gradient-color-begin=\"#BBE4FF\" over-gradient-color-end=\"#FFFFFF\" down-gradient-color-end=\"#ffffff\"><post-buttons frame-color=\"#CACACA\" gradient-color-end=\"#CACACA\"></post-buttons></buttons><listboxes frame-color=\"#CACACA\" corner-roundness=\"4;4;4;4\" gradient-color-begin=\"#F4F4F4\" gradient-color-end=\"#FFFFFF\"></listboxes><checkboxes checkmark-color=\"#00B600\" frame-color=\"#D5D5D5\" corner-roundness=\"3;3;3;3\" gradient-color-begin=\"#F4F4F4\" gradient-color-end=\"#FFFFFF\"></checkboxes><servicemarker gradient-color-begin=\"#ffffff\" gradient-color-end=\"#D5D5D5\"></servicemarker><tooltips color=\"#6D5128\" gradient-color-begin=\"#FFFFFF\" gradient-color-end=\"#FFE4BB\" size=\"10\" frame-color=\"#FFDBA4\"></tooltips></controls><texts color=\"#202020\"><headers color=\"#202020\"></headers><messages color=\"#202020\"></messages><links color=\"#52A4DA\" underline=\"false\" over-color=\"#353535\" down-color=\#353535\" down-bold=\"false\"></links></texts></body></config>',emailSubject:'$sender$ sent you this link from Stargate.mgm.com',emailBody:'Hi,<br /> Check out what I found over at <br />Stargate.mgm.com:<br /><br />$url$<br /><br />Talk to you later,<br />$sender$'};pconf['onClose']= closeGigya;Wildfire.initPost('624752', 'divWildfirePost', 400, 300, pconf);<\/script>");

}
		

var next_clicked = function(params) {
	if(slideshowPlaying == true){
		clearTimeout(l);
		slideshowTimeCount = 0;
		slideshowPlaying == false;
	}	
	if(nextBackFlashClicker == false){ 
		nextBackFlashClicker = true;
		//alert("Next Clicked " + params.item_id + ' - ' + idItems); 
		for(i=0;i < stillIds.length;i++)
		{
			if(stillIds[i] == params.item_id)
			{
				//alert('we got it its at position ' + i);
				//alert(stillIds[i + 1]);	
				var target ="/dynamic/getObjects.php?1=";								
				whichStillsNext = stillIds[i+1];
				var target = target + whichStillsNext;
				$.getJSON(target,
				 function(data){
					$.each(data.items, function(i,item){
						downloaderFile = item.fullImage;
						if(downloaderFile == ""){downloaderFile = null};
						//alert(item.name);
						//alert(item.previewImage + item.fullImage + item.contentType + whichStillsNext);				
						var params = {
							content_path: item.previewImage,
							content_type: 'image',
							full_size_preview: item.largeImage,
							item_id: whichStillsNext,
							download_path: downloaderFile,
							item_title:item.name,
							autoplay:false,
							hd_enabled:false,
							share_in_flash:false
						}
						//alert(params.item_id);
						document.getElementById('flash_media_player').load_media(params);
					});
				})
				i = stillIds.length;
			};
		}	
		nextBackFlashClicker = false;	
	}		
}

var prev_clicked = function(params) {
		if(slideshowPlaying == true){
			clearTimeout(l);
			slideshowTimeCount = 0;
			slideshowPlaying = false;
		}
	if(nextBackFlashClicker == false){ 
		nextBackFlashClicker = true;
		//alert("Next Clicked " + params.item_id + ' - ' + idItems); 
		for(i=1;i < stillIds.length;i++)
		{
			if(stillIds[i] == params.item_id)
			{
				//alert(stillIds[i - 1]);	
				var target ="/dynamic/getObjects.php?1=";								
				whichStillsNext = stillIds[i - 1];
				var target = target + whichStillsNext;
				$.getJSON(target,
				 function(data){
					$.each(data.items, function(i,item){
						downloaderFile = item.fullImage;						
						if(downloaderFile == ""){downloaderFile = null};
						//alert(item.previewImage + item.fullImage + item.contentType + whichStillsNext);				
						var params = {
							content_path: item.previewImage,
							content_type: 'image',
							full_size_preview: item.largeImage,
							item_id: whichStillsNext,
							download_path: downloaderFile,
							item_title:item.name,
							autoplay:false,
							hd_enabled:false,
							share_in_flash:false
						}
						//alert(params.item_id);
						document.getElementById('flash_media_player').load_media(params);
					});
				})
				i = stillIds.length;
			};
		}	
		nextBackFlashClicker = false;	
	}		
}
		
var clickedSlideshow = 1;

function slideshow_clicked(params) {
	if(clickedSlideshow == 1){
		slideshow_play(params);
		clickedSlideshow = 2;
	}else{
		clearTimeout(l);
		slideshowTimeCount = 0;
		slideshowPlaying = false;
		clickedSlideshow = 1;
	}
}

function slideshow_play(params) {
	slideshowPlaying = true;
	if(slideshowTimeCount == 0){
		stillTracker = params.item_id;
		for(i=0;i < stillIds.length;i++)
		{
			if(stillIds[i] == params.item_id)
			{
				slideshowPosition = i;
				slideshowTimeCount++;
				 t = setTimeout("slideshow_play()",10);	
				 	i = stillIds.length;			
			}
		}
	}else{
		slideshowPosition = slideshowPosition + 1;
		var target ="/dynamic/getObjects.php?1=";								
		whichStillsNext = stillIds[slideshowPosition];

		var target = target + whichStillsNext;
		$.getJSON(target,
		 function(data){
			$.each(data.items, function(i,item){				
				downloaderFile = item.fullImage;
				if(downloaderFile == ""){downloaderFile = null};			
				var params = {
					content_path: item.previewImage,
					content_type: 'image',
					full_size_preview: item.fullImage,
					item_id: whichStillsNext,
					download_path: downloaderFile,
					item_title: item.name,
					autoplay:false,
					hd_enabled:false,
					share_in_flash:false
				}
				document.getElementById('flash_media_player').load_media(params);
			});
		})
		l = setTimeout("slideshow_play()",6400);
	}
}


		
			
			
$(document).ready(function(){
	$('#thumb_back_link').click(function(){
		backThumbs();
	})
	$('#thumb_next_link').click(function(){
		nextThumbs();
	})
		
	whereAt = 0;
	idItems = new Array();
	for(i=0;i<4;i++)
	{ 
		if(whereAt < ids.length){
			idItems.push(ids[whereAt]);
			whereAt++;
		}
	}
	
	var target ="/dynamic/getObjects.php?";								
	for(j = 0; j <= (idItems.length - 1); j++)
	{
		target = target+ (j+1) +'='+idItems[j]+'&';
	}
	target = target.substr(0,target.length-1);
	
	$.getJSON(target,
		 function(data){
					$.each(data.items, function(i,item){
				
					if(item.contentType == 'Still'){
						$('#thumbWrapper').append('<a href="'+item.previewImage+'" id="'+idItems[i-1]+'" full="'+item.fullImage+'" rel="'+item.largeImage+'" type="'+item.contentType.toLowerCase()+'" class="thumb_item_link media_center_image" title="'+ item.name+'" onclick="return false"><div class="thumb_item" style="float:left"><div class="media_type_overlay" style="position:absolute"><img src="/images/image-overlay.png" width="113px" height="70px"></div><img src="'+ item.thumb +'" alt="'+ item.id+ '"  width="113" height="70" /></div></a>');						
					}else if(item.contentType == 'Hulu'){
						$('#thumbWrapper').append('<a href="'+item.name+'" id="'+idItems[i-1]+'" title="'+item.name+'" type="'+item.contentType.toLowerCase()+'" class="thumb_item_link media_center_image" rel="'+escape(item.contentPath)+'" onclick="return false"><div class="thumb_item" style="float:left"><div class="media_type_overlay" style="position:absolute"><img src="/images/video-overlay.png" width="113px" height="70px"></div><img src="'+ item.thumb +'" alt="'+ item.id+ '"  width="113" height="70" /></div></a>');
					}else if(item.contentType == 'YouTube'){
						$('#thumbWrapper').append('<a href="'+item.name+'" id="'+idItems[i-1]+'" title="'+item.name+'" type="'+item.contentType.toLowerCase()+'" class="thumb_item_link media_center_image" rel="'+escape(item.contentPath)+'" onclick="return false"><div class="thumb_item" style="float:left"><div class="media_type_overlay" style="position:absolute"><img src="/images/video-overlay.png" width="113px" height="70px"></div><img src="'+ item.thumb +'" alt="'+ item.id+ '"  width="113" height="70" /></div></a>');
					}else{
						$('#thumbWrapper').append('<a href="'+item.contentPath+'" id="'+idItems[i-1]+'" rel="'+item.previewImage+'" type="'+item.contentType.toLowerCase()+'" class="thumb_item_link media_center_image" title="'+ item.name+'" onclick="return false"><div class="thumb_item" style="float:left"><div class="media_type_overlay" style="position:absolute"><img src="/images/video-overlay.png" width="113px" height="70px"></div><img src="'+ item.thumb +'" alt="'+ item.id+ '"  width="113" height="70" /></div></a>');						
					}
				});
			})

	function nextThumbs(){
		if(nextBackClicker == false){
								
			if(mediaClickCounter <= Math.ceil((ids.length/4)-2))
			{
				
				nextBackClicker = true;
				//var idItems = new Array();
				mediaClickCounter++;
				var whereAt = mediaClickCounter * offset;
				
				for(i=0;i<4;i++)
				{ 
					if(whereAt < ids.length){
						idItems[i] = ids[whereAt];
						whereAt++;
					}
				}
				
				var target ="/dynamic/getObjects.php?";								
				for(j = 0; j <= (idItems.length - 1); j++)
				{
					target = target+ (j+1) +'='+idItems[j]+'&';
				}
				target = target.substr(0,target.length-1);
				$('.media_type_overlay').hide(1);	
				$("#thumbWrapper").animate({marginLeft:'-500px','opacity':'0'},200,function(){
					
						$(this).remove();
						$('#thumb_container_area').append('<div id="loading" style="display:none;margin:25px 0 0 180px"><img height="8" src="/images/loade_img.gif" alt="Loading..."/></div>');
						$("#loading").fadeIn(300,function(){
						$.getJSON(target,function(data){
								
								$.each(data.items, function(i,item){
									
									$("#thumb_container_area").wrapInner($('<div id="thumbWrapper"></div>'));
									if(item.contentType == 'Still'){
										$('#thumbWrapper').append('<a href="'+item.previewImage+'" id="'+idItems[i-1]+'" full="'+item.fullImage+'" rel="'+item.largeImage+'" type="'+item.contentType.toLowerCase()+'" class="thumb_item_link media_center_image" title="'+ item.name+'" onclick="return false"><div class="thumb_item" style="float:left"><div class="media_type_overlay" style="position:absolute;display:none"><img src="/images/image-overlay.png" width="113px" height="70px"></div><img src="'+ item.thumb +'" alt="'+ item.id+ '"  width="113" height="70" /></div></a>');						
										$('.thumb_item').animate({'opacity':'0'},0);
									}else if(item.contentType == 'Hulu')
									{
										$('#thumbWrapper').append('<a href="'+item.name+'" id="'+idItems[i-1]+'" title="'+item.name+'" type="'+item.contentType.toLowerCase()+'" class="thumb_item_link media_center_image" rel="'+escape(item.contentPath)+'" onclick="return false"><div class="thumb_item" style="float:left"><div class="media_type_overlay" style="position:absolute;display:none"><img src="/images/video-overlay.png" width="113px" height="70px"></div><img src="'+ item.thumb +'" alt="'+ item.id+ '"  width="113" height="70" /></div></a>');
										$('.thumb_item').animate({'opacity':'0'},0);
									}else if(item.contentType == 'YouTube'){
										$('#thumbWrapper').append('<a href="'+item.name+'" id="'+idItems[i-1]+'" title="'+item.name+'" type="'+item.contentType.toLowerCase()+'" class="thumb_item_link media_center_image" rel="'+escape(item.contentPath)+'" onclick="return false"><div class="thumb_item" style="float:left"><div class="media_type_overlay" style="position:absolute"><img src="/images/video-overlay.png" width="113px" height="70px"></div><img src="'+ item.thumb +'" alt="'+ item.id+ '"  width="113" height="70" /></div></a>');
										$('.thumb_item').animate({'opacity':'0'},0);
									}else{
										$('#thumbWrapper').append('<a href="'+item.contentPath+'" id="'+idItems[i-1]+'" rel="'+item.previewImage+'" type="'+item.contentType.toLowerCase()+'" class="thumb_item_link media_center_image" title="'+ item.name+'" onclick="return false"><div class="thumb_item" style="float:left"><div class="media_type_overlay" style="position:absolute;display:none"><img src="/images/video-overlay.png" width="113px" height="70px"></div><img src="'+ item.thumb +'" alt="'+ item.id+ '"  width="113" height="70" /></div></a>');						
										$('.thumb_item').animate({'opacity':'0'},0);
									}
									$('#loading').fadeOut(500,function(){
										$('.thumb_item').animate({'opacity':'1'},150).fadeIn();
										$('.media_type_overlay').fadeIn();
										nextBackClicker = false;
									})
								});
							})
						})
					})
			}else{return false;}
		}else{return false;}
	}
	
	function backThumbs(){
		if(nextBackClicker == false){							
			if(mediaClickCounter > 0)
			{
				nextBackClicker = true;
				//var idItems = new Array();
				mediaClickCounter--;
				var whereAt = mediaClickCounter * offset;
				
				for(i=0;i<4;i++)
				{ 
					if(whereAt < ids.length){
						idItems[i] = ids[whereAt];
						whereAt++;
					}
				}
				
				var target ="/dynamic/getObjects.php?";								
				for(j = 0; j <= (idItems.length - 1); j++)
				{
					target = target+ (j+1) +'='+idItems[j]+'&';
				}
				target = target.substr(0,target.length-1);
				$('.media_type_overlay').hide(1);	
				$("#thumbWrapper").animate({marginLeft:'500px','opacity':'0','filter':'alpha(opacity=0)'},200,
					function(){
						$(this).remove();
						$('#thumb_container_area').append('<div id="loading" style="display:none;margin:25px 0 0 180px"><img height="8" src="/images/loade_img.gif" alt="Loading..."/></div>');
						$("#loading").fadeIn(300,function(){
						$.getJSON(target,
						 function(data){
									$.each(data.items, function(i,item){
									
									$("#thumb_container_area").wrapInner($('<div id="thumbWrapper"></div>'));
									if(item.contentType == 'Still'){
										$('#thumbWrapper').append('<a href="'+item.previewImage+'" id="'+idItems[i-1]+'" full="'+item.fullImage+'" rel="'+item.largeImage+'" type="'+item.contentType.toLowerCase()+'" class="thumb_item_link media_center_image" title="'+ item.name+'" onclick="return false"><div class="thumb_item" style="float:left"><div class="media_type_overlay" style="position:absolute;display:none"><img src="/images/image-overlay.png" width="113px" height="70px"></div><img src="'+ item.thumb +'" alt="'+ item.id+ '"  width="113" height="70" /></div></a>');						
										$('.thumb_item').animate({'opacity':'0'},0);
									}else if(item.contentType == 'Hulu')
									{
										
																		$('#thumbWrapper').append('<a href="'+item.name+'" id="'+idItems[i-1]+'" title="'+item.name+'" type="'+item.contentType.toLowerCase()+'" class="thumb_item_link media_center_image" rel="'+escape(item.contentPath)+'" onclick="return false"><div class="thumb_item" style="float:left"><div class="media_type_overlay" style="position:absolute;display:none"><img src="/images/video-overlay.png" width="113px" height="70px"></div><img src="'+ item.thumb +'" alt="'+ item.id+ '"  width="113" height="70" /></div></a>');
										$('.thumb_item').animate({'opacity':'0'},0);
									}else if(item.contentType == 'YouTube'){
										$('#thumbWrapper').append('<a href="'+item.name+'" id="'+idItems[i-1]+'" title="'+item.name+'" type="'+item.contentType.toLowerCase()+'" class="thumb_item_link media_center_image" rel="'+escape(item.contentPath)+'" onclick="return false"><div class="thumb_item" style="float:left"><div class="media_type_overlay" style="position:absolute"><img src="/images/video-overlay.png" width="113px" height="70px"></div><img src="'+ item.thumb +'" alt="'+ item.id+ '"  width="113" height="70" /></div></a>');
										$('.thumb_item').animate({'opacity':'0'},0);
									}else{
										$('#thumbWrapper').append('<a href="'+item.contentPath+'" id="'+idItems[i-1]+'" rel="'+item.previewImage+'" type="'+item.contentType.toLowerCase()+'" class="thumb_item_link media_center_image" title="'+ item.name+'" onclick="return false"><div class="thumb_item" style="float:left"><div class="media_type_overlay" style="position:absolute;display:none"><img src="/images/video-overlay.png" width="113px" height="70px"></div><img src="'+ item.thumb +'" alt="'+ item.id+ '"  width="113" height="70" /></div></a>');						
										$('.thumb_item').animate({'opacity':'0'},0);
									}
									
									$('#loading').fadeOut(100,function(){
										$('.thumb_item').animate({'opacity':'1'},150).fadeIn();
										$('.media_type_overlay').fadeIn();
										nextBackClicker = false;
									})
								});
							})
						})
					})
			}else{return false;}
		}else{return false;}
	}
	
	var initLoadTimer = setTimeout(function(){
		if ($('.thumb_item_link:first').attr("type") == 'hulu') 
		{
			$("#adComponent").show();
			$("#episodes_ad300x250").hide();
			$("#hulu_brand_bar").fadeIn(150);
			if (huluPlaying == true) {
				return false;
			}
			else {
				huluPlaying = true;
				var theEmbedCoded = $('.thumb_item_link:first').attr('rel');
				$('#hulu_brand_bar a').attr('href',"http://www.hulu.com/watch?content_id="+theEmbedCoded);
				$(document).ready(function(){
					$(".hulu_player").show();
					//$('#content_play_area').prepend('<div class="hulu_player"><div id="videoPlayerComponent"></div>');
					$(".hulu_player").css({
						'zIndex': '1'
					})
					$(".hulu_player embed").attr('width', '600').attr('height', '360').attr('wmode', 'opaque').attr('bgcolor',"#000000");
					$(".hulu_player object").attr('width','600').attr('height','360');
					$("#media_content_holder").css({
						'opacity': '0'
					});
					loadHuluContent(theEmbedCoded);	
				});
			}
		}else if($('.thumb_item_link:first').attr("type") == 'youtube'){
			var theEmbedCoded = $('.thumb_item_link:first').attr('rel');
			youtube_playing = true;
			document.getElementById('flash_media_player').stop_media();
			$('#flash_media_player').css({'opacity':'0'});
			$('#youtube_player').show(1,function(){
				setTimeout(function(){
				loadNewVideo(theEmbedCoded,0)},1000);										   
			});
		}else {
			huluPlaying = false;
			$(".hulu_player").hide();
			$("#media_content_holder").css({'opacity':'1'});
			var contentsType = $('.thumb_item_link:first').attr('type');
			if (contentsType == 'still') {
				contentsType = 'image';
			};
			var contentPath = $('.thumb_item_link:first').attr('href');
			var previewImage = $('.thumb_item_link:first').attr('rel');
			var itemId = $('.thumb_item_link:first').attr('id');
			var itemTitle = $('.thumb_item_link:first').attr('title');
			var downloadFile = $('.thumb_item_link:first').attr('full');
			var autoplay = false;
			var hd_enabled = true;
			clickedThumbTracker = itemId;

			load_media(contentPath,contentsType,previewImage,itemId,itemTitle,autoplay,hd_enabled,downloadFile);
		}
	},1500);

})

$(document).ready(function(){
	$('#play_btn').hide();
	$('#unmute_btn').hide();
	
	$('.vidLink').live('click',function(){
		loadNewVideo($(this).attr('id'),0);
		$('#play_btn').hide();
		$('#pause_btn').show();
	});
	$('#play_btn').live('click',function(){
		play();
		$('#play_btn').hide();
		$('#pause_btn').show();
	});
	$('#pause_btn').live('click',function(){
		pause();
		$('#play_btn').show();
		$('#pause_btn').hide();
	});
	$('#mute_btn').live('click',function(){
		mute();
		$('#unmute_btn').show();
		$('#mute_btn').hide();
	});
	$('#unmute_btn').live('click',function(){
		unMute();
		$('#mute_btn').show();
		$('#unmute_btn').hide();
	});
	$('#share_btn').live('click',function(){
		getEmbedCode();
	});
	
	
	
	
});
	$('#scrubber').live('click',function(e){
		var scrubber_width = $('#scrubber').width() - $('#vid_scrubber').width();
		var vid_scrubber_pos = $('#scrubber').offset();
		seekTo(parseInt(getDuration() * ((e.pageX-vid_scrubber_pos.left)/316)));
	});

	$('#vol_scrubber').live('click', function(e){
		var vol_scrubber_pos = $('#vol_scrubber').offset();
		var vol_scrubber_width = $('#vol_scrubber').width()- $('#vid_scrubber').width();
		setVolume(e.pageX - vol_scrubber_pos.left);
	});


	function animateScrubber(total,loaded){
		$('#vid_scrubber').css({'marginLeft':parseInt((loaded/total) * 316)+'px'});
		$('#scrubber_progress').css({'width':parseInt((loaded/total) * 316)+'px'});
	};
	function animateVolumeScrubber(vol){
		$('#vol_scrub').css({'marginLeft':parseInt((vol/100)*87)+'px'});
		$('#vol_scrubber_progress').css({'width':parseInt((vol/100)*87)+'px'});
	};
	

	
	function onYouTubePlayerReady(playerId) {
	  ytplayer = document.getElementById("myytplayer");
	  setInterval(updateytplayerInfo, 250);
	  updateytplayerInfo();
	  ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
	  ytplayer.addEventListener("onError", "onPlayerError");
	};
	
	function onytplayerStateChange(newState) {
	  setytplayerState(newState);
	};
	
	function onPlayerError(errorCode) {
	  alert("An error occured: " + errorCode);
	};
	
	function updateytplayerInfo() {
	  animateScrubber(getDuration(),getCurrentTime());
	  animateVolumeScrubber(getVolume());
	};
	
	// functions for the api calls
	function loadNewVideo(id, startSeconds) {
	  if (ytplayer) {
		ytplayer.loadVideoById(id, parseInt(startSeconds));
	  };
	};
	
	function cueNewVideo(id, startSeconds) {
	  if (ytplayer) {
		ytplayer.cueVideoById(id, startSeconds);
	  };
	};
	
	function play() {
	  if (ytplayer) {
		ytplayer.playVideo();
	  };
	};
	
	function pause() {
	  if (ytplayer) {
		ytplayer.pauseVideo();
	  };
	};
	
	function stop() {
	  if (ytplayer) {
		ytplayer.stopVideo();
	  };
	};
	
	function getPlayerState() {
	  if (ytplayer) {
		return ytplayer.getPlayerState();
	  };
	};
	
	function seekTo(seconds) {
	  if (ytplayer) {
		ytplayer.seekTo(seconds, true);
	  };
	};
	
	function getBytesLoaded() {
	  if (ytplayer) {
		return ytplayer.getVideoBytesLoaded();
	  };
	};
	
	function getBytesTotal() {
	  if (ytplayer) {
		return ytplayer.getVideoBytesTotal();
	  };
	};
	
	function getCurrentTime() {
	  if (ytplayer) {
		return ytplayer.getCurrentTime();
	  };
	};
	
	function getDuration() {
	  if (ytplayer) {
		return ytplayer.getDuration();
	  };
	};
	
	function getStartBytes() {
	  if (ytplayer) {
		return ytplayer.getVideoStartBytes();
	  };
	};
	
	function mute() {
	  if (ytplayer) {
		ytplayer.mute();
	  };
	};
	
	function unMute() {
	  if (ytplayer) {
		ytplayer.unMute();
	  };
	};
	
	function getEmbedCode() {
	  alert(ytplayer.getVideoEmbedCode());
	};
	
	function getVideoUrl() {
	  alert(ytplayer.getVideoUrl());
	};
	
	function setVolume(newVolume) {
	  if (ytplayer) {
		ytplayer.setVolume(newVolume);
	  };
	};
	
	function getVolume() {
	  if (ytplayer) {
		return ytplayer.getVolume();
	  };
	};
	
	function clearVideo() {
	  if (ytplayer) {
		ytplayer.clearVideo();
	  };
	};
	




