
var wfxCarouselIndex = 0;
//var wfxCarouselFrameCount = 2;

function wfxCarouselPrev(id, frameCount) {
	if(wfxCarouselIndex > 0) {
		var carousel = $("#"+id);
		var plate = carousel.find(".CarouselPlate");
		var frame = carousel.find(".CarouselFrame");
		
		wfxCarouselHideMovies(frameCount);
	
		var width = frame.width();
		var pos = parseInt(plate.css('left'));
		if(pos < 0) {
			var offset =  pos + width;
			plate.animate({'left': offset}, 500);
		}
	
		wfxCarouselIndex--;
	//	wfxCarouselShowMovie(wfxCarouselIndex);

		carousel.find(".CarouselNext").css('visibility', 'visible');
		if(wfxCarouselIndex <= 0) {
			carousel.find(".CarouselPrev").css('visibility', 'hidden');
			carousel.css('margin-left', '-25px');
		}
		
		wfxCarouselUpdateFrameNumbers(id, frameCount, wfxCarouselIndex);
	}
}

function wfxCarouselNext(id, frameCount) {
	//console.log(wfxCarouselIndex+" < "+frameCount);
	var end = frameCount-1;
	if(wfxCarouselIndex < end) {
		var carousel = $("#"+id);
		var plate = carousel.find(".CarouselPlate");
		var frame = carousel.find(".CarouselFrame");
		
		wfxCarouselHideMovies(frameCount);
	
		var width = frame.width();
		var pos = parseInt(plate.css('left'));
		var max = -frameCount * width;
		if(pos > max) {
			var offset =  pos - width;
			plate.animate({'left': offset}, 500);
		}
		
		wfxCarouselIndex++;
	//	wfxCarouselShowMovie(wfxCarouselIndex);

		carousel.find(".CarouselPrev").css('visibility', 'visible');
		if(wfxCarouselIndex >= end) {
			carousel.find(".CarouselNext").css('visibility', 'hidden');
		}
		carousel.css('margin-left', '0px');
		
		wfxCarouselUpdateFrameNumbers(id, frameCount, wfxCarouselIndex);
	}
}

function wfxCarouselGoToFrame(id, frameCount, frameNum, skipAnimation) {
	//console.log("wfxCarouselGoToFrame:"+id);
	//console.log("id:"+id+", frameCount:"+frameCount+", frameNum:"+frameNum); 
	//if(wfxCarouselIndex < frameCount) {
		var carousel = $("#"+id);
		var plate = carousel.find(".CarouselPlate");
		var frame = carousel.find(".CarouselFrame");
		
		wfxCarouselHideMovies(frameCount);
	
		var width = frame.width();
		var pos = parseInt(plate.css('left'));
		var offset = -frameNum * width;
		
		if(skipAnimation == 1) {
			plate.css('left', offset);
		}
		else {
			plate.animate({'left': offset}, 500);
		}
		
		wfxCarouselIndex = frameNum;

		carousel.find(".CarouselPrev").css('visibility', 'visible');
		if(wfxCarouselIndex >= frameCount) {
			carousel.find(".CarouselNext").css('visibility', 'hidden');
		}
		
		wfxCarouselUpdateFrameNumbers(id, frameCount, frameNum);
		wfxCarouselIndex = frameNum;
	//}
		var doPulseNext = false;
		var end = frameCount-1;
		if(wfxCarouselIndex >= end) {
			carousel.find(".CarouselNext").css('visibility', 'hidden');
		}
		else {
			carousel.find(".CarouselNext").css('visibility', 'visible');
			doPulseNext = true;
		}
		if(!wfxCarouselIndex || wfxCarouselIndex <= 0) {
			carousel.find(".CarouselPrev").css('visibility', 'hidden');
		}
		else {
			carousel.find(".CarouselPrev").css('visibility', 'visible');
		}

		carousel.find(".CarouselPrev").css('visibility', 'visible');
		if(wfxCarouselIndex >= end) {
			carousel.find(".CarouselNext").css('visibility', 'hidden');
		}
		else {
			doPulseNext = true;
		}
		carousel.css('margin-left', '0px');
		
		if(doPulseNext) {
			wfxCarouselPulseNextStart(id);
		}
}

