/** * Copyright (c) 2009-2010, As Is Software, dba Media Lab Inc. * http://www.medialab.com */ /*global $ml, $, fc_CookieFix, Image, XMLSerializer, console */ /* * * Initialization process: * Init all docent stuff * As necessry, based on xml: * New picturebox * New exhibit sheet * New view sheet * New panel sheet * New bigbox * * Each picbox and sheet will initially select the current * view/exb as passed in. That way only the jscript for that * component needs to know about its possibly asynchronous * initialization requirements. * */ var com; if(!com) { com = {}; } else if(typeof(com) != "object") { throw new Error("can't initialize...com is not an object."); } if(!com.medialab) { com.medialab = {}; } else if(typeof(com.medialab) != "object") { throw new Error("can't initialize...com.medialab is not an object."); } if(!com.medialab.sg) { com.medialab.sg = {}; } if (!com.medialab.sg.gallery1) { com.medialab.sg.gallery1 = {};} com.medialab.sg.gallery1.docent = function (xmlPathIn, myNameIn) { if (typeof($ml)=='undefined') { $ml = $; } var that=this; var myName = myNameIn ? myNameIn : "docent"; var curExhibit = 0; var prevExhibit = -1; var exhibitChange = true; var curView = 0; var prevView = -1; var exhibitCount = 0; var thumbsheetCount = 0; var galleryXMLpath = xmlPathIn; var thumbsheet = null; var viewsheet = null; var viewsheetOptions = null; var exhibitsheetOptions = null; var pictureboxOptions = null; var bigboxOptions = null; var panelOptions = null; var bigbox = null; var picturebox = null; var panel = null; var xml = null; var thumbsheetDirty = false; var viewsheetDirty = false; var pictureboxDirty = false; var urlViewParamName = "view"; var urlExhibitParamName = "exb"; var preload = false; // preload or not var preloads = []; // array of images to preload var preloadMax = 50; var metaHrefs = null; var metaChunks = null; var metaDelimOpen = "[["; var metaDelimClose = "]]"; var formMetaData = null; var useConsole = false; var debug = false; this.sgalert = function(msg) { if (debug) { alert(msg); } }; function sgalert(msg) { that.sgalert(msg); } this.getCurrentExhibit = function () { return curExhibit; }; this.getCurrentView = function () { return curView; }; this.getExhibitCount = function () { return exhibitCount; }; this.getName = function () { return myName; }; this.getxml = function() { return xml; }; this.selectThumb = function (thumbnum, mode, updateThumbsheet) { //alert('hi'); //this.sgalert("Selected thumb (v1)" + thumbnum); this.sgconsole("selectthumb. mode: " + mode + " thumbnum: " + thumbnum + ' curexhibit ' + curExhibit); if (mode == "exhibit") { if (thumbnum != curExhibit) { exhibitChange = true; prevExhibit = curExhibit; curExhibit = thumbnum; curView=0; prevView=0; thumbsheetDirty = updateThumbsheet; viewsheetDirty = true; pictureboxDirty=true; this.sgconsole("About to sync"); this.sync(); } else { if(panel && panel.goToPanel) { panel.goToPanel(thumbnum); } } } else { if (thumbnum != curView) { exhibitChange = false; viewsheetDirty=updateThumbsheet; pictureboxDirty=true; prevView = curView; curView = thumbnum; this.sync(); } } // if(this.getThumbSheetOption(mode,'selectionaction')=='bigbox') { // this.openBigbox(); // } return false; }; // this.sgalert = function(msg) { alert("sgalert public"); alert(msg); } function checkEnvironment () { sgalert("DOCENT: checkEnvironment()"); // check for any required libs, like jquery if (!$ml) { return false; } return true; } this.init = function() { /*var urlExbNum = this.getUrlParam(urlExhibitParamName); // this was the old way of allowing ehxhibit number in the url var urlViewNum = this.getUrlParam(urlViewParamName); // it didn't allow for targeting a specific gallery if(urlExbNum) { // see getExhibitFromURL() below for the new way. curExhibit = urlExbNum; if(urlViewNum) { curView = urlViewNum; } }*/ if(!checkEnvironment()) { alert("SiteGrinder gallery missing components!"); return; } if (!loadGalleryXML()) { alert("SiteGrinder gallery can't find XML file!"); return; } }; function sync() { that.sgconsole('wrong sync called'); that.sync();} function getExhibitFromURL() { var queryExhibit = getUrlParameter(myName+'_exhibit'); // this will be, for example, "weddings_d_exhibit=8", the value being zero-based. if(queryExhibit !== "" && !isNaN(queryExhibit) && Number(queryExhibit) < exhibitCount) { return Number(queryExhibit); } else { return 0; } } function getUrlParameter(name){ name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); var regexS = "[\\?&]" + name + "=([^&#]*)"; var regex = new RegExp(regexS); var results = regex.exec(window.location.href); if (results === null) { return ""; } else { return results[1]; } } this.getThumbsheetOption = function (sheetType,optionIn) { // these are dopplegangered just to avoid constant camelcaps typo bugs return this.getThumbSheetOption(sheetType,optionIn); }; this.getThumbSheetOption = function (sheetType,optionIn) { var options = null; if(sheetType == "view") { options = viewsheetOptions; } else { options = exhibitsheetOptions; } return this.getAnOption(options,optionIn); }; this.getPictureboxOption = function (optionIn) { return this.getPictureBoxOption(optionIn); }; this.getPictureBoxOption = function (optionIn) { return this.getAnOption(pictureboxOptions,optionIn); }; this.getBigboxOption = function (optionIn) { return this.getBigBoxOption(optionIn); }; this.getPanelOption = function (modeIn, optionIn) { return this.getAnOption(panelOptions,optionIn); }; this.getBigboxOption = function (optionIn) { return this.getBigBoxOption(optionIn); }; this.getBigBoxOption = function (optionIn) { return this.getAnOption(bigboxOptions,optionIn); }; this.getAnOption = function (options,option) { var type = $ml('option[varname='+option+']', options).attr("type"); var valMix = $ml('option[varname='+option+']', options).attr("value"); if (typeof valMix == 'undefined') { this.sgconsole('Docent asked for a non existent option: ' + option); return ""; } var valLower = valMix.toLowerCase(); if (type == 'undefined') { this.sgconsole('option type not found in options for option ' + option); if (isNaN(valLower)) { if (valLower == "true") { return true; } else if (valLower == "false") { return false; } else { return valMix; } } else { if (valLower.substring(0, 2) == '0x') { return valLower; } // color else { return parseFloat(valMix); } } } else { var typeLower =type.toLowerCase(); switch (typeLower) { case 'boolean' : return (valLower == 'true'); case 'number' : return parseFloat(valLower); case 'color': return valMix; default: return valMix; } } }; this.getCSSFormattedColor = function(col) { if(col.substring(0,1) == '#') { return col; } else if (col.substring(0,2) == '0x') { return '#' + col.substring(2,col.length); } else { return '#' + col; } }; this.getFlashMovie = function(movieName) { // IE and Netscape refer to the movie object differently. // This function returns the appropriate syntax depending on the browser. this.sgconsole("finding movie " + movieName); if (navigator.appName.indexOf ("Microsoft") !=-1) { return window[movieName]; } else { return document[movieName]; } }; // thanks to FF windows when the movie isn't visible on screen this routine now has to check for initWithXML... // this means all docent-checked flash components must export an initWithXML() with that exact name this.flashMovieIsLoaded = function(theMovie) { // First make sure the movie's defined. if (typeof(theMovie) != "undefined" && theMovie!==null) { this.sgconsole("movie " + theMovie); // If it is, check how much of it is loaded. // incredibly IE returns 'unknown' as the type of PercentLoaded() but 'function' for initWithXML() if (typeof theMovie.initWithXML != 'undefined') { try { if (typeof theMovie.PercentLoaded != 'undefined' && typeof theMovie.PercentLoaded != 'unknown') { // just checking for PercentLoaded causes IE to error with Flash 10.1 return theMovie.PercentLoaded() == 100; // check percentloaded just in case if the func is available } } catch(err) { } return true; // assume loaded since initWithXML is there } else { return false; } } else { this.sgconsole("movie undefined"); // If the movie isn't defined, it's not loaded. return false; } }; this.processXML = function (fullGalleryXMLIn){ // ** Called when ajax has successfully loaded our XML ** that.sgconsole("about to process xml"); xml=$ml(fullGalleryXMLIn); that.sgconsole("Docent: processXML (xml loaded)"); that.postXMLInit(); }; // // setUpPreloads() // function preloadNext(){ if(preloads.length > 0) { var curSrc = $ml.trim($ml(preloads.shift()).text()); that.sgconsole('PRELOADING next picturebox image '+ curSrc +'('+ preloads.length +' left)'); var img = new Image(); img.onerror = preloadErr; img.onload = preloadNext; img.src = curSrc; } } function setUpPreloads(){ if (preload) { var qString = ""; var result = null; if (picturebox) { // current picturebox qString = 'exhibits exhibit:eq(' + curExhibit + ') view:eq('+curView+') picturebox url'; result = $ml(qString, xml).get(); if (result) { preloads = preloads.concat(result); } } if (thumbsheet) { // current thumbnail qString = 'exhibits exhibit:eq(' + curExhibit + ') view thumbnail url'; result = $ml(qString, xml).get(); if (result) { preloads = preloads.concat(result); } } if (viewsheet) { // add current view thumbnails first qString = 'exhibits exhibit:eq(' + curExhibit + ') view viewthumb url'; result = $ml(qString, xml).get(); if (result) { preloads = preloads.concat(result); } } if(thumbsheet){ // now load all the exhibit thumbs for (var curEx = 0; curEx < exhibitCount; curEx++) { qString = 'exhibits exhibit:eq('+ curEx +') view:eq(0) thumbnail url'; result = $ml(qString, xml).get(); if (result) { preloads = preloads.concat(result); } } } for (curEx = 0; curEx < exhibitCount; curEx++) { if (curEx != curExhibit) { // now add the main pictureboxes and viewthumbs in order from zero if (picturebox) { qString = 'exhibits exhibit:eq('+curEx+') view:eq(0) picturebox url'; result = $ml(qString, xml).get(); if (result) { preloads = preloads.concat(result); } } if(viewsheet) { qString = 'exhibits exhibit:eq('+curEx+') view viewthumb url'; result = $ml(qString, xml).get(); if (result) { preloads = preloads.concat(result); } } } } that.sgconsole('preload list has ' + preloads.length + 'items'); if(preloads.length > preloadMax) { preloads.length = preloadMax; } preloadNext(); } } function preloadErr() { this.sgconsole('PRELOADING err ' + this.src); preloadNext(); } this.postXMLInit = function() { // ** Called to init whatever is necessary after XML loaded // the options are stored as an optimization so the whole tree isn't parsed each time an option is looked for this.sgconsole("Docent: postXMLInit"); exhibitCount = $ml('exhibit',xml).length; curExhibit = getExhibitFromURL(); this.sgconsole("About to update metadata"); this.updateMetadata(); this.sgconsole("binding preloader"); //$ml(window).bind('load', pictureboxPreload); var thumbsheetCount = $ml('thumbsheet',xml).length; $ml('options thumbsheet',xml).each(function(n){ //console.log("docent making thumbsheet"); var sheetConstructor = $ml('option[varname="constructor"]',this).attr("value"); if(!sheetConstructor) { sgalert('No thumbsheet contructor provided in xml!'); } if (!com.medialab.sg.gallery1[sheetConstructor]) {sgalert('Thumbsheet constructor "' + sheetConstructor + '" not available!');} var sheetmode = $ml('option[varname="mode"]',this).attr("value"); that.sgconsole("Found a "+ sheetmode +" sheet with constructor " + sheetConstructor); if(sheetmode == 'exhibit') { exhibitsheetOptions = this; thumbsheet = new com.medialab.sg.gallery1[sheetConstructor](xml,sheetmode,that); //thumbsheet = new window[sheetConstructor](xml,sheetmode,that); } else { viewsheetOptions = this; viewsheet = new com.medialab.sg.gallery1[sheetConstructor](xml,sheetmode,that); // viewsheet = new window[sheetConstructor](xml,sheetmode,that); } }); var pictureboxCount = $ml('options picturebox',xml).length; if (pictureboxCount > 0) { var pboxConstructor = $ml('options picturebox option[varname="constructor"]',xml).attr("value"); that.sgconsole("Found a picturebox with constructor " + pboxConstructor); if(!pboxConstructor) { sgalert('No picturebox contructor provided in xml!'); } if (!window[pboxConstructor]) {sgalert('Picturebox constructor "' + pboxConstructor + '" not available!');} pictureboxOptions = $ml('options picturebox',xml); picturebox = new com.medialab.sg.gallery1[pboxConstructor](xml, this); } var bigboxCount = $ml('options bigbox',xml).length; if (bigboxCount > 0) { var bboxConstructor = $ml('options bigbox option[varname="constructor"]',xml).attr("value"); that.sgconsole("Found a bigbox with constructor " + bboxConstructor); if(!bboxConstructor) { sgalert('No bigbox contructor provided in xml!'); } if (!window[pboxConstructor]) {sgalert('Bigbox constructor "' + bboxConstructor + '" not available!');} bigboxOptions = $ml('options bigbox',xml); this.sgconsole("Found a BIGBOX: " + bboxConstructor); bigbox = new com.medialab.sg.gallery1[bboxConstructor](xml, this); } var panelCount = $ml('options panel',xml).length; if (panelCount > 0) { var panelMode = $ml('options panel option[varname="mode"]',this).attr("value"); if(typeof(panelMode) == 'undefined') { panelMode = 'exhibit'; } var panelConstructor = $ml('options panel option[varname="constructor"]',xml).attr("value"); that.sgconsole("Found a panel with constructor " + panelConstructor); if(!panelConstructor) { sgalert('No panel contructor provided in xml!'); } if (!window[panelConstructor]) {sgalert('panel constructor "' + panelConstructor + '" not available!');} panelOptions = $ml('options panel',xml); this.sgconsole("Found a panel!!"); panel = new com.medialab.sg.gallery1[panelConstructor](xml, panelMode, this); } setUpPreloads(); //this.sync(); }; this.openBigbox = function(mode) { if(!mode) { if(!viewsheet || curView === 0) {mode='exhibit'; } else { mode='view';} } this.sgconsole('js docent opening '+ mode +' mode bigbox with exhibit ' + curExhibit + ', view ' + curView); bigbox.openByView(mode, curExhibit, curView); }; this.getLargestImageUrl = function() { var foundUrl = $ml('exhibits exhibit:eq(' + curExhibit + ') views view:eq(' + curView + ') bigbox url', xml).text(); if (foundUrl) { return foundUrl; } else { foundUrl = $ml('exhibits exhibit:eq(' + curExhibit + ') views view:eq(' + curView + ') picturebox url', xml).text(); // not found so check picturebox if (foundUrl) { return foundUrl; } else { foundUrl = $ml('exhibits exhibit:eq(' + curExhibit + ') views view:eq(' + curView + ') thumbnail url', xml).text(); // not found so check thumbnail if (foundUrl) { return foundUrl; } } } return ""; }; this.printCurrentExhibit = function() { var imgUrl = this.getLargestImageUrl(); if (typeof imgUrl == 'string' && imgUrl.length > 0) { var printPageContents = "\n" + "\n" + "Temporary Printing Window\n" + "