window.PP2 = {
	init: function () {
		var base = this,
			elDialogCarousel = $('div.dialog ul.carousel'),
			elDialogHeroCarousel = $('div.dialog-hero ul.carousel'),
			audio = {
				path: PP2.resources.sitePath + '/_ui/swf/SFXDispatcher.swf',
				width: '182',
				height: '44',
				version: '9.0.0',
				id: 'sound-target',
				expressInstall: false
			},
			flashvars = {},
			params = {
				bgcolor: '#000000',
				wmode: 'transparent',
				quality: 'high',
				allowscriptaccess: 'always'
			},
			i,
			ii;

		// remove no-js experience
		$(document.body).removeClass('no-js');

		// load in characters
		base.loadCharacters($('img.hero'), -300, 500, 250);
		base.loadCharacters($('div.platter img'), -100, 500, 250);
		base.loadCharacters($('div.dialog img.char'), -100, 500, 250);

		// set localized region
		if ($('body#page-lang').length) {
			new this.setRegion('ul#nav-region li a', 'pokemon-pokepark2-lang', 365);
		}

		// interstitial for external links
		new PP2.ModalWindow($('a[rel=external]'), {
			onModalOpen: function (obj, trigger, modal) {
				base.buildInterstitial(obj, trigger, modal);
			}
		});

		// Home Page media carousel
		if ($('body#page-home').length) {
			new PP2.Carousel($('div#featured-media ul.carousel li'), {
				elWrapper: $('div#featured-media'),
				arrows: true,
				onPanelLoad: function (el) {
					$(el).parent().find('object').remove();

					new PP2.embedVideo($(el).find('img'));
				}
			});
		}

		// Fun Stuff  carousels
		if ($('body#section-fun-stuff').length) {
			new PP2.Carousel($('div#section-wallpaper > ul > li'), {
				elWrapper: $('div#section-wallpaper'),
				arrows: true,
				onPaginationClick: function (el) {
					base.trackImageView($(el).find('img'));
				}
			});
			
			new PP2.Carousel($('div#section-video-gallery > ul > li'), {
				elWrapper: $('div#section-video-gallery'),
				contentSelector: 'p.caption',
				arrows: true,
				itemCount: true,
				itemCountSeparator: PP2.resources.itemCountSeparator,
				onPanelLoad: function (el) {
					$(el).parent().find('object').remove();

					new PP2.embedVideo($(el).find('img'));
				}
			});
		}

		// generic carousels throughout the site
		if (elDialogCarousel.length) {
			for (i = 0, ii = elDialogCarousel.length; i < ii; i++) {
				new PP2.Carousel($(elDialogCarousel[i]).find('li'), {
					slider: true,
					contentSelector: 'p.caption',
					arrows: true,
					panelSwitchDuration: 500,
					onPaginationClick: function (el) {
					base.trackImageView($(el).find('img'));
				}
				});
				
			}
		}

		// generic hero carousels throughout the site
		if (elDialogHeroCarousel.length) {
			for (i = 0, ii = elDialogHeroCarousel.length; i < ii; i++) {
				new PP2.Carousel($(elDialogHeroCarousel[i]).find('li'), {
					slider: true,
					contentSelector: 'p.largeCaption',
					arrows: true,
					panelSwitchDuration: 500,
					onPaginationClick: function (el) {
					base.trackImageView($(el).find('img'));
				}
				});
			}
		}

		$('body').append($('<div id="sound-target" />'));

		swfobject.embedSWF(audio.path, audio.id, audio.width, audio.height, audio.version, audio.expressInstall, flashvars, params);
	},

	// set region on element click
	setRegion: function (target, name, days) {
		var base = this,
			el = $(target),
			id,
			path;

		el.click(function(e) {
			e.preventDefault();

			id = $(this).attr('id');
			path = $(this).attr('href');

			PP2.utilities.cookie.destroy(name);

			PP2.utilities.cookie.set(name, id, days);

			document.location = path;
		});
	},

	buildInterstitial: function (obj, trigger, modal) {
		var base = this,
			elTitle = $('<h3 />').text(PP2.resources.interstitialTitleText),
			elText = $('<p />').text(PP2.resources.interstitialBodyText),
			elBtnWrapper = $('<ul class="nav" />'),
			elBtnCancel = $('<li />').append($('<a />').addClass('btn-red btn-five').text(PP2.resources.interstitialCancelText)),
			elBtnContinue = $('<li />').append($('<a />').addClass('btn-red btn-five').attr('href', trigger.href).attr('target', '_blank').text(PP2.resources.interstitialContinueText));

		elBtnCancel.click(function (e) {
			obj.hideModal();
		});

		elBtnContinue.click(function (e) {
			obj.hideModal();
		});

		elBtnWrapper.append(elBtnCancel, elBtnContinue);
		modal.append(elTitle, elText, elBtnWrapper);
	},

	// drop characters in from the top
	loadCharacters: function (target, distance, duration, interval) {
		var base = this,
			el = target,
			loadInterval,
			index = 0;

		for (i = 0, ii = el.length; i < ii; i++) {
			$(el[i]).data('top', $(el[i]).position().top);
			$(el[i]).css({
				opacity : 0,
				top : distance
			});
		}

		loadInterval = setInterval(function () {
			if (index < el.length) {
				$(el[index]).animate({
					opacity : 1,
					top : $(el[index]).data('top')
				}, duration, 'easeOutBack');

				index++;
			} else {
				clearInterval(loadInterval);
			}
		}, interval);
	},
	
	// returns name of image being viewed
	trackImageView: function (el) {
		var base = this;			
		
		if(el.attr('data-video') == undefined){
			
			mediaString = el.attr('src');
			mediaName = mediaString.substring( mediaString.lastIndexOf('/')+1, mediaString.length);
			_gaq.push(['_trackEvent', 'Screenshots', 'View', mediaName]);
		}
	},

	utilities: {

		// cookie interactions
		cookie: {
			set: function (name, value, days) {
				var expires,
					date = new Date();

				if (days) {
					date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
					expires = '; expires=' + date.toGMTString();
				} else {
					expires = '';
				}

				document.cookie = name + '=' + value + expires + '; path=/';
			},

			get: function (name) {
				var nameEQ = name + '=',
					ca = document.cookie.split(';'),
					c, i = 0, ii = 0;

				for (i = 0, ii = ca.length; i < ii; i++) {
					c = ca[i];

					while (c.charAt(0) === ' ') {
						c = c.substring(1, c.length);
					}

					if (c.indexOf(nameEQ) === 0) {
						return c.substring(nameEQ.length, c.length);
					}
				}
				return null;
			},

			destroy: function (name) {
				this.set(name, '', -1);
			}
		}
	},
	
	// Returns values of played/completed videos, sitewide. For use with GA
	flashReady: function(track) {
		var elFlash = document.getElementById(track.id);

		elFlash.addMediaEventListener('mediaOpen', 'PP2.flashStarted');
	},		

	flashStarted: function(track) {
		var elFlash = document.getElementById(track.id);		

		elFlash.removeMediaEventListener('mediaOpen', 'PP2.flashStarted');
		elFlash.addMediaEventListener('mediaPlayComplete', 'PP2.flashCompleted');
		
		var country = document.documentElement.lang,
			mediaString = track.src.url,
			mediaName = mediaString.substring( mediaString.lastIndexOf('/')+1, mediaString.length);

		_gaq.push(['_trackEvent', 'Videos', 'Play', mediaName+' '+country]);
	},	

	flashCompleted: function(track) {
		var elFlash = document.getElementById(track.id);		

		elFlash.removeMediaEventListener('mediaPlayComplete', 'PP2.flashCompleted');
		elFlash.addMediaEventListener('mediaOpen', 'PP2.flashStarted');
		
		var country = document.documentElement.lang,
			mediaString = track.src.url,
			mediaName = mediaString.substring( mediaString.lastIndexOf('/')+1, mediaString.length);

		_gaq.push(['_trackEvent', 'Videos', 'Complete', mediaName+' '+country]);
	}	
};

	

/*** Load On DOM Ready ***/

$(function () {
	PP2.init();
});
