(function($){

	$.extend({

		tlavideo: {

			marquee: function() {
				
			if (($('div#marquee').length)) {
		
				/** show all articles, overrides js free one article functionality **/
				$('div#marquee div.marquee-group div.article-sequence-group').show();
				
				/** add initial active class for the slideshow - pertains to banners & banner number **/
				$('div#marquee div.marquee-group div.article-sequence-group:first-child').addClass('active');
				
				/** if there's more than one group **/
				if (! $('div#marquee ul#marquee-group-name').children('li:only-child').length) {
				
					/** show the article group list, set the first child to active & make room for the info area **/
					$('div#marquee ul#marquee-group-name').show();
					$('div#marquee ul#marquee-group-name').children('li:first').addClass('active');
					$('div#marquee span.ul-corner').show();
				} else {
					$('div#marquee span.ul-corner').show().addClass('banner-mask-right');
				}
				
				/** show the first article group **/
				var firstArticle = $('div#marquee ul#marquee-group-name').children('li:first)').children('a').attr('class');
				var $firstArticleGroup = $('div#marquee div.' + firstArticle);
				$firstArticleGroup.addClass('current').show();
				
				/** check to see if the first gallery matches the first approved gallery **/
				if (!$firstArticleGroup.hasClass('first')) {
					$('div.marquee-group').removeClass('first');
					$firstArticleGroup.addClass('first');
				}
				
				/** create lpage article swap **/
				$('div#marquee-groups div.marquee-group').each(function(){
					$(this).append("<div class='lpage-article-swap'></div>");
					$(this).children('div.article-sequence-group').each(function(i){
						var articleID = $(this).children('div').attr('id');
						$(this).parent().children('div.lpage-article-swap').append("<span class='" + articleID + "'>" + (i + 1) + "</span>");
						$(this).parent().children('div.lpage-article-swap').children('span:eq(0)').addClass('active');
						$('div#marquee div.lpage-article-swap span:eq(0)').addClass('active');
					});
				});
				
				playSlideShow = setInterval('marqueeSlideShow();', 3000);
				
				$('div#marquee div.lpage-article-swap span').hover(function(){
					if (typeof(playSlideShow) != 'undefined') {
						clearInterval(playSlideShow);
					}
					
					/** remove any slideshow classes and make the hovered number active **/
					$('div#marquee div.current div.lpage-article-swap span').removeClass('active').removeClass('last-active').removeClass('second');
					$(this).addClass('active').css('cursor', 'pointer');
					
					/** figure out the selected banner  **/
					var articleNumber = $(this).attr('class');
					var articleNumberNew = articleNumber.replace(' active', '');
					var selectedArticle = 'div#marquee div.current div.article-sequence-group div#' + articleNumberNew + ', div#marquee div.current div.article-sequence-group div#' + articleNumberNew + '_2';
					
					/** hide all articles **/
					$('div#marquee div.current div.article-sequence-group div.lpage-article').css({
						opacity: 0.0
					}); 
					
					/**hide all articles parents get a reset z-index (for ie) & remove active class **/
					$('div#marquee div.current div.article-sequence-group').css({
						"z-index": 0
					}).removeClass('active'); 
					
					/** show the selected  banner **/
					$(selectedArticle).css({
						opacity: 1.0,
						"z-index": 10
					});
					
					/** give it's parent a higher z-index (for ie) **/
					$(selectedArticle).parent('div.article-sequence-group').css({
						"z-index": 10
					}); 
				
				}, function(){});
			
				$('div#marquee ul#marquee-group-name li a').hover(function(){
					
					/** remove any slideshow classes on the articles **/
					$('div#marquee div.article-sequence-group, div#marquee div.lpage-article-swap span').removeClass('active').removeClass('last-active').removeAttr('style').show();
					
					/** add the active class to each group **/
					$('div#marquee div.marquee-group div.article-sequence-group:first-child, div#marquee div.lpage-article-swap span:first-child').addClass('active');
					
					/** make sure the articles are visible **/
					$('div#marquee div.article-sequence-group div.lpage-article').removeAttr('style');
					
					/** if the slideshow is playing stop it **/
					if (typeof(playSlideShow) != 'undefined') {
						clearInterval(playSlideShow);
					}
					
					/** hide all groups and remove current class **/
					$('div#marquee div.marquee-group').hide().removeClass('current');
					
					/** figure out the new selected gallery **/
					var articleGroupId = $(this).attr('class');
					var selectedArticleGroup = 'div#marquee div.' + articleGroupId;
					
					/** show the selected gallery, add class current and highlight the new arrow */
					$(selectedArticleGroup).show().addClass('current');
					$('div#marquee ul#marquee-group-name li').removeClass('active');
					$(this).parent().addClass('active');
					
					playSlideShow = setInterval('marqueeSlideShow();', 3000);
					
					/** make sure the right corner is showing **/
					
					if (!$('div#marquee ul#marquee-group-name').children('li:only-child').length) {
						if ($('ul#marquee-group-name li:first').hasClass('active')) {
							$('div#marquee span.ul-corner').css('background', 'url(/skins/graphics/7.0/tlaraw-new/marquee/ul-corner-first.gif) 0 0 no-repeat');
						} else {
							$('div#marquee span.ul-corner').css('background', 'url(/skins/graphics/7.0/tlaraw-new/marquee/ul-corner.gif) 0 0 no-repeat');
						}
					}
					
				}, function(){});
				
				$('div#marquee ul#marquee-group-name li a').click(function(){return false;});
				
				
			}
				
			//allow jQuery chaining
			return this;
				
			}, //end marquee
			
			siteTabs: function(){
				$('ul#main-nav a, ul#main-nav li.beta-flag').hover(function(){
					$(this).oneTime(700, "hoverSiteTabs", function(){
						if ($(this).hasClass('beta-flag')) {
							$(this).children('span.tooltip').show();
							var tooltipWidth = $(this).children('span.tooltip').width();
							var tooltipHeight = $(this).children('span.tooltip').height();
							var newCSS = {height:tooltipHeight, width:tooltipWidth};
							$(this).css(newCSS);
							$(this).siblings().css('left', tooltipWidth - 51);
						} else {
							$(this).siblings('span.tooltip').show();
						}
					});
				}, function(){
					if ($(this).hasClass('beta-flag')) {
						$(this).children('span.tooltip').hide();
						$(this).removeAttr('style');
						$(this).siblings().removeAttr('style');
					} else {
						$(this).siblings('span.tooltip').hide();
					}
					$(this).stopTime("hoverSiteTabs");
				});
	
				//allow jQuery chaining
				return this;
				
			}, //end siteTabs
			
			formAutoFocus: function() {
				
				$('input#searchtext').focus();
				$('div#login input[name=email]').focus();
				
				//allow jQuery chaining
				return this;
			}
			
		}, /* end tlavideo */

		getURLParam: function(strParamName){
			var strReturn = "";
			var strHref = window.location.href;
			var bFound=false;

			var cmpstring = strParamName + "=";
			var cmplen = cmpstring.length;

			if ( strHref.indexOf("?") > -1 ) {
				var strQueryString = strHref.substr(strHref.indexOf("?")+1);
				var aQueryString = strQueryString.split("&");
				for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
					if (aQueryString[iParam].substr(0,cmplen)==cmpstring){
						var aParam = aQueryString[iParam].split("=");
						strReturn = aParam[1];
						bFound=true;
						break;
					}

				}
			}
			if (bFound==false) return null;
			return strReturn;
		}

	});

	$(function(){ /* onloads begin here */
		 
		$.tlavideo.siteTabs().marquee().vodScenes().windowTabs().suckerFish().rawBottom().polls().bigForm().hideRefineSearch().horizontalScrollFix().formAutoFocus();
		compactForms($('form'));
		syncToUser();
		$.tlavideo.updateVMB();
		var newOverlay = new PPMOverlay;
		newOverlay.init();
		lsideDropDowns();
		if ($('body.raw-portal').length) {
			$slide = $('div.models img:first');
			var rndNum = Math.floor(Math.random() * $slide.siblings().length);
			var $random = $slide.siblings().eq(rndNum);
			$random.addClass('active');
			$('body.raw-portal').find('div.models img').css('display', 'block');
			var play = setInterval( "slideShow($slide)", 5000 );
			$('div.compact label span').show();
		}
		
		/* remove movie trailer on details page */
		/****************************************/
		
		$('div#details div#film-preview').remove();
		
		//konami code
		if ($('body').hasClass('cult')) {
			function onKonamiCode(fn){
				var codes = (function(){
					var c = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65];
					onKonamiCode.requireEnterKey && c.push(13);
					return c;
				})(), expecting = function(){
					expecting.codes = expecting.codes || Array.apply({}, codes);
					expecting.reset = function(){
					expecting.codes = null;
					};
					return expecting.codes;
				}, handler = function(e){
					if (expecting()[0] == (e || window.event).keyCode) {
						expecting().shift();
						if (!expecting().length) {
							expecting.reset();
							fn();
						}
					}
					else {
						expecting.reset();
					}
				};
				window.addEventListener ? window.addEventListener('keydown', handler, false) : document.attachEvent('onkeydown', handler);
			}
			
			onKonamiCode.requireEnterKey = false; // True/false
			onKonamiCode(function(){
			    //alert('konami code is working');
				$('a#konami').trigger('click');
			});
	}
		
	}); /* onloads end here */
	
})(jQuery);