var wfxCarouselPulseNextCount = 0;
function wfxCarouselPulseNextStart(id) {
	wfxCarouselPulseNextCount = 0;
	wfxCarouselPulseNext(id, 0.1);
}
function wfxCarouselPulseNext(id, val) {
	//console.log("wfxCarouselPulseNext:"+val);
	var carousel = $("#"+id);
	carousel.find(".CarouselNext").animate({opacity:val}, 500);
	
	if(wfxCarouselPulseNextCount < 11) {
		wfxCarouselPulseNextCount++;
		if(val == 1.0) val = 0.1;
		else val = 1.0;
		wfxCarouselPulseNext(id, val);
	}
}

function wfxCarouselUpdateFrameNumbers(id, frameCount, frameNum) {
// update the frame number buttons, if there are any
	var frameNums = $("#homeFrameButtons");
	if(frameNums) {
	//	var children = frameNums.children();
	//	children.attr("class", "homeFrameButton");
	//	children[frameNum].attr("class", "homeFrameButtonOn");
		frameNums.children().each( function(i) {
			if(i == frameNum) {
				$(this).attr("class", "homeFrameButtonOn");
			}
			else {
				$(this).attr("class", "homeFrameButton");
			}
		});
	}
	for(var x=0; x <= frameCount; x++) {
		$('#fullText'+x).hide(); 
		$('#sumText'+x).show(); 
	}
	wfxCarouselUpdateFrameName();
}
function wfxCarouselUpdateFrameName() {
	if(typeof(wfxCarouselNames) != "undefined") {
		$("#homeProjectHeading").html(wfxCarouselNames[wfxCarouselIndex]);
	}
}
function wfxCarouselHideMovies(frameCount) {
// This hides all the embedded movies before scrolling
	for(var x=0; x<=frameCount; x++) {
		var frame = $("#CarouselFrame"+x);
//		if(frame.find(".moviePlayer").css('display') == 'block') {
			//frame.find("embed").Stop();
//			if(x == 0) {
//				document.Movie0.Stop();
//			}
			//document['Movie'+x].Stop();
//		}
		frame.find(".moviePlayer").empty();
		frame.find(".moviePlayer").css('display', 'none');
		frame.find(".moviePoster").css('display', 'block');
	}

}

function wfxCarouselShowMovie(index, srcPath) {
// This shows the embedded movie for the current frame
	var frame = $("#CarouselFrame"+index);
	frame.find(".moviePlayer").css('display', 'block');
	frame.find(".moviePoster").css('display', 'none');
	
	wfxCreateMovie(frame.find(".moviePlayer"), srcPath);
}
function wfxShowMovie(id, srcPath) {
// This shows the embedded movie for the current frame
	//console.log("wfxShowMovie:"+id+" :"+srcPath);
	var frame = $("#"+id);
	frame.find(".moviePlayer").css('display', 'block');
	frame.find(".moviePoster").css('display', 'none');

	wfxCreateMovie(frame.find(".moviePlayer"), srcPath);
}

function wfxSetLatestProject(name) {
	$("#homeProjectHeading").html(name);
}

function wfxCreateMovie(parentObj, srcPath) {
	var html = "";
	html += "<object width='640' height='376' codebase='http://www.apple.com/qtactivex/qtplugin.cab' classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B' width='640' height='376'>";
	html += "<param name='src' value='"+srcPath+"'>";
	html += "<param name='Autoplay' value='true'>";
	html += "<embed src='"+srcPath+"' "; 
	html += "width='640' height='376' autoplay='true' controller='true' loop='false' enablejavascript='true' pluginspage='http://www.apple.com/quicktime/download/'>"; 
	html += "</object>";
	
	parentObj.html(html);
}

