var FX = {
	GridNav: {
		init: function() {
			Event.observe($$('#watch-episodes a').first(),'click',function(el){
                  this.onclick = function() { return false; };
									if (this.hasClassName("active")) {
										FX.GridNav.watch_episodes.hide();
                  } else {
										FX.GridNav.watch_episodes.show();
										FX.GridNav.shows.hide();
                  }
              });
              Event.observe($$('#menu-flyout div.close a').first(),'click',function(){
									FX.GridNav.watch_episodes.hide();
              });

              Event.observe($$('#shows a').first(),'click',function(el){
                  this.onclick = function() { return false; };
									if (this.hasClassName("active")) {
										FX.GridNav.shows.hide();
                  } else {
										FX.GridNav.shows.show();
										FX.GridNav.watch_episodes.hide();
                  }
              });
              Event.observe($$('#shows-flyout div.close a').first(),'click',function(){
                 FX.GridNav.shows.hide();
              });
		},
		watch_episodes: {
			show:function() {
				Effect.Appear('menu-flyout', {duration:.3});
				$$('#watch-episodes a').first().addClassName("active");
			},
			hide:function() {
				Effect.Fade('menu-flyout', {duration:.3});
				$$('#watch-episodes a').first().removeClassName("active");
			}
		},
		shows: {
			show:function() {
				Effect.Appear('shows-flyout', {duration:.3});
				$$('#shows a').first().addClassName("active");
			},
			hide:function() {
				Effect.Fade('shows-flyout', {duration:.3});
				$$('#shows a').first().removeClassName("active");
			}
		}
	},
	NowPlaying: {

	},
	Shows: {
		init: function() {
			$$('div.info'). invoke('hide');
			$$('div.image-bg').each(function(el){
			this.on = true;
				el.observe('mouseover',function(){
					$$('div.info').each(function(info){
						if (info.style.display != "none") {
							info.fade({duration:.3, fps:60});
							if (info.next('div.showtime') != null) {
								info.next('div.showtime').appear({duration:.3, fps:60});
							}
						}
					});
					this.next('div.info').appear({duration:.3, fps:60});
					if (this.next('div.showtime') != null) {
						this.next('div.showtime').fade({duration:.3, fps:60});
					}
				});
			});



			// -------------------------------------
			// 	WATCH EPISODES DROPDOWN FUNCTIONS
			// -------------------------------------

			//Handle the Watch Episodes nav item
			$('gn-watch-episodes').observe('click',function(){
				if( $('episodes').hasClassName('closedNav') ) {
					Effect.Appear('episodes',
					{duration:.3,
					afterFinish: function(effect)
						{
							$('savior1').show();
							$('episodes').addClassName('openNav');
							$('gn-shows').style.zIndex = 500;
						}
					});

					$('episodes').removeClassName('closedNav');
				}
			});

			// On MouseOut close the episode dropdown
			$('episodes').onmouseout = function(e){
				if (!e) var e = window.event;
				var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
				while (reltg.tagName != 'BODY'){
					if (reltg.id == this.id){return;}
					reltg = reltg.parentNode;
				}
				if( $('episodes').hasClassName('openNav') ) {
					// hide menu
					$('savior1').hide();
					$('episodes').removeClassName('openNav');
					$('episodes').addClassName('closedNav');
					Effect.Fade('episodes',{duration:.2});
					$('gn-shows').style.zIndex = 100;
					$('gn-watch-episodes').style.zIndex = 100;

				}
			}

			// CLOSE BUTTONS EPISODE DROPDOWN
			$('episodes-close').observe('click',function(){
				if( $('episodes').hasClassName('openNav') ) {
					$('savior1').hide();
					$('gn-shows').style.zIndex = 100;
					$('gn-watch-episodes').style.zIndex = 100;
					$('episodes').removeClassName('openNav');
					$('episodes').addClassName('closedNav');
					Effect.Fade('episodes',{duration:.2});
				}
			});

			// -------------------------------
			// 	SHOWS DROPDOWN FUNCTIONS
			// -------------------------------

			//Handle the Shows nav item
			$('gn-shows').observe('click',function(){
				if( $('shows-titles').hasClassName('closedNav') ) {
					Effect.Appear('shows-titles',
					{duration:.3,
					afterFinish: function(effect)
						{
							$('savior2').show();
							$('shows-titles').addClassName('openNav');
							$('gn-watch-episodes').style.zIndex = 100;
						}
					});

					$('shows-titles').removeClassName('closedNav');
				}
			});

			// On MouseOut close the shows dropdown
			$('shows-titles').onmouseout = function(e){
				if (!e) var e = window.event;
				var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
				while (reltg.tagName != 'BODY'){
					if (reltg.id == this.id){return;}
					reltg = reltg.parentNode;
				}
				if( $('shows-titles').hasClassName('openNav') ) {
					// hide menu
					$('savior2').hide();
					$('shows-titles').removeClassName('openNav');
					$('shows-titles').addClassName('closedNav');
					$('gn-shows').style.zIndex = 100;
					$('gn-watch-episodes').style.zIndex = 100;
					Effect.Fade('shows-titles',{duration:.2});
				}
			}

			// CLOSE BUTTONS SHOWS DROPDOWN
			$('gn-shows-close').observe('click',function(){
				if( $('shows-titles').hasClassName('openNav') ) {
					$('savior2').hide();
					$('gn-shows').style.zIndex = 100;
					$('gn-watch-episodes').style.zIndex = 100;
					$('shows-titles').removeClassName('openNav');
					$('shows-titles').addClassName('closedNav');
					Effect.Fade('shows-titles',{duration:.2});
				}
			});
		}
	},

	Movies: {
		LoadMovie: function(){
			$('player').show();
		}
	},

	/* HANDLE POPUPS FOR AGE GATE*/

	Popups:{
		setTransparency: function(){
			$('popup-wrapper').setOpacity(0.95);
		},

		launchPopUp: function(el) {
			FX.Popups.goModal();
			element = $(el);
			element.appear({ duration: 0.3, from: 0, to: 1 ,afterFinish: FX.Popups.setTransparency});
			element.addClassName('open');
			// function to center the popup on screen
			var centerPopUp = function(){
				var thePopup = element;
				Element.clonePosition(thePopup, $('age-verification'), {offsetLeft: 175, offsetTop: 50});
		 		if(!thePopup.hasClassName('shadow')){
		 			thePopup.addClassName('shadow');
		 		}
			}

			// center
			centerPopUp();
			// keep popup in center while resizing
			Event.observe(window, 'resize', centerPopUp);

			// CHECK TO SEE IF POP CLOSE BUTTON IS CLICKED
			$('close-popup-btn').observe('click',function(){
				// close popup
				this.up(1).fade({ duration: 0.3, from: 1, to: 0 });
				FX.Popups.stopModal();
				this.up(1).removeClassName('open');
			});
		},

		goModal: function(){
	        var d = new Element('div', { 'class': 'modal_screen', id:'modal_layer'});
	        d.setOpacity(0.8);
	        document.body.appendChild(d);
	    },

		stopModal: function(){
			$('modal_layer').remove();
		}
	},

	Verify: {
		/*
		LoadEpisode: function(episode_id) {
			this.episode_id = episode_id;
			this.jar = new CookieJar({
				expires:30* 1000 * 60 * 60 * 24,   // 30 days
				path: '/'
			});

			if((ep_id_is_sf == 1)&&(age_gate != "YES")&&(first_play)){
				LoadExtra(default_clip_ID, default_xml);
				return;
			}


			if((this.jar.get('age') == null)&&(ageNotRequired != true)){ //Cookie has not been previously set and age verification not required.
				//	alert("no cookie");
				//$('player').hide();
				$('age-verification').show();
				$('age-verification-form').show();
			} else { //The cookie has been previously set
				if (FX.Verify.isVerified()){ //Check if the cookie is verified or not
					//alert("verified");
					var verified = true;
					$('age-verification').hide();
					$('age-verification-failed').hide();
					$('player').show();
					//if the episode exists and they didn't deep-link to a short form clip
					if (this.episode_id != null && this.episode_id != ""){
						LoadEpisode(this.episode_id);
						//mn_clipID = this.episode_id;
					}
				} else { //They're not verified

					//if page does not need to check age verification
					if(ageNotRequired == true)
					{
						$('age-verification').hide();
						$('age-verification-failed').hide();
						$('player').show();
						//if the episode exists and they didn't deep-link to a short form clip
						if (this.episode_id != null && this.episode_id != ""){
							LoadEpisode(this.episode_id);
							//mn_clipID = this.episode_id;
						}
					}
					else
					{
						//	alert("not verified");
						hidePlayer();
						//$('player').hide();
						$('age-verification').show();
						$('age-verification-form').hide();
						$('age-verification-failed').show();
					}
				}
			}
			first_play = false;
		},

		LoadCleanEpisode : function(){
		//	alert("loading clean");
			first_play = false;
			LoadExtra(clean_clip_ID, clean_xml);
		},

		prelimCheck: function(episode_id) {
			//this.episode_id = episode_id;
			this.jar = new CookieJar({
				expires:30* 1000 * 60 * 60 * 24,   // 30 days
				path: '/'
			});
			//$('episode_id').value = episode_id;
			//Check if we already have a cookie
			//jar = new CookieJar();
			myjar = this.jar.get('age');
			if(this.jar.get('age') == null){ //Cookie has not been previously set
			age = {verified:false};
				this.jar.put('age',age);
				$('age-verification-form').show();
			}else{
				return (myjar.verified);
			}

		},
		*/
		Age: function() {
		//this.episode_id = episode_id;
			var min_age = 18;
			if($('age_month').selectedIndex == 0){
				FX.Popups.launchPopUp('popup-wrapper');
				$$('p.error-message')[0].update("Please select a valid birth month");
				return false;
			}
			if ($('age_day').selectedIndex == 0 ){
				FX.Popups.launchPopUp('popup-wrapper');
				$$('p.error-message')[0].update("Please select a valid birth day");
				return false;
			}

			var month = parseInt($('age_month').value) - 1;
			var day   = parseInt($('age_day').value);
			//var year  = parseInt($('age_year').value);
			var year  = $('age_year').value;
			/*
			var pattern = /\d{4}-\d{4}/;

			if(pattern.test(year)){
				alert('valid date')
			}else{
				alert('invalid date')
			}
			*/
			if ($('age_year').selectedIndex == 0 || isNaN(year)){
				FX.Popups.launchPopUp('popup-wrapper');
				$$('p.error-message')[0].update("Please select a valid birth year");
				return false;
			}

			year = parseInt(year);

			var bdate = new Date((year + min_age), month, day);

			//alert('test'+bdate)

			var today = new Date();
			if ( (today.getTime() - bdate.getTime()) < 0) {
				//Not old enough
				//Set cookie be UNverified
				FX.Verify.setUnverified();
				$('age-verification-form').hide();
				$('age-verification-failed').show();
			} else {
				//Yep, old enough to view
				//Set cookie with 30 day expiration
				//LoadEpisode(this.episode_id);

				FX.Verify.setVerified();
				$('player').show();
				$('age-verification').hide();
				FDMVideo.playCurrentTitle();

			}
			return false;
		},
		/*
		isVerified: function(){
			if (show_count > 0){
				myjar = this.jar.get('age');
				if(myjar) return (myjar.verified);
				return null;
			}else{
				//FX.Verify.setVerified();
				return true;
			}
		},
		checkVerification: function(){
			this.jar = new CookieJar({
					expires:30* 1000 * 60 * 60 * 24,   // 30 days
					path: '/'
				});
			myjar = this.jar.get('age');
			if (myjar == null){
				age = {verified:false};
				return null;
			}
			return (myjar.verified);
		},
		checkAge: function(){
			//$('player').hide();
			$('age-verification').show();
			$('age-verification-form').show();
		},
		*/
		setVerified: function() {
		this.jar = new CookieJar({
				expires:30* 1000 * 60 * 60 * 24,   // 30 days
				path: '/'
			});
			//alert("setting verified");
			age = {verified:true};
			this.jar.put('age',age);
		},
		setUnverified: function() {
		this.jar = new CookieJar({
				expires:30* 1000 * 60 * 60 * 24,   // 30 days
				path: '/'
			});
			//alert("setting UNverified");
			age = {verified:false};
			this.jar.put('age',age);
		}
	}
};
