Event.observe(window, 'load', dailysplice_pageloaded, false);

// Preload images.
if (document.images){
  var pic1 = new Image();
  pic1.src="/images/dailysplice-album-art.gif";
  var pic2 = new Image();
  pic2.src="/images/button/done_button.png";
  var pic3 = new Image();
  pic3.src="/images/list-item_background_hover.gif";
  var pic4 = new Image();
  pic4.src="/images/check-box_full.png";
  var pic5 = new Image();
  pic5.src="/images/check-box_empty.png";
}

function replaceBrokenImage(img, repl_src){
  if(typeof(img) != "undefined") {
    if(typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
      img.src = repl_src;
    }

    if (!img.complete) {
      img.src = repl_src;
    }
  }
}

function callInProgress (xmlhttp) {
  switch (xmlhttp.readyState) {
    case 1: case 2: case 3:
    return true;
    break;
    // Case 4 and 0
    default:
    return false;
    break;
  }
}

function showFailureMessage() {
  alert('uh oh, it looks like the network is down. Try again shortly');
}
// Register global responders that will occur on all AJAX requests

/* Register Ajax load indicator. */
Ajax.Responders.register({
  onCreate: function(request) {
    if ($('ajax-message') && Ajax.activeRequestCount > 0) {
      Element.show('ajax-message');
      var tempDiv=document.getElementById("no-ajax-wait-msg");
      if(!tempDiv){
        $('ajax-message').style.display = "block";
      }
    }
    request['timeoutId'] = window.setTimeout(
      function() {
      // If we have hit the timeout and the AJAX request is active, abort it and let the user know
        if (callInProgress(request.transport)) {
          request.transport.abort();
          showFailureMessage();
          // Run the onFailure method if we set one up when creating the AJAX object
          if (request.options['onFailure']) {
            request.options['onFailure'](request.transport, request.json);
          }
        }
      }, 10000 // Five seconds
    );
  },
  // end of onCreate

  onComplete: function(request) {
    if ($('ajax-message') && Ajax.activeRequestCount == 0) {
      Element.hide('ajax-message');
    }
    // Clear the timeout, the request completed ok
    window.clearTimeout(request['timeoutId']);
  }
});

var container;

function navigate_link_in_new_window(url) {
	if (url) window.open(url)
}

var infobox = new lightbox();

function media_playPodcastEp(ep_id) {
	media_playUrl("/play?ep_id=" + ep_id);
}

function media_playMyStation() {
	media_playUrl("/play");
}

function media_playStation(s_id, ep_id) {
	if(typeof(ep_id) != "undefined") {
		media_playUrl("/play?s_id=" + s_id + "&ep_id=" + ep_id);
	} else {
		media_playUrl("/play?s_id=" + s_id);
	}
}

function createTempDiv() {
	tmp = document.createElement('div');
	tmp.id = 'temp';
	tmp.style.display = 'none';
	bod = document.getElementsByTagName('body')[0];
	bod.appendChild(tmp);
}

function dailysplice_pageloaded() {
	feed_logos = document.images;
	for(var i=0; i<feed_logos.length; i++) {
		replaceBrokenImage(feed_logos[i], '/images/dailysplice-album-art.gif');
	}
	createTempDiv();
//	Event.observe(document.body, 'mousemove', UpdateCursorPositionDocAll, false);
}

function dailysplice_getResponseType(response) {
	var result = "ok";
	$('temp').innerHTML = response.responseText;
	var children = $A($('temp').childNodes);
	children.each(function(child) {
		if(child.id == "__search_results") { result = "search"; }
		else if(child.id == "error") { result = "error"; }
	});
	Element.remove('temp');
	createTempDiv();
	return result;
}

function dailysplice_processServerResponse(response, type) {
	if(type=="search") {
		$('MainZone').innerHTML = response.responseText;
	}
}

function dailysplice_ajaxFailed(response) {
   alert("Request to Server Failed!");
}

