// Creating array of objects that store the keyword to look for (kwd) and program name to set (pg)
(function(){
	// Grab meta keyworkds tag if hidespbox_ exists in it
	var meta = $('meta[name=keywords][content*=hidespbox_]');
	s_package_type = window.s_package_type || '';
	
	// if a meta tag of that type exists and this page is not part of another package
	if ((meta[0]) && (s_package_type == '')) {
		
		// ===== EDIT ONLY THE DB BELOW WHEN ADDING NEW PROGRAMS =======
		// Create database of keywords, program names, package types, and optionally the page and module is that you want to pull in, and what div to place it into
		var db = {
//					iamsdog:{pg:'',pt:'',ajax_page:'/keeping-pet-healthy/sponsor_boxes_for_tts',ajax_id:'sb-1130-tap-03',where:'ContentPane20',live:true},
//					iamscat:{pg:'',pt:'',ajax_page:'/keeping-pet-healthy/sponsor_boxes_for_tts',ajax_id:'sb-1130-tap-04',where:'ContentPane20',live:true},					
				  };
				  
				  
		// ===== DO NOT EDIT PAST THIS LINE =======
		
		// Pull out keyword out of meta content and make sure it exists in database object
		var key = meta[0].content.match(/hidespbox_(\w*)/)[1];
		var entry = db[key];
		
		var preview = (document.location.href.indexOf('.preview.') > -1 || document.location.href.indexOf('.staging.') > -1) ? true : false;

		if(entry && (preview || entry.live)){
				// Immediately hide sponsor boxes using CSS, then empty them and the next spacer after page loads
				$("head").append('<style type="text/css"> .sponsorBox_right_rdr, .icm_wrapper {display: none;}  </style>');
			
				// Set global var no page view for omniture
				s_not_pageview = "y";
					
				// When page is ready, trigger the pageview
				$(function(){
					// Set sponsor brand to a substring of the value in the db object
					s_sponsor_brand = entry.pg.substring(0,entry.pg.lastIndexOf('_')).toLowerCase();
					
					// Set both props and s_vars to the values from db object
					s_md.prop28 = s_sponsor_brand;
					s_sponsor_program = entry.pg.toLowerCase();
					s_md.prop29 = s_sponsor_program; 
					
					// Hardcoded values for Package Type
					s_package_type = entry.pt;
					s_md.prop30 = s_package_type.toLowerCase();
					s_md.eVar30 = s_md.prop30;
					
					// Pull out prop45 out of the same variable (_asset) the beacon grabs it from
					if(_asset==""){_asset="default";}
					s_md.prop45 = "tts-" + key + "/" + _asset;
					
					wmdPageview(s_pagename);
			
					// Empty out the sponsor boxes and next spacer, just to be extra-careful, always done for these style programs
					$("div.sponsorBox_right_rdr").empty();
					$("div.icm_wrapper").empty();
					$("div.sponsorBox_right_rdr + div.moduleSpacer_rdr").remove();			
					 
					//  add new sponsor box or div to the page. Start with ajax in the entry page 
					if(entry.ajax_page) {
						$.get(entry.ajax_page, function(data){
							//Grab the link out of that page and drop it into our head in order to get sponsor box CSS on the page if it doesnt exist.
							var matches = data.match(/<link[^>]+>/gi);
							if (matches) {
								matches = matches.join('');
								$('head').append(matches);
							}

							// Grab the div
							var ajax_div = $(data).find('#'+entry.ajax_id);
							
							// In case it is a sponsor box, or these divs are marked hidden, add in a style to display them
							$("head").append('<style type="text/css"> .' + ajax_div.attr("class") + ' {display: block !important;} </style>');
							
							// Add in the div. html() only grabs stuff inside of that div. Here I'm recreating the container div. This is faster then cloning the element and removing it, and also better then .parent.html() in case there are more divs on the page we ajaxed that we do not want
							$('#' + entry.where).append('<div id="' + entry.ajax_id + '" class="' + ajax_div.attr("class") + '">' + ajax_div.html() + '</div>');
						});
					}
					
				});
		}
	}
})();
