
// popup
function Dancingopen(url) {window.open(url, 'extraplayer', 'height=1006,width=720,resizable=no,status=no,scrollbars=no,location=no,menubar=no,toolbar=no,left=30,top=30');}
function Pollopen(url) {window.open(url, 'extraplayer', 'height=300,width=400,resizable=no,status=no,scrollbars=no,location=no,menubar=no,toolbar=no,left=30,top=30');}
function Poll2open(url) {window.open(url, 'extraplayer', 'height=600,width=400,resizable=no,status=no,scrollbars=no,location=no,menubar=no,toolbar=no,left=30,top=30');}
function Popopen(url) {window.open(url, 'extraplayer', 'height=392,width=760,resizable=no,status=no,scrollbars=no,location=no,menubar=no,toolbar=no,left=30,top=30');}
function Vidopen(url) {window.open(url, 'xvideo', 'height=392,width=758,resizable=no,status=no,scrollbars=no,location=no,menubar=no,toolbar=no,left=30,top=30');}
function Picopen(url) {window.open(url, 'xpic', 'height=533,width=332,resizable=yes,status=no,scrollbars=no,location=no,menubar=no,toolbar=no,left=50,top=30');}
function Sendopen(url) {window.open(url, 'xsend', 'height=340,width=500,resizable=no,status=no,scrollbars=no,location=no,menubar=no,toolbar=no,left=100,top=100');}
function Contestopen(url) {window.open(url,'xcontest', 'height=580,width=610,resizable=yes,status=no,scrollbars=no,location=no,menubar=no,toolbar=no,left=50,top=10');}
function Contestopen2(url) {window.open(url,'xcontest', 'height=650,width=650,resizable=yes,status=no,scrollbars=yes,location=no,menubar=no,toolbar=no,left=50,top=10');}
function Contestopen3(url) {window.open(url,'xcontest', 'height=630,width=610,resizable=yes,status=no,scrollbars=no,location=no,menubar=no,toolbar=no,left=50,top=10');}
function Infoopen(url) {window.open(url,'xinfo', 'height=906,width=700,resizable=no,status=no,scrollbars=yes,location=no,menubar=no,toolbar=no,left=100,top=100');}
function Textopen(url) {window.open(url,'extracontent', 'resizable=yes,status=yes,scrollbars=yes,location=yes,menubar=yes,toolbar=yes');self.close();}
function Info2open(url) {window.open(url,'xinfo', 'height=612,width=900,resizable=no,status=no,scrollbars=yes,location=no,menubar=no,toolbar=no,left=100,top=100');}
function Vidpopopen(url) {window.open(url, 'xvideo', 'height=255,width=320,resizable=no,status=no,scrollbars=no,location=no,menubar=no,toolbar=no');}
function Spot(url) {window.open(url,'xspot', 'height=600,width=900,resizable=no,status=no,scrollbars=no,location=no,menubar=no,toolbar=no,left=100,top=60');}
function Popembed(url) {window.open(url, 'embedpopup', 'height=200,width=670,resizable=no,status=no,scrollbars=no,location=no,menubar=no,toolbar=no,left=30,top=30');}
function Videopopup(url) {window.open(url, 'videopopup', 'height=315,width=420,resizable=no,status=no,scrollbars=no,location=no,menubar=no,toolbar=no,left=30,top=30');}
function Genpopup(url,w,h) {window.open(url, 'xpopup', 'height='+h+',width='+w+',resizable=no,status=no,scrollbars=yes,location=no,menubar=no,toolbar=no,left=30,top=30');}

// search
(function($) {
var interval = null;
var checklist = [];
jQuery.elementReady = function(id, fn) {
	checklist.push({id: id, fn: fn});
	if (!interval) {
		interval = setInterval(check, jQuery.elementReady.interval_ms);
	}
	return this;
};

// Plugin settings
jQuery.elementReady.interval_ms = 23; // polling interval in ms

// Private function
function check() {
	var docReady = jQuery.isReady; // check doc ready first; thus ensure that check is made at least once _after_ doc is ready
	for (var i = checklist.length - 1; 0 <= i; --i) {
		var el = document.getElementById(checklist[i].id);
		if ( (el && (el.nextSibling || el.parentNode.nextSibling))|| docReady) {

			var fn = checklist[i].fn; // first remove from checklist, then call function
			checklist[i] = checklist[checklist.length - 1];
			checklist.pop();
			fn.apply(el, [$]);
		}
	}
	if (docReady) {
		clearInterval(interval);
		interval = null;
	}
};

})(jQuery);


