

/* -----------------------------------------
	CJ Image (JS) v1.0 JavaScript Function
	Written by: Doug Jones	www.cjboco.com
------------------------------------------ */

var CJ_Image = {
	Calculate: function(options) {
	
		// declare some local variables
		var ratioX, ratioY, scale, newWidth, newHeight;
		
		// Check to make sure all the required variables were sent and pass validation
		if (typeof options.srcWidth !== "number" || typeof options.srcHeight !== "number" || typeof options.destWidth !== "number" || typeof options.destHeight !== "number" || typeof options.method !== "string") {
			return;
		}
		
		// Grab scale ratios
		ratioX = options.destWidth / options.srcWidth;
		ratioY = options.destHeight / options.srcHeight;
		
		// Determine which algorithm to use
		if (options.method === "fit") {
			scale = ratioX < ratioY ? ratioX: ratioY;
		} else if (options.method === "fill") {
			scale = ratioX > ratioY ? ratioX: ratioY;
		}
		
		// Set new dimensions
		newWidth = parseInt(options.srcWidth * scale, 10);
		newHeight = parseInt(options.srcHeight * scale, 10);
		
		// Return the new dimensions, plus the offsets, and if the destination box
		// is smaller or equal to the source image dimensions
		return {
			width: newWidth,
			height: newHeight,
			offset: {
				x: parseInt((options.destWidth - newWidth) / 2, 10),
				y: parseInt((options.destHeight - newHeight) / 2, 10)
			},
			fits: options.srcWidth >= options.destWidth && options.srcHeight >= options.destHeight ? true: false
		};
	}
};

/*****************
From Lightbox...
******************/

function getPageSize() {

	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) { // all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth;
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = xScroll;
	} else {
		pageWidth = windowWidth;
	}
	
	return [pageWidth,pageHeight];
}
/*********************/

function loadMovie(id){			
	data = {'a':1,'i':id};
	
	var media_type = $("#currentposttype").html();
		
	$.post("/wp-content/themes/mindshare-classic/media-theater/media-ajax.php",data,  
			 function(result){	
				var player = document.getElementById("ply");
				var poster, movie, data, new_active;				
				var old_active = $("#currentpostinfo").html();
				
				new_active = result.post_id;
				$("#mediapost").fadeOut('slow', function(){
					$("#mediapost").html(result.post_display).fadeIn('slow');
					$(".comment_trigger").click(toggleComments);	
					if($("#comments_open").length > 0)
					{
						toggleComments();
					}
				});
					 		
				 poster = result.thumbnail;
				 movie = result.movie_path;
				// updateMediaDisplay('video');
				// if(player == null)
				$("#postvisual").html(result.media_display);
				
					//else
				//	 player.sendEvent("Load",{file:movie, image:poster});
				 
				$("#teaser_"+old_active).removeClass("active");
				$("#teaser_"+new_active).addClass("active");
				
				$("#ajax-activity").removeClass('ajax-loading-video');
				$("#ajax-activity").removeClass('ajax-loading-photo');
				
				$("#currentpostinfo").html(new_active);				
				
			 }, "json"
		);
	
	
	
	
}

