IBSYS.using('ad.strategies');

IBSYS.ad.strategies.AdifyVideo = function(mergeData, sharedData, positionIndex) {
	var adifyID = IBSYS.propPath('IBSYS.ad.AdifyIDs.interactive.flashplayer');
	
	if (adifyID) {
		var m = mergeData.contentMeta;
		var randomKey1 = Math.round(Math.random() * 100000000); 
		var randomKey2 = new Date().getTime()/1000; 
		var keywords = [];
		/* 18 == 'urn:ibsys:content:'.length */
		keywords.push({coid: m.contentId.substring(18)});
		var ref = mergeData.useZone || sharedData.currentZone;
		if (ref != 'national' && ref != 'local') {
			keywords.push({referrer: ref});
		}
		keywords.push({vidcat: (m.category || m.topic || 'homepage')});
		var section = mergeData.section || pageProps.sectionpath || 'homepage';
		keywords.push({section: section});
		
		if (section.toLowerCase().replace(/[^a-z0-9]/g,'') != 'video') {
			/* Adify/Ad Ops need section0video keyword included for all prerolls in addition to actual section */
			keywords.push({section: 'video'});
		}
		
		keywords.push({pool: m.pool});
		
		for (var i=0; i<keywords.length; i++) {
			for (key in keywords[i]) {
				if (key && keywords[i][key]) {
					keywords[i] = (key + '0' + keywords[i][key]).replace(/[^A-Za-z0-9]/g,'');
				} else {
					IBSYS.log('Problem in AdifyVideo.js with keyword: ' + key);
					keywords.splice(i, 1);
				}
			}
		}

		var s = 'http://ad.afy11.net/ad?asId=' + adifyID + '&kd=' +
			keywords.join('%2C') + '&ct=15&enc=2&rand=' +
			(randomKey1 + 1) + '&rk1=' + randomKey1 + '&rk2=' + randomKey2;
		
		mergeData.element.setAdUrl(s, mergeData.contentMeta.contentId);
	} else {
		mergeData.element.setAdUrl('', mergeData.contentMeta.contentId);
	}
}

if (IBSYS.application.Application.getProperty('ads.defaultStrategy') == 'Adify') {
	IBSYS.ad.AdManager.setEventMapping('newMediaContent', {
		mapping: [{
			Strategy: {
				Name: 'AdifyVideo',
				Parameters: {
					contentMeta: {event: 'eventData'}
				}
			},
			Rules: [{
				Type: 'CheckValue',
				check: {position: 'type'},
				expect: 'asxProxy'
			},
			{
				Type: 'CheckValue',
				check: { event: 'eventData.showAds'},
				expect: true
			},{
				Type: 'CheckValue',
				check: {application: 'ads.defaultStrategy'},
				expect: 'Adify'
			}]
		}]
	});
	
	IBSYS.ad.AdManager.setEventMapping('playingVideoAd', {
		mapping: [{
			Strategy: {
				Name: "Adify",
				Parameters: {
					addtlKW: {comp: [{section: 'video'}]},
					randomKeys: {event: 'eventData.adifyRandomValues'},
					interactive: false,
					topic: {
				  	propPath: 'IBSYS.media.vidPlayModule.currentVideo.topic'
				  },
					useId: {
						propPath: 'IBSYS.media.vidPlayModule.currentVideo.coidValue'
					},
					section: {
						propPath: 'IBSYS.media.vidPlayModule.currentVideo.category'
					},
					getSect: false
				}
			},
			Rules: [{
				expect: "DOM",
				check: {position: "type"},
				Type: "CheckValue"
			},{
				Type: 'adPositionSizeIn',
				sizes: '300x250,160x600,728x90'
			}]
		}]
	});
	
	IBSYS.ad.AdManager.setEventMapping('videoAdSkipped', {
		mapping: [{
			Strategy: {
				Name: "Adify",
				Parameters: {
					interactive: false,
					topic: {
				  	propPath: 'IBSYS.media.vidPlayModule.currentVideo.topic'
				  },
					useId: {
						propPath: 'IBSYS.media.vidPlayModule.currentVideo.coidValue'
					},
					section: {
						propPath: 'IBSYS.media.vidPlayModule.currentVideo.category'
					},
					getSect: false
				}
			},
			Rules: [{
				expect: "DOM",
				check: {position: "type"},
				Type: "CheckValue"
			},{
				Type: 'adPositionSizeIn',
				sizes: '300x250,160x600,728x90'
			}]
		}]
	});
}