// Remove IE6 image flickering
try {document.execCommand('BackgroundImageCache', false, true);}
catch(error) {}

jQuery.fn.extend({
	setSearch: function(options){
		var $form = jQuery(this),
			frmId = $form.attr('id'),
			frmClass = $form.attr('class'),
			$searchList = $form.find('ul'),
			$searchBox = $form.find('input[type=text]'), 
			$searchWrap = $form.parent(), 
			$searchDefault = $form.find('input[type=radio]:first'), 
			options = options || {},
			dTxt = options.defaultText || "What are you looking for?",
			dSrch = options.defaultSearch || "http://extratv.warnerbros.com/search/index.php",
			$searchFilter = $form.find("input[name='search']"),
			$searchPerPage = $form.find("input[name='per_page']"),
			srchStr;
		if(!$searchList[0] || $searchList[0] == undefined) return;
		function setDropDown(){
			if(!qW){
				var inputs = document.getElementById(frmId).getElementsByTagName('input');
				for(i=0;i<inputs.length;i++){
					var s = inputs[i];
					if (s.type == 'text') {
						var qW = s.clientWidth ? s.clientWidth : s.innerWidth;
					}
				}
			}
//			$searchList.css('width', qW);
			$searchBox.bind("click", function(){
				if ($searchBox.val() == dTxt) { $searchBox.val(""); }
				$searchList.toggle();
			});

		}
		function setForm(){
			$form.attr('action', dSrch);
			$searchFilter.val("all");
			$searchPerPage.val("10");
			$searchDefault.click();
			$searchBox.val(dTxt);
			$form.bind("submit", function(){
				srchStr = $searchBox.val();
				if (srchStr == dTxt) {$searchBox.val("");} 
				return true;
			});
			setDropDown();
		}
		function setFilter(el){
			switch( el.attr("value") ) {
				case "photos" :
				case "videos" :
					$searchPerPage.val("30");
					break;
				case "all" :
				case "articles" :
				default:
					$searchPerPage.val("10");
					break;
			}
			$searchFilter.val(el.attr("value"));
		}
		function setBinding(){
			var $rdBtns = $searchList.find('input');
			$rdBtns.each(function(){
				var $rd = jQuery(this);
				$rd.bind('click', function(){
					setFilter($rd);
				});
			});
		}
		function init(){
			setForm();
			setBinding();
		}
		init();
	}
 });

function initSearch(){
	jQuery.elementReady('frmSearch', function(){
		jQuery(this).setSearch();
	});
}

function hiddenText(iid,value,classname)
{
	document.getElementById(iid).value=value;
	document.getElementById(iid).className=classname;
}

function displayText(iid,value,classname)
{
	if(document.getElementById(iid).value =="")
	{
		document.getElementById(iid).value=value;
		document.getElementById(iid).className=classname;
	}
}

/*
start: sponsored link handlers
this will handle the adding of the tpixAdConfig.ord value
to the sponsored by links in featured areas and blog
*/
// adblock no likey the ord so we gots to make a new one, sometimes
//if (typeof(tpixAdConfig) == "undefined") {
    //var tpixAdConfig = {ord: Math.ceil(1+1E12*Math.random())};
//}

function addSponsoredLinksOrd(elm) {
    var href = elm.attr("href");
    //if (href.indexOf("?") == -1) href += "?";
    href += tpixAdConfig.ord;
    elm.attr("href", href);
}

function addSponsoredLinksPixelTracker(elm) {
    var href = elm.attr("src");
    //if (href.indexOf("?") == -1) href += "?";
    href += tpixAdConfig.ord;
    var tracker = '<img src="' + href + '" width="1" height="1" />';
    elm.append(tracker);
}