function wfxSetPageType(ptid) {
	//$("#ccm-select-page-type a.").click();
	$("input[name=ctID]").val(ptid);
	
		$('#ccm-page-type-scroller').css('display','none');
		$('#ccm-show-page-types').css('display','block');
		$('#ccm-selectedPgType').html( $('#pgTypeName'+ptid).html() );
		
		$('#ccm-metadata-fields').css('display','block');		
		$('.ccm-field-meta').css('display','none');
		//set all attributes as not active
		$('.ccm-meta-field-selected').each(function(i,el){ el.value=0; })
		
		//all shown attributes
		/*
		var shownAttrKeys=$('#shownAttributeKeys'+ptid).val().split(',');		
		for(var i=0;i<shownAttrKeys.length;i++){
			$('#ccm-field-ak'+shownAttrKeys[i]).css('display','block');
			$('#ccm-meta-field-selected'+shownAttrKeys[i]).val(shownAttrKeys[i]);
		}
		*/
		
		//show required attributes
		$('.ccm-meta-close').css('display','block');
		var requiredAttrKeys=$('#requiredAttributeKeys'+ptid).val().split(',');		
		for(var i=0;i<requiredAttrKeys.length;i++){
			$('#ccm-field-ak'+requiredAttrKeys[i]).css('display','block');
			$('#ccm-meta-field-selected'+requiredAttrKeys[i]).val(requiredAttrKeys[i]);		
			$('#ccm-remove-field-ak'+requiredAttrKeys[i]).css('display','none');
		}
		
		//remove all options from the custom attributes select menu
		$("#ccm-meta-custom-fields option").each(function() {
			if(this.value.length>0) $(this).remove();
		});
		
		// add the hidden attribute back to the custom attributes select menu	
		$('.ccm-meta-close').each(function(){ 
			var metaCstmSelect=$("#ccm-meta-custom-fields").get(0); 
			var thisField = $(this).attr('id').substring(19);
			var thisName = $(this).attr('ccm-meta-name'); 
			if($('#ccm-field-ak'+thisField).css('display')=='block') return;
			metaCstmSelect.options[metaCstmSelect.options.length] = new Option(thisName, thisField);
		}); 
}

function wfxEditPage() {
	//$("a#ccm-nav-edit").click(function() {
		if (!ccm_topPaneDeactivated) {
			setTimeout(function() {
				// stupid safari? wtf?
				window.location.href = CCM_REL + '/index.php?cID=' + CCM_CID + '&ctask=check-out&ccm_token=' + CCM_SECURITY_TOKEN;
			}, 50);
		}
	//});
}

function wfxClickPortfolioThumb(index, imgSrc, movieSrc) {
	//console.log("wfxClickPortfolioThumb:"+index);
	//alert('wfxClickPortfolioThumb');
	$('[id^=portfolioThumb]').removeClass('selected');
	//$(this)
	$('#portfolioThumb'+index).addClass('selected');
	//console.log('#portfolioThumb'+index);
	
	// Update the image/movie
	if(movieSrc) {
	// Show the movie frame
		$('.movieContainer').css('display', 'block');
		$('.imageContainer').css('display', 'none');
		
		var frame = $("#"+'portfolioContent');
		frame.find(".moviePlayer").css('display', 'none');
		frame.find(".moviePoster").css('display', 'block');

		$('.moviePosterA').click( function() { wfxShowMovie('portfolioContent', movieSrc); } );
		$('.moviePosterImg').css('background', 'url('+imgSrc+')');
	}
	else {
	// Show the image frame
		$('.movieContainer').css('display', 'none');
		$('.imageContainer').css('display', 'block');
		
		// set the image src
		$('.imageContainerImg').attr('src', "themes/realityx/img/loading.gif");
		$('.imageContainerImg').attr('src', imgSrc);
	}
	
	// switch the captions
	$('[id^=portfolioCaption]').css('display', 'none');
	$('#portfolioCaption'+index).css('display', 'block');
}

function wfxShowAndHide(showID, hideID) {
	//console.log("wfxShowAndHide("+showID+", "+hideID+")");
	$("#"+showID).show();
	$("#"+hideID).hide();
}

//-----------------------------------------------------------------------------------------------------------------------------
//: ArticleSetView
//-----------------------------------------------------------------------------------------------------------------------------
// This provides functions supporting ArticleSetViews during regular page interaction
//-----------------------------------------------------------------------------------------------------------------------------
function wfxSelectTab(id, index) {
	//console.log("wfxSelectTab("+id+", "+index+")");
	
	$("#"+id).find('.wfxViewTabPage').each(function(i) {
		var item = $(this);
		if(index == i) {
			item.css('display', 'block');
			item.addClass('selected');
		}
		else {
			item.css('display', 'none');
			item.removeClass('selected');
		}
	});
	$("#"+id).find('.wfxViewTab').each(function(i) {
		//console.log(i);
		var item = $(this);
		if(index == i) {
			item.addClass('selected');
		}
		else {
			item.removeClass('selected');
		}
	});
	
	return false;
}