var articleInterval = new Array();
var articleIntervalCount = new Array();
var odBannerPos = 0;

function rotateArticle(seq) {
	var rowCount = 0;
	var curRow = 1;
	// figure out how many rows this SEQ has and set the display style
	while (document.getElementById("seq" + seq + "_row" + curRow)) {
		if (curRow == 1) {
			document.getElementById("seq" + seq + "_row" + curRow).style.display = "block";
		} else {
			document.getElementById("seq" + seq + "_row" + curRow).style.display = "none";
		}
		rowCount++;
		curRow++;
	}
	// set the rotation interval for this sequence
	articleInterval[seq] = window.setInterval("switchArticle(" + seq + "," + rowCount + ",'next')", 7000);
	articleIntervalCount[seq] = 0;
}

function switchArticle(seq,rowCount,action) {
	var curVisible = 0;
	var nextVisible = 0;
	// figure out which row is currently visible
	for (i=1; i <= rowCount; i++) {
		if (document.getElementById("seq" + seq + "_row" + i).style.display == "block") {
			curVisible = i;
			break;
		}
	}
	// based on current visible article, find the next article in the list to show
	if (action == 'next') {
		if (curVisible < rowCount) {
			nextVisible = curVisible + 1;
		} else if (curVisible == rowCount) {
			nextVisible = 1;
		}
	} else {
		if (curVisible <= rowCount && curVisible > 1) {
			nextVisible = curVisible - 1;
		} else if (curVisible == 1) {
			nextVisible = rowCount;
		}
	}
	// switch the display parameter on current and next visible articles
	document.getElementById("seq" + seq + "_row" + curVisible).style.display = "none";
	document.getElementById("seq" + seq + "_row" + nextVisible).style.display = "block";
	articleIntervalCount[seq]++;
	if (articleIntervalCount[seq] > 41) {
		skipArticle(seq,rowCount,'stop');
	}
}