if (jQuery) {
    jQuery(document).ready(function(){
        jQuery(".sponsored_featured_link").each(function(idx){
            addSponsoredLinksOrd(jQuery(this));
        });
        jQuery(".sponsored_blog_link").each(function(idx){
            addSponsoredLinksOrd(jQuery(this));
        });
        jQuery(".sponsored_featured_img").each(function(idx){
            addSponsoredLinksPixelTracker(jQuery(this));
        });
        jQuery(".sponsored_blog_img").each(function(idx){
            addSponsoredLinksPixelTracker(jQuery(this));
        });
    });
}
/* end: sponsored link handlers */

/**
 * turns an arbitrary element into a hyperlink a tag
 **/
jQuery.fn.tpclickable = function() {
	return this.each(function() {
		var e = jQuery(this);
		var href = jQuery(this).attr('href');
		var target = jQuery(this).attr('target');
		var w = 1024;
		var h = 768;	

		e.click(function() {
			if( target == "_blank" || target == "blank" ) {
				Genpopup( href, w, h )
			} else {
				window.location = href;
			}
		});
	
		e.hover(function() {
			window.status = href;
		}, function() {
			window.status = ''
		});
	
		e.addClass('anchor');	
	
	});
};

jQuery(document).ready(function() {
	jQuery('#inline embed').each(function(i) {
		jQuery(this).hide();
		jQuery('<div id="ds2kaltura_' + i + '"></div>').insertBefore(jQuery(this));
		var pattern = /\?mediaKey\=([a-zA-Z0-9-]+)/;
		var mediaKey = pattern.exec(jQuery(this).attr('src'))[1];
		ds2kaltura(i, mediaKey);
		});
	});
	
	ds2kaltura = function(index, mediaKey) {
		var lookupURL = 'http://apps.telepixtv.com/ds2kaltura/getEntryId.php?site=extra&mediaKey='+mediaKey+'&callback=?';
		$.getJSON(lookupURL, function(data) {
		var entryId = data.entryId;
		var kalturaCode = '<object id="kaltura_player_1310413985" name="kaltura_player_1310413985" type="application/x-shockwave-flash" allowFullScreen="true" allowNetworking="all" allowScriptAccess="always" height="333" width="400" bgcolor="#000000" xmlns:dc="http://purl.org/dc/terms/" xmlns:media="http://search.yahoo.com/searchmonkey/media/" rel="media:video" resource="http://www.kaltura.com/index.php/kwidget/cache_st/1310413985/wid/_594231/uiconf_id/5069622/entry_id/'+entryId+'" data="http://www.kaltura.com/index.php/kwidget/cache_st/1310413985/wid/_594231/uiconf_id/5069622/entry_id/'+entryId+'"><param name="allowFullScreen" value="true" /><param name="allowNetworking" value="all" /><param name="allowScriptAccess" value="always" /><param name="bgcolor" value="#000000" /><param name="flashVars" value="&" /><param name="movie" value="http://www.kaltura.com/index.php/kwidget/cache_st/1310413985/wid/_594231/uiconf_id/5069622/entry_id/'+entryId+'" /><a href="http://corp.kaltura.com">video platform</a> <a href="http://corp.kaltura.com/video_platform/video_management">video management</a> <a href="http://corp.kaltura.com/solutions/video_solution">video solutions</a> <a href="http://corp.kaltura.com/video_platform/video_publishing">video player</a> <a rel="media:thumbnail" href="http://cdnbakmi.kaltura.com/p/594231/sp/59423100/thumbnail/entry_id/'+entryId+'/width/120/height/90/bgcolor/000000/type/2"></a> <span property="dc:description" content="Successful restaurateur and TV host Guy Fieri reveals three simple rules to follow when dieting. Check it out!"></span><span property="media:title" content="Food Network′s Guy Fieri Dishes Diet Advice"></span> <span property="media:width" content="400"></span><span property="media:height" content="333"></span> <span property="media:type" content="application/x-shockwave-flash"></span> </object>';
		jQuery('#ds2kaltura_'+index).html(kalturaCode);
		jQuery('#ds2kaltura_'+index).next().hide();
	});
}

