if( !console ) { var console = { log: function(msg) { alert(msg); } } }

/*______________________________________
		SWF Flash Player
______________________________________*/
function createFlashMovContainer(movname,movieplayer,slideAttr,swfWidth,swfHeight){
  var flashvars = {
      movURL: movname,
      movieDirect: "http://rebeccapollock.com/"
  };
  var params = {
	  menu: "false",
	  wmode: "transparent",
	  allowScriptAccess: "always"
  };
  var attributes = {
	  id: slideAttr,
	  name: slideAttr
  };
  swfobject.embedSWF(movieplayer, slideAttr, swfWidth, swfHeight, "9.0.0", "images/expressInstall.swf", flashvars, params, attributes);
}

/*______________________________________
		Quicktime Mov Player
______________________________________*/
function createEmbedMovContainer(movname,placeholder,slideAttr,qtWidth,qtHeight){
	var html ="";
	html += "<embed src='"+movname+"' type='image/x-macpaint' pluginspage='http://www.apple.com/quicktime/download' autoplay='true' loop='false' lang='en' width='"+qtWidth+"' height='"+qtHeight+"' correction='full' scale='tofit' bgcolor='000000' cache='true'></embed>";
	$("#"+slideAttr).empty().append(html);
}

function toggleSlide(slide){ 
	$(".slideList li").each(function(s,ele){
		if($(ele).hasClass("active")){
			$(ele).removeClass("active");
		}
		$(".slideList li").eq(slide).addClass("active");
	});
	$(".workThumb .toggleList").each(function(t, media){
		$(media).hide();
	});
	$(".workThumb .toggleList").eq(slide).fadeIn("slow");
}

//onload
$(document).ready(function(){

var xmlFile = $.url.param("type");
var projectName = $.url.param("project");

//search for variables
if(xmlFile){ getXMLFile(xmlFile) } else {
	var xmlFile = $.url.attr("file").split(".html")[0];
	getXMLFile(xmlFile);
}
function getXMLFile(xmlFile){
	$.ajax({
		url: '/xml/rp_'+xmlFile+'.xml',
		type: 'GET',
		dataType: 'xml',
		timeout: 3000,
		error: function(){
			alert('Error loading XML project document');
		},
		success: function(xml){ XMLContentWorkLoader(xml); }
	});

}
/*XML*/
function XMLContentWorkLoader(xml){
	$(xml).find(xmlFile).children().each(function(c,childrenList){ 
		var getProject = $(childrenList).get(0).tagName;
		/*first assign active state to accordionDetail and display project content*/
		if (projectName == getProject){
			//timer set to ensure active state
			setTimeout( function(){$("#"+xmlFile+" li").eq(c).addClass("active")}, 1000);
			currentclient = childrenList;
			showProjectContent(currentclient);
		} 
		/*if param project = null get the first item from XML*/
		if(projectName == null) { $("#"+xmlFile+" li:eq(0)").addClass("active"); if(c==0){ showProjectContent(childrenList)} }
	});
} //XMLContentWorkLoader

function showProjectContent(currentclient){
	/*change document title with client/project name*/
	title = "Rebecca Pollock Art + Design. - Work - "+$(currentclient).find("nav").text();
	document.title = title;
	
	/*set up project slide list*/
	$(currentclient).find("assets").children().each(function(a,assetsType){
		
		list = "<li><span onclick='toggleSlide("+(a)+")'>"+(a+1)+"</span></li>";
		$(".slideList").append(list);
		$(".slideList li:first").addClass('active'); //set the first/default li to active state
		
		var type = $(assetsType).get(0).tagName;
		var projectDisplayClass = "projectDisplay";
		switch (type)
		{
			case "thumbnail":
				var insertThumbnails = $("<img />").attr({ 
					src: $(assetsType).text() ,
					title: $(assetsType).attr("title"),
					alt: $(assetsType).attr("title"),
					width: $(assetsType).attr("width"),
					height: $(assetsType).attr("height")
				}).addClass("slideContainer_"+a + " toggleList");
				/*display images*/
				if (a == 0) { $(".workThumb").fadeIn("slow").append(insertThumbnails)} else { $(".workThumb").append(insertThumbnails.hide()); }
				break;
			case "mov":
				$(".workThumb").append("<div class='toggleList'><div id='slideContainer_"+a+"'><a href='http://www.adobe.com/products/flashplayer/' target='_blank'><img src='images/flashPlaceholder.jpg' alt='flash placeholder' /></a></div></div>");
				$(".toggleList").width($(assetsType).attr("width")+"px");
				movname = $(assetsType).text();
				movieplayer = $(assetsType).attr("movieplayer");
				slideAttr = "slideContainer_"+a;
				swfWidth = $(assetsType).attr("width");
				swfHeight = $(assetsType).attr("height");
				//call swfobject function createFlashContainer()
				createFlashMovContainer(movname,movieplayer,slideAttr,swfWidth,swfHeight);
				break;
			case "quicktime":
				$(".workThumb").append("<div class='toggleList toggleListDiv'><div id='slideContainer_"+a+"'><a href='http://www.apple.com/quicktime/download/' target='_blank'><img src='"+$(assetsType).attr("placeholder")+"' alt='Click here to download Apple QuickTime to view mov.' /></a></div></div>");
				$(".toggleListDiv").width($(assetsType).attr("width")+"px");
				movname = $(assetsType).text();
				placeholder = $(assetsType).attr("placeholder");
				slideAttr = "slideContainer_"+a;
				qtWidth = $(assetsType).attr("width");
				qtHeight = $(assetsType).attr("height");
				//call embed function createEmbedMovContainer()
				createEmbedMovContainer(movname,placeholder,slideAttr,qtWidth,qtHeight);
				if (a == 0) { $(".toggleList:eq(0)").fadeIn("slow") } else { $(".toggleList:eq("+a+")").hide(); }
				break;
			case "swf":
				$(".workThumb").append("<div class='toggleList'><div id='slideContainer_"+a+"'><a href='http://www.adobe.com/products/flashplayer/' target='_blank'><img src='images/flashPlaceholder.jpg' alt='flash placeholder' /></a></div></div>");
				$(".toggleList").width($(assetsType).attr("width")+"px");
				swfobject.embedSWF($(assetsType).text(), "slideContainer_"+a, $(assetsType).attr("width"), $(assetsType).attr("height"), "9.0.0", "images/expressInstall.swf");
				if (a == 0) { $(".toggleList:eq(0)").fadeIn("slow") } else { $(".toggleList:eq("+a+")").hide(); }
				break;
			default:
				alert("Error invalid type: "+type);
		}

	});
	$(".slideList li:last").addClass("nobg");
	/*add basics*/
	$(".projectDescription").append( $(currentclient).find("description").text());
	if ($(currentclient).find("links")){
		links = "<p class='links'>"+$(currentclient).find("links").text()+"</p>";
		$(links).insertAfter(".projectDescription");
	}
	//console.log("attr", $(currentclient).attr("client"),$(currentclient).attr());
	if($(currentclient).attr("client")){
		$(".projectDetailContainer").append("<p>client: "+ $(currentclient).attr("client")+"</p>");}
	else if ($(currentclient).attr("instructor")){
		$(".projectDetailContainer").append("<p>instructor: "+ $(currentclient).attr("instructor")+"</p>");
	}
	else if ($(currentclient).attr("nonclient")){
		$(".projectDetailContainer").append("<p>"+ $(currentclient).attr("nonclient")+"</p>");
	}
	//$(".client").append("client: "+ $(currentclient).attr("client"));
}

}); //main document