function updateMediaDisplay(media_type){
	if(media_type == 'slideshow'){		
		if($("#currentposttype").html() != 'slideshow' || $('#mediatheater').hasClass('single-media')){
			$('#media-theater').removeClass('single-media');
			if($('#media-overlay').width() == 0){
				$("#postmenu").addClass("photo-mode");
				
				var arrayPageSize = getPageSize();
		        $('#media-overlay').css({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' , opacity:0});
				$("#media-overlay").animate({
					opacity:'.8'
				}, "slow", function(){
					$("#medialist").removeClass("video-mode");
					$("#medialist").addClass("photo-mode");
					
					
					$(".mediatab").addClass("photo-mode");
					$(".mediatabs").addClass("photo-mode");
					$(".mediatab-slideshow-active").addClass("photo-mode");
					$(".mediaplayer").addClass("photo-mode");
					$("#mediapost").addClass("photo-mode");
					
					$(".media-horizontalrule").show();
					
					$("#postvisual").html(" ");
					$("#postvisual").addClass("loading-photo");
					
					
					
					
					$('.mediaplayer').animate({
						height:'441px'
					},"slow", function(){				
						
					});
					
					$('#postvisual').animate({
						width:'602px',
						height:'455px'
					},"slow");

					$('#caption_container').removeClass('hidden');
					
				});
			}
		}
		else{
			var arrayPageSize = getPageSize();
			 $('#media-overlay').css({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' , opacity:0});
			
			$("#media-overlay").animate({
				opacity:'.8'
			}, "slow");
		}	
	}else if($("#currentposttype").html() == 'slideshow' && media_type == 'video'){
		
		$("#media-overlay").animate({
			width:0,
			height:0
		}, "slow");
		
		$('.mediaplayer').animate({
			height:'300px'
		},"slow");
		
		$('#postvisual').animate({
			width:'450px',
			height:'275px'
		},"slow");
		

		$("#medialist").removeClass("photo-mode");
		$("#medialist").addClass("video-mode");
		
		
		$(".mediatab").removeClass("photo-mode");
		$(".mediatabs").removeClass("photo-mode");
		$(".mediatab-slideshow-active").addClass("photo-mode");
		$(".mediaplayer").removeClass("photo-mode");
		$("#mediapost").removeClass("photo-mode");
		$("#postmenu").removeClass("photo-mode");
		$(".media-horizontalrule").hide();
		$('#caption_container').addClass('hidden');
	}
	
	$("#currentposttype").html(media_type);
}



function loadSlideshow(id){
	data = {'a':1,'i':id};
	
	var media_type = $("#currentposttype").html();
	
	
	//updateMediaDisplay('slideshow');
	
	$.post("/wp-content/themes/mindshare-classic/media-theater/media-ajax.php",data,  
			 function(result){	
				
				var old_active = $("#currentpostinfo").html();								
				new_active = result.post_id;
				
				$("#mediapost").fadeOut('slow', function(){
					$("#mediapost").html(result.post_display);
					$(".comment_trigger").click(toggleComments);
					if($("#comments_open").length > 0)
					{
						toggleComments();
					}
					$("#mediapost").fadeIn('slow', function(){
						
										
						var arrayPageSize = getPageSize();
				       		 $('#media-overlay').css({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px'})
					});	
				});
				
				$("#caption").animate({opacity:0},'slow', function(){
					$("#caption").html(result.caption);
					$("#caption").animate({
							opacity:1},'slow');
				});
	 		
				$("#postvisual").animate({
					opacity:0},'slow', function(){
					$("#postvisual").html(result.media_display)
							
					$("#slide").load(function(){
					
						var resized = CJ_Image.Calculate({
							srcWidth: $("#slide").width(),
							srcHeight: $("#slide").height(),
							destWidth: 600,
							destHeight: 400,
							method: "fit"
						});
						
						$("#slide").css({"width":resized.width,"height":resized.height,
							 "top":resized.offset.y,"left":resized.offset.x});
						
						$("#postvisual").animate({
							opacity:1},'slow');
						
					});
					
				
					
				});			
				
				$("#teaser_"+old_active).removeClass("active");
				$("#teaser_"+new_active).addClass("active");
				
				$("#ajax-activity").removeClass('ajax-loading-video');
				$("#ajax-activity").removeClass('ajax-loading-photo');
				
				$("#currentpostinfo").html(new_active);

				
								
			 }, "json"
		);
	
}

function loadTab(id){
	var old_active_tab;
	var data;
	var media_type = $("#currentposttype").html();
	if(media_type == 'video')
		$("#ajax-activity").addClass('ajax-loading-video');
	else
		$("#ajax-activity").addClass('ajax-loading-photo');
	
	old_active_tab = $("#current-tab-info").html();
	data = {'a':2, 'i':id};
	
	$("#tab_"+old_active_tab).removeClass('active');
	$("#tab_"+id).addClass('active');
	
	$("#current-tab-info").html(id);
	
	$.post("/wp-content/themes/mindshare-classic/media-theater/media-ajax.php",data,  
			 function(result){	
				$("#postmenu").fadeOut('slow', function(){
					$("#postmenu").html(result.post_list);
					 updateMediaDisplay(result.media_type);
					loadMedia(result.current_post_id, result.media_type);
										
					$("#postmenu").show();											
					$('#medialist').jScrollPane();					
				});
			}, "json"
		);
	
	
	
	
}

function loadMedia(post_id, media_type){
	if(media_type == 'video')
		loadMovie(post_id);
	else if(media_type == 'slideshow')
		loadSlideshow(post_id);
}




function nextSlide(){
	var current_slide = $("#slidenum").html();
	var current_post = $("#currentpostinfo").html();
				
	data = {'a':3, 'i':current_post ,'s':current_slide};
	updateSlide(data);
}
function prevSlide(){
	var current_slide = $("#slidenum").html();
	var current_post = $("#currentpostinfo").html();
			
	data = {'a':4, 'i':current_post ,'s':current_slide};
	updateSlide(data);
}
function updateSlide(data){
	$(".slideshow-photo").html(" ");
	//$(".slideshow-photo").addClass("loading-photo");
	$.post("/wp-content/themes/mindshare-classic/media-theater/media-ajax.php",data, 
			 function(result){	
				$(".slideshow-nav").html(result.nav);				
				
				$("#caption").animate({opacity:0},'slow', function(){
					$("#caption").html(result.caption);
					$("#caption").animate({
							opacity:1},'slow');
				});

				$(".slideshow-photo").animate({
					opacity:0},'slow', function(){
						$(".slideshow-photo").removeClass("loading-photo");
					$(".slideshow-photo").html(result.photo);
					
					$("#slide").load(function(){
					
						var resized = CJ_Image.Calculate({
							srcWidth: $("#slide").width(),
							srcHeight: $("#slide").height(),
							destWidth: 600,
							destHeight: 400,
							method: "fit"
						});
						
						$("#slide").css({"width":resized.width,"height":resized.height,
							 "top":resized.offset.y,"left":resized.offset.x});
						
						$(".slideshow-photo").animate({
							opacity:1},'slow');
						
					});
					
				});					
			 }, "json"
		);			
}
function toggleComments()
{
	var media_type = $("#currentposttype").html();
	$('#comments_long').toggle();
	if(media_type == 'slideshow')
	{
		var arrayPageSize = getPageSize();
		$('#media-overlay').css({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' , opacity:".8"});
	}
	
}
$(document).ready(function(){
	$('#medialist').jScrollPane();
	$("#media-overlay").click(function(){	
		$("#media-overlay").animate({
			width:0,
			height:0,
			opacity:0
		}, "slow");
	});
	$(".comment_trigger").click(toggleComments);

	if($("#comments_open").length > 0)
	{
		$(".comment_trigger").click();
	}
	
});
$("#comments_long").ready(function(){
	
});