function dailysplice_reportError(response) {
	infobox.deactivate();
	infobox.activate();
	infobox.customDisplayLightbox();
	infobox.processInfo(response);
}

function dailysplice_updateFeedList(response) {
	var responseType = dailysplice_getResponseType(response);
	if( responseType == "error") {
		dailysplice_reportError(response);
	} else if( responseType == "search") {
		dailysplice_processServerResponse(response, responseType);
	} else if(responseType=="ok") {
		$("left_side").innerHTML = response.responseText;
		Sortable.create("feedlist", {constraint:'vertical', scroll:'ListItems', dropOnEmpty:true, onUpdate:function(){new Ajax.Request('/feeds/update_feed_order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize("feedlist")})}})
	}
}

function AddRemoveTag(item)
{
	infobox.deactivate();
	infobox.activate();
	new Ajax.Request("/user_tag/more_info", {method: 'get', onComplete: dailysplice_moreInfo, parameters: '_taginfo='+item});
}

function PopupSharePodcast(item)
{
	infobox.deactivate();
	infobox.activate();
	new Ajax.Request("/podcasts/popup_share_podcast", {method: 'get', onComplete: dailysplice_moreInfo, parameters: '_taginfo='+item});
}

function PopupShareEp(item)
{
	infobox.deactivate();
	infobox.activate();
	new Ajax.Request("/podcasts/popup_share_episode", {method: 'get', onComplete: dailysplice_moreInfo, parameters: '_taginfo='+item});
}

function dailysplice_moreInfo(response) {
	infobox.customDisplayLightbox('538', '350');
	infobox.processInfo(response);
}

function dailysplice_settingsStatus(contents){
	// Called when a user's dailysplice settings are updated.
	if( dailysplice_getResponseType(origRequest) == "error") {
	}
}

// show/hide header login box
function toggleDisplay(divName){
  var tempDiv=document.getElementById(divName);
  if(!tempDiv){
    return false;
  }
  if((tempDiv.style.display=="block")||(tempDiv.style.display=="")){
    tempDiv.style.display="none";
    return false;
  }else if(tempDiv.style.display=="none"){
    tempDiv.style.display="block";
    return true;
  }
}

function openLoginBox(){
  toggleDisplay('header-login-wrap');
  toggleDisplay('header-search-div');
  return false;
}
// End of show/hide header login box

function ShowDelImg(obj){
   var children = obj.childNodes;
   var img=children[1];
   var title=children[0];
   img.style.visibility="visible";
}

function HideDelImg(obj) {
   var children = obj.childNodes;
   var img=children[1];
   var title=children[0];
   img.style.visibility="hidden";
}

function ActiveSubNav(seq){
   var div1=document.getElementById("subNav1");
   var div2=document.getElementById("subNav2");
   var div3=document.getElementById("subNav3");
   if(seq=="o"){
     div2.style.display="none";
     div3.style.display="none";
     div1.style.display="block";
   }
   else if(seq=="t"){
     div1.style.display="none";
     div3.style.display="none";
     div2.style.display="block";
   }
   else if(seq=="s"){
     div1.style.display="none";
     div2.style.display="none";
     div3.style.display="block";
   } else if(seq=="m") {
     div1.style.display="none";
     div2.style.display="none";
     div3.style.display="none";
   }
}

function ActiveRegSub(subName) {
   var div1=document.getElementById(subName);
   div1.style.display="block";
}

function HideRegSub(subName) {
   var div1=document.getElementById(subName);
   div1.style.display="none";
}

function Redirect(url){
  window.location = url
}

// Start of div layer popup on mouse hover
// http://bontragerconnection.com/ and http://www.willmaster.com/
// Version: July 28, 2007
var cX = 0; var cY = 0; var rX = 0; var rY = 0;
function UpdateCursorPosition(e){ cX = e.pageX; cY = e.pageY;}
function UpdateCursorPositionDocAll(e){ if(event) {cX = event.clientX; cY = event.clientY;}}
if(document.all) { document.onmousemove = UpdateCursorPositionDocAll; }
else { document.onmousemove = UpdateCursorPosition; }
function AssignPosition(d, p_left, p_top) {
if(self.pageYOffset) {
	rX = self.pageXOffset;
	rY = self.pageYOffset;
	}
else if(document.documentElement && document.documentElement.scrollTop) {
	rX = document.documentElement.scrollLeft;
	rY = document.documentElement.scrollTop;
	}
else if(document.body) {
	rX = document.body.scrollLeft;
	rY = document.body.scrollTop;
	}
if(document.all) {
	cX += rX; 
	cY += rY;
	}
d.style.left = (cX - p_left) + "px";
d.style.top = (cY + p_top) + "px";
}
function HideContent(d) {
if(d.length < 1) { return; }
document.getElementById(d).style.display = "none";
}
function ShowContent(d, p_left, p_top) {
if(d.length < 1) { return; }
var dd = document.getElementById(d);
AssignPosition(dd, p_left, p_top);
dd.style.display = "block";
}
function ReverseContentDisplay(d) {
if(d.length < 1) { return; }
var dd = document.getElementById(d);
AssignPosition(dd, p_left, p_top);
if(dd.style.display == "none") { dd.style.display = "block"; }
else { dd.style.display = "none"; }
}
// End of div layer popup on mouse hover

function show_ratingdiv(d) {
if(d.length < 1) { return; }
var dd = document.getElementById(d);
dd.style.display = "block";
}

function hide_ratingdiv(d) {
if(d.length < 1) { return; }
var dd = document.getElementById(d);
dd.style.display = "none";
}

function imageError(theImage) {
  $(theImage).src = '/images/default-podcast-logo.gif';
}

function changeCheckBox(thisForm) {
	if(thisForm.remember_me.value == "1") {
		thisForm.remember_me_real.style.backgroundImage =  'url("/images/check-box_empty.png")';
		thisForm.remember_me.value = "0";
	} else {
		thisForm.remember_me_real.style.backgroundImage = 'url("/images/check-box_full.png")';
		thisForm.remember_me.value = "1";
	}
}

function changeThumb(thisForm) {
	if(thisForm.thumb_rating.value == "0") {
		thisForm.thumb_rating_real.style.backgroundImage = 'url("/images/thumb-up.gif")';
		document.getElementById("like_or_not").innerHTML="I Like It!!"
		thisForm.thumb_rating.value = "1";
	} else {
		thisForm.thumb_rating_real.style.backgroundImage =  'url("/images/thumb-down.gif")';
		document.getElementById("like_or_not").innerHTML="I Don't Like It!!"
		thisForm.thumb_rating.value = "0";
	}
}
/*
function checkAll(thisField) {
	alert(thisField.value);
	var boxes = cBoxes.getElementsByTagName("input");
	
	if(thisField.value == 1) {
		for (var i = 0, tEl; tEl = boxes[i]; i++) {
			if ( tEl.type == "checkbox") {
				tEl.checked = true;
			}
		}
	}else {
		for (var i = 0, tEl; tEl = boxes[i]; i++) {
			if ( tEl.type == "checkbox") {
				tEl.checked = false;
			}
		}
	}
	return false;
}*/

/*
Auto center window script- Eric King (http://redrival.com/eak/index.shtml)
Permission granted to Dynamic Drive to feature script in archive
For full source, usage terms, and 100's more DHTML scripts, visit http://dynamicdrive.com
*/

/*
pop up window function
Auto center window script- Eric King (http://redrival.com/eak/index.shtml)
Permission granted to Dynamic Drive to feature script in archive
For full source, usage terms, and 100's more DHTML scripts, visit http://dynamicdrive.com
*/

var win = null;
function NewWindow(mypage,myname,w,h,scroll){
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
settings =
'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
win = window.open(mypage,myname,settings)
}
/* end of pop up window function */