function skipArticle(seq,rowCount,action) {
	if (action == 'next' || action == 'prev') {
		window.clearInterval(articleInterval[seq]);
		switchArticle(seq,rowCount,action);
		document.images['stopArticle' + seq].src = '../skins/graphics/icon_play.gif';
		//articleInterval[seq] = window.setInterval("switchArticle(" + seq + "," + rowCount + ",'next')", 7000);
		articleInterval[seq] = 0;
	} else if (action == 'stop') {
		if (articleInterval[seq]) {
			window.clearInterval(articleInterval[seq]);
			articleInterval[seq] = 0;
			document.images['stopArticle' + seq].src = '../skins/graphics/icon_play.gif';
		} else {
			articleInterval[seq] = window.setInterval("switchArticle(" + seq + "," + rowCount + ",'next')", 7000);
			articleIntervalCount[seq] = 0;
			document.images['stopArticle' + seq].src = '../skins/graphics/icon_pause.gif';
		}
	}
	return false;
}

function marqueeSlideShow() {
	var $activeArticle = $('div#marquee div.current div.active');
	var $nextArticle = $activeArticle.next('div.article-sequence-group');
	var $activeGroup = $('ul#marquee-group-name li.active');
	var $nextGroup = $activeGroup.next('li').length ? $activeGroup.next('li').children('a') : $('ul#marquee-group-name li:first a');
	var $activeNumber = $('div#marquee div.current div.lpage-article-swap span.active');
	var $nextNumber = $activeNumber.next('span');
	if ($nextArticle.length) {
		$activeArticle.addClass('last-active');
		$nextArticle.css({
			opacity: 0.0
		}).addClass('active').animate({
			opacity: 1.0
		}, 0, function(){
			$activeArticle.removeClass('active last-active');
		});
		$activeNumber.removeClass('active');
		$nextNumber.addClass('active');
	} else {
		$nextGroup.trigger('mouseenter');
		if (!$('div#marquee ul#marquee-group-name').children('li:only-child').length) {
			if ($('ul#marquee-group-name li:first').hasClass('active')) {
				$('div#marquee span.ul-corner').css('background', 'url(/skins/graphics/7.0/tlaraw-new/marquee/ul-corner-first.gif) 0 0 no-repeat');
			} else {
				$('div#marquee span.ul-corner').css('background', 'url(/skins/graphics/7.0/tlaraw-new/marquee/ul-corner.gif) 0 0 no-repeat');
			}
		}
	}
	
}
