// SNI PUP, v 1.3.8 - 26

var scrippsEnvironment = '';

(function(){
	// Map over sniPUP in case of overwrite
	if ( window.sniPUP )
		var _sniPUP2 = window.sniPUP;
	
	var sniPUP = window.sniPUP = function( n, options ) {
		// The sniPUP object is actually just the init constructor 'enhanced'
		return new sniPUP.prototype.init( n, options );
	};
	window.$pup = sniPUP;
	
	sniPUP.instances = [];

	sniPUP.fn = sniPUP.prototype = {
		init: function( n, options ) {
			if ( typeof n == 'object' ) options = n;
			// the following commented is an example of how
			// default properties can be pulled into the PUP
			// in the future in case these props are eventually
			// needed BEFORE the .write method
			var defaults = {
				channelID				: 0,
				configXML				: '',
				siteID					: '',
				server					: '',
				lcDivID					: null,
				appDivID				: null,
				forceList				: null,
				autoPlay				: null,
				searchWithin			: true,
				showList				: null,
				videoID					: null,
				hasSearch				: null,
				onForceChannel			: null,
				onPlaylistComplete		: null
			}
			
			this.xmlURLS = {
				food	: 'http://www.foodnetwork.com/food/channel/xml/0,3088,6122,00.xml',
				hgtv	: 'http://www.hgtv.com/hgtv/channel/xml/0,3088,6125,00.xml',
				diy		: 'http://www.diynetwork.com/diy/channel/xml/0,3088,6124,00.xml',
				fine	: 'http://www.fineliving.com/fine/channel/xml/0,3088,6123,00.xml',
				hpro	: 'http://www.hgtvpro.com/hpro/channel/xml/0,3088,6127,00.xml',
				gac		: 'http://www.gactv.com/gac/channel/xml/0,3088,6126,00.xml'
			}
			this.options = sniPUP.extend(defaults, options);
			this.id = sniPUP.instances.length;
			sniPUP.instances[this.id] = this;
			this.isReady = false;
			this.firstWrite = true;
			this.searchWithin = true;
			this.playlistComplete = false;
			this.externalQuery = '';
			
			this.appDivName = n;
			
			this.swfVersion = '9.0.18';
			this.swfBGColor = '#FFFFFF';
			this.swf = "http://common.scrippsnetworks.com/common/pup/PUP";
			//this.swf = "http://common.scrippsnetworks.com/common/pup/dev/PUP";
			//this.swf = "http://common.scrippsnetworks.com/common/pup/staging/PUP";
			this.appSWFName = "sniPUP" + this.id;
			
			// implemented for usage with options param list and without having to instantiate the .write() method
			// EXAMPLE USAGE FOR A FUTURE PUP INSTANTIATION
			//var pup = new sniPUP({
			//	appDivID	: 'videoplayer',
			//	lcDivName	: 'bigbox-ad',
			//	channelID	: 6307,
			//	siteID		: 'HPRO',
			//	showList	: true,
			//	autoPlay	: true
			//});
			if ( options ) {
				// if using optins (new initialization) provide backwards compatibility
				this.appDivName = this.options.appDivID != null ? this.options.appDivID : this.appDivName;
				this.lcDivName = this.options.lcDivID != null ? this.options.lcDivID : this.lcDivName;
				this.channelID = this.options.channelID != 0 ? this.options.channelID : this.channelID;
				this.configXML = this.options.configXML != '' ? this.options.configXML : this.configXML;
				this.siteID = this.options.siteID != '' ? this.options.siteID : this.siteID;
				this.server = this.options.server != '' ? this.options.server : this.server;
				this.forceList = this.options.forceList != null ? this.options.forceList : this.forceList;
				this.autoPlay = this.options.autoPlay != null ? this.options.autoPlay : this.autoPlay;
				this.hasSearch = this.options.hasSearch != null ? this.options.hasSearch : this.hasSearch;
				this.searchWithin = this.options.searchWithin != true ? this.options.searchWithin : this.searchWithin;
				this.showList = this.options.showList != null ? this.options.showList : this.showList;
				this.videoID = this.options.videoID != null ? this.options.videoID : this.videoID;
				this.onForceChannel = this.options.onForceChannel != null ? this.options.onForceChannel : this.onForceChannel;
				this.onPlaylistComplete = this.options.onPlaylistComplete != null ? this.options.onPlaylistComplete : this.onPlaylistComplete;
				this.write();
			}
			return this;
		},
		ready: function(fn) {
			// Attach the listeners
			bindReady();
			if ( sniPUP.isReady ) {
				// Execute the function immediately
				fn.call( document, sniPUP );
			} else {
				// Otherwise, remember the function for later
				sniPUP.readyFunc = fn;
			}
			return this;
		},
		getApp: function() {
			return this.appSWFName;
		},
		getBestOfURL: function() {
			return eval('this.xmlURLS.' + this.siteID);
		},
		getSearchURL: function(query, start, end) {
			var type = this.siteID.toUpperCase() + ' UP Video Search Type';
			if ( this.server == "staging" ) {
				return "http://204.78.40.156/ScrippsNetworks/xmlSearch/xmlSearch.do?query=" + escape(query) + "&start=" + start + "&end=" + end + "&search_type=" + escape(type);
			}
			return "http://search.gactv.com/ScrippsNetworks/xmlSearch/xmlSearch.do?query=" + escape(query) + "&start=" + start + "&end=" + end + "&search_type=" + escape(type);
		},
		isSearchBlocked: function() {
			if ( this.siteID == "gac" || this.siteID == "food" ) {
				return true;
			} else {
				return false;
			}
		},
		// DEPRICATED FUNCTION
		//getLCDivElement: function() {
		//	if ( document.getElementById(this.lcDivName) ) {
		//		this.lcBannerElement = document.getElementById(this.lcDivName);
		//		clearInterval(this.lcDivInterval);
		//	}
		//},
		noFlash: function() {
			// prepend noFlash message
			var initialContent = this.appDivElement.innerHTML;	
			var noFlashHTML = "<span style='background-color:#FFFFCC; display:block; padding:10px; text-align:center;'><b>Can't see these videos?</b><br/><a href='http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash' target='_blank'>Download Adobe Flash Player now for free.</a></span>";		
			this.appDivElement.innerHTML = noFlashHTML + "<br>" + initialContent;
		},
		write: function() {
			var pup = this;
			/* IF the PUP is already loaded then just run doWrite */
			if ( !this.firstWrite ) {
				pup.doWrite(pup);
			/* ELSE wait for the DOM to be ready first */
			} else {
//				this.ready(function() {
					pup.doWrite(pup);
//				});
			}
		},
		doWrite: function(pup) {
			pup.appDivElement = document.getElementById(pup.appDivName);
			// set width and height per parameters passed from script block on html page
			pup.appDivElement.style.visibility = "hidden";
			// DEPRICATED : pup.lcBannerElement should ALWAYS be equal to  document.getElementById(pup.lcDivName)
			//if ( !document.getElementById(pup.lcDivName) ) {
			//	pup.lcDivInterval = setInterval('sniPUP.instances[' + pup.id + '].getLCDivElement()', 100);
			//} else {
				pup.lcBannerElement = document.getElementById(pup.lcDivName);
			//}
			pup.noFlash();
			// check for swfobject.js
			if ( typeof SWFObject == 'undefined') {
				alert("This application requires the latest SWFObject");
			} else {
				// SWFObject found - now call function to write to page
				pup.writeSWF(pup);
			}
		},
		fixProps: function() {
			// provide backwards compatibility for options
			// ensure certain properties are passed into swfObject correctly
			if ( this.channelID ) this.channelID = this.channelID.toString();
			if ( this.configXML ) this.configXML = this.configXML.toString();
			if ( this.id ) this.id = this.id.toString();
			if ( this.forceList ) this.forceList = this.forceList.toString();
			if ( this.siteID ) this.siteID = this.siteID.toString().toLowerCase();
			if ( this.server ) this.server = this.server.toString().toLowerCase();
			if ( this.autoPlay ) this.autoPlay = this.autoPlay.toString();
			if ( this.hasSearch ) this.hasSearch = this.hasSearch.toString();
			if ( this.searchWithin ) this.searchWithin = this.searchWithin.toString();
			if ( this.showList ) this.showList = this.showList.toString();
			if ( this.videoID ) this.videoID = this.videoID.toString();
		},
		search: function(str) {
			this.externalQuery = str;
			this.write();
		},
		writeSWF: function(pup) {
			pup.playlistComplete = false;
			if ( typeof getDartEnterpriseUrl != 'undefined' ) { pup.DartEnterpriseUrl = getDartEnterpriseUrl("PREROLL", "1"); }
			if ( pup.firstWrite ) {
				pup.query = getQueryParamValue('q');
				pup.videoID = getQueryParamValue('videoid');
			}
			pup.fixProps();
			var soPUP = new SWFObject(pup.swf + '.swf', pup.appSWFName, '100%', '100%', pup.swfVersion, pup.swfBGColor);
			soPUP.addParam('allowScriptAccess', 'always');
			soPUP.addParam('wmode', 'opaque');
			if ( pup.externalQuery != '' ) {
				pup.query = pup.externalQuery;
			}
			
			if ( pup.query == '' ) {
				soPUP.addVariable("c", pup.channelID);
			} else {
				if ( pup.searchWithin.toString() == 'true' ) {
					if ( typeof pup.onForceChannel != 'undefined' ) {
						this.onForceChannel();
					}
					soPUP.addVariable("c", escape(pup.getSearchURL(pup.query, 1, 50)));
				} else {
					soPUP.addVariable("c", pup.channelID);
				}
			}
			soPUP.addVariable("configXML", pup.configXML);
			soPUP.addVariable("siteID", pup.siteID);
			soPUP.addVariable("server", pup.server);
			soPUP.addVariable("instanceID", pup.id);
			soPUP.addVariable("fl", pup.forceList);
			
			if ( getQueryParamValue("ap") == "true" ) {
				pup.autoPlay = "true";
			}
			soPUP.addVariable("s", pup.hasSearch);
			soPUP.addVariable("searchWithin", pup.searchWithin);
			if ( pup.videoID == "" ) {
				if ( pup.autoPlay != "true" ) {
					soPUP.addVariable("sl", pup.showList);
				} else {
					soPUP.addVariable("ap", pup.autoPlay);
					soPUP.addVariable("sl", "false");
				}
			} else {
				soPUP.addVariable("sl", "false");
				soPUP.addVariable("videoID", pup.videoID);	
			}
			soPUP.addParam("allowFullScreen", "true");
			soPUP.write(pup.appDivName);
			pup.appDivElement.style.visibility = "visible";
			pup.firstWrite = false;
			pup.externalQuery = pup.videoID = pup.query = '';
		}
	};
	var readyBound = false;
	var userAgent = navigator.userAgent.toLowerCase();
	sniPUP.browser = {
		version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
		safari: /webkit/.test( userAgent ),
		opera: /opera/.test( userAgent ),
		msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
		mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
	};
	sniPUP.readyFunc = null;
	sniPUP.prototype.init.prototype = sniPUP.prototype;
	sniPUP.ready = function() {
		if ( !sniPUP.isReady ) {
			sniPUP.isReady = true;
			if ( sniPUP.readyFunc ) {
				sniPUP.readyFunc.apply( document );
				sniPUP.readyFunc = null;
			}
		}
	};
	sniPUP.extend = function() {
		// copy reference to target object
		var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options;
		// Handle a deep copy situation
		if ( target.constructor == Boolean ) {
			deep = target;
			target = arguments[1] || {};
			// skip the boolean and the target
			i = 2;
		}
		// Handle case when target is a string or something (possible in deep copy)
		if ( typeof target != "object" && typeof target != "function" ) { target = {}; }
		// extend sniPUP itself if only one argument is passed
		if ( length == 1 ) {
			target = this;
			i = 0;
		}
		for ( ; i < length; i++ ) {
			// Only deal with non-null/undefined values
			if ( (options = arguments[ i ]) != null ) {
				// Extend the base object
				for ( var name in options ) {
					// Prevent never-ending loop
					if ( target === options[ name ] ) { continue; }
					// Recurse if we're merging object values
					if ( deep && options[ name ] && typeof options[ name ] == "object" && target[ name ] && !options[ name ].nodeType ) {
						target[ name ] = sniPUP.extend( target[ name ], options[ name ] );
					} else if ( options[ name ] != undefined ) {
						// Don't bring in undefined values
						target[ name ] = options[ name ];
					}
				}
			}
		}
		// Return the modified object
		return target;
	};
	function bindReady() {
		if ( readyBound ) return;
		readyBound = true;
		// Mozilla, Opera (see further below for it) and webkit nightlies currently support this event
		if ( document.addEventListener && !sniPUP.browser.opera) document.addEventListener( "DOMContentLoaded", sniPUP.ready, false );
		// If IE is used and is not in a frame
		// Continually check to see if the document is ready
		if ( sniPUP.browser.msie && window == top ) (function() {
			if (sniPUP.isReady) return;
			try { document.documentElement.doScroll("left"); } catch( error ) { setTimeout( arguments.callee, 0 ); return; }
			sniPUP.ready();
		})();
		if ( sniPUP.browser.opera )
			document.addEventListener( "DOMContentLoaded", function () {
				if (sniPUP.isReady) return;
				for (var i = 0; i < document.styleSheets.length; i++)
					if (document.styleSheets[i].disabled) {
						setTimeout( arguments.callee, 0 );
						return;
					}
				sniPUP.ready();
			}, false);
		if ( sniPUP.browser.safari ) {
			var numStyles;
			(function(){
				if (sniPUP.isReady) return;
				if ( document.readyState != "loaded" && document.readyState != "complete" ) { setTimeout( arguments.callee, 0 ); return; }
				if ( numStyles === undefined ) numStyles = document.styleSheets.length;
				if ( document.styleSheets.length != numStyles ) { setTimeout( arguments.callee, 0 ); return; }
				sniPUP.ready();
			})();
		}
		if ( document.all ) {
			if ( window.jQuery ) {
				jQuery.event.add( window, "load", sniPUP.ready );
			} else {
				window.attachEvent('onload', sniPUP.ready);
			}
		} else {
			window.addEventListener('load', sniPUP.ready, false);
		}

		//sniPUP.event.add( window, "load", sniPUP.ready );
	};
})();

