/*************************************************************************************
* Viewport - view port javascript for integrating MM Online Systems into your website
* This is a pared down version of our usual jQuery-rich viewport script, to avoid collisions
* with third-party libraries such as scriptaculous and lightbox.
*
* see:  http://www.magicmemories.com/vp/readme
*
* Copyright Magic Memories Limited 2010
* Kate Butler, 29 November 2010
*
***************************************************************************************/

//Default server URL to retrieve MM code from
var protocol = document.location.protocol;	//Includes the : but not the //
var serverURLDefault = protocol + "//my.magicmemories.com";

//ID for the <script> tag which loads the MM viewport
var scriptDOMID = "mm_viewport";

// compress other included files to save download time
var compress = false;

// this keeps us from rechecking and/or putting up the interface when it shouldn't be
var winLoaded = false;

//Find out which server which we loaded the MM viewport code from by looking for a <script> tag
//with ID equal to the value of 'scriptDOMID' defined above and parsing its src attrbute. If no matching 
//tag is found the value of serverURLDefault will be used.
getServer();

//Flag to set whether we show the darkened screen and "Loading, please wait" message while loading the viewport
//This causes clashes with scriptaculous and lightbox and possible others so we won't do this in this version!
var isLoading = false;

// load viewport tools script
if (compress === true) {
	loadjscssfile(server + "/js/mm_vptools64.js", "js");
} else {
	loadjscssfile(server + "/js/mm_vptools.js", "js");
}

// load viewport object model
if (compress === true) {
	loadjscssfile(server + "/js/mm_vpwc64.js", "js");
} else {
	loadjscssfile(server + "/js/mm_vpwc.js", "js");
}

//Find out the name of the server which we loaded the MM viewport code from by looking for a <script> tag
//with ID equal to the value of 'scriptDOMID' defined above and parsing its src attrbute. If no matching 
//tag is found the value of serverURLDefault will be used.
function getServer() {
	var serverURL;
	try {
		serverURL = document.getElementById(scriptDOMID).src.split("/");
	} catch(err) {
		serverURL = serverURLDefault.split("/");
	}
	if (serverURL.length > 0) {
		server = serverURL[0] + "/" + serverURL[1] + "/" + serverURL[2] + "/vp";
	}
}

//Include a source code file by including it in this document's <head> tag
function loadjscssfile(filename, filetype){
	if (filetype=="js"){ //if filename is a external JavaScript file
		var fileref=document.createElement('script');
		fileref.setAttribute("type","text/javascript");
  		fileref.setAttribute("src", filename);
 	}
 	else if (filetype=="css"){ //if filename is an external CSS file
  		var fileref=document.createElement("link");
  		fileref.setAttribute("rel", "stylesheet");
  		fileref.setAttribute("type", "text/css");
  		fileref.setAttribute("href", filename);
 	}
	//Append the <script> or <link> element created above onto the <head>
 	if (typeof fileref!="undefined") document.getElementsByTagName("head")[0].appendChild(fileref);
}

// run on load to see of there is a viewport to show
function checkURI() {
	checkURIParams();
}

// add to the onload event when a viewport request is found
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
 			if (oldonload) {
 				oldonload();
			}
			func();
 		};
	}
}

// Add an onload listener for those that support it.  This should prevent us from overwriting other onload events
// and more likely, prevent having ours overwritten
if ( typeof window.addEventListener != "undefined" ) {
	window.addEventListener( "load", checkURI, false );
} else if ( typeof window.attachEvent != "undefined" ) {
	window.attachEvent( "onload", checkURI );
} else {
	addLoadEvent( checkURI );
}