/* FUNCTIONS FOR FLASH TO TALK TO */
function sniPUP_changeAd(i, sBannerURL) {
	var sBannerHTML = '<HTML><IFRAME id="pupBigBoxAd" src="' + sBannerURL + '" frameborder="0" scrolling="no" width="300" height="250" marginwidth="0" marginheight="0"/></HTML>';
	sniPUP.instances[i].lcBannerElement.innerHTML = sBannerHTML;
};

function sniPUP_isDartReady(siteID){
	var b;
	switch ( siteID ) {
		case "food": { b = true; break; }
		case "hgtv": { b = true; break; }
		case "diy": { b = true; break; }
		case "fine": { b = true; break; }
		case "hpro": { b = true; break; }
		case "gac": { b = true; break; }
		default: { b = false; break; }
	}
	return b;
};

function sniPUP_realShowNewChannel(i, url) {
	sniPUP.instances[i].write();
};

function sniPUP_showNewChannel(i, url) {
	sniPUP.instances[i].autoPlay = false;
	sniPUP.instances[i].channelID = escape(url);
	setTimeout("sniPUP_realShowNewChannel(" + i + ")", 500);
	if (typeof sniPUP.instances[i].onForceChannel != "undefined" ) {
		sniPUP.instances[i].onForceChannel();
	}
};

function sniPUP_onPlaylistComplete(i) {
	if (typeof sniPUP.instances[i].onPlaylistComplete != "undefined" ) {
		sniPUP.instances[i].onPlaylistComplete = sniPUP.instances[i].options.onPlaylistComplete != null ? sniPUP.instances[i].options.onPlaylistComplete : sniPUP.instances[i].onPlaylistComplete;
		sniPUP.instances[i].onPlaylistComplete();
		sniPUP.instances[i].playlistComplete = true;
	}
};

function sniPUP_getDescriptor(adType, pos){
	if(window.MavenAd) return MavenAd(adType, "", pos);
	else return getDartEnterpriseUrl(adType, pos);
}//end sniPUP_getDescriptor

function getDartEnterpriseUrlProxy(i) {
	return sniPUP.instances[i].DartEnterpriseUrl;
};

function getDartEnterpriseUrlProxy(i) {
	return sniPUP.instances[i].DartEnterpriseUrl;
};

function sniPUP_videosNotFound(i) {
	//For time constaints we will only return the "Best of"
	//results instead of trying to get the page keywords
	sniPUP_showNewChannel(i, sniPUP.instances[i].getBestOfURL());
	/*
	if ( !sniPUP.instances[i].isSearchBlocked() ) {
		if ( typeof document.globalPageKeywords != 'undefined' && document.globalPageKeywords.trim() != '' ) {
			sniPUP_showNewChannel(i, sniPUP.instances[i].getSearchURL(document.globalPageKeywords, 1, 50));
		} else {
			sniPUP_showNewChannel(i, sniPUP.instances[i].getBestOfURL());
		}
	} else {
		sniPUP_showNewChannel(i, sniPUP.instances[i].getBestOfURL());
	}
	*/
};

function sniPUP_searchPUP(str, i) {
	var instanceNum = 0;
	if ( i ) { instanceNum = i; }
	sniPUP.instances[instanceNum].search(str);
};